POW consensus algorithm principle and implementation

About POW

Proof of Work, work to prove.
POW consensus algorithm is mainly determined by calculating the difficulty value out who is going to block. Workload POW refers equation solving, who first solved the problem, and who have the right to block out. The equation is calculated by a block in front of a hash value and a random value nonce hash value of the next block, the first to find the nonce, who can be the first to calculate the hash value of the next block, in this way It is called difficult to calculate the value of the equation because there is no fixed solution, but to keep trying, this equation solution is called hash collision, the probability of an event, the more the number of collisions, the more difficulty solving equations Big. Bitcoin is the use of POW consensus algorithm

Specific algorithm implementation principle

This involves two important concepts, is a difficulty factor, is a nonce, nonce can be understood as a random number, it is in mining to find a qualified nonce value.
4 is assumed here that the degree of difficulty (degree of difficulty is the initial credits bit 4), the data block plus a packed nonce value, nonce value has been incremented from zero, these packed data calculated hash values, hash meet top there are 4 0, that is successful mining. Difficulty factor is how much, hash foremost we need to meet the number 0.

Code is a simple implementation of the principle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main

import (
"strconv"
"time"
"crypto/sha256"
"strings"
"encoding/hex"
"fmt"
)

// pow mining algorithm

// define the degree of difficulty of
a large column   consensus principle and implementation of algorithms POW "> const difiiculty = 4

struct {Block type
Index int // block height
TimeStamp Int64
the Data String // transactions
the Hash String
prehash String
a Nonce int
Difficulty // int difficulty factor
}

// Create the block chain
var BlockChain [] Block

// Creation block
func GenesisBlock () * Block {

var geneBlock = Block{0, time.Now().Unix(), "", "", "", 0, difiiculty}
geneBlock.Hash = hex.EncodeToString(BlockHash(geneBlock))

return &geneBlock

}



func BlockHash(block Block) []byte {
re := strconv.Itoa(block.Index) + strconv.Itoa(int(block.TimeStamp)) + block.Data + block.Prehash +
strconv.Itoa(block.Nonce) + strconv.Itoa(block.Difficulty)

h := sha256.New()
h.Write([]byte(re))
hashed := h.Sum(nil)

return hashed


}

func isBlockValid(block Block) bool {
prefix := strings.Repeat("0", block.Difficulty)
return strings.HasPrefix(block.Hash, prefix)
}

// Create a new block pow mining
FUNC CreateNewBlock (lastBlock * Block, Data String) * Block {
var newBlock Block
newBlock.Index = lastBlock.Index +. 1
newBlock.TimeStamp Time.now = (). The Unix ()
newBlock.Data = Data
newBlock.Prehash = lastBlock.Hash
newBlock.Difficulty = difiiculty
newBlock.Nonce = 0
// excavation - difficulty coefficient values and the number of hash values of the current block is the same as the foregoing 0
for {
// calculated hash
cuhash: = hex.EncodeToString (BlockHash (newBlock))
fmt.Println ( "in mining", cuhash)
newBlock.Hash = cuhash
IF isBlockValid (newBlock) {

// check block
IF VerflyBlock (newBlock, lastBlock *) {
fmt.Println ( "successful mining")
return & newBlock
}
} newBlock.Nonce ++



}
}


// check whether the new block legitimate
FUNC VerflyBlock (newBlock Block, lastBlock Block) {BOOL
IF lastBlock.Index + 1'd! = {Newblock.Index
return to false
}
IF newblock.Prehash! = {LastBlock.Hash
return to false
}
return true

}

func main() {

var genBlock = GenesisBlock()

newBlock := CreateNewBlock(genBlock,"新区块")
fmt.Println(newBlock)

}

Export

1
2
3
4
5
6
Ac6665903c0cd2f000e17483fbcf6e3e8fa365de2b55663e7c94167f816d1489 mining in 
mining in a46e18c7938ccb2d0554232f94c6e8db933fae509adafd4091f5f0b51951e6ae
mining in 3738b5eb5f8f956974fc767058a6d7c94da0fc406e86df2d508b9b87fc109171
mining in 0000694b1acaec754175f0a49a1aa190e122b58e9f58125bd18ceec898f8d811
mining success
& {11,530,267,247 new block 0000694b1acaec754175f0a49a1aa190e122b58e9f58125bd18ceec898f8d811 a8df431924b17633bdf0303763661aa7a41c2608cd99f6527542e1326c718152 12167 4}

Guess you like

Origin www.cnblogs.com/sanxiandoupi/p/11711068.html