mirror of
https://github.com/luxfi/bft.git
synced 2026-10-30 17:01:54 +00:00
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]>
163 lines
4.2 KiB
Go
163 lines
4.2 KiB
Go
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package random_network
|
|
|
|
import (
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/luxfi/bft/common"
|
|
"github.com/luxfi/bft/engine"
|
|
"github.com/luxfi/bft/testutil"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type randomNodeConfig struct {
|
|
mempool *Mempool
|
|
storage *Storage
|
|
wal *testutil.TestWAL
|
|
logger *testutil.TestLogger
|
|
}
|
|
|
|
type Node struct {
|
|
*testutil.BasicNode
|
|
|
|
storage *Storage
|
|
wal *testutil.TestWAL
|
|
mempool *Mempool
|
|
|
|
logger *testutil.TestLogger
|
|
isCrashed atomic.Bool
|
|
}
|
|
|
|
func NewNode(t *testing.T, nodeID common.NodeID, net *testutil.BasicInMemoryNetwork, config *FuzzConfig, nodeConfig randomNodeConfig) *Node {
|
|
var l *testutil.TestLogger
|
|
if nodeConfig.logger != nil {
|
|
l = nodeConfig.logger
|
|
} else {
|
|
l = CreateNodeLogger(t, config, nodeID)
|
|
}
|
|
|
|
var mempool *Mempool
|
|
|
|
if nodeConfig.mempool != nil {
|
|
mempool = nodeConfig.mempool
|
|
} else {
|
|
mempool = NewMempool(l, config)
|
|
}
|
|
|
|
comm := testutil.NewTestComm(nodeID, net, testutil.AllowAllMessages)
|
|
epochConfig, wal, _ := testutil.DefaultTestNodeEpochConfig(t, nodeID, comm, mempool)
|
|
epochConfig.Logger = l
|
|
epochConfig.MaxRoundWindow = 100
|
|
epochConfig.ReplicationEnabled = true
|
|
|
|
// storage
|
|
var storage *Storage
|
|
if nodeConfig.storage != nil {
|
|
storage = nodeConfig.storage
|
|
} else {
|
|
storage = NewStorage(mempool)
|
|
}
|
|
epochConfig.Storage = storage
|
|
|
|
// wal
|
|
if nodeConfig.wal != nil {
|
|
wal = nodeConfig.wal
|
|
}
|
|
epochConfig.WAL = wal
|
|
|
|
epochConfig.BlockDeserializer = &BlockDeserializer{
|
|
mempool: mempool,
|
|
}
|
|
|
|
e, err := engine.NewEpoch(epochConfig)
|
|
require.NoError(t, err)
|
|
|
|
n := &Node{
|
|
BasicNode: testutil.NewBasicNode(t, e, l),
|
|
storage: storage,
|
|
mempool: mempool,
|
|
logger: l,
|
|
wal: wal,
|
|
isCrashed: atomic.Bool{},
|
|
}
|
|
|
|
n.BasicNode.CustomHandler = n.HandleMessage
|
|
|
|
return n
|
|
}
|
|
|
|
func (n *Node) HandleMessage(msg *common.Message, from common.NodeID) error {
|
|
msgCopy := n.copyMessage(msg)
|
|
return n.BasicNode.HandleMessage(&msgCopy, from)
|
|
}
|
|
|
|
// copyBlock creates a copy of a engine.Block with the node's mempool.
|
|
func (n *Node) copyBlock(b common.Block) common.Block {
|
|
blockCopy := *b.(*Block)
|
|
blockCopy.mempool = n.mempool
|
|
return &blockCopy
|
|
}
|
|
|
|
// copyMessage creates a copy of the message and its relevant fields to avoid mutating shared state in the in-memory network
|
|
// this is important because blocks are not serialized/deserialized in our current comm implementation, so sending blocks
|
|
// also sends relevant state associated with the node that is sending the message which can cause unintended side effects.
|
|
func (n *Node) copyMessage(msg *common.Message) common.Message {
|
|
msgCopy := *msg
|
|
|
|
switch {
|
|
case msgCopy.BlockMessage != nil:
|
|
blockMsgCopy := *msgCopy.BlockMessage
|
|
blockMsgCopy.Block = n.copyBlock(msgCopy.BlockMessage.Block)
|
|
msgCopy.BlockMessage = &blockMsgCopy
|
|
|
|
case msgCopy.ReplicationResponse != nil:
|
|
rrCopy := *msgCopy.ReplicationResponse
|
|
|
|
// Also copy the Data slice to avoid mutating shared slice
|
|
rrCopy.Data = make([]common.QuorumRound, len(msgCopy.ReplicationResponse.Data))
|
|
copy(rrCopy.Data, msgCopy.ReplicationResponse.Data)
|
|
msgCopy.ReplicationResponse = &rrCopy
|
|
|
|
// convert quorum rounds to our type
|
|
for i, qr := range msgCopy.ReplicationResponse.Data {
|
|
if qr.Block != nil {
|
|
msgCopy.ReplicationResponse.Data[i].Block = n.copyBlock(qr.Block)
|
|
}
|
|
}
|
|
|
|
if msgCopy.ReplicationResponse.LatestRound != nil {
|
|
latestRoundCopy := *msgCopy.ReplicationResponse.LatestRound
|
|
msgCopy.ReplicationResponse.LatestRound = &latestRoundCopy
|
|
if latestRoundCopy.Block != nil {
|
|
msgCopy.ReplicationResponse.LatestRound.Block = n.copyBlock(latestRoundCopy.Block)
|
|
}
|
|
}
|
|
|
|
if msgCopy.ReplicationResponse.LatestSeq != nil {
|
|
latestSeqCopy := *msgCopy.ReplicationResponse.LatestSeq
|
|
msgCopy.ReplicationResponse.LatestSeq = &latestSeqCopy
|
|
if latestSeqCopy.Block != nil {
|
|
msgCopy.ReplicationResponse.LatestSeq.Block = n.copyBlock(latestSeqCopy.Block)
|
|
}
|
|
}
|
|
default:
|
|
// no-op
|
|
}
|
|
return msgCopy
|
|
}
|
|
|
|
func (n *Node) areTxsAccepted(txs []*TX) bool {
|
|
n.mempool.lock.Lock()
|
|
defer n.mempool.lock.Unlock()
|
|
|
|
for _, tx := range txs {
|
|
if _, exists := n.mempool.acceptedTXs[tx.ID]; !exists {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|