Skip to content

sb-saksham/rust-blockchain-implementation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Blockchain Implementation Overview

main.rs is the main entrypoint of the file. Use cargo run to run the file.

Core Components

The blockchain is built using three foundational structs:

  1. 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.

  2. Wallet (transaction.rs): Holds the cryptographic key pair (private_key for signing, public_key/address for receiving and verification).

  3. 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.

  4. Blockchain (blockchain.rs): The managing structure that stores the ordered list of Block(s), manages pending transactions, and allows mining and validation.

The Transaction Lifecycle

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.

Transaction Flow

[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];

Explanation of the Transaction Flow

  1. Signing: The Wallet uses its private_key to sign the hash of the transaction data. This signature proves that the transaction was created by the owner of the private key (the sender).

  2. Verification: Any network node can take the sender's public key (stored as the sender address in the transaction) and run the tx.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.

Block Mining and Chain Validation (Consensus)

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.

Block and Mining Flow

[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];

Explanation of Mining and Validation

  1. New Block Creation: The Blockchain::mine_block() function pulls all pending transactions and the current_hash of the last block to form the core data of the new block.

  2. Proof-of-Work (Block::mine): This is the consensus mechanism. The nonce is a counter that is incremented repeatedly. For each increment, the Block::calculate_hash() method is called. This process continues until the resulting current_hash meets the difficulty target (valid_proof check, e.g., starts with "ff").

  3. Chain Linkage: Once a valid hash is found, the new block is finalized and pushed onto the Blockchain vector. The new block's previous_hash points to the last block's current_hash, forming the chain.

  4. 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_hash of the current block must exactly match the current_hash of the preceding block. If any check fails, the entire chain is rejected.

About

Cubane Hiring Assignment

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages