Hey!

We are very proud to announce that we have finally received a request from a woman!

Eli asks:

What is the Proof of work? How the miner mined a block?

The answer is not so easy!

First of all, the Proof of Work (We use to call it PoW), it’s the consensus algorithm of Bitcoin protocol, and it’s essential in order to validate blocks and secure the blockchain.

You can imagine the PoW like a miners race. The miners are nodes that search the solution of mathematic problem.

A miner tries to find the solution using the items block, it has to find a number less than a threshold established by the Bitcoin protocol. To find it, many test are carried out by the miner using the SHA256 algorithm. As we discuss in the article about SHA256, if the input of the function is the same, we get the same result, always. For that reason a miner uses a particular item, called nonce: its value changes randomly during the PoW in order to obtain the right solution (digest’s SHA256).

When a miner finds the solution, the network (other nodes) check if it is correct: this is how the consensus works! If the nodes check the block and find the wrong, the system reject the block.

The miner will be then rewarded for his computational effort in finding the answer to the problem. This prize is called reward (which today, July 2020 it’s 6,25 bitcoins for each block plus the fees of each transaction that has entered the block itself. This reward is halved every 210.000 blocks, approximately every 4 years, as explained in the Halving video — Check this video about Halving! 🚀

The difficulty threshold isadjusted every 2016 blocks, approximately 2 weeks.

Ok, let’s go to check the theory with the practice, like in our amazing book Bitcoin from Theory to practice 💪🏻

In Action

What is the Proof of work? How does the miner mine a block?

Let’s use the block 1773164. Let’s back its hash with the call getblockhash, and its information with getblock. We will use the sub-shell to make a single call.

$ bitcoin-cli getblock $(bitcoin-cli getblockhash 1773164)
{
"hash": "00000000000000eb74d096b83594d770f23d633e3a8b08813763489fbde7a0ef",
"confirmations": 2,
"strippedsize": 1704,
"size": 2722,
"weight": 7834,
"height": 1773164,
"version": 545259520,
"versionHex": "20800000",
"merkleroot": "75870dd46a503862af6fcf700be5e02e008db4521d96713bda51607ed05e2a18",
"tx": [
"5e68fb0018f65216db1f559437705350934451da086c0bc3e4073c0f2b8df4cc",
"64b3ceadb693d33be7cdc4233c4607d77d37effcf426c9ce75146bfb269a93dd",
"8f4ce2cfa530a5f3912d2c75be3b4d018da18037a68c6b2fac0415d09bdd39b3",
"9d67968905df317779d3c92b7e052fae96020ccbc68d3f77d40bca0f2e50449d",
"4684cbc6831af10960835cd0c10831a23eb90e736dab9e96229e45828ded2760",
"6f66bcfab129e6d3a5b18e3e9554d54178b415cb54ecf82e350cc24d63209eff",
"fc874f84759c9c4944037af4b3be02efd03c6436ebc8b8571519658de1e5580a",
"6d11edc6b84444206dd97f9924e8ca6b273b0bce9ea58218590a275a64da740a",
"370151706116ba9ae7bff77d9244a6a3e5401aadb43de221bf3f804386724c4d",
"5d6c8801705493612138d3addfb0b0b5a49cc5177b5f2cee370d4b765d3e37b2"
],
"time": 1592919152,
"mediantime": 1592917919,
"nonce": 3940145976,
"bits": "1a01a5f2",
"difficulty": 10178811.40698772,
"chainwork": "000000000000000000000000000000000000000000000160b51965fab057015d",
"nTx": 10,
"previousblockhash": "00000000000001249b9a4e000135acecec2dcd7385eba54639ff962f3883e861",
"nextblockhash": "0000000000000171c5c04355788d2531b349b55c6189e2e3719d2b5c82fa2026"
}

As you can see, the block contains some information, but not all of them are used to try to win the PoW race.

The miner had to find fewer numbers than the numbers reported in bits.

The bits is the encoded form (compressed representation) of the target of the candidate block.

The candidate block is the block that the mines use to carry out his tests and win the proof of work in order to insert it in the blockchain.

We can retrieve the current candidate block using the getblocktemplate method.

To retrieve the actual target, we can use jq in order to parse Json.

$ bitcoin-cli getblocktemplate '{"rules": ["segwit"]}'

00000000000001a5f20000000000000000000000000000000000000000000000

At this time, the miner must find a number that must be below the hexadecimal just extracted.

However We are not miners. So We’ll replicate the miner’s work that the miner has done on the block we have chosen.

The values the miner uses are:

Version hexPreviousblockhashmerkleroottimebitsnonce

For convenience, We use Env variables to save their value.

We Save in env variable ver, the versionhex in its little endian representation.

$ ver=`printf 20800000 | tac -rs ..| tr -d ‘\n’`

We save in env variable prev, the previous blockhash in little endian.

$ prev=`printf 00000000000001249b9a4e000135acecec2dcd7385eba54639ff962f3883e861 | tac -rs .. | tr -d '\n'`

Save in env variable mkl, the merkle root hash in its little endian representation.

$ mkl=`printf 75870dd46a503862af6fcf700be5e02e008db4521d96713bda51607ed05e2a18 | tac -rs .. | tr -d '\n'`

Save in env variable time, the time in hexadecimal format and little endian.

$ time=`printf '%x\n' 1592919152 | tac -rs .. | tr -d '\n'`

Save in env variable bits, the bits in little endian.

$ bits=`echo 1a01a5f2 | tac -rs .. | tr -d '\n'`

#bitcoin #security #miners #bitcoin-spotlight #proof-of-work-explained #hackernoon-top-story #how-does-a-miner-mine-a-block #mining-a-bitcoin-block-explain

How Do Miners Mine A Block: A Proof of Work Deep Dive
2.75 GEEK