mirror of
https://github.com/luxfi/consensus.git
synced 2026-09-24 00:26:58 +00:00
Owner mandate: decomplect the n=1 finalize, do it right, one path. Supersedes the async band-aid (v1.35.13) with the structural fix. The single-node selfVoter shortcut (integration.go) was the COMPLECTING element: for K==1 it called engine.ReceiveVote from inside RequestVotes while buildBlocksLocked held t.mu, self-deadlocking on the non-reentrant t.mu.RLock (the live n=1 freeze — node hangs at 'single-node mode: self-voting'), and delivered a redundant vote that could sit uncounted in handleVote's not-yet-tracked buffer. REMOVED entirely (field, wiring, RequestVotes branch). A K==1 chain now finalizes SOLELY through the inline build-path finalizer (buildBlocksLocked → buildSingleValidatorCertLocked → acceptWithCertCore) — one finalize path, matching avalanchego's 'a lone validator's own decision finalizes locally.' buildBlocksLocked skips RequestVotes for K==1 (no peers); RequestVotes returns early when the sample resolves to no peers (true no-op, not a query to nobody). Also (RED decentralization follow-up): gate reclampCommitteeLocked AND the buildSingleValidatorCertLocked count>1 synthesize-refusal on a RESOLVED set (GetFinalizedHeight set = ≥1 finalized block), so a fresh single-validator chain that transiently over-reports its validator count cannot wedge its own first block. PROOF through the REAL runtime path: TestBlue_SingleValidator_DecidesThroughFullRuntimePath now drives NewRuntime (real gossiperProposer) 0→1→2 and asserts VM.Accept + height advances — HANGS pre-decomplect (deadlock, 25s timeout), PASSES post. params_test / single_node_test converted to the decomplected invariant (K==1 polls nobody; K>1 polls network). Full engine/chain suite green incl -race. v1.35.14. Co-authored-by: Hanzo Dev <[email protected]>
281 lines
8.1 KiB
Go
281 lines
8.1 KiB
Go
package chain
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/luxfi/ids"
|
|
)
|
|
|
|
// nValidatorSampler returns n distinct validators including self.
|
|
type nValidatorSampler struct {
|
|
self ids.NodeID
|
|
peers []ids.NodeID
|
|
}
|
|
|
|
func makeValidators(n int) (ids.NodeID, *nValidatorSampler) {
|
|
self := ids.GenerateTestNodeID()
|
|
peers := make([]ids.NodeID, 0, n-1)
|
|
for i := 1; i < n; i++ {
|
|
peers = append(peers, ids.GenerateTestNodeID())
|
|
}
|
|
return self, &nValidatorSampler{self: self, peers: peers}
|
|
}
|
|
|
|
func (s *nValidatorSampler) Sample(_ ids.ID, k int) ([]ids.NodeID, error) {
|
|
all := append([]ids.NodeID{s.self}, s.peers...)
|
|
if k > len(all) {
|
|
k = len(all)
|
|
}
|
|
return all[:k], nil
|
|
}
|
|
|
|
func (s *nValidatorSampler) Count(_ ids.ID) int { return 1 + len(s.peers) }
|
|
|
|
// consensusConfig describes a test configuration.
|
|
type consensusConfig struct {
|
|
name string
|
|
k int // sample size
|
|
alpha int // acceptance threshold
|
|
totalValidators int // total validators in network
|
|
acceptVotes int // votes that accept
|
|
rejectVotes int // votes that reject
|
|
expectSelfVote bool // should self-voter fire
|
|
expectNetworkPoll bool // should send to network
|
|
}
|
|
|
|
// TestConsensusConfigurations tests various K/Alpha/validator configurations.
|
|
// Covers: 1/1, 1/2, 2/3, 3/5, 14/21 (67%), 15/20 (75%), 69/100 (69%).
|
|
func TestConsensusConfigurations(t *testing.T) {
|
|
tests := []consensusConfig{
|
|
// K=1: single-node --dev mode. Self-vote, no network poll.
|
|
{
|
|
name: "K=1 single-node (--dev mode)",
|
|
k: 1,
|
|
alpha: 1,
|
|
totalValidators: 1,
|
|
expectSelfVote: true,
|
|
expectNetworkPoll: false,
|
|
},
|
|
// K=2: minimum multi-node. 1 peer needed.
|
|
{
|
|
name: "K=2 two-node (1 peer)",
|
|
k: 2,
|
|
alpha: 2,
|
|
totalValidators: 2,
|
|
expectSelfVote: false,
|
|
expectNetworkPoll: true,
|
|
},
|
|
// K=3, Alpha=2: 2/3 threshold (standard small network).
|
|
{
|
|
name: "K=3 Alpha=2 (2/3 threshold)",
|
|
k: 3,
|
|
alpha: 2,
|
|
totalValidators: 3,
|
|
expectSelfVote: false,
|
|
expectNetworkPoll: true,
|
|
},
|
|
// K=5, Alpha=3: 3/5 threshold.
|
|
{
|
|
name: "K=5 Alpha=3 (3/5 threshold)",
|
|
k: 5,
|
|
alpha: 3,
|
|
totalValidators: 5,
|
|
expectSelfVote: false,
|
|
expectNetworkPoll: true,
|
|
},
|
|
// K=21, Alpha=14: 67% threshold (mainnet minimum).
|
|
{
|
|
name: "K=21 Alpha=14 (67% mainnet min)",
|
|
k: 21,
|
|
alpha: 14,
|
|
totalValidators: 21,
|
|
expectSelfVote: false,
|
|
expectNetworkPoll: true,
|
|
},
|
|
// K=20, Alpha=15: 75% threshold (production default).
|
|
{
|
|
name: "K=20 Alpha=15 (75% production)",
|
|
k: 20,
|
|
alpha: 15,
|
|
totalValidators: 20,
|
|
expectSelfVote: false,
|
|
expectNetworkPoll: true,
|
|
},
|
|
// K=100, Alpha=69: 69% threshold (large validator set).
|
|
{
|
|
name: "K=100 Alpha=69 (69% large set)",
|
|
k: 100,
|
|
alpha: 69,
|
|
totalValidators: 100,
|
|
expectSelfVote: false,
|
|
expectNetworkPoll: true,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
nodeID, sampler := makeValidators(tc.totalValidators)
|
|
gossiper := &testGossiper{}
|
|
|
|
// DECOMPLECTED: there is no single-node self-voter. A K==1 proposer polls NOBODY (no
|
|
// peers) and finalizes via the inline build-path finalizer; a K>1 proposer polls the
|
|
// network. RequestVotes never self-votes.
|
|
proposer := &gossiperProposer{
|
|
gossiper: gossiper,
|
|
chainID: ids.GenerateTestID(),
|
|
networkID: ids.GenerateTestID(),
|
|
validators: sampler,
|
|
nodeID: nodeID,
|
|
k: tc.k,
|
|
}
|
|
|
|
blockID := ids.GenerateTestID()
|
|
err := proposer.RequestVotes(context.Background(), VoteRequest{
|
|
BlockID: blockID,
|
|
BlockData: []byte("test-block"),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("RequestVotes failed: %v", err)
|
|
}
|
|
|
|
_ = tc.expectSelfVote // self-vote path removed; field retained as documentation only
|
|
if tc.expectNetworkPoll && gossiper.pushQueries == 0 {
|
|
t.Error("expected network poll but no PushQuery sent")
|
|
}
|
|
if !tc.expectNetworkPoll && gossiper.pushQueries > 0 {
|
|
t.Errorf("unexpected network poll: %d PushQuery(s) sent", gossiper.pushQueries)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestConsensusAcceptanceThresholds verifies that blocks are accepted only
|
|
// when the acceptance threshold (Alpha) is reached, for various K values.
|
|
func TestConsensusAcceptanceThresholds(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
k int
|
|
alpha int
|
|
beta int
|
|
votes int // number of accept votes
|
|
shouldFinal bool // should reach finality
|
|
}{
|
|
// K=1: 1 vote = accept.
|
|
{"K=1 1-vote accept", 1, 1, 1, 1, true},
|
|
// K=3: need 2 votes.
|
|
{"K=3 1-vote no-accept", 3, 2, 1, 1, false},
|
|
{"K=3 2-vote accept", 3, 2, 1, 2, true},
|
|
{"K=3 3-vote accept", 3, 2, 1, 3, true},
|
|
// K=5: need 3 votes.
|
|
{"K=5 2-vote no-accept", 5, 3, 1, 2, false},
|
|
{"K=5 3-vote accept", 5, 3, 1, 3, true},
|
|
// K=21: need 14 votes (67%).
|
|
{"K=21 13-vote no-accept", 21, 14, 1, 13, false},
|
|
{"K=21 14-vote accept", 21, 14, 1, 14, true},
|
|
// K=20: need 15 votes (75%).
|
|
{"K=20 14-vote no-accept", 20, 15, 1, 14, false},
|
|
{"K=20 15-vote accept", 20, 15, 1, 15, true},
|
|
// K=100: need 69 votes (69%).
|
|
{"K=100 68-vote no-accept", 100, 69, 1, 68, false},
|
|
{"K=100 69-vote accept", 100, 69, 1, 69, true},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
consensus := NewChainConsensus(tc.k, tc.alpha, tc.beta)
|
|
blockID := ids.GenerateTestID()
|
|
parentID := ids.GenerateTestID()
|
|
ctx := context.Background()
|
|
|
|
block := &Block{
|
|
id: blockID,
|
|
parentID: parentID,
|
|
height: 1,
|
|
}
|
|
|
|
if err := consensus.AddBlock(ctx, block); err != nil {
|
|
t.Fatalf("AddBlock failed: %v", err)
|
|
}
|
|
|
|
// Submit votes.
|
|
for i := 0; i < tc.votes; i++ {
|
|
if err := consensus.ProcessVote(ctx, blockID, true); err != nil {
|
|
t.Fatalf("ProcessVote %d failed: %v", i, err)
|
|
}
|
|
}
|
|
|
|
// Poll to trigger finalization check.
|
|
if err := consensus.Poll(ctx, map[ids.ID]int{blockID: tc.votes}); err != nil {
|
|
t.Fatalf("Poll failed: %v", err)
|
|
}
|
|
|
|
accepted := consensus.IsAccepted(blockID)
|
|
if tc.shouldFinal && !accepted {
|
|
t.Errorf("expected block accepted with %d/%d votes (alpha=%d) but not accepted",
|
|
tc.votes, tc.k, tc.alpha)
|
|
}
|
|
if !tc.shouldFinal && accepted {
|
|
t.Errorf("block accepted with %d/%d votes (alpha=%d) but should NOT be accepted",
|
|
tc.votes, tc.k, tc.alpha)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestConsensusRejection verifies that blocks are rejected when reject
|
|
// votes reach the alpha threshold.
|
|
func TestConsensusRejection(t *testing.T) {
|
|
consensus := NewChainConsensus(5, 3, 1)
|
|
blockID := ids.GenerateTestID()
|
|
ctx := context.Background()
|
|
|
|
block := &Block{id: blockID, parentID: ids.GenerateTestID(), height: 1}
|
|
if err := consensus.AddBlock(ctx, block); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// 3 reject votes = alpha threshold.
|
|
for i := 0; i < 3; i++ {
|
|
consensus.ProcessVote(ctx, blockID, false)
|
|
}
|
|
consensus.Poll(ctx, map[ids.ID]int{blockID: 0})
|
|
|
|
if consensus.IsAccepted(blockID) {
|
|
t.Error("block should NOT be accepted")
|
|
}
|
|
if !consensus.IsRejected(blockID) {
|
|
t.Error("block should be rejected with 3 reject votes (alpha=3)")
|
|
}
|
|
}
|
|
|
|
// TestSelfVoteOnlyForK1 ensures self-voting is ONLY created for K=1.
|
|
// This is critical — multi-node networks MUST use network consensus.
|
|
func TestSelfVoteOnlyForK1(t *testing.T) {
|
|
for _, k := range []int{2, 3, 5, 10, 20, 21, 100} {
|
|
t.Run("K="+string(rune('0'+k/10))+string(rune('0'+k%10)), func(t *testing.T) {
|
|
nodeID, sampler := makeValidators(k)
|
|
gossiper := &testGossiper{}
|
|
|
|
// DECOMPLECTED: no self-voter exists. A K>1 proposer polls the network.
|
|
proposer := &gossiperProposer{
|
|
gossiper: gossiper,
|
|
chainID: ids.GenerateTestID(),
|
|
networkID: ids.GenerateTestID(),
|
|
validators: sampler,
|
|
nodeID: nodeID,
|
|
k: k,
|
|
}
|
|
|
|
_ = proposer.RequestVotes(context.Background(), VoteRequest{
|
|
BlockID: ids.GenerateTestID(),
|
|
BlockData: []byte("block"),
|
|
})
|
|
|
|
if gossiper.pushQueries == 0 {
|
|
t.Fatalf("K=%d should send network queries", k)
|
|
}
|
|
})
|
|
}
|
|
}
|