How does Bitcoin mining work?

In the previous lesson, you learned what a Bitcoin miner is, how it stores fresh transactions in its memory pool, and then starts the “mining” process by creating a candidate block.

If what I just wrote sounds like a foreign language to you, I strongly recommend you start with the first lesson of my Beginner’s Guide to Bitcoin Mining.

If you’re still with me, let’s take a closer look at the candidate block…

Just like you can be separated into two basic parts with a head and a body, a candidate block can be broken down into two parts:

  1. Block header (yellow area)
  2. Block body (gray area)
    Block Header and Body

If we look closer at a candidate block, it actually does NOT just contain transactions.

Candidate Block Anatomy

When a miner creates a candidate block, it includes a “block header” (yellow area).

The “body” of the block (gray area) holds the transactions.

Aside from a candidate block containing a bunch of transactions, it also contains some metadata that provides a “summary” of the block.

Metadata is just data that provide information about other data. In this case, the metadata provides information about the block itself. This information is known as the “block header”.

Inside the block header, there are several pieces of data.

For the purpose of this lesson, I’m going to simplify what’s inside the block header.

Timestamp

The first piece of data inside the Block Header is the “Timestamp”.

The timestamp indicates when the block was created.

Timestamp

Previous Block Hash

The next piece of data is called the “Previous Block” or more specifically, the “Previous Block Hash”.

The Previous Block Hash is the block hash of the prior block in the blockchain.

Previous Block Hash

In order to understand what this means, we need to take a look at the file.

Look inside blockchain

Now that we’re looking inside the file, we can see that the data is structured in a specific way.

Look inside the file and see blockchainstructure

Let’s visualize this as the blockchain.

The candidate block that the miner has created has to be added on top of the most recent block.

Block must bee added on top of the most recent block

Let’s say the most recent block is the one colored in blue.

Most recent block in blockchain

Since the Block Hash of block colored in blue is the most recent block in the file (the “blockchain”), then that’s the block that the miner wants to build on top of.

Think of the Block Hash as like a custom-generated ID number for a block.

So what the miner will do is add this as the “Previous Block Hash” in the Block Header of its OWN candidate block.

Block hash added as Previous Block Hash of Candidate Block

“Transaction Summary” Hash

The next piece of data is what I’ll call the “Transaction Summary” Hash.

As the name suggests, it provides a “summary” of all the transactions in the block expressed as a string of characters.

Without going into specifics, what happens is that all the transactions in the block (highlighted in red) are put into a hash function and hashed in a certain order and you end up with a single hash.

Merkle Root

 

This hash is what is used as the “Transaction Summary” Hash.Merkle Root Example

What this hash provides is a unique “fingerprint” based on all the transactions included in a block.

This helps to ensure that transactions haven’t been tampered with.

This “Transaction Summary” Hash is actually known as the “Merkle Root“. 🤓

Remember, one tiny change in data will totally change the hash. This means if someone were to try to alter any transaction in the block in the future, it would cause the “Transaction Summary” Hash to change and be totally different from the original value that’s in the Block Header.Merkle Root as Fingerprint

Basically, changing the transaction would create a totally different “fingerprint” and wouldn’t match the original “fingerprint”.

This change would let everyone instantly know that a transaction(s) was tampered with.

Block Hash

So far, the Block Header contains three pieces of data:

  1. Timestamp
  2. Previous Block Hash
  3. “Transaction Summary” Hash

The next step then is to put this Block Header data into a hash function.

This will produce a random number called the “Block Hash”.

(Although a more accurate name would be the “Block Header Hash” since it’s the hash of just the block header, not the entire block.)

Just like the Previous Block Hash mentioned earlier, think of the Block Hash as a custom-generated ID number that will be assigned to the candidate block.

Run block header through hash function
Why does the miner do this?

In order for the miner to get its block added to the blockchain, it must find a Block Hash that meets a certain requirement.

If it can NOT meet this requirement, then the block will NOT be considered valid and the miner can’t add its block to the blockchain.

More specifically, the Block Hash must be a number that starts with a certain number of zeros.

How many zeros exactly?

It varies. The software of the Bitcoin system determines the minimum number of zeros that the Block Hash should begin with.

Originally, the requirement was for only a small number of zeros, but as more and more miners joined, the Bitcoin software started requiring a greater number of zeros.

Bitcoin mining process without nonce

A miner must generate an output (the “Block Hash”) that satisfies the output threshold set by the Bitcoin software.

This “threshold” just specifies the numbers of zeros that the Block Hash must begin with.

It’s like the “limbo” dance.

Bitcoin mining is like a limbo dance

The lower the threshold, the more zeros that are required.

The more zeros that are required, the higher the difficulty to find a “correct” Block Hash.

This is because more leading zeros means fewer possible solutions, which means more time is required to “find” a correct Block Hash.

Finding a value that starts with three zeros like “000123…” is much harder than trying to find a value with six zeros like “000000123…

Here’s a recent example of an actual valid Block Hash:

0000000000000000000586b367c292dfd274bf2e67575cf8b4d00735fc1df6ff

Notice how many zeros it starts with.

The “certain number of zeros” comes from what’s called the “mining difficulty”. This is automatically adjusted up or down by the software running the Bitcoin network. The difficulty changes based on the number of miners. The more computers that are mining, the greater the difficulty and the more zeros are needed at the start making it harder to find the winning nonce.

A Block Hash can be interpreted as a very large number and must be below a certain threshold. This is why block hashes start with a series of zeros followed by an alphanumeric string. Some blocks have as many as twenty leading zeros, while earlier blocks have as few as eight. The number of zeros required roughly demonstrates the difficulty of mining at the time the block was published.

What happens if the Block Hash doesn’t start with the required number of zeros?

Then the block can NOT be added to the file (the “blockchain”).

Fortunately, the miner can have multiple attempts.

But if a miner ran just the Block Header containing just the Timestamp, Previous Block Hash, and “Transaction Summary” Hash through the hash function again, it would always get the same result.

The miner needs to alter the data somehow before trying to hash the Block Header again.

Bitcoin provides a way!

The Nonce

In the Block Header, there is an extra field called the “nonce” (light blue box)

Block header contains a nonce

There is a special part of the Bock Header that miners can populate with an arbitrary number.

The miner is free to put any number it wants in.

The nonce is completely separate from the transactions in the block. Its only purpose is to allow miners to fill it with a number, and change the number if the Block Hash doesn’t meet the requirement of starting with a certain number of zeros.

By using a nonce, a miner can manipulate the output of the hash function to “guess” the desired output.

Since the other pieces of data in the Block Header can’t be changed, the only piece of data that can be altered is the nonce.

So if the first hashing attempt of the Block Hader fails, the miner changes the value of the nonce.

You can think of this process as like trying to find the combination of a combination padlock.

Combination Lock

There are no shortcuts possible when trying to find the combination of numbers that unlocks it. You have to try every possible combination until at some point, you find the correct one by chance.

You have to guess over and over until you get lucky!

And just as it’s extremely easy and quick to check that a lock combination is the correct one (it either unlocks or doesn’t), it is extremely easy and quick for the miner, and for other nodes after, to check that the nonce is indeed the right one.

The miner is also competing against other miners. The first miner to find a nonce that results in a valid Block Hash is granted the right to add its block into the blockchain and is rewarded for doing so.

For example, let’s say the Block Hash needs to start with at least four zeros.

The only way to find a Block Hash with the required number of initial zeros is to randomly pick a nonce value and run the Block Header through the hash function.

In this scenario, the miner starts with “0” for the nonce value (light blue box).

Nonce is 0

If that fails to produce the desired result, then all the mincer can do is to try again with a different nonce.

The miner changes the nonce, then starts over again until it manages to find a Block Hash with at least the required minimum number of zeros. In this case, four zeros.

Remember, even though you’ve only made a small adjustment, with a hash function, even the tiniest change will produce completely different results.

So the miner’s first attempt was to use the number “0” in the nonce. It then runs the Block Header through the hash function and sees if the Block Hash starts with four zeros.

As you can see, the Bloch Hash only starts with two zeros so it does NOT meet the criteria needed to add the block to the blockchain.

If the output is not valid, the miner keeps trying with different nonce values.

Since the value of “0” didn’t work, the miner can change the value of the Nonce to“1” (light blue box) and then run the Block Header through the hash function again.

Nonce changed to 1

As you can see, the Block Hash starts with no zeros so this doesn’t work either.

Since the value of “1” didn’t work, the miner can change the value of the Nonce to“2” (light blue box) and then run the Block Header through the hash function again.

Nonce changed to 2

In this case, the Block Hash finally starts with four zeros and meets the criteria.

It’s a successful Block Hash result! 🎉

As you can see, it’s unpredictable which nonce will produce a Block Hash with the correct number of zeros, so the miner has to keep trying different nonces to find the right value.

Miners can mine for long periods of time with no guarantee that they will ever find a correct nonce. Most never will!

The nonce is used as a counter, where its value is just constantly incremented until either it finds the correct Block Hash or some other miner does.

It’s like a number guessing game!

You’re simply trying to find a hash that begins with a certain number of zeros by changing the value of the nonce.

Miners do this repeatedly….HOPING that the result will produce a Block Hash that meets the criteria.

The more powerful their computers are, the more “guesses” they can make.

This “guessing” process is known as mining.

The “winning” miner is rewarded with a set number of bitcoin (plus transaction fees) called the “block reward.”

One miner wins the block reward roughly every 10 minutes.

Block Reward

Since miners expend so much time and energy to “guess” the correct nonce and confirm transactions on the Bitcoin blockchain, the Bitcoin software rewards them for their effort with:

  1. Newly created bitcoin When a miner confirms transactions, they are bundled in a block and added to the Bitcoin blockchain. For every new block that gets added to the blockchain, new bitcoins are paid out to the miner. This is known as the block subsidy. The specific amount of bitcoins is based on an issuance schedule determined by the protocol at the time of the block creation. Currently, the new issuance is 6.25 BTC per block and will decrease to 3.125 BTC per block around May 2024.
  2. Transaction fees. When people transact with bitcoin, they pay a fee to the miners to incentivize them further to confirm their transaction. When a miner confirms a bundle of transactions, it also collects all the fees from each individual transaction.

Mining is a monotonous and repetitive process:

Bitcoin Mining Process

Take a Block Header with the nonce, hash it, see if the hash starts with a certain number of zeros, and if not, repeat the process with a different nonce.

I’m getting sleepy just thinking about it. 😴

So when you read or hear that mining involves ‘“solving complex mathematical problems”, now you know that’s not a correct description.

There is nothing complex about mining!  The process itself is pretty simple. You’re just trying to guess a correct number as fast as possible or just fast enough to be the FIRST miner to guess right so your candidate block is the one that gets added to the blockchain. That’s it!

Hash rate” indicates the “guessing power” of a mining computer. The more hashes you can guess per second, the higher your hash rate. For example, 1 trillion (1,000,000,000) guesses per second is equal to a hash rate of 1 terrahash (TH/s).

Just because the process is simple, it doesn’t mean it’s easy. That’s why it’s called “mining”. You’re trying to “dig” up the correct number which requires a lot of effort, similar to actual mining in the real world.

Now that the miner has found a nonce to be able to generate a Block Hash that meets the requirements (starts with at least 4 zeros), the block is added to the miner’s copy of the file (the “blockchain”).

Remember how the blue block was the most recent block in the miner’s blockchain? Not anymore.

Since the miner was able to guess the correct nonce, it was able to add its block to the blockchain (colored in green).

Block added to blockchain

So from a high-level view, the file on the miner’s computer has been updated (denoted in green).

Miner's blockchain updated

And then, the node will transmit this block to nearby nodes to which it is directly connected.

The nodes will check it, verify it, and make sure the Block Header produces a Block Hash that meets the criteria. In this case, the Block Hash needed to start with four zeros.Bitcoin miner propogates new block

These nodes will update their file (the “blockchain”) by adding the block to the file.

THEIR file will be updated and now be the same version as the winning miner’s (green file).

All the transactions in their mempool that are part of this winning block move from “fresh” (or “unconfirmed”) to “confirmed” status and are now permanently added to the file.

Any conflicting transactions will be kicked out.

Thanks to the process of mining, only the purple transaction will make it into the blockchain.Blockchain synced across network

Then these nodes will pass on the block to other nodes.Nodes will pass on the block to other nodes.

Once the new block is accepted by a majority of miners, all miners start over and create a brand new candidate block and the mining process repeats itself.

Mining acts as a sorting mechanism for transactions across a network of computers, where “mined” blocks have the final say on what belongs in the blockchain.

The Block Hash from this newly created block will now be used by miners for the “Previous Block Hash” in their candidate blocks.

Newly created block will now be used by miners

The Block Hash acts as the “chain”.

Miners must now include the previous block’s Block Hash in the new block they are creating.

Miner include new block hash in new block

For example, this means that to mine block 101, miners need to know the hash of block 100.

Until block 101 has been mined, block 102 can’t be mined.

This forces miners to focus on block 101 which in turn includes the hash of block 100. No miner can skip ahead.

But instead of having each block have a “block number” each block refers to the previous block by its Bock Hash.

This implies that every block is linked to each other and this is what creates a “chain of blocks” known as a blockchain.

This “chain of blocks” is held together not by block numbers, but by block hashes.

Why You Can’t Cheat Bitcoin

Let’s say everyone is working on block 101.

But one miner wants to alter a transaction in block 80.

It would have to make its changes and redo all the computations for blocks 80-100 AND also do block 101.

That’s 21 blocks of expensive computing!

Not only that, but the miner would have to do it all before the other miners on the Bitcoin network finish the current block (block 101) that they’re working on. This is basically impossible.

It is very hard to get away with tampering with any transactions that already form part of the blockchain.

This is what is meant when blockchains are described as immutable.

Proof-of-Work (PoW)

The process of mining is often referred to as a “Proof-of-Work” or PoW.

PoW is known as the “consensus mechanism” that Bitcoin uses to get all nodes to agree on the “official” copy of Bitcoin’s blockchain. It is how Bitcoin is able to reach a consensus without the need for dispute resolution or intervention from a central authority.

The term “proof-of-work” just refers to the fact that it takes “work” to find a block hash below a certain threshold. And once you do, anyone can verify it which provides the “proof”.

The process of “guessing a nonce” and then running the block header through the hash function is being used as a way to prove that you have performed a required amount of “work” on your block.

The first miner to produce a valid hash is allowed to add a new block to the blockchain and they receive a reward of bitcoins.

If a surge of miners joins the Bitcoin network, the PoW will adjust to make it more difficult to find a valid block hash. If this increased difficulty discourages too many miners and they stop mining, the difficulty will be decreased to make it easier to mine.

This process, known as the difficulty adjustment, occurs roughly every two weeks, and it ensures that new blocks are added to the blockchain roughly every 10 minutes, no matter how many miners join or leave the network.

Difficulty adjustments are made based on the speed at which miners are creating new blocks.

By adjusting the difficulty of mining, Bitcoin can ensure new bitcoins are created at a predetermined rate, regardless of the amount of computing power participating in the network.

The difficulty adjustment is important because it ensures that miners can’t mine too much of the supply of the bitcoin too fast.

The specialized computer hardware and electricity required to mine also means that attacking the network is prohibitively expensive. This ensures the security and integrity of Bitcoin’s blockchain.

Anyone with enough computing power to attack the network is better off mining honestly and earning the block reward (new bitcoins plus transaction fees).

Interested in learning more about the technical aspects of Bitcoin? learnmeabitoin.com provides an awesome easys-to-follow guide to help folks understand how Bitcoin works.

Summary

Congratulation! You now know how “mining” in Bitcoin works!

  • Miners search for an acceptable hash that will be the “Block Hash” by choosing a nonce, running the hash function, and checking the output.
  • If the hash doesn’t have the right amount of leading zeroes, miners change the nonce, run the hash function, and check again.
  • When a miner is finally lucky enough to find a nonce that works, and “wins”, the block gets sent out to other nodes (including other miners) in the Bitcoin network, each of whom can then run the hash function with the winning miner’s nonce, and verify that it works.
  • If the solution is accepted by a majority of the nodes, the block gets added to the blockchain and the winning miner gets the block reward.
  • All miners “reset” and start working on a new candidate block, using the recently mined block’s hash as a reference in their Block Header (as the “Previous Block Header”).
  • This process where miners try to “guess” or find a correct nonce is known as a “Proof-of-Work” (PoW).

Now that we’ve learned about miners and mining, let’s update our map of the Bitcoin system:

Bitcoin as a System (Mining Added)