Files
zeekayandHanzo Dev 974d0d5422 rename: drop the upstream protocol name from the tree
The module is luxfi/bft and that is now the only name it answers to.

- simplex/ -> engine/. Upstream's module is named for the protocol, so its root
  package and its engine package shared that name; under luxfi/bft the engine
  package would have collided with the module's own root package. It is the
  consensus engine, so engine.Epoch and engine.NewEpoch read without stutter.
- Identifiers keep the qualifier where it carries meaning and only change the
  word: a block produced by this protocol as against a pre-existing chain's block
  is a real distinction, so makeNonSimplexBlock became makeNonBFTBlock,
  lastNonSimplexInnerBlockHeight became lastNonBFTInnerBlockHeight,
  newSnowToSimplexChain became newSnowToBFTChain. Dropping the qualifier outright
  would have left names that assert nothing.
- The canoto-tagged fields of StateMachineMetadata lose the prefix entirely:
  EpochInfo, ProtocolMetadata, Blacklist. The wire format is unchanged — canoto
  keys off the tag numbers, which still read 1,2,3,4,5, and the generated
  .canoto.go files were renamed in step with the source. The msm fuzz and
  encoding round-trip tests cover this.
- Prose cites the protocol paper by reference number instead of by name, so the
  origin stays findable without the name. The HyperSDK sentence still says
  Avalanche because that sentence is about Avalanche's VM and is true.

Also here, from review of the logger port:
- monitor_test's Fatal panics instead of returning. It reported a state the
  caller had declared impossible and then carried on regardless, which made it
  the one Fatal in the tree that was weaker than the original.
- TestInstanceNonValidatorBootstraps asserts the property it is named for — that
  the node was still a non-validator when the bootstrap finished — rather than
  counting how many times a debug line was emitted. The count tracks how often
  onEpochChange happened to observe the branch, so transitions can coalesce; it
  measured 2 here where it had been reliably 3, and a logger-cost explanation was
  tested and refuted. The count never checked the property either way: a node
  that wrongly became a validator mid-bootstrap would still have counted 3.
- SilenceExceptKeywords documents last-call-wins, and Trace/Verbo now reaching
  hooks is recorded where a future hook author will see it.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-06 05:16:49 -07:00

477 lines
13 KiB
Go

// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package engine_test
import (
"context"
"fmt"
"testing"
"time"
. "github.com/luxfi/bft/common"
. "github.com/luxfi/bft/engine"
"github.com/luxfi/bft/testutil"
"github.com/stretchr/testify/require"
)
func TestRetrieveFromStorage(t *testing.T) {
block := testutil.NewTestBlock(ProtocolMetadata{Seq: 0}, emptyBlacklist)
finalization := Finalization{
Finalization: ToBeSignedFinalization{
BlockHeader: block.BlockHeader(),
},
}
normalStorage := testutil.NewInMemStorage()
err := normalStorage.Index(context.Background(), block, finalization)
require.NoError(t, err)
for _, testCase := range []struct {
description string
storage Storage
expectedErr error
expectedVerifiedBlock *VerifiedFinalizedBlock
}{
{
description: "no blocks in storage",
storage: testutil.NewInMemStorage(),
},
{
description: "normal storage",
storage: normalStorage,
expectedVerifiedBlock: &VerifiedFinalizedBlock{
VerifiedBlock: block,
Finalization: finalization,
},
},
} {
t.Run(testCase.description, func(t *testing.T) {
lastBlock, err := RetrieveLastIndexFromStorage(testCase.storage)
require.ErrorIs(t, err, testCase.expectedErr)
require.Equal(t, testCase.expectedVerifiedBlock, lastBlock)
})
}
}
type unverifiableQC struct{}
func (u *unverifiableQC) Verify(Nodes) error {
return fmt.Errorf("invalid QC")
}
func TestVerifyQC(t *testing.T) {
var nodeIDs []NodeID
var nodes Nodes
for _, nodeID := range []NodeID{{1}, {2}, {3}, {4}, {5}} {
nodes = append(nodes, Node{Id: nodeID})
nodeIDs = append(nodeIDs, nodeID)
}
validatorsToPks := make(map[string][]byte)
for _, n := range nodes {
validatorsToPks[string(n.Id)] = []byte{}
}
quorumSize := Quorum(len(nodes))
signatureAggregator := &testutil.TestSignatureAggregator{N: len(nodes)}
// Test
tests := []struct {
name string
finalization Finalization
quorumSize int
expectedErr error
msgInvalid bool
}{
{
name: "valid finalization",
finalization: func() Finalization {
block := testutil.NewTestBlock(ProtocolMetadata{}, emptyBlacklist)
finalization, _ := testutil.NewFinalizationRecord(t, signatureAggregator, block, nodeIDs[:quorumSize])
return finalization
}(),
quorumSize: quorumSize,
}, {
name: "not enough signers",
finalization: func() Finalization {
block := testutil.NewTestBlock(ProtocolMetadata{}, emptyBlacklist)
finalization, _ := testutil.NewFinalizationRecord(t, signatureAggregator, block, nodeIDs[:quorumSize-1])
return finalization
}(),
quorumSize: quorumSize,
expectedErr: fmt.Errorf("quorum certificate signed by insufficient (3) nodes"),
},
{
name: "signer signed twice",
finalization: func() Finalization {
block := testutil.NewTestBlock(ProtocolMetadata{}, emptyBlacklist)
doubleNodes := []NodeID{{1}, {2}, {3}, {4}, {4}}
finalization, _ := testutil.NewFinalizationRecord(t, signatureAggregator, block, doubleNodes)
return finalization
}(),
quorumSize: quorumSize,
expectedErr: fmt.Errorf("quorum certificate is signed by the same node (0400000000000000) more than once"),
},
{
name: "quorum certificate not in finalization",
finalization: Finalization{Finalization: ToBeSignedFinalization{}},
quorumSize: quorumSize,
expectedErr: fmt.Errorf("nil QuorumCertificate"),
},
{
name: "nodes are not eligible signers",
finalization: func() Finalization {
block := testutil.NewTestBlock(ProtocolMetadata{}, emptyBlacklist)
signers := []NodeID{{1}, {2}, {3}, {4}, {6}}
finalization, _ := testutil.NewFinalizationRecord(t, signatureAggregator, block, signers)
return finalization
}(), quorumSize: quorumSize,
expectedErr: fmt.Errorf("quorum certificate contains an unknown signer (0600000000000000)"),
},
{
name: "invalid QC",
finalization: func() Finalization {
block := testutil.NewTestBlock(ProtocolMetadata{}, emptyBlacklist)
finalization, _ := testutil.NewFinalizationRecord(t, signatureAggregator, block, nodeIDs[:quorumSize])
return finalization
}(),
quorumSize: quorumSize,
msgInvalid: true,
expectedErr: fmt.Errorf("invalid QC"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
isQuorum := func(signers []NodeID) bool {
return len(signers) >= tt.quorumSize
}
if tt.msgInvalid {
err := VerifyQC(tt.finalization.QC, isQuorum, validatorsToPks, &unverifiableQC{}, nodes)
require.EqualError(t, err, tt.expectedErr.Error())
} else {
err := VerifyQC(tt.finalization.QC, isQuorum, validatorsToPks, &tt.finalization, nodes)
if tt.expectedErr != nil {
require.EqualError(t, err, tt.expectedErr.Error())
} else {
require.NoError(t, err)
}
}
})
}
}
func TestGetHighestQuorumRound(t *testing.T) {
// Test
nodes := []NodeID{{1}, {2}, {3}, {4}, {5}}
l := testutil.MakeLogger(t, 0)
signatureAggregator := &testutil.TestSignatureAggregator{N: len(nodes)}
// seq 1
block1 := testutil.NewTestBlock(ProtocolMetadata{
Seq: 1,
Round: 1,
}, emptyBlacklist)
notarization1, err := testutil.NewNotarization(l, signatureAggregator, block1, nodes)
require.NoError(t, err)
finalization1, _ := testutil.NewFinalizationRecord(t, signatureAggregator, block1, nodes)
// seq 10
block10 := testutil.NewTestBlock(ProtocolMetadata{Seq: 10, Round: 10}, emptyBlacklist)
notarization10, err := testutil.NewNotarization(l, signatureAggregator, block10, nodes)
require.NoError(t, err)
tests := []struct {
name string
round *Round
eNote *EmptyNotarization
lastBlock *VerifiedFinalizedBlock
expectedQr *VerifiedQuorumRound
}{
{
name: "only empty notarization",
eNote: testutil.NewEmptyNotarization(nodes, 1),
expectedQr: &VerifiedQuorumRound{
EmptyNotarization: testutil.NewEmptyNotarization(nodes, 1),
},
},
{
name: "nothing",
expectedQr: nil,
},
{
name: "round with finalization",
round: SetRound(block1, nil, &finalization1),
expectedQr: &VerifiedQuorumRound{
VerifiedBlock: block1,
Finalization: &finalization1,
},
},
{
name: "round with notarization",
round: SetRound(block1, &notarization1, nil),
expectedQr: &VerifiedQuorumRound{
VerifiedBlock: block1,
Notarization: &notarization1,
},
},
{
name: "higher round than empty notarization",
round: SetRound(block10, &notarization10, nil),
eNote: testutil.NewEmptyNotarization(nodes, 1),
expectedQr: &VerifiedQuorumRound{
VerifiedBlock: block10,
Notarization: &notarization10,
},
},
{
name: "higher empty notarization",
eNote: testutil.NewEmptyNotarization(nodes, 100),
round: SetRound(block10, &notarization10, nil),
expectedQr: &VerifiedQuorumRound{
EmptyNotarization: testutil.NewEmptyNotarization(nodes, 100),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
qr := GetLatestVerifiedQuorumRound(tt.round, tt.eNote)
require.Equal(t, tt.expectedQr, qr)
})
}
}
func TestBatchSequences(t *testing.T) {
tests := []struct {
name string
seqs []uint64
numNodes int
maxSize uint64
expected [][]uint64
}{
{
name: "empty input",
seqs: []uint64{},
numNodes: 3,
maxSize: 10,
expected: nil,
},
{
name: "zero nodes",
seqs: []uint64{1, 2, 3},
numNodes: 0,
maxSize: 10,
expected: nil,
},
{
name: "zero max size",
seqs: []uint64{1, 2, 3},
numNodes: 2,
maxSize: 0,
expected: nil,
},
{
name: "single sequence",
seqs: []uint64{5},
numNodes: 3,
maxSize: 10,
expected: [][]uint64{{5}},
},
{
name: "even split among nodes",
seqs: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8},
numNodes: 3,
maxSize: 10,
expected: [][]uint64{
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
},
},
{
name: "even split among nodes with gaps",
seqs: []uint64{0, 1, 3, 4, 6, 7, 8, 10, 11},
numNodes: 3,
maxSize: 10,
expected: [][]uint64{
{0, 1, 3},
{4, 6, 7},
{8, 10, 11},
},
},
{
name: "remainder goes to first nodes",
seqs: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
numNodes: 3,
maxSize: 10,
expected: [][]uint64{
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9},
},
},
{
name: "gaps split by total not per run",
seqs: []uint64{0, 1, 2, 3, 4, 6, 7, 8, 9, 10, 12, 13, 14, 15},
numNodes: 5,
maxSize: 40,
expected: [][]uint64{
{0, 1, 2},
{3, 4, 6},
{7, 8, 9},
{10, 12, 13},
{14, 15},
},
},
{
name: "unsorted input is sorted first",
seqs: []uint64{9, 0, 4, 2, 7},
numNodes: 2,
maxSize: 10,
expected: [][]uint64{
{0, 2, 4},
{7, 9},
},
},
{
name: "more nodes than sequences",
seqs: []uint64{1, 2, 3},
numNodes: 5,
maxSize: 10,
expected: [][]uint64{{1}, {2}, {3}},
},
{
name: "single node share capped at maxSize",
seqs: []uint64{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
},
numNodes: 1,
maxSize: 10,
expected: [][]uint64{
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{10, 11, 12, 13, 14, 15, 16, 17, 18, 19},
{20, 21, 22, 23, 24},
},
},
{
name: "node shares exceed maxSize and are chunked",
seqs: []uint64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14},
numNodes: 3,
maxSize: 3,
expected: [][]uint64{
{0, 1, 2}, {3, 4, 5},
{6, 7, 8}, {9, 10, 11},
{12, 13, 14},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := BatchSequences(tt.seqs, uint64(tt.numNodes), tt.maxSize)
require.Equal(t, tt.expected, result)
})
}
}
func TestNotarizationTime(t *testing.T) {
defaultFinalizeVoteRebroadcastTimeout := time.Second * 6
var round uint64
var have bool
var checkedIfWeHaveNotFinalizedRound int
haveNotFinalizedRound := func() (uint64, bool) {
checkedIfWeHaveNotFinalizedRound++
return round, have
}
var invoked int
rebroadcastFinalizationVotes := func() {
invoked++
}
nt := NewNotarizationTime(
defaultFinalizeVoteRebroadcastTimeout,
haveNotFinalizedRound,
rebroadcastFinalizationVotes,
func() uint64 {
return round
})
// First call should set the time and the round.
have = true
round = 100
now := time.Now()
nt.CheckForNotFinalizedNotarizedBlocks(now)
require.Zero(t, checkedIfWeHaveNotFinalizedRound)
// Next call happens just before we would check if we have not finalized.
now = now.Add(defaultFinalizeVoteRebroadcastTimeout / 3).Add(-time.Millisecond)
nt.CheckForNotFinalizedNotarizedBlocks(now)
require.Equal(t, 0, invoked)
require.Zero(t, checkedIfWeHaveNotFinalizedRound)
// Next call happens just after we would check if we have not finalized.
now = now.Add(time.Millisecond)
nt.CheckForNotFinalizedNotarizedBlocks(now)
require.Equal(t, 0, invoked)
require.Equal(t, 1, checkedIfWeHaveNotFinalizedRound)
// Advance the time some more. We still haven't reached defaultFinalizeVoteRebroadcastTimeout so no rebroadcast just yet.
now = now.Add(defaultFinalizeVoteRebroadcastTimeout / 3)
nt.CheckForNotFinalizedNotarizedBlocks(now)
require.Equal(t, 0, invoked)
require.Equal(t, 2, checkedIfWeHaveNotFinalizedRound)
// We need to wait a full defaultFinalizeVoteRebroadcastTimeout before we rebroadcast.
// This is because we are unaware when was our last rebroadcast time.
now = now.Add(defaultFinalizeVoteRebroadcastTimeout)
nt.CheckForNotFinalizedNotarizedBlocks(now)
require.Equal(t, 1, invoked)
require.Equal(t, 3, checkedIfWeHaveNotFinalizedRound)
// Next call happens shortly after, no rebroadcast should happen.
now = now.Add(defaultFinalizeVoteRebroadcastTimeout / 2)
nt.CheckForNotFinalizedNotarizedBlocks(now)
require.Equal(t, 1, invoked)
require.Equal(t, 4, checkedIfWeHaveNotFinalizedRound)
// Next rebroadcast happens after exactly the timeout.
now = now.Add(defaultFinalizeVoteRebroadcastTimeout / 2).Add(time.Millisecond)
nt.CheckForNotFinalizedNotarizedBlocks(now)
require.Equal(t, 2, invoked)
require.Equal(t, 5, checkedIfWeHaveNotFinalizedRound)
// We now change the round, even though enough time has passed, no rebroadcast should happen.
// Since we have advanced the round, we don't check if we have not finalized.
round = 101
now = now.Add(2 * defaultFinalizeVoteRebroadcastTimeout)
nt.CheckForNotFinalizedNotarizedBlocks(now)
require.Equal(t, 2, invoked)
require.Equal(t, 5, checkedIfWeHaveNotFinalizedRound)
// We now finalized everything, so no rebroadcast should happen.
have = false
now = now.Add(defaultFinalizeVoteRebroadcastTimeout)
nt.CheckForNotFinalizedNotarizedBlocks(now)
require.Equal(t, 2, invoked)
}
func TestNodeIDsFromVotes(t *testing.T) {
nodes := []NodeID{{1}, {2}, {3}, {4}, {5}}
votes := make([]*Vote, len(nodes))
for i, n := range nodes {
votes[i] = &Vote{Signature: Signature{Signer: n}}
}
result := NodeIDsFromVotes(votes)
require.Equal(t, nodes[1:4], result[1:4])
require.Equal(t, nodes[:1], result[:1])
require.Equal(t, nodes, result)
}