区块链怎么达成共识交易,区块链怎么达成共识模型
近年来,随着区块链技术的迅猛发展,越来越多的行业都开始关注如何通过区块链达成共识交易。今天,我们就来聊聊区块链怎么达成共识交易。
首先,我们来聊聊共识机制。共识机制是区块链系统中最重要的一环,它是由参与者之间协商达成一致,以便确定交易的有效性和安全性。目前,主流的共识机制主要有工作量证明(Proof of Work)、权益证明(Proof of Stake)、拜占庭容错(Byzantine Fault Tolerance)等几种。工作量证明是最常见的共识机制,它是一种挖矿模式,参与者可以通过计算能力来获得报酬。权益证明则是一种投票模式,参与者可以通过投票来确定交易的有效性。拜占庭容错则是一种多重签名模式,参与者可以通过多重签名来确定交易的有效性。
其次,我们来聊聊智能合约。智能合约是区块链技术中的一种重要应用,它是一种可执行的代码,可以自动执行各种交易,而无需人工干预。智能合约可以让参与者之间的交易更加安全和可靠,因为它可以在不同参与者之间建立信任关系,而无需中间机构的参与。智能合约还可以帮助参与者更好地管理资产,因为它可以提供更安全、更可靠的资产管理服务。
最后,我们来聊聊去中心化交易所。去中心化交易所是一种利用区块链技术实现的去中心化的交易所,它可以实现安全、可靠、高效的交易服务。去中心化交易所的优势在于,它不需要中心化的第三方机构参与,参与者可以直接参与交易,减少交易成本。另外,去中心化交易所还可以实现更安全、更可靠的交易,因为它可以通过智能合约实现安全的交易服务。
以上就是关于区块链怎么达成共识交易的相关内容,从共识机制、智能合约和去中心化交易所三个方面介绍了区块链怎么达成共识交易的相关知识。希望通过本文的介绍,大家对区块链怎么达成共识交易有了更深入的了解。
请查看相关英文文档
Ⅰ What are the blockchain consensus mechanisms?
1.pow(Proof of Work)
One sentence introduction: The more you work, the more you receive more.
2.POS Proof of Stake, Proof of Stake
One sentence introduction: The more you hold, the more you get.
3.PBFT: Practical Byzantine Fault Tolerance, Practical Byzantine Fault Tolerance
Introduction: It provides (n-1)/3 fault tolerance on the premise of ensuring liveness & safety.
4.dBFT: delegated BFT Authorized Byzantine Fault Tolerance Algorithm
Introduction: The dBFT mechanism adopted by Xiaoyi selects bookkeepers based on equity, and then the bookkeepers reach a consensus through the Byzantine Fault Tolerance algorithm. .
5.POOL verification pool
Based on traditional distributed consistency technology, plus data verification mechanism.
ChainNova is a blockchain company. It feels pretty good. Although I don’t know much about hashing algorithms, I still know a little bit about it.
II How is the consensus algorithm of blockchain technology formed?
Chongqing Jinwowo analyzed the formation method of the consensus algorithm as follows:
The consensus mechanism of blockchain , that is, how to reach a consensus among all distributed nodes, generate and update data through algorithms, and determine the validity of a record. This is both a means of identification and a means of preventing tampering.
Blockchain mainly includes four different consensus mechanisms, which are suitable for different application scenarios and strike a balance between efficiency and security.
III Consensus Mechanism of Blockchain
1. How to confirm and reach consensus on transaction information on the network?
Although the consensus mechanism is often mentioned, the meaning and understanding of the consensus mechanism are not clear. Therefore, it is necessary to understand the relevant concepts, principles and implementation methods of the consensus mechanism.
The transaction information of the blockchain is transmitted to each node in the network through network broadcast. How to confirm the broadcast information and reach a consensus among the entire network nodes and finally write it into the block? If there is no corresponding reliable and secure implementation mechanism, it will be difficult to realize its basic functions. Therefore, the consensus mechanism is a key to the operation of the entire network.
The consensus mechanism solves the problem of how the blockchain can achieve consistency in a distributed scenario. The blockchain can reach a relatively balanced state among many nodes because of the consensus mechanism. So how does the consensus mechanism solve the problem of mutual trust between nodes based on the idea of decentralization?
When the idea of distribution was proposed, people began to design consensus algorithms based on the FLP theorem and the CAP theorem. Standardly speaking, the consistency of an ideal distributed system should meet the following three points:
1. Termination: The consistency result can be completed within a limited time.
2. Consensus: The final decision-making results of different nodes should be the same.
3. Validity: The result of the decision must be a proposal put forward by other processes.
However, in actual computer clusters, the following problems may exist:
1. Nodes have different transaction processing capabilities, and the throughput of network node data is different
>
2. The communication channel between nodes may be unsafe
3. There may be malicious nodes
4. When the asynchronous processing capabilities reach a high degree of consistency, The scalability of the system will become worse (cannot tolerate the addition of new nodes).
Scientists believe that it is impossible to achieve complete consistency in a distributed scenario. However, engineers can sacrifice part of the cost in exchange for the consistency of distributed scenarios. The above two major theorems also have this idea. Therefore, various formula mechanisms based on blockchain design can be regarded as sacrificing part of the cost in exchange for more adaptability. My idea is to make a flexible transformation on this idea, that is, sacrificing part of the cost at the appropriate time and space in exchange for consistency adapted to the scene at that time, and a flexible blockchain system can be realized that is pluggable. Pull-out blockchain system. Today I will introduce my views and analysis on various consensus mechanisms. Distributed systems with or without malicious nodes are divided into Byzantine fault-tolerant and non-Byzantine fault-tolerant mechanisms.
The FLP theorem is the impossibility of FLP. It proves that in a distributed scenario, no matter any algorithm, even if only one process hangs up, there is an inability to reach consensus for other non-failed processes. possible.
FLP is based on the following assumptions:
Can only be modified once: Each process initially records a value (0 or 1). The process can receive messages, change the value, and send messages. When the process enters the decide state, the value will no longer change. The protocol ends successfully when all non-failed processes enter the decided state. This is relaxed to the extent that some processes enter the decided state, even if the agreement is successful.
Asynchronous communication: The biggest difference from synchronous communication is that there is no clock, no time synchronization, no timeout, and no detectionFailure, messages can be delayed arbitrarily, and messages can be out of order.
Communication is robust: As long as the process does not fail, the message will be delayed indefinitely, but will eventually be delivered; and the message will only be delivered once (no duplication).
Fail-Stop model: A process failure is like a downtime and no longer processes any messages.
Number of failed processes: At most one process fails.
CAP is the most discussed theory in distributed systems, especially in the field of distributed storage. CAP was proposed by Eric Brewer at the PODC meeting in 2000. It was a result of Eric Brewer's research on data consistency (consistency), service availability (availability), and partition fault tolerance (partition- tolerance) conjecture:
Data consistency (consistency): If the system returns success for a write operation, then subsequent read requests must read the new data; if the system returns failure, then all read operations No one can read this data. For the caller, the data has strong consistency (also called atomic and linearizable consistency) [5]
Service availability (availability) : All read and write requests are responded to within a certain period of time, can be terminated, and will not wait forever
Partition-tolerance: In the case of network partitions, the separated nodes can still function normally External services
If AP is met at a certain moment, the separated nodes can provide external services at the same time but cannot communicate with each other, which will lead to inconsistent status, that is, C cannot be met; if CP is met, C will not be achieved in the case of network partitions. , the request can only wait forever, that is, A is not satisfied; if CA is to be satisfied, the node status must be consistent within a certain period of time, and network partitions must not occur, then P cannot be satisfied.
C, A, and P can only satisfy at most two of them. Like the FLP theorem, the CAP theorem also indicates an unreachable result (impossibility result).
IV What is the consensus mechanism in blockchain technology
1. What is the consensus mechanism
In a decentralized structural system, due to the The status is equal. When disagreements arise, how to reach consensus becomes a problem.
So, a well-designed and simple-to-operate sharedThe recognition mechanism is the key for a distributed system to operate smoothly.
In short, the consensus mechanism is an algorithm that reaches consensus on the order of things within a period of time. It is a mechanism for blockchain nodes to reach a consensus across the entire network on block information.
IV What is the consensus mechanism of blockchain?
How to achieve consensus in a decentralized network? In the blockchain system, there is no centralized accounting institution like a bank. It is crucial to ensure the consistency of each transaction on all accounting nodes, that is, to achieve consensus across the entire network. The consensus mechanism solves this problem. At present, the main consensus mechanisms include the proof-of-work mechanism PoW and the proof-of-stake mechanism PoS. PoW determines your probability of obtaining accounting rights by evaluating your workload. The greater the workload, the more likely you are to obtain this accounting opportunity. PoS determines your probability of obtaining accounting rights by evaluating the number and length of time you hold tokens. This is similar to the dividend system of stocks. People who hold relatively large shares can receive more dividends. The principle of DPOS is similar to that of POS, except that some "people's congress representatives" are selected. The main difference from PoS is that nodes elect several agents, who verify and keep accounts. With the development of technology, more advanced consensus mechanisms may be born in the future.
VI 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 consume resources for calculation, and verification only needs to be done once.
PThe oS 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 a node pledges more funds, the probability of it being selected to package a block The bigger.
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. 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 replica replication algorithm. All replicas operate in the process of a view (view) rotation. The master The node is determined by the view number and the set of node numbers, that is: main 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
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 <
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
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
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 COMMIT message that passes verification, indicating that most nodes in the current network have reached a consensus, run the client's requested operation o, and return
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 acts maliciously and does not broadcast the client's request, the client sets a timeout mechanism. If the timeout occurs, the request message is broadcast 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
When the master node p = v + 1 mod |R| receives 2f valid VIEW-CHANGE messages, it broadcasts
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. After executing the request The replica node needs to clear the record messages of the previous request.
The simplest way is to perform consensus synchronization of the current state again after the Reply message. This is relatively expensive, so it can be done after executing multiple requests K (for example: 100)Perform a state synchronization. This status synchronization message is the CheckPoint message.
Replica node i sends
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 RaftIn a cluster, a minimum number of nodes is required to provide the expected level of consensus guarantee, also known as 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
< p> Cluster scheduling: HashiCorp NomadCan only accommodate faulty nodes (CFT), not evil nodes
Sequential voting, only serial apply, so high concurrency Poor performance in scenarios
Raft solves the distributed consensus problem by solving the three main sub-problems surrounding Leader election and managing the security functions of distributed logs and algorithms.
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. Right nowDuring the normal period, 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 should use the same and deterministic function to process 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 follower may have the following situation compared with leader
When leader and follower are inconsistent, 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 of each Follower's log (based on the above two guarantees). ), and then overwrite the Followers entries after that position one by one. 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, at most one leader can be elected in any 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.
Ⅶ Are there 7 core technologies for blockchain operation that you should know?
How many of the 7 core technologies for blockchain operation do you know?
1 .Blockchain link
As the name suggests, blockchain is a chain composed of blocks. Each block is divided into two parts: block header and block body (including transaction data). The block header includes the hash (PrevHash) value (also called hash value) of the previous block used to implement block linking and the random number (nonce) used to calculate the mining difficulty. The hash value of the previous block is actually the hash value of the header of the previous block, and the rules for calculating random numbers determine which miner can obtain the power to record the block.
2. Consensus Mechanism
Blockchain was born with Bitcoin and is the basic technical architecture of Bitcoin. Blockchain can be understood as a decentralized accounting system based on the Internet. A decentralized digital currency system like Bitcoin,To ensure the consistency of the accounting of each honest node without a central node, a blockchain is needed to complete it. Therefore, the core of blockchain technology is a consensus mechanism that reaches consensus on the legality of transactions among individuals who have no basis for trust in each other without central control.
There are currently four main categories of blockchain consensus mechanisms: PoW, PoS, DPoS, and distributed consensus algorithms.
3. Unlocking scripts
Scripts are an important technology for automatic verification and automatic execution of contracts on the blockchain. Each output of each transaction does not strictly point to an address, but to a script. A script is like a set of rules that govern how the recipient can spend the assets locked on this output.
The legality verification of transactions also relies on scripts. Currently it relies on two types of scripts: locking scripts and unlocking scripts. The locking script is a condition added to the output transaction, implemented through a script language, and is located at the output of the transaction. The unlocking script corresponds to the locking script. Only if the conditions required by the locking script are met, the corresponding assets on this script can be spent, which is located at the input of the transaction. Many flexible items can be expressed through scripting language. The interpretation script is similar to a "virtual machine" in our programming field, which is distributed and runs on every node in the blockchain network.
4. Transaction Rules
Blockchain transactions are the basic units that constitute blocks, and are also the actual effective content that the blockchain is responsible for recording. A blockchain transaction can be a transfer or other transactions such as the deployment of smart contracts.
In the case of Bitcoin, a transaction refers to a payment transfer. The trading rules are as follows:
1) The input and output of the transaction cannot be empty.
2) For each input of the transaction, if its corresponding UTXO output can be found in the current transaction pool, the transaction will be rejected. Because the current transaction pool is a transaction that has not been recorded in the blockchain, and each input of the transaction should come from a confirmed UTXO. If found in the current transaction pool, it is a double spend transaction.
3) For each input in the transaction, its corresponding output must be UTXO.
4) Each input unlocking script must work with the corresponding output locking script to verify the compliance of the transaction.
5. Transaction Priority
The priority of blockchain transactions is determined by the blockchain protocol rules. For Bitcoin, the priority of a transaction being included in a block is determined by the time it takes for the transaction to be broadcast to the network and the size of the transaction. As the time it takes for a transaction to be broadcast to the network increases and the chain age of the transaction increases, the priority of the transaction is increased and will eventually be included in the block. For Ethereum, the priority of a transaction is also related to the transaction fee that the publisher of the transaction is willing to pay. The higher the transaction fee that the publisher is willing to pay, the higher the priority of the transaction being included in the block.
6.Merkle proof
The original application of Merkle proof is the Bitcoin system(Bitcoin), which was described and created by Satoshi Nakamoto in 2009. The Bitcoin blockchain uses Merkle proofs in order to store transactions in every block. This makes the transaction unable to be tampered with and makes it easy to verify whether the transaction is included in a specific block.
7.RLP
RLP (Recursive Length Prefix, recursive length prefix encoding) is a main encoding method for object serialization in Ethereum. Its purpose is to encode any nested sequence of binary virtual data. Lu Ji
Ⅷ Three common consensus mechanisms in blockchain
Blockchain is a distributed ledger system built on P2P network and participated by nodes. Its biggest feature is It is "decentralization". That is to say, in the blockchain system, there is no need to establish trust between users, between users and institutions, and between institutions. Transactions can be realized by relying only on the blockchain protocol system.
But how to ensure the accuracy, authority, and reliability of the ledger? Why do nodes on the blockchain network participate in accounting? What should I do if the node is fake? How to prevent the ledger from being tampered with? How to ensure data consistency between nodes? …These are the problems that blockchain needs to solve when establishing “decentralized” transactions, resulting in the consensus mechanism.
The so-called "consensus mechanism" is to complete the verification and confirmation of transactions in a very short time through the voting of special nodes; when there is a disagreement, without central control, several Nodes participate in decision-making to reach consensus, that is, how to establish a trust relationship between individuals who have no basis for trust in each other.
Blockchain technology uses a set of consensus-based mathematical algorithms to establish a "trust" network between machines, thereby creating new credit through technical endorsement rather than centralized credit institutions.
Different blockchain types require different consensus algorithms to ensure that the last block on the blockchain can reflect the status of the entire network at any time.
So far, the blockchain consensus mechanisms mainly include the following: POW workload proof, POS equity proof, DPOS authorized equity proof, Paxos, PBFT (Practical Byzantine Fault Tolerance Algorithm), dBFT, DAG ( Directed acyclic graph)
Next, we will mainly talk about the principles and application scenarios of common POW, POS, and DPOS consensus mechanisms
Concept:
Proof of work was originally an economic term, referring to the measurement method set up by the system to achieve a certain goal. A simple understanding is a certificate to confirm that you have done a certain amountwork, and prove that the corresponding workload has been completed by certifying the results of the work.
The proof-of-work mechanism has the advantage of complete decentralization. In a blockchain with a proof-of-work mechanism as the consensus, nodes can enter and exit freely and calculate the numerical solution of the random hash. The ability to compete for accounting rights and obtain correct numerical solutions to generate blocks is a concrete manifestation of node computing power.
Applications:
The most famous application of POW is Bitcoin. In the Bitcoin network, during the Block generation process, miners need to solve complex cryptographic mathematical problems to find a Block Hash that meets the requirements, consisting of N leading zeros. The number of zeros depends on the difficulty value of the network. During this period, a lot of trial calculations (workload) are required, and the calculation time depends on the hashing speed of the machine.
Finding a reasonable hash is a probabilistic event. When a node has n% of the computing power of the entire network, the node has an n/100 probability of finding the Block Hash. After the node successfully finds a satisfactory Hash value, it will immediately broadcast the packaged block to the entire network. The nodes in the network will verify it immediately after receiving the broadcast packaged block.
If the verification passes, it means that a node has successfully solved the puzzle, and it will no longer compete for the current block, but choose to accept the block, record it in its own ledger, and then proceed to the next block. Competitive guessing game of blocks. Only the fastest puzzle-solving block in the network will be added to the ledger, and other nodes will copy it, thus ensuring the uniqueness of the entire ledger.
If a node engages in any cheating behavior, it will cause the network node verification to fail and directly discard its packaged block. This block will not be recorded in the general ledger, and the cost of the cheating node will be It is all in vain. Therefore, under the huge mining cost, miners voluntarily comply with the consensus protocol of the Bitcoin system, thus ensuring the security of the entire system.
Advantages and Disadvantages
Advantages: The results can be verified quickly, the system bears a large number of nodes, and the cost of evil is high to ensure the conscious compliance of miners.
Disadvantages: It requires a large amount of algorithm consumption, and it takes a long time to reach consensus
Concept:
Proof of Stake mechanism (Proof of Stake), requirements A certifier provides 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 will send some 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.This speeds up the search for random numbers.
Application:
In 2012, a netizen with the pseudonym Sunny King launched Peercoin, which was the first application of the proof-of-stake mechanism in encrypted electronic currency. The biggest innovation of PPC is that its mining method mixes POW and POS methods, uses a proof-of-work mechanism to issue new coins, and uses a proof-of-stake mechanism to maintain network security.
In order to implement POS, Sunny King learned from Satoshi Nakamoto’s Coinbase and designed a special type of transaction called Coinstake.
The picture above shows how Coinstake works. Coin age refers to the holding period of the currency. If you own 10 coins and hold them for 10 days, then you have collected 100 days of coins. age. If you use these 10 coins, the coin age is consumed (destroyed).
Advantages and Disadvantages:
Advantages: Shortens the time required to reach consensus and is more energy-saving than proof of work.
Disadvantages: Essentially, nodes in the network are still required to perform mining operations, and the authenticity of transfers is difficult to guarantee
Concept:
Authorized equity certificate Mechanism (Delegated Proof of Stake) is similar to board voting. This mechanism has a built-in real-time shareholder voting system, just like the system is convening a never-ending shareholders' meeting at any time, where all shareholders vote to decide company decisions.
While trying to solve the problems of traditional PoW and PoS mechanisms, authorized proof of equity can also offset the negative effects of centralization by implementing technological democracy. 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.
Application:
Bitshare is a type of cryptocurrency that uses the DPOS mechanism. By introducing the concept of witnesses, witnesses can generate blocks, and everyone who holds BitShares can vote for witnesses. Candidates who receive the top N (N is usually defined as 101) votes in total can be elected as witnesses. The number of elected witnesses (N) must meet: at least oneHalf of the participating voters believe that N is fully decentralized.
The candidate list of witnesses is updated every maintenance cycle (1 day). The witnesses are then randomly arranged, and each witness has 2 seconds of permission time to generate a block in order. If the witness cannot generate a block in a given time slice, the block generation permission is given to the witness corresponding to the next time slice. . This design of DPoS makes the generation of blocks faster and more energy-saving.
DPOS makes full use of the votes of shareholders to reach consensus in a fair and democratic way. The N witnesses they voted for can be regarded as N mining pools, and these N mining pools Each other's rights are completely equal. Shareholders can change these witnesses (mining pools) at any time by voting, as long as the computing power they provide is unstable, the computer is down, or they try to use their power to do evil.
Advantages and Disadvantages:
Advantages: Reduce the number of nodes participating in verification and accounting, thereby achieving second-level consensus verification
Disadvantages: Centrality Weaker, the security is weaker than POW, and the node agents are artificially selected, and the fairness is lower than POS. At the same time, the entire consensus mechanism still relies on the additional issuance of tokens to maintain the stability of the agent nodes.
Ⅸ 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, all parties involved in the transaction canEach node considers its own interests and does not have any motivation or behavior to violate regulations and cheating. Therefore, each node will actively and consciously abide by the preset rules to judge the authenticity and reliability of each transaction, and will verify the passing records. Write to the blockchain. The interests of each node are different, and logically there is no incentive for them to collude to deceive. 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 . Instead of 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. It is trying to solve the traditional PIn addition to the problems of the oW mechanism and the PoS mechanism, it can also offset the negative effects of centralization through the implementation of technological democracy.
The share authorization certification mechanism is similar to board voting. This mechanism has a built-in real-time shareholder voting system, just like the system is convening a never-ending shareholders' meeting at any time, where all shareholders vote. determine 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 workload proof 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 allNumerical 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 this proof-of-work goal, it is necessary to continuously increment the nonce value and perform a SHA-256 hash operation on the resulting character creation. 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 raised 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.
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 ensure that the network always generates a new block in 10 minutes, regardless of the mining power.
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.