main.rs is the main entrypoint of the file. Use cargo run to run the file.
The blockchain is built using three foundational structs:
-
Transaction (transaction.rs): The data payload representing a transfer of value (sender,recipient,amount). It contains a hash of its own data and a digital signature. -
Wallet (transaction.rs): Holds the cryptographic key pair (private_keyfor signing,public_key/address for receiving and verification). -
Block (block.rs): The container for verified transactions. It is linked to the previous block via hashing and secured by a Proof-of-Work mechanism. -
Blockchain (blockchain.rs): The managing structure that stores the ordered list ofBlock(s), manages pending transactions, and allows mining and validation.
The transaction lifecycle ensures that only the rightful owner of funds (the sender) can authorize a transfer. This relies on the ECDSA via the k256 crate.
[Sender Wallet: Private Key] --> {Create Transaction Data}; --> [TX.calculate_hash()]; --> [Sender Wallet.sign_transaction(Hash)]; --> [Transaction Object with Signature]; --> [Added to Pending Transactions]; --> {Block Mining Process}; Verification [Vverifying Signature] --> {Check: Public Key matches TX.sender?}; --> {Check: Signature valid against TX.hash?} = True --> K[TX is Valid]; False --> L[TX is Rejected];
-
Signing: The
Walletuses itsprivate_keyto sign the hash of the transaction data. This signature proves that the transaction was created by the owner of the private key (the sender). -
Verification: Any network node can take the sender's public key (stored as the
senderaddress in the transaction) and run thetx.verify()function. If the public key can successfully decrypt the signature and match it against the transaction's stored hash, the transaction is accepted as authentic.
The mining process secures the ledger by making block creation computationally expensive (Proof-of-Work), and the validation process ensures the immutable link between blocks.
[Last Block] --> {New Block Creation}; --> [Collect Pending Transactions]; --> [Block Data: Index, Timestamp, Prev. Hash, TXs];
Mining (Proof-of-Work) --> [Start Nonce = 0]; --> Calculate Hash --> {Hash starts with 'ff'?}; No --> [Nonce += 1]; --> Calculate Hash --> Check Again Yes --> [Valid Hash Found]; [Finalized Block (Nonce & Hash)]; --> [Blockchain.blocks.push(Block)]; {Verification Loop} --> Check PoW, Hash Match & TXs --> [Chain Valid];
-
New Block Creation: The
Blockchain::mine_block()function pulls all pending transactions and thecurrent_hashof the last block to form the core data of the new block. -
Proof-of-Work (
Block::mine): This is the consensus mechanism. Thenonceis a counter that is incremented repeatedly. For each increment, theBlock::calculate_hash()method is called. This process continues until the resultingcurrent_hashmeets the difficulty target (valid_proofcheck, e.g., starts with "ff"). -
Chain Linkage: Once a valid hash is found, the new block is finalized and pushed onto the
Blockchainvector. The new block'sprevious_hashpoints to the last block'scurrent_hash, forming the chain. -
Chain Validation (
Blockchain::verify_chain): To check the integrity of the entire ledger, the chain is scanned from Block #1 onwards:-
Internal Check: Each block is validated using
Block::verify_block(), which checks its own Proof-of-Work and verifies the signature of every transaction inside its payload. -
Linkage Check: The
previous_hashof the current block must exactly match thecurrent_hashof the preceding block. If any check fails, the entire chain is rejected.
-