A lightweight blockchain node client built in Rust. This client can participate in consensus, validate transactions, relay data between peers, and synchronize with other nodes on the network.
- Blockchain Basics: Implements a simple blockchain with transaction validation and block production.
- Peer-to-Peer Networking: Nodes communicate using a peer-to-peer network to share and synchronize the blockchain.
- Blockchain Synchronization: Newly connected nodes synchronize with peers to ensure they have the latest blockchain state.
- Rust: Leveraging Rust's performance and memory safety features for high efficiency.
- Rust: Make sure you have Rust installed. You can download it from rust-lang.org.
- Clone the repository:
git clone https://github.com/DamianFigiel/lightweight-blockchain-rust.git cd blockchain_node_client
- Build the project:
cargo build --release
To start a node, specify the port and an optional peer port. The first node can be started without a peer, and subsequent nodes can connect to it.
cargo run -- --port 3000
This starts a blockchain node on port 3000.
cargo run -- --port 3001 --peer-port 3000
This starts a second node on port 3001 and connects it to the node on port 3000. Node 2 will synchronize its blockchain with Node 1 upon startup.
--port <PORT>
: Specifies the port to bind the node to. Defaults to 3000.--peer-port <PEER_PORT>
: Specifies an optional peer port for connecting to another node. If provided, the node will synchronize with the peer.
.
├── src
│ ├── blockchain.rs # Blockchain logic and Proof of Work consensus
│ ├── network.rs # Networking and peer-to-peer communication
│ ├── block.rs # Core block data structures (Block, Transaction)
│ └── main.rs # Entry point and CLI argument parsing
└── Cargo.toml # Project dependencies
-
Blockchain: Each node maintains a blockchain with transactions. Blocks are mined using a simple PoW algorithm, where the difficulty is based on finding a hash with a specific number of leading zeros.
-
Networking: Nodes communicate using TcpStream and TcpListener from the tokio asynchronous runtime. Each node can connect to peers and request their blockchain for synchronization.
-
Synchronization: When a new node connects to a peer, it requests the current blockchain. If the peer’s blockchain is longer and valid, the new node replaces its local blockchain with the synchronized one.
- Run Node 1: Start the first node and let it produce a few blocks.
- Run Node 2: Start a second node with --peer-port to connect it to Node 1.
- Observe Synchronization: Node 2 will synchronize with Node 1, adopting the longer blockchain.
- Transaction Pool: Implement a pool for handling multiple pending transactions.
- Enhanced Consensus Mechanism: Upgrade from Proof of Work to other consensus algorithms, like Proof of Stake.
- Improved Error Handling: Add more robust error handling and reconnection logic for networking.
This project is licensed under the MIT License. See the LICENSE file for details.