-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockchain.go
254 lines (226 loc) · 6.54 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package main
import (
"bytes"
"code.google.com/p/gocask"
"encoding/gob"
"fmt"
"github.com/conformal/btcwire"
"log"
"strings"
)
type BlockChain struct {
Last btcwire.ShaHash
Genesis btcwire.ShaHash
ChainHead btcwire.ShaHash
ChainHeadDepth int
Database *gocask.Gocask
}
type BlockHandle struct {
Hash btcwire.ShaHash
Header btcwire.BlockHeader
Depth int
// We can't know the size of a block apriori
// so lets store a key to lookup the block when we
// need it
Block string
}
// return the block headers identifier hash
func (bh *BlockHandle) BlockSha() (btcwire.ShaHash, error) {
return bh.Header.BlockSha(btcwire.ProtocolVersion)
}
// Save block writes a block to the db
func (chain *BlockChain) Save(header *btcwire.BlockHeader) (*BlockHandle, error) {
parent, err := chain.Get(&header.PrevBlock)
if err != nil {
log.Fatal("No parent found for: %#v", header)
return nil, err
}
// save a string to store our 'real' block with
block_sha, err := header.BlockSha(btcwire.ProtocolVersion)
if err != nil {
log.Fatal("Something went wrong deriving the block hash for: %#v", header)
return nil, err
}
bls_elems := []string{"blk", block_sha.String()}
block_lookup_string := strings.Join(bls_elems, "")
block := BlockHandle{Hash: block_sha,
Header: *header,
Depth: parent.Depth + 1,
Block: block_lookup_string}
var block_bytes bytes.Buffer
encoder := gob.NewEncoder(&block_bytes)
encoder.Encode(block)
err = chain.Database.Put(block_sha.String(), block_bytes.Bytes())
return &block, err
}
// fetch our handle to block through the databse
func (chain *BlockChain) Get(hash *btcwire.ShaHash) (*BlockHandle, error) {
block_bytes, err := chain.Database.Get(hash.String())
if err != nil {
err := fmt.Errorf("No Block with Hash: %s", hash.String())
return nil, err
}
block_reader := bytes.NewBuffer(block_bytes)
decoder := gob.NewDecoder(block_reader)
var block BlockHandle
err = decoder.Decode(&block)
if err != nil {
log.Fatal("Error decoding block:", err)
}
return &block, nil
}
// find our blocks parent in the DB
func (chain *BlockChain) FindParent(bh *BlockHandle) (*BlockHandle, error) {
parent_hash := bh.Header.PrevBlock
parent, err := chain.Get(&parent_hash)
if err != nil {
return nil, err
}
return parent, nil
}
func InitializeBlockChain() (*BlockChain, error) {
gob.Register(btcwire.ShaHash{})
db, err := gocask.NewGocask("bitcoin")
chain_bytes, err := db.Get("chainhead")
if err == nil {
var bc BlockChain
bc.Database = db
head_hash, err := btcwire.NewShaHash(chain_bytes)
if err != nil {
return nil, err
}
bc.Last = *head_hash
bc.ChainHead = *head_hash
chainhead_handle, err := bc.Get(head_hash)
if err != nil {
log.Fatal("Couldn't derive Chain Head")
return nil, nil
}
bc.ChainHeadDepth = chainhead_handle.Depth
bc.Genesis = btcwire.GenesisHash
fmt.Printf("Loaded previous blockchain with a depth of: %d\n", bc.ChainHeadDepth)
return &bc, nil
} else {
bc := BlockChain{Last: btcwire.GenesisHash, ChainHead: btcwire.GenesisHash, Genesis: btcwire.GenesisHash}
err = db.Put("chainhead", btcwire.GenesisHash.Bytes())
if err != nil {
return nil, err
}
bc.Database = db
// save a string to store our 'real' block with
block_sha, err := btcwire.GenesisBlock.Header.BlockSha(btcwire.ProtocolVersion)
if err != nil {
log.Fatal("Something went wrong deriving the genesis block hash")
return nil, err
}
bls_elems := []string{"blk", block_sha.String()}
block_lookup_string := strings.Join(bls_elems, "")
block := BlockHandle{Hash: block_sha,
Header: btcwire.GenesisBlock.Header,
Depth: 0,
Block: block_lookup_string}
var block_bytes bytes.Buffer
encoder := gob.NewEncoder(&block_bytes)
encoder.Encode(block)
err = db.Put(block_sha.String(), block_bytes.Bytes())
if err != nil {
log.Fatal("Couldn't save Genesis Block Header")
return nil, err
}
// genesis := BlockHandle{Hash: btcwire.GenesisHash, Header: btcwire.GenesisBlock.Header}
return &bc, nil
}
return nil, nil
}
// create a block from a header we've read and add it to our blockchain
func (chain *BlockChain) InitializeBlock(header *btcwire.BlockHeader) (*BlockHandle, error) {
block, err := chain.Save(header)
if err != nil {
err := fmt.Errorf("Error saving block: %s", err)
return nil, err
}
// add a reference to our current chain head
chain_head_bytes, err := chain.Database.Get("chainhead")
if err != nil {
return nil, err
}
chain_head_hash, err := btcwire.NewShaHash(chain_head_bytes)
if err != nil {
return nil, err
}
chain_head, err := chain.Get(chain_head_hash)
if err != nil {
return nil, err
}
if chain_head.Depth < block.Depth {
chain_head_sha, _ := block.BlockSha()
chain.ChainHead = chain_head_sha
// we should defer this to only write when we're about to exit because of the
// append only nature of bitcask
chain.Database.Put("chainhead", chain_head_sha.Bytes())
chain.ChainHeadDepth = block.Depth
}
return block, nil
}
func (chain *BlockChain) LongestPath() []*BlockHandle {
var (
parent *BlockHandle
err error
)
head, err := chain.Get(&chain.ChainHead)
if err != nil {
log.Fatal("No chainhead!")
}
parent, err = chain.FindParent(head)
if err != nil {
log.Fatal("No parent to root, something's fucky: %#v", head)
return nil
}
var blocks []*BlockHandle
blocks = append(blocks, head)
for {
blocks = append(blocks, parent)
parent, err = chain.FindParent(parent)
if err != nil {
break // we're at the top
}
}
return blocks
}
// create a block chain locator sequence to fetch our items
func (chain *BlockChain) CreateLocator() []*btcwire.ShaHash {
longest_path := chain.LongestPath()
var locator []*btcwire.ShaHash
// A valid locator contains the last ten locator hashes
for i, block := range longest_path {
if i < 10 {
block_sha, err := block.BlockSha()
if err != nil {
log.Fatal("Error building Block Hash: %#v", block)
}
locator = append(locator, &block_sha)
} else {
break
}
}
if len(locator) < 10 {
return locator
}
// as well as the hashes at position 10 + step ^ 2
// incrementing step linearly, e.g 11, 14, 18, 26
step := 1
longest_path = longest_path[9:]
for {
if step >= len(longest_path) {
break
}
block := longest_path[step]
block_sha, err := block.BlockSha()
if err != nil {
log.Fatal("Error building Block Hash: %#v", block)
}
locator = append(locator, &block_sha)
step = step * 2
}
return locator
}