Files
consensus/engine/chain/convergence_hardening_test.go
zeekayandHanzo Dev bba3234534 consensus: a quorum of zero is not a quorum
The chain engine decides on `acceptVotes() >= alpha`, so alpha=0 makes that
predicate hold over an EMPTY accept set. Any arriving vote — including one
AGAINST the block — then marks it accepted with zero accept votes, and Poll's
`rejectVotes() >= alpha` fires on nothing too, rejecting every fresh block and
dropping it from the tips. Two partitioned nodes could settle different blocks
at the same height with no quorum and no cert.

alpha=0 was reachable because Config.Validate had no callers — New,
NewWithConfig, NewWithParams and NewRuntime all reached NewChainConsensus
unchecked — and because Parameters.Valid gated its alpha checks behind
`AlphaPreference != 0`. The one value that destroys safety was the one value
that skipped validation.

Three independent layers close it, each judged by ONE definition of a safe
quorum:

  - config.Parameters.ValidQuorum, split out of Valid, judges K and the two
    integer alphas: 1 <= alpha <= K (unconditional now), AlphaConfidence >=
    AlphaPreference (never enforced before, so preference=15/confidence=1
    passed), and the Byzantine overlap floor, also unconditional. Valid calls
    it, so the two cannot drift. The split lets the engine demand SAFETY
    without also demanding the descriptive Alpha ratio and block timings,
    which have no bearing on whether a quorum can fork.

  - Transitive.Start refuses to run unsafe parameters. The check belongs here,
    not in the constructors: options are applied AFTER config, so WithParams
    can replace params a constructor already validated. Start is the one point
    every engine passes through with its final parameters.

  - NewChainConsensus, the single place the running predicate is armed, pins
    (k, alpha) into k >= 1, 1 <= alpha <= k. A path that skips both checks
    still cannot arm a predicate satisfiable by nothing.

Reclamp gains the safety floor as well. It installs a quorum on a LIVE engine
from a DERIVED validator count, and used to accept any well-formed alpha —
including k=4/alpha=2, where two disjoint quorums overlap in nothing. The
supermajority the committee sizer derives clears the floor at every k, so no
legitimate re-clamp is refused.

Fixtures that encoded unsafe quorums are corrected rather than exempted.
params5 was K=5/alpha=3: two 3-of-5 quorums overlap in a SINGLE node, so one
faulty validator (f=1 at K=5) can certify both sides of a fork. It is now
K=5/alpha=4 — the Byzantine floor, and what FeasibleParams(Local, 5) derives
for the live nets — which makes params5Prod an exact duplicate, so it goes.
Two config cases declared K and Beta with no integer alpha at all and expected
Valid to pass; that is a zero-vote quorum, not an unspecified one. Catch-up
certs carrying four signatures now declare the 4-of-5 threshold they are, and
the forged-signature case declares a real threshold so it still fails on the
forgery rather than on a stale bound.

The single-validator path is untouched: K=1/alpha=1 satisfies the floor, needs
no vote verifier, and bftCommittee still leaves a genuine K=1 preset unclamped.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-01 12:01:23 -07:00

69 lines
3.1 KiB
Go

// Copyright (C) 2019-2026, Lux Industries, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
// convergence_hardening_test.go — white-box gates for the RED-round convergence
// hardening: N4 (operator-tunable settle window) and N1 (proven-loser parent filter).
package chain
import (
"testing"
"time"
"github.com/luxfi/ids"
)
// TestConvergence_SettleWindow_OperatorOverride proves N4: an explicit
// Params.ConvergenceSettleWindow wins over the RoundTO/2 auto value (so a WAN operator can
// lengthen the settle without touching round cadence), and a near-zero misconfiguration is
// FLOORED rather than disabling the settle entirely.
func TestConvergence_SettleWindow_OperatorOverride(t *testing.T) {
vs := newTestValidatorSet(5)
p := params5()
p.ConvergenceSettleWindow = 1500 * time.Millisecond
e, _ := newQuorumEngine(t, p, vs, 0, &recordingGossiper{})
if got := e.convergenceSettleWindow(); got != 1500*time.Millisecond {
t.Fatalf("operator settle override not honored: got %s want 1.5s", got)
}
p2 := params5()
p2.ConvergenceSettleWindow = 5 * time.Millisecond // misconfigured near-zero
e2, _ := newQuorumEngine(t, p2, vs, 0, &recordingGossiper{})
if got := e2.convergenceSettleWindow(); got < 150*time.Millisecond {
t.Fatalf("settle floor not applied to a near-zero override: got %s (must be ≥150ms)", got)
}
}
// TestConvergence_ParentProvenLoser_ExcludesDeadBranch proves N1: a height-H slot whose
// parent LOST its own height's convergence (a strictly-lower-canonical H-1 sibling is
// tracked) is filtered out of the votable set, so this node never wastes its one height-H
// signature on a dead branch — while the untracked finalized-tip parent is NEVER filtered
// (conservative, so the normal path cannot be starved).
func TestConvergence_ParentProvenLoser_ExcludesDeadBranch(t *testing.T) {
vs := newTestValidatorSet(5)
e, _ := newQuorumEngine(t, params5(), vs, 0, &recordingGossiper{})
lowID := ids.ID{0x01} // H-1 sibling with the LOWEST canonical → the winner
highID := ids.ID{0xff} // H-1 sibling with a higher canonical → the loser
hChild := ids.ID{0x77} // an H block extending the LOSER
e.mu.Lock()
e.pendingBlocks[lowID] = &PendingBlock{ConsensusBlock: &Block{id: lowID, parentID: ids.Empty, height: 1, canonicalID: lowID}, ProposedAt: time.Now()}
e.pendingBlocks[highID] = &PendingBlock{ConsensusBlock: &Block{id: highID, parentID: ids.Empty, height: 1, canonicalID: highID}, ProposedAt: time.Now()}
e.pendingBlocks[hChild] = &PendingBlock{ConsensusBlock: &Block{id: hChild, parentID: highID, height: 2, canonicalID: hChild}, ProposedAt: time.Now()}
loserHigh := e.parentIsProvenLoserLocked(highID)
winnerLow := e.parentIsProvenLoserLocked(lowID)
untrackedTip := e.parentIsProvenLoserLocked(ids.Empty)
e.mu.Unlock()
if !loserHigh {
t.Fatal("N1: the higher-canonical H-1 sibling MUST be a proven loser (a lower sibling is tracked)")
}
if winnerLow {
t.Fatal("N1: the lowest-canonical H-1 sibling must NOT be a loser (it is the winner)")
}
if untrackedTip {
t.Fatal("N1: an UNTRACKED parent (finalized tip) must NEVER be filtered — conservative, no over-filter")
}
}