Skip to content

Commit

Permalink
addblock: Fix blockchain initialization.
Browse files Browse the repository at this point in the history
This updates the addblock command to create UtxoBackend and UtxoCache
instances, which are required for initializing the blockchain.
  • Loading branch information
rstaudt2 committed Oct 13, 2021
1 parent 3d6d9b8 commit 8988df3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
11 changes: 10 additions & 1 deletion cmd/addblock/addblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ func realMain() error {
}
defer db.Close()

// Load the UTXO database.
utxoDb, err := blockchain.LoadUtxoDB(context.Background(), activeNetParams,
cfg.DataDir)
if err != nil {
log.Errorf("Failed to load UTXO database: %v", err)
return err
}
defer utxoDb.Close()

fi, err := os.Open(cfg.InFile)
if err != nil {
log.Errorf("Failed to open file %v: %v", cfg.InFile, err)
Expand All @@ -96,7 +105,7 @@ func realMain() error {
// The done channel returned from start will contain an error if
// anything went wrong.
ctx, cancel := context.WithCancel(context.Background())
importer, err := newBlockImporter(ctx, db, fi, cancel)
importer, err := newBlockImporter(ctx, db, utxoDb, fi, cancel)
if err != nil {
log.Errorf("Failed create block importer: %v", err)
return err
Expand Down
13 changes: 12 additions & 1 deletion cmd/addblock/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/decred/dcrd/database/v3"
"github.com/decred/dcrd/dcrutil/v4"
"github.com/decred/dcrd/wire"
"github.com/syndtr/goleveldb/leveldb"
)

var zeroHash = chainhash.Hash{}
Expand Down Expand Up @@ -307,17 +308,27 @@ func (bi *blockImporter) Import(ctx context.Context) chan *importResults {

// newBlockImporter returns a new importer for the provided file reader seeker
// and database.
func newBlockImporter(ctx context.Context, db database.DB, r io.ReadSeeker, cancel context.CancelFunc) (*blockImporter, error) {
func newBlockImporter(ctx context.Context, db database.DB, utxoDb *leveldb.DB, r io.ReadSeeker, cancel context.CancelFunc) (*blockImporter, error) {
subber := indexers.NewIndexSubscriber(ctx)
go subber.Run(ctx)

// Instantiate a UTXO backend and UTXO cache.
utxoBackend := blockchain.NewLevelDbUtxoBackend(utxoDb)
utxoCache := blockchain.NewUtxoCache(&blockchain.UtxoCacheConfig{
Backend: utxoBackend,
FlushBlockDB: db.Flush,
MaxSize: 100 * 1024 * 1024, // 100 MiB
})

chain, err := blockchain.New(context.Background(),
&blockchain.Config{
DB: db,
ChainParams: activeNetParams,
Checkpoints: activeNetParams.Checkpoints,
TimeSource: blockchain.NewMedianTime(),
IndexSubscriber: subber,
UtxoBackend: blockchain.NewLevelDbUtxoBackend(utxoDb),
UtxoCache: utxoCache,
})
if err != nil {
return nil, err
Expand Down

0 comments on commit 8988df3

Please sign in to comment.