How does Proof of Stake Work?
Tags: blockchain, web3, ethereum, consensus
Published: 2023-01-28 11:54:45

Proof of Stake (PoS) is a component of the mechanism used to achieve consensus on the state of a blockchain.

Another common way is to use 👉 Proof of Work

After September 15, 2022, a major update happened to the Ethereum blockchain, known as The Merge. Due to that, Ethereum now uses Proof of Stake, unlike Bitcoin which still uses PoW.

PoS is important for the participants in the network to reach a consensus. But what actually is a ...

## Consensus Protocol?
In decentralized systems like Ethereum, all the nodes on the network are required to agree on the blockchain's current state. For example, if I ask all of the nodes "what is my current ETH balance", they should have the same answer.

![Consensus](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mse23q0sd9hpczcxe0s.png)

To achieve this, we developed consensus protocols. They can also be viewed as an economic mechanism (as we are dealing with tokens) which also prevents illegal operations and attacks on the network.

Proof of Stake is one of the consensus mechanisms used by the Ethereum blockchain and many others.

But what is ...

## The idea behind PoS?
The working of PoS is explained in the following points

A validator will stake some tokens like ETH into a smart contract on the Ethereum blockchain

The ETH now acts as collateral and can be destroyed if the validator does some malicious action, eg. lying about the current state of the network.

The validator also becomes responsible for checking the validity of the new blocks and sometimes creating them.

**What's in it for the Validators?** The validator can cause you to lose your capital which you staked for a transaction to execute if they lied. Hence even the validators are rewarded to be truthful about their actions on the blockchain.

But now this raises another question ...

## Why do we need PoS?
PoS has some killer advantages over the previously used PoW. Some of them are listed here:

- It uses very less energy than PoW. Because in PoW you require high-end computation power, which requires a lot of hardware components to mine competitively, unlike PoS where you stake your earnings and just say the truth.

- Much easier to enter as a validator. In PoW, one needs to have expensive hardware to produce blocks. In the case of PoS that comes down to something like a good "gaming PC"

- Reduces centralization risks even further than PoW. Operations on PoW are expensive, so miners create mining farms where they club their resources and collectively create blocks. It becomes a risk as it takes more steps towards centralization.

- Rewards of ETH to validators are reduced. This is not a negative point, because unlike nodes in PoW, nodes in PoS require less energy to operate overall.

- Penalties for attacks such as 51% are exponentially more than it was for PoW. Because you have your tokens staked, instead of just hardware.

All this is great, but ...

## How are Blocks created in PoS?

![Block creation in PoS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkwpmt2ecbilb1q7hs5d.png)

Validators are responsible for creating new blocks and also validating them. A user must deposit 32 ETH into a deposit contract and run a node software which has an execution client, a consensus client and a validator client.

The flow goes this way -

- The validator receives new blocks from peers on the network

- They re-execute the transactions in that block to confirm it is valid

- They broadcast a vote in favour of the block in the network

- If there are enough votes in the favour of the block, it is added to the chain

This happens every 12-13 seconds, the time of a new block on Ethereum.

## Security
Validators also help in keeping the network secure. They should have sufficient resources like hardware, internet, uptime to participate, etc for the rewards.

They miss the ETH rewards if they fail to meet expectations.

**But what is preventing these validators to do something wrong?** If they behave dishonestly their stake can be slashed leading to a huge financial loss and being kicked out of the network.

The following are some of the ways a validator can misbehave-

- Proposing multiple blocks in a single 12-second period

- Submitting false votes for a block

- Sybil attack

Sybil attack is quite common in the case of Decentralized systems. This is where a participant pretends to be many different users on the network.

_A separate article is coming up focusing on Sybil Attack._

## Finding the Correct Chain?
In the case of a fork, where 2 validators introduce a block at the same time in parallel, how does the network decide on which chain to fall back on and add the blocks?

When 2 validators produce a block in parallel, there become 2 different versions of the Chain. So all the validators need to follow a "Correct Chain" where they can add blocks and delete their forks.

In the case of Proof of Work, this was decided by the Longest Chain Rule because that's the chain which has the most "Work" put in.

For PoS, Ethereum uses **LMD-GHOST** algorithm which identifies the fork which has the greatest amount of Votes in its history.

_A separate article will follow discussing more on LMD-GHOST algorithm._

The algorithm will check which fork received more votes from the validators and then everyone will resume from that fork.

## Finality
When a transaction is a part of a block that cannot be changed on Ethereum, it is said to have Finality. In PoS it is done with checkpoint blocks.

After every 32 blocks, a checkpoint block is introduced. This period is called an epoch. The validators also vote for pairs of checkpoint blocks.

If **Block "n"** was a checkpoint, **block "n+32"** is too. Validators vote for **(Block "n", Block "n+32")** which is considered a valid sequence. After enough votes, Block "n" is marked -> _finalized_ and Block "n+32" is -> _justified_

This prevents any attacker to change the contents of a finalized block without spending a large number of resources. The attacker would require 33% of the entire stake to rewrite the block history.

Basically, the attacker would vote against (Block "n", Block "n+32") pair and never let Block "n" be finalized.

PoS does have its limitations as it is more complex than PoW. But with constant development and research, they will be covered as well.

---

That's all :)