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.
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.
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.
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.
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.
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.
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.
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