-
Notifications
You must be signed in to change notification settings - Fork 80
/
blockchain.go
464 lines (427 loc) · 13.5 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
// Copyright (C) 2015-2016 The Lightning Network Developers
// Copyright (c) 2016-2017 The OpenBazaar Developers
package spvwallet
import (
"errors"
"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 (
targetTimespan = time.Hour * 24 * 14
targetSpacing = time.Minute * 10
epochLength = int32(targetTimespan / targetSpacing) // 2016
maxDiffAdjust = 4
minRetargetTimespan = int64(targetTimespan / maxDiffAdjust)
maxRetargetTimespan = int64(targetTimespan * maxDiffAdjust)
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
}
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,
}
h, err := b.db.Height()
if h == 0 || err != nil {
log.Info("Initializing headers db with checkpoints")
checkpoint := GetCheckpoint(walletCreationDate, params)
// Put the checkpoint to the db
sh := StoredHeader{
header: checkpoint.Header,
height: 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, OrphanHeaderError
}
}
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 {
// Get hash of n-1 header
prevHash := prevHeader.header.BlockHash()
height := prevHeader.height
// 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.ReduceMinDifficulty {
diffTarget, err := b.calcRequiredWork(header, int32(height+1), prevHeader)
if err != nil {
log.Errorf("Error calclating difficulty", err)
return false
}
if header.Bits != diffTarget {
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) {
// If this is not a difficulty adjustment period
if height%epochLength != 0 {
// If we are on testnet
if b.params.ReduceMinDifficulty {
// If it's been more than 20 minutes since the last header return the minimum difficulty
if header.Timestamp.After(prevHeader.header.Timestamp.Add(targetSpacing * 2)) {
return b.params.PowLimitBits, nil
} else { // Otherwise return the difficulty of the last block not using special difficulty rules
for {
var err error = nil
for err == nil && int32(prevHeader.height)%epochLength != 0 && prevHeader.header.Bits == b.params.PowLimitBits {
var sh StoredHeader
sh, err = b.db.GetPreviousHeader(prevHeader.header)
// Error should only be non-nil if prevHeader is the checkpoint.
// In that case we should just return checkpoint bits
if err == nil {
prevHeader = sh
}
}
return prevHeader.header.Bits, nil
}
}
}
// Just return the bits from the last header
return prevHeader.header.Bits, nil
}
// We are on a difficulty adjustment period so we need to correctly calculate the new difficulty.
epoch, err := b.GetEpoch()
if err != nil {
log.Error(err.Error())
return 0, err
}
return calcDiffAdjust(*epoch, prevHeader.header, 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
}
func (b *Blockchain) GetEpoch() (*wire.BlockHeader, error) {
sh, err := b.db.GetBestHeader()
if err != nil {
return &sh.header, err
}
for i := 0; i < 2015; i++ {
sh, err = b.db.GetPreviousHeader(sh.header)
if err != nil {
return &sh.header, err
}
}
log.Debug("Epoch", sh.header.BlockHash().String())
return &sh.header, 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)
}
// GetCommonAncestor 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 wire.BlockHeader, p *chaincfg.Params) uint32 {
duration := end.Timestamp.UnixNano() - start.Timestamp.UnixNano()
if duration < minRetargetTimespan {
log.Debugf("Whoa there, block %s off-scale high 4X diff adjustment!",
end.BlockHash().String())
duration = minRetargetTimespan
} else if duration > maxRetargetTimespan {
log.Debugf("Uh-oh! block %s off-scale low 0.25X diff adjustment!\n",
end.BlockHash().String())
duration = maxRetargetTimespan
}
// calculation of new 32-byte difficulty target
// first turn the previous target into a big int
prevTarget := blockchain.CompactToBig(end.Bits)
// new target is old * duration...
newTarget := new(big.Int).Mul(prevTarget, big.NewInt(duration))
// divided by 2 weeks
newTarget.Div(newTarget, big.NewInt(int64(targetTimespan)))
// clip again if above minimum target (too easy)
if newTarget.Cmp(p.PowLimit) > 0 {
newTarget.Set(p.PowLimit)
}
return blockchain.BigToCompact(newTarget)
}