-
Notifications
You must be signed in to change notification settings - Fork 27
/
blockchain.go
488 lines (444 loc) · 14.1 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
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
488
// Copyright (C) 2015-2016 The Lightning Network Developers
// Copyright (c) 2016-2017 The OpenBazaar Developers
package bitcoincash
import (
"errors"
"fmt"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"math/big"
"sort"
"sync"
"time"
)
// Blockchain settings. These are kindof Bitcoin specific, but not contained in
// chaincfg.Params so they'll go here. If you're into the [ANN]altcoin scene,
// you may want to paramaterize these constants.
const (
targetSpacing = 600
medianTimeBlocks = 11
)
var OrphanHeaderError = errors.New("header does not extend any known headers")
// Wrapper around Headers implementation that handles all blockchain operations
type Blockchain struct {
lock *sync.Mutex
params *chaincfg.Params
db Headers
crationDate time.Time
checkpoint Checkpoint
}
func NewBlockchain(filePath string, walletCreationDate time.Time, params *chaincfg.Params) (*Blockchain, error) {
hdb, err := NewHeaderDB(filePath)
if err != nil {
return nil, err
}
b := &Blockchain{
lock: new(sync.Mutex),
params: params,
db: hdb,
crationDate: walletCreationDate,
}
b.checkpoint = GetCheckpoint(walletCreationDate, params)
h, err := b.db.Height()
if h == 0 || err != nil {
log.Info("Initializing headers db with checkpoints")
// Put the checkpoint to the db
sh := StoredHeader{
header: b.checkpoint.Header,
height: b.checkpoint.Height,
totalWork: big.NewInt(0),
}
err := b.db.Put(sh, true)
if err != nil {
return nil, err
}
}
return b, nil
}
func (b *Blockchain) CommitHeader(header wire.BlockHeader) (bool, *StoredHeader, uint32, error) {
b.lock.Lock()
defer b.lock.Unlock()
newTip := false
var commonAncestor *StoredHeader
// Fetch our current best header from the db
bestHeader, err := b.db.GetBestHeader()
if err != nil {
return false, nil, 0, err
}
tipHash := bestHeader.header.BlockHash()
var parentHeader StoredHeader
// If the tip is also the parent of this header, then we can save a database read by skipping
// the lookup of the parent header. Otherwise (ophan?) we need to fetch the parent.
if header.PrevBlock.IsEqual(&tipHash) {
parentHeader = bestHeader
} else {
parentHeader, err = b.db.GetPreviousHeader(header)
if err != nil {
return false, nil, 0, fmt.Errorf("Header %s does not extend any known headers", header.BlockHash().String())
}
}
valid := b.CheckHeader(header, parentHeader)
if !valid {
return false, nil, 0, nil
}
// If this block is already the tip, return
headerHash := header.BlockHash()
if tipHash.IsEqual(&headerHash) {
return newTip, nil, 0, nil
}
// Add the work of this header to the total work stored at the previous header
cumulativeWork := new(big.Int).Add(parentHeader.totalWork, blockchain.CalcWork(header.Bits))
// If the cumulative work is greater than the total work of our best header
// then we have a new best header. Update the chain tip and check for a reorg.
if cumulativeWork.Cmp(bestHeader.totalWork) == 1 {
newTip = true
prevHash := parentHeader.header.BlockHash()
// If this header is not extending the previous best header then we have a reorg.
if !tipHash.IsEqual(&prevHash) {
commonAncestor, err = b.GetCommonAncestor(StoredHeader{header: header, height: parentHeader.height + 1}, bestHeader)
if err != nil {
log.Errorf("Error calculating common ancestor: %s", err.Error())
return newTip, commonAncestor, 0, err
}
log.Warningf("REORG!!! REORG!!! REORG!!! At block %d, Wiped out %d blocks", int(bestHeader.height), int(bestHeader.height-commonAncestor.height))
}
}
newHeight := parentHeader.height + 1
// Put the header to the database
err = b.db.Put(StoredHeader{
header: header,
height: newHeight,
totalWork: cumulativeWork,
}, newTip)
if err != nil {
return newTip, commonAncestor, 0, err
}
return newTip, commonAncestor, newHeight, nil
}
func (b *Blockchain) CheckHeader(header wire.BlockHeader, prevHeader StoredHeader) bool {
height := prevHeader.height
// Due to the rolling difficulty period our checkpoint block consists of a block and a hash of a block 146 blocks later
// During this period we can skip the validity checks as long as block checkpoint + 146 matches the hardcoded hash.
if height+1 <= b.checkpoint.Height+147 {
h := header.BlockHash()
if b.checkpoint.Check2 != nil && height+1 == b.checkpoint.Height+147 && !b.checkpoint.Check2.IsEqual(&h) {
return false
}
return true
}
// Get hash of n-1 header
prevHash := prevHeader.header.BlockHash()
// Check if headers link together. That whole 'blockchain' thing.
if prevHash.IsEqual(&header.PrevBlock) == false {
log.Errorf("Headers %d and %d don't link.\n", height, height+1)
return false
}
// Check the header meets the difficulty requirement
if b.params.Name != chaincfg.RegressionNetParams.Name { // Don't need to check difficulty on regtest
diffTarget, err := b.calcRequiredWork(header, int32(height+1), prevHeader)
if err != nil {
log.Errorf("Error calclating difficulty", err)
return false
}
if header.Bits != diffTarget && b.params.Name == chaincfg.MainNetParams.Name {
log.Warningf("Block %d %s incorrect difficulty. Read %d, expect %d\n",
height+1, header.BlockHash().String(), header.Bits, diffTarget)
return false
} else if diffTarget == b.params.PowLimitBits && header.Bits > diffTarget && b.params.Name == chaincfg.TestNet3Params.Name {
log.Warningf("Block %d %s incorrect difficulty. Read %d, expect %d\n",
height+1, header.BlockHash().String(), header.Bits, diffTarget)
return false
}
}
// Check if there's a valid proof of work. That whole "Bitcoin" thing.
if !checkProofOfWork(header, b.params) {
log.Debugf("Block %d bad proof of work.\n", height+1)
return false
}
return true // it must have worked if there's no errors and got to the end.
}
// Get the PoW target this block should meet. We may need to handle a difficulty adjustment
// or testnet difficulty rules.
func (b *Blockchain) calcRequiredWork(header wire.BlockHeader, height int32, prevHeader StoredHeader) (uint32, error) {
// Special difficulty rule for testnet
if b.params.ReduceMinDifficulty && header.Timestamp.After(prevHeader.header.Timestamp.Add(targetSpacing*2)) {
return b.params.PowLimitBits, nil
}
suitableHeader, err := b.GetSuitableBlock(prevHeader)
if err != nil {
log.Error(err)
return 0, err
}
epoch, err := b.GetEpoch(prevHeader.header)
if err != nil {
log.Error(err)
return 0, err
}
return calcDiffAdjust(epoch, suitableHeader, b.params), nil
}
func (b *Blockchain) CalcMedianTimePast(header wire.BlockHeader) (time.Time, error) {
timestamps := make([]int64, medianTimeBlocks)
numNodes := 0
iterNode := StoredHeader{header: header}
var err error
for i := 0; i < medianTimeBlocks; i++ {
numNodes++
timestamps[i] = iterNode.header.Timestamp.Unix()
iterNode, err = b.db.GetPreviousHeader(iterNode.header)
if err != nil {
return time.Time{}, err
}
}
timestamps = timestamps[:numNodes]
sort.Sort(timeSorter(timestamps))
medianTimestamp := timestamps[numNodes/2]
return time.Unix(medianTimestamp, 0), nil
}
// Rollsback and grabs block n-144, n-145, and n-146, sorts them by timestamps and returns the middle header.
func (b *Blockchain) GetEpoch(hdr wire.BlockHeader) (StoredHeader, error) {
sh := StoredHeader{header: hdr}
var err error
for i := 0; i < 144; i++ {
sh, err = b.db.GetPreviousHeader(sh.header)
if err != nil {
return sh, err
}
}
oneFourtyFour := sh
sh, err = b.db.GetPreviousHeader(oneFourtyFour.header)
if err != nil {
return sh, err
}
oneFourtyFive := sh
sh, err = b.db.GetPreviousHeader(oneFourtyFive.header)
if err != nil {
return sh, err
}
oneFourtySix := sh
headers := []StoredHeader{oneFourtyFour, oneFourtyFive, oneFourtySix}
sort.Sort(blockSorter(headers))
return headers[1], nil
}
// Rollsback grabs the last two headers before this one. Sorts the three and returns the mid.
func (b *Blockchain) GetSuitableBlock(hdr StoredHeader) (StoredHeader, error) {
n := hdr
sh, err := b.db.GetPreviousHeader(hdr.header)
if err != nil {
return sh, err
}
n1 := sh
sh, err = b.db.GetPreviousHeader(n1.header)
if err != nil {
return sh, err
}
n2 := sh
headers := []StoredHeader{n, n1, n2}
sort.Sort(blockSorter(headers))
return headers[1], nil
}
func (b *Blockchain) GetNPrevBlockHashes(n int) []*chainhash.Hash {
var ret []*chainhash.Hash
hdr, err := b.db.GetBestHeader()
if err != nil {
return ret
}
tipSha := hdr.header.BlockHash()
ret = append(ret, &tipSha)
for i := 0; i < n-1; i++ {
hdr, err = b.db.GetPreviousHeader(hdr.header)
if err != nil {
return ret
}
shaHash := hdr.header.BlockHash()
ret = append(ret, &shaHash)
}
return ret
}
func (b *Blockchain) GetBlockLocator() blockchain.BlockLocator {
var ret []*chainhash.Hash
parent, err := b.db.GetBestHeader()
if err != nil {
return ret
}
rollback := func(parent StoredHeader, n int) (StoredHeader, error) {
for i := 0; i < n; i++ {
parent, err = b.db.GetPreviousHeader(parent.header)
if err != nil {
return parent, err
}
}
return parent, nil
}
step := 1
start := 0
for {
if start >= 9 {
step *= 2
start = 0
}
hash := parent.header.BlockHash()
ret = append(ret, &hash)
if len(ret) == 500 {
break
}
parent, err = rollback(parent, step)
if err != nil {
break
}
start += 1
}
return blockchain.BlockLocator(ret)
}
// Returns last header before reorg point
func (b *Blockchain) GetCommonAncestor(bestHeader, prevBestHeader StoredHeader) (*StoredHeader, error) {
var err error
rollback := func(parent StoredHeader, n int) (StoredHeader, error) {
for i := 0; i < n; i++ {
parent, err = b.db.GetPreviousHeader(parent.header)
if err != nil {
return parent, err
}
}
return parent, nil
}
majority := bestHeader
minority := prevBestHeader
if bestHeader.height > prevBestHeader.height {
majority, err = rollback(majority, int(bestHeader.height-prevBestHeader.height))
if err != nil {
return nil, err
}
} else if prevBestHeader.height > bestHeader.height {
minority, err = rollback(minority, int(prevBestHeader.height-bestHeader.height))
if err != nil {
return nil, err
}
}
for {
majorityHash := majority.header.BlockHash()
minorityHash := minority.header.BlockHash()
if majorityHash.IsEqual(&minorityHash) {
return &majority, nil
}
majority, err = b.db.GetPreviousHeader(majority.header)
if err != nil {
return nil, err
}
minority, err = b.db.GetPreviousHeader(minority.header)
if err != nil {
return nil, err
}
}
}
// Rollback the header database to the last header before time t.
// We shouldn't go back further than the checkpoint
func (b *Blockchain) Rollback(t time.Time) error {
b.lock.Lock()
defer b.lock.Unlock()
checkpoint := GetCheckpoint(b.crationDate, b.params)
checkPointHash := checkpoint.Header.BlockHash()
sh, err := b.db.GetBestHeader()
if err != nil {
return err
}
// If t is greater than the timestamp at the tip then do nothing
if sh.header.Timestamp.Before(t) {
return nil
}
// If the tip is our checkpoint then do nothing
checkHash := sh.header.BlockHash()
if checkHash.IsEqual(&checkPointHash) {
return nil
}
rollbackHeight := uint32(0)
for i := 0; i < 1000000000; i++ {
sh, err = b.db.GetPreviousHeader(sh.header)
if err != nil {
return err
}
checkHash := sh.header.BlockHash()
// If we rolled back to the checkpoint then stop here and set the checkpoint as the tip
if checkHash.IsEqual(&checkPointHash) {
rollbackHeight = checkpoint.Height
break
}
// If we hit a header created before t then stop here and set this header as the tip
if sh.header.Timestamp.Before(t) {
rollbackHeight = sh.height
break
}
}
err = b.db.DeleteAfter(rollbackHeight)
if err != nil {
return err
}
return b.db.Put(sh, true)
}
func (b *Blockchain) BestBlock() (StoredHeader, error) {
sh, err := b.db.GetBestHeader()
if err != nil {
return StoredHeader{}, err
}
return sh, nil
}
func (b *Blockchain) GetHeader(hash *chainhash.Hash) (StoredHeader, error) {
sh, err := b.db.GetHeader(*hash)
if err != nil {
return sh, err
}
return sh, nil
}
func (b *Blockchain) Close() {
b.lock.Lock()
b.db.Close()
}
// Verifies the header hashes into something lower than specified by the 4-byte bits field.
func checkProofOfWork(header wire.BlockHeader, p *chaincfg.Params) bool {
target := blockchain.CompactToBig(header.Bits)
// The target must more than 0. Why can you even encode negative...
if target.Sign() <= 0 {
log.Debugf("Block target %064x is neagtive(??)\n", target.Bytes())
return false
}
// The target must be less than the maximum allowed (difficulty 1)
if target.Cmp(p.PowLimit) > 0 {
log.Debugf("Block target %064x is "+
"higher than max of %064x", target, p.PowLimit.Bytes())
return false
}
// The header hash must be less than the claimed target in the header.
blockHash := header.BlockHash()
hashNum := blockchain.HashToBig(&blockHash)
if hashNum.Cmp(target) > 0 {
log.Debugf("Block hash %064x is higher than "+
"required target of %064x", hashNum, target)
return false
}
return true
}
// This function takes in a start and end block header and uses the timestamps in each
// to calculate how much of a difficulty adjustment is needed. It returns a new compact
// difficulty target.
func calcDiffAdjust(start, end StoredHeader, p *chaincfg.Params) uint32 {
work := new(big.Int).Sub(end.totalWork, start.totalWork)
// In order to avoid difficulty cliffs, we bound the amplitude of the
// adjustement we are going to do.
duration := end.header.Timestamp.Unix() - start.header.Timestamp.Unix()
if duration > 288*int64(targetSpacing) {
duration = 288 * int64(targetSpacing)
} else if duration < 72*int64(targetSpacing) {
duration = 72 * int64(targetSpacing)
}
projectedWork := new(big.Int).Mul(work, big.NewInt(int64(targetSpacing)))
pw := new(big.Int).Div(projectedWork, big.NewInt(duration))
e := new(big.Int).Exp(big.NewInt(2), big.NewInt(256), nil)
nt := new(big.Int).Sub(e, pw)
newTarget := new(big.Int).Div(nt, pw)
// clip again if above minimum target (too easy)
if newTarget.Cmp(p.PowLimit) > 0 {
newTarget.Set(p.PowLimit)
}
return blockchain.BigToCompact(newTarget)
}