dcrd/blockchain/notifications.go
Dave Collins 57b59b4e26
blockchain: Decouple processing and download logic.
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)
2021-01-07 10:43:49 -06:00

201 lines
7.1 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 blockchain
import (
"fmt"
"github.com/decred/dcrd/chaincfg/chainhash"
"github.com/decred/dcrd/dcrutil/v4"
)
// NotificationType represents the type of a notification message.
type NotificationType int
// NotificationCallback is used for a caller to provide a callback for
// notifications about various chain events.
type NotificationCallback func(*Notification)
// Constants for the type of a notification message.
const (
// NTNewTipBlockChecked indicates the associated block intends to extend
// the current main chain and has passed all of the sanity and
// contextual checks such as having valid proof of work, valid merkle
// and stake roots, and only containing allowed votes and revocations.
//
// It should be noted that the block might still ultimately fail to
// become the new main chain tip if it contains invalid scripts, double
// spends, etc. However, this is quite rare in practice because a lot
// of work was expended to create a block which satisfies the proof of
// work requirement.
//
// Finally, this notification is only sent if the chain is believed
// to be current and the chain lock is NOT released, so consumers must
// take care to avoid calling blockchain functions to avoid potential
// deadlock.
//
// Typically, a caller would want to use this notification to relay the
// block to the rest of the network without needing to wait for the more
// time consuming full connection to take place.
NTNewTipBlockChecked NotificationType = iota
// NTBlockAccepted indicates the associated block was accepted into
// the block chain. Note that this does not necessarily mean it was
// added to the main chain. For that, use NTBlockConnected.
NTBlockAccepted
// NTBlockConnected indicates the associated block was connected to the
// main chain.
NTBlockConnected
// NTBlockDisconnected indicates the associated block was disconnected
// from the main chain.
NTBlockDisconnected
// NTChainReorgStarted indicates that a chain reorganization has commenced.
NTChainReorgStarted
// NTChainReorgDone indicates that a chain reorganization has concluded.
NTChainReorgDone
// NTReorganization indicates that a blockchain reorganization has taken
// place.
NTReorganization
// NTSpentAndMissedTickets indicates spent or missed tickets from a newly
// accepted block.
NTSpentAndMissedTickets
// NTNewTickets indicates newly maturing tickets from a newly accepted
// block.
NTNewTickets
)
// notificationTypeStrings is a map of notification types back to their constant
// names for pretty printing.
var notificationTypeStrings = map[NotificationType]string{
NTNewTipBlockChecked: "NTNewTipBlockChecked",
NTBlockAccepted: "NTBlockAccepted",
NTBlockConnected: "NTBlockConnected",
NTBlockDisconnected: "NTBlockDisconnected",
NTChainReorgStarted: "NTChainReorgStarted",
NTChainReorgDone: "NTChainReorgDone",
NTReorganization: "NTReorganization",
NTSpentAndMissedTickets: "NTSpentAndMissedTickets",
NTNewTickets: "NTNewTickets",
}
// String returns the NotificationType in human-readable form.
func (n NotificationType) String() string {
if s, ok := notificationTypeStrings[n]; ok {
return s
}
return fmt.Sprintf("Unknown Notification Type (%d)", int(n))
}
// BlockAcceptedNtfnsData is the structure for data indicating information
// about an accepted block. Note that this does not necessarily mean the block
// that was accepted extended the best chain as it might have created or
// extended a side chain.
type BlockAcceptedNtfnsData struct {
// BestHeight is the height of the current best chain. Since the accepted
// block might be on a side chain, this is not necessarily the same as the
// height of the accepted block.
BestHeight int64
// ForkLen is the length of the side chain the block extended or zero in the
// case the block extended the main chain.
//
// This can be used in conjunction with the height of the accepted block to
// determine the height at which the side chain the block created or
// extended forked from the best chain.
ForkLen int64
// Block is the block that was accepted into the chain.
Block *dcrutil.Block
}
// BlockConnectedNtfnsData is the structure for data indicating information
// about a connected block.
type BlockConnectedNtfnsData struct {
// Block is the block that was connected to the main chain.
Block *dcrutil.Block
// ParentBlock is the parent block of the one that was connected to the main
// chain.
ParentBlock *dcrutil.Block
// IsTreasuryActive indicates whether or not the treasury agenda is active
// for the block that was connected.
IsTreasuryActive bool
}
// BlockDisconnectedNtfnsData is the structure for data indicating information
// about a disconnected block.
type BlockDisconnectedNtfnsData struct {
// Block is the block that was disconnected from the main chain.
Block *dcrutil.Block
// ParentBlock is the parent block of the one that was disconnected from the
// main chain meaning this block is now the tip of the main chain.
ParentBlock *dcrutil.Block
// IsTreasuryActive indicates whether or not the treasury agenda was active
// for the block that was **disconnected**.
IsTreasuryActive bool
}
// ReorganizationNtfnsData is the structure for data indicating information
// about a reorganization.
type ReorganizationNtfnsData struct {
OldHash chainhash.Hash
OldHeight int64
NewHash chainhash.Hash
NewHeight int64
}
// TicketNotificationsData is the structure for new/spent/missed ticket
// notifications at blockchain HEAD that are outgoing from chain.
type TicketNotificationsData struct {
Hash chainhash.Hash
Height int64
StakeDifficulty int64
TicketsSpent []chainhash.Hash
TicketsMissed []chainhash.Hash
TicketsNew []chainhash.Hash
}
// Notification defines notification that is sent to the caller via the callback
// function provided during the call to New and consists of a notification type
// as well as associated data that depends on the type as follows:
// - NTNewTipBlockChecked: *dcrutil.Block
// - NTBlockAccepted: *BlockAcceptedNtfnsData
// - NTBlockConnected: *BlockConnectedNtfnsData
// - NTBlockDisconnected: *BlockDisconnectedNtfnsData
// - NTChainReorgStarted: nil
// - NTChainReorgDone: nil
// - NTReorganization: *ReorganizationNtfnsData
// - NTSpentAndMissedTickets: *TicketNotificationsData
// - NTNewTickets: *TicketNotificationsData
type Notification struct {
Type NotificationType
Data interface{}
}
// sendNotification sends a notification with the passed type and data if the
// caller requested notifications by providing a callback function in the call
// to New.
func (b *BlockChain) sendNotification(typ NotificationType, data interface{}) {
// Ignore it if the caller didn't request notifications.
if b.notifications == nil {
return
}
// Generate and send the notification.
n := Notification{Type: typ, Data: data}
b.notifications(&n)
}