Skip to content

Commit

Permalink
blockchain: fix IsSpeedy() bug, add more logging in BIP 9 state machine
Browse files Browse the repository at this point in the history
  • Loading branch information
Roasbeef committed Mar 16, 2022
1 parent f7f7bb3 commit 6ab97a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
25 changes: 25 additions & 0 deletions blockchain/thresholdstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ func thresholdStateTransition(state ThresholdState, prevNode *blockNode,
// speed deployments can only transition to failed
// after a confirmation window.
if !checker.IsSpeedy() && checker.HasEnded(prevNode) {
log.Infof("Moving from state=%v, to state=%v", state,
ThresholdFailed)

state = ThresholdFailed
break
}
Expand All @@ -179,6 +182,9 @@ func thresholdStateTransition(state ThresholdState, prevNode *blockNode,
// once its start time has been reached (and it hasn't
// already expired per the above).
if checker.HasStarted(prevNode) {
log.Infof("Moving from state=%v, to state=%v", state,
ThresholdStarted)

state = ThresholdStarted
}

Expand All @@ -187,6 +193,9 @@ func thresholdStateTransition(state ThresholdState, prevNode *blockNode,
// expires before it is accepted and locked in, but
// only if this deployment isn't speedy.
if !checker.IsSpeedy() && checker.HasEnded(prevNode) {
log.Infof("Moving from state=%v, to state=%v", state,
ThresholdFailed)

state = ThresholdFailed
break
}
Expand Down Expand Up @@ -214,13 +223,23 @@ func thresholdStateTransition(state ThresholdState, prevNode *blockNode,
// period that voted for the rule change meets the
// activation threshold.
case count >= checker.RuleChangeActivationThreshold():
log.Infof("Moving from state=%v, to state=%v", state,
ThresholdLockedIn)

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):
log.Infof("Moving from state=%v, to state=%v", state,
ThresholdFailed)

state = ThresholdFailed

default:
log.Infof("Still at state=%v, threshold=%v", state,
float64(count)/float64(checker.RuleChangeActivationThreshold()))
}

case ThresholdLockedIn:
Expand All @@ -232,8 +251,14 @@ func thresholdStateTransition(state ThresholdState, prevNode *blockNode,
// If we aren't eligible to active yet, then we'll just
// stay in the locked in position.
if !checker.EligibleToActivate(prevNode) {
log.Infof("Moving from state=%v, to state=%v", state,
ThresholdLockedIn)

state = ThresholdLockedIn
} else {
log.Infof("Moving from state=%v, to state=%v", state,
ThresholdActive)

// The new rule becomes active when its
// previous state was locked in assuming it's
// now eligible to activate.
Expand Down
5 changes: 3 additions & 2 deletions blockchain/versionbits.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (c deploymentChecker) HasEnded(blkNode *blockNode) bool {
// This is part of the thresholdConditionChecker interface implementation.
func (c deploymentChecker) RuleChangeActivationThreshold() uint32 {
// Some deployments like taproot used a custom activation threshold
// that ovverides the network level threshold.
// that overrides the network level threshold.
if c.deployment.CustomActivationThreshold != 0 {
return c.deployment.CustomActivationThreshold
}
Expand Down Expand Up @@ -234,7 +234,8 @@ func (c deploymentChecker) EligibleToActivate(blkNode *blockNode) bool {
//
// This is part of the thresholdConditionChecker interface implementation.
func (c deploymentChecker) IsSpeedy() bool {
return c.deployment.MinActivationHeight != 0
return (c.deployment.MinActivationHeight != 0 ||
c.deployment.CustomActivationThreshold != 0)
}

// Condition returns true when the specific bit defined by the deployment
Expand Down

0 comments on commit 6ab97a3

Please sign in to comment.