In the past, I made a video explaining how
blockchains work, and in it, I also explain what mining is. In a nutshell, mining is the process of creating
a new block for a blockchain 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 blockchains try 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 coins.
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? Everyone 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 is not enough. If you’re 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 and be the first to solve a cryptographic
puzzle.
Your chances of solving it are 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 reward for creating a new block. This time delay of 10 minutes makes it impossible
to artificially create the longest chain because the probability that your computer alone can
pick this lock – or solve the puzzle – on time, is highly unlikely.
You need many people, trying many random combinations
to get it done within the timeframe. Awesome, problem solved! Err… 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 2016th
block. 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. If less 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? Well the system also has to compensate for
the amount of people that are mining. More miners mean that there is a higher chance
of someone solving the puzzle in a shorter timeframe, which means the difficulty should
be increased to maintain it. And the opposite is true as well. If a lot of people stop mining, the creation
of blocks will be longer and thus the difficulty has to be reduced.
So in summary, the difficulty adjustment makes
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. See, the main reason why people are mining
is because of the profits. 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 your mining operation. And 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 this “mining market.” Not only does this centralize a technology
that was meant to be completely distributed, but it also opens the doors for these large
mining farms to run attacks against the network.
Solutions for this problem are being discussed
though, 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 is 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 10 minutes to
mine and the last block took 100 days to mine, it wouldn’t affect the difficulty at all. Whoa, what a rookie mistake, cough! Uhm, so don’t feel too bad about your coding
skills! Thank you so much for watching, and as always,
subscribe if you liked this video and till next time!.