
Sun Xiaojun
image description
System R&D Engineer of Meitu Cloud Division, mainly working on Meitu blockchain and cloud live broadcast.
Editor's note: This article comes from the BFTF Technology Community Alliance, author: BFTF Technology Community Alliance, published with authorization.
This speech is mainly divided into four parts. The first part briefly introduces the infrastructure of Ethereum. The first part summarizes several current consensus algorithms. The third part is the practice of DPoS in Meitu Ethereum, and finally compares DPoS in EOS.
The basic structure of Ethereum is shown in the figure, including external HTTP interface, consensus algorithm module, EVM, storage module, etc.
Among them, the transaction processing module, after the node receives the transaction, saves it in the transaction pool, and obtains the transaction that satisfies the conditions from the transaction pool during the packaging process. When operations such as block rollback occur, the packaged transactions will be put back into the transaction pool.
The account balance data of Ethereum is stored on the (MPT) tree composed of Patricia Trie and Merkle Trie, mainly using the characteristics of Patricia Trie to realize the fast lookup of account balance and Merkle Trie to prove whether the transaction has been tampered with.
PoW achieves consensus by satisfying a certain workload proof. This is a consensus algorithm that has been tested by Bitcoin and Ethereum for a long time, but it needs to consume a lot of computing resources, and the QPS of transactions is low. PoS completes the search of random numbers by introducing the rights and interests of holders. The more assets a user owns, the greater the probability of finding the correct random number. PoS has ledger forks, long-distance attacks, currency age attacks and other problems. DPoS is considered to be an improved version of PoS. DPoS conducts elections every once in a while, and then these elected nodes are responsible for block generation and mutual supervision and verification, which can greatly reduce the time for block generation and block confirmation.
The algorithm implementation mainly includes two core parts: block validator election and block validator scheduling.
To add a consensus algorithm to Ethereum, it is necessary to implement the interface defined by the consensus engine.
Packaging process: Miners will regularly check whether the current validator is the current node through CheckValidator, and if so, create a new block task through CreateNewWork.
If a node wants to become a validator, it must first become a candidate, and then other people can vote on this candidate. Whether it is voting or becoming a candidate, it is actually a transaction for nodes. Previous transactions were mainly transfers or contract calls, so now several more transaction types are added.
In addition, in order to avoid replaying historical data from the genesis block for each election, DPoS adds several global state trees to record the state of elections and voting, and stores the root corresponding to the tree in the block header.
Election: Judging the cycle of the block; according to the situation of the block in the previous cycle, kick out some candidates who were selected but the number of blocks did not meet the requirements; as of the last block, select the top N candidates with the highest number of votes As a validator; shuffle the order of validators.
When a new block is received, the information of the last irreversible block needs to be updated; the last irreversible block is a block confirmed by two-thirds of the nodes.
In fast sync mode, broadcasted blocks will be discarded directly, and will only be received after entering full sync. If we start multiple nodes with the default synchronization method (fast sync) at the same time, all nodes cannot enter the full sync mode due to the same frequency of block generation between nodes, and the blocks synchronized between nodes will be discarded. The solution is to start the founding node in full sync mode. Since the verifier's information is stored in the block body, a block cannot be verified only through the block header.
There are 21 verification nodes in DPoS in EOS; the time interval for building blocks is 500ms, and each node makes 12 blocks in a row. This improves the speed of building blocks, and at the same time ensures that each node can There are blocks propagated to the other 20 nodes. The latest white paper of EOS mentions a kind of BFT-DPoS. After a node prints a block, it can be verified to other nodes through the BFT algorithm, which can speed up the confirmation of the final irreversible block, increase the transaction confirmation speed, and reduce the fork of the chain. possibility.
https://v.qq.com/x/page/o0736irezov.html