为广大币圈朋友提供币圈基础入门专业知识!
当前位置首页 > 区块链知识> 正文

区块链中心计算公式是什么,区块链中心计算公式有哪些

发布时间:2023-12-06-04:20:00 来源:网络 区块链知识 区块   心计   公式

区块链中心计算公式是什么,区块链中心计算公式有哪些


请查看相关英文文档

① Comic illustration: What is blockchain

Comic illustration: What is blockchain

What is blockchain?
Blockchain, English Blockchain, is essentially a decentralized distributed database. Anyone can become a node of this huge network as long as they set up their own server and connect to the blockchain network.
Since the blockchain is essentially a database, what exactly is stored in it? Let’s take a look at the basic unit of blockchain: Block.
A block is divided into two parts:
1. Block header
The block header stores the header information of the block, including the hash value (PreHash) of the previous block. The hash value of the block body (Hash), the timestamp (TimeStamp), etc.
2. Block body
The block body stores the detailed data (Data) of this block. This data contains several rows of records, which can be transaction information or some other information.
What does the hash value just mentioned mean?
Everyone must have heard of MD5. MD5 is a typical hash algorithm that can convert a string of plaintext of any length into a string of fixed length (128 bits). This string is the hash value.
In our blockchain, a more complex hash algorithm called SHA256 is used. After a series of complex calculations, the latest data information (such as transaction records) will eventually be converted into a 256-bit hash value string through this hash algorithm, which is the Hash in the block header. The format is as follows:

Blocks and Hash have a one-to-one correspondence, and Hash can be regarded as the unique identifier of the block.
How are different blocks related to each other? Rely on Hash and PreHash to associate. The PreHash value of each block is equal to the Hash value of the previous block.
Why do we need to calculate the hash value of the block?
Since the blockchain is a chain structure, there must be a head node (the first block) and a tail node (the last block) of the chain. Once someone calculates the hash value of the latest data in the blockchain, which is equivalent to packaging the latest transaction records, a new block will be created and connected to the end of the blockchain.
The Hash of the new block header is the hash value just calculated, and the PreHash is equal to the Hash of the previous block. The data in the block body stores the transaction records before packaging, and this part of the data information has become unmodifiable.
This process of calculating Hash values ​​and creating new blocks is called mining.
The server used for massive calculations is called a mining machine.
The workers who operate calculations are called miners.
Compute HashWhat’s so hard about it? Let’s give the most superficial explanation. The formula for calculating the hash value is as follows:
Hash = SHA-256 (Hash of the last block + basic information of the new block + transaction record information + random number)
Among them, the transaction record information is also a string of hash values, and its calculation involves a data structure Merkle Tree. Interested friends can check the relevant information, we will not introduce it for now.
The key computational difficulty here lies in the generation of random numbers. In order to increase the difficulty of Hash calculation, the wretched inventor of the blockchain requires that the first 72 bits of the Hash result must be 0. This probability is too small.
Since (the Hash of the last block + the basic information of the new block + the transaction record information) is fixed, whether the Hash that meets the requirements can be obtained depends entirely on the value of the random number. Miners must go through massive calculations and repeatedly generate random numbers in a general attempt to "get lucky" before they can get the correct Hash and successfully mine.
At the same time, the block header also contains a dynamic difficulty coefficient. When the world's hardware computing power becomes faster and faster, the difficulty coefficient of the blockchain will also increase, making the entire network capable of completing the task every 10 minutes on average. A new block is generated.
Friends, do you understand how difficult mining is? It should be added that different blockchain applications are different in details. The mining rules described here take Bitcoin as an example.
Applications of Blockchain

The concept of Bitcoin (BitCoin) was first proposed by Satoshi Nakamoto in 2008, and then based on this idea, open source software and P2P built on it were designed and released. network. Bitcoin is a P2P form of digital currency. Peer-to-peer transmission means a decentralized payment system.
What is a P2P network?
Traditional currencies are uniformly issued by the central bank, and all personal savings are uniformly managed by banks. This is a typical centralized system.
Bitcoin is deployed on a decentralized network composed of many peer nodes around the world. Every node is qualified to record and issue this digital currency.
As for the underlying data storage of Bitcoin, it is based on blockchain technology. Each transaction in Bitcoin corresponds to a row in the block data. A simple diagram is as follows:
Each row of the transaction record contains a timestamp, transaction details, and digital signature.
The table is only for ease of understanding. The actual stored transaction details are anonymous, and only the wallet addresses of the payer and payee are recorded.
As for digital signatures, they can be understood as anti-counterfeiting marks for each single transaction, generated by an asymmetric encryption algorithm.
Let’s talk about the rewards of Bitcoin miners:
The Bitcoin protocol stipulates that miners who mine new blocks will receiveThe reward has been 50 Bitcoins since 2008, and has been halved every 4 years. Currently, it is 12.5 Bitcoins in 2018. The new Bitcoins in circulation are all born in this way. No wonder everyone is so eager to mine Bitcoins!
Advantages and Disadvantages of Blockchain
Advantages of Blockchain:
1. Decentralization
Blockchain does not rely on a central node. The data of the entire system is All peer nodes in the entire network are jointly maintained and can store and verify data. In this way, unless the attacker hacks more than half of the nodes in the entire network, the entire system will not be destroyed.
2. Information cannot be tampered
The data in the block cannot be tampered with. Once the data is tampered with even a little bit, the hash value corresponding to the entire block will change accordingly, and it will no longer be a valid hash value, and the subsequent linked blocks will also be broken.
Disadvantages of blockchain:
1. Excessive consumption of energy
To generate a new block, a large amount of server resources must be used to perform a large number of unnecessary trial calculations, which seriously consumes electricity.
2. Network delay of information
Take Bitcoin as an example. Any transaction data needs to be synchronized to all other nodes. The synchronization process will inevitably be affected by network transmission delay, resulting in a long time consuming.
A few additional points:
1. Part of the content of this comic refers to Ruan Yifeng’s blog post "Blockchain Introductory Tutorial". I would like to thank this great master for his popular science.
2. Due to limited space, the knowledge about Merkle Tree and asymmetric encryption has not been discussed in detail for the time being. Interested friends can check the information for further study.

② Tutorials for getting started with blockchain


However, there are very few simple and easy-to-understand introductory articles. What exactly blockchain is and what makes it special is rarely explained.
Next, I will try to write a best-understood blockchain tutorial. After all, it is not difficult. The core concept is very simple and can be explained clearly in a few sentences. I hope that after reading this article, you will not only understand the blockchain, but also understand what mining is, why mining is getting more and more difficult, and other issues.
It should be noted that I am not an expert in this area. Although I have been paying attention to it for a long time, my detailed understanding of blockchain started at the beginning of this year. You are welcome to correct any errors or inaccuracies in the article.
1. The essence of blockchain
What is blockchain? In a word, it is a special distributed database.
First of all, the main function of blockchain is to store information. Any information that needs to be saved can be written to the blockchain and read from it, so it is a database.
Secondly, anyone can set up a server, join the blockchain network, and become a node. In the world of blockchain, there is no central node. Every node is equal and stores the entire database. You can write/read to any nodeFetch data, because all nodes will eventually be synchronized to ensure that the blockchain is consistent.
2. The biggest features of blockchain
Distributed databases are not a new invention, and there have been such products on the market for a long time. However, blockchain has a revolutionary feature.
Blockchain has no administrator, it is completely centerless. Other databases have administrators, but blockchain does not. If one wanted to add auditing to the blockchain, it would not be possible because it is designed to prevent the emergence of a central authority.
It is precisely because it is unmanageable that blockchain can be uncontrollable. Otherwise, once big companies and large groups control the management, they will control the entire platform, and other users will have to take orders from them.
However, without an administrator, everyone can write data into it. How can we ensure that the data is trustworthy? What if it is modified by a bad person? Please read on, this is the wonderful thing about blockchain place.
3. Block
Blockchain is composed of blocks. Blocks are much like database records. Every time data is written, a block is created.
Each block contains two parts.
Head: records the characteristic values ​​of the current block
Body: actual data
The block header contains multiple characteristic values ​​of the current block.
Generation time
Hash of the actual data (i.e. block body)
Hash of the previous block
...
Here, you need to understand what a hash is , which is necessary to understand blockchain.
The so-called hashing means that the computer can calculate a characteristic value of the same length for any content. The hash length of the blockchain is 256 bits, which means that no matter what the original content is, a 256-bit binary number will be calculated in the end. And it can be guaranteed that as long as the original content is different, the corresponding hash must be different.
For example, the hash of the string 123 is (hexadecimal), which is 256 bits when converted to binary, and only 123 can get this hash. (Theoretically, it is possible for other strings to get this hash, but the probability is extremely low and can be approximated as impossible.)
Therefore, there are two important inferences.
Corollary 1: The hash of each block is different, and the block can be identified by the hash.
Corollary 2: If the content of the block changes, its hash will definitely change.
4. The non-modifiable nature of Hash
Blocks and hashes have a one-to-one correspondence, and the hash of each block is calculated based on the block header (Head). That is to say, the characteristic values ​​​​of the block header are connected together in order to form a very long string, and then the hash is calculated on this string.
Hash = SHA256 (block header)
The above is the calculation formula of block hash. SHA256 is the hash algorithm of the blockchain. Note that this formula only contains the block header, not the block body, that isIt is said that the hash is uniquely determined by the block header. As mentioned earlier, the block header contains a lot of content, including the hash of the current block body and the hash of the previous block. This means that if the content of the current block body changes, or the hash of the previous block changes, it will definitely cause the hash of the current block to change.
This point has great significance for blockchain. If someone modifies a block, the hash of the block changes. In order for subsequent blocks to still be connected to it (because the next block contains the hash of the previous block), the person must modify all subsequent blocks in sequence, otherwise the modified block will be removed from the blockchain . Due to the reasons mentioned later, hash calculation is very time-consuming, and it is almost impossible to modify multiple blocks in a short period of time, unless someone controls more than 51% of the computing power of the entire network.
It is through this linkage mechanism that the blockchain ensures its own reliability. Once the data is written, it cannot be tampered with. This is just like history, what happened happened, and it can’t be changed from now on.
Each block is connected to the previous block, which is where the name blockchain comes from.
5. Mining
Since synchronization between nodes must be ensured, the adding speed of new blocks cannot be too fast. Just imagine, you have just synchronized a block and are preparing to generate the next block based on it, but at this time, another node generates a new block, and you have to give up half of the calculations and synchronize again. Because each block can only be followed by one block, you can only generate the next block after the latest block. So, you have no choice but to sync as soon as you hear the signal.
So, the inventor of the blockchain, Satoshi Nakamoto (this is a pseudonym, and his true identity is still unknown) deliberately made it difficult to add new blocks. His design is that on average, the entire network can generate a new block every 10 minutes, which is only six per hour.
This output speed is not achieved through commands, but by deliberately setting up massive calculations. In other words, only through an extremely large amount of calculations can the effective hash of the current block be obtained and the new block added to the blockchain. Because the amount of calculation is too large, it cannot be done quickly.
This process is called mining, because the difficulty of calculating a valid hash is like finding a grain of sand that meets the conditions among the sand in the world. The machine that calculates hashes is called a mining machine, and the person who operates the mining machine is called a miner.
6. Difficulty coefficient
After reading this, you may have a question. People say that mining is difficult, but isn’t mining just about using a computer to calculate a hash? This is the strength of computers. How could it be? It becomes very difficult, why can’t it be calculated?
It turns out that not just any hash can be used, only hashes that meet the conditions will be accepted by the blockchain. This condition is particularly harsh, causing most hashes to fail to meet the requirements and must be recalculated.
It turns out that the block header contains a difficulty coefficient (difficulty), which determines the difficulty of calculating the hash. For example, the 100,000th blockThe difficulty coefficient is 14484.16236122.
The blockchain protocol stipulates that the target value (target) can be obtained by dividing the difficulty coefficient by a constant. Obviously, the greater the difficulty coefficient, the smaller the target value.
The validity of the hash is closely related to the target value. Only hashes smaller than the target value are valid, otherwise the hash is invalid and must be recalculated. Since the target value is very small, the chance that the hash is smaller than this value is extremely slim, and it may be calculated 1 billion times before it is considered a hit. This is the fundamental reason why mining is so slow.
As mentioned earlier, the hash of the current block is uniquely determined by the block header. If the hash of the same block needs to be calculated repeatedly, it means that the block header must keep changing, otherwise it is impossible to calculate different hashes. All feature values ​​in the block header are fixed. In order to make the block header change, Satoshi Nakamoto deliberately added a random item called Nonce.
Nonce is a random value. The role of the miner is actually to guess the value of Nonce so that the hash of the block header can be smaller than the target value so that it can be written to the blockchain. Nonce is very difficult to guess. At present, we can only use trial and error one by one through exhaustive methods. According to the protocol, Nonce is a 32-bit binary value, which can reach a maximum of 2.147 billion. The Nonce value of the 100,000th block is 274148111. It can be understood that the miner started from 0 and calculated 274 million times before obtaining a valid Nonce value so that the calculated hash can meet the conditions.
If you are lucky, you may find Nonce in a while. If you are unlucky, you may have calculated it 2.147 billion times without finding the Nonce, that is, it is impossible to calculate a hash that meets the conditions for the current block body. At this time, the protocol allows miners to change the block body and start a new calculation.
7. Dynamic adjustment of difficulty coefficient
As mentioned in the previous section, mining is random, and there is no guarantee that a block will be produced in exactly ten minutes. Sometimes it can be calculated in one minute, and sometimes it may take several hours. No result. Overall, with the improvement of hardware equipment and the increase in the number of mining machines, the computing speed will definitely become faster and faster.
In order to keep the output rate constant at ten minutes, Satoshi Nakamoto also designed a dynamic adjustment mechanism for the difficulty coefficient. He stipulated that the difficulty factor should be adjusted every two weeks (2016 blocks). If the average block generation speed in these two weeks is 9 minutes, it means that it is 10% faster than the legal speed, so the next difficulty factor will be increased by 10%; if the average block generation speed is 11 minutes, it means It is 10% slower than the legal speed, so the difficulty factor of the next step must be lowered by 10%.
The difficulty coefficient is adjusted higher and higher (the target value is getting smaller and smaller), which makes mining more and more difficult.
8. Forks of the blockchain
Even if the blockchain is reliable, there is still an unresolved problem: if two people write data to the blockchain at the same time, that is to say, two people write data to the blockchain at the same time. block join as itThey are all connected to the previous block, forming a fork. Which block should be adopted at this time?
The current rule is that new nodes always adopt the longest blockchain. If there is a fork in the blockchain, it will look at which branch is behind the fork to reach 6 new blocks first (called six confirmations). Based on a block calculation of 10 minutes, it can be confirmed in one hour.
Since the generation speed of new blocks is determined by computing power, this rule means that the branch with the most computing power is the authentic blockchain.
9. Summary
Blockchain, as an unmanaged distributed database, has been running for 8 years since 2009 without major problems. This proves it works.
However, in order to ensure the reliability of data, blockchain also has its own price. The first is efficiency. You have to wait at least ten minutes to write data to the blockchain. All nodes synchronize the data, which requires more time. The second is energy consumption. The generation of blocks requires miners to perform countless meaningless calculations. This is Very energy consuming.
Therefore, the applicable scenarios of blockchain are actually very limited.
There is no management authority that all members trust
The written data does not require real-time use
The benefits of mining can make up for its own costs
If the above conditions cannot be met, then the traditional database is Better solution.
Currently, the largest application scenario (and possibly the only application scenario) of blockchain is the cryptocurrency represented by Bitcoin.

③ Beida Jade Bird Java Training: The organizational structure content that makes up the basic computing functions of the blockchain

With the rapid development of the Internet, consumers are more interested in blockchain technology and The awareness of digital virtual currencies is also constantly increasing.
Today, let’s take a look at the structures of the basic computing methods of blockchain technology.
Let’s learn about the specific situation with the following java course http://www.kmbdqn.cn/.
The basic elements that make up computing technology are storage, processing and communication.
Mainframes, PCs, mobile devices, and cloud services all exhibit these elements in their own way.
There are also dedicated building blocks within each element to allocate resources.
This article focuses on the large framework of the blockchain: introducing the modules of each computing element in the blockchain and some implementation cases of each module, preferring an overview rather than a detailed explanation.
The building blocks of blockchain are the following are the building blocks of each computing element in decentralized technology: Storage: token storage, database, file system/blob processing: stateful business logic, stateless business logic, high Performance Computing Communication: Connecting Data, Value, and State Network Storage As a basic computing element, the storage part contains the following building blocks.
Token storage.
Tokens are storage media of value (such as assets, securities, etc.). The value can be bitcoins, air miles or the copyright of digital works.
Main Token Storage SystemThe function is to issue and transfer tokens (with multiple variations) while preventing events such as multiple payments from occurring.
Bitcoin and Zcash are two "pure" systems that only focus on the token itself.
Ethereum began to use tokens for various services to realize its ideal of serving as a global computing center.
These are examples where tokens are used as internal incentives to operate the entire network architecture.
There are also tokens that are not internal tools used by the network to promote its own operation, but are used as incentives for higher-level networks, but their tokens are actually stored in the underlying architecture.
One example is an ERC20 token like Golem, which runs on the Ethereum network layer.
Another example is Envoke’s IP authorization token, which runs on the IPDB network layer.
Database.
Databases are specifically designed to store structured metadata, such as data tables (relational databases), document storage (such as JSON), key-value storage, time series or graph databases.
Databases can quickly retrieve data using queries like SQL.
Traditional distributed (but centralized) databases such as MongoDB and Cassandra typically store hundreds of terabytes or even petabytes of data, with performance reaching millions of writes per second.
Query languages ​​like SQL are very powerful because they separate implementation from specifications so that they are not tied to a specific application.
SQL has been used as a standard for decades, so the same database system can be used in many different industries.
In other words, to discuss generality outside of Bitcoin, one does not necessarily have to talk about Turing completeness.
You only need one database, which is both concise and easy to expand.
There are times when Turing completeness is also very useful, which we will discuss in detail in the "Decentralized Processing" section.
BigchainDB is a decentralized database software and a specialized document storage system.
It is based on MongoDB (or RethinkDB) and inherits the latter's query and expansion logic.
But it also has the characteristics of blockchain, such as decentralized control, tamper resistance and token support.
IPDB is a regulated public instance of BigchainDB.
In the field of blockchain, it can also be said that IOTA is a time series database.
File system/blob data storage.
These systems store large files (movies, music, large data sets) in a hierarchical structure of directories and files.
IPFS and Tahoe-LAFS are decentralized file systems that contain decentralized or centralized blob storage.
FileCoin, Storj, Sia and Tieron are decentralized blob storage systems, as is the ancient and excellent BitTorrent, although the latter uses a p2p system rather than a token.
Ethereum Swarm, Dat, and Swarm-JS basically support the above two methods.
Data Market.
This kind of system connects data owners (such as enterprises) with data users (such as AI startups).
They sit on top of databases and file systems, but they are still the core architecture because countless data-hungry applications (such as AI) rely on such services.
Ocean is an example of a protocol and network on which a data market can be created.
There are also application-specific data markets: EnigmaCatalyst for crypto markets, Datum for private data, and DataBrokerDAO for IoT data streams.
Processing Next we discuss processing this basic computational element.
"Smart contract" system usually refers to a system that processes data in a decentralized form [3].
It actually has two subsets with completely different properties: stateless (combined) business logic and stateful (sequential) business logic.
There is a huge difference between stateless and stateful in terms of complexity, verifiability, etc.
The three types of decentralized processing modules are High Performance Computing (HPC).
Stateless (combined) business logic.
This is arbitrary logic and does not retain state internally.
In electronic engineering terms, it can be understood as a combinational digital logic circuit.
This logic can be expressed as a truth table, a logic diagram, or code with conditional statements (a combination of if/then, and, or, not, etc. judgments).
Because they are stateless, it is easy to verify large stateless smart contracts, creating large verifiable and secure systems.
N inputs and one output require O(2^N) calculations to verify.
The Inter-Ledger Protocol (ILP) includes a crypto-conditions (CC) protocol to clearly label combinational circuits.
CC is easy to understand because it became an Internet standard through the IETF, while ILP is widely used in various centralized and decentralized payment networks (such as Ripple used by more than 75 banks).
CC has many independently implemented versions, including JavaScript, Python, Java, etc.
BigchainDB, Ripple and other systems also use CC to support combined business logic/smart contracts.

④ Blockchain and edge computing What is blockchain and edge computing analysis

1. Blockchain: Blockchain is a concept of Bitcoin. It uses a block chain data structure to verify and store data, and uses a distributed node consensus algorithm to generate and update data. Utilize cryptographic methods to ensure the security of data transmission and access. It is a new distributed infrastructure and computing method that uses smart contracts composed of automated script codes to program and operate data. It is essentially a disintermediated database.

2. Edge computing: Edge computing is the ability to use an integrated platform of network, device computing, storage, etc., close to the source of data to provide corresponding services nearby. The main feature of edge computing is to provide services based on a regional model near the data end. Since the demand end and the service provider end are very close geographically, it is easier to provide fast and high-quality services, thus gaining popularity from Internet companies. .

3. Edge computing does not actually appear in the past few years. As early as 2003, IBM cooperated with content distribution network CDN and cloud service provider AKAMAI in edge computing. , and edge computing has also begun to be cautiously applied on a large scale in recent years with the development of blockchain and 5G technology. Since edge computing needs to connect many devices scattered around, it also needs to form a distributed network. This This structure is similar to the decentralization of blockchain, so in many cases, edge computing and blockchain can be well combined.

⑤ The most straightforward explanation of blockchain

In recent years, the term "blockchain" has become a hot topic, and news media have been reporting on it, but everyone may not be familiar with blockchain. The understanding of chain is still at the stage of looking at flowers in the fog. Today we will unveil its mystery.

In fact, the essence of blockchain is very simple and can be explained in one sentence: decentralized distributed database.

The main function of the blockchain is to store information. Anyone can write information and read it at the same time, so it is a public database.

Characteristics of blockchain

Distributed database technology has long existed in the market, but the difference is that although blockchain is also a distributed database , but it has no administrator and is completely decentralized.

Decentralization is a disruptive feature of blockchain technology. It does not require a centralized agent and realizes a direct point-to-point interaction, enabling high-efficiency, large-scale, and information interaction without a centralized agent. way becomes reality.

However, without an administrator, everyone can write data into it. How can we ensure that the data is trustworthy? What should I do if I am changed by a bad person? The designers have already thought of this, which proves that blockchain is a truly epoch-making product.

Block

The blockchain is composed of blocks. Blocks are much like database records. Every time data is written, a block is created.

Each block contains two parts:

Block header (Head): records the characteristic value of the current block
Block body (Body): actual Data
The block header contains multiple characteristic values ​​of the current block.

Generation time
Actual data (That is, the hash of the block body)
The hash of the previous block
...
Each node in the system has the latest complete database copy. Modifying the database of a single node is invalid. , because the system will automatically compare and consider the same data record that appears the most times to be true. At the same time, every step of the data record will be retained on the blockchain, and the information at each step can be traced.

Here, you need to understand what a hash is, which is necessary to understand the blockchain.

The so-called "hash" means that the computer can calculate a characteristic value of the same length for any content. The hash length of the blockchain is 256 bits, which means that no matter what the original content is, a 256-bit binary number will be calculated in the end. And it can be guaranteed that as long as the original content is different, the corresponding hash must be different.

For example, the hash of the string 123 is (hexadecimal), which is 256 bits when converted to binary, and only 123 can get this hash. (Theoretically, it is possible for other strings to get this hash, but the probability is extremely low and can be approximately considered impossible.)

Therefore, there are two important inferences.

Corollary 1: The hash of each block is different, and the block can be identified by the hash.
Corollary 2: If the content of the block changes, its hash will definitely change.

The immutability of hashes

Blocks and hashes have a one-to-one correspondence, and the hash of each block is for the "block header" (Head) computational. That is to say, the characteristic values ​​​​of the block header are connected together in order to form a very long string, and then the hash is calculated on this string.

Hash = SHA256 (block header)

The above is the calculation formula of block hash. SHA256 is the hash algorithm of the blockchain. Note that this formula only includes the block header and not the block body. In other words, the hash is uniquely determined by the block header.

As mentioned earlier, the block header contains a lot of content, including the hash of the current block body and the hash of the previous block. This means that if the content of the current block body changes, or the hash of the previous block changes, it will definitely cause the hash of the current block to change.

This is of great significance to the blockchain. If someone modifies a block, the hash of the block changes. In order for subsequent blocks to still be connected to it (because the next block contains the hash of the previous block), the person must modify all subsequent blocks in sequence, otherwise the modified block will be removed from the blockchain . For reasons to be mentioned later, the calculation of hashes is very time-consuming, and it is almost impossible to modify multiple blocks in a short time unless there isPeople control more than 51% of the computing power of the entire network.

It is through this linkage mechanism that the blockchain ensures its own reliability. Once the data is written, it cannot be tampered with. This is just like history, what happened happened, and it can’t be changed from now on.

⑥ Blockchain --- Consensus Algorithm

The PoW algorithm is a mechanism to prevent the abuse of distributed service resources and denial of service attacks. It requires nodes to perform complex operations that consume a moderate amount of time and resources, and the operation results can be quickly verified by other nodes, using time and energy as a guarantee to ensure that services and resources are used according to real needs.

The most basic technical principle in the PoW algorithm is the use of hashing algorithms. Assume that the hash value Hash(r) is found. If the original data is r (raw), the operation result is R (Result).

R = Hash(r)

The characteristic of the hash function Hash() is that for any input value r, the result R is obtained, and r cannot be deduced from R. When the input original data r changes by 1 bit, the resulting R value changes completely. In the Bitcoin PoW algorithm, the algorithm difficulty d and the random value n are introduced, and the following formula is obtained:

Rd = Hash(r+n)

This formula requires filling in the random In the case of value n, the first d bytes of the calculation result Rd must be 0. Due to the unknown nature of the hash function results, each miner has to do a lot of calculations to get the correct result. After the calculation result is broadcast to the entire network, other nodes only need to perform a hash operation to verify it. The PoW algorithm uses this method to make calculations consume resources, and verification only needs to be done once.

The PoS algorithm requires node verifiers to pledge a certain amount of funds to be eligible for mining and packaging, and the regional chain system uses a random method when selecting packaging nodes. When the node pledges The more funds there are, the greater the probability of being selected to package the block.

In POS mode, each coin generates 1 coin age every day. For example, if you hold 100 coins for a total of 30 days, then your coin age will be 3000 at this time. At this time, if you verify a POS block, your currency age will be cleared to 0, and the corresponding digital currency interest will be obtained from the block.

The process of a node producing blocks through the PoS algorithm is as follows: To become a block producing node, an ordinary node must first pledge its assets. When it is its turn to produce a block, it packages the block and then broadcasts it to the entire network. , other verification nodes will verify the legitimacy of the block.

 

The DPoS algorithm is similar to the PoS algorithm and also uses shares and equity pledges.

But the difference is that the DPoS algorithm uses a delegated pledge method, which is similar to the method of universal election of representatives to select N super nodes to record and produce blocks.

Voters cast their votes for a certain node. If a certain node is elected as an accounting node, then the accounting node can often use any method to reward its voters after obtaining the block reward.

These N accounting nodes will take turns to produce blocks, and the nodes will supervise each other. If they do evil, the pledge deposit will be deducted.

By trusting a small number of honest nodes, unnecessary steps in the block signing process can be removed, increasing the speed of transactions.

Byzantine problem:

Byzantium was the capital of the ancient Eastern Roman Empire. For defense, an army led by a single general was stationed in each fiefdom. Between the generals The message could only be delivered by messenger. In a war, all generals must reach a consensus and decide whether to go to war together.

However, there may be traitors within the army who will influence the generals to reach a consensus. The Byzantine Generals Problem refers to the problem of how the remaining generals can reach a unanimous decision when one of the generals is known to be a traitor.

BFT:

BFT is Byzantine fault tolerance. Byzantine fault tolerance technology is a type of fault tolerance technology in the field of distributed computing. The Byzantine hypothesis is a modeling of the real world, where computers and networks may behave unpredictably due to hardware errors, network congestion or outages, and malicious attacks. Byzantine fault tolerance techniques are designed to handle these abnormal behaviors and meet the specification requirements of the problem to be solved.

Byzantine fault-tolerant system:

The failed node is called a Byzantine node, and the normal node is a non-Byzantine node.

Assuming that the distributed system has n nodes, and assuming that the entire system has no more than m Byzantine nodes (n ≥ 3m + 1), the Byzantine fault-tolerant system needs to meet the following two conditions:

In addition, the Byzantine fault-tolerant system needs to achieve the following two indicators:

PBFT is the practical Byzantine fault-tolerant algorithm, which solves the problem of inefficiency of the original Byzantine fault-tolerant algorithm. The time complexity of the algorithm is O(n^2 ), so that Byzantine fault tolerance problems can be solved in actual system applications

PBFT is a state machine copy replication algorithm. All copies operate in the process of a view (view) rotation. The master Nodes are determined by the view number and the set of node numbers, that is,: Primary node p = v mod |R|. v: view number, |R| number of nodes, p: primary node number.

The consensus process of the PBFT algorithm is as follows: the client (Client) initiates a message request (request) and broadcasts it to each replica node (Replica), and one of the master nodes (Leader) initiates a proposal message pre -prepare and broadcast. Other nodes obtain the original message and send prepare messages after the verification is completed. Each node receives 2f+1 prepare messages, that is, it is considered ready and sends a commit message. When the node receives 2f+1 commit messages and the client receives f+1 identical reply messages, it means that the request initiated by the client has reached a network-wide consensus.

The specific process is as follows:

Client c sends a request to master node p. o: the specific operation of the request, t: the timestamp appended by the client during the request, c: client identifier. REQUEST: Contains message content m, and message digest d(m). The client signs the request.

When the master node receives the client's request, it needs to conduct the following verifications:

a. Whether the signature of the client's request message is correct.

Illegal requests are discarded. For a correct request, a number n is assigned. The number n is mainly used to sort the client's requests. Then broadcast a <, m> message to other replica nodes. v: view number, d client message summary, m message content. performs master node signature. n is [h, H] to be within a certain range. For specific reasons, please refer to the Garbage Collection chapter.

When replica node i receives the PRE-PREPARE message from the master node, the following verification is required:

a. Whether the signature of the master node's PRE-PREPARE message is correct.

b. Whether the current replica node has received a PRE-PREPARE message under the same v and also numbered n, but with different signatures.

c. Whether the abstracts of d and m are consistent.

d. Whether n is within the interval [h, H].

Illegal requests are discarded. Correct request, replica node i sends a message to other nodes including the master node, v, n, d, m is the same as the content of the above PRE-PREPARE message, i is the current replica node number. perform the signature of replica node i. Record PRE-PREPARE and PREPARE messages to the log, which are used to restore unfinished request operations during the View Change process.

When the master node and replica node receive the PREPARE message, they need to conduct the following verifications:

a. Whether the signature of the replica node's PREPARE message is correct.

b. Whether the current replica node has received n under the same view v.

c. Whether n is within the interval [h, H].

d. Whether d is the same as d in the currently received PRE-PPREPARE

Illegal request is discarded. If replica node i receives 2f+1 verified PREPARE messages, it sends a message to other nodes including the master node, where v, n, d, i is the same as the content of the above PREPARE message. same. performs the signature of replica node i. Record the COMMIT message to the log, which is used to restore the unfinished request operation during the View Change process. Record PREPARE messages sent by other replica nodes to the log.

When the master node and replica node receive the COMMIT message, they need to conduct the following verifications:

a. Whether the signature of the COMMIT message of the replica node is correct.

b. Whether the current replica node has received n under the same view v.

c. Whether the abstracts of d and m are consistent.

d. Whether n is within the interval [h, H].

Illegal requests are discarded. If replica node i receives 2f+1 verified COMMIT messages, it means that most nodes in the current network have reached a consensus, run the client's request operation o, and return For the client, r: is the result of the request operation. If the client receives f+1 identical REPLY messages, it means that the request initiated by the client has reached the consensus of the entire network. Otherwise, the client needs to determine whether to resend the request to the master node. Record COMMIT messages sent by other replica nodes to the log.

If the master node does evil, it may assign the same sequence number to different requests, or not allocate sequence numbers, or make adjacent sequence numbers discontinuous. The backup node should have the responsibility to actively check the validity of these sequence numbers.

If the master node goes offline or does evil and does not broadcast the client's request, the client sets a timeout mechanism. If it times out, it broadcasts the request message to all replica nodes.. The replica node detects that the master node has done something evil or is offline, and initiates the View Change protocol.

View Change protocol:

The replica node broadcasts ​​messages to other nodes. n is the number of the latest stable checkpoint, C is the 2f+1 verified CheckPoint message set, and P is the PRE-PREPARE and PREPARE message set of outstanding requests by the current replica node.

When the master node p = v + 1 mod |R| receives 2f valid VIEW-CHANGE messages, it broadcasts messages to other nodes. V is a valid set of VIEW-CHANGE messages. O is the set of uncompleted PRE-PREPARE messages reinitiated by the master node. Selection rules for the PRE-PREPARE message set:

The replica node receives the NEW-VIEW message from the master node, verifies the validity, and if valid, enters the v+1 state and starts the PRE-PREPARE message in O processing flow.

In the above algorithm process, in order to ensure that the previous request can be restored during the View Change process, each replica node records some messages to the local log. When the request is executed The replica node needs to clear the record messages of the previous request.

The simplest way is to execute the consensus synchronization of the current state again after the Reply message. This is relatively expensive, so it can be executed after executing multiple requests K (for example: 100). A status synchronization. This status synchronization message is the CheckPoint message.

Replica node i sends to other nodes, n is the last view request number retained by the current node, d is a summary of the current status, and the CheckPoint message record to the log. If replica node i receives 2f+1 verified CheckPoint messages, the messages in the previous log are cleared and n is used as the current stable checkpoint.

This is an ideal situation. In fact, when the replica node i sends a CheckPoint message to other nodes, the other nodes have not completed K requests, so they will not respond to i's request immediately. It will also follow its own rhythm, moving forward, but the CheckPoint issued at this time does not form stable.

In order to prevent i from processing requests too quickly, set a high and low water level interval [h, H] mentioned above to solve this problem. The low water level h is equal to the number of the previous stable checkpoint, and the high water level H = h + L, where L is the value we specify, which is equal to an integer multiple of the number of requests processed in the checkpoint cycle K, and can be set to L = 2K. When the request processed by replica node i exceeds the high water mark H, it will stop and wait for the stable checkpoint to change before continuing.

In blockchain scenarios, it is generally suitable for private chain and alliance chain scenarios that require strong consistency. For example, in the IBM-led blockchain Hyperledger project, PBFT is an optional consensus protocol. In Hyperledger's Fabric project, the consensus module is designed as a pluggable module and supports consensus algorithms such as PBFT and Raft.

Raft is based on a leader-driven consensus model, in which an outstanding leader (Leader) will be elected, and the Leader will be fully responsible for managing the cluster. Responsible for managing replication logs between all nodes in the Raft cluster.

In the figure below, the Leader (S1) of the cluster will be selected during the startup process and serve all commands/requests from clients. All nodes in a Raft cluster maintain a distributed log (replicated log) to store and submit commands (log entries) issued by clients. The Leader accepts log entries from clients and replicates them among all followers (S2, S3, S4, S5) in the Raft cluster.

In a Raft cluster, a minimum number of nodes is required to provide the expected level of consensus guarantee, which is also called a quorum. The minimum number of votes required to perform an operation in a Raft cluster is (N / 2 +1), where N is the total number of members in the group, that is, at least more than half of the votes, which is why cluster nodes usually have an odd number of nodes. So, in the example above, we need at least 3 nodes to have consensus guarantees.

If the legal quorum node is unavailable for any reason, that is, the votes do not exceed half, the negotiation will not reach an agreement and new logs cannot be submitted.

Data storage: Tidb/TiKV

Log: Alibaba's DLedger

Service discovery: Consul& etcd

Cluster scheduling: HashiCorp Nomad

Can only accommodate faulty nodes (CFT), not evil nodes

Sequential voting, only Apply serially, so the performance is poor in high concurrency scenarios

Raft solves the distributed consensus by solving the three main sub-problems surrounding Leader election and managing the security functions of distributed logs and algorithms. question.

When we start a new Raft cluster or a leader is unavailable, a new leader will be elected through negotiation among all member nodes in the cluster. Therefore, in a given instance, a node of a Raft cluster can be in any of the following states: Follower, Candidate, or Leader.

When the system first starts, all nodes are followers. If they do not receive the heartbeat signal from the leader within a period of time, the follower will be converted into a candidate;

If a node If a Candidate node receives votes from the majority of nodes, the Candidate can be converted into a Leader, and the remaining Candidate nodes will return to the Follower state;

Once a Leader discovers that there is a Leader node in the system that is older than itself. If the term is higher, it will be converted to Follower.

Raft uses a heartbeat-based RPC mechanism to detect when a new election starts. During normal times, the Leader will regularly send heartbeat messages to all available Followers (in practice, the log and heartbeat may be sent together). Therefore, the other node starts in the Follower state and remains in the Follower state as long as it receives periodic heartbeats from the current Leader.

When the Follower reaches its timeout, it will start the election process in the following way:

Based on the responses that the Candidate receives from other nodes in the cluster, the three steps for the election can be derived result.

The implementation of consensus algorithms is generally based on replicated state machines. What is a replicated state machine:

In simple terms: the same initial recognition state + the same input = Same end state. Different nodes must be the same and certainUse a specific function to process the input, rather than introducing uncertain values, such as local time, etc. It is a good idea to use replicated log. Log has the characteristics of persistence and order preservation, and is the cornerstone of most distributed systems.

With the Leader, all concurrent requests from the client can form an orderly log (status) sequence on the Leader's side to represent the order in which these requests are processed. The Leader then sends its log sequence to the Followers to maintain the global consistency of the entire system. Note that this is not strong consistency, but eventual consistency.

The log consists of log entries with a sequential number (log index). Each log entry consists of the term when it was created, and the data contained in the log, which can be of any type, from simple types to blocks of the blockchain. Each log entry can be represented by a [term, index, data] sequence pair, where term represents the term, index represents the index number, and data represents the log data.

The Leader attempts to execute replication commands on a majority of the nodes in the cluster. If the replication is successful, the command is submitted to the cluster and the response is sent back to the client. Similar to two-phase commit (2PC), but the difference from 2PC is that the leader only needs the consent of more than half of the nodes (in a working state).

Both leader and follower may crash, so the log maintained by the follower may have the following situations compared with the leader

When the leader and follower are inconsistent, the leader forces the follower to copy its own log, the Leader will try from back to front. Each time AppendEntries fails, it will try the previous log entry (decrementing the nextIndex value) until it successfully finds the consistent position point of each Follower's log (based on the two guarantees mentioned above), and then moves backward one by one. Overrides the Followers entry after this position. So missing or extra entries may persist for multiple terms.

Requires the candidate's log to be at least as up-to-date as other nodes. If not, the follower node will not vote for the candidate.

Means that each submitted entry must exist in at least one of these servers. If a candidate's log is at least as up-to-date as the other logs in the majority, it will save all committed entries, avoiding a log rollback event.

That is, anyAt most one leader can be elected during the term. This is very important, there can only be one leader in a replica set at any time. There is more than one leader in the system at the same time, which is called brain split. This is a very serious problem and will cause data coverage loss. In raft, two points guarantee this property:

Therefore, there must be only one leader in a certain term.

When the status of nodes in the cluster changes (the cluster configuration changes), the system is vulnerable to system failure. So, to prevent this, Raft uses something called a two-phase approach to changing cluster membership. Therefore, in this approach, the cluster first changes to an intermediate state (called federated consensus) before implementing a new membership configuration. Federated consensus enables the system to be used to respond to client requests even when transitioning between configurations, and its main purpose is to improve the availability of distributed systems.

⑦ In-depth understanding of the consensus mechanism and algorithm principles of the blockchain

The so-called "consensus mechanism" is to complete the verification of transactions in a very short time through the voting of special nodes and confirmation; for a transaction, if several nodes with unrelated interests can reach a consensus, we can think that the entire network can also reach a consensus on it. To put it more simply, if a Chinese Weibo influencer, a virtual currency player in the United States, an African student and a European traveler do not know each other, but they all agree that you are a good person, then it can basically be concluded that You're not a bad person.

In order for the entire blockchain network node to maintain the same data and ensure the fairness of each participant, all participants in the entire system must have a unified agreement, which is what we have here The consensus algorithm to be used. All Bitcoin nodes follow unified protocol specifications. The protocol specification (consensus algorithm) consists of relevant consensus rules, which can be divided into two major cores: proof of work and the longest chain mechanism. The ultimate expression of all rules (consensus) is the longest chain of Bitcoin. The purpose of the consensus algorithm is to ensure that Bitcoin continues to operate on the longest chain, thereby ensuring the consistency and reliability of the entire accounting system.

Users in the blockchain do not need to consider the credit of the other party when conducting transactions, do not need to trust the other party, and do not need a trusted intermediary or central agency. They only need to follow the blockchain protocol. Realize the transaction. The premise for smooth transactions without the need for a trusted third-party intermediary is the consensus mechanism of the blockchain, that is, in a market environment of mutual understanding and trust, each node participating in the transaction considers its own interests and does not violate any regulations. Motives and behaviors of cheating, so each node will actively and consciously abide by the preset rules to judge the authenticity and reliability of each transaction, and write the record of passing inspection into the blockchain. The interests of each node are different, and logicallyThey have no incentive to collude to cheat, and this is especially obvious when some nodes in the network have public reputation. Blockchain technology uses a consensus algorithm based on mathematical principles to establish a "trust" network between nodes, and uses technical means to achieve an innovative credit network.

At present, the mainstream consensus algorithm mechanisms in the district industry include: workload proof mechanism, equity proof mechanism, share authorization proof mechanism and Pool verification pool.

The workload proof mechanism is the proof of workload, which is a requirement that must be met when generating a new transaction information (i.e. a new block) to be added to the blockchain. In a blockchain network built based on the proof-of-work mechanism, nodes compete for accounting rights by calculating the numerical solution of random hashing. The ability to obtain the correct numerical solution to generate blocks is a specific manifestation of the node's computing power. The proof-of-work mechanism has the advantage of being completely decentralized. In a blockchain with a proof-of-work mechanism as the consensus, nodes can enter and exit freely. The well-known Bitcoin network uses a proof-of-work mechanism to produce new currencies. However, since the application of the workload proof mechanism in the Bitcoin network has attracted most of the computing power of computers around the world, it is difficult for other blockchain applications that want to try to use this mechanism to obtain the same scale of computing power to maintain their own security. At the same time, mining based on the proof-of-work mechanism also causes a lot of waste of resources, and the period required to reach consensus is also long, so this mechanism is not suitable for commercial applications.

In 2012, a netizen with the pseudonym Sunny King launched Peercoin. This encrypted electronic currency uses a proof-of-work mechanism to issue new coins and a proof-of-stake mechanism to maintain network security. This is the role of the proof-of-stake mechanism in encrypted electronic currency. first application in . Rather than requiring the certifier to perform a certain amount of computational work, Proof of Stake simply requires the certifier to provide ownership of a certain amount of cryptocurrency. The way the proof-of-stake mechanism works is that when a new block is created, the miner needs to create a "coin rights" transaction, which sends a number of coins to the miners themselves according to a preset ratio. The proof-of-stake mechanism reduces the mining difficulty of nodes in equal proportions based on the proportion and time of tokens owned by each node based on the algorithm, thus speeding up the search for random numbers. This consensus mechanism can shorten the time required to reach consensus, but essentially still requires nodes in the network to perform mining operations. Therefore, the PoS mechanism does not fundamentally solve the problem that the PoW mechanism is difficult to apply in the commercial field.

The share authorization certification mechanism is a new consensus mechanism to ensure network security. While trying to solve the problems of the traditional PoW mechanism and PoS mechanism, it can also offset the negative effects of centralization by implementing technological democracy.

The share authorization proof mechanism is similar to board voting. This mechanism has a built-in real-time shareholder voting system, just like the systemA never-ending shareholders' meeting is held at any time, where all shareholders vote on company decisions. The decentralization of the blockchain established based on the DPoS mechanism relies on a certain number of representatives rather than all users. In such a blockchain, all nodes vote to elect a certain number of node representatives, who act on behalf of all nodes to confirm blocks and maintain the orderly operation of the system. At the same time, all nodes in the blockchain have the power to remove and appoint representatives at any time. If necessary, all nodes can vote to disqualify the current node representatives and re-elect new representatives to achieve real-time democracy.

The share authorization certification mechanism can greatly reduce the number of nodes participating in verification and accounting, thereby achieving second-level consensus verification. However, this consensus mechanism still cannot perfectly solve the application problems of blockchain in business, because this consensus mechanism cannot get rid of its dependence on tokens, and the existence of tokens is not required in many commercial applications.

The Pool verification pool is established based on traditional distributed consistency technology and is supplemented by a data verification mechanism. It is a consensus mechanism widely used in current blockchains.

The Pool verification pool can work without relying on tokens. Based on mature distributed consensus algorithms (Pasox, Raft), it can achieve second-level consensus verification, which is more suitable for multi-party participation. Polycentric business model. However, the Pool verification pool also has some shortcomings. For example, the degree of distribution that the consensus mechanism can achieve is not as good as the PoW mechanism.

Here we mainly explain some algorithm principles of the blockchain workload proof mechanism and the Bitcoin network. How to prove your workload? I hope everyone can have a basic understanding of the consensus algorithm.

The main feature of the proof-of-work system is that the client has to do a certain amount of difficult work to get a result, and the verifier can easily use the results to check whether the client has done the corresponding work. A core feature of this scheme is asymmetry: the work is modest for the requester and easy to verify for the verifier. It differs from CAPTCHAs, which are easier to solve by humans rather than easier to solve by computers.

The figure below shows the workload proof process.

For example, give a basic character "hello, world!", the workload requirement we give is that you can add a nonce (random number) after this character creation Integer value, perform SHA-256 operation on the changed (nonce added) character creation, if the result (expressed in hexadecimal form) starts with "0000", the verification is passed. In order to achieve the goal of this workload proof, it is necessary to continuously increase the nonce value.The resulting character string is hashed with SHA-256. According to this rule, it takes 4251 operations to find the hash with leading 4 zeros.

Through this example, we have a preliminary understanding of the proof-of-work mechanism. Some people may think that if proof of work is just such a process, then it is enough to remember that the nonce is 4521 so that the calculation can pass verification. Of course not, this is just an example.

Next we simply change the input to "Hello, World! + integer value". The integer value ranges from 1 to 1000, which means that the input is turned into an array of 1 to 1000: Hello, World !1;Hello,World!2;...;Hello,World!1000. Then perform the above proof of work on each input in the array in turn - find the hash with leading 4 zeros.

Due to the pseudo-random nature of the hash value, it is easy to calculate based on the relevant knowledge of probability theory. It is expected that it will take 2 to the 16th power of attempts to obtain a hash hash with four leading zeros. List. If you count the actual results of the 1,000 calculations just performed, you will find that the average number of calculations is 66,958, which is very close to 2 to the 16th power (65,536). In this example, the number of calculations expected by mathematics is actually the required "workload". Repeating the workload proof multiple times will be a probability event that conforms to statistical laws.

The actual number of calculations used to count the input characters and obtain the corresponding target result is as follows:

For any node in the Bitcoin network, if you want to generate a new block To join the blockchain, you must solve this puzzle of the Bitcoin network. The key elements of this question are the proof-of-work function, block and difficulty value. The workload proof function is the calculation method of this question, the block is the input data of this question, and the difficulty value determines the amount of calculation required to understand this question.

The proof-of-work function used in the Bitcoin network is the SHA-256 mentioned above. Blocks are actually generated in the proof-of-work process. Kuangong constantly constructs block data and checks whether each calculated result meets the required workload, thereby determining whether the block meets the network difficulty. The block header is the input data of the Bitcoin proof-of-work function.

The difficulty value is an important reference indicator for miners to mine. It determines how many hash operations it takes for miners to generate a legal block. The Bitcoin network generates a block approximately every 10 minutes. If the generation of new blocks basically maintains this speed under different network computing power conditions, the difficulty value must be adjusted according to changes in the computing power of the entire network. The general principle is to keep the network generated for 10 minutes regardless of the mining power.A new block.

The adjustment of the difficulty value occurs independently and automatically in each complete node. Every 2016 blocks, all nodes will automatically adjust the difficulty value according to a unified format. This formula is based on the time spent in the latest 2016 blocks and the expected time (assuming a withdrawal is generated every 10 minutes, the expected time is 20160 minutes) and adjusted according to the ratio of actual duration to expected duration. That is, if blocks are generated faster than 10 minutes, increase the difficulty value; anyway, decrease the difficulty value. The formula is expressed as follows:

New difficulty value = old difficulty value * (20160 minutes/time spent in the past 2016 blocks).

Proof of work requires a target value. The calculation formula of the target value (Target) of Bitcoin's proof of work is as follows:

Target value = maximum target value/difficulty value, where the maximum target value is a constant value

The size of the target value is inversely proportional to the difficulty value. To achieve the Bitcoin workload proof, the block hash value calculated in the mine must be less than the target value.

We can also simply understand the process of Bitcoin workload as performing SHA-256 hash operation by constantly changing the block header (that is, trying different nonce values) and using it as input. Find a process that has a hash value in a specific format (that is, requires a certain number of leading 0s), and the more leading 0s required, the more difficult it becomes.

The steps of Bitcoin’s proof-of-work puzzle can be roughly summarized as follows:

The process can be represented by the following figure:

Bitcoin’s proof of work is the main work we commonly call “mining”. Understanding the workload proof mechanism will lay the foundation for us to further understand the consensus mechanism of the Bitcoin blockchain.

⑧ Self-study blockchain (6) BTC-mining difficulty

Let’s take a look at the mining calculation formula

H (block header) target , this target is the target threshold

The hash algorithm used by BTC is SHA-256, and the hash value it generates is 256 bits, so there are 2^256 values, and this is its output space, to increase the difficulty of mining, adjust the proportion of the target value in this output space.

Mining difficulty is inversely proportional to the target threshold. When the computing power is strong, adjust the difficulty to make the target threshold smaller.

Without adjusting the difficulty, asAs the number of miners increases, and as the computing power increases, the time to mine a block will become shorter, from 10 minutes to 1 minute or even a few seconds. What kind of problems will this bring? Many people may think this is not good. Waiting for six confirmations for a transaction will shorten the time and make the transaction faster. In fact, if the block generation time is shortened to a very short time, the risk is very high. Because of the network delay, the block generation time is shortened, and different nodes are likely to receive different block information, resulting in the emergence of many forked nodes. Miners will continue to mine based on the blocks they think are correct. In this case, it is easier for malicious nodes to launch a fork attack, because the computing power of honest nodes is dispersed.

As a result, it does not require 51% of the computing power to succeed, so shortening the block generation time is not conducive to the stability of the BTC system. Although 10 minutes is not necessarily the optimal time, it is still considered reasonable.

The following is the computing power growth curve

The following is the mining difficulty curve

The following is the average mining time

Let’s take a look at the difficulty formula: the mining difficulty is adjusted every 2016 blocks, and one block is produced every 10 minutes. On average, it is adjusted once every two weeks.

previous_difficulty is the last mining difficulty, and the denominator is the time spent in the last 2016 blocks

Each node mines independently, and the BTC protocol is also open source , will there be miners who do not modify the mining difficulty? The possibility exists, but it does not affect the result, because broadcasting to other nodes requires independent verification of the hash value of the block header. There is a compression code of difficulty in this header. The result of modifying the difficulty will not be recognized by honest nodes. .

博客主人唯心底涂
男,单身,无聊上班族,闲着没事喜欢研究股票,无时无刻分享股票入门基础知识,资深技术宅。
  • 35763 文章总数
  • 3637265访问次数
  • 3078建站天数