Bitcoin's Block Difficulty and Hash Rate
Bitcoin's increasing hash rate gives greater protection to the network
TLDR:
Bitcoin block difficulty is continually adjusted every 2016 blocks (approximately 14 days) in order to maintain new blocks being mined every 10 minutes.
As more miners come online the block difficulty also trends upwards, and more hashes need to be performed to mine an individual block, increasing hash rate.
Hash rate increasing also increases the network’s collective energy spent to mine a block, and the more energy is spent means the more money is spent.
Miners require a 51% hash rate to attack the network, the higher the hash-rate the more expensive this is, hence the more protected the network becomes.
The Bitcoin hash rate hit a new all-time-high of 667 million terahashes per second last week. Bitcoin’s price may be stagnant but hash-rate just keeps going up and up!
Not many people understand the hash rate particularly well and how it protects the network, so this week I decided to take a look at this.
Bitcoin Block Difficulty
Bitcoin is programmed to produce 1 new block at an average of every 10 minutes. It does this through controlling the “difficulty” of mining a block. If a sequence of blocks are mined faster than an average of 10m then the algorithm will adjust the difficulty up, and if they are mined slower than 10m then it will adjust difficulty down.
The block difficulty is adjusted every 2016 blocks, in other words every 14 days (because 14 days x 24 hours x 6 blocks per hour = 2016 blocks), and is a crucial element of the overall Bitcoin system in controlling it’s monetary policy and inflation rate.
But how is block difficulty adjusted?
Well, in Bitcoin’s proof-of-work a miner successfully mines a block when they find a hash of that block which has enough leading zeros needed for the current difficulty level, as I covered before in the post on Consensus Algorithms.
Therefore, the code just needs to ajdust the number of 0s that the hash needs to have at the start to change it’s difficulty. More 0s makes it harder to mine a block, while less 0s makes it easier.
This constant and regular adjustment of the target difficulty within the Bitcoin system ensures that as more miners come online or go offline, Bitcoin never deviates too much from the desired 10 minute block times!
Difficulty Adjustment Code
In my Bitcoin Halving post I looked at how elegant the halving code was. So I decided to do the same thing with Bitcoin’s difficulty adjustment code today.
It’s more complex than the simple halving function, but honestly there’s not a huge amount going on and it’s still quite elegant and easy to grasp. Don’t worry if you can’t read code, you’ll get an approximate idea of what’s happening by following along.
The first important function is GetNextWorkRequired() as shown below.
This function gets called with the latest block, block header, and proof-of-work consensus parameters. It checks if the block is a multiple 2016 by doing:
if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
This is essentially a comparison to check if we’ve reached a “DifficultyAdjustmentInterval”, which is set to 2016 in the consensus parameters, against the current block’s height taken from “pindexLast->nHeight+1”.
If we haven’t reached an adjustment block then there’s some testnet code that can be ignored, and all the code does is return the current difficulty stored in the latest block:
return pindexLast->nBits
However, if we’re on one of the specific interval blocks that comes every 14 days, then it calls to CalculateNextWorkRequired() with the date-and-time that the previous interval block was mined, along with the latest block and consensus parameters again.
This function first sets a “limit” so that you don’t get too large an adjustment in either direction (where the code says “limit adjustment step”).
Then more importantly it “retargets” to a new difficulty by essentially running this formula:
bnNew = nBits * (nActualTimespan / params.nPowTargetTimespan)
Where:
nBits = the previous difficulty setting
nActualTimespan = refers to the time that passed in those 2016 blocks
nPowTargetTimespan = is the ideal time of 10m per block for those 2016 blocks
This new “target number” (bnNew) is the number a block’s hash needs to be below or equal to be valid. A higher target number means easier mining (ie. less leading 0s), while a lower target number means harder mining (ie. more leading 0s).
Hash Rate
Miners in proof-of -work consensus are therefore locked in a never ending cycle of global competitions that last approximately 10 minutes each to find a valid hash. They individually try out hash-upon-hash until one of them finds a valid block that fits the current difficulty, and then the competition starts over again.
In the past there were fewer miners and the difficulty was much lower, meaning it was easier to find a valid hash and less hashes were run collectively by all these miners to create a valid block.
However, as the number of miners has grown, so has difficulty grown to keep block times at 10 minutes, and therefore so has the number of hashes performed to find a valid block.
Today thousands of miners collectively mine across hundreds of machines globally to find a valid block and collect their reward. This collective hashing of thousands of miners is known as the network’s hash rate!
The hash rate has continued on a never ending upward trend since the network began as you can see below, and last week this total hashing power reached a new all-time-high of 667m TH/s (tera-hashes per second)!
Hashing power is often split across different miner pools where miners are rewarded accordingly based on how much they contribute to a pool - there’s many fascinating details to dive into how this all works but I’ll discuss it more in depth in a future post on miners.
As you can see above though the hash-rates almost continuously grown in a somewhat exponential trend regardless of price speculation. And as the network’s hash-rate continues to grow and grow, by consequence it also gets stronger and stronger.
Protecting the network
The network’s hash-rate has a really important role in Bitcoin that’s often misunderstood by people, which is that it ultimately protects the network. A higher hash-rate means a stronger network - but why?
Well, the consistent hashing by miners is computationally expensive, which in turn is energy intensive, so you need to spend energy, in other words money, to write to Bitcoin’s blockchain!
If everyone is playing nicely this may seem unnecessary, but Bitcoin is a decentralised protocol without central co-ordination, so you can easily get a malicious or defective miner who tries to break the rules to, for example, create BTC for themselves.
Theoretically a malicious miner (or mining pool) with over 51% of the hashing power would be able to continuously win the block production competition and take over what get’s written into the blockchain. Therefore the higher the Bitcoin hash-rate, the more energy (ie. money) a malicious miner needs to spend to attack the system!
Even so, if this were to happen, network participants would notice it quickly and could choose to fork off the chain, causing that miner to lose all the energy/money that they spent with only a short period of disruption to the network.
Moreover, Bitcoin’s designed in such an elegant way, that even with over 51% of the hashing power, a malicious miner would still be unable to steal people’s funds, because the cryptography protecting people’s private keys is yet unhackable.
So if a malicious miner tried to add a transaction moving people’s funds without their private keys, then that transaction would be invalid and it would invalidate the entire block, so they would once again be throwing energy and money away!
The incentives at play here are known as “game theory”, and can be described by the prisoner’s dilemma:
Miners who co-operate are rewarded with new BTC, while miners who defect and try to attack the system will ultimately spend a bunch of energy/money, and end up with nothing to show for themselves, at most a short period of time where valid blocks aren’t produced until the the network stabilises.
This decentralised coordination problem is in fact known as the “Byzantine Generals” problem in computer Science. Satoshi managed to solve it through Bitcoin’s proof-of-work and cryptography with the game theory described above, which is one of the many reasons that makes Bitcoin such a huge innovation!
Now you might be asking yourself, is it bad that Bitcoin keeps consuming more and more energy over time? Well that’s an interesting question but one for a future post.
For now it suffices to say that the higher the hash-rate, the higher the energy spent, and therefore the larger the incentive is to collaborate with the network and be rewarded by it than to play against it - hence the more protected the network becomes!