Mining Difficulty - Simply Explained - YouTube

Channel: Simply Explained

[0]
In the past, I made a video explaining how blockchains work, and in it, I also explain
[4]
what mining is.
[5]
In a nutshell, mining is the process of creating a new block for a blockchain and filling that
[10]
block with transactions.
[12]
But what I didn’t explain is the “mining difficulty.”
[15]
To understand what it is, let’s look again at the problem that blockchains try to solve:
[21]
creating consensus amongst a bunch of computers or nodes on the internet.
[25]
In the case of a cryptocurrency, that is agreeing about how individual users have spent coins.
[31]
It might not seem like it, but this is a challenging problem to solve.
[35]
Nodes on this network aren’t always honest, and they can appear or disappear whenever
[39]
they like.
[40]
Now, imagine you want to join the Bitcoin network.
[43]
Your computer goes out on the internet and looks for other nodes that are participating.
[48]
But how does your computer know which one to trust?
[52]
Everyone one of these nodes could be trying to game the system and make themselves rich.
[56]
The solution proposed by the creator of Bitcoin was simple: the longest valid chain is the
[62]
one to pick.
[63]
But this alone is not enough.
[65]
If you’re an attacker, you could fabricate a very long chain with false transactions,
[69]
and everyone will think that yours contains the truth.
[72]
Yikes!
[73]
To solve this problem, Satoshi slowed down the process of creating new blocks with the
[78]
proof-of-work algorithm.
[80]
This is almost like a lottery where your computer is doing a bunch of random calculations to
[85]
try and be the first to solve a cryptographic puzzle.
[89]
Your chances of solving it are quite low.
[92]
It’s almost like all miners in the world are trying to pick the same, complex lock.
[98]
Everyone tries a bunch of combinations, but in the end, only one can solve it first.
[103]
And when that happens, you have to start all over again to create the next block.
[107]
On average, there is one computer every 10 minutes that solves the puzzle and gets the
[112]
mining reward for creating a new block.
[115]
This time delay of 10 minutes makes it impossible to artificially create the longest chain because
[120]
the probability that your computer alone can pick this lock - or solve the puzzle - on
[124]
time, is highly unlikely.
[125]
You need many people, trying many random combinations to get it done within the timeframe.
[131]
Awesome, problem solved!
[133]
Err

[134]
But hold on!
[135]
There is one more issue: our computers get faster over time, so newer computers could
[140]
perform more calculations and potentially create blocks in under 10 minutes.
[145]
Meaning an attacker could purchase a bunch of really fast computers and try to get ahead
[149]
of everyone else.
[150]
Luckily, Satoshi thought of this as well, and this brings us finally to the mining “difficulty.”
[156]
This is a number that controls how hard your computer has to work to create a new block.
[161]
And this isn’t a fixed number.
[163]
It’s automatically adjusted every 2016th block.
[166]
The system looks at how fast these blocks were created.
[170]
Normally this should take exactly two weeks.
[173]
If more blocks were created in that time period, the difficulty is increased.
[177]
If less blocks were created, the difficulty is reduced.
[181]
That seems simple enough, but why would the difficulty have to be adjusted every two weeks?
[187]
We don’t get massive computer upgrades every two weeks, right?
[190]
Well the system also has to compensate for the amount of people that are mining.
[194]
More miners mean that there is a higher chance of someone solving the puzzle in a shorter
[199]
timeframe, which means the difficulty should be increased to maintain it.
[203]
And the opposite is true as well.
[205]
If a lot of people stop mining, the creation of blocks will be longer and thus the difficulty
[210]
has to be reduced.
[212]
So in summary, the difficulty adjustment makes sure that mining new blocks always takes around
[217]
10 minutes, no matter how fast our computers are and no matter how many people are mining.
[222]
Great system but it has one big drawback: it tends to lead to centralization.
[227]
See, the main reason why people are mining is because of the profits.
[231]
As long as your mining operation is profitable, you keep it going.
[235]
However, an increase in difficulty could mean that your mining operation now consumes more
[240]
electricity than it generates in rewards, at which point you’re forced to stop your
[245]
mining operation.
[247]
And this means that only bigger miners remain active because they have economies of scale.
[252]
In the case of Bitcoin, we see a few big mining farms and mining pools taking a significant
[257]
portion of this “mining market.”
[260]
Not only does this centralize a technology that was meant to be completely distributed,
[264]
but it also opens the doors for these large mining farms to run attacks against the network.
[270]
Solutions for this problem are being discussed though, but that’s for another video!
[274]
Fun side fact: if you’re a developer, like me, you might be thinking that this Satoshi
[278]
guy is brilliant because he thought of all of this.
[281]
But he’s not perfect and also makes mistakes.
[284]
There is a small bug in the difficulty adjustment code of Bitcoin.
[288]
After 2016 blocks the difficulty is recalculated but the code only looks at the first 2015
[295]
blocks.
[296]
So if all blocks took exactly 10 minutes to mine and the last block took 100 days to mine,
[302]
it wouldn’t affect the difficulty at all.
[305]
Whoa, what a rookie mistake, cough!
[307]
Uhm, so don’t feel too bad about your coding skills!
[311]
Thank you so much for watching, and as always, subscribe if you liked this video and till
[314]
next time!