-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathmain.go
106 lines (95 loc) · 2.56 KB
/
main.go
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
package main
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"log"
"math/big"
"strconv"
"time"
)
type block struct {
//上一个块的hash
prehash string
//本块hash
hash string
//时间戳
timestamp string
//区块内容
data string
//区块高度
height int
//挖出本块的地址
address string
}
//用于存储区块链
var blockchain []block
//代表挖矿节点
type node struct {
//代币数量
tokens int
//质押时间
days int
//节点地址
address string
}
//挖矿节点
var mineNodesPool []node
//概率节点池
var probabilityNodesPool []node
//初始化
func init() {
//手动添加两个节点
mineNodesPool = append(mineNodesPool, node{1000, 1, "AAAAAAAAAA"})
mineNodesPool = append(mineNodesPool, node{100, 3, "BBBBBBBBBB"})
//初始化随机节点池(挖矿概率与代币数量和币龄有关)
for _, v := range mineNodesPool {
for i := 0; i <= v.tokens*v.days; i++ {
probabilityNodesPool = append(probabilityNodesPool, v)
}
}
}
//生成新的区块
func generateNewBlock(oldBlock block, data string, address string) block {
newBlock := block{}
newBlock.prehash = oldBlock.hash
newBlock.data = data
newBlock.timestamp = time.Now().Format("2006-01-02 15:04:05")
newBlock.height = oldBlock.height + 1
newBlock.address = getMineNodeAddress()
newBlock.getHash()
return newBlock
}
//对自身进行散列
func (b *block) getHash() {
sumString := b.prehash + b.timestamp + b.data + b.address + strconv.Itoa(b.height)
hash := sha256.Sum256([]byte(sumString))
b.hash = hex.EncodeToString(hash[:])
}
//随机得出挖矿地址(挖矿概率跟代币数量与币龄有关)
func getMineNodeAddress() string {
bInt := big.NewInt(int64(len(probabilityNodesPool)))
//得出一个随机数,最大不超过随机节点池的大小
rInt, err := rand.Int(rand.Reader, bInt)
if err != nil {
log.Panic(err)
}
return probabilityNodesPool[int(rInt.Int64())].address
}
func main() {
//创建创世区块
genesisBlock := block{"0000000000000000000000000000000000000000000000000000000000000000", "", time.Now().Format("2006-01-02 15:04:05"), "我是创世区块", 1, "0000000000"}
genesisBlock.getHash()
//把创世区块添加进区块链
blockchain = append(blockchain, genesisBlock)
fmt.Println(blockchain[0])
i := 0
for {
time.Sleep(time.Second)
newBlock := generateNewBlock(blockchain[i], "我是区块内容", "00000")
blockchain = append(blockchain, newBlock)
fmt.Println(blockchain[i+1])
i++
}
}