Part 2: Implementing Blockchain and Cryptocurrency with PoW consensus algorithm
A small scale, easy to understand yet, comprehensive, step by step implementation of Blockchain and cryptocurrency with Proof of Work consensus algorithm in node.js
In the previous post, we went through an introduction to the blockchain, proof of work and also talked about the design and architecture of the code that we will be implementing.
In this post, we’ll be starting getting our hands dirty.
Prerequisites:
- Node.js and NPM
- Text Editor ( with a dark theme 😛)
- Postman or any other app to interact with HTTP APIs.
That’s it.
Let’s Code:
We’ll start by creating the root directory of our project. Give a name to your project. Let’s say Nodechain.
mkdir nodechain
cd nodechain
Create a new node project
npm init -y
We need a package called nodemon to run our development server and reload our server everytime we save. This tool saves us from a lot of headaches.
npm install nodemon --save-dev
Alright. Moving on.
The Block
Blockchain is made of blocks so let’s create a block class. Block has 4 main properties:
timestamp — the time of creation of block in milliseconds
lastHash — hash of the last block on the chain
hash — hash of the current block
data — data in the block or the transactions
So create a new file block.js
in your root directory and create a class with these properties.
Also, let’s add a toString()
function that will print the details of the block in a readable format.
In blockchain, the hash value of each is calculated based on the hash value of the block that came before.
Since there are no blocks at the beginning of the chain, blockchains have the concept of the Genesis block. Genesis block serves the purpose of being the origin of the blockchain. It’s a hardcoded block with dummy values for the timestamp, lastHash, hash and data values.
Let’s add a staticgenesis()
function to our block class with some hardcode values. This function will create a new block with dummy values. This static because we don’t want to create an instance to call this function.
Next thing we will add to our block class is the ability to create a new block based on the data and the last hash. Let’s call it mineBlock()
This staticmineBlock()
function takes 2 arguments, the lastBlock, and the data and based on these two items it generates a new block. A lot of our functions in the entire project will be static.
To generate a hash value we need to node package called crypto-js
Let’s install it.
npm i crypto-js --save
Import the sha-256 function from this module.
const SHA256 = require('crypto-js/sha256');
We won’t use the hash function directly, instead, we’ll create a separate function since we’ll be using it again in the future.
Let’s create a mineBlock()
function by using all the above stuff.
We’ll add more features in here, very soon.
Here’s the block class we have created so far.
Now that we have created our block class we must test it. To test this class, it’s better that we create a testing environment. For this project, we will be using jest.
npm i jest --save-dev
Note: if you have trouble installing jest, which I once had, try downgrading to a lower version, it worked for me.
With jest, we can execute testing files in a javascript project. To find testing files, jest will look for files with a *.test.js extension so to test our block class let’s create a block.test.js file.
Let’s create some test cases. We’ll test if the constructor, genesis, hash, and mineBlock are working as expected.
To run this file, make some changes to the scripts in the package.json file. Add a test script in it.
"test": "jest --watchAll"
Let’s run the tests.
npm run test
We’ll 2 tests passing showing our functions are working just fine.
To check if the tests are working fine. Make some changes to the test file and then run tests again. You’ll see that the tests will fail.
Cool. We have created a basic block. Let’s move on and create a blockchain class to which will link up these blocks.
The Blockchain
Create a blockchain.js
file in the root directory. To create a blockchain class we need to import the block class.
In the constructor, we’ll give an initial block to the chain. Here, we’ll make use of the static genesis function that we created to start our chain.
Next, let’s give our blockchain class the ability to add more blocks to the chain by creating a addBlock(data)
function.
Here, we’ll use our mineBlock()
function to create a new block and later pushing it to the chain. We’ll access the last block on the chain and then call the mineBlock()
function using the data passed to the addBLock()
Here’s what our blockchain.js file looks like till now.
Cool. Time to test our blockchain. Create a blockchain.test.js file.
Let’s add a few test cases.
We’ll create two variables blockchain
which will be used to add blocks too. Let's test the addBlock()
function now.
Run the tests, now you’ll see 4 tests passing.
Next, we’ll add more functionality to the blockchain to support multiple contributors.
Part 3: Implementing Blockchain and Cryptocurrency with PoW consensus algorithm
Thank you for reading. In the next part we’ll write code to validate a blockchain and add support for multiple miners. Hope you enjoyed coding. If you found this helpful throw a bunch of claps.
If you have any questions about blockchain, ethereum or the world in general, leave a comment. :)