Implementing Proof of Stake Part — 2

Kashish Khullar
Coinmonks
Published in
3 min readJan 28, 2019

--

This article presents an easy implementation of Proof of Stake consensus algorithm in node.js

Source bitcoin wiki

In the previous post, we went through an introduction to the proof of stake, how it works and also talked about the design and architecture of the code that we will be implementing.

In this post, we’ll be starting coding.

Prerequisites:

  1. Node.js and NPM
  2. Text Editor ( with a dark theme 😛)
  3. 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 POSchain.

mkdir poschain
cd poschain

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 these 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

validator — the address of the guy/gal whose made this block

signature — the encrypted hash of the block, signed by the validator

So create a new file block.js in your root directory and create a class with these properties.

the block

Also, add a toString() function that will print the details of the block in a readable format.

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 createBlock()

This staticcreateBlock() function currently 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.

The backticks and dollar syntax, that’s ES6.

Let’s create a createBlock() function by using all the above stuff.

In the upcoming tutorials, we will later add the validator and signature when we implement a wallet.

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.

The blockchain class

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 createBlock()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 createBlock() function using the data passed to the addBLock()

Here’s what our blockchain.js file looks like till now.

Cool.

Next, we’ll add more functionality to the blockchain to support multiple contributors.

Thank you for reading. In the next part, we’ll write code to validate a blockchain and add support for multiple nodes. 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. :)

Get Best Software Deals Directly In Your Inbox

--

--