In the past I made a video explaining how block chains work and in it I also explained what, in a nutshell, mining is: the process of creating a new block for a block chain and filling that block with transactions. But what I didn't explain is the mining difficulty. To understand what it is, let's look again at the problem that block chains are trying to solve: creating consensus amongst a bunch of computers or nodes on the Internet. In the case of a cryptocurrency, that is agreeing about how individual users have spent their points. It might not seem like it, but this is a challenging problem to solve. Nodes on this network aren't always honest, and they can appear or disappear whenever they like.
Now imagine you want to join the Bitcoin network. Your computer goes out on the Internet and looks for other nodes that are participating, but how does your computer know which one to trust? Every one of these nodes could be trying to game the system and make themselves rich. The solution proposed by the creator of Bitcoin was simple: the longest valid chain is the one to pick. But this alone isn't enough. If you are an attacker, you could fabricate a very long chain with false transactions, and everyone will think that yours contains the truth. Yikes.
To solve this problem, Satoshi slowed down the process of creating new blocks with the proof-of-work algorithm. This is almost like a lottery, where your computer is doing a bunch of random calculations to try to be the first to solve a cryptographic puzzle. Your chances of solving it are actually quite low. It's almost like all miners in the world are trying to pick the same complex lock. Everyone tries a bunch of combinations, but in the end only one can solve it first, and when that happens, you have to start all over again to create the next block. On average, there is one computer every 10 minutes that solves the puzzle and gets the mining rewards for creating a new block. This time delay of 10 minutes makes it impossible to artificially create a long chain, because the probability that your computer alone can pick this lock is highly unlikely. You need many people trying many random combinations to get it done within the time frame.
Awesome, problem solved. But hold on, there is one more issue. Our computers get faster over time, so newer computers could perform more calculations and potentially create blocks in under 10 minutes, meaning an attacker could purchase a bunch of really fast computers and try to get ahead of everyone else. Luckily, Satoshi thought of this as well, and this brings us finally to the mining difficulty. This is a number that controls how hard your computer has to work to create a new block, and this isn't a fixed number. It's automatically adjusted every 2016 blocks. The system looks at how fast these blocks were created. Normally this should take exactly two weeks. If more blocks were created in that time period, the difficulty is increased, and if fewer blocks were created, the difficulty is reduced.
That seems simple enough, but why would the difficulty have to be adjusted every two weeks? We don't get massive computer upgrades every two weeks, right? While the system also has to compensate for the number of people that are mining: more miners mean that there is a higher chance of someone solving the puzzle in a shorter time frame, which means the difficulty should be increased to maintain the 10-minute average. The opposite is true as well. If a lot of people stopped mining, the creation of blocks will take longer, and the difficulty has to be reduced.
So in summary, the difficulty adjustments make sure that mining new blocks always takes around 10 minutes, no matter how fast our computers are and no matter how many people are mining. Great system, but it has one big drawback: it tends to lead to centralization. The main reason people mine is profit. As long as your mining operation is profitable, you keep it going. However, an increase in difficulty could mean that your mining operation now consumes more electricity than it generates in rewards, at which point you're forced to stop. This means that only bigger miners remain active, because they have economies of scale. In the case of Bitcoin, we see a few big mining farms and mining pools taking a significant portion of the mining market. Not only does this centralize a technology that was meant to be completely distributed, but it also opens the door for these large mining farms to run attacks against the network. Solutions for this problem are being discussed, but that's for another video.
Fun side fact: if you're a developer like me, you might be thinking that this Satoshi guy is brilliant because he thought of all of this. But he's not perfect and also makes mistakes. There's a small bug in the difficulty adjustment code of Bitcoin. After 2016 blocks, the difficulty is recalculated, but the code only looks at the first 2015 blocks. So if all blocks took exactly ten minutes to mine except the last block, which took 100 days to mine, it wouldn't affect the difficulty at all. Cool, what a rookie mistake. So don't feel too bad about your own coding skills.
Thank you so much for watching, and as always, subscribe if you like this video. Until next time.
