Bitcoin block confirmation

Tharaka Wijesundara
6 min readMar 22, 2023

--

Have you been exhausted searching for a complete but simple guide to understanding how a bitcoin transaction takes place? 😰 This is a series of articles that explain bitcoin from its history of evolution to the complete journey of a bitcoin transaction with its underlying mathematics. The series consists of six articles and now you are in the fifth article.5️⃣ To kick off from the very beginning, click here. Happy reading.👨‍💻👩‍💻

You have come a long way up to the very last section before we move onto the mathematical side of address generation and verification. Now the Bobs transaction has been picked up and is ready to be mined. Here, we focus on what mining is all about.

Hashing

Hashing is a process where any arbitrary length input is converted to a compressed fixed-length output. There is a particular feature in hash functions called Avalanche Effect which means a small change in input (even a 1-bit change) will result in a drastic change in the output. There are different types of hashing functions in use but some of them are broken(insecure).

For example, MD2, MD4, and MD5 function in the MD family and SHA — 1 in the SHA family.

There are some properties that cryptographic hash functions must adhere to.

  • Pre-image resistant — one wayness

Computationally hardness to reverse the hash function. In other words, having a hash value of H, it is difficult to find an input x that hashes again to H.

  • Second pre-image resistant

Let’s say the hash for the input value x is h(x). Then it is difficult to find another input y such that h(x) = h(y).

  • Collision resistant

This property is somewhat similar to the previous one. Here what we are referring to as a collision is, having two different inputs x and y which results in the same value after hashing. Since hash functions compress the arbitrary length input to a fixed length output, it’s obvious that there must be collisions. However if the complexity of finding such is huge, in the computing world we refer to it as collision-free. Based on the above definitions we can come up with a condition; if a function is collision-resistant then it is second pre-image resistant as well.

Here in the context of Bitcoin, we are referring to cryptographic hash functions because every hash function is not a cryptographic hash function but every cryptographic hash function is a hash function.

What is meant by breaking a cryptographic hash?

For a hash function to be a cryptographic one, it needs to adhere to the above properties. If it fails, then we can call it a broken hash. As an example, MD5 and SHA-1 are no longer secure because there were some collisions found.

Target hash

As you might have seen in the referenced video in the previous article, there is a specific field called Nonce (A number that can only be used once) in each block. Since mining is the process of finding a unique number that is unique to each block, the number that the miner or pool of miners is searching for is called the Nonce. The difficulty of finding Nonce depends on the hash rate (Total number of aggregated hash calculations in the network per unit of time) of the network.

In the bitcoin blockchain network, Nonce is the required number to make the hash value of the block less than or equal to the target hash.

**Target hash of the Genesis block**
*0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF*

**The hash of the Genesis block**
*0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f*

As you can see the Hash of the block is less than the target hash.

In the Bitcoin blockchain, the target hash has been being updated from the genesis block for every 2016 number of blocks to ensure the time difference between two adjacent blocks is 10 mins. However, in real-time, the time difference would not get the same as 10mins.

10 mins per block => 20160 mins => 2 weeks.

This means the difficulty value will be adjusted every 2 weeks. So why do we need to update this?🤔

According to Moore’s law, the processing speed is getting increased every two years. If we kept the mining difficulty constant, the time between two blocks will get reduced and since there is a reward for each mined block, the total bitcoin supply of 21 million coins would be added to the supply within a shorter period of time. The same problem arises when the total number of active mining nodes changes. In both of the above cases, the changing parameter is the hash rate of the network. Therefore as a whole, we can state that the hash rate and the difficulty are directly proportional.

How is the target hash calculated?🤔

Satoshi Nakamoto set up an initial difficulty value and then it has been adjusted after every 2 weeks by taking the time difference between 2016 blocks.

Expected difference - 20160 mins
Actual difference - 19456 mins

Ratio - 19456 / 20160 => 0.965

Network has slowed down and difficulty should be reduced.⏬
And wise versa is also possible.

Note: Actual time difference can be found out using the timestamp attached to each block

In each block under the bits field, you can find the compressed representation of the difficulty. At the time of writing,
386,304,419 is the difficulty of the latest block.

Note: This is a compressed version of the difficulty. Refer to this and follow the steps to find the uncompressed version of the number.

Consensus mechanism

Since the network is distributed and maintains a single set of values, the whole network should agree upon the next block which is going to be appended. That is what we call a consensus mechanism. In Bitcoin, we use Proof of Work consensus. Proof of work is nothing but competing with each other for finding a Nonce. Once found, the miner should notify the peers in the network with block details and once they verified the correctness, its peers will be notified. Likewise, the block details reach each and every node in the network and the newly mined block will be added to their blockchain.

Even though the nonce finding mechanism is computationally so hard, verifying process is pretty simple.

Hash[block details including Nonce] <= Target hash

In bitcoin, there is an algorithm called the transaction selection algorithm for selecting validated transactions from the Mempool for the current mining block. If each and every mining node runs the same algorithm, they will end up having the same set. However, the running algorithm will get differ between nodes depending on their favor.

Example:

Miner P - (TX A, TX B, **TX C**)
Miner Q - (TX A, TX B, **TX D**)

Let’s say Miner P wins the battle and broadcasted its block. Then Miner Q has to stop the job he was doing and start it from the very beginning by selecting another set of TXs because TXA and TXB have already been added in the latest block.

What if two or more miners have completed the math problem at the same time and nodes get notified with multiple mined block details?😕Then, eventually, one will be accepted while the others fall into the orphaned category. 😌It depends on the confirmations on top of each mined block, in other words, how many blocks were mined based on the previous one because each block contains the hash of the previous. Please refer to these detailed articles for an in-depth understanding.

With that, we have come to a conclusion about the bitcoin transaction journey. I kept the mathematical concepts behind the address generation and verification for the next article.

References:

  1. https://www.tutorialspoint.com/cryptography/cryptography_hash_functions.htm
  2. https://en.bitcoin.it/wiki/Genesis_block
  3. https://learnmeabitcoin.com/technical/target
  4. https://en.wikipedia.org/wiki/Category:Broken_hash_functions
  5. https://en.wikibooks.org/wiki/Cryptography/Breaking_Hash_Algorithms

--

--