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

Enjoy the video in Bitcoin in Action.

Bitcoin in Action
6 min readJul 15, 2020

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 is adjusted 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.

$ bitcoin-cli getblocktemplate ‘{“rules”: [“segwit”]}’

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

$ bitcoin-cli getblocktemplate ‘{“rules”: [“segwit”]}’ | jq -r ‘.target’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 hex
  • Previousblockhash
  • merkleroot
  • time
  • bits
  • nonce

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’`

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

$ nonce=`printf ‘%x\n’ 3940145976 | tac -rs .. | tr -d ‘\n’`

Now, we need to concat all values and apply SHA256 twice.

$ printf $ver$prev$mkl$time$bits$nonce | xxd -r -p | sha256sum -b | xxd -r -p | sha256sum -b

and then we need to get little endian representation:

$ printf efa0e7bd9f48633781088b3a3e633df270d79435b896d074eb00000000000000 | tac -rs ..00000000000000eb74d096b83594d770f23d633e3a8b08813763489fbde7a0ef

The result obtained is the number that satisfies the difficulty of that time.

How we can be sure about that?

We Convert both the hash obtained and bits to base 10.

In order to obtain the bits in base10, it’s necessary to divide it in two parts, respectively coefficient and exponent.

The current bits is:

1a01a5f2

The coefficient is 0x1a. it’s represent the exponent’s length
In this case 26 bytes, because 1a in base 10 is 26.

$ echo ‘ibase=16; 1A’ | bc26

The coefficient 01a5f2 is the reminder of the bits.

We must therefore have a 52 hexadecimal character long string, including the exponent. Keep in mind, 1 byte can be represented by 2 hexadecimal characters.

We get, then:

01a5f20000000000000000000000000000000000000000000000

We check that we’ve written the correct length.

$ printf 01a5f20000000000000000000000000000000000000000000000 | wc -c52

Excellent, let’s convert it in base10 and save the value in env var BITS_10.

$ BITS_10=”$(echo “obase=10; ibase=16; -u; $(echo 01a5f20000000000000000000000000000000000000000000000 | tr ‘[:lower:]’ ‘[:upper:]’)” |bc | sed -n 2p)”

Let’s examine the result obtained:

$ echo $BITS_102648593653332025323234430866859553558338063400000453437554688

We also convert the hash obtained previously to base10 and save it in env var TARGET.

$ TARGET=”$(echo “obase=10; ibase=16; -u; $(echo 00000000000000eb74d096b83594d770f23d633e3a8b08813763489fbde7a0ef | tr ‘[:lower:]’ ‘[:upper:]’)” |bc | sed -n 2p)”

Now, $TARGET must be less than $BITS_10, which is the difficulty imposed by the protocol at that time.

$ echo “$TARGET<$BITS_10”|bc1

YO! We get as the result 1, so true! The miner has successfully solved the mathematical problem.

How does the network replicate the result? It’s very simple, knowing the parameters, especially the nonce, verifies that the result is correct. If different values return the same results, as said in the video concerning the SHA256 (Check this article!) we are facing a kind of collision.

The result is the block hash.

With this slightly more technical article we have tried to demonstrate what the miner does to create a block, and to secure the blockchain. As you can imagine, it’s a very broad topic: a few minutes are not enough to analyze every single nuance.

You can find more in our book Bitcoin from Theory to practice.

I hope I have answered your question!

See you next time! 🤟🏻

— —

🎥 Bitcoin in Action

🐙 GitHub: https://bit.ly/2Lj3yeY

📖 Libro Bitcoin dalla teoria alla pratica (Amazon)
📖 Libro Bitcoin dalla teoria alla pratica (pagamento in bitcoin)

📖 Libro Bitcoin dalla teoria alla pratica (Amazon)
📖 Book Bitcoin from theory to practice

📖 Tascabile Bitcoin 199 domande (Amazon)
📖 Tascabile Bitcoin 199 domande (pagamento in bitcoin)

📖 Pocket Book Bitcoin 199 questions (Amazon)
📖 Pocket Book Bitcoin 199 questions (accept bitcoin)

🎥 Video Corso Bitcoin dalla teoria alla pratica


Twitter , Facebook, Linkedin, Medium, Instagram, Youtube, GitHub

Television isn’t a good idea (Radio Stations)
Email isn’t a good idea (Post offices)
Amazon isn’t a good idea (Retail stores)
Bitcoin isn’t a good idea (Central banks)

In crypto we trust

--

--

Bitcoin in Action

Bitcoin. Money for nerds? Digital gold? Perhaps, but certainly it is a social revolution!