Proof of Work for Bitcoin
Tags: blockchain, web3
Published: 2023-01-14 12:33:42

It is one thing to know what is a consensus mechanism and another to understand it.

Proof of work is a protocol that allows all of the nodes on the network to agree on the state of the blockchain. This protocol is still used for **Bitcoin**.

Like Bitcoin, it was also used for the Ethereum network, until September 12, 2022, when the Ethereum merge happened resulting in the switching of Ethereum from Proof of Work to Proof of Stake.

Proof of Work (PoW) is part of a consensus protocol. But what is ...

## Consensus?
It is defined as a general agreement about something, an idea or a deal.

The participants in the Blockchain which are known as nodes are distributed. They have their local storage and each node machine can have its state.

For the blockchain network to work properly, every node must have the same state of the local blockchain copy in its storage. Nodes are machines/computers but they are distributed and decentralized all over the network.

In a centralized system, where multiple computers are connected to one central server which handles all of the data exchanges, it is easy to maintain the same state of all of the computers connected to it.

![Centralized Systems](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2uaao9x4qldoktpqbfs.png)

Because the server knows and can perform read/write to synchronise all the computers connected to it.

In a decentralized system, as there is no central server, all the computers are connected to a network. So to keep them in sync, some protocol needs to be established.

It's the Consensus protocols that help us on achieving agreement on the state of the blockchain at any given point.

## Understanding PoW
It is a consensus protocol currently used by Bitcoin and previously by Ethereum.

Here the miners are required to create new blocks of valid transactions. After the block is added, the miner broadcasts the event to the rest of the nodes on the network. (To understand how mining works in blockchains, read this 👉 [Mining in Blockchain](https://adityas.hashnode.dev/blockchain-mining) )

In PoW, a miner is supposed to solve a difficult mathematical puzzle, before anyone else to get the rewards. The puzzle is quite hard but very easy for someone else to verify its validity.

## So, first come first serve?
Not exactly, alone just PoW cannot be seen as a consensus protocol. There are some other mechanisms and procedures which help in selecting the miner who will produce a block.

A Sybil resistance mechanism will be used in this case. It is a way to measure the endurance of the protocol in case of a **Sybil Attack**.

**What's a Sybil attack?** It is a problem when a user or a group pretends to be many different users. Outside the digital world, it is like you shouting "present" in school for yourself and your friend too.

A decentralized system must ensure security against this to give rewards to deserving miners. If the selection of miners is just done randomly, a Sybil attack can be done easily.

## Selection of Chain
Each node contains a copy of the entire blockchain with them, and then each miner will append new blocks to their local chain. You can say there will be many different states of a blockchain if each miner has its copy.

How will the other miners agree on 100s of different states? There should be a single chain which contains the latest state and all nodes should agree to the state of that one blockchain.

Each miner will include different blocks in their blockchain, this is called a fork.

![Blockchain Forking](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/os4ilwb2iknu9bn6zuh1.png)

Blockchains need a single continuous chain, else there will be many states of a single blockchain and it will be impossible to have consensus on all of those.

Blockchains like Bitcoin and Ethereum use the longest-chain rule. The chain which is accepted by a higher number of nodes and is growing longer is considered. The forked chains are deleted after they have served their purpose.

The **PoW + longest chain rule** is also called as Nakamoto Consensus.

## What is the "Work" in PoW?
PoW aims to make the miners work for their rewards. They should spend their resources i.e time and energy, in this case, computation power to propose new blocks.

The miners will have to produce valid blocks for which they generate a certificate of legitimacy. That allows anyone to check the block's validity easily and if the miners cheat, they will lose their resources for nothing in return.

When Ethereum used to work on PoW, this is the way the miners had to do work -

- A miner will select a bunch of transactions to include in a block

- The network rules will select a slice of data from the current blockchain state. Because the whole blockchain is always increasing, it is much more feasible to work on the part of it that has the latest state

- A target value is calculated by passing the dataset through a hashing function. The lower the target the more difficult the mining process, and vice-versa

- Now the miner will use brute force to generate a random number which you call the nonce.

- Some more parameters, in addition to target, nonce, and dataset are passed in a hashing function to get a result which should be lower than the target value.

- The miner has to do trial and error for random numbers to be the valid nonce to get a value lower than the target. The lower the target, the more attempts are required.

Hence, here is the rewarding "work" the miners do.

---

That's all :)