Talking about my understanding of Bitcoin and blockchain, mining is not difficult, it is difficult to mine

Bitcoin concept

As we all know, bitcoin is a digital currency.
Compared with our paper currency, bitcoin has the following advantages:
1. The property can be controlled. Unlike banks that can freeze your account, bitcoin is not controlled by others.
2. There is no inflation trouble. Banks can print more money based on economic conditions, but the total amount of Bitcoin is unchanged (21 million).
3. Bitcoin has no counterfeit banknotes. This is an obvious advantage of Bitcoin.
4. Bitcoin transactions are not restricted by country.

Blockchain concept

An important person in cryptopunk: Nakamoto Village, "Bitcoin: A Peer-to-Peer Electronic Cash System" in 2008. Refine the technology in the blockchain, and gradually have the concept of blockchain.

Blockchain is a collection of technologies.
The data in the Bitcoin system is stored in blocks, and the blocks are connected by hash to form a chain of blocks, so it is called a blockchain.

Blockchain application scenarios:

1. Bookkeeping,
equity transactions, etc.
2. Asset
transactions, settlement, payment
3. Non-tampering
various certificates, crowdfunding, etc.
4. Peer-to-peer
sharing economy, etc., to solve the problem of mutual distrust
5. Privacy and
anonymous transactions, no one knows How much bitcoin do I have, only when I trade to verify whether it can be traded

Bitcoin's decentralization

Centralization means that all data is recorded in one place, including new and deleted verification queries, etc. '
Decentralization means that data is stored in each place, which directly leads to two problems:
how to verify and ownership issues,
simply say how Prove that you are yours, and how to prove that you are right

Verification: use hash function

The hash function here can be understood as the hash encrypted by md5. One of the most notable features is that it is irreversible.
For example, Hash (a) gets b, so as long as a changes, for example, Hash (a1) won't get b, and we can't get a through b.
With this foundation, we can regard the sequence number + timestamp + hash value as the head of the block, and
use the hash value and the intersection record each time to obtain a new hash value, which forms a new block header.
As shown in the following figure, each block is a package of transaction records . Everyone only needs to compare whether the last hash value is consistent with their own, and you can know whether it is correct.

Insert picture description here

Ownership: Asymmetric encryption technology

First of all analogy with banks, banks use a series of operations such as entering our own information and setting a password when we open an account, so that the bank gives us the ownership of this account.
There is no third party like Bitcoin in the transfer of Bitcoin.
Bitcoin payment has three main parameter records: the billing address, billing address, and transaction amount.
The address is public, and we will have a private key locally. The private key is the only source for generating this address. Only this private key can generate the address, and the address cannot be reversed to the private key.
Therefore, the private key is absolutely confidential and cannot be retrieved after forgetting. Whoever has the private key can trade .

Then there is another problem: how to prove that we have the private key of this address without revealing the private key.
That is to use the signature.
Students who have studied https should be familiar with this one.
Signature process:
1. First hash the original transaction record to obtain the summary information 7a7b34c5d
2. Use the summary information 7a7b34c5d and private key a6d77a88e0 to perform the signature operation to obtain the signature information 5c3c5d2d6a

Insert picture description here

After receiving the signature information, all accounts will be circularly broadcasted. The broadcast notification information includes:
transaction original information: payment address, collection address, transaction amount.
Signature information: 5c3c5d2d6a
This broadcast operation is equivalent to saying to everyone , I am the sending address, how much I have traded with the receiving address, the signature information is 5c3c5d2d6a, let me verify
the node that received the broadcast, and start the verification process:
Insert picture description here

It is easy to understand:
signature is an asymmetric encryption process, that is, using the hash value of the payment information and the private key to encrypt, get the signature information
and verification is an asymmetric decryption process, that is, using the signature information and the payment address to decrypt, to get the hash of the payment information the value of
this problem is solved, account transactions by the private ownership control, and no private key leak out.

What is mining

Those who have heard of mining probably know that mining consumes a lot of electricity, and it is constantly calculating. So why do you keep calculating?

The verification mentioned above, that is, accounting, is actually very resource consuming, because every day does not know how many transactions, no node will be willing to verify for free.
Therefore, the Bitcoin system provides a reward mechanism: there is a reward for bookkeeping.
With this reward mechanism, everyone will fight for bookkeeping, so there are the following rules:
Rule 1: Within a period of time (usually 10 minutes) only one person can successfully bookkeeping.
Rule 2: You can compete for unique accounting rights by solving cryptography problems, that is, increasing workload.
Rule 3: Other nodes just copy the accounting results.
So, who can solve the cryptography problem, who is more likely to keep accounts and get rewards.
So what is this cryptography problem?
As we said before, a verification is a hash (the last hash value, transaction record) to get a new hash value.
This is a bit simple, and almost everyone can complete it quickly.
In order to ensure that it is easier to select a person within a period of time, the difficulty of keeping accounts must be increased.
As a result, the hash method changes, adding a random number, becomes the following:

Hash (last hash value, transaction record, random number) to get a new hash value,
and this will require a new hash value, and the new hash value needs to meet several zeros, such as 0000a6h7jj85.
We know that the hash number, as long as it is slightly modified, will cause a wide range of value changes, so when hashing, you can constantly modify the random number to get a new hash value.
In theory, as long as we keep using random number hash, we will always get the hash value that meets the condition.
Now basically for ordinary computers, if you want to get four hash values ​​starting with 0, it is basically about 1 ~ 2 minutes.
Nowadays, because there are too many people participating in the competition, the difficulty will increase. The hash value that starts with 18 zeros has been reached. Whoever first finds this hash value can get a unique accounting right .

The process of finding this hash value is very random, so it is called mining. When you get the only right to keep accounts, you can say that it is mining.

Mining steps

1. Collect transactions that have not been recorded in the ledger in the broadcast
2. Verify the validity of the transaction
3. Add a transaction to transfer to yourself (that is, mining rewards)

Estimate the difficulty of getting the accounting right

For example, now you need to get a new hash value, which needs to start with 18 zeros.
Hash is hexadecimal, so
if you want to get 0 in the first digit, it takes 16 calculations on average.
If you need to get 0 for the first two, you need 16 * 16 calculations on average, which is the power of 16.
By analogy, if you want to get the first 18 zeros, you need to calculate the 16th power .
So now it is all combined mining by multiple people, and then divided after mining, there are very few lone wolves.
In a simple analysis, for example, mining can get 100,000 yuan, so as long as the mining cost is less than 100,000, someone will go to mine. , So it will eventually reach a balanced state, that is, it will cost an average of almost 100,000 yuan to dig up 100,000 mines.

Consensus mechanism

Although the calculation is more difficult, there may be a situation where multiple nodes complete the workload at the same time. In this case, who's block will be used?
First, each computing node must abide by the rules, that is, the workload of the node is only effective when other nodes agree. That rule is the consensus mechanism.

Consensus mechanism: each node only recognizes the blockchain with the largest cumulative workload, that is, everyone finds the longest chain

For example:
now that there is a chain # 1213 <– # 1214
everyone starts mining, digging # 1215, at a certain time, both
A and B have dug at the same time, A extends the blockchain # 1215A, B extends the blockchain # 1215B, Then A and B broadcast their results separately.
The branch that appeared at this time, which one is used?
At this time, C received the broadcast information of A. For C, the longest blockchain at this time was # 1215A, so C started to count again to compete for the next block.
Similar to C, Ding received B's broadcast message at this time. Ding thought that the longest blockchain at this time was # 1215B, so Ding started competing for the next block with # 1215B.
At a certain moment, C calculated first, so he extended the
blockchain to: # 1213 <– # 1214 <– # 1215A <– # 1216C, and broadcast it to
this point, other nodes in the network received this # 1216 C, and # 1215B, know that C's is the longest.
Then, in the end, # 1213 <– # 1214 <– # 1215A <– # 1216C will be selected as the final chain, and the original chain of B will be abandoned.

Transaction confirmation time

Bitcoin's block time is designed to be 10 minutes, which is actually a direct compromise on faster transaction confirmation and lower forks.
If the confirmation time is very short, then the transaction confirmation will be frequent, and the fork will be a lot.
If the confirmation time is very long, the fork will decrease, but the transaction confirmation will be very slow.
Therefore, to ensure that the transaction confirmation is fast, there cannot be too many forks It took only 10 minutes.

Follow up

The above are just some simple concepts of bitcoin and blockchain. Deeper knowledge, we must continue to mine
1.
Bitcoin nodes of bitcoin's P2P network are all peer-to-peer communication, there is no way to know the IP of other services Each server may be online at any time, and may be offline at any time. How does that node connect to other nodes?
2. Block structure: Merkle number]
Bitcoin storage information details
3. Bitcoin white paper
4. Bitcoin Transaction script
5. How to apply blockchain technology to the actual project

Published 203 original articles · praised 186 · 210,000 views

Guess you like

Origin blog.csdn.net/java_zhangshuai/article/details/104979848