Design a Blockchain Ledger (Simplified)
HardData StructuresBlockchainHashingImmutabilityConsensusLinked List
Problem Statement
Design a simplified blockchain ledger that demonstrates core blockchain concepts: blocks containing transactions, cryptographic linking via hashes, proof-of-work mining, and tamper detection — in memory.
Requirements
Functional Requirements
- Transaction: sender, receiver, amount, timestamp, unique ID
- Block: index, list of transactions, timestamp, previousHash, currentHash, nonce (for PoW)
- Genesis block: first block with previousHash = '0000...'
- Mine a block: find a nonce such that SHA256(block content + nonce) starts with N leading zeros (difficulty)
- Add mined block to chain
- Validate chain: verify each block's hash and previousHash linkage
- Tamper detection: if any block is modified, validation fails from that block onward
- Transaction pool: pending transactions waiting to be included in next block
- View all transactions for a given wallet address
Non-Functional Requirements
- Thread-safe chain append
- Singleton BlockchainService
Key Entities
- Transaction: id, sender, receiver, amount, timestamp
- Block: index, transactions, timestamp, previousHash, hash, nonce
- Blockchain: chain (List<Block>), pendingTransactions, difficulty
- MiningService: finds nonce for PoW; computes block hash
- BlockchainService: Singleton; manages transactions, mining, validation
Constraints
- Difficulty = number of leading zeros required in block hash (default 4)
- Block capacity: max 10 transactions per block
- Once a block is added to the chain, it is immutable
- Chain validation must be O(n) in chain length
!Constraints
- Mining required before block can be added to chain
- Difficulty: hash must start with `difficulty` leading zero characters
- Max 10 transactions per block
- Chain validation: O(n) — verify hashes sequentially
- Thread-safe chain append — only one block can be mined and appended at a time
Output
Run or submit your code to see output here.