Skip to content

Latest commit

 

History

History
155 lines (79 loc) · 6.31 KB

ch04-difficulty-adjustment.asciidoc

File metadata and controls

155 lines (79 loc) · 6.31 KB

Bitcoin’s Difficulty Adjustment

How does the bitcoin network ensure that blocks continue to get mined once every 10 minutes on average?

In this chapter I’ll explain the Difficulty Adjustment, and why it might be the most genius part of bitcoin 👇

If you remember the previous chapter on bitcoin mining, we discussed how the miners are trying to construct a block that hashes to something that starts with a bunch of 0’s.

So I have a question for you, which is harder:

  1. Finding some data that hashes to something beginning with one zero ("0").

  2. Or finding some data that hashes to something beginning with 10 zeros ("0000000000").

Question Mark What GIF by The BarkPost

If you said number two, you’re right!

Go ahead and try it yourself with this online hash calculator if you like.

SHA256 Hash Calculator

SHA256 Hash Calculator

Look, in just about 30 seconds of clicking, I found a hash that starts with a 0.

Super easy, I just kept typing a bunch of b’s and trying each one

bbbbb-sha256-hash

But finding a hash that starts with 10 zeros is a whole new ballgame. It would take me forever trying to just click around on that website.

I’d definitely need some custom hardware, and probably a whole factory of them.

single-bitcoin-miner

In fact, this is exactly what’s going on in the bitcoin network right now.

Look at the hash of the most recent bitcoin block…​ it starts with 19 zeros!!

block-639-031

But mining a bitcoin block wasn’t always this hard. Check out block 100, back in the good old days in 2009.

This block hash only starts with 8 zeros.

It was so much easier to mine blocks back then - people could mine them with just their simple desktop computers.

block-100

So what changed? And how did it change?

This is…​ the Difficulty Adjustment!

The number of leading zeros required for a block’s hash to be valid is designed to change over time.

In other words, the mining difficulty adjusts.

hack my life kevin GIF by truTV

Imagine if the bitcoin network did not adjust the difficulty.

Imagine if the network had required just 8 leading zeros for all block hashes, forever.

What would happen?

Hmmm Thinking GIF

Well, as the prices went up, more and more miners joined the network, there would be more total computer power trying to mine blocks…​

As a result, blocks would start getting mined very quickly. We might start getting a block once every minute, or even every second.

Family Guy Woah GIF

Alternatively, what if a bunch of miners went out of business or stopped trying? There might not be enough total computing power to find blocks quickly, and blocks might start taking really really long to find, grinding the network to a halt.

Sloth Walking Slow GIF

Luckily, Satoshi had a brilliant idea. Let the mining difficulty adjust based on network conditions, so that the average block time stays at 10 minutes.

Here’s how it works:

Every block has a timestamp in its header, which states approximately at what time the block was mined.

block-639031-timestamp

And once every 2016 blocks (~2 weeks), each node in the network performs a calculation…​

Zach Galifianakis Reaction GIF

They look at the timestamp of the current block, and compare it to the timestamp of the block from 2015 blocks ago.

They then calculate the total time it took to mine all those blocks in that 2016 block window.

Call this time T

t GIF by GIPHY Studios Originals

If every block took an average of 10 minutes to find, then this calculated time T should be exactly 2 weeks.

Why 2 weeks?

(10 minutes / block) * (2016 blocks) = 20160 minutes

20160 minutes = 336 Hours = 14 days = 2 weeks!

2 weeks GIF by AMAs

So here is the calculation that every node in the network performs:

If T is greater than 2 weeks, it means blocks were mined slower than expected, and the difficulty should decrease.

Instead of requiring 19 leading zeros for block hashes, they might bump it down to 18

Just To Make Things Easier Enabuns GIF

If T is less than 2 weeks, it means blocks were mined faster than expected, and the difficulty should increase.

Instead of requiring 19 leading zeros, they might bump it up to 20.

season 4 hollywood GIF by PBS

Sometimes the time is close enough to 2 weeks, that no difficulty change is made at all.

But how do we ensure that all nodes arrive to the same conclusion on this calculation?

Well, they all are looking at the same blockchain, and they are all running the same software, so they are guaranteed to arrive at the same conclusion.

Pretty genius if you ask me.

Genius Think GIF

So there you have it…​ that’s how the difficulty adjustment works.

But why does it matter so much?

What’s so important about making sure we have 10-minute block intervals?

In my opinion, the biggest implication of this is that it ensures the supply issuance of bitcoin stays at an expected rate.

Remember, each new block creates new bitcoins.

Faster or slower block times affect the rate of new bitcoins entering the market.

In every other asset class, when the price goes UP, more of it gets produced, and the supply begins to increase more quickly, which ultimately drives the price back DOWN.

@saifedean has explained this wonderfully in his book The Bitcoin Standard.

But bitcoin fixes this.

We now have a monetary asset who’s supply issuance is on a FIXED schedule, for all of eternity, no matter how hard people try to create more.

We’ve never seen a monetary commodity like this in history, and this is why Bitcoin is a massive breakthrough in monetary technology.

Get you some and a spacesuit. We’re going to the moon.

Space Dance Nasa GIF