- Overview
- Key Features
- Getting Started
- Client Interaction
- Architecture
- Technical Details
- Ecosystem
- Future Improvements
- License
- Contributing
MockChain is a modular blockchain implementation written in Rust that demonstrates fundamental blockchain concepts while providing a practical framework for experimentation. This project features a pluggable consensus mechanism, transaction management, and a gRPC API for external interaction.
The blockchain supports different consensus algorithms through a trait-based plugin system:
- Proof of Work (PoW): A hashrate-based consensus where miners compete to solve computational puzzles
- Proof of Stake (PoS): A consensus mechanism that selects validators based on their economic stake
The consensus system is designed to be extensible:
pub trait Consensus: Send + Sync {
fn generate_block(&self, index: u64, transactions: Vec<Transaction>, previous_hash: String) -> Block;
fn validate_block(&self, block: &Block, previous_hash: &str) -> bool;
fn start(&self, blockchain: Arc<Mutex<Blockchain>>);
fn name(&self) -> &str;
}
Transactions are cryptographically secured using:
- ECDSA Signatures: Using the secp256k1 curve (the same as Bitcoin)
- SHA-256 Hashing: For transaction and block integrity
Each transaction contains:
- Sender address (public key)
- Recipient address
- Amount
- Timestamp
- Digital signature
The blockchain exposes a gRPC interface for client applications, defined in protobuf:
submit_transaction
: Send tokens from one address to anotherget_balance
: Query an address's current balancerequest_faucet
: Request test tokens for development
Each block contains:
- Block index
- Timestamp
- List of transactions
- Previous block's hash
- Current block's hash
- Nonce (used in PoW)
- Miner's address
- Test Faucet: Easily obtain test tokens for development
- Async Runtime: Built on tokio for concurrent operation
- Structured Logging: Comprehensive logging for troubleshooting
- Rust (1.54.0+)
- Protobuf compiler (
protoc
)
-
Clone the repository:
git clone https://github.com/0xsourav/mockchain.git cd mockchain
-
Build the project:
cargo build --release
Start a blockchain node with default settings:
RUST_LOG=info cargo run --release
You can choose different consensus mechanisms by modifying the following line in main.rs
:
// For Proof of Work with difficulty 3
let consensus_type = ConsensusType::ProofOfWork { difficulty: 3 };
// For Proof of Stake with minimum stake of 1000
// let consensus_type = ConsensusType::ProofOfStake { min_stake: 1000 };
Mockallet is the official wallet implementation for this blockchain. It provides a user-friendly way to interact with the MockChain network.
Features of Mockallet:
- Key management (generation and storage)
- Balance checking
- Transaction creation and signing
- Faucet interaction for test tokens
Check out the Mockallet repository for installation and usage instructions.
The blockchain node exposes a gRPC server on [::1]:50051
by default.
// Example gRPC client code
let mut client = BlockchainServiceClient::connect("http://[::1]:50051").await?;
let request = Request::new(FaucetRequest {
address: "your_public_key_here".to_string(),
});
let response = client.request_faucet(request).await?;
println!("Response: {:?}", response);
let mut client = BlockchainServiceClient::connect("http://[::1]:50051").await?;
// Create and sign a transaction
let tx = ProtoTransaction {
from: sender_public_key,
to: recipient_address,
amount: 100,
timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs(),
signature: signature_bytes,
};
let request = Request::new(tx);
let response = client.submit_transaction(request).await?;
The system is designed with the following components:
- Block: Contains transactions and chain metadata
- Transaction: Represents a transfer of value with cryptographic proof
- Blockchain: Manages the chain state and transaction pool
- Consensus: Pluggable algorithms for block creation and validation
- BlockchainServer: gRPC service implementation
Transactions undergo multiple verification steps:
- Signature verification using the sender's public key
- Balance check to ensure the sender has sufficient funds
- Block validation by consensus rules
For Proof of Work consensus:
- The miner collects pending transactions from the pool
- A candidate block is created with these transactions
- The nonce is incremented until the block hash meets difficulty requirements
- The valid block is added to the chain
- The miner receives a reward of 50 tokens
- MockChain: This blockchain implementation
- Mockallet: Official wallet implementation for interacting with the blockchain
- Peer-to-peer network communication
- Merkle tree implementation for transaction verification
- Support for smart contracts
- Enhanced wallet integration
Contributions are welcome! Please feel free to submit a Pull Request.
-
gRPC API Enhancements:
- Implement a service for retrieving transaction history by address
- Create an endpoint for querying the full blockchain state
- Add block explorer functionality via gRPC
-
Consensus Mechanisms:
- Complete the Proof of Stake implementation (currently stubbed)
- Add educational implementations of other consensus algorithms:
- Delegated Proof of Stake (DPoS)
- Practical Byzantine Fault Tolerance (PBFT)
- Proof of Authority (PoA)
- Improve documentation on consensus pluggability
-
Testing and Documentation:
- Create comprehensive test suites for different consensus mechanisms
- Develop educational examples demonstrating blockchain fundamentals
- Document performance characteristics under different consensus models
When contributing, please follow the existing code style and include appropriate tests for your changes.