Author: Lin Guanhong/The Ghost at my Fingertips
The Denver nuggets: juejin. Cn/user / 178526…
Blog: www.cnblogs.com/linguanh/
Making: github.com/af913337456…
Tencent cloud column: cloud.tencent.com/developer/u…
BTW: My technical book “Blockchain Ethereum DApp Development Combat” has been published and can be purchased online, suitable for elementary and intermediate blockchain technology related research and development personnel to read.
directory
- Before the order
- PoS consensus algorithm
- The characteristics of the PoS
- Writing PoS code
- Candidate block array
- Address of a node in a block
- Share allocation
- Winners are chosen based on equity
- The end of the
Before the order
This article is a preface to the previous article, which was linked to:
Juejin. Cn/post / 684490… (I) Consensus algorithm of blockchain: general introduction and popular explanation of bifurcation
This article will focus on the PoS consensus algorithm and use code examples to illustrate it realistically
About what is the consensus algorithm this question, please check (a) blockchain consensus algorithm: the overall introduction and popular explanation of bifurcation, which has given the answer.
PoS consensus algorithm
PoS is Proof of Stake. The literal meaning is, joint stock system. That is to say, whose share is more, whose word affair power is bigger, this and the meaning of the shareholder in the joint-stock company in our life are about the same.
However, in the case of blockchain, we can’t actually assign shares to the nodes in the chain. Instead, there are other things that act as shares and we assign those things to the nodes in the chain. Here are some examples to illustrate this concept.
For example, PoS in the application of virtual currency, we can control the amount of money, as the number of shares, the number of shares, assuming that a public chain is measured by the number of XXX tokens, the number of shares of this node, how much power it has. Suppose there are 3 nodes in A public chain network, A, B and C. Node A has 10000 XXX tokens, while B and C have 1000 and 2000 tokens respectively. Then, in this Ethereum network, block A is most likely to be selected, and the power of speech is relatively large.
For example, assuming a future non-virtual currency blockchain, public chain, and a chain combined with entity industry, such as car chain, we can allocate shares by the number of cars owned by each owner and the value of his car. For example, we stipulate a formula: Number of cars * value of cars = number of shares, and shares are a concept in PoS, a concept that measures the right to speak.
The characteristics of the PoS
The above description has already illustrated the concept of PoS consensus algorithm. Because it measures power in terms of the number of things that it owns, which means that as long as our node has one of these things, even if it has only one of these things, it has power, even if it’s small, it doesn’t even have a chance to show up, but it still has a chance.
In PoS, blocks are already minted (there is no “mining” here, so we don’t use the word to prove shares), and PoW has mining.
As a result, it has the following characteristics:
-
Advantages:
- The time to reach consensus is shortened and the speed of consensus blocks in the chain is faster
- You don’t have to use a lot of energy to mine
- It pays to cheat, because if a person who owns more than 51% of the shares cheats, it means that he has cheated himself, because he is the person who owns the most shares, and the result of cheating is that the more he owns, the more he loses
-
Disadvantages:
- Attacks are cheap and can only be launched if the node has the number of items, such as tokens
Block attacks on dirty data
- In addition, nodes with a large number of tokens are more likely to get the right to account, which will make the network consensus dominated by a few rich accounts, thus losing impartiality
- Attacks are cheap and can only be launched if the node has the number of items, such as tokens
Writing PoS code
In order to make it accessible to more people and non-GO developers, the following will be implemented through pseudo code. Please leave the complete GO code in the email.
First we use an array of candidate blocks to hold the block objects broadcast by each node and generated by its current node:
CandidateBlocks []Blocks Candidate block arrayCopy the code
Each block structure has a variable that records the address of the node that generated the block
type Block struct {
Timestamp string // Timestamp, which represents the generation time of the block
Hash string // The hash value of this block
PrevHash string // The hash value of the previous block of this block
NodeAddress string // Generate a node address for this block
Data string // The data carried by the block
}
Copy the code
There is then a child thread that is responsible for traversing the candidate block array to get its token number based on the address of the node in the block, and then allocating equity
stakeRecord []string / / array
for block ~ candidateBlocks {
coinNum = getCoinBalance(block.NodeAddress) // The number of coins obtained
for i ~ coinNum { // Loop to add as many coins as possible
if stakeRecord.contains(block.NodeAddress) { // Whether and contain
break // The included ones will not be added again
}
stakeRecord = append(block.NodeAddress) / / add}}Copy the code
An election winner is then chosen from stakeRecord. This probability is related to the coinNum above, the bigger the better the chance.
index = randInt() // Get an integer random number
winner = stakeRecord[index] // Retrieve the address of the winner node
Copy the code
Finally, we can take the block generated by this winner to access the public chain, and then broadcast out
for block ~ candidateBlocks {
if block.NodeAddress == winner {
/ / add}}// Broadcast out.Copy the code