Author: Daniil Krizhanovskiyi
Date: September 2024
Blockchain-on-Java is a basic blockchain system built using Java. The project supports two consensus algorithms: Proof of Work (PoW) and Proof of Stake (PoS). The blockchain is decentralized, allowing multiple nodes to communicate through a peer-to-peer (P2P) network, propagate blocks, and process transactions securely.
The project follows a modular architecture adhering to SOLID principles for maintainability and scalability.
blockchain-on-java
├── pom.xml # Maven configuration file
├── README.md # Project documentation
├── src
│ ├── main
│ │ └── java
│ │ └── com
│ │ └── example
│ │ └── blockchain
│ │ ├── blockchain
│ │ │ ├── Block.java # Core blockchain block class
│ │ │ ├── Blockchain.java # Blockchain management class
│ │ ├── consensus
│ │ │ ├── Consensus.java # Consensus algorithm interface
│ │ │ ├── PoWConsensus.java # Proof of Work consensus implementation
│ │ │ ├── PoSConsensus.java # Proof of Stake consensus implementation
│ │ ├── cryptography
│ │ │ ├── CryptoUtil.java # Utility for cryptographic functions (signing, hashing)
│ │ │ ├── StringUtil.java # SHA-256 hashing utility
│ │ ├── network
│ │ │ ├── Node.java # Peer-to-peer node implementation
│ │ │ ├── P2PNetwork.java # Network management for nodes
│ │ ├── transactions
│ │ │ ├── Transaction.java # Transaction representation
│ └── test
│ └── java
│ └── com
│ └── blockchain
│ ├── BlockTest.java # Unit tests for the Block class
│ ├── BlockchainTest.java # Unit tests for the Blockchain class
│ ├── ConsensusTest.java # Unit tests for PoW and PoS consensus mechanisms
│ ├── TransactionTest.java # Unit tests for the Transaction classThe Blockchain.java class manages the blockchain, which includes adding blocks, validating the chain's integrity, and mining blocks according to the consensus algorithm in use.
The Block.java class represents a block in the blockchain. It contains:
- A list of transactions.
- A timestamp.
- A nonce for mining.
- A hash generated using SHA-256.
The Transaction.java class represents a transaction between two entities in the blockchain. It includes:
- Sender: The sender's address.
- Recipient: The recipient's address.
- Amount: The value being transferred.
PoWConsensus.java implements the Proof of Work algorithm. Miners must solve a cryptographic puzzle to create a new block by finding a nonce that satisfies the block's hash difficulty.
PoSConsensus.java implements the Proof of Stake algorithm, where validators are selected to mine blocks based on the amount of cryptocurrency they hold (their stake).
StringUtil.java provides SHA-256 hashing functionality used to generate block and transaction hashes.
CryptoUtil.java implements digital signatures using RSA, allowing transactions to be securely signed by the sender and verified by others.
Node.java represents a node in the blockchain's P2P network. Nodes can:
- Connect to peer nodes.
- Broadcast transactions and blocks.
- Receive transactions and blocks from other nodes.
P2PNetwork.java manages the decentralized network of nodes, allowing them to communicate and synchronize the blockchain.
- Java 17 or later
- Maven for dependency management
To build the project, run the following command:
mvn clean installTo run the blockchain simulation, navigate to Blockchain.java in the src/main/java/com/example/blockchain/blockchain/ directory and execute it.
The project includes comprehensive unit tests to ensure correctness. Tests cover blockchain functionality, consensus mechanisms, transaction handling, and networking.
To run the tests:
mvn testImplement smart contracts on top of the blockchain for automated execution of agreements.
Introduce Layer-2 solutions, such as sharding, to improve the scalability of the blockchain.
Add ZKPs to improve privacy and allow for the validation of transactions without revealing details.
Blockchain-on-Java is a foundational project designed to demonstrate core blockchain principles. It can be extended with advanced features such as smart contracts, dynamic consensus adjustments, and improved cryptography to support real-world use cases.
This project is licensed under the MIT License - see the LICENSE file for details.