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

189 lines
4.7 KiB
Go

// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package nonvalidator
import (
"bytes"
"sync"
"testing"
"github.com/luxfi/bft/common"
"github.com/luxfi/bft/engine"
"github.com/luxfi/bft/testutil"
"github.com/stretchr/testify/require"
)
// messageQueue keeps a queue of messages
type messageQueue struct {
responsesLock sync.Mutex
responses []*messageInfo
}
func (m *messageQueue) clearResponses() {
m.responsesLock.Lock()
defer m.responsesLock.Unlock()
m.responses = []*messageInfo{}
}
func (m *messageQueue) enqueue(mi *messageInfo) {
m.responsesLock.Lock()
defer m.responsesLock.Unlock()
m.responses = append(m.responses, mi)
}
func (m *messageQueue) popResponse() (*messageInfo, bool) {
m.responsesLock.Lock()
defer m.responsesLock.Unlock()
if len(m.responses) == 0 {
return nil, false
}
msg := m.responses[0]
m.responses = m.responses[1:]
return msg, true
}
// routerComm appends messages being sent or broadcast to the message queue.
type routerComm struct {
t *testing.T
nodes common.Nodes
// ID is the sender of messages and is the Node using the struct.
ID common.NodeID
messageQueue *messageQueue
}
type testEpochs struct {
t *testing.T
epochs []*engine.Epoch
}
func newTestEpochs(tc *testChain, msgQueue *messageQueue, maxSeqWindow uint64) *testEpochs {
nodes := tc.nodes()
epochs := make([]*engine.Epoch, 0, len(nodes))
for _, node := range nodes {
epochNodeID := node.Id
comm := &routerComm{
nodes: nodes,
t: tc.t,
ID: epochNodeID,
messageQueue: msgQueue,
}
conf, _, _ := testutil.DefaultTestNodeEpochConfig(tc.t, epochNodeID, comm, testutil.NewTestBlockBuilder())
conf.MaxRoundWindow = maxSeqWindow
conf.Storage = tc
conf.SignatureAggregatorCreator = tc.signatureAggregatorCreator
conf.ReplicationEnabled = true
epoch, err := engine.NewEpoch(conf)
require.NoError(tc.t, err)
epochs = append(epochs, epoch)
}
return &testEpochs{
t: tc.t,
epochs: epochs,
}
}
// start starts every epoch in the network, failing the test if any epoch
// fails to start.
func (e *testEpochs) start() {
for _, epoch := range e.epochs {
require.NoError(e.t, epoch.Start())
}
}
// stop stops every epoch in the network.
func (e *testEpochs) stop() {
for _, epoch := range e.epochs {
epoch.Stop()
}
}
// handleMessage routes messages between the non-validator and its epochs: a request
// originating from the non-validator is delivered to the addressed epoch, while
// a response from an epoch is delivered back to the non-validator.
func handleMessage(epochs *testEpochs, nv *NonValidator, mi *messageInfo) {
if !bytes.Equal(mi.from, nv.ID) {
// a response from an epoch: deliver it to the non-validator.
require.NoError(epochs.t, nv.HandleMessage(mi.msg, mi.from))
return
}
// a request from the non-validator: route it to the addressed epoch.
for _, epoch := range epochs.epochs {
if bytes.Equal(epoch.ID, mi.to) {
require.NoError(epochs.t, epoch.HandleMessage(mi.msg, mi.from))
return
}
}
require.Failf(epochs.t, "no epoch for destination", "destination %x", mi.to)
}
func (r *routerComm) Validators() common.Nodes { return r.nodes }
// Enqueues a message sent from this node to `destination`.
func (r *routerComm) Send(msg *common.Message, destination common.NodeID) {
r.handle(msg, destination)
}
// Enqueues a copy of the message addressed to every other node in the network.
func (r *routerComm) Broadcast(msg *common.Message) {
for _, n := range r.nodes {
if bytes.Equal(n.Id, r.ID) {
continue
}
r.handle(msg, n.Id)
}
}
func (r *routerComm) handle(msg *common.Message, to common.NodeID) {
switch {
case msg.VerifiedReplicationResponse != nil:
// Outgoing responses are of the verified type, but incoming responses
// are of the unverified type, so we translate before enqueuing.
vrr := msg.VerifiedReplicationResponse
data := make([]common.QuorumRound, 0, len(vrr.Data))
for _, vqr := range vrr.Data {
data = append(data, *verifiedQRtoQR(&vqr))
}
msg = &common.Message{
ReplicationResponse: &common.ReplicationResponse{
Data: data,
LatestRound: verifiedQRtoQR(vrr.LatestRound),
LatestSeq: verifiedQRtoQR(vrr.LatestFinalizedSeq),
},
}
r.messageQueue.enqueue(&messageInfo{msg: msg, from: r.ID, to: to})
default:
r.messageQueue.enqueue(&messageInfo{msg: msg, from: r.ID, to: to})
}
}
func verifiedQRtoQR(vqr *common.VerifiedQuorumRound) *common.QuorumRound {
if vqr == nil {
return nil
}
qr := &common.QuorumRound{
Notarization: vqr.Notarization,
Finalization: vqr.Finalization,
EmptyNotarization: vqr.EmptyNotarization,
}
if vqr.VerifiedBlock != nil {
qr.Block = vqr.VerifiedBlock.(common.Block)
}
return qr
}