Mining - How does mining work in Bitcoin?
  • Hash256
  • Hash160
  • Reverse Bytes
  • Hexadecimal

Mining Adding new blocks to the blockchain.

Mining is the process of trying to add a new block of transactions on to the blockchain. It is basically a network-wide competition where any node on the network can work to try and mine the next block on to the chain.

A newly-mined block is broadcast across the network, and each node will verify then add this block on to their blockchain. Each mining node will then restart the mining process to build on top of this new block in the chain.

The system designed so that a new block is mined roughly once every 10 minutes.

1. How does mining work?

The mining process begins by filling a candidate block with transactions from your node’s memory pool. This candidate block is what we’re going to try and mine on to our (and everyone else’s) blockchain.

Next we construct a block header for this block, which is basically a short summary of all of the data inside the block. It also includes a reference to an existing block in the blockchain that we want this block to build on top of.

Now we are ready to start “mining” this block. To do this we put this block’s block header in to the SHA256 hash function, and hope that the number it spits out is below the current target.

The target is the number you need to get below to add the block to the blockchain.

If the hash of your block header isn’t below the target, you can keep trying by incrementing the nonce field in the block header, which allows you to keep the same basic block header but get a completely different hash result for it.

The mining process is basically hashing a block header as fast as you can to try and be the first node to get a low-enough result.

If you’re lucky you will get a hash for your block below the current target. If so, you can then broadcast this block to the network, where each node will confirm that the block header hashes to below the target, and then add this “mined” block on to their blockchain.

Congratulations, you have just mined a block of transactions on to the blockchain.

From here, each node will stop working on their own candidate block, and construct a new one with fresh transactions from their memory pool and start trying to build on top of this new block in the chain.

As a result, miners are constantly working independently yet collaboratively to extend the blockchain with new blocks of transactions.

2. Who can mine blocks?

Any node can try to mine a block, and each node has a chance of being successful. As a result, this creates a network-wide competition where any node on the network could be the one to add the next batch of transactions on to the blockchain.

However, although anyone can try mine, being able to perform hash calculations as fast as possible does improve your chances of being able to mine the next block.

As a result, miners with the most processing power (or “hashing power”) are likely to mine more blocks than those who cannot hash as quickly.

Consequently, even though anyone can mine, it tends to be concentrated amongst those with specialized mining hardware, and those with access to cheap electricity to power that hardware.

3. Why mine blocks?

If you are able to mine a block, you can claim a block reward.

When you construct a candidate block, you can put your own special transaction at the top of the block. This is called the coinbase transaction, and it allows you to send yourself a fixed amount of bitcoins that do not currently exist.

The coinbase transaction effectively allows you to create new bitcoins and send them to yourself.

So if you end up mining this block and it becomes a part of the longest chain, you can spend these newly-created bitcoins once the block becomes 100 blocks deep in the chain.

Therefore, this block reward acts as an incentive for miners to mine new blocks and continually try to extend the longest known chain of blocks.

Tip: Sending bitcoins that do not already exist is only allowed in the coinbase transaction, which makes them the source of all new bitcoins.

Tip: The fact that mining blocks allows you to introduce new bitcoins in to the network is why the process is called “mining”, although from a practical point of view it’s about adding transactions on to the blockchain.

4. How long does it take to mine a block?

The system is designed so that one miner across the bitcoin network will successfully mine a new block once every 10 minutes (on average).

This timing is controlled by the target, which is like a limbo pole that a block hash has to get under for it to be allowed on to the blockchain.

Each node knows the current target for the current height of the blockchain.

If blocks end up being mined faster than 10 minutes on average over a two-week period (because miners buy better hardware and can mine blocks faster), the target will adjust downwards so that it becomes more difficult to mine a block, and so the average time between blocks reverts back to around 10 minutes on average.

As a result, the target adjusts regularly to try and keep a regular interval of 10 minutes between new blocks being mined, which allows for a predictably consistent rate of new blocks and issuance of new bitcoins in to the network.

5. Why do we use mining?

Because this system allows computers across a network to resolve any problems with conflicting transactions that might be inserted in to the network, without the need for a central computer or authority to sort them out.

Bitcoin runs across a network of independent computers, so it’s possible to create two conflicting transactions (two transactions sending the same bitcoins to different places) and then insert them in to different nodes on the network at the same time. Some nodes will receive transaction A first, and other nodes will receive transaction B first.

How can all the computers agree on which transaction should make it in to the blockchain?

Thanks to the mechanism of mining, one of the nodes on the network will mine the next block and decide which of the two transactions makes it on to the blockchain. When nodes receive this block they will add it to their chain and remove any conflicting transactions from their memory pool.

As a result, the process of mining acts as a sorting mechanism for transactions, and where no single node on the network is in complete control of which transactions will make it on to the blockchain.

6. How do you mine a block?

You start by constructing a block header for your block.

For example, here’s what the block header for block 100,000 would have started out like:


Now you’ve got the block header, you try and “mine” it by putting it through the SHA256 hash function (twice). You keep incrementing the nonce value as you go to try and get a result below the target.

For example:

Nonce     Hash256
--------  -------
00000000  66637f4d9166fb477548a67f5f05d9bde2b7f30fd57b21661ca537723422dbc7
00000001  57cf53d815f3cf4d8025fbc4c495f65c2463fb7a06b5ea28b786a508b5208604
00000002  f6ce3fd0ad29de9ece621662dc6e9e9f71195adc51735e75b1d64ab5063781f9
00000003  d9e7a9627a292844470a9e881093c7f6407e9a97f298cca5bbdb97841d3179ce
00000004  bd0a01da6cd0910a1b6dcd0c43a06091fbbb0845552d018b0c79e31e0f1e9796
00000005  d342413bdb80f801ff6cfd442dc50fe21637563d8c891b3a1c0835e95e9384a2
00000006  c0684f43e341f831264f1cde1af906a88c51cc6f2c93b53a090dca3f81271b4b
00000007  5722df6ac1bfb243d0713109297573e6a9eda958403140f38d06e7e2b866ba0b
00000008  2ab080885c6b88428d70b3a20d3cd09939b8669057d625bf81eb04d3fe2e7c75
00000009  57f0e686284585078fb768ffecd13f85d8d74ccfbeb7a130ef3c1bf489ad43ad
0000000a  c62b5901384b8dc5551dca043a5d3bf5652a7929031199d804c7f5831d316cbf

Eventually you may find a nonce value that produces a hash result below the target:

Nonce    Hash256
-------- -------
0f2b5710 000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506

Example Code

puts "Ruby Code!"

By Greg Walker,

Last Updated: 14 Nov 2019
  • 14 Nov 2019: edits
  • 12 Nov 2019: edits
  • 11 Nov 2019: guide/mining - first draft (text)
  • 24 May 2019: Added Hashover comment system (basic functionality to test it out)
  • 26 Nov 2018: Added sub titles to every page.
  • 27 Mar 2018: Added more descriptive titles to be shown in search engines.
  • 13 Mar 2018: guide/ - renamed all files from .text to .md and removed numerical prefix. Makes it cleaner and makes for easier working with search box.

Hey there, it's Greg.

I'll let you know about cool website updates, or if something seriously interesting happens in bitcoin.

Don't worry, it doesn't happen very often.