Part 1: Implementing Blockchain and Cryptocurrency with PoW consensus algorithm
By: Kashish Khullar
Tags: Blockchain, Nodejs, Proof Of Work, Tutorial, Bitcoin

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

Proof of Work

Proof of Work ( PoW ) is a blockchain consensus algorithm, applied by Satoshi Nakamoto in 2009. It was first used by Bitcoin and later adopted by Ethereum.

In order to add new blocks to the blockchain, an algorithm is required to decide which miner gets the ability to add a block to the blockchain. Hence, this is the primary goal of PoW.

Working

In PoW the miners are required to invest a large amount of computational power in order to find a hash string which matches a given set of constraints. In Bitcoin, miners are required to find a hash such that there is some known number of leading zeros at the beginning of the hash. A miner continuously finds new hash values of a block by changing a number, which is hashed along with the block, called nonce. The count leading number of zeros in the hash value is called the difficulty. It is the difficulty that is attributed for such long delays in block generation.

Consider this simple example:

let block = {"random":"data"};

let nonce = 1;

let difficulty = 5; let blockHash = hash( block,nonce,difficulty );

//blockHash = 6B86B273FF34FCE19D6B804EFF5A3F5747ADA4EAA22F1D49C01E52DDB7875B4B

Since the blockHash doesn’t follow the constraints the miner will increment the nonce and try again. The miner has to do that until it receives something like this:

00000273FF34FCE19D6B804EFF5A3F5747ADA4EAA22F1D49C01E52DDB7875B4B

Moreover, it is not just one miner but every miner on the network runs the same algorithm on the same block. This creates a competitive environment where every miner competes with every other. The first miner who finds a hash value which satisfies the given constraints wins the round and becomes the leader of the round. It then adds the block after signing it with its private key and broadcasts it to the network. The other miners add the received block into there local chains.

For doing such computationally expensive task, the miner gets an incentive. In the case bitcoin, Miners receive some amount of bitcoin and the transaction fees of all the transactions that are present in the block they just added to the blockchain.

Advantages

Since PoW is computationally so expensive, an effective attack also requires similar processing power. To compete with the entire network is financially infeasible for an attacker. An attacker would gradually run out of cash to fund its own attack.

Disadvantages

A major disadvantage of PoW is also its advantage. The huge cost in running this algorithm. To realize how much power is consumed, read power compares article which elaborates this problem.

Bitcoin mining is now using more electricity than 159 individual countries

Secondly, bitcoin isn’t completely secure and PoW isn’t a panacea to all attacks. 51% attack is a scenario in which an attacker might garner enough computational power which is greater than 51% of the entire network, then the attacker can successfully tamper the blockchain.

Structure

Let’s understand the structure of the PoW algorithm through this diagrams:

Structure of Proof of Work Consensus Algorithm

Design

Before we start coding lets us analyze what we have to make.

Following are the classes in our code that we make from the above overview:

Block class Blockchain class Transaction class Wallet class P2P-Server class Miner class

Later we’ll add more but these seem fine for better understanding. Let’s move on.

Part 2: Implementing Blockchain and Cryptocurrency with PoW consensus algorithm