-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconsensus.go
487 lines (406 loc) · 9.92 KB
/
consensus.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package ozcoin
import (
db "github.com/syndtr/goleveldb/leveldb"
"errors"
"log"
)
/*
* Initiates a recursive call to resolve the incoming block. The block is
* either added to the main chain, a side chain, the orphan database, or
* discard. Each effects of `AddOrOrphan` are made atomic by waiting for the a
* SIGNAL on startChan.
*/
func (c *Client) AddOrOrphan(req HashMsg, startChan chan struct{}, doneChan chan SHA256Sum) {
// Wait to make chain operations atomic
_ = <-startChan
// Resolve block
success, err := c.AddOrOrphanHelper(req)
if err != nil {
log.Println("AddOrOrphanHelper failed:", err)
}
log.Println("Block resolution successful?:", success)
// Broadcast transaction if successful
if success {
log.Println("Broadcasting block")
err := c.BcastBlock(req.Hash)
if err != nil {
log.Println(err)
}
}
// Signal completion
doneChan <- req.Hash
}
/*
* Primary consensus algorithm
*
* Recursively tries to adopt a given block by:
* 1. Load block
* 2. Extends main or side chain? Extend and done
* 3. Extends Orphan? AddOrOrphanHelper for PrevHash
* 4. If adoption failed? Save block and done
* 5. Now extends main or side chain? Extend and done
*/
func (c *Client) AddOrOrphanHelper(req HashMsg) (bool, error) {
// Load block or fetch block
block, err := c.LoadOrFetchBlock(req)
if err != nil {
return false, errors.New("Unable to load previous block")
}
header := block.Header
if !ValidHeader(header) {
return false, nil
}
if !c.PrevalidBlock(*block) {
return false, nil
}
prevHash := header.PrevHash
// Extending current chain
if prevHash == c.LastHeader.Hash() || (prevHash == SHA256Sum{}) {
return c.ExtendMainChain(header, block)
}
// Load parent block from main database
_, err = c.GetHeader(prevHash)
if err == nil {
return c.ExtendSideChain(header, block)
}
// Load previous header from sidechain database
_, err = c.GetSideHeader(prevHash)
if err == nil {
return c.ExtendSideChain(header, block)
}
// Recursively try to adopt orphan chain
newReq := req.NewHash(prevHash)
success, err := c.AddOrOrphanHelper(newReq)
if err != nil {
return false, err
}
// If adoption failed, cache header and block in orphan databases
if !success {
err = c.PutOrphanHeader(header)
if err != nil {
return false, err
}
if c.Type == BLOCKCHAIN_CLIENT {
err = c.PutOrphanBlock(*block)
if err != nil {
return false, err
}
}
return false, nil
}
// Load previous header from main database
_, err = c.GetHeader(prevHash)
if err == nil {
return c.ExtendMainChain(header, block)
}
// Load previous header from sidechain database
_, err = c.GetSideHeader(prevHash)
if err != nil {
return c.ExtendSideChain(header, block)
}
// Otherwise chain was not successfully adopted
panic("THIS SHOULD NEVER HAPPEN")
}
/*
* Adds the block to the main chain database.
*/
func (c *Client) ExtendMainChain(header BlockHeader, block *Block) (bool, error) {
if !c.PostValidBlock(*block, nil, nil) {
log.Println("Block is not post valid")
return false, nil
}
// Add to valid header database
err := c.PutHeader(header)
if err != nil {
return false, err
}
// Write block if blockchain
err = c.WriteBlock(*block)
if err != nil {
return false, err
}
if c.UpdateWallet {
b := *block
go func() {
err := c.Wallet.NewBlock(b)
if err != nil {
log.Println(err)
}
}()
}
c.LastHeader = header
return true, nil
}
/*
* Adds a block to an existing side chain. If this new chain is heavier than the
* main chain, the main chain is swapped for the side chain.
*/
func (c *Client) ExtendSideChain(header BlockHeader, block *Block) (bool, error) {
// Get fork paths
mainPath, sidePath, err := c.FindForkPaths(header.Hash())
if err != nil {
return false, err
}
// Get difficulties of main and side chains
mainDiff, err := c.MainChainTotalDifficulty()
if err != nil {
return false, err
}
sideDiff, err := c.SideChainTotalDifficulty(header.PrevHash)
if err != nil {
return false, err
}
sideDiff += header.Difficulty
// If sidechain is smaller, validate and save blocks
if mainDiff > sideDiff {
if !c.PostValidBlock(*block, mainPath, sidePath) {
return false, nil
}
err := c.PutSideHeader(header)
if err != nil {
return false, err
}
// Write sidechain block if blockchain
err = c.PutSideBlock(*block)
if err != nil {
return false, err
}
return true, nil
}
err = c.SwapMainFork(mainPath, sidePath)
if err != nil {
return false, err
}
c.LastHeader = header
return true, nil
}
/*
* Replaces the main chain with a side chain.
*/
func (c *Client) SwapMainFork(mainPath, sidePath []SHA256Sum) error {
headerBatch := &db.Batch{}
sideHeaderBatch := &db.Batch{}
// Remove main chain headers
for _, hash := range mainPath {
h, err := c.GetHeader(hash)
if err != nil {
return err
}
if h == nil {
log.Println("Header should be in main chain")
panic("Header should be in main chain")
}
headerBatch.Delete(hash[:])
sideHeaderBatch.Put(hash[:], h.Json())
}
// Add sidechain headers
for _, hash := range sidePath {
h, err := c.GetHeader(hash)
if err != nil {
return err
}
if h == nil {
log.Println("Header should be in side chain")
panic("Header should be in side chain")
}
headerBatch.Put(hash[:], h.Json())
sideHeaderBatch.Delete(hash[:])
}
// Commit batches
err := c.dbm.headerDB.Write(headerBatch, nil)
if err != nil {
return err
}
err = c.dbm.sideHeaderDB.Write(headerBatch, nil)
if err != nil {
return err
}
blockBatch := &db.Batch{}
sideBlockBatch := &db.Batch{}
pimgBatch := &db.Batch{}
txnPoolBatch := &db.Batch{}
// Remove main chain blocks
deleteBlocks := []Block{}
for _, hash := range mainPath {
// Retrieve block
b, err := c.GetBlock(hash)
if err != nil {
log.Println("Block should be in main chain:", err)
panic("Block should be in main chain")
}
deleteBlocks = append(deleteBlocks, *b)
blockBatch.Delete(hash.Bytes())
sideBlockBatch.Put(hash.Bytes(), b.Json())
// Add deletions to batch
for _, txn := range b.Txns {
pimgHash := Hash(txn.Sig.Preimage.Bytes()).Bytes()
pimgBatch.Delete(pimgHash)
// Only replace if blockchain client
if c.Type == BLOCKCHAIN_CLIENT {
txnPoolBatch.Put(pimgHash, txn.Json())
}
}
}
// Add side chain blocks
addBlocks := []Block{}
for _, hash := range sidePath {
// Retrieve block
b, err := c.GetBlock(hash)
if err != nil {
log.Println("Block should be in main chain:", err)
panic("Block should be in main chain")
}
addBlocks = append(addBlocks, *b)
blockBatch.Put(hash[:], b.Json())
sideBlockBatch.Delete(hash[:])
// Add deletions to batch
for _, txn := range b.Txns {
pimgHash := Hash(txn.Sig.Preimage.Bytes()).Bytes()
txnPoolBatch.Delete(pimgHash)
pimgBatch.Put(pimgHash, pimgHash)
}
}
log.Println("Writing preimages")
// Write preimages
err = c.dbm.pimgDB.Write(pimgBatch, nil)
if err != nil {
return err
}
log.Println("Writing txn pool")
err = c.dbm.txnPoolDB.Write(txnPoolBatch, nil)
if err != nil {
return err
}
// Commit blocks if blockchain client
if c.Type == BLOCKCHAIN_CLIENT {
err = c.dbm.blockDB.Write(blockBatch, nil)
if err != nil {
return err
}
err = c.dbm.sideBlockDB.Write(sideBlockBatch, nil)
if err != nil {
return err
}
}
// Teardown main fork maps
for _, hash := range mainPath {
block, err := c.FindBlock(hash)
if err != nil {
return err
}
err = c.DeleteMapToBlock(*block)
if err != nil {
return err
}
}
// Build side fork maps
for _, hash := range sidePath {
block, err := c.FindBlock(hash)
if err != nil {
return err
}
err = c.PutMapToBlock(*block)
if err != nil {
return err
}
}
if c.UpdateWallet {
go func() {
for _, b := range deleteBlocks {
err := c.Wallet.DeleteBlock(b)
if err != nil {
log.Println("Could not delete block:", err)
}
}
for _, b := range addBlocks {
err := c.Wallet.NewBlock(b)
if err != nil {
log.Println("Could not add block:", err)
}
}
}()
}
return nil
}
/*
* Computes the fork hash for where the side chain meets the main chain. Then
* returns a list of hashes in each fork after the fork hash.
*/
func (c *Client) FindForkPaths(sidehash SHA256Sum) ([]SHA256Sum, []SHA256Sum, error) {
prevHeader, err := c.GetSideHeader(sidehash)
if err != nil {
return nil, nil, err
}
prevHash := prevHeader.PrevHash
sideHashes := []SHA256Sum{prevHash}
for {
prevHeader, err = c.GetHeader(prevHash)
if err != nil {
return nil, nil, err
}
if prevHeader != nil {
sideHashes = append(sideHashes, prevHash)
break
}
prevHeader, err = c.GetSideHeader(prevHash)
if err != nil {
return nil, nil, err
}
if prevHeader == nil {
return nil, nil, nil
}
sideHashes = append(sideHashes, prevHash)
}
forkHash := sideHashes[len(sideHashes)-1]
prevHash = c.LastHeader.Hash()
mainHashes := []SHA256Sum{}
for {
prevHeader, err = c.GetHeader(prevHash)
if err != nil {
return nil, nil, err
}
if prevHeader == nil {
return nil, nil, nil
}
if prevHash == forkHash {
return mainHashes, sideHashes, nil
}
mainHashes = append(mainHashes, prevHash)
}
}
/*
* Wraps the AddToTxnPoolHelper to be atomic and broadcast on success.
*/
func (c *Client) AddToTxnPool(req HashMsg, startChan chan struct{}, doneChan chan SHA256Sum) {
// Wait to make chain operations atomic
_ = <-startChan
success, err := c.AddToTxnPoolHelper(req)
if err != nil {
log.Println(err)
}
if success {
err = c.BcastTxn(req.Hash)
if err != nil {
log.Println(err)
}
}
// Signal completion
doneChan <- req.Hash
}
/*
* Checks that a txn is Valid and then adds it to Txn Pool.
*/
func (c *Client) AddToTxnPoolHelper(req HashMsg) (bool, error) {
txn, err := c.LoadOrFetchTxn(req)
if err != nil {
return false, err
}
if !ValidTxn(*txn) && !ValidCoinbaseTxn(*txn) {
return false, errors.New("Invalid txn")
}
err = c.PutTxnPool(*txn)
if err != nil {
return false, err
}
return true, nil
}