A minimal blockchain simulation to demonstrate key blockchain concepts such as block linking, hashing, validation, and proof-of-work.
- Node.js (v18+ recommended)
- npm
npm installnpm start- Add dummy transactions before mining
- Mine blocks with proof-of-work
- Print the blockchain
- Tamper with the blockchain and detect it using validation
- Modular code with
BlockandBlockchainclasses
.
├── blockchain.js # Block and Blockchain class definitions
├── index.js # Driver code to run the simulation
├── package.json # Node.js project metadata and scripts
└── README.md # Project overview and instructions
-
✅ Blockchain with Linked Blocks:
Each block contains a reference (previousHash) to the hash of the previous block, ensuring a linked structure that mimics real-world blockchains. -
✅ Validation Method:
TheisChainValid()method verifies:- The integrity of each block’s hash.
- The correctness of the block linkage via
previousHash.
Tampering with any block's data will break the chain’s integrity and be detected.
-
✅ Well-Structured and Modular:
The project uses object-oriented design with:- A
Blockclass (handles hashing and mining logic). - A
Blockchainclass (manages the chain, validation, and transactions).
- A
-
✅ Readable Code:
Variable names are descriptive, and the logic follows a clean, intuitive flow. -
⚠️ Comments:
While the code is mostly self-explanatory, basic inline comments can be added to enhance understanding for newer developers.
-
✅ Proof-of-Work (PoW):
Implemented in themineBlock()method using a configurabledifficultylevel. Blocks are mined only when their hash starts with a specific number of leading zeroes (e.g.,"000"for difficulty 3), simulating computational effort. -
✅ Dynamic Transactions:
Transactions are added usingcreateTransaction()and grouped into blocks duringminePendingTransactions()— allowing multiple transactions per block before mining.
docker build -t simple-blockchain .
docker run simple-blockchain