Mining Difficulty Deconstructed

Samantha Ouyang
Coinmonks
Published in
8 min readDec 31, 2020

--

When I first learned about Bitcoin mining, I thought it was as easy as sitting at home and doing as I wished, while my PC would be put to work, making money in my sleep.

Well, not exactly. Turns out there’s a lot more to it which makes the above scenario quite literally impossible.

But in order to understand why, we first have to understand the whole concept of mining and what that means in terms of cryptocurrencies.

What is Mining?

In essence, mining is the process of creating new blocks, filling them with transaction records, and adding them to the blockchain for everyone to see and agree upon how certain coins have been spent.

In order to add a block however, miners must solve a “cryptographic puzzle,” or in more technical terms, they must solve for a valid hash of the block.

I won’t get too much into hash functions, but put simply, the hash is made by taking all of the information on the block, then tossing it, turning it, stretching it, kneading it — you get the idea — until it returns a hexadecimal number with exactly 64 digits, or to a typical human, what appears to be a random jumble of letters and numbers that have no relation whatsoever to what it had previously been.

For example, the phrase “Hello!” would return 334D016F755CD6DC58C53A86E183882F8EC14F52FB05345887C8A5EDD42C87B7.

If you change any of the data that was on the block, the jumble will be drastically different, but if nothing is changed, it will always return the same thing. You can try it for yourself here!

But how do you know if the hash is valid? This is where the difficulty target comes in. The target delineates a threshold value, and for a hash to be valid, it must be less than the target.

You might ask, how do you get different hashes if you can’t alter the transaction information? Well, there’s another component of the block that’s factored in when creating the hash — this one you can alter, and it’s called the nonce.

A diagram of the anatomy of a block. Source: Khaled Salah

Miners play around with this number until the hash that is outputted is less than the target. Since the hash values are random, the lower the target, the more difficult it is to find a valid hash. Just like rolling dice, it’s harder to roll a number less than 3 than it is to role a number less than 4.

How do you come up with the value of the target though? The answer to this is where we get into the topic of today: mining difficulty.

What is Mining Difficulty?

It’s quite self-explanatory, but the mining difficulty simply refers to how difficult and time-consuming it is to find a valid hash for the block. A better question to ask would be why mining even needs to be difficult in the first place.

The whole point of a blockchain is to create consensus among multiple individuals in a network, meaning to agree upon one truth. How they do this is by taking the longest valid chain in the network, determined by how much work (time spent guessing the hash) was put into it, and accepting this as the main chain to build off of.

Having this difficulty ensures the security of the network, as it prevents against fraud. If it were easy to add blocks, someone could send out a fraudulent transaction, then spam the network to create their own longest chain. With mining difficulty however, the process of adding blocks is slowed down to prevent spam, so that it takes an average of 10 minutes to mine a block.

The tricky part here is that an attacker could easily bypass the difficulty by finding ways to increase their computational power. Thankfully, we thought ahead and came up with a way for the system to regulate difficulty.

How is Mining Difficulty Determined?

The difficulty is adjusted periodically to ensure that it always takes an average of 10 minutes to mine a block. After every 2016 blocks that are added, the system looks at how long it took to create those 2016 blocks. Normally, it should take around 2 weeks, or 20160 minutes. If it took longer than 2 weeks, the difficulty would be reduced, and if it took less than 2 weeks, the difficulty would be increased. The difficulty can be determined as follows.

Formula to derive the new difficulty.

As you can see by the inverse proportionality, if it took longer than 2 weeks, the fraction would be less than 1, and the new difficulty would be reduced. If it took longer than 2 weeks, the fraction would be greater than 1, and the new difficulty would increase.

If the difficulty is reduced, the target value is increased, making it easier to find a valid hash, and vice versa. The maximum target value is 1, and from this, it can be seen that the difficulty is also inversely proportional to the target.

Relationship between the difficulty and the target.

In order to prevent abrupt changes, the difficulty cannot increase or decrease by more than a factor of 4 in one adjustment.

But returning to the question of how we come up with the target, we need to explore the different factors that affect how long it takes to mine a block.

There are two things that may affect this time:

  1. Computing power
  2. Number of individuals in the network

Computing Power

In the early days of Bitcoin, you likely could have been able to succeed in mining the way I used to think people did — with their PCs. However, miners were soon forced to upgrade.

Since mining is essentially a race, with people competing to find the hash first and get the reward, those with more powerful computers would be able to make more guesses in the same span of time, and would consequently have a higher chance of winning.

The upgrades started off with a simple switch from CPUs to graphics cards, but in recent years, far more upgrades have been made, with what are called ASICs (Application-Specific Integrated Circuits) — a device designed for the sole purpose of mining — now being the bare minimum needed to be able to compete.

But even with that, it’s very unlikely you’ll earn anything. Currently, you can expect to mine a block around once every 40 years. If you want to make money, you’ll probably have to join a mining pool — miners that join forces to increase their probability of winning.

Given all these advancements and the fact that computers are constantly getting faster and faster, the difficulty must compensate for this in order to keep up with them and maintain the 10 minute average.

Size of the Network

Wait, but it’s not like we’re getting faster computers every 2 weeks, right? While the constant improvement of computers was the main reason behind the need for the adjustment mechanism, the system also compensates for the number of individuals in the network.

The more miners there are, the less time it would take to find a valid hash, since more guesses are made in the same amount of time. As such, the difficulty increases when there are more miners present, and decreases when there are less.

Wrapping it All Up

So back to where we were at the beginning — the reason why it would be pretty much impossible to mine Bitcoin with your PC in today’s day and age is due to the constant increase in mining difficulty, resulting from compensations made for equipment upgrades and an increased network size. Your PC simply doesn’t have the computational power anymore to keep up with everyone else, so your chances of ever being able to successfully mine a block are extremely, extremely low.

Even though the system is designed so that difficulty can be reduced, it seldom does. Faster, more powerful equipment is constantly being developed, while more and more people are joining the Bitcoin network. In fact, since the creation of Bitcoin in January 2009 to November 2020, there have been 250 difficulty increases, and only 53 difficulty decreases.

Difficulty increases and decreases after the invention of ASICs. Source: Coindesk

TL;DR

  • Mining is the process of adding new blocks, and is done by finding a valid hash of the block.
  • A valid hash is one that is less than the target, found by varying the nonce of the block.
  • Mining difficulty is adjusted every 2016 blocks by evaluating how long it took to create them relative to the standard time of 2 weeks.
  • Mining difficulty was put in place to ensure the security of the blockchain, protect against attacks, and keep up with the increases in hashing power.

If you enjoyed reading this article or learned something new, be sure to drop a follow on Medium and connect with me on LinkedIn! Also, if you’d like to keep up with the projects I’m working on, feel free to subscribe to my monthly newsletter. Thanks for reading!

Also, Read

Get Best Software Deals Directly In Your Inbox

--

--

Samantha Ouyang
Coinmonks

Passionate about creating impact with emerging tech. Current obsessions include blockchain, empowering women in STEM, chemistry, and sustainable materials.