This repository contains a simple implementation of a blockchain in Rust. The blockchain consists of a Block
struct that represents individual blocks in the chain, and a WorkQueue
struct that manages parallel mining tasks.
This project aims to demonstrate the basic concepts of a blockchain and parallel mining using Rust. It provides a simplified implementation of a blockchain with functionalities like block creation, mining, and validation.
A block in a blockchain contains information. Many of these blocks create a blockchain. Many miners (worker threads) will compete over the right to mine a block by seeing who can find the first proof value, which, when combined with other information from the block, will give a hash that ends in x zero bits (x is one of the block's information). This hash is then stored as part of the next block's information.
The Block
struct represents an individual block in the blockchain. Each block contains the following attributes:
prev_hash
: The hash of the previous block in the chain.generation
: The generation number of the block.difficulty
: The difficulty level of mining (number of leading zeros required in the block's hash).data
: The data stored in the block (currently represented as a string).proof
: The proof of work (PoW) value that satisfies the mining difficulty.
The Block
struct provides methods for block initialization, block mining, and validation.
The WorkQueue
struct is responsible for managing parallel mining tasks. It allows multiple worker threads to work concurrently on mining tasks. The WorkQueue
is implemented using a Single-Producer Multi-Consumer (SPMC) channel to handle task distribution and result collection.
To use this blockchain implementation, follow these steps:
-
Clone the repository:
git clone https://github.com/codingpog/Simple_Blockchain_With_Rust.git
-
Import the required modules at the beginning of your Rust file.
use crate::block::Block; use crate::queue::WorkQueue;
-
Create and mine blocks
// Create an initial block with a specified difficulty level. let difficulty = 3; let initial_block = Block::initial(difficulty); // Create a new block that follows the previous block. let data = "Transaction data here".to_string(); let next_block = Block::next(&previous_block, data); // Mine the block using a specified number of worker threads. let workers = 4; next_block.mine(workers); // Validate the mined block. assert!(next_block.is_valid());
-
Use WorkQueue for parallel mining
// Create a WorkQueue with a specified number of worker threads. let workers = 8; let mut queue = WorkQueue::<Block>::new(workers); // Enqueue blocks for mining. queue.enqueue(next_block); // Receive the valid proof from the first worker that finds it. let valid_proof = queue.recv(); // Shutdown the WorkQueue when done. queue.shutdown();