Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions blockchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,18 +135,19 @@ type BlockChain struct {
// The following fields are set when the instance is created and can't
// be changed afterwards, so there is no need to protect them with a
// separate mutex.
assumeValid chainhash.Hash
latestCheckpoint *chaincfg.Checkpoint
deploymentVers map[string]uint32
db database.DB
dbInfo *databaseInfo
chainParams *chaincfg.Params
timeSource MedianTimeSource
notifications NotificationCallback
sigCache *txscript.SigCache
indexSubscriber *indexers.IndexSubscriber
interrupt <-chan struct{}
utxoCache UtxoCacher
assumeValid chainhash.Hash
allowOldForks bool
expectedBlocksInTwoWeeks int64
deploymentVers map[string]uint32
db database.DB
dbInfo *databaseInfo
chainParams *chaincfg.Params
timeSource MedianTimeSource
notifications NotificationCallback
sigCache *txscript.SigCache
indexSubscriber *indexers.IndexSubscriber
interrupt <-chan struct{}
utxoCache UtxoCacher

// subsidyCache is the cache that provides quick lookup of subsidy
// values.
Expand Down Expand Up @@ -204,16 +205,18 @@ type BlockChain struct {
disapprovedViewLock sync.Mutex
disapprovedView *UtxoViewpoint

// rejectForksCheckpoint tracks the block to treat as a checkpoint for the
// purposes of rejecting old forks. It will be nil when no suitable block
// is known or old forks are allowed for the current network.
//
// It is protected by the chain lock.
rejectForksCheckpoint *blockNode

// assumeValidNode tracks the assumed valid block. It will be nil when a
// block header with the assumed valid block hash has not been discovered or
// when assume valid is disabled. It is protected by the chain lock.
assumeValidNode *blockNode

// checkpointNode tracks the most recently known checkpoint. It will be nil
// when no checkpoints are known or are disabled. It is protected by the
// chain lock.
checkpointNode *blockNode

// The state is used as a fairly efficient way to cache information
// about the current best chain state that is returned to callers when
// requested. It operates on the principle of MVCC such that any time a
Expand Down Expand Up @@ -2269,6 +2272,14 @@ type Config struct {
// This field is required.
ChainParams *chaincfg.Params

// AllowOldForks enables processing of blocks that are forks deep in the
// chain history. This should realistically never need to be enabled in
// practice, however, it is provided for testing purposes as well as a
// recovery mechanism in the extremely unlikely case that a node were to
// somehow get stuck on a bad fork and be unable to reorg to the good one
// due to the old fork rejection semantics.
AllowOldForks bool

// AssumeValid is the hash of a block that has been externally verified to
// be valid. It allows several validation checks to be skipped for blocks
// that are both an ancestor of the assumed valid block and an ancestor of
Expand All @@ -2277,12 +2288,6 @@ type Config struct {
// This field may not be set for networks that do not require it.
AssumeValid chainhash.Hash

// LatestCheckpoint specifies the most recent known checkpoint that is
// typically the default checkpoint in ChainParams.
//
// This field can be nil if the caller does not wish to specify a checkpoint.
LatestCheckpoint *chaincfg.Checkpoint

// TimeSource defines the median time source to use for things such as
// block processing and determining whether or not the chain is current.
//
Expand Down Expand Up @@ -2355,9 +2360,23 @@ func New(ctx context.Context, config *Config) (*BlockChain, error) {
subsidyCache = standalone.NewSubsidyCache(params)
}

// Calculate the expected number of blocks in 2 weeks and cache it in order
// to avoid repeated calculation.
const timeInTwoWeeks = time.Hour * 24 * 14
expectedBlksInTwoWeeks := int64(timeInTwoWeeks / params.TargetTimePerBlock)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, good call to cache this.


// Fork rejection semantics are disabled when explicitly requested or the
// hard-coded assume valid hash is not set for the current network.
//
// Note that this is very intentionally using the hard-coded value specified
// in the chain params as opposed to the one that a user might have
// overridden in the passed config since fork rejection affects consensus.
allowOldForks := config.AllowOldForks || params.AssumeValid == *zeroHash

b := BlockChain{
assumeValid: config.AssumeValid,
latestCheckpoint: config.LatestCheckpoint,
allowOldForks: allowOldForks,
expectedBlocksInTwoWeeks: expectedBlksInTwoWeeks,
deploymentVers: deploymentVers,
db: config.DB,
chainParams: params,
Expand Down
11 changes: 2 additions & 9 deletions blockchain/chainio.go
Original file line number Diff line number Diff line change
Expand Up @@ -1587,15 +1587,8 @@ func (b *BlockChain) initChainState(ctx context.Context,
return err
}

// Find the most recent checkpoint.
if b.latestCheckpoint != nil {
node := b.index.lookupNode(b.latestCheckpoint.Hash)
if node != nil {
log.Debugf("Most recent checkpoint is %s (height %d)",
node.hash, node.height)
b.checkpointNode = node
}
}
// Attempt to discover and set the old fork rejection checkpoint node.
b.maybeSetForkRejectionCheckpoint()

// Find the assumed valid node.
if b.assumeValid != *zeroHash {
Expand Down
173 changes: 0 additions & 173 deletions blockchain/checkpoints.go

This file was deleted.

6 changes: 1 addition & 5 deletions blockchain/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,8 @@ const (
// not match the expected value.
ErrBadCommitmentRoot = ErrorKind("ErrBadCommitmentRoot")

// ErrBadCheckpoint indicates a block that is expected to be at a
// checkpoint height does not match the expected one.
ErrBadCheckpoint = ErrorKind("ErrBadCheckpoint")

// ErrForkTooOld indicates a block is attempting to fork the block chain
// before the most recent checkpoint.
// before the fork rejection checkpoint.
ErrForkTooOld = ErrorKind("ErrForkTooOld")

// ErrNoTransactions indicates the block does not have a least one
Expand Down
1 change: 0 additions & 1 deletion blockchain/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestErrorKindStringer(t *testing.T) {
{ErrHighHash, "ErrHighHash"},
{ErrBadMerkleRoot, "ErrBadMerkleRoot"},
{ErrBadCommitmentRoot, "ErrBadCommitmentRoot"},
{ErrBadCheckpoint, "ErrBadCheckpoint"},
{ErrForkTooOld, "ErrForkTooOld"},
{ErrNoTransactions, "ErrNoTransactions"},
{ErrNoTxInputs, "ErrNoTxInputs"},
Expand Down
3 changes: 0 additions & 3 deletions blockchain/fullblocktests/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ var regNetParams = &chaincfg.Params{
StakeRewardProportionV2: 8,
BlockTaxProportion: 1,

// Checkpoints ordered from oldest to newest.
Checkpoints: nil,

// Consensus rule change deployments.
//
// The miner confirmation window is defined as:
Expand Down
Loading