The double-spending issue refers to the act of using the same coin more than once. If this problem is not resolved, a bitcoin coin loses its scarcity. Scarcity is a key feature of a currency. Without it, the coin can no longer be called a cryptocurrency. The consensus mechanism is designed for resolving the double-spending problem. To understand how the mechanism works, you need to know the concepts of proof of work (PoW) and mining.
As we learned earlier, a miner has to solve a mathematical puzzle ahead of other miners to receive the role of being a builder of the next block and receive an award for doing the work. The work of resolving the mathematical problem is called the PoW. The mathematical problem itself does not have a value. The main purpose is to give a sufficient window of time to miners validating transactions. This window of waiting time is maintained at 10 minutes. A miner can query its copy of the distributed ledger and validate the following facts:
- The requester of a transaction has the coins
- Any other transactions in the ledger have not spent the same coins
- Other transactions within the candidate block do not spend the same coins
The process of repeatedly guessing an answer to the puzzle is called mining. Hardware that is manufactured and dedicated to the mining work is called a mining rig.
As per bitcoin protocol, mining is the only way to issue a new coin. Rewarding a miner serves several purposes:
- Compensates a miner's investments on hardware
- Covers mining operation costs such as utility bills, human salaries, site rentals, and so on
- Gives miners incentives to safeguard the network from being attacked by malicious hackers
The total number of mintable bitcoin coins is fixed at 21 million. Currently (January 2019), close to 17.5 million coins have been issued. The bitcoin protocol defines a rule for dynamically adjusting the payout rate to the mining work, and the remaining 3.5 million coins will need another 122 years to be mined completely.
The mining payout rate is dynamically adjusted and follows the following rule:
The rate changes at every 210,000 blocks. It is a function of the block height on the chain with genesis=0, and is calculated using 64-bit integer operations as (50 * 100,000,000) >> (height / 210,000). The rate that initially started with 50 coins has fallen to 25 coins at block 210,000. It fell to 12.5 coins at block 420,000 and will eventually go down to 0 when the network reaches the size of 6,930,000 blocks.
Bitcoin adjusts the difficulty level of the puzzle for maintaining the 10-minute window. Based on the most recent rate of a new block being added, the difficulty level is calculated or adjusted accordingly. If the average rate of new blocks being added is fewer than ten minutes, the difficulty level will be increased. If the average rate takes more than ten minutes, it's decreased. The difficulty level is updated every 2,016 blocks.
With the relevant concepts being explained, we are ready to talk about the mining steps. For illustration purposes, we assume that the mathematical puzzle is to find the first hash value whose first character is 0 in order to maintain the 10-minute per new block window. Per the bitcoin protocol, a miner follows these steps to solve the puzzle:
- First, obtain the SHA-256 hash of a block in construction.
- If the resulted hash has a leading 0, the miner solves the puzzle. The miner adds the block to his/her copy of the distributed ledger on the node and claims the coin rewards. The winner broadcasts the news to other nodes. Other miners of the network check the answer and validate that the new block contains valid transactions.
- If passing the checks, all nodes on the network add the block to their copies of the ledger. Miners start to work on the next block.
- If the winner is a hacker and includes bad transactions such as double-spending a coin, the validation on transactions will fail. Other miners will not include the block in their ledger copies. They will continue to mine on the current block. As time passes, the path containing the bad block will no longer be the longest path. Per bitcoin protocol, the longest path is considered to be the blockchain and should be copied by all nodes. In other words, the path containing the bad block becomes an orphan, and will eventually be dropped. This is essentially how all nodes on the network reach a consensus to add only good blocks to the blockchain and prevent bad blocks from being included.
- If the resulted hash does not start with 0, per protocol, a miner adds a sequence number, known to be a nonce, starting from 0 at the end of the input text and retries the hash.
- If a resulted hash still does not contain a leading 0, change the nonce to 1, and obtain a new hash. Repeat the steps until a miner finds a new hash with a leading zero.
The following is a hypothetical example. The original plaintext is input string and a nonce varying from 0 to 3. Their corresponding SHA-256 hashes are as follows:
- input string: f23f4781d6814ebe349c6b230c1f700714f4f70f735022bd4b1fb69421859993
- input string0: 5db70bb3ae36e5b87415c1c9399100bc60f2068a2b0ec04536e92ad2598b6bbb
- input string1: 5d0a0f2c69b88343ba44d64168b350ef62ce4e0da73044557bff451fd5df6e96
- input string2: 7b8fe11e193f835e37301f20416c76c9cd55d962a5ad009f4302ee2607ba8d1a
- input string3: c37e5a2e94575060277e3b1abf9d3ebbe44274e72bb86f2a526266c9c5aa3722
The algorithm for adjusting the difficulty level is to change the required number of leading 0s, along with some minor tuning. Requiring additional leading 0s will increase the average trying times, and therefore the difficulty level is higher. The current bitcoin difficulty level is 18 leading 0s to maintain the 10 minute window.