An attempt to implement simple blockchain using Rust, while learning Rust.
A detailed checklist to track the progress:.
- Implement blockchain structure with vector of blocks.
- Add block header (timestamp, previous_hash, current_hash, nonce, difficulty).
- Create block body with transactions (inputs, outputs, metadata).
- Generate the genesis block.
Here is the current state of the blockchain:
- Implement a basic consensus algorithm (e.g., Proof of Work or Proof of Stake).
- Create a mining function to solve nonce for PoW.
- Develop a staking mechanism for PoS, if chosen.
- Pre-mining / Developer fund - creator gets the initial set of coins to himself for development
- Initial Coin Offering (ICO) - issuing / selling tokens to early adopters
- Send mining fee for each new block mined to Node's wallet
- UTXO maturity period for newly created coinbase mining fee
- Distribute small amounts of cryptocurrencies for free to help users start interacting, e.g. when a Node joins for the first time.
- Implement mempool for the unprocessed (pending) transactions.
- Once the block is mined, remove transactions from mempool and update transaction status.
- Define Transaction and its usage
- Decide on block size, based on the usage of blockchain
- Decide if block will be flexible in size (changeable) or fixed
- Add transaction fee
- Implement transaction validation (e.g., check inputs vs. outputs).
- Add digital signatures for transactions to ensure authenticity.
- Implement which transaction go into block, and which don't based on transaction fee
- Implement Wallet & Address creation
- Implement send of value functionality
- Get balance & transaction history for all accounts
- Build TCP client/server communication for wallet and node communication.
- Build a peer-to-peer (P2P) network for node communication.
- Implement block and transaction propagation among nodes.
- Develop synchronization for consistent blockchain copies across nodes.
- Create a function to verify block integrity (hash, timestamp, difficulty).
- Verify single block validity.
- Verify validity of part of the chain.
- Verify validity of part of the full chain.
- Implement block validation rules for consensus enforcement.
- Mine a block based on a condition, e.g. block is minimum 70% of its limit
- Add block mining fee
- Design a system to track user balances and UTXOs.
- Update blockchain state after adding new blocks.
- Implement Merkle trees for block transactions.
- Integrate Merkle root in block headers for transaction verification.
- Create a CLI interface for blockchain interaction.
- Build a simple web interface for user interactions like sending transactions and viewing blocks.
- Add measures to prevent double-spending.
- Implement protections against replay attacks.
- Include time constraints for block mining to prevent stale blocks.
- Add detailed logging for blockchain events.
- Implement monitoring tools to track blockchain performance metrics.
- Introduce smart contract functionality for extended capabilities.
- Research scalability solutions (e.g., sharding, layer-2 technologies).
- TBD
This is a Rust-based blockchain aimed at learning purposes. I created it based on different resources and techniques and I used what I thought is correct and interesting.
Down is the explanation of each section of a blockchain.
Wallet is able to:
- create new accounts
- connect to blockchain node
- exchange messages, such as:
- get balance
- get transaction history
- ping
- WebSocket Server:
There is a bare WebSocket implementation that listens to new connections and spawns a new tokio task for each new connection. It should be spawned in a separate thread not to block the main thread.
WebSocket server accepts address and message handler as parameters in
.run()
function that allows a separation of business logic from the actual WS server. WebSocket server supports:
- send
- broadcast
- anycast (TBD)
-
Blockchain Listener: Blockchain node has a BlockchainListener that is a wrapper around my implementation of WebSocketServer using Tokio library. Current intention is to use Blockchain Listener for communication between
Wallets
andBlockchain Node
such as to get balance, get transaction history, ping and similar functionalities. -
Inter-Node Communication:
- TBD
- Single Block Verification:
Block is a fundamental part of the blockchain. Its main purpose is to carry and store transactions. Blockchains building blocks are actually - blocks. Each block has a header and body. In order to verify a block, there are several checks implemented, which can be found in the
enum BlockValidationError
:
enum BlockValidationError {
#[error("Block not found with the specified hash")]
BlockNotFound,
#[error("Blockchain must have at least 2 blocks")]
InsufficientBlocks,
#[error("Invalid block hash format or value")]
InvalidHash,
#[error("Previous block not found in chain")]
PreviousBlockNotFound,
#[error("Previous hash mismatch")]
PreviousHashMismatch,
#[error("Block timestamp must be greater than previous block")]
InvalidTimestamp,
}
- Part & Full Chain Validity: There should be an option, other than to check a single disparity, to check both:
- the full chain validity
- validity on a certain range.
- Validation rules for consensus enforcement: Beyond cryptographic integrity, there should be a consenus mechanism to check for:
- valid transactions - each transaction in the block must adhere to certain rules
- PoW/Stake validation - at the moment, there is only a simple PoW mechanism with simple block difficulty for local development.
- block creation rules - mechanism to understand who and when can create blocks and what types of transaction to include
- network rules - enforce particuolar network structure with block size limit and transaciton format
Consists of blockchain, block, blockchain_listener.
Includes configuration constants.
Consists of Transaction definition, Header, Body (Input, Output, Metadata), and has blueprint for TransactionManager that will create transactions and track status of a transaction.
Has helper functions for hash and transactions.
Includes Wallet, WalletClient to connect to the Node, Accounts, Addresses, Wallet Message types to exchange with the Node.
Includes WebSocketClient and WebSocketServer implementation for various stuff.
At one point, you should be able to run tests per crate. TBD
So far so good. Probably ton of refactoring coming in place.