Blockchain Fundamentals
Understanding what blockchain is and how it works at a fundamental level.
Blockchain Fundamentals
A blockchain is a distributed, immutable ledger that records transactions in a secure and verifiable way.
Core Concepts
Decentralization
Unlike traditional databases controlled by a single entity, blockchains are distributed across many nodes (computers). Each node maintains a copy of the entire blockchain.
Immutability
Once data is written to a blockchain, it cannot be altered. This is achieved through cryptographic hashing and consensus mechanisms.
Transparency
All transactions are visible to all participants, while maintaining privacy through cryptographic techniques.
Blockchain Structure
RUST// A blockchain is a chain of blocks struct Blockchain { blocks: Vec<Block>, } // Each block contains transactions struct Block { index: u64, timestamp: u64, transactions: Vec<Transaction>, previous_hash: String, hash: String, }
Key Properties
- Distributed: No single point of failure
- Immutable: Historical data cannot be changed
- Transparent: All transactions are visible
- Secure: Cryptography ensures integrity
- Consensus: Agreement on valid transactions
Use Cases
- Cryptocurrencies: Digital money (Bitcoin, Ethereum)
- Smart Contracts: Self-executing contracts
- Supply Chain: Tracking goods from origin to consumer
- Identity Management: Decentralized identity systems
- Voting Systems: Transparent and verifiable elections
Why Rust?
Rust's memory safety and performance make it ideal for:
- Validating transactions quickly
- Securing cryptographic operations
- Handling concurrent consensus mechanisms
- Building reliable node software
Code Examples
Blockchain Structure
Basic blockchain data structure in Rust
struct Block {
index: u64,
timestamp: u64,
data: String,
previous_hash: String,
hash: String,
}
struct Blockchain {
blocks: Vec<Block>,
}
impl Blockchain {
fn new() -> Self {
Blockchain {
blocks: Vec::new(),
}
}
fn add_block(&mut self, block: Block) {
self.blocks.push(block);
}
}
fn main() {
let mut blockchain = Blockchain::new();
let genesis = Block {
index: 0,
timestamp: 1234567890,
data: String::from("Genesis Block"),
previous_hash: String::from("0"),
hash: String::from("genesis_hash"),
};
blockchain.add_block(genesis);
println!("Blockchain created with {} blocks", blockchain.blocks.len());
}Explanation:
This shows the basic structure of a blockchain. Each block links to the previous one via the previous_hash field. The Blockchain struct maintains a vector of all blocks.
Block Validation
Checking if a block is valid
struct Block {
index: u64,
previous_hash: String,
hash: String,
}
fn is_valid_block(current: &Block, previous: &Block) -> bool {
if current.index != previous.index + 1 {
return false;
}
if current.previous_hash != previous.hash {
return false;
}
true
}
fn main() {
let block1 = Block {
index: 0,
previous_hash: String::from("0"),
hash: String::from("hash1"),
};
let block2 = Block {
index: 1,
previous_hash: String::from("hash1"),
hash: String::from("hash2"),
};
println!("Block valid: {}", is_valid_block(&block2, &block1));
}Explanation:
Block validation ensures the integrity of the blockchain. We check that blocks are sequential and that each block correctly references the previous block's hash.
Exercises
Create a Blockchain
Create a blockchain and add two blocks to it!
Starter Code:
struct Block {
index: u64,
data: String,
previous_hash: String,
hash: String,
}
struct Blockchain {
blocks: Vec<Block>,
}
fn main() {
}