2016-04-25 11:12:09 +02:00
|
|
|
// Copyright (c) 2016-2017 The btcsuite developers
|
2016-08-29 00:51:55 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package blockchain
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-20 01:18:57 +01:00
|
|
|
"time"
|
2016-08-29 00:51:55 +02:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2021-03-20 01:18:57 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2016-08-29 00:51:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// ThresholdState define the various threshold states used when voting on
|
|
|
|
// consensus changes.
|
|
|
|
type ThresholdState byte
|
|
|
|
|
|
|
|
// These constants are used to identify specific threshold states.
|
|
|
|
const (
|
|
|
|
// ThresholdDefined is the first state for each deployment and is the
|
2017-05-30 16:59:51 +02:00
|
|
|
// state for the genesis block has by definition for all deployments.
|
2017-02-02 09:44:22 +01:00
|
|
|
ThresholdDefined ThresholdState = iota
|
2016-08-29 00:51:55 +02:00
|
|
|
|
|
|
|
// ThresholdStarted is the state for a deployment once its start time
|
|
|
|
// has been reached.
|
2017-02-02 09:44:22 +01:00
|
|
|
ThresholdStarted
|
2016-08-29 00:51:55 +02:00
|
|
|
|
|
|
|
// ThresholdLockedIn is the state for a deployment during the retarget
|
|
|
|
// period which is after the ThresholdStarted state period and the
|
|
|
|
// number of blocks that have voted for the deployment equal or exceed
|
|
|
|
// the required number of votes for the deployment.
|
2017-02-02 09:44:22 +01:00
|
|
|
ThresholdLockedIn
|
2016-08-29 00:51:55 +02:00
|
|
|
|
|
|
|
// ThresholdActive is the state for a deployment for all blocks after a
|
|
|
|
// retarget period in which the deployment was in the ThresholdLockedIn
|
|
|
|
// state.
|
2017-02-02 09:44:22 +01:00
|
|
|
ThresholdActive
|
2016-08-29 00:51:55 +02:00
|
|
|
|
|
|
|
// ThresholdFailed is the state for a deployment once its expiration
|
|
|
|
// time has been reached and it did not reach the ThresholdLockedIn
|
|
|
|
// state.
|
2017-02-02 09:44:22 +01:00
|
|
|
ThresholdFailed
|
2016-08-29 00:51:55 +02:00
|
|
|
|
|
|
|
// numThresholdsStates is the maximum number of threshold states used in
|
|
|
|
// tests.
|
2017-02-02 09:44:22 +01:00
|
|
|
numThresholdsStates
|
2016-08-29 00:51:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// thresholdStateStrings is a map of ThresholdState values back to their
|
|
|
|
// constant names for pretty printing.
|
|
|
|
var thresholdStateStrings = map[ThresholdState]string{
|
|
|
|
ThresholdDefined: "ThresholdDefined",
|
|
|
|
ThresholdStarted: "ThresholdStarted",
|
|
|
|
ThresholdLockedIn: "ThresholdLockedIn",
|
|
|
|
ThresholdActive: "ThresholdActive",
|
|
|
|
ThresholdFailed: "ThresholdFailed",
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the ThresholdState as a human-readable name.
|
|
|
|
func (t ThresholdState) String() string {
|
|
|
|
if s := thresholdStateStrings[t]; s != "" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Unknown ThresholdState (%d)", int(t))
|
|
|
|
}
|
|
|
|
|
|
|
|
// thresholdConditionChecker provides a generic interface that is invoked to
|
|
|
|
// determine when a consensus rule change threshold should be changed.
|
|
|
|
type thresholdConditionChecker interface {
|
2021-03-20 01:18:57 +01:00
|
|
|
// HasStarted returns true if based on the passed block blockNode the
|
|
|
|
// consensus is eligible for deployment.
|
|
|
|
HasStarted(*blockNode) bool
|
2016-08-29 00:51:55 +02:00
|
|
|
|
2022-01-14 02:42:07 +01:00
|
|
|
// HasEnded returns true if the target consensus rule change has
|
|
|
|
// expired or timed out.
|
2021-03-20 01:18:57 +01:00
|
|
|
HasEnded(*blockNode) bool
|
2016-08-29 00:51:55 +02:00
|
|
|
|
|
|
|
// RuleChangeActivationThreshold is the number of blocks for which the
|
|
|
|
// condition must be true in order to lock in a rule change.
|
|
|
|
RuleChangeActivationThreshold() uint32
|
|
|
|
|
|
|
|
// MinerConfirmationWindow is the number of blocks in each threshold
|
|
|
|
// state retarget window.
|
|
|
|
MinerConfirmationWindow() uint32
|
|
|
|
|
2022-01-14 02:42:07 +01:00
|
|
|
// EligibleToActivate returns true if a custom deployment can
|
|
|
|
// transition from the LockedIn to the Active state. For normal
|
|
|
|
// deployments, this always returns true. However, some deployments add
|
|
|
|
// extra rules like a minimum activation height, which can be
|
|
|
|
// abstracted into a generic arbitrary check at the final state via
|
|
|
|
// this method.
|
|
|
|
EligibleToActivate(*blockNode) bool
|
|
|
|
|
|
|
|
// IsSpeedy returns true if this is to be a "speedy" deployment. A
|
|
|
|
// speedy deployment differs from a regular one in that only after a
|
|
|
|
// miner block confirmation window can the deployment expire.
|
|
|
|
IsSpeedy() bool
|
|
|
|
|
|
|
|
// Condition returns whether or not the rule change activation
|
|
|
|
// condition has been met. This typically involves checking whether or
|
|
|
|
// not the bit associated with the condition is set, but can be more
|
|
|
|
// complex as needed.
|
2016-08-29 00:51:55 +02:00
|
|
|
Condition(*blockNode) (bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// thresholdStateCache provides a type to cache the threshold states of each
|
2017-02-02 09:44:22 +01:00
|
|
|
// threshold window for a set of IDs.
|
2016-08-29 00:51:55 +02:00
|
|
|
type thresholdStateCache struct {
|
2017-02-02 09:44:22 +01:00
|
|
|
entries map[chainhash.Hash]ThresholdState
|
2016-08-29 00:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup returns the threshold state associated with the given hash along with
|
|
|
|
// a boolean that indicates whether or not it is valid.
|
2017-02-01 22:25:25 +01:00
|
|
|
func (c *thresholdStateCache) Lookup(hash *chainhash.Hash) (ThresholdState, bool) {
|
|
|
|
state, ok := c.entries[*hash]
|
2016-08-29 00:51:55 +02:00
|
|
|
return state, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update updates the cache to contain the provided hash to threshold state
|
2017-02-02 09:44:22 +01:00
|
|
|
// mapping.
|
2017-02-01 22:25:25 +01:00
|
|
|
func (c *thresholdStateCache) Update(hash *chainhash.Hash, state ThresholdState) {
|
|
|
|
c.entries[*hash] = state
|
2016-08-29 00:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// newThresholdCaches returns a new array of caches to be used when calculating
|
|
|
|
// threshold states.
|
|
|
|
func newThresholdCaches(numCaches uint32) []thresholdStateCache {
|
|
|
|
caches := make([]thresholdStateCache, numCaches)
|
|
|
|
for i := 0; i < len(caches); i++ {
|
|
|
|
caches[i] = thresholdStateCache{
|
2017-02-02 09:44:22 +01:00
|
|
|
entries: make(map[chainhash.Hash]ThresholdState),
|
2016-08-29 00:51:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return caches
|
|
|
|
}
|
|
|
|
|
2021-03-20 01:18:57 +01:00
|
|
|
// PastMedianTime returns the past median time from the PoV of the passed block
|
|
|
|
// header. The past median time is the median time of the 11 blocks prior to
|
|
|
|
// the passed block header.
|
|
|
|
//
|
|
|
|
// NOTE: This is part of the chainfg.BlockClock interface
|
|
|
|
func (b *BlockChain) PastMedianTime(blockHeader *wire.BlockHeader) (time.Time, error) {
|
|
|
|
prevHash := blockHeader.PrevBlock
|
|
|
|
prevNode := b.index.LookupNode(&prevHash)
|
|
|
|
|
|
|
|
// If we can't find the previous node, then we can't compute the block
|
|
|
|
// time since it requires us to walk backwards from this node.
|
|
|
|
if prevNode == nil {
|
|
|
|
return time.Time{}, fmt.Errorf("blockHeader(%v) has no "+
|
|
|
|
"previous node", blockHeader.BlockHash())
|
|
|
|
}
|
|
|
|
|
|
|
|
blockNode := newBlockNode(blockHeader, prevNode)
|
|
|
|
|
|
|
|
return blockNode.CalcPastMedianTime(), nil
|
|
|
|
}
|
|
|
|
|
2022-01-24 04:06:21 +01:00
|
|
|
// thresholdStateTransition given a state, a previous node, and a toeholds
|
|
|
|
// checker, this function transitions to the next state as defined by BIP 009.
|
|
|
|
// This state transition function is also aware of the "speedy trial"
|
|
|
|
// modifications made to BIP 0009 as part of the taproot softfork activation.
|
|
|
|
func thresholdStateTransition(state ThresholdState, prevNode *blockNode,
|
|
|
|
checker thresholdConditionChecker,
|
|
|
|
confirmationWindow int32) (ThresholdState, error) {
|
|
|
|
|
|
|
|
switch state {
|
|
|
|
case ThresholdDefined:
|
|
|
|
// The deployment of the rule change fails if it
|
|
|
|
// expires before it is accepted and locked in. However
|
|
|
|
// speed deployments can only transition to failed
|
|
|
|
// after a confirmation window.
|
|
|
|
if !checker.IsSpeedy() && checker.HasEnded(prevNode) {
|
|
|
|
state = ThresholdFailed
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// The state for the rule moves to the started state
|
|
|
|
// once its start time has been reached (and it hasn't
|
|
|
|
// already expired per the above).
|
|
|
|
if checker.HasStarted(prevNode) {
|
|
|
|
state = ThresholdStarted
|
|
|
|
}
|
|
|
|
|
|
|
|
case ThresholdStarted:
|
|
|
|
// The deployment of the rule change fails if it
|
|
|
|
// expires before it is accepted and locked in, but
|
|
|
|
// only if this deployment isn't speedy.
|
|
|
|
if !checker.IsSpeedy() && checker.HasEnded(prevNode) {
|
|
|
|
state = ThresholdFailed
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, the rule change is still being voted
|
|
|
|
// on by the miners, so iterate backwards through the
|
|
|
|
// confirmation window to count all of the votes in it.
|
|
|
|
var count uint32
|
|
|
|
countNode := prevNode
|
|
|
|
for i := int32(0); i < confirmationWindow; i++ {
|
|
|
|
condition, err := checker.Condition(countNode)
|
|
|
|
if err != nil {
|
|
|
|
return ThresholdFailed, err
|
|
|
|
}
|
|
|
|
if condition {
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the previous block node.
|
|
|
|
countNode = countNode.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
// The state is locked in if the number of blocks in the
|
|
|
|
// period that voted for the rule change meets the
|
|
|
|
// activation threshold.
|
|
|
|
case count >= checker.RuleChangeActivationThreshold():
|
|
|
|
state = ThresholdLockedIn
|
|
|
|
|
|
|
|
// If this is a speedy deployment, we didn't meet the
|
|
|
|
// threshold above, and the deployment has expired, then
|
|
|
|
// we transition to failed.
|
|
|
|
case checker.IsSpeedy() && checker.HasEnded(prevNode):
|
|
|
|
state = ThresholdFailed
|
|
|
|
}
|
|
|
|
|
|
|
|
case ThresholdLockedIn:
|
|
|
|
// At this point, we'll consult the deployment see if a
|
|
|
|
// custom deployment has any other arbitrary conditions
|
|
|
|
// that need to pass before execution. This might be a
|
|
|
|
// minimum activation height or another policy.
|
|
|
|
//
|
|
|
|
// If we aren't eligible to active yet, then we'll just
|
|
|
|
// stay in the locked in position.
|
|
|
|
if !checker.EligibleToActivate(prevNode) {
|
|
|
|
state = ThresholdLockedIn
|
|
|
|
} else {
|
|
|
|
// The new rule becomes active when its
|
|
|
|
// previous state was locked in assuming it's
|
|
|
|
// now eligible to activate.
|
|
|
|
state = ThresholdActive
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing to do if the previous state is active or failed since
|
|
|
|
// they are both terminal states.
|
|
|
|
case ThresholdActive:
|
|
|
|
case ThresholdFailed:
|
|
|
|
}
|
|
|
|
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
2016-12-03 05:54:04 +01:00
|
|
|
// thresholdState returns the current rule change threshold state for the block
|
|
|
|
// AFTER the given node and deployment ID. The cache is used to ensure the
|
|
|
|
// threshold states for previous windows are only calculated once.
|
2016-08-29 00:51:55 +02:00
|
|
|
//
|
|
|
|
// This function MUST be called with the chain state lock held (for writes).
|
|
|
|
func (b *BlockChain) thresholdState(prevNode *blockNode, checker thresholdConditionChecker, cache *thresholdStateCache) (ThresholdState, error) {
|
|
|
|
// The threshold state for the window that contains the genesis block is
|
|
|
|
// defined by definition.
|
|
|
|
confirmationWindow := int32(checker.MinerConfirmationWindow())
|
|
|
|
if prevNode == nil || (prevNode.height+1) < confirmationWindow {
|
|
|
|
return ThresholdDefined, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the ancestor that is the last block of the previous confirmation
|
|
|
|
// window in order to get its threshold state. This can be done because
|
|
|
|
// the state is the same for all blocks within a given window.
|
2017-02-03 19:13:53 +01:00
|
|
|
prevNode = prevNode.Ancestor(prevNode.height -
|
2016-08-29 00:51:55 +02:00
|
|
|
(prevNode.height+1)%confirmationWindow)
|
|
|
|
|
|
|
|
// Iterate backwards through each of the previous confirmation windows
|
|
|
|
// to find the most recently cached threshold state.
|
|
|
|
var neededStates []*blockNode
|
|
|
|
for prevNode != nil {
|
|
|
|
// Nothing more to do if the state of the block is already
|
|
|
|
// cached.
|
2017-02-01 22:25:25 +01:00
|
|
|
if _, ok := cache.Lookup(&prevNode.hash); ok {
|
2016-08-29 00:51:55 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// The state is simply defined if the start time hasn't been
|
|
|
|
// been reached yet.
|
2021-03-20 01:18:57 +01:00
|
|
|
if !checker.HasStarted(prevNode) {
|
2017-02-01 22:25:25 +01:00
|
|
|
cache.Update(&prevNode.hash, ThresholdDefined)
|
2016-08-29 00:51:55 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add this node to the list of nodes that need the state
|
|
|
|
// calculated and cached.
|
|
|
|
neededStates = append(neededStates, prevNode)
|
|
|
|
|
|
|
|
// Get the ancestor that is the last block of the previous
|
|
|
|
// confirmation window.
|
2017-02-03 19:13:53 +01:00
|
|
|
prevNode = prevNode.RelativeAncestor(confirmationWindow)
|
2016-08-29 00:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start with the threshold state for the most recent confirmation
|
|
|
|
// window that has a cached state.
|
|
|
|
state := ThresholdDefined
|
|
|
|
if prevNode != nil {
|
|
|
|
var ok bool
|
2017-02-01 22:25:25 +01:00
|
|
|
state, ok = cache.Lookup(&prevNode.hash)
|
2016-08-29 00:51:55 +02:00
|
|
|
if !ok {
|
|
|
|
return ThresholdFailed, AssertError(fmt.Sprintf(
|
|
|
|
"thresholdState: cache lookup failed for %v",
|
|
|
|
prevNode.hash))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since each threshold state depends on the state of the previous
|
|
|
|
// window, iterate starting from the oldest unknown window.
|
2022-01-24 04:06:21 +01:00
|
|
|
var err error
|
2016-08-29 00:51:55 +02:00
|
|
|
for neededNum := len(neededStates) - 1; neededNum >= 0; neededNum-- {
|
|
|
|
prevNode := neededStates[neededNum]
|
|
|
|
|
2022-01-24 04:06:21 +01:00
|
|
|
// Based on the current state, the previous node, and the
|
|
|
|
// condition checker, transition to the next threshold state.
|
|
|
|
state, err = thresholdStateTransition(
|
|
|
|
state, prevNode, checker, confirmationWindow,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return state, err
|
2016-08-29 00:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the cache to avoid recalculating the state in the
|
|
|
|
// future.
|
2017-02-01 22:25:25 +01:00
|
|
|
cache.Update(&prevNode.hash, state)
|
2016-08-29 00:51:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return state, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ThresholdState returns the current rule change threshold state of the given
|
2016-04-25 11:12:09 +02:00
|
|
|
// deployment ID for the block AFTER the end of the current best chain.
|
2016-08-29 00:51:55 +02:00
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) ThresholdState(deploymentID uint32) (ThresholdState, error) {
|
2016-09-27 04:00:28 +02:00
|
|
|
b.chainLock.Lock()
|
2017-08-18 14:25:54 +02:00
|
|
|
state, err := b.deploymentState(b.bestChain.Tip(), deploymentID)
|
2016-09-27 04:00:28 +02:00
|
|
|
b.chainLock.Unlock()
|
|
|
|
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
|
2016-10-19 01:54:55 +02:00
|
|
|
// IsDeploymentActive returns true if the target deploymentID is active, and
|
|
|
|
// false otherwise.
|
|
|
|
//
|
|
|
|
// This function is safe for concurrent access.
|
|
|
|
func (b *BlockChain) IsDeploymentActive(deploymentID uint32) (bool, error) {
|
|
|
|
b.chainLock.Lock()
|
2017-08-18 14:25:54 +02:00
|
|
|
state, err := b.deploymentState(b.bestChain.Tip(), deploymentID)
|
2016-10-19 01:54:55 +02:00
|
|
|
b.chainLock.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return state == ThresholdActive, nil
|
|
|
|
}
|
|
|
|
|
2016-09-27 04:00:28 +02:00
|
|
|
// deploymentState returns the current rule change threshold for a given
|
|
|
|
// deploymentID. The threshold is evaluated from the point of view of the block
|
|
|
|
// node passed in as the first argument to this method.
|
|
|
|
//
|
|
|
|
// It is important to note that, as the variable name indicates, this function
|
|
|
|
// expects the block node prior to the block for which the deployment state is
|
|
|
|
// desired. In other words, the returned deployment state is for the block
|
|
|
|
// AFTER the passed node.
|
|
|
|
//
|
|
|
|
// This function MUST be called with the chain state lock held (for writes).
|
2017-02-03 19:13:53 +01:00
|
|
|
func (b *BlockChain) deploymentState(prevNode *blockNode, deploymentID uint32) (ThresholdState, error) {
|
2016-08-29 00:51:55 +02:00
|
|
|
if deploymentID > uint32(len(b.chainParams.Deployments)) {
|
|
|
|
return ThresholdFailed, DeploymentError(deploymentID)
|
|
|
|
}
|
2016-09-27 04:00:28 +02:00
|
|
|
|
2016-08-29 00:51:55 +02:00
|
|
|
deployment := &b.chainParams.Deployments[deploymentID]
|
|
|
|
checker := deploymentChecker{deployment: deployment, chain: b}
|
|
|
|
cache := &b.deploymentCaches[deploymentID]
|
2016-09-27 04:00:28 +02:00
|
|
|
|
|
|
|
return b.thresholdState(prevNode, checker, cache)
|
2016-08-29 00:51:55 +02:00
|
|
|
}
|
2017-02-02 09:44:22 +01:00
|
|
|
|
|
|
|
// initThresholdCaches initializes the threshold state caches for each warning
|
|
|
|
// bit and defined deployment and provides warnings if the chain is current per
|
2019-08-27 04:09:13 +02:00
|
|
|
// the warnUnknownRuleActivations function.
|
2017-02-02 09:44:22 +01:00
|
|
|
func (b *BlockChain) initThresholdCaches() error {
|
|
|
|
// Initialize the warning and deployment caches by calculating the
|
|
|
|
// threshold state for each of them. This will ensure the caches are
|
|
|
|
// populated and any states that needed to be recalculated due to
|
|
|
|
// definition changes is done now.
|
2017-08-18 14:25:54 +02:00
|
|
|
prevNode := b.bestChain.Tip().parent
|
2017-02-02 09:44:22 +01:00
|
|
|
for bit := uint32(0); bit < vbNumBits; bit++ {
|
|
|
|
checker := bitConditionChecker{bit: bit, chain: b}
|
|
|
|
cache := &b.warningCaches[bit]
|
|
|
|
_, err := b.thresholdState(prevNode, checker, cache)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for id := 0; id < len(b.chainParams.Deployments); id++ {
|
|
|
|
deployment := &b.chainParams.Deployments[id]
|
|
|
|
cache := &b.deploymentCaches[id]
|
|
|
|
checker := deploymentChecker{deployment: deployment, chain: b}
|
|
|
|
_, err := b.thresholdState(prevNode, checker, cache)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 04:09:13 +02:00
|
|
|
// No warnings about unknown rules until the chain is current.
|
2017-02-02 09:44:22 +01:00
|
|
|
if b.isCurrent() {
|
2017-08-18 14:25:54 +02:00
|
|
|
bestNode := b.bestChain.Tip()
|
2017-02-02 09:44:22 +01:00
|
|
|
|
|
|
|
// Warn if any unknown new rules are either about to activate or
|
|
|
|
// have already been activated.
|
2017-08-18 14:25:54 +02:00
|
|
|
if err := b.warnUnknownRuleActivations(bestNode); err != nil {
|
2017-02-02 09:44:22 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|