Bitcoin Proof of Work — The Only Article You Will Ever Have to Read

Bitcoin Proof of Work — The Only Article You Will Ever Have to Read

How exactly does proof of work mining works? Well, it’s actually pretty simple. Let me give you a clue: the miners don’t need to solve a “complex” mathematical problem. Einstein used to solve complex mathematical problems. Miners are just trying to guess the lottery.

If you have been reading my articles, you know that I like to go straight to the point. This is not a novel, and I don’t want to waste the time of my readers. Still, this article is pretty long to please bear with me!

Proof of Work Mining

Mining is the method that is used in the blockchain to group transactions into a block, append this block to the blockchain and broadcast the new block to the network. Mining ensures the consensus mechanism is maintained and keeps the blockchain decentralized.

We are going to see in-depth how mining works in this article. This will be the only article you will ever have to read about proof of work consensus. Really. It’s all here.

Mining algorithms

Proof of work blockchains has mining algorithms or cryptographic hash functions that takes a block as an input and creates a small output. The hash function is designed so that the miner has to keep hashing blocks until he brutes the desired output. Continue reading, and I will show you how this happens. To complete this task, the miner needs to try this same task a million or billions of times, requiring hardware, electricity and time.

For Bitcoin, the mining hash function is SHA256 and Bitcoin applies a double-SHA256 process for increased security. Ethereum uses Ethash or Dagger-Hashimoto Keccak. Different proof of work blockchains use different algorithms, but the most well known are:

  • SHA256–256 bit “Secure Hashing Algorithm” can be mined by general-purpose CPUs, and it’s used by cryptocurrencies such as Bitcoin, Bytecoin, Peercoin, Namecoin and a few more. SHA256 is a very easy function, i.e. it is a simple boolean operation and 32-bit addition. Simple functions like this can be implemented in digital logic in ASIC chips, creating very efficient mining hardware.
  • Ethash — Ethash is Ethereum’s proof of work mining algorithm. Because it’s more memory intensive, GPUs offer a more efficient hash rate. ASIC miners were also developed to mine Ethash. Some of the blockchains using this algorithm, including Etehreum and Ethereum Classic.
  • X11 — Dash developers created the X11 algorithm, and it’s considered one of the safest. ASIC miners were also developed for X11 mining. Some of the cryptocurrencies using it are Dash, Karmacoin and MonetaryUnit.
  • Scrypt — Scrypt is also one widely used mining algorithm. It’s faster than the SHA256, and the leading blockchains using it are Dogecoin ?, Litecoin, Potcoin, MidasCoin and Gulden. Talking about Scrypt, I made The First-Ever Dogecoin Course. Check it out!
  • Cryptonight — this is another proof of work algorithm with a cool name. It was designed to be more efficient with CPU mining. The cryptocurrencies using it are Monero, Dashcoin, DigitalNote, Bytecoin, among others.

CPU Mining, GPU Mining and ASIC Mining

CPU, GPU and ASIC mining respond in different ways to the different blockchain algorithms. Some algorithms are more easily mined (i.e. have a better hash rate) with CPU — more general-purpose — GPU — more resource-intensive for simple computations and better for memory intensive algorithms — or ASIC — designed for specific purposes. ASIC miners, although very efficient, they also have some disadvantages. Some blockchains developers have been trying to update the protocols in order to make them ASIC resistant, i.e. not possible to mine using ASIC devices. When this happens, the ASIC miners are rendered useless because they were designed with a specific algorithm in mind and can’t perform any other task. Some blockchain developers believe that ASIC devices may increase centralization.

Note that this applies only to proof of work blockchains and not proof of stake or permission blockchains.

Proof of work

Let’s start by looking at proof of work, Bitcoin’s consensus mechanism, and most first-generation blockchains. Proof of work is a leader-based consensus mechanism. BTW, if you wanna decode Bitcoin’s genesis block coinbase transaction, check this article.

The miners need to calculate a challenging lottery or mathematical problem in the Bitcoin blockchain and other blockchains using a proof of work consensus mechanism. It’s a bit like trying to guess the number of a lottery ticket ?. Once a node resolves the lottery, he broadcasts the result to the other nodes, and a new block is appended to the blockchain.

The lottery’s difficulty is adjusted to make sure that all the miner’s computing power generates one Bitcoin block on average every 10 minutes. Suppose the number of miners or computational power in the network increases, the difficulty of calculating the mathematical problem will increase to keep the coin distribution/supply and the block production predictable. In that case, proof of work difficulty is adjusted when more people join or leave the network.

In other blockchains such as Ethereum, blocks may are added with different frequency. In Ethereum, for example, a new block is added every 15 seconds.

You can see proof of work as a string of data that is difficult to produce (it is time-consuming and resource-intensive) but once discovered, and it is easy for others to verify it.

One of the reasons it requires some work to add a new block to the blockchain is to make it more secure. Each miner needs to solve a difficult lottery to add a new block, but if it was too easy, then any hacker could easily try to add new blocks or change the blockchain to his benefit. Proof of work consensus is what makes Bitcoin the most secure public network ever created in human history.

The mathematical problem is nothing more than guessing a nonce, a long string of numbers with millions or billions of trial and error. In order words, brute-forcing the result. A miner must guess a nonce, add it to the hash of the current header, rehash the value, and compare the result to the target hash.

I’m sure you have also heard about the mining difficulty. The mining difficulty is mostly a measure of how hard it is to find a new block in the blockchain or, in other words, how rare is the nonce is that miners needed to brute force to find a hash smaller than the target hash.

I always thought that it is quite funny when people say that what miners are doing is to “solve a very complex mathematical problem” when what they are really doing is trying to guess a “lottery” number. Miners are not Einstein! They are not trying to find the mathematical wonders of the universe. ?

In most public/permissionless blockchain, miners will receive a block reward for their work. This reward will likely pay their expenses related to hardware and electricity, and it is an incentive to keep running the network. In 2021, the Bitcoin block reward is 6.25 BTC, and it will continue to be 6.25 until 2024 when it halves again (it halves approximately every 4 years or, to be more accurate, every 210 000 blocks). So in 2024, the Bitcoin block reward will be 3.125 BTC. In 2028 it will be 1.5625 BTC and so on. Because of this block reward halving, the Bitcoin maximum supply is capped at 21 million Bitcoins, and it will be reached in 2140.

Proof of work is used mainly by permissionless blockchains where any person can add a node to the blockchain, and the identities of nodes in the blockchain are not known. Thus, it is necessary to have a consensus mechanism that allows nodes to collaborate in a trustless way.

Proof of work accounts for 90% of the total market capitalization of existing cryptocurrencies. It requires work from the nodes that are participating in the validation of blocks to, well, do some difficult work. Miners need a certain incentive to perform this work. We will also see that it takes many quadrillion calculations to mine one block in the Bitcoin network. That’s why in all proof of work blockchains, miners receive a block reward incentive, transactions fees or both.

The economic incentive behind proof of work is a perfect example of how to keep the consensus in a completely decentralized network. To be a node/miner in a proof of work blockchain requires hardware resources, electricity and time, but if nodes play by the rules, i.e. according to the consensus mechanism, they receive a reward. If nodes try to attack the blockchain, tamper with data or perform double-spending, they will not get the reward, and they will be just wasting resources.

In 2020, miners received around 7 Billion dollars worth of Bitcoin as a block reward and about 800 Million dollars in Bitcoin fees paid by the users. Not bad, huh? Being a miner is a rough job tho.

Pros of proof of work:

  • Scalability of the network (in terms of node count)
  • Security increases with node count, i.e. bigger the mining community, the more secure it is
  • Miners receive an incentive in exchange for their computing power contribution to the network
  • Very secure in most cases

Cons of proof of work:

  • Vulnerable to 51% attacks (in small proof of work blockchains)
  • Mining difficulty usually gets harder, and mining hardware get obsolete quickly (profitability can go to zero in 3 years only)
  • Mining is expensive and usually requires specialized hardware to be profitable
  • It requires huge quantities of electricity
  • Proof of work blockchains generally have low throughput (studies show that proof of work blockchains can achieve up to 60 transactions per second without compromising security)
  • It takes time to have a transaction verified in the blockchain depending on the blockchain protocol being used — this is why it is recommended to wait 10 to 20 minutes until a transaction is verified/committed in the Bitcoin network (1 or 2 blocks) or 5 minutes in the Ethereum blockchain (around 20 blocks). On the other hand, Dogecoin blocks are mined every 1 minute.

The Hash rate

Hash rate is the number of computing hashes that a hardware can perform. Different hardware has different hash rates. From CPUs to GPUs or ASIC chips — application-specific integrated circuit — miners try to have the most efficient hardware in order to have the best hash rate possible, which will increase their probability of mining a block in the blockchain and receive the rewards for it.

Difficulty

The difficulty represents how hard it is to find the hash needed to mine a new block in the blockchain in a proof of work blockchain. The difficulty represents the number of possibilities/combinations for the miners to guess the hash. Greater the difficulty, more work the miners need to perform in order to guess the hash and create a new block. The difficulty is adjusted in the Bitcoin blockchain so that new blocks are created on average every 10 minutes. If more miners join the Bitcoin network, thus contributing with more hash power, the difficulty will increase and adjust so that the miners discover the hash on average every 10 minutes.

The mining difficulty is adjusted every 2016 blocks, which is approximately two weeks considering an average of 10 minutes per block. Bitcoin algorithm provides this adjustment very easily. If the previous 2016 blocks took more or less than two weeks to find, the difficulty is increased or decreased in the proportion of the amount of time difference to the two weeks.

How the proof of work mining process works

Mining Bitcoin is harder than mining gold. Imagine having to repeat hashes quadrillions of times until you find the correct one!

Different blockchains may have different mechanisms but let’s look at our genesis blockchain, Bitcoin, as an example.

In bitcoin mining, miners need to come up with a block hash that meets a certain requirement. To come up with this hash, they need to change a piece of data inside the block called the nonce, and they need to do it a LOT of times. This is the lottery guessing process. Sounds complicated? No worries, let’s make it simple.

How many hashes on average do bitcoin miners worldwide perform to mine one block? Well, as I said, a lot of times:

hashes per bitcoin
= (network hash rate) / (6.25 BTC per 10 minutes)
= (140 * Th / s) / (6.25 * BTC / (600 * s) )
= 140 * 600 / 6.25 * Th / s / BTC * s
= 13,440 Th / BTC
= 13,440,000,000,000,000 h/BTC

We divide the current network hashrate per second by 6.25 (BTC block reward per 10 minutes), and we get a VERY big number.

Miners need to calculate on average 13.44 Quadrillion hashes to generate one single Bitcoin as per January 2021. If we multiply this by 6.25, it takes 84 Quadrillion hashes to mine one single block! Miners need to repeat the calculation in the figure above literally quadrillions of times until a block is mined. This is why Bitcoin mining spends so much electricity but, on the other hand, give us the most secure blockchain ever.

Imagine hypothetically that you are trying to find mine this Bitcoin block all by yourself and that all you have is the best CPU in the market, the AMD Ryzen 9 3900X 12-Core Processor. It would take you 225 730 years to mine one single Bitcoin block at the current difficulty.

Unfortunately, it’s pretty useless to mine Bitcoin with a normal computer. This was possible back in 2009 and 2010 when the hash power in the network was still low, but currently, only very powerful hardware like specialized ASIC miners can do the job. Back then, not many people were interested in mining Bitcoin, and the BTC price was only a few cents.

The early believers in Bitcoin that mined it in the early days were able to buy the Lambos ?.

But… why is that so difficult to mine Bitcoin?? And how is the mining process done?

As we saw before, the proof of work difficulty increases when more hashing power is added to the network. The difficulty and bits field of a block represents the number of zeros that the target hash has. This number of zeros at the beginning of the hash is a condition that miners need to meet. To generate a hash that corresponds to this condition, miners will have to follow the process below, trying different nonces until they create the needed hash.

To create a block hash (i.e. to mine a block), you need to meet certain conditions in proof of work. Some small variations may apply depending on the algorithm used, but the condition is usually to find a hash whose value that is under a certain requirement. In other words, the hash needs to start with a number of zeros.

The process is quite simple yet very repetitive (miners repeat it billions or quadrillions of times, as we saw).

The diagram that I made is very clear. According to the diagram, to perform the necessary work to mine the block and get the block hash, the miner will perform the task. This is where the nonce, one of the fields in the block header, comes into play. A nonce is a 32-bit number, and the miner will need to guess so that it will create a hash according to the target.

  1. The miner will create the block header, convert it into a string of data
  2. The miner adds a nonce to the data and feeds that to a hashing algorithm (the nonce is the only part of the block that the miner can freely change)
  3. Get the resulting hash
  4. Run a test to check if the condition, i.e. the target hash, is met
  5. If the condition is met, it means that the miner found the nonce that generates a hash according to the target
  6. If the condition was not met (most likely), the miner will try a different nonce, i.e. he goes back to point 2, trying a new nonce until it produces a hash that meets the target

Miners will most likely have to perform these computations quadrillions of times until they get the correct value.

Once a miner finds the correct hash, he will broadcast the block to the network, and the other miners can easily verify that that nonce produces a hash according to the target. The miner that mined the block, will also include in the block the coinbase transaction. This transaction corresponds to the block reward that he will receive.

Dices, Bits and Target

In mining, the miners try to find hash values lower than the target value (this is a field in every blockchain block). The hash value must be lower than the value represented by the bits field in the block header. This will also mean that the target hash will have to start with a number of zeros. This number of zeros will increase with difficulty. The greater the difficulty, the harder it is to mine a block because it’s harder to find a hash meeting the required target.

Why do these “zeros” in the hash impact so much the difficulty? If you think the probability of hashing a block with, let’s say, 20 straight zeros, it’s extremely low. It’s like trying to find a lottery thicket among quadrillions of lottery tickets that starts with 20 zeros. Or the same as tossing A dice and get the same number 15 times in a row. It’s a very very low probability, and that’s why it is so hard.

To illustrate the probabilities and how hard it is to get a hash with a big number of zeros to be lower than the target hash, let’s imagine that we are playing with dices. What’s the probability of tossing 15 dices and get them all with the same value?

P = pⁿ = (1/s)ⁿ

Assuming that our dices are 6 sided dices:

1 dice: (1/6)¹ = 0.16 or 16% probability

2 dices: (1/6)² = 0.02(7) or 2.77% probability

4 dices: (1/6)⁴ = 0.00077160493827 or 0.077160493827% probability

6 dices: (1/6)⁶ = 0.00002143347050 or 0.002143347050% probability

15 dices: (1/6)¹⁵ = 0.00000000000212682249 or 0.000000000212682249%

This probability which means that you would need to toss the 15 dices on average 470 184 984 737 times to get them all facing the same value. That’s 470 billion times!

You see now how it is harder than it looks to get all dices with the same value or a hash with so many “zeros”.

Now that we understand the concept let’s look at it with the actual formulas and see how miners find the hash according to the difficulty.

Difficulty = MAX_TARGET / current_target

So MAX_TARGET is the difficulty that was set for the first block, and it’s basically Difficulty = 1, and it’s written as 1d00ffff hexadecimal value.

If we convert the bits value of our block of 386,867,735 to hexadecimal, we will get the value 0x170f2217. This value is divided into four bytes. The first is the index, followed by 3 bytes. In this case, bits are the index of 0x17 and the coefficient of 0x0f2217. Here, the 0x means that we are talking about a hexadecimal number, aka hex, a numerical system used in computing.

The target threshold is a 256-bit integer which the hash needs to be equal or below to meet the target hash. This is the target hash we were referring to in the mining process.

We can also compare this target value with the hash value, following a simple equation:

Target = coefficient * 2 ^(8 * (index — 3))

We need first to convert our bits value to hex:

386,867,735 à 0x170f2217

Then, we substitute the equation with our values,

0x0f2217 * 2^(0x08 * (0x17–0x03))

You can use your Python calculator to help with the dec to hex and hex to dec conversions:

Converting from hexadecimal to decimal, we get,

991767 x 2 ^ (8 x (23–3)) = 1449469094350757594478113895488529861555105250349678592

Which in hex is

0x0000 0000 0000 0000 00f22170000000000000000000000000000000000000000

Which when comparing to the hash of the block, we see that our hash of the block is lower:

0x0000 0000 0000 0000 00092f5f21cd6db1d374252d6e343bd7a0fcac448ef01a5

Easy right?

We could also use the Python calculator to go straight to the point and check if the hash is lower than the target.

Let’s calculate the target using the equation:

Target = coefficient * 2 ^(8 * (index -3))

As you can see, we get exactly the same result, which is a target value. If you want to try it by yourself, you can download python through your command prompt.

We can also check that the block hash is lower than the target:

This last function shows that the block hash integer is lower than the target integer, meaning that the hash met the target, i.e. condition is true. YAY! ????

? Follow me and also check my ? blockchain courses:

? The First-Ever Dogecoin Course

?‍? Fintech, Cloud and Cybersecurity Course

? The Complete NFTs Course

?‍? Unblockchain Course — The Brain-Friendly Blockchain Course


Bitcoin Proof of Work — The Only Article You Will Ever Have to Read was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Henrique Centieiro

Bitcoin Proof of Work — The Only Article You Will Ever Have to Read

How exactly does proof of work mining works? Well, it’s actually pretty simple. Let me give you a clue: the miners don’t need to solve a “complex” mathematical problem. Einstein used to solve complex mathematical problems. Miners are just trying to guess the lottery.

If you have been reading my articles, you know that I like to go straight to the point. This is not a novel, and I don’t want to waste the time of my readers. Still, this article is pretty long to please bear with me!

Proof of Work Mining

Mining is the method that is used in the blockchain to group transactions into a block, append this block to the blockchain and broadcast the new block to the network. Mining ensures the consensus mechanism is maintained and keeps the blockchain decentralized.

We are going to see in-depth how mining works in this article. This will be the only article you will ever have to read about proof of work consensus. Really. It’s all here.

Mining algorithms

Proof of work blockchains has mining algorithms or cryptographic hash functions that takes a block as an input and creates a small output. The hash function is designed so that the miner has to keep hashing blocks until he brutes the desired output. Continue reading, and I will show you how this happens. To complete this task, the miner needs to try this same task a million or billions of times, requiring hardware, electricity and time.

For Bitcoin, the mining hash function is SHA256 and Bitcoin applies a double-SHA256 process for increased security. Ethereum uses Ethash or Dagger-Hashimoto Keccak. Different proof of work blockchains use different algorithms, but the most well known are:

  • SHA256–256 bit “Secure Hashing Algorithm” can be mined by general-purpose CPUs, and it’s used by cryptocurrencies such as Bitcoin, Bytecoin, Peercoin, Namecoin and a few more. SHA256 is a very easy function, i.e. it is a simple boolean operation and 32-bit addition. Simple functions like this can be implemented in digital logic in ASIC chips, creating very efficient mining hardware.
  • Ethash — Ethash is Ethereum’s proof of work mining algorithm. Because it’s more memory intensive, GPUs offer a more efficient hash rate. ASIC miners were also developed to mine Ethash. Some of the blockchains using this algorithm, including Etehreum and Ethereum Classic.
  • X11 — Dash developers created the X11 algorithm, and it’s considered one of the safest. ASIC miners were also developed for X11 mining. Some of the cryptocurrencies using it are Dash, Karmacoin and MonetaryUnit.
  • Scrypt — Scrypt is also one widely used mining algorithm. It’s faster than the SHA256, and the leading blockchains using it are Dogecoin ?, Litecoin, Potcoin, MidasCoin and Gulden. Talking about Scrypt, I made The First-Ever Dogecoin Course. Check it out!
  • Cryptonight — this is another proof of work algorithm with a cool name. It was designed to be more efficient with CPU mining. The cryptocurrencies using it are Monero, Dashcoin, DigitalNote, Bytecoin, among others.

CPU Mining, GPU Mining and ASIC Mining

CPU, GPU and ASIC mining respond in different ways to the different blockchain algorithms. Some algorithms are more easily mined (i.e. have a better hash rate) with CPU — more general-purpose — GPU — more resource-intensive for simple computations and better for memory intensive algorithms — or ASIC — designed for specific purposes. ASIC miners, although very efficient, they also have some disadvantages. Some blockchains developers have been trying to update the protocols in order to make them ASIC resistant, i.e. not possible to mine using ASIC devices. When this happens, the ASIC miners are rendered useless because they were designed with a specific algorithm in mind and can’t perform any other task. Some blockchain developers believe that ASIC devices may increase centralization.

Note that this applies only to proof of work blockchains and not proof of stake or permission blockchains.

Proof of work

Let’s start by looking at proof of work, Bitcoin’s consensus mechanism, and most first-generation blockchains. Proof of work is a leader-based consensus mechanism. BTW, if you wanna decode Bitcoin's genesis block coinbase transaction, check this article.

The miners need to calculate a challenging lottery or mathematical problem in the Bitcoin blockchain and other blockchains using a proof of work consensus mechanism. It’s a bit like trying to guess the number of a lottery ticket ?. Once a node resolves the lottery, he broadcasts the result to the other nodes, and a new block is appended to the blockchain.

The lottery’s difficulty is adjusted to make sure that all the miner’s computing power generates one Bitcoin block on average every 10 minutes. Suppose the number of miners or computational power in the network increases, the difficulty of calculating the mathematical problem will increase to keep the coin distribution/supply and the block production predictable. In that case, proof of work difficulty is adjusted when more people join or leave the network.

In other blockchains such as Ethereum, blocks may are added with different frequency. In Ethereum, for example, a new block is added every 15 seconds.

You can see proof of work as a string of data that is difficult to produce (it is time-consuming and resource-intensive) but once discovered, and it is easy for others to verify it.

One of the reasons it requires some work to add a new block to the blockchain is to make it more secure. Each miner needs to solve a difficult lottery to add a new block, but if it was too easy, then any hacker could easily try to add new blocks or change the blockchain to his benefit. Proof of work consensus is what makes Bitcoin the most secure public network ever created in human history.

The mathematical problem is nothing more than guessing a nonce, a long string of numbers with millions or billions of trial and error. In order words, brute-forcing the result. A miner must guess a nonce, add it to the hash of the current header, rehash the value, and compare the result to the target hash.

I’m sure you have also heard about the mining difficulty. The mining difficulty is mostly a measure of how hard it is to find a new block in the blockchain or, in other words, how rare is the nonce is that miners needed to brute force to find a hash smaller than the target hash.

I always thought that it is quite funny when people say that what miners are doing is to “solve a very complex mathematical problem” when what they are really doing is trying to guess a “lottery” number. Miners are not Einstein! They are not trying to find the mathematical wonders of the universe. ?

In most public/permissionless blockchain, miners will receive a block reward for their work. This reward will likely pay their expenses related to hardware and electricity, and it is an incentive to keep running the network. In 2021, the Bitcoin block reward is 6.25 BTC, and it will continue to be 6.25 until 2024 when it halves again (it halves approximately every 4 years or, to be more accurate, every 210 000 blocks). So in 2024, the Bitcoin block reward will be 3.125 BTC. In 2028 it will be 1.5625 BTC and so on. Because of this block reward halving, the Bitcoin maximum supply is capped at 21 million Bitcoins, and it will be reached in 2140.

Proof of work is used mainly by permissionless blockchains where any person can add a node to the blockchain, and the identities of nodes in the blockchain are not known. Thus, it is necessary to have a consensus mechanism that allows nodes to collaborate in a trustless way.

Proof of work accounts for 90% of the total market capitalization of existing cryptocurrencies. It requires work from the nodes that are participating in the validation of blocks to, well, do some difficult work. Miners need a certain incentive to perform this work. We will also see that it takes many quadrillion calculations to mine one block in the Bitcoin network. That’s why in all proof of work blockchains, miners receive a block reward incentive, transactions fees or both.

The economic incentive behind proof of work is a perfect example of how to keep the consensus in a completely decentralized network. To be a node/miner in a proof of work blockchain requires hardware resources, electricity and time, but if nodes play by the rules, i.e. according to the consensus mechanism, they receive a reward. If nodes try to attack the blockchain, tamper with data or perform double-spending, they will not get the reward, and they will be just wasting resources.

In 2020, miners received around 7 Billion dollars worth of Bitcoin as a block reward and about 800 Million dollars in Bitcoin fees paid by the users. Not bad, huh? Being a miner is a rough job tho.

Pros of proof of work:

  • Scalability of the network (in terms of node count)
  • Security increases with node count, i.e. bigger the mining community, the more secure it is
  • Miners receive an incentive in exchange for their computing power contribution to the network
  • Very secure in most cases

Cons of proof of work:

  • Vulnerable to 51% attacks (in small proof of work blockchains)
  • Mining difficulty usually gets harder, and mining hardware get obsolete quickly (profitability can go to zero in 3 years only)
  • Mining is expensive and usually requires specialized hardware to be profitable
  • It requires huge quantities of electricity
  • Proof of work blockchains generally have low throughput (studies show that proof of work blockchains can achieve up to 60 transactions per second without compromising security)
  • It takes time to have a transaction verified in the blockchain depending on the blockchain protocol being used — this is why it is recommended to wait 10 to 20 minutes until a transaction is verified/committed in the Bitcoin network (1 or 2 blocks) or 5 minutes in the Ethereum blockchain (around 20 blocks). On the other hand, Dogecoin blocks are mined every 1 minute.

The Hash rate

Hash rate is the number of computing hashes that a hardware can perform. Different hardware has different hash rates. From CPUs to GPUs or ASIC chips — application-specific integrated circuit — miners try to have the most efficient hardware in order to have the best hash rate possible, which will increase their probability of mining a block in the blockchain and receive the rewards for it.

Difficulty

The difficulty represents how hard it is to find the hash needed to mine a new block in the blockchain in a proof of work blockchain. The difficulty represents the number of possibilities/combinations for the miners to guess the hash. Greater the difficulty, more work the miners need to perform in order to guess the hash and create a new block. The difficulty is adjusted in the Bitcoin blockchain so that new blocks are created on average every 10 minutes. If more miners join the Bitcoin network, thus contributing with more hash power, the difficulty will increase and adjust so that the miners discover the hash on average every 10 minutes.

The mining difficulty is adjusted every 2016 blocks, which is approximately two weeks considering an average of 10 minutes per block. Bitcoin algorithm provides this adjustment very easily. If the previous 2016 blocks took more or less than two weeks to find, the difficulty is increased or decreased in the proportion of the amount of time difference to the two weeks.

How the proof of work mining process works

Mining Bitcoin is harder than mining gold. Imagine having to repeat hashes quadrillions of times until you find the correct one!

Different blockchains may have different mechanisms but let’s look at our genesis blockchain, Bitcoin, as an example.

In bitcoin mining, miners need to come up with a block hash that meets a certain requirement. To come up with this hash, they need to change a piece of data inside the block called the nonce, and they need to do it a LOT of times. This is the lottery guessing process. Sounds complicated? No worries, let’s make it simple.

How many hashes on average do bitcoin miners worldwide perform to mine one block? Well, as I said, a lot of times:

hashes per bitcoin
= (network hash rate) / (6.25 BTC per 10 minutes)
= (140 * Th / s) / (6.25 * BTC / (600 * s) )
= 140 * 600 / 6.25 * Th / s / BTC * s
= 13,440 Th / BTC
= 13,440,000,000,000,000 h/BTC

We divide the current network hashrate per second by 6.25 (BTC block reward per 10 minutes), and we get a VERY big number.

Miners need to calculate on average 13.44 Quadrillion hashes to generate one single Bitcoin as per January 2021. If we multiply this by 6.25, it takes 84 Quadrillion hashes to mine one single block! Miners need to repeat the calculation in the figure above literally quadrillions of times until a block is mined. This is why Bitcoin mining spends so much electricity but, on the other hand, give us the most secure blockchain ever.

Imagine hypothetically that you are trying to find mine this Bitcoin block all by yourself and that all you have is the best CPU in the market, the AMD Ryzen 9 3900X 12-Core Processor. It would take you 225 730 years to mine one single Bitcoin block at the current difficulty.

Unfortunately, it’s pretty useless to mine Bitcoin with a normal computer. This was possible back in 2009 and 2010 when the hash power in the network was still low, but currently, only very powerful hardware like specialized ASIC miners can do the job. Back then, not many people were interested in mining Bitcoin, and the BTC price was only a few cents.

The early believers in Bitcoin that mined it in the early days were able to buy the Lambos ?.

But… why is that so difficult to mine Bitcoin?? And how is the mining process done?

As we saw before, the proof of work difficulty increases when more hashing power is added to the network. The difficulty and bits field of a block represents the number of zeros that the target hash has. This number of zeros at the beginning of the hash is a condition that miners need to meet. To generate a hash that corresponds to this condition, miners will have to follow the process below, trying different nonces until they create the needed hash.

To create a block hash (i.e. to mine a block), you need to meet certain conditions in proof of work. Some small variations may apply depending on the algorithm used, but the condition is usually to find a hash whose value that is under a certain requirement. In other words, the hash needs to start with a number of zeros.

The process is quite simple yet very repetitive (miners repeat it billions or quadrillions of times, as we saw).

The diagram that I made is very clear. According to the diagram, to perform the necessary work to mine the block and get the block hash, the miner will perform the task. This is where the nonce, one of the fields in the block header, comes into play. A nonce is a 32-bit number, and the miner will need to guess so that it will create a hash according to the target.

  1. The miner will create the block header, convert it into a string of data
  2. The miner adds a nonce to the data and feeds that to a hashing algorithm (the nonce is the only part of the block that the miner can freely change)
  3. Get the resulting hash
  4. Run a test to check if the condition, i.e. the target hash, is met
  5. If the condition is met, it means that the miner found the nonce that generates a hash according to the target
  6. If the condition was not met (most likely), the miner will try a different nonce, i.e. he goes back to point 2, trying a new nonce until it produces a hash that meets the target

Miners will most likely have to perform these computations quadrillions of times until they get the correct value.

Once a miner finds the correct hash, he will broadcast the block to the network, and the other miners can easily verify that that nonce produces a hash according to the target. The miner that mined the block, will also include in the block the coinbase transaction. This transaction corresponds to the block reward that he will receive.

Dices, Bits and Target

In mining, the miners try to find hash values lower than the target value (this is a field in every blockchain block). The hash value must be lower than the value represented by the bits field in the block header. This will also mean that the target hash will have to start with a number of zeros. This number of zeros will increase with difficulty. The greater the difficulty, the harder it is to mine a block because it’s harder to find a hash meeting the required target.

Why do these “zeros” in the hash impact so much the difficulty? If you think the probability of hashing a block with, let’s say, 20 straight zeros, it’s extremely low. It’s like trying to find a lottery thicket among quadrillions of lottery tickets that starts with 20 zeros. Or the same as tossing A dice and get the same number 15 times in a row. It’s a very very low probability, and that’s why it is so hard.

To illustrate the probabilities and how hard it is to get a hash with a big number of zeros to be lower than the target hash, let’s imagine that we are playing with dices. What’s the probability of tossing 15 dices and get them all with the same value?

P = pⁿ = (1/s)ⁿ

Assuming that our dices are 6 sided dices:

1 dice: (1/6)¹ = 0.16 or 16% probability

2 dices: (1/6)² = 0.02(7) or 2.77% probability

4 dices: (1/6)⁴ = 0.00077160493827 or 0.077160493827% probability

6 dices: (1/6)⁶ = 0.00002143347050 or 0.002143347050% probability

15 dices: (1/6)¹⁵ = 0.00000000000212682249 or 0.000000000212682249%

This probability which means that you would need to toss the 15 dices on average 470 184 984 737 times to get them all facing the same value. That’s 470 billion times!

You see now how it is harder than it looks to get all dices with the same value or a hash with so many “zeros”.

Now that we understand the concept let’s look at it with the actual formulas and see how miners find the hash according to the difficulty.

Difficulty = MAX_TARGET / current_target

So MAX_TARGET is the difficulty that was set for the first block, and it’s basically Difficulty = 1, and it’s written as 1d00ffff hexadecimal value.

If we convert the bits value of our block of 386,867,735 to hexadecimal, we will get the value 0x170f2217. This value is divided into four bytes. The first is the index, followed by 3 bytes. In this case, bits are the index of 0x17 and the coefficient of 0x0f2217. Here, the 0x means that we are talking about a hexadecimal number, aka hex, a numerical system used in computing.

The target threshold is a 256-bit integer which the hash needs to be equal or below to meet the target hash. This is the target hash we were referring to in the mining process.

We can also compare this target value with the hash value, following a simple equation:

Target = coefficient * 2 ^(8 * (index — 3))

We need first to convert our bits value to hex:

386,867,735 à 0x170f2217

Then, we substitute the equation with our values,

0x0f2217 * 2^(0x08 * (0x17–0x03))

You can use your Python calculator to help with the dec to hex and hex to dec conversions:

Converting from hexadecimal to decimal, we get,

991767 x 2 ^ (8 x (23–3)) = 1449469094350757594478113895488529861555105250349678592

Which in hex is

0x0000 0000 0000 0000 00f22170000000000000000000000000000000000000000

Which when comparing to the hash of the block, we see that our hash of the block is lower:

0x0000 0000 0000 0000 00092f5f21cd6db1d374252d6e343bd7a0fcac448ef01a5

Easy right?

We could also use the Python calculator to go straight to the point and check if the hash is lower than the target.

Let’s calculate the target using the equation:

Target = coefficient * 2 ^(8 * (index -3))

As you can see, we get exactly the same result, which is a target value. If you want to try it by yourself, you can download python through your command prompt.

We can also check that the block hash is lower than the target:

This last function shows that the block hash integer is lower than the target integer, meaning that the hash met the target, i.e. condition is true. YAY! ????

? Follow me and also check my ? blockchain courses:

? The First-Ever Dogecoin Course

?‍? Fintech, Cloud and Cybersecurity Course

? The Complete NFTs Course

?‍? Unblockchain Course — The Brain-Friendly Blockchain Course


Bitcoin Proof of Work — The Only Article You Will Ever Have to Read was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Henrique Centieiro


Print Share Comment Cite Upload Translate Updates
APA

Henrique Centieiro | Sciencx (2021-05-20T14:23:25+00:00) Bitcoin Proof of Work — The Only Article You Will Ever Have to Read. Retrieved from https://www.scien.cx/2021/05/20/bitcoin-proof-of-work%e2%80%8a-%e2%80%8athe-only-article-you-will-ever-have-to-read/

MLA
" » Bitcoin Proof of Work — The Only Article You Will Ever Have to Read." Henrique Centieiro | Sciencx - Thursday May 20, 2021, https://www.scien.cx/2021/05/20/bitcoin-proof-of-work%e2%80%8a-%e2%80%8athe-only-article-you-will-ever-have-to-read/
HARVARD
Henrique Centieiro | Sciencx Thursday May 20, 2021 » Bitcoin Proof of Work — The Only Article You Will Ever Have to Read., viewed ,<https://www.scien.cx/2021/05/20/bitcoin-proof-of-work%e2%80%8a-%e2%80%8athe-only-article-you-will-ever-have-to-read/>
VANCOUVER
Henrique Centieiro | Sciencx - » Bitcoin Proof of Work — The Only Article You Will Ever Have to Read. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/20/bitcoin-proof-of-work%e2%80%8a-%e2%80%8athe-only-article-you-will-ever-have-to-read/
CHICAGO
" » Bitcoin Proof of Work — The Only Article You Will Ever Have to Read." Henrique Centieiro | Sciencx - Accessed . https://www.scien.cx/2021/05/20/bitcoin-proof-of-work%e2%80%8a-%e2%80%8athe-only-article-you-will-ever-have-to-read/
IEEE
" » Bitcoin Proof of Work — The Only Article You Will Ever Have to Read." Henrique Centieiro | Sciencx [Online]. Available: https://www.scien.cx/2021/05/20/bitcoin-proof-of-work%e2%80%8a-%e2%80%8athe-only-article-you-will-ever-have-to-read/. [Accessed: ]
rf:citation
» Bitcoin Proof of Work — The Only Article You Will Ever Have to Read | Henrique Centieiro | Sciencx | https://www.scien.cx/2021/05/20/bitcoin-proof-of-work%e2%80%8a-%e2%80%8athe-only-article-you-will-ever-have-to-read/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.