This completely reworks the way block index and processing works to use
headers-first semantics and support out of order processing of the
associated block data.
This will ultimately provide a wide variety of benefits as it means the
entire shape of the block tree can be determined from the headers alone
which in turn will allow much more informed decisions to be made,
provides better information regarding sync status and warning
conditions, and allows future work to add invalidation and
reconsideration of arbitrary blocks.
It must be noted that this is a major change to the way the block index
and block handling is done and is deeply integrated with the consensus
rules, so it will need a significant amount of testing and extremely
careful review.
Also of note is that there are a lot of assumptions made in the calling
code in regards to expected error attribution and the state the chain
has at the moment notifications are processed, so all of those semantics
have intentionally been retained, even though they might seem a bit out
of place now, in order to limit the amount of changes introduced at a
time and thus better ensures correctness.
For example, in the future it would probably make more sense to make the
notifications entirely asynchronous and for validation failures to be
reported via those asynchronous notifications. However, before that can
happen, all callers would first need to be updated to ensure they do not
rely on the chain being in the same state it was at the moment the
notification was generated.
In addition to all of the existing full block tests, this introduces
a comprehensive set of processing order tests which exercise all of the
new logic.
High level overview of the changes:
- Introduce tracking for whether a block is fully linked, meaning it
builds on a branch that has block data for all of its ancestors
- Add received order tracking to ensure miners are not able to gain an
advantage in terms of chain selection by only advertising a header
- Add several new pieces of information to the block index:
- Header with the most cumulative work that is not known to be invalid
- Header with the most cumulative work that is known to be invalid
- Map of best chain candidates to aid in efficient selection
- Map of unlinked children for more efficient linking
- Prunable cached chain tips to significantly reduce potential search
space during invalidation
- Introduce a compare function which determines which of two nodes
should be considered better for the purposes of best chain selection
- Add chain tip iteration capabilities with potential filtering
- Mark all descendants invalid due to known invalid ancestor when a
block is marked invalid
- Add ability to determine if a block can currently be validated
- Rework the chain reorganization func to work with an arbitrary target
- Modify the overall chain reorg func to reorg to the block with the
most cumulative work that is valid in the case it is not possible to
reorg to the target block
- Add a new MultiError type for keeping track of multiple errors in the
same call
- Add a new ErrNoBlockData error kind
- Update all cases that only require all of the ancestor block data to
be available to check for that condition instead of the more strict
condition of already being fully validated
- Introduce a new processing lock separate from the overall chain lock
- Add new method and ability to independently accept block headers
- Rework the block processing func to accept blocks out of order so long
as their header is already known valid
- Keep a cache of blocks that recently passed contextual validation
checks
- Cleanup and correct various comments to match reality
- Add comprehensive tests to exercise the new processing logic and best
header tracking (for both invalid and not known invalid)
362 lines
10 KiB
Go
362 lines
10 KiB
Go
// Copyright (c) 2013-2016 The btcsuite developers
|
|
// Copyright (c) 2015-2020 The Decred developers
|
|
// Use of this source code is governed by an ISC
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/decred/dcrd/blockchain/v4"
|
|
"github.com/decred/dcrd/blockchain/v4/indexers"
|
|
"github.com/decred/dcrd/chaincfg/chainhash"
|
|
"github.com/decred/dcrd/database/v2"
|
|
"github.com/decred/dcrd/dcrutil/v4"
|
|
"github.com/decred/dcrd/wire"
|
|
)
|
|
|
|
var zeroHash = chainhash.Hash{}
|
|
|
|
// importResults houses the stats and result as an import operation.
|
|
type importResults struct {
|
|
blocksProcessed int64
|
|
blocksImported int64
|
|
duration time.Duration
|
|
err error
|
|
}
|
|
|
|
// blockImporter houses information about an ongoing import from a block data
|
|
// file to the block database.
|
|
type blockImporter struct {
|
|
db database.DB
|
|
chain *blockchain.BlockChain
|
|
r io.ReadSeeker
|
|
processQueue chan []byte
|
|
doneChan chan bool
|
|
errChan chan error
|
|
quit chan struct{}
|
|
wg sync.WaitGroup
|
|
blocksProcessed int64
|
|
blocksImported int64
|
|
receivedLogBlocks int64
|
|
receivedLogTx int64
|
|
lastHeight int64
|
|
lastBlockTime time.Time
|
|
lastLogTime time.Time
|
|
startTime time.Time
|
|
}
|
|
|
|
// readBlock reads the next block from the input file.
|
|
func (bi *blockImporter) readBlock() ([]byte, error) {
|
|
// The block file format is:
|
|
// <network> <block length> <serialized block>
|
|
var net uint32
|
|
err := binary.Read(bi.r, binary.LittleEndian, &net)
|
|
if err != nil {
|
|
if !errors.Is(err, io.EOF) {
|
|
return nil, err
|
|
}
|
|
|
|
// No block and no error means there are no more blocks to read.
|
|
return nil, nil
|
|
}
|
|
if net != uint32(activeNetParams.Net) {
|
|
return nil, fmt.Errorf("network mismatch -- got %x, want %x",
|
|
net, uint32(activeNetParams.Net))
|
|
}
|
|
|
|
// Read the block length and ensure it is sane.
|
|
var blockLen uint32
|
|
if err := binary.Read(bi.r, binary.LittleEndian, &blockLen); err != nil {
|
|
return nil, err
|
|
}
|
|
if blockLen > wire.MaxBlockPayload {
|
|
return nil, fmt.Errorf("block payload of %d bytes is larger "+
|
|
"than the max allowed %d bytes", blockLen,
|
|
wire.MaxBlockPayload)
|
|
}
|
|
|
|
serializedBlock := make([]byte, blockLen)
|
|
if _, err := io.ReadFull(bi.r, serializedBlock); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return serializedBlock, nil
|
|
}
|
|
|
|
// processBlock potentially imports the block into the database. It first
|
|
// deserializes the raw block while checking for errors. Already known blocks
|
|
// are skipped and orphan blocks are considered errors. Finally, it runs the
|
|
// block through the chain rules to ensure it follows all rules and matches
|
|
// up to the known checkpoint. Returns whether the block was imported along
|
|
// with any potential errors.
|
|
func (bi *blockImporter) processBlock(serializedBlock []byte) (bool, error) {
|
|
// Deserialize the block which includes checks for malformed blocks.
|
|
block, err := dcrutil.NewBlockFromBytes(serializedBlock)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
// update progress statistics
|
|
bi.lastBlockTime = block.MsgBlock().Header.Timestamp
|
|
bi.receivedLogTx += int64(len(block.MsgBlock().Transactions))
|
|
|
|
// Skip blocks that already exist.
|
|
blockHash := block.Hash()
|
|
if bi.chain.HaveBlock(blockHash) {
|
|
return false, nil
|
|
}
|
|
|
|
// Don't bother trying to process orphans.
|
|
prevHash := &block.MsgBlock().Header.PrevBlock
|
|
if !prevHash.IsEqual(&zeroHash) {
|
|
if !bi.chain.HaveBlock(prevHash) {
|
|
return false, fmt.Errorf("import file contains block "+
|
|
"%v which does not link to the available "+
|
|
"block chain", prevHash)
|
|
}
|
|
}
|
|
|
|
// Ensure the blocks follows all of the chain rules and match up to the
|
|
// known checkpoints.
|
|
forkLen, err := bi.chain.ProcessBlock(block, blockchain.BFFastAdd)
|
|
if err != nil {
|
|
if errors.Is(err, blockchain.ErrMissingParent) {
|
|
return false, fmt.Errorf("import file contains an orphan block: %v",
|
|
blockHash)
|
|
}
|
|
|
|
return false, err
|
|
}
|
|
isMainChain := forkLen == 0
|
|
if !isMainChain {
|
|
return false, fmt.Errorf("import file contains a block that "+
|
|
"does not extend the main chain: %v", blockHash)
|
|
}
|
|
|
|
return true, nil
|
|
}
|
|
|
|
// readHandler is the main handler for reading blocks from the import file.
|
|
// This allows block processing to take place in parallel with block reads.
|
|
// It must be run as a goroutine.
|
|
func (bi *blockImporter) readHandler() {
|
|
out:
|
|
for {
|
|
// Read the next block from the file and if anything goes wrong
|
|
// notify the status handler with the error and bail.
|
|
serializedBlock, err := bi.readBlock()
|
|
if err != nil {
|
|
bi.errChan <- fmt.Errorf("error reading from input "+
|
|
"file: %v", err.Error())
|
|
break out
|
|
}
|
|
|
|
// A nil block with no error means we're done.
|
|
if serializedBlock == nil {
|
|
break out
|
|
}
|
|
|
|
// Send the block or quit if we've been signalled to exit by
|
|
// the status handler due to an error elsewhere.
|
|
select {
|
|
case bi.processQueue <- serializedBlock:
|
|
case <-bi.quit:
|
|
break out
|
|
}
|
|
}
|
|
|
|
// Close the processing channel to signal no more blocks are coming.
|
|
close(bi.processQueue)
|
|
bi.wg.Done()
|
|
}
|
|
|
|
// logProgress logs block progress as an information message. In order to
|
|
// prevent spam, it limits logging to one message every cfg.Progress seconds
|
|
// with duration and totals included.
|
|
func (bi *blockImporter) logProgress() {
|
|
bi.receivedLogBlocks++
|
|
|
|
now := time.Now()
|
|
duration := now.Sub(bi.lastLogTime)
|
|
if duration < time.Second*time.Duration(cfg.Progress) {
|
|
return
|
|
}
|
|
|
|
// Truncate the duration to 10s of milliseconds.
|
|
durationMillis := int64(duration / time.Millisecond)
|
|
tDuration := 10 * time.Millisecond * time.Duration(durationMillis/10)
|
|
|
|
// Log information about new block height.
|
|
blockStr := "blocks"
|
|
if bi.receivedLogBlocks == 1 {
|
|
blockStr = "block"
|
|
}
|
|
txStr := "transactions"
|
|
if bi.receivedLogTx == 1 {
|
|
txStr = "transaction"
|
|
}
|
|
log.Infof("Processed %d %s in the last %s (%d %s, height %d, %s)",
|
|
bi.receivedLogBlocks, blockStr, tDuration, bi.receivedLogTx,
|
|
txStr, bi.lastHeight, bi.lastBlockTime)
|
|
|
|
bi.receivedLogBlocks = 0
|
|
bi.receivedLogTx = 0
|
|
bi.lastLogTime = now
|
|
}
|
|
|
|
// processHandler is the main handler for processing blocks. This allows block
|
|
// processing to take place in parallel with block reads from the import file.
|
|
// It must be run as a goroutine.
|
|
func (bi *blockImporter) processHandler() {
|
|
out:
|
|
for {
|
|
select {
|
|
case serializedBlock, ok := <-bi.processQueue:
|
|
// We're done when the channel is closed.
|
|
if !ok {
|
|
break out
|
|
}
|
|
|
|
bi.blocksProcessed++
|
|
bi.lastHeight++
|
|
imported, err := bi.processBlock(serializedBlock)
|
|
if err != nil {
|
|
bi.errChan <- err
|
|
break out
|
|
}
|
|
|
|
if imported {
|
|
bi.blocksImported++
|
|
}
|
|
|
|
bi.logProgress()
|
|
|
|
case <-bi.quit:
|
|
break out
|
|
}
|
|
}
|
|
bi.wg.Done()
|
|
}
|
|
|
|
// statusHandler waits for updates from the import operation and notifies
|
|
// the passed doneChan with the results of the import. It also causes all
|
|
// goroutines to exit if an error is reported from any of them.
|
|
func (bi *blockImporter) statusHandler(resultsChan chan *importResults) {
|
|
select {
|
|
// An error from either of the goroutines means we're done so signal
|
|
// caller with the error and signal all goroutines to quit.
|
|
case err := <-bi.errChan:
|
|
resultsChan <- &importResults{
|
|
blocksProcessed: bi.blocksProcessed,
|
|
blocksImported: bi.blocksImported,
|
|
duration: time.Since(bi.startTime),
|
|
err: err,
|
|
}
|
|
close(bi.quit)
|
|
|
|
// The import finished normally.
|
|
case <-bi.doneChan:
|
|
resultsChan <- &importResults{
|
|
blocksProcessed: bi.blocksProcessed,
|
|
blocksImported: bi.blocksImported,
|
|
duration: time.Since(bi.startTime),
|
|
err: nil,
|
|
}
|
|
}
|
|
}
|
|
|
|
// Import is the core function which handles importing the blocks from the file
|
|
// associated with the block importer to the database. It returns a channel
|
|
// on which the results will be returned when the operation has completed.
|
|
func (bi *blockImporter) Import() chan *importResults {
|
|
// Start up the read and process handling goroutines. This setup allows
|
|
// blocks to be read from disk in parallel while being processed.
|
|
bi.wg.Add(2)
|
|
go bi.readHandler()
|
|
go bi.processHandler()
|
|
|
|
// Wait for the import to finish in a separate goroutine and signal
|
|
// the status handler when done.
|
|
go func() {
|
|
bi.wg.Wait()
|
|
bi.doneChan <- true
|
|
}()
|
|
|
|
// Start the status handler and return the result channel that it will
|
|
// send the results on when the import is done.
|
|
resultChan := make(chan *importResults)
|
|
go bi.statusHandler(resultChan)
|
|
return resultChan
|
|
}
|
|
|
|
// newBlockImporter returns a new importer for the provided file reader seeker
|
|
// and database.
|
|
func newBlockImporter(db database.DB, r io.ReadSeeker) (*blockImporter, error) {
|
|
// Create the various indexes as needed.
|
|
//
|
|
// CAUTION: the txindex needs to be first in the indexes array because
|
|
// the addrindex uses data from the txindex during catchup. If the
|
|
// addrindex is run first, it may not have the transactions from the
|
|
// current block indexed.
|
|
var indexes []indexers.Indexer
|
|
if cfg.TxIndex || cfg.AddrIndex {
|
|
// Enable transaction index if address index is enabled since it
|
|
// requires it.
|
|
if !cfg.TxIndex {
|
|
log.Infof("Transaction index enabled because it is " +
|
|
"required by the address index")
|
|
cfg.TxIndex = true
|
|
} else {
|
|
log.Info("Transaction index is enabled")
|
|
}
|
|
indexes = append(indexes, indexers.NewTxIndex(db))
|
|
}
|
|
if cfg.AddrIndex {
|
|
log.Info("Address index is enabled")
|
|
indexes = append(indexes, indexers.NewAddrIndex(db, activeNetParams))
|
|
}
|
|
if !cfg.NoExistsAddrIndex {
|
|
log.Info("Exists address index is enabled")
|
|
indexes = append(indexes, indexers.NewExistsAddrIndex(db,
|
|
activeNetParams))
|
|
}
|
|
|
|
// Create an index manager if any of the optional indexes are enabled.
|
|
var indexManager indexers.IndexManager
|
|
if len(indexes) > 0 {
|
|
indexManager = indexers.NewManager(db, indexes, activeNetParams)
|
|
}
|
|
|
|
chain, err := blockchain.New(context.Background(),
|
|
&blockchain.Config{
|
|
DB: db,
|
|
ChainParams: activeNetParams,
|
|
Checkpoints: activeNetParams.Checkpoints,
|
|
TimeSource: blockchain.NewMedianTime(),
|
|
IndexManager: indexManager,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &blockImporter{
|
|
db: db,
|
|
r: r,
|
|
processQueue: make(chan []byte, 2),
|
|
doneChan: make(chan bool),
|
|
errChan: make(chan error),
|
|
quit: make(chan struct{}),
|
|
chain: chain,
|
|
lastLogTime: time.Now(),
|
|
startTime: time.Now(),
|
|
}, nil
|
|
}
|