Part 1: Implementing Blockchain and Cryptocurrency with PoW consensus algorithm

Kashish Khullar
Oct 24, 2018 · 4 min read

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

Coinmonks

Coinmonks is a non-profit Crypto educational publication.

Sign up for Coinmonks

By Coinmonks

A newsletter that brings you week's best crypto and blockchain stories and trending news directly in your inbox, by CoinCodeCap.com Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Kashish Khullar

Written by

You landed at the right place

Coinmonks

Coinmonks is a non-profit Crypto educational publication. Follow us on Twitter @coinmonks Our other project — https://coincodecap.com

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store