-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.go
122 lines (95 loc) · 2.21 KB
/
blockchain.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"github.com/boltdb/bolt"
"log"
)
const dbFile = "blockchain_demo.db"
const blocksBucket = "blocks"
type Blockchain struct {
tip []byte
db *bolt.DB
}
func (bc *Blockchain) AddBlock(data string) {
var lastHash []byte
err := bc.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(blocksBucket))
lastHash = b.Get([]byte("l"))
return nil
})
if err != nil {
log.Panic(err)
}
newBlock := NewBlock(data, lastHash)
err = bc.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(blocksBucket))
err := b.Put(newBlock.Hash, newBlock.Serialize())
if err != nil {
log.Panic(err)
}
err = b.Put([]byte("l"), newBlock.Hash)
if err != nil {
log.Panic(err)
}
bc.tip = newBlock.Hash
return nil
})
if err != nil {
log.Panic(err)
}
}
func NewGenesisBlock() *Block {
return NewBlock("Genesis Block", []byte{})
}
func NewBlockchain() *Blockchain {
var tip []byte
db, err := bolt.Open(dbFile, 0600, nil)
if err != nil {
log.Panic(err)
}
err = db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(blocksBucket))
if b == nil {
genesis := NewGenesisBlock()
b, err := tx.CreateBucket([]byte(blocksBucket))
if err != nil {
log.Panic(err)
}
err = b.Put(genesis.Hash, genesis.Serialize())
if err != nil {
log.Panic(err)
}
err = b.Put([]byte("l"), genesis.Hash)
if err != nil {
log.Panic(err)
}
tip = genesis.Hash
} else {
tip = b.Get([]byte("l"))
}
return nil
})
bc := Blockchain{tip, db}
return &bc
}
// blockchain.go in ex2.1~3
// package main
// type Blockchain struct {
// blocks []*Block
// }
// func (bc *Blockchain) AddBlock(data string) {
// //可能用到的函数:
// // len(array):获取数组长度
// // append(array,b):将元素b添加至数组array末尾
// if(len(bc.blocks) == 0){
// bc.blocks = append(bc.blocks, NewGenesisBlock())
// } else {
// bc.blocks = append(bc.blocks, NewBlock(data, bc.blocks[len(bc.blocks)-1].Hash))
// }
// }
// func NewGenesisBlock() *Block {
// //创世区块前置哈希为空,Data为"Genesis Block"
// return NewBlock("Genesis Block", []byte{})
// }
// func NewBlockchain() *Blockchain {
// return &Blockchain{[]*Block{NewGenesisBlock()}}
// }