Implementing Proof of Stake Part — 1

Kashish Khullar
Coinmonks
Published in
4 min readJan 26, 2019

--

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

Source: Bitcoin Wiki

Proof of Stake

Proof of Stake (PoS), like Proof of Work, is a blockchain consensus algorithm. Though it is remarkably different in the way it works. Many blockchains use this algorithm in production and soon Ethereum will its version of PoS called Casper.

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

Working

In Proof of Stake, miners/validators are required to stake their tokens/balance in order to be chosen as the next block creator. Therefore, the miner that stakes the most amount of its currency has the highest chance of being chosen as the leader and creating the next block. Some blockchain solutions don’t follow such principle and instead choose the miner on a random basis.

If a miner is caught cheating or tampers with a block, it loses its entire staked coins. Hence, such a restriction compels the miners to be honest.

Consider this example:

let stakedCoins = { 
"address-1" : 10
"address-2" : 70
"address-3" : 50
"address-4" : 20
}
let leader = Object.keys(balances).reduce((a,b) => balances[a] > balances[b] ? a :b)// leader = address-2

This was a simple example in which the address that stakes the most tokens is chosen as a leader.

Compared to Proof of Work, where miners compete with each other in terms of computation power, here they compete in terms of currency.

Casper is Ethereum’s PoS implementation in which validators have to deposit some tokens in a smart contract in order to propose a block. Any cheater,if caught, loses all of the staked tokens.

Advantages

With PoS, a person with a common laptop can become a validator.

Proof of Stake will significantly reduce the consumption of electricity in comparison of PoW and therefore is more environment-friendly. Secondly, the time-consuming mining process will be replaced by a constant amount of computation. This will, consequently, will improve the speed of the entire network.

Attacks will result in a loss instead of gain

Instead of gaining from the attacks, the attack will actually lose wealth. Since in PoS, cheating results in loss of coins, attacks become very expensive and hence prevents forking.

Disadvantages

A miner with 51% wealth will own the network.

With the PoS protocol, a miner that holds 51% of the total coins will always be chosen as the leader. As a result, the decentralization of the blockchain becomes futile if a single node always creates the blocks. It is however unlikely that a node would own 51% of the total coins.

Many blockchains, however, have protocols in place to prevent this situation. One such solution is coinage. Where the tendency to be elected as the leader with time reduces or the coin which is the validator which has staked tokens the longest time becomes the leader.

Structure

We will be implementing the following given structure in node.js:

Basic Structure of PoS
  1. If a node in the network wants to become a validator then it first submits a validator fee.
  2. When the transaction is confirmed it can then stake some coins to compete with other validators.
  3. Meanwhile, each node is responsible for broadcasting the transactions they receive from clients.
  4. When a sufficient amount of transactions are created, the validators elect a leader with the maximum staked coins. The elected leader then creates a block and broadcasts it to the network.
  5. Each node validates the block, executes all the transactions in the block and adds the block in the chain.
  6. The block also has a special reward transaction. The leader for the given round gets the transactions fees of the transactions present in the block as a reward.

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. Account Model
  4. Validator class
  5. Stake Model
  6. Transaction class
  7. Transaction-pool
  8. Wallet
  9. P2P-Server
  10. APIs

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

Part 2: Implementing Proof of Stake

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

--

--