Blockchain Mining DifficultyBy Hethu Nanayakkara
The crypto-currency Bitcoin recorded an all-time high at USD 4,300 in Mid-August 2017. The popularity of crypto-currencies like Bitcoin and Ether (Etherium) have tremendously increased adaptation over the years. Blockchain is the underlying technology that powers these digital crypto-currencies.
This post is to explain some technical concepts behind blockchain and explain the mining difficulty adjustments that takes place in the network to keep the growth of the blockchain stable.
A “blockchain” is a public database of transactions shared in a peer-to-peer network between participating nodes secured by cryptography. A transaction in the blockchain is permanently time-stamped and is immutable. The blockchain consists of “blocks” of data, with each block containing validated transactions with a reference to the previous block. This creates a chain of blocks, thus the name “blockchain”. This chaining allows anyone to validate the transactions up to the beginning of the chain, all the way back to the very first “genesis block”.
Blockchain “miners” are special nodes in the network that validates new transactions to create new blocks on the blockchain. To create a new block, miners require huge amounts of computing resources or processing power. Miners are required to create a block that satisfies certain criteria set by the network. They need to prove that they created a new block (the “proof-of-work”) by showing their candidate block. The new block gets added to the blockchain only if the majority of the network nodes agree on the validity of the block.
To explain the concepts further, we will use the Bitcoin blockchain as an example. A block’s main content is a list of validated transactions. The block header contains a reference to the previous block and few other meta data. The header includes an important value called the “nonce”. Nonce is a random value generated by the miners and included in the block so that the block-hash satisfies the hash requirement (hash target).
The SHA-256 hash algorithm generates a 64-digit hexadecimal hash value. The current Bitcoin blockchain requirement is to find a nonce that creates a block-hash with 17 leading zeros.
Note: The actual hash difficulty is not about the leading zeros. It is about finding a hash less than the hash target set by the network. However, this is approximately about finding a hash having a specific number of leading zeros.
Miners reward themselves with the “block reward” for creating new blocks. The block reward started at 50 BTC when the blockchain began in 2009 and halves every 210,000 blocks, roughly every 4 years. Current block reward is 12.5 BTC. The block reward will ultimately reach zero and a total of 21 Million Bitcoins would be mined by the year 2140.
Up to now, miners have added around 480,000 blocks and generated 16.5 Million Bitcoins. This is the total amount of Bitcoins currently in circulation and owned by all Bitcoin users.
The Blockchain Supercomputer
Finding a block with 17 leading zeros is an extremely difficult resource-intensive task for any single computer. However, with all the miners in the network with their combined computing power, it takes an average of 10 minutes to construct a new block. Think of this as one supercomputer generating blocks every 10 minutes.
When new mining nodes with faster and faster CPUs are added to the network, the blockchain super computer becomes more and more powerful. Naturally, it should produce new blocks faster. However, the Bitcoin network ensures that new blocks are created at a constant pace by recalibrating the hash-difficulty so that the new blocks will be created at an average of 10 minutes.
For example, when the Bitcoin blockchain started in 2009, the initial requirement was to generate a block with a block-hash having 8 leading zeros. Today, due to the much faster supercomputer, it needs to find a block having 17 leading zeros.
Understanding the Blockchain Hashing Difficulty
A SHA-256 generates a 256-bit hash (64 hex-digits). The Bitcoin network defines a “hash target” every 2016 blocks. This is roughly every 2 weeks when a block is created every 10 minutes.
One block every 10 mins = 6 blocks per hour, 6 x 24 in a day, and 6 x 24 x 14 = 2016 blocks in two weeks.
The miners are looking for a block hash (a binary number) that is less than or equal to this “hash target”. For example, if the hash target is 0000a1b2c3e4f5, any hash less than or equal to this number is a valid block hash. Many hashes would satisfy this requirement and any one of those would be a valid. However, it is an extremely difficult task to find such a hash. Lesser the hash target, the more difficult it is to find a satisfying hash.
As an example, if we were to find a hash less than or equal to 0FFFF, we have 65,536 choices. However, if we were to find a hash less than or equal to 000FF, we have only 256 choices. A lower target number means fewer choices. Visually, more leading zeros we require in our hash, much harder it is to find a satisfying hash.
You can view the current Bitcoin blockchain information by visiting blockchain.info. Search for any block by giving the block number. For example, if we look at a recent Block #456789, we will see two important values related to the difficulty calculation: Bits and Difficulty
“Bits” is a value represented in compact notation indicating the “hash target”. Bits is a four-byte value. The first byte indicates the number of bytes (length) of the hash target while the following three bytes indicate the most significant bytes.
In our Block #456789 example;
Bits: 402809567 (decimal) => 18 02 62 DF (hex)
Each byte has two hex digits. The first byte represents the length of the hash target: 18 (hex) => 24 (decimal). Which means the hash target is 24 bytes in length, with the most significant bytes specified in the subsequent three bytes.
When the three significant bytes are written as a 24-byte value:
Hash target: 02 62 DF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
The miners are attempting to find a block hash less than or equal to this hash target.
SHA-256 hashes have 64 hex digits. When the hash target is represented as a full 32-byte value (64 hex digits):
Visually for simplicity, this can thought of as miners trying to find block-hashes containing 17 or more leading zeros.
Adjusting the Hash Target
As mentioned before, the hash target is adjusted every 2016 blocks so that the time to generate blocks remain at an average of 10 minutes.
If it has taken less than 10 minutes average time to generate the last 2016 blocks, the formula will yield a smaller hash target, which is harder to find.
In the example block above, we see a large value called “Difficulty”. The “difficulty” gives a relative figure of the hash difficulty of mining a block compared to mining the very first block. The difficulty is a ratio between the block #0 (genesis block) hash target and the current hash target:
The genesis block Bits were hardcoded to 486604799 (0x 1D 00 FF FF). This results in a hash target of 8 zeros followed by FF FF followed by 26 zeros. This is a very large hash target. The hash target becomes smaller and smaller over time; there by making the difficulty becomes larger.
The difficulty of the block #0 was 1. Since the Difficulty depends on the hash target, its value changes every 2016 blocks.
The difficulty increases exponentially. Today, your typical general-purpose computer would take thousands of years to mine a new block on its own. The combined mining power of the Bitcoin supercomputer can do this in 10 minutes.