How does Bitcoin mining work

This post is following the previous one, in which I described the functioning of the Bitcoin network. It is more or less a translation of the last part of the same blogpost on Crypto Lyon’s website.

Introduction

In the last post, I tried to make understandable how Bitcoin works and how it is decentralized without using technical terms. In this one, I’ll use some more precise ones in order to describe a specific part of the operation : mining. What does a miner do exactly ? How is it done ? Which are the steps to get it done ?

I’ll try to be specific but not to overwhelm you with extra information. As always, if you have any question don’t be afraid to ask it on the Crypto Lyon Discord server.

The steps

Let’s get real. As I said in the Bitcoin overview, every node on the network keeps fresh transactions in the memory pool. A miner being a node, it can access new transactions to add in a block directly.

But, how to put them in ?

The block must respect a standard structure :

  • it is divided in 2 parts : the header (metadatas) and the content (the actual transactions)
  • each part contains specific fields

The header contains the magic bytes (4 bytes), the header length (4 bytes), a version number (4 bytes), the hash of the previous bloc (32 bytes), the merkle root (a specific way of hashing datas) of all the transactions in the block (32 bytes), a timestamp (4bytes), the difficulty target (4 bytes), the nonce (4 bytes), the number of transactions in the block (variable length).

The content contains all the transactions, each transaction containing multiple fields I won’t detail here.

What to do exactly ?

Now that we have a source of transaction and a pattern, let’s make a block ! Here are the steps a miner would do :

  • Get transactions from the mining pool with some rules : unless the miner wants to have some specific impact on the network, it will take the transactions with the higher rate of fees/byte.
  • The miner then add a transaction (the first of the set) to pay himself (the value of this transaction was of 50 bitcoins at the network launch and is divided every 4 years), called the coinbase transaction, and tries to do the best combination of transactions in order to get the most fees but trying to stay under the block size limit : since Segwit this limit is about 1.8MB. To be precise the limit is :
4 * NON_SEGWIT_DATA + 1 * SEGWIT_DATA <= 4000000 bytes
  • Once the miner has its transaction set, it “fills” the header by taking the main net magic bytes (0xd9b4bef9), computing the block length (nothing more will be added so the block size is now known), the version number depending of the client’s version (was 0x00000001 for the first block, now it is 0x20800000 at the time of writing), taking the hash of the previous block (the miner is a node, it has the chain), computing the merkle root of all the transactions he took, setting up the timestamp of the block (actually the timestamp is not accurate, see here for more infos), specifies the difficulty target used, and sets the nonce to 0.
  • At this stage, the block is filled and just needs one more thing : to be accepted by the network, which means its hash should be less than the target. So the miner computes the hash of the block header by passing it two times in the sha256 function.
  • If the hash is less than the target, then it sends it to the network.
  • If not, increment the nonce in the header (which means nonce=nonce+1) before hashing it again.
  • If the hash is less than the target, then it sends it to the network.
  • If not, increment the nonce in the header (which means nonce=nonce+1) before hashing it again.

Actually, a round of this process takes less than a second because it is what computers are aimed to do : brute computing. How many rounds a “computer” can do in a second is called the hashrate. You must have seen this term somewhere : now you know exactly what it refers to.

Conclusion

I presented you the basic method, used by the reference Bitcoin client : bitcoin-core. In 9 years things have evolved, so has mining algorithms. It results in that currently, the hashrate on the Bitcoin network is of 50exaH/s ~= 50*10¹⁹hash/s : to compare, there are approximately 9*10¹⁵ meters in a light-year.

This process of mining (Proof Of Work) externalizes the third party in the Bitcoin network and is still the only method to achieve it.

Here are some link if you want to dive a little deeper in mining :