This project is a simple implementation of a blockchain written in Rust. Actually, this project is a Rust rewrite of the Naivechain project. It includes basic functionalities such as creating a blockchain, adding blocks, and a peer-to-peer network for sharing blocks between nodes. This implementation does not include a consensus protocol.
The project is divided into several modules:
main.rs: The entry point of the application. It sets up the HTTP server and the peer-to-peer network.api.rs: Handles API requests for interacting with the blockchain, including retrieving and mining blocks.engine.rs: Manages the blockchain's internal logic and communication between nodes.chain.rs: Defines theBlockandChainstructures and implements the logic for creating and validating blocks.net.rs: Configures the peer-to-peer network usinglibp2pand manages message transmission between nodes.
- Uses the
actix_webframework to set up an HTTP server for handling API requests. - Parses command-line arguments using
clap. - Initializes the blockchain with a genesis block.
- Configures the network and starts the engine to handle blockchain operations.
- Defines the
ApiStatestruct, which holds the blockchain and transmission handlers. - Provides API endpoints for:
- Retrieving the current blockchain (
/blocks/get). - Mining a new block (
/blocks/mine). - Viewing peers (
/peers). - Adding new peers (
/addpeer).
- Retrieving the current blockchain (
- Manages the blockchain logic, including handling incoming P2P messages.
- Ensures the local blockchain is up-to-date by responding to and sending messages across the network.
- Defines the
Blockstruct with fields likeindex,previous_hash,timestamp,data, andhash. - Implements methods for creating and validating blocks, as well as managing the blockchain's state.
- Configures the P2P network using
libp2p, enabling nodes to discover each other via mDNS and communicate using the GossipSub protocol. - Manages incoming and outgoing P2P messages, ensuring blocks are shared across nodes.
- Rust (latest stable version)
- Cargo (Rust package manager)
- Clone the repository:
$ git clone <repository-url>
$ cd naivechain-rs- Build the project:
$ cargo build- Run the application:
$ cargo run -- --port <PORT_NUMBER>