Blockchain Fundamentals
Core concepts, distributed ledger technology, consensus mechanisms, and blockchain architecture.
Overview
Blockchain is a distributed, immutable ledger for recording transactions.
Core Concepts
Block Structure
{
"index": 1,
"timestamp": 1705123456789,
"data": {
"transactions": [...]
},
"previousHash": "0000000000000000000...",
"hash": "0000abc123...",
"nonce": 12345
}
Key Properties
| Property | Description |
|---|---|
| Decentralization | No single point of control |
| Immutability | Cannot alter past records |
| Transparency | Open verification |
| Consensus | Agreement on state |
Consensus Mechanisms
Proof of Work (PoW)
def proof_of_work(block, difficulty=4):
target = "0" * difficulty
nonce = 0
while True:
block.nonce = nonce
block_hash = calculate_hash(block)
if block_hash.startswith(target):
return nonce, block_hash
nonce += 1
Proof of Stake (PoS)
def select_validator(stakeholders):
total_stake = sum(s.stake for s in stakeholders)
random_point = random.uniform(0, total_stake)
current = 0
for validator in stakeholders:
current += validator.stake
if current >= random_point:
return validator
Network Types
| Type | Access | Example |
|---|---|---|
| Public | Open to all | Bitcoin, Ethereum |
| Private | Restricted | Hyperledger |
| Consortium | Group-controlled | R3 Corda |
| Hybrid | Mixed | Enterprise solutions |
Blockchain Architecture
Architecture Diagram
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
ā Application Layer ā
ā (Smart Contracts, DApps) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Network Layer ā
ā (P2P, gossip protocol) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Consensus Layer ā
ā (PoW, PoS, BFT) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā¤
ā Data Layer ā
ā (Blocks, Merkle trees) ā
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
Practice
Implement a simple blockchain with proof of work.