mirror of
https://github.com/luxfi/bft.git
synced 2026-11-23 00:12:40 +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]>
192 lines
4.5 KiB
Go
192 lines
4.5 KiB
Go
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package engine
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/luxfi/log"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestMonitorDoubleClose(t *testing.T) {
|
|
start := time.Now()
|
|
mon := NewMonitor(start, makeLogger(t))
|
|
require.True(t, mon.shouldRun())
|
|
mon.Close()
|
|
require.False(t, mon.shouldRun())
|
|
mon.Close()
|
|
require.False(t, mon.shouldRun())
|
|
}
|
|
|
|
func TestMonitorPrematureCancelTask(t *testing.T) {
|
|
start := time.Now()
|
|
|
|
ticked := make(chan struct{})
|
|
|
|
mon := NewMonitor(start, makeLogger(t))
|
|
defer mon.Close()
|
|
mon.logger.(*testLogger).intercept(func(entry logEntry) error {
|
|
if entry.Message == "Ticked" {
|
|
ticked <- struct{}{}
|
|
}
|
|
return nil
|
|
})
|
|
|
|
t.Run("Cancelled future task does not fire", func(t *testing.T) {
|
|
panic := func() {
|
|
panic("test failed")
|
|
}
|
|
|
|
mon.FutureTask(time.Hour, panic)
|
|
mon.CancelFutureTask()
|
|
|
|
mon.AdvanceTime(start.Add(time.Hour))
|
|
|
|
select {
|
|
case <-ticked:
|
|
case <-time.After(time.Minute):
|
|
require.FailNow(t, "timed out waiting on tick")
|
|
}
|
|
})
|
|
|
|
t.Run("Non-Cancelled future task fires", func(t *testing.T) {
|
|
finish := make(chan struct{})
|
|
|
|
mon.FutureTask(time.Hour, func() {
|
|
close(finish)
|
|
})
|
|
|
|
mon.AdvanceTime(start.Add(time.Hour * 2))
|
|
|
|
<-ticked
|
|
<-finish
|
|
})
|
|
|
|
t.Run("Cancelled task does not fire", func(t *testing.T) {
|
|
// Occupy the monitor goroutine, otherwise it can dequeue and start the task below
|
|
// before we cancel it, and cancelling cannot stop a task that already started.
|
|
busy := make(chan struct{})
|
|
released := make(chan struct{})
|
|
|
|
mon.RunTask(func() {
|
|
close(busy)
|
|
<-released
|
|
})
|
|
<-busy
|
|
|
|
finish := make(chan struct{})
|
|
|
|
mon.RunTask(func() {
|
|
<-finish
|
|
close(finish) // Test should panic if we have a double close
|
|
})
|
|
|
|
mon.CancelTask()
|
|
close(released)
|
|
|
|
close(finish)
|
|
})
|
|
}
|
|
|
|
func TestMonitorAsyncWaitFor(t *testing.T) {
|
|
start := time.Now()
|
|
mon := NewMonitor(start, makeLogger(t))
|
|
defer mon.Close()
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
mon.RunTask(wg.Done)
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestMonitorAsyncWaitUntilWithWaitFor(t *testing.T) {
|
|
start := time.Now()
|
|
mon := NewMonitor(start, makeLogger(t))
|
|
defer mon.Close()
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
mon.FutureTask(10*time.Millisecond, wg.Done)
|
|
mon.RunTask(func() {
|
|
mon.AdvanceTime(start.Add(10 * time.Millisecond))
|
|
})
|
|
wg.Wait()
|
|
}
|
|
|
|
func TestMonitorAsyncWaitForWithNestedWaitUntil(t *testing.T) {
|
|
start := time.Now()
|
|
mon := NewMonitor(start, makeLogger(t))
|
|
defer mon.Close()
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
mon.RunTask(func() {
|
|
go mon.AdvanceTime(start.Add(10 * time.Millisecond))
|
|
mon.FutureTask(10*time.Millisecond, wg.Done)
|
|
})
|
|
wg.Wait()
|
|
}
|
|
|
|
// logEntry and testLogger are local to this package rather than reused from
|
|
// testutil: testutil imports bft, so an internal test that imported it back
|
|
// would close an import cycle.
|
|
type logEntry struct {
|
|
Level log.Level
|
|
Message string
|
|
}
|
|
|
|
type testLogger struct {
|
|
mu sync.Mutex
|
|
hook func(entry logEntry) error
|
|
}
|
|
|
|
func (tl *testLogger) intercept(hook func(entry logEntry) error) {
|
|
tl.mu.Lock()
|
|
defer tl.mu.Unlock()
|
|
tl.hook = hook
|
|
}
|
|
|
|
func (tl *testLogger) emit(level log.Level, msg string, fields []log.Field) {
|
|
tl.mu.Lock()
|
|
hook := tl.hook
|
|
tl.mu.Unlock()
|
|
|
|
if hook != nil {
|
|
_ = hook(logEntry{Level: level, Message: msg})
|
|
}
|
|
|
|
tl.mu.Lock()
|
|
defer tl.mu.Unlock()
|
|
fmt.Fprintf(os.Stdout, "%s %s", strings.ToUpper(level.String()), msg)
|
|
for _, f := range fields {
|
|
fmt.Fprintf(os.Stdout, " %s=%v", f.Key, f.Value)
|
|
}
|
|
fmt.Fprintln(os.Stdout)
|
|
}
|
|
|
|
func (tl *testLogger) Trace(msg string, fields ...log.Field) { tl.emit(log.DebugLevel, msg, fields) }
|
|
func (tl *testLogger) Verbo(msg string, fields ...log.Field) { tl.emit(log.DebugLevel, msg, fields) }
|
|
func (tl *testLogger) Debug(msg string, fields ...log.Field) { tl.emit(log.DebugLevel, msg, fields) }
|
|
func (tl *testLogger) Info(msg string, fields ...log.Field) { tl.emit(log.InfoLevel, msg, fields) }
|
|
func (tl *testLogger) Warn(msg string, fields ...log.Field) { tl.emit(log.WarnLevel, msg, fields) }
|
|
func (tl *testLogger) Error(msg string, fields ...log.Field) { tl.emit(log.ErrorLevel, msg, fields) }
|
|
|
|
// Fatal must not return: it reports a state the caller has declared impossible,
|
|
// so continuing past it would run code the caller already ruled out. Panicking
|
|
// keeps the attribution a process exit would destroy.
|
|
func (tl *testLogger) Fatal(msg string, fields ...log.Field) {
|
|
tl.emit(log.FatalLevel, msg, fields)
|
|
panic("FATAL: " + msg)
|
|
}
|
|
|
|
func makeLogger(_ *testing.T) *testLogger {
|
|
return &testLogger{}
|
|
}
|