As you might have known newly created Bitcoins are released per 10-minute average in the process of mining Bitcoin. Miners, who are special nodes running the Bitcoin software, participating in verifying Bitcoin transactions in the Bitcoin network, are involved in this so-called mining process, where these miners consume enormous processing power on finding a valid hash that grants the block they have created to be included in the longest chain in the Bitcoin blockchain. As a reward, the miner receives some newly minted Bitcoin. Though the amount of Bitcoin they will receive is not constant and will be reduced in half over time. This is what we call Bitcoin halving. At the time of writing this post, the reward each miner gets is 6.72699351 BTC (Block 665811).
This is a very competitive task and only the people who are accessible to cheap electricity and suitable hardware to calculate the valid hash for the next block first will win the race, putting the luck on the other miners the next time. Finding a valid hash for the next block is hard because the miner has to generate the SHA-256 hash below a defined target by the Bitcoin network and the possibility of finding a valid hash is very low. It is guaranteed that any miner in the Bitcoin network will find the next block in 10 minutes of time, and after every 2016 blocks have been found, the difficulty gets re-adjusted based on the actual time the last 2016 blocks took to be added on the Blockchain.
This is a screenshot I took of the block header of block 665811. You can see a field called bits and the value for that field is a hex number (0x170da8a1). This is actually a compact way of representing the number because to avoid taking so many spaces in the block header when they are propagating over the network, but if we work out to get the value of the bits represented as a 256-bit number, you will get this value in hex:
For a moment, let’s look at the hash value for this block:
So, what’s this hex value all about? Well, this is the target value. That means if you take to consider the hash value for this block, the hash value is below the target value, and that is why this block was able to be included in the Blockchain at the end of the day. The miner somehow was able to put the effort into generating this valid hash in 10 minutes.
In the below screenshot you can see the block header of Genesis block, the first-ever block Satoshi mined for the Bitcoin blockchain in 2009. The target value for this block is:
For this Genesis block, the target value was not generated by the system, instead, Satoshi initially came up with this value at their best guess. This target value for the Genesis block influences how the target values for other blocks should be calculated. Let’s say, for example, the scenario of calculating the target value for the blocks between 2016 and 4032. When calculating the new target, we need to get the ratio of finding the blocks for the last 2016 blocks, which means, we divide the actual time it took for the last 2016 blocks to be mined from the expected time, which of course is 2016 * 10 * 60 seconds. If the result (the ratio) we get is less than 1, we can assume that the blocks have been mined at a fast pace, which means each block took less than 10 minutes to get mined. On the other hand, if we get the ratio greater than 1, the time each block took to be mined was greater than the expected 10 minutes. The new target is then calculated by multiplying the current network target by the ratio. To keep the time exactly as 10 minutes as possible, the target is then re-adjusted for the next 2016 blocks to make them too difficult or easier to be mined, depending on the ratio we ended up with.
In the header, we can see another field called difficulty which is another part of this target calculating process. Simply, the difficulty is how we understand how a particular block was difficult to be mined with respect to how difficult it was to be mined the Genesis block. Basically, it’s the ratio between the max target (the target of the Genesis block) and the current target we calculated. So, we can find the difficulty for the next 2016 by dividing the max target by the current target.
If we consider the difficulty of block 665811, it was 20,607,418,304,385.63. We can think that, on average, it took this much of times as long to find this valid block as it would at difficulty 1, the initial difficulty defined in the Genesis block.
Since you need to get an idea about how exactly the target can be calculated for the next blocks, I will add a code snippet explaining each step clearly in the next post.