mirror of
https://github.com/luxfi/consensus.git
synced 2026-09-24 00:26:58 +00:00
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]>
135 lines
3.1 KiB
Go
135 lines
3.1 KiB
Go
package config
|
||
|
||
import (
|
||
"math"
|
||
"testing"
|
||
)
|
||
|
||
// TestConsensusThresholds verifies thresholds are properly configured
|
||
func TestConsensusThresholds(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
params Parameters
|
||
k int
|
||
alpha int
|
||
alphaF float64
|
||
minRatio float64
|
||
}{
|
||
{
|
||
name: "DefaultParams",
|
||
params: DefaultParams(),
|
||
k: 20,
|
||
alpha: 14, // 70% of 20
|
||
alphaF: 0.69, // 69% threshold
|
||
minRatio: 0.69,
|
||
},
|
||
{
|
||
name: "MainnetParams",
|
||
params: MainnetParams(),
|
||
k: 21,
|
||
alpha: 15, // ~71% of 21
|
||
alphaF: 0.69, // 69% threshold
|
||
minRatio: 0.69,
|
||
},
|
||
{
|
||
name: "TestnetParams",
|
||
params: TestnetParams(),
|
||
k: 11,
|
||
alpha: 8, // ~73% of 11
|
||
alphaF: 0.69, // 69% threshold
|
||
minRatio: 0.69,
|
||
},
|
||
{
|
||
name: "LocalParams",
|
||
params: LocalParams(),
|
||
k: 3,
|
||
alpha: 2, // 2/3 of 3
|
||
alphaF: 0.67, // 2/3 threshold
|
||
minRatio: 0.66,
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
// Check K value
|
||
if tt.params.K != tt.k {
|
||
t.Errorf("K = %d, want %d", tt.params.K, tt.k)
|
||
}
|
||
|
||
// Check Alpha matches expected threshold
|
||
if tt.params.Alpha != tt.alphaF {
|
||
t.Errorf("Alpha = %f, want %f", tt.params.Alpha, tt.alphaF)
|
||
}
|
||
|
||
// Check AlphaPreference matches
|
||
if tt.params.AlphaPreference != tt.alpha {
|
||
t.Errorf("AlphaPreference = %d, want %d", tt.params.AlphaPreference, tt.alpha)
|
||
}
|
||
|
||
// Verify AlphaPreference meets minimum ratio of K
|
||
minAlpha := math.Ceil(float64(tt.params.K) * tt.minRatio)
|
||
if float64(tt.params.AlphaPreference) < minAlpha {
|
||
t.Errorf("AlphaPreference %d is below %.0f%% threshold %f",
|
||
tt.params.AlphaPreference, tt.minRatio*100, minAlpha)
|
||
}
|
||
|
||
// Check that parameters are valid
|
||
if err := tt.params.Valid(); err != nil {
|
||
t.Errorf("Parameters validation failed: %v", err)
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// TestParameterValidation verifies parameter validation with 69% threshold
|
||
func TestParameterValidation(t *testing.T) {
|
||
tests := []struct {
|
||
name string
|
||
params Parameters
|
||
expectedErr error
|
||
}{
|
||
{
|
||
// The integer α is REQUIRED, not optional: AlphaPreference is what the
|
||
// chain engine counts toward finality, so omitting it (leaving 0) is a
|
||
// quorum of zero, not "unspecified". K=20 → α=14 (the DefaultParams
|
||
// tuning, at the BFT floor for K=20).
|
||
name: "Valid 69% threshold",
|
||
params: Parameters{
|
||
K: 20,
|
||
Alpha: 0.69,
|
||
Beta: 14,
|
||
AlphaPreference: 14,
|
||
AlphaConfidence: 14,
|
||
},
|
||
expectedErr: nil,
|
||
},
|
||
{
|
||
name: "Below 2/3 threshold",
|
||
params: Parameters{
|
||
K: 20,
|
||
Alpha: 0.65, // Below minimum 2/3 threshold
|
||
Beta: 14,
|
||
},
|
||
expectedErr: ErrInvalidAlpha,
|
||
},
|
||
{
|
||
name: "Invalid K",
|
||
params: Parameters{
|
||
K: 0,
|
||
Alpha: 0.69,
|
||
Beta: 14,
|
||
},
|
||
expectedErr: ErrInvalidK,
|
||
},
|
||
}
|
||
|
||
for _, tt := range tests {
|
||
t.Run(tt.name, func(t *testing.T) {
|
||
err := tt.params.Valid()
|
||
if err != tt.expectedErr {
|
||
t.Errorf("Valid() error = %v, want %v", err, tt.expectedErr)
|
||
}
|
||
})
|
||
}
|
||
}
|