Part 1: Implementing Blockchain and Cryptocurrency with PoW consensus algorithm

Kashish Khullar
Coinmonks
4 min readOct 24, 2018

--

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

Source: Bitcoin Wiki

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.

Discover and review best Crypto softwares

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:

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:

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

Its main advantages — protection from Ddos-attacks and the influence of low fractions of cryptocurrencies owned by the miner in extracting capacity.

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:

  1. Block class
  2. Blockchain class
  3. Transaction class
  4. Wallet class
  5. P2P-Server class
  6. 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

Thank you for reading. In the next part, we’ll start writing some code. Hope you enjoyed this short intro. If you found this helpful throw a bunch of claps.

If you have any questions blockchain, ethereum or the world in general, leave a comment. :)

Get Best Software Deals Directly In Your Inbox

--

--