This paper was first published inSimple explanation of blockchain community

Link to the original text:How bitcoin mines (mining principle) – proof of workloadThe original text has been updated. Please read it

stayBookkeeping principle of blockchainFirst, we learned that bookkeeping is the process of calculating hash package of transaction records, transaction time, account book serial number, last hash value and other information.

We know that all calculation and storage need to consume computer resources. Since there are costs to be paid, why should nodes participate in accounting? In the design of Nakamoto Cong (the father of bitcoin), the node who completes the bookkeeping can get a certain amount of bitcoin rewards given by the system. This reward process is also the process of issuing bitcoin. Therefore, we call bookkeeping as “mining“. This paper will discuss this process in detail.

## Bookkeeping

Since there is a reward for bookkeeping, you can add a certain number of bitcoin (currently 12.5 bitcoin, each bitcoin is more than 40000 yuan when writing blog posts, so you can calculate how much money). Therefore, there will be a scramble for bookkeeping, and everyone will keep accounts together, which will cause problems: if there is inconsistency in bookkeeping, bitcoin system will be introduced To solve this problem, the rules are as follows:

- Within a period of time (about 10 minutes, the specific time will interact with the difficulty of cryptography problems), only one person can be successful in bookkeeping
- By solving the cryptography problem (i.e. proof of workload), the competition obtains the unique accounting right
- Other nodes copy bookkeeping results

However, before proving the workload, the bookkeeping node will do the following preparations:

- Collect the original transaction information in the broadcast that has not been recorded in the ledger
- Check whether there is enough balance in the payment address in each transaction information
- Verify that the transaction has the correct signature
- Package and record the transaction information that has been verified
- Add a bonus deal: add 12.5 bitcoin to your address

If the node competes for the right to account successfully, it can get 12.5 bitcoin reward.

## proof of work

fromBookkeeping principle of blockchainWe have learned that the hash value of the previous block and the current account page information are used as the original information for each bookkeeping.

If it’s just like this, it’s obvious that everyone can easily complete the bookkeeping.

In order to ensure that only one person can account in about 10 minutes, it is necessary to improve the difficulty of accounting, so that the result of hash must start with several zeros. In order to satisfy this condition, a random number variable is introduced in hash.

Use pseudo code to express it as follows:

```
#No difficulty: hash (previous hash value, transaction record set) = 456635bcd
Hash (last hash value, transaction record set, random number) = 0000afd635bcd
```

We know that if you change any part of the original information of hash, the hash value will also change constantly. Therefore, when you change the value of the random number constantly, you can always find a random number to make the hash result begin with several zeros (this process is called guessing in the following). If you find the node of the random number first, you will get the unique bookkeeping weight of this bookkeeping.

### Calculation analysis

How difficult is the bookkeeping section,

Hash value is a string composed of numbers and uppercase and lowercase letters. Each bit has 62 possibilities (26 upper case letters, 26 lower case letters, any one of 10 numbers). Assuming that the probability of any character appearing is equal, then the probability of the first digit being 0 is 1 / 62 (regardless of the characters in other bits). In theory, it takes 62 attempts of hash operation to appear When the first bit is 0, if the first two bits are 0, the square hash operation of 62 must be tried. If n zeros start, the n-th power operation of 62 needs to be tried. Let’s take a look at the current actual block ා 493050 information:

Note: data fromhttps://blockchain.info

We can see that the hash value starts with 18 zeros. In theory, we need to try the 18th power of 62. This number is very, very huge. I don’t know. It should be over 100 billion. Such a large amount of calculation requires a lot of computing equipment, power, etc,

At present, there should be no single miner independently involved in mining. Basically, the mining pool is formed by the miners (the miners in the mine pool divide the income according to the percentage of calculation power).

From an economic point of view, only if there is profit from mining (the rising price of bitcoin also increases the income), there will be new miners to join in, which will intensify the competition and increase the difficulty of computing power. Mining requires more calculation and power, and the final cost will be close to the income due to the interaction.

Digression: due to the low power cost and higher relative income in China, China’s computing power accounts for more than half of the whole network

## verification

After the node successfully finds the satisfied hash value, it will immediately carry out the broadcast package block for the whole network, and the nodes of the network will immediately verify the broadcast package block after receiving the broadcast package block.

If the verification is passed, it indicates that a node has successfully solved the puzzle, and it will no longer compete for the current block package. Instead, it will choose to accept this block, record it in its own ledger, and then perform the competition puzzle of the next block.

In the network, only the fastest puzzle solving block will be added to the ledger, and other nodes will be copied, thus ensuring the uniqueness of the entire ledger.

If a node has any cheating behavior, it will lead to the node verification failure of the network, directly discarding its packed block, this block can not be recorded in the general ledger book, and the cost of cheating nodes will be wasted. Therefore, under the huge mining cost, the miners will voluntarily abide by the consensus agreement of bitcoin system, which will ensure the security of the whole system 。

extended reading Bitcoin block structure Merkle tree and simple payment verification analysisYou can learn more about how the block structure validates transactions.

## explain

The miner’s income actually includes not only the newly issued 12.5 bitcoin reward, but also the transaction fee income (this article ignores some details to make the trunk clearer).

If you are interested, you can see what information is contained in the block. The red arrow indicates the information involved in this article.

Consensus protocol is mentioned in this paper. Bitcoin consensus protocol is mainly composed of workload proof and longest chain mechanism, which will be explained in detail in a blog post.

Block chain-Learning blockchain systematically to create the best blockchain technology blog