forked from decred/dcrd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
703 lines (594 loc) · 20.7 KB
/
common.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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
// Copyright (c) 2016 The btcsuite developers
// Copyright (c) 2016-2023 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
/*
Package indexers implements optional block chain indexes.
*/
package indexers
import (
"context"
"encoding/binary"
"errors"
"fmt"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/chaincfg/v3"
"github.com/decred/dcrd/database/v3"
"github.com/decred/dcrd/dcrutil/v4"
"github.com/decred/dcrd/internal/blockchain/progresslog"
"github.com/decred/dcrd/wire"
)
var (
// byteOrder is the preferred byte order used for serializing numeric
// fields for storage in the database.
byteOrder = binary.LittleEndian
// indexTipsBucketName is the name of the db bucket used to house the
// current tip of each index.
indexTipsBucketName = []byte("idxtips")
// interruptMsg is the error message for interrupt requested errors.
interruptMsg = "interrupt requested"
)
// ChainQueryer provides a generic interface that is used to provide access to
// the chain details required by indexes.
//
// All functions MUST be safe for concurrent access.
type ChainQueryer interface {
// MainChainHasBlock returns whether or not the block with the given hash is
// in the main chain.
MainChainHasBlock(*chainhash.Hash) bool
// ChainParams returns the network parameters of the chain.
ChainParams() *chaincfg.Params
// Best returns the height and hash of the current best block.
Best() (int64, *chainhash.Hash)
// BlockHeaderByHash returns the block header identified by the given hash.
BlockHeaderByHash(hash *chainhash.Hash) (wire.BlockHeader, error)
// BlockHashByHeight returns the hash of the block at the given height in
// the main chain.
BlockHashByHeight(int64) (*chainhash.Hash, error)
// BlockHeightByHash returns the height of the block with the given hash
// in the main chain.
BlockHeightByHash(hash *chainhash.Hash) (int64, error)
// BlockByHash returns the block of the provided hash.
BlockByHash(*chainhash.Hash) (*dcrutil.Block, error)
// IsTreasuryAgendaActive returns true if the treasury agenda is active at
// the provided block.
IsTreasuryAgendaActive(*chainhash.Hash) (bool, error)
}
// Indexer defines a generic interface for an indexer.
type Indexer interface {
// Key returns the key of the index as a byte slice.
Key() []byte
// Name returns the human-readable name of the index.
Name() string
// Version returns the current version of the index.
Version() uint32
// DB returns the database of the index.
DB() database.DB
// Queryer returns the chain queryer.
Queryer() ChainQueryer
// Tip returns the current index tip.
Tip() (int64, *chainhash.Hash, error)
// Create is invoked when the indexer is being created.
Create(dbTx database.Tx) error
// Init is invoked when the index is being initialized.
// This differs from the Create method in that it is called on
// every load, including the case the index was just created.
Init(ctx context.Context, chainParams *chaincfg.Params) error
// ProcessNotification indexes the provided notification based on its
// notification type.
ProcessNotification(dbTx database.Tx, ntfn *IndexNtfn) error
// IndexSubscription returns the subscription for index updates.
IndexSubscription() *IndexSubscription
// WaitForSync subscribes clients for the next index sync update.
WaitForSync() chan bool
// NotifySyncSubscribers signals subscribers of an index sync update.
// This should only be called when an index is synced.
NotifySyncSubscribers()
}
// IndexDropper provides a method to remove an index from the database. Indexers
// may implement this for a more efficient way of deleting themselves from the
// database rather than simply dropping a bucket.
type IndexDropper interface {
DropIndex(context.Context, database.DB) error
}
// AssertError identifies an error that indicates an internal code consistency
// issue and should be treated as a critical and unrecoverable error.
type AssertError string
// Error returns the assertion error as a huma-readable string and satisfies
// the error interface.
func (e AssertError) Error() string {
return "assertion failed: " + string(e)
}
// internalBucket is an abstraction over a database bucket. It is used to make
// the code easier to test since it allows mock objects in the tests to only
// implement these functions instead of everything a database.Bucket supports.
type internalBucket interface {
Get(key []byte) []byte
Put(key []byte, value []byte) error
Delete(key []byte) error
}
// interruptRequested returns true when the provided channel has been closed.
// This simplifies early shutdown slightly since the caller can just use an if
// statement instead of a select.
func interruptRequested(ctx context.Context) bool {
return ctx.Err() != nil
}
// makeDbErr creates a database.Error given a set of arguments.
func makeDbErr(kind database.ErrorKind, desc string) database.Error {
return database.Error{Err: kind, Description: desc}
}
// dbPutIndexerTip uses an existing database transaction to update or add the
// current tip for the given index to the provided values.
func dbPutIndexerTip(dbTx database.Tx, idxKey []byte, hash *chainhash.Hash, height int32) error {
serialized := make([]byte, chainhash.HashSize+4)
copy(serialized, hash[:])
byteOrder.PutUint32(serialized[chainhash.HashSize:], uint32(height))
indexesBucket := dbTx.Metadata().Bucket(indexTipsBucketName)
return indexesBucket.Put(idxKey, serialized)
}
// dbFetchIndexerTip uses an existing database transaction to retrieve the
// hash and height of the current tip for the provided index.
func dbFetchIndexerTip(dbTx database.Tx, idxKey []byte) (*chainhash.Hash, int32, error) {
indexesBucket := dbTx.Metadata().Bucket(indexTipsBucketName)
if indexesBucket == nil {
str := fmt.Sprintf("%s bucket not found", string(indexTipsBucketName))
return nil, 0, makeDbErr(database.ErrBucketNotFound, str)
}
serialized := indexesBucket.Get(idxKey)
if len(serialized) == 0 {
str := fmt.Sprintf("no index tip value found for %s ", string(idxKey))
return nil, 0, makeDbErr(database.ErrValueNotFound, str)
}
if len(serialized) < chainhash.HashSize+4 {
str := fmt.Sprintf("unexpected end of data for "+
"index %q tip", string(idxKey))
return nil, 0, makeDbErr(database.ErrCorruption, str)
}
var hash chainhash.Hash
copy(hash[:], serialized[:chainhash.HashSize])
height := int32(byteOrder.Uint32(serialized[chainhash.HashSize:]))
return &hash, height, nil
}
// indexVersionKey returns the key for an index which houses the current version
// of the index.
func indexVersionKey(idxKey []byte) []byte {
verKey := make([]byte, len(idxKey)+1)
verKey[0] = 'v'
copy(verKey[1:], idxKey)
return verKey
}
// dbPutIndexerVersion uses an existing database transaction to update the
// version for the given index to the provided value.
func dbPutIndexerVersion(dbTx database.Tx, idxKey []byte, version uint32) error {
serialized := make([]byte, 4)
byteOrder.PutUint32(serialized[0:4], version)
indexesBucket := dbTx.Metadata().Bucket(indexTipsBucketName)
return indexesBucket.Put(indexVersionKey(idxKey), serialized)
}
// existsIndex returns whether the index keyed by idxKey exists in the database.
func existsIndex(db database.DB, idxKey []byte) (bool, error) {
var exists bool
err := db.View(func(dbTx database.Tx) error {
indexesBucket := dbTx.Metadata().Bucket(indexTipsBucketName)
if indexesBucket != nil && indexesBucket.Get(idxKey) != nil {
exists = true
}
return nil
})
return exists, err
}
// incrementalFlatDrop uses multiple database updates to remove key/value pairs
// saved to a flat index.
func incrementalFlatDrop(ctx context.Context, db database.DB, idxKey []byte, idxName string) error {
const maxDeletions = 2000000
var totalDeleted uint64
for numDeleted := maxDeletions; numDeleted == maxDeletions; {
numDeleted = 0
err := db.Update(func(dbTx database.Tx) error {
bucket := dbTx.Metadata().Bucket(idxKey)
cursor := bucket.Cursor()
for ok := cursor.First(); ok; ok = cursor.Next() &&
numDeleted < maxDeletions {
if err := cursor.Delete(); err != nil {
return err
}
numDeleted++
}
return nil
})
if err != nil {
return err
}
if numDeleted > 0 {
totalDeleted += uint64(numDeleted)
log.Infof("Deleted %d keys (%d total) from %s",
numDeleted, totalDeleted, idxName)
}
if interruptRequested(ctx) {
return indexerError(ErrInterruptRequested, interruptMsg)
}
}
return nil
}
// indexDropKey returns the key for an index which indicates it is in the
// process of being dropped.
func indexDropKey(idxKey []byte) []byte {
dropKey := make([]byte, len(idxKey)+1)
dropKey[0] = 'd'
copy(dropKey[1:], idxKey)
return dropKey
}
// dropIndexMetadata drops the passed index from the database by removing the
// top level bucket for the index, the index tip, and any in-progress drop flag.
func dropIndexMetadata(db database.DB, idxKey []byte) error {
return db.Update(func(dbTx database.Tx) error {
meta := dbTx.Metadata()
indexesBucket := meta.Bucket(indexTipsBucketName)
err := indexesBucket.Delete(idxKey)
if err != nil {
return err
}
err = meta.DeleteBucket(idxKey)
if err != nil && !errors.Is(err, database.ErrBucketNotFound) {
return err
}
err = indexesBucket.Delete(indexVersionKey(idxKey))
if err != nil {
return err
}
return indexesBucket.Delete(indexDropKey(idxKey))
})
}
// dropFlatIndex incrementally drops the passed index from the database. Since
// indexes can be massive, it deletes the index in multiple database
// transactions in order to keep memory usage to reasonable levels. For this
// algorithm to work, the index must be "flat" (have no nested buckets). It
// also marks the drop in progress so the drop can be resumed if it is stopped
// before it is done before the index can be used again.
func dropFlatIndex(ctx context.Context, db database.DB, idxKey []byte, idxName string) error {
// Nothing to do if the index doesn't already exist.
exists, err := existsIndex(db, idxKey)
if err != nil {
return err
}
if !exists {
log.Infof("Not dropping %s because it does not exist", idxName)
return nil
}
log.Infof("Dropping all %s entries. This might take a while...",
idxName)
// Mark that the index is in the process of being dropped so that it
// can be resumed on the next start if interrupted before the process is
// complete.
err = markIndexDeletion(db, idxKey)
if err != nil {
return err
}
// Since the indexes can be so large, attempting to simply delete
// the bucket in a single database transaction would result in massive
// memory usage and likely crash many systems due to ulimits. In order
// to avoid this, use a cursor to delete a maximum number of entries out
// of the bucket at a time.
err = incrementalFlatDrop(ctx, db, idxKey, idxName)
if err != nil {
return err
}
// Remove the index tip, version, bucket, and in-progress drop flag now that
// all index entries have been removed.
err = dropIndexMetadata(db, idxKey)
if err != nil {
return err
}
log.Infof("Dropped %s", idxName)
return nil
}
// dropIndex drops the passed index from the database without using incremental
// deletion. This should be used to drop indexes containing nested buckets,
// which can not be deleted with dropFlatIndex.
func dropIndex(db database.DB, idxKey []byte, idxName string) error {
// Nothing to do if the index doesn't already exist.
exists, err := existsIndex(db, idxKey)
if err != nil {
return err
}
if !exists {
log.Infof("Not dropping %s because it does not exist", idxName)
return nil
}
log.Infof("Dropping all %s entries. This might take a while...",
idxName)
// Mark that the index is in the process of being dropped so that it
// can be resumed on the next start if interrupted before the process is
// complete.
err = markIndexDeletion(db, idxKey)
if err != nil {
return err
}
// Remove the index tip, version, bucket, and in-progress drop flag.
// Removing the index bucket also recursively removes all values saved to
// the index.
err = dropIndexMetadata(db, idxKey)
if err != nil {
return err
}
log.Infof("Dropped %s", idxName)
return nil
}
// markIndexDeletion marks the index identified by idxKey for deletion. Marking
// an index for deletion allows deletion to resume next startup if an
// incremental deletion was interrupted.
func markIndexDeletion(db database.DB, idxKey []byte) error {
return db.Update(func(dbTx database.Tx) error {
indexesBucket := dbTx.Metadata().Bucket(indexTipsBucketName)
return indexesBucket.Put(indexDropKey(idxKey), idxKey)
})
}
// tip returns the current tip hash and height of the provided index.
func tip(db database.DB, key []byte) (int64, *chainhash.Hash, error) {
var hash *chainhash.Hash
var height int32
err := db.View(func(dbTx database.Tx) error {
var err error
hash, height, err = dbFetchIndexerTip(dbTx, key)
return err
})
if err != nil {
return 0, nil, err
}
return int64(height), hash, err
}
// notifySyncSubscribers signals provided subscribers the index subcribed to
// is synced.
func notifySyncSubscribers(subscribers map[chan bool]struct{}) {
for sub := range subscribers {
close(sub)
delete(subscribers, sub)
}
}
// recoverIndex reverts the index to a block on the main chain by repeatedly
// disconnecting the index tip if it is not on the main chain.
func recoverIndex(ctx context.Context, idx Indexer) error {
// Fetch the current tip for the index.
height, hash, err := idx.Tip()
if err != nil {
return err
}
// Nothing to do if the index does not have any entries yet.
if height == 0 {
return nil
}
// Nothing to do if the index tip is on the main chain.
if idx.Queryer().MainChainHasBlock(hash) {
return nil
}
log.Infof("%s: recovering from tip %d (%s)", idx.Name(), height, hash)
// Create a progress logger for the recovery process below.
progressLogger := progresslog.NewBlockProgressLogger("Recovered", log)
queryer := idx.Queryer()
var cachedBlock *dcrutil.Block
for !queryer.MainChainHasBlock(hash) {
if interruptRequested(ctx) {
return indexerError(ErrInterruptRequested, interruptMsg)
}
// Get the block, unless it's already cached.
var block *dcrutil.Block
if cachedBlock == nil && height > 0 {
block, err = queryer.BlockByHash(hash)
if err != nil {
return err
}
} else {
block = cachedBlock
}
parentHash := &block.MsgBlock().Header.PrevBlock
parent, err := queryer.BlockByHash(parentHash)
if err != nil {
return err
}
cachedBlock = parent
isTreasuryEnabled, err := queryer.IsTreasuryAgendaActive(parentHash)
if err != nil {
return err
}
ntfn := &IndexNtfn{
NtfnType: DisconnectNtfn,
Block: block,
Parent: parent,
IsTreasuryEnabled: isTreasuryEnabled,
Done: make(chan bool),
}
err = updateIndex(ctx, idx, ntfn)
if err != nil {
return err
}
// Update the tip to the previous block.
hash = &block.MsgBlock().Header.PrevBlock
height--
progressLogger.LogBlockHeight(block.MsgBlock())
}
log.Infof("%s: index recovered to tip %d (%s)", idx.Name(),
height, hash)
return nil
}
// finishDrop determines if the provided index is in the middle
// of being dropped and finishes dropping it when it is. This is necessary
// because dropping an index has to be done in several atomic steps rather
// than one big atomic step due to the massive number of entries.
func finishDrop(ctx context.Context, indexer Indexer) error {
var drop bool
err := indexer.DB().View(func(dbTx database.Tx) error {
// The index does not need to be dropped if the index tips
// bucket hasn't been created yet.
indexesBucket := dbTx.Metadata().Bucket(indexTipsBucketName)
if indexesBucket == nil {
return nil
}
// Mark the indexer as requiring a drop if one is already in
// progress.
dropKey := indexDropKey(indexer.Key())
if indexesBucket.Get(dropKey) != nil {
drop = true
}
return nil
})
if err != nil {
return err
}
// Nothing to do if the index does not need dropping.
if !drop {
return nil
}
if interruptRequested(ctx) {
return indexerError(ErrInterruptRequested, interruptMsg)
}
log.Infof("Resuming %s drop", indexer.Name())
switch d := indexer.(type) {
case IndexDropper:
err := d.DropIndex(ctx, indexer.DB())
if err != nil {
return err
}
default:
err := dropIndex(indexer.DB(), indexer.Key(), indexer.Name())
if err != nil {
return err
}
}
return nil
}
// createIndex determines if each of the provided index has already
// been created and creates it if not.
func createIndex(indexer Indexer, genesisHash *chainhash.Hash) error {
return indexer.DB().Update(func(dbTx database.Tx) error {
// Create the bucket for the current tips as needed.
meta := dbTx.Metadata()
indexesBucket, err := meta.CreateBucketIfNotExists(indexTipsBucketName)
if err != nil {
return err
}
// Nothing to do if the index tip already exists.
idxKey := indexer.Key()
if indexesBucket.Get(idxKey) != nil {
return nil
}
// Store the index version.
err = dbPutIndexerVersion(dbTx, idxKey, indexer.Version())
if err != nil {
return err
}
// The tip for the index does not exist, so create it and
// invoke the create callback for the index so it can perform
// any one-time initialization it requires.
if err := indexer.Create(dbTx); err != nil {
return err
}
// Set the tip for the index to values which represent an
// uninitialized index (the genesis block hash and height).
err = dbPutIndexerTip(dbTx, idxKey, genesisHash, 0)
if err != nil {
return err
}
return nil
})
}
// upgradeIndex determines if the provided index needs to be upgraded.
// If it does it is dropped and recreated.
func upgradeIndex(ctx context.Context, indexer Indexer, genesisHash *chainhash.Hash) error {
if err := finishDrop(ctx, indexer); err != nil {
return err
}
return createIndex(indexer, genesisHash)
}
// maybeNotifySubscribers updates subscribers the index is synced when
// the tip is identical to the chain tip.
func maybeNotifySubscribers(ctx context.Context, indexer Indexer) error {
if interruptRequested(ctx) {
return indexerError(ErrInterruptRequested, interruptMsg)
}
bestHeight, bestHash := indexer.Queryer().Best()
tipHeight, tipHash, err := indexer.Tip()
if err != nil {
return fmt.Errorf("%s: unable to fetch index tip: %w",
indexer.Name(), err)
}
if tipHeight == bestHeight && *bestHash == *tipHash {
indexer.NotifySyncSubscribers()
}
return nil
}
// notifyDependent relays the provided index notification to the dependent of
// the provided index if there is one set.
func notifyDependent(ctx context.Context, indexer Indexer, ntfn *IndexNtfn) error {
if interruptRequested(ctx) {
return indexerError(ErrInterruptRequested, interruptMsg)
}
sub := indexer.IndexSubscription()
if sub == nil {
msg := fmt.Sprintf("%s: no index update subscription found",
indexer.Name())
return indexerError(ErrFetchSubscription, msg)
}
// Notify the dependent subscription if set.
sub.mtx.Lock()
if sub.dependent != nil {
err := updateIndex(ctx, sub.dependent.idx, ntfn)
if err != nil {
sub.mtx.Unlock()
return err
}
}
sub.mtx.Unlock()
return nil
}
// updateIndex processes the notification for the provided index.
func updateIndex(ctx context.Context, indexer Indexer, ntfn *IndexNtfn) error {
tip, _, err := indexer.Tip()
if err != nil {
msg := fmt.Sprintf("%s: unable to fetch index tip: %v",
indexer.Name(), err)
return indexerError(ErrFetchTip, msg)
}
var expectedHeight int64
switch ntfn.NtfnType {
case ConnectNtfn:
expectedHeight = tip + 1
case DisconnectNtfn:
expectedHeight = tip
default:
msg := fmt.Sprintf("%s: unknown notification type received: %v",
indexer.Name(), ntfn.NtfnType)
return indexerError(ErrInvalidNotificationType, msg)
}
switch {
case ntfn.Block.Height() < expectedHeight:
// Relay the notification to the dependent if its height is less
// than that of the expected notification since its possible for a
// dependent to have a lower tip height than its prerequisite.
log.Tracef("%s: relaying notification for height %d to dependent",
indexer.Name(), ntfn.Block.Height())
notifyDependent(ctx, indexer, ntfn)
case ntfn.Block.Height() > expectedHeight:
// Receiving a notification with a height higher than the expected
// implies a missed index update.
msg := fmt.Sprintf("%s: missing index notification, expected "+
"notification for height %d, got %d", indexer.Name(),
expectedHeight, ntfn.Block.Height())
return indexerError(ErrMissingNotification, msg)
default:
err = indexer.DB().Update(func(dbTx database.Tx) error {
return indexer.ProcessNotification(dbTx, ntfn)
})
if err != nil {
return err
}
err = notifyDependent(ctx, indexer, ntfn)
if err != nil {
return err
}
err = maybeNotifySubscribers(ctx, indexer)
if err != nil {
return err
}
}
return nil
}