Files
bft/msm/encoding_test.go
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

428 lines
11 KiB
Go

// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package metadata
import (
"testing"
"github.com/luxfi/bft/common"
"github.com/luxfi/bft/host"
"github.com/luxfi/bft/testutil"
"github.com/stretchr/testify/require"
)
func TestEpochInfoIsZero(t *testing.T) {
require.True(t, (&EpochInfo{}).IsZero())
require.False(t, (&EpochInfo{EpochNumber: 1}).IsZero())
require.False(t, (&EpochInfo{PChainReferenceHeight: 1}).IsZero())
}
func TestEpochInfoEqual(t *testing.T) {
hash1 := [32]byte{1, 2, 3}
hash2 := [32]byte{4, 5, 6}
tests := []struct {
name string
a *EpochInfo
b *EpochInfo
expected bool
}{
{
name: "first nil second nil",
a: nil,
b: nil,
expected: true,
},
{
name: "first non-nil second nil",
a: &EpochInfo{},
b: nil,
expected: false,
},
{
name: "first nil second non-nil (via nil receiver)",
a: nil,
b: &EpochInfo{},
expected: false,
},
{
name: "both zero",
a: &EpochInfo{},
b: &EpochInfo{},
expected: true,
},
{
name: "equal with all fields",
a: &EpochInfo{
PChainReferenceHeight: 10,
EpochNumber: 2,
PrevSealingBlockHash: hash1,
NextPChainReferenceHeight: 20,
PrevVMBlockSeq: 5,
SealingBlockSeq: 15,
},
b: &EpochInfo{
PChainReferenceHeight: 10,
EpochNumber: 2,
PrevSealingBlockHash: hash1,
NextPChainReferenceHeight: 20,
PrevVMBlockSeq: 5,
SealingBlockSeq: 15,
},
expected: true,
},
{
name: "different PChainReferenceHeight",
a: &EpochInfo{PChainReferenceHeight: 1},
b: &EpochInfo{PChainReferenceHeight: 2},
expected: false,
},
{
name: "different EpochNumber",
a: &EpochInfo{EpochNumber: 1},
b: &EpochInfo{EpochNumber: 2},
expected: false,
},
{
name: "different PrevSealingBlockHash",
a: &EpochInfo{PrevSealingBlockHash: hash1},
b: &EpochInfo{PrevSealingBlockHash: hash2},
expected: false,
},
{
name: "different NextPChainReferenceHeight",
a: &EpochInfo{NextPChainReferenceHeight: 1},
b: &EpochInfo{NextPChainReferenceHeight: 2},
expected: false,
},
{
name: "different PrevVMBlockSeq",
a: &EpochInfo{PrevVMBlockSeq: 1},
b: &EpochInfo{PrevVMBlockSeq: 2},
expected: false,
},
{
name: "different SealingBlockSeq",
a: &EpochInfo{SealingBlockSeq: 1},
b: &EpochInfo{SealingBlockSeq: 2},
expected: false,
},
{
name: "with BlockValidationDescriptor equal",
a: &EpochInfo{
BlockValidationDescriptor: &BlockValidationDescriptor{
AggregatedMembership: AggregatedMembership{
Members: []NodeBLSMapping{{NodeID: host.NodeID{1}, Weight: 10}},
},
},
},
b: &EpochInfo{
BlockValidationDescriptor: &BlockValidationDescriptor{
AggregatedMembership: AggregatedMembership{
Members: []NodeBLSMapping{{NodeID: host.NodeID{1}, Weight: 10}},
},
},
},
expected: true,
},
{
name: "with BlockValidationDescriptor different",
a: &EpochInfo{
BlockValidationDescriptor: &BlockValidationDescriptor{
AggregatedMembership: AggregatedMembership{
Members: []NodeBLSMapping{{NodeID: host.NodeID{1}, Weight: 10}},
},
},
},
b: &EpochInfo{
BlockValidationDescriptor: &BlockValidationDescriptor{
AggregatedMembership: AggregatedMembership{
Members: []NodeBLSMapping{{NodeID: host.NodeID{2}, Weight: 20}},
},
},
},
expected: false,
},
{
name: "with NextEpochApprovals equal",
a: &EpochInfo{
NextEpochApprovals: &NextEpochApprovals{NodeIDs: []byte{1, 2}, Signature: []byte{3, 4}},
},
b: &EpochInfo{
NextEpochApprovals: &NextEpochApprovals{NodeIDs: []byte{1, 2}, Signature: []byte{3, 4}},
},
expected: true,
},
{
name: "with NextEpochApprovals different",
a: &EpochInfo{
NextEpochApprovals: &NextEpochApprovals{NodeIDs: []byte{1, 2}, Signature: []byte{3, 4}},
},
b: &EpochInfo{
NextEpochApprovals: &NextEpochApprovals{NodeIDs: []byte{5, 6}, Signature: []byte{7, 8}},
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, tt.a.Equal(tt.b))
})
}
}
func TestNodeBLSMappingEquals(t *testing.T) {
tests := []struct {
name string
a NodeBLSMapping
b NodeBLSMapping
expected bool
}{
{
name: "both zero",
expected: true,
},
{
name: "equal with values",
a: NodeBLSMapping{NodeID: host.NodeID{1, 2, 3}, BLSKey: []byte{4, 5}, Weight: 100},
b: NodeBLSMapping{NodeID: host.NodeID{1, 2, 3}, BLSKey: []byte{4, 5}, Weight: 100},
expected: true,
},
{
name: "different NodeID",
a: NodeBLSMapping{NodeID: host.NodeID{1}},
b: NodeBLSMapping{NodeID: host.NodeID{2}},
expected: false,
},
{
name: "different BLSKey",
a: NodeBLSMapping{BLSKey: []byte{1}},
b: NodeBLSMapping{BLSKey: []byte{2}},
expected: false,
},
{
name: "different Weight",
a: NodeBLSMapping{Weight: 1},
b: NodeBLSMapping{Weight: 2},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, tt.a.Equals(&tt.b))
})
}
}
func TestBlockValidationDescriptorEquals(t *testing.T) {
tests := []struct {
name string
a *BlockValidationDescriptor
b *BlockValidationDescriptor
expected bool
}{
{
name: "both nil",
expected: true,
},
{
name: "first nil second non-nil",
b: &BlockValidationDescriptor{},
expected: false,
},
{
name: "first non-nil second nil",
a: &BlockValidationDescriptor{},
expected: false,
},
{
name: "equal members",
a: &BlockValidationDescriptor{
AggregatedMembership: AggregatedMembership{
Members: []NodeBLSMapping{{NodeID: host.NodeID{1}, Weight: 10}},
},
},
b: &BlockValidationDescriptor{
AggregatedMembership: AggregatedMembership{
Members: []NodeBLSMapping{{NodeID: host.NodeID{1}, Weight: 10}},
},
},
expected: true,
},
{
name: "different members",
a: &BlockValidationDescriptor{
AggregatedMembership: AggregatedMembership{
Members: []NodeBLSMapping{{NodeID: host.NodeID{1}, Weight: 10}},
},
},
b: &BlockValidationDescriptor{
AggregatedMembership: AggregatedMembership{
Members: []NodeBLSMapping{{NodeID: host.NodeID{2}, Weight: 20}},
},
},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, tt.a.Equals(tt.b))
})
}
}
func TestAggregatedMembershipEquals(t *testing.T) {
tests := []struct {
name string
members []NodeBLSMapping
other []NodeBLSMapping
expected bool
}{
{
name: "both empty",
expected: true,
},
{
name: "different lengths",
members: []NodeBLSMapping{{Weight: 1}},
expected: false,
},
{
name: "equal",
members: []NodeBLSMapping{{NodeID: host.NodeID{1}, BLSKey: []byte{2}, Weight: 3}},
other: []NodeBLSMapping{{NodeID: host.NodeID{1}, BLSKey: []byte{2}, Weight: 3}},
expected: true,
},
{
name: "different",
members: []NodeBLSMapping{{NodeID: host.NodeID{1}}},
other: []NodeBLSMapping{{NodeID: host.NodeID{2}}},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
am := &AggregatedMembership{Members: tt.members}
require.Equal(t, tt.expected, am.Equals(tt.other))
})
}
}
func TestNextEpochApprovalsEquals(t *testing.T) {
tests := []struct {
name string
a *NextEpochApprovals
b *NextEpochApprovals
expected bool
}{
{
name: "both nil",
expected: true,
},
{
name: "first nil second non-nil",
b: &NextEpochApprovals{},
expected: false,
},
{
name: "first non-nil second nil",
a: &NextEpochApprovals{},
expected: false,
},
{
name: "equal",
a: &NextEpochApprovals{NodeIDs: []byte{1, 2}, Signature: []byte{3, 4}},
b: &NextEpochApprovals{NodeIDs: []byte{1, 2}, Signature: []byte{3, 4}},
expected: true,
},
{
name: "different NodeIDs",
a: &NextEpochApprovals{NodeIDs: []byte{1}},
b: &NextEpochApprovals{NodeIDs: []byte{2}},
expected: false,
},
{
name: "different Signature",
a: &NextEpochApprovals{Signature: []byte{1}},
b: &NextEpochApprovals{Signature: []byte{2}},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, tt.a.Equals(tt.b))
})
}
}
func TestNodeBLSMappingsCompare(t *testing.T) {
tests := []struct {
name string
a NodeBLSMappings
b NodeBLSMappings
expected bool
}{
{
name: "both nil",
expected: true,
},
{
name: "different lengths",
a: NodeBLSMappings{{Weight: 1}},
expected: false,
},
{
name: "equal same order",
a: NodeBLSMappings{{NodeID: host.NodeID{1}, Weight: 10}, {NodeID: host.NodeID{2}, Weight: 20}},
b: NodeBLSMappings{{NodeID: host.NodeID{1}, Weight: 10}, {NodeID: host.NodeID{2}, Weight: 20}},
expected: true,
},
{
name: "equal different order",
a: NodeBLSMappings{{NodeID: host.NodeID{2}, Weight: 20}, {NodeID: host.NodeID{1}, Weight: 10}},
b: NodeBLSMappings{{NodeID: host.NodeID{1}, Weight: 10}, {NodeID: host.NodeID{2}, Weight: 20}},
expected: true,
},
{
name: "different values",
a: NodeBLSMappings{{NodeID: host.NodeID{1}, Weight: 10}},
b: NodeBLSMappings{{NodeID: host.NodeID{1}, Weight: 99}},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.expected, tt.a.Equal(tt.b))
})
}
}
func TestValidatorSetApprovalsFilter(t *testing.T) {
logger := testutil.MakeLogger(t)
approvals := ValidatorSetApprovals{
{NodeID: host.NodeID{1}, PChainHeight: 10},
{NodeID: host.NodeID{2}, PChainHeight: 20},
{NodeID: host.NodeID{3}, PChainHeight: 30},
}
filtered := approvals.Filter(func(v common.ValidatorSetApproval, _ common.Logger) bool {
return v.PChainHeight > 15
}, logger)
require.Len(t, filtered, 2)
require.Equal(t, uint64(20), filtered[0].PChainHeight)
require.Equal(t, uint64(30), filtered[1].PChainHeight)
// Filter all
filtered = approvals.Filter(func(common.ValidatorSetApproval, common.Logger) bool {
return false
}, logger)
require.Empty(t, filtered)
}