-
Notifications
You must be signed in to change notification settings - Fork 5
/
block.go
406 lines (344 loc) · 7.47 KB
/
block.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package ozcoin
import (
db "github.com/syndtr/goleveldb/leveldb"
"encoding/json"
"errors"
"log"
"time"
)
/*
* BlockHeader
*
* Records information for properly sequencing and verifying blocks.
*/
type BlockHeader struct {
SeqNum uint64 `json:"seq_num"`
PrevHash SHA256Sum `json:"prev_hash"`
MerkleRoot SHA256Sum `json:"merkle_root"`
Time time.Time `json:"time"`
Difficulty uint64 `json:"difficulty"`
Nonce uint64 `json:"nonce:"`
}
/*
* Checks PoW, Time, and Genesis Hash.
*/
func ValidHeader(header BlockHeader) bool {
if !header.ValidPoW() {
return false
}
if header.Time.Add(2 * time.Hour).Before(time.Now()) {
return false
}
if (header.SeqNum == 0) && (header.PrevHash != SHA256Sum{}) {
return false
}
return true
}
/*
* Checks that a block header's hash is lower than the claimed difficulty.
*/
func (bh BlockHeader) ValidPoW() bool {
difficulty := bh.Difficulty
zeroBytes := difficulty / 8
bitOffset := difficulty % 8
h := bh.Hash()
for i, c := range h {
if uint64(i) < zeroBytes {
if c != 0 {
return false
}
} else {
break
}
}
c := h[zeroBytes]
for j := 0; j < 8; j++ {
if uint64(j) < bitOffset {
if (c >> (7 - uint64(j)) & 1) != 0 {
return false
}
} else {
break
}
}
return true
}
/*
* Block
*
* Stores a block header along with a list of txns.
*/
type Block struct {
Header BlockHeader `json:"header"`
Txns []Txn `json:"txns"`
}
/*
* The blocks's json bytes.
*/
func (b Block) Json() []byte {
blockJson, err := json.Marshal(b)
if err != nil {
log.Println(err)
panic("Unable to marshal block")
}
return blockJson
}
/*
* Creates a new block to mined that extends the current `LastHeader`. The
* coinbase txn is sent to the address provided.
*/
func (c *Client) NewBlock(prev BlockHeader, address WalletPublicKey) Block {
seqNum := prev.SeqNum + 1
// Gather txns and add fees
txns := c.TxnsFromPool()
fees := uint64(0)
for _, t := range txns {
fees += t.Body.Fee
}
// Create new coinbase commitment
coinbaseTxn := NewCoinbaseTxn(address, seqNum, fees)
block := Block{
Header: BlockHeader{
SeqNum: seqNum,
PrevHash: prev.Hash(),
MerkleRoot: SHA256Sum{},
Time: time.Now(),
Difficulty: INITIAL_DIFFICULTY,
Nonce: 0,
},
Txns: []Txn{
coinbaseTxn,
},
}
block.Txns = append(block.Txns, txns...)
block.Header.MerkleRoot = block.MerkleHash()
block.Header.Difficulty = c.ComputeDifficulty(block)
return block
}
/*
* Mines the Genesis block and sends the coinbase to `address`.
*/
func GenesisBlock(address WalletPublicKey) Block {
coinbaseTxn := NewCoinbaseTxn(address, 0, 0)
b := Block{
Header: BlockHeader{
SeqNum: 0,
PrevHash: SHA256Sum{},
MerkleRoot: SHA256Sum{},
Time: time.Now(),
Difficulty: INITIAL_DIFFICULTY,
Nonce: 0,
},
Txns: []Txn{
coinbaseTxn,
},
}
b.Header.MerkleRoot = b.MerkleHash()
for !b.Header.ValidPoW() {
b.Header.Nonce += 1
}
return b
}
/*
* Less intensive validations.
*/
func (c *Client) PrevalidBlock(b Block) bool {
if b.Txns == nil || len(b.Txns) == 0 {
log.Println("Txns nil or len 0")
return false
}
if !c.ValidTxns(b) {
log.Println("Invalid txns")
return false
}
if !b.VerifyMerkleHash() {
log.Println("Merkle hash failed")
return false
}
return true
}
/*
* More intensive validations.
*/
func (c *Client) PostValidBlock(b Block, mainPath, sidePath []SHA256Sum) bool {
if !c.ValidDifficulty(b) {
return false
}
// Check median of last 11 blocks
if !c.VerifyTxns(b, mainPath, sidePath) {
return false
}
return true
}
/*
* Computes merkle hash and compares with block header.
*/
func (b Block) VerifyMerkleHash() bool {
existing := b.Header.MerkleRoot
computed := b.MerkleHash()
return computed == existing
}
/*
* Computes merkle hash of txns
*/
func (b Block) MerkleHash() SHA256Sum {
// Calculate number of initial slots
numSlots := 1
for numSlots < len(b.Txns) {
numSlots *= 2
}
// Fill in slots
slots := make([]SHA256Sum, numSlots)
for i := 0; i < len(slots); i++ {
if i < len(b.Txns) {
slots[i] = Hash(b.Txns[i].Json())
} else {
slots[i] = SHA256Sum{}
}
}
// Combine and reduce
for len(slots) > 1 {
numNewSlots := len(slots) / 2
newSlots := make([]SHA256Sum, numNewSlots)
for i := 0; i < numNewSlots; i++ {
bytes := []byte{}
bytes = append(bytes, slots[2*i][:]...)
bytes = append(bytes, slots[2*i+1][:]...)
newSlots[i] = Hash(bytes)
}
slots = slots[:numNewSlots]
}
return slots[0]
}
/*
* Writes block depending on client type
*/
func (c *Client) WriteBlock(b Block) error {
if c.Type == BLOCKCHAIN_CLIENT {
log.Println("Writing block")
err := c.PutBlock(b)
if err != nil {
log.Println(err)
return err
}
}
// Build batched writes
pimgBatch := &db.Batch{}
txnPoolBatch := &db.Batch{}
for i, txn := range b.Txns {
// Only preimages for non-coinbase txns
if i != 0 {
pimgHash := Hash(txn.Sig.Preimage.Bytes()).Bytes()
pimgBatch.Put(pimgHash, pimgHash)
log.Println("Deleteing pimg from txn pool")
txnPoolBatch.Delete(pimgHash)
}
}
// Write preimages
err := c.dbm.pimgDB.Write(pimgBatch, nil)
if err != nil {
log.Println(err)
return err
}
// Write txns
err = c.PutMapToBlock(b)
if err != nil {
log.Println(err)
return err
}
log.Println("Writing to txn pool batch")
// Write txn pool
err = c.dbm.txnPoolDB.Write(txnPoolBatch, nil)
if err != nil {
log.Println(err)
return err
}
log.Println("Write block success")
return nil
}
/*
* The json bytes for the block header.
*/
func (b BlockHeader) Json() []byte {
headerJson, err := json.Marshal(b)
if err != nil {
log.Println(err)
panic("Unable to marshal block header")
}
return headerJson
}
/*
* The hash to end all hashes.
*/
func (b BlockHeader) Hash() SHA256Sum {
return Hash(b.Json())
}
/*
* Tries to load a block from local storage. If this fails, the block is fetched
* from `req.Address`.
*/
func (c *Client) LoadOrFetchBlock(req HashMsg) (*Block, error) {
log.Println("Requesting block from:", req.Address)
// If this failed, request block
block, err := c.LoadBlock(req.Hash)
if err != nil {
block, err = c.FetchBlock(req.Hash, req.Address)
if err != nil {
log.Println("FETCH BLOCK FAILED:", err)
return nil, err
}
}
if block == nil {
return nil, errors.New("Unable to load block")
}
log.Println("LOADED BLOCK:", *block)
return block, nil
}
/*
* Tries to load a block from local storage.
*/
func (c *Client) LoadBlock(hash SHA256Sum) (*Block, error) {
if c.Type != BLOCKCHAIN_CLIENT {
return nil, errors.New("Not BLOCKCHAIN_CLIENT")
}
block, err := c.GetBlock(hash)
if err != nil {
block, err = c.GetSideBlock(hash)
if err != nil {
block, err = c.GetOrphanBlock(hash)
}
}
return block, err
}
/*
* Tries to load a block from local storage, otherwise iterates through peers
* until one succeeds.
*/
func (c *Client) FindBlock(hash SHA256Sum) (*Block, error) {
block, err := c.LoadBlock(hash)
if err == nil {
return block, nil
}
iter := c.dbm.peerDB.NewIterator(nil, nil)
for iter.Next() {
address := string(iter.Key())
block, err := c.FetchBlock(hash, address)
if err == nil {
return block, nil
}
}
iter.Release()
return nil, iter.Error()
}
/*
* Sends block to all peers.
*/
func (c *Client) BcastBlock(hash SHA256Sum) error {
iter := c.dbm.peerDB.NewIterator(nil, nil)
for iter.Next() {
address := string(iter.Key())
go c.sendBcast("GossipCore.BcastBlockRPC", address, hash)
}
iter.Release()
return iter.Error()
}