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]>
202 lines
4.1 KiB
Go
202 lines
4.1 KiB
Go
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package wal
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func new(t *testing.T) *WriteAheadLog {
|
|
fileName := filepath.Join(t.TempDir(), "engine.wal")
|
|
wal := New(fileName)
|
|
return wal
|
|
}
|
|
|
|
func TestWalSingleRw(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
r := []byte{3, 4, 5}
|
|
|
|
// writes and reads from wal
|
|
wal := new(t)
|
|
defer func() {
|
|
// Close twice to make sure we can do it
|
|
require.NoError(wal.Close())
|
|
require.NoError(wal.Close())
|
|
}()
|
|
|
|
// Close before appending just to make sure we can do it
|
|
require.NoError(wal.Close())
|
|
|
|
require.NoError(wal.Append(r))
|
|
|
|
// Close before reading just to make sure we can do it
|
|
require.NoError(wal.Close())
|
|
|
|
readRecords, err := wal.ReadAll()
|
|
require.NoError(err)
|
|
require.Equal(
|
|
[][]byte{r},
|
|
readRecords,
|
|
)
|
|
}
|
|
|
|
func TestWalMultipleRws(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
r1 := []byte{3, 4, 5}
|
|
r2 := []byte{1, 2, 3}
|
|
records := [][]byte{r1, r2}
|
|
|
|
wal := new(t)
|
|
defer func() {
|
|
require.NoError(wal.Close())
|
|
}()
|
|
|
|
require.NoError(wal.Append(r1))
|
|
require.NoError(wal.Append(r2))
|
|
|
|
readRecords, err := wal.ReadAll()
|
|
require.NoError(err)
|
|
require.Equal(records, readRecords)
|
|
}
|
|
|
|
func TestWalAppendAfterRead(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
r1 := []byte{3, 4, 5}
|
|
r2 := []byte{1, 2, 3}
|
|
records := [][]byte{r1, r2}
|
|
|
|
wal := new(t)
|
|
defer func() {
|
|
require.NoError(wal.Close())
|
|
}()
|
|
|
|
require.NoError(wal.Append(r1))
|
|
|
|
readRecords, err := wal.ReadAll()
|
|
require.NoError(err)
|
|
require.Equal(records[:1], readRecords)
|
|
|
|
require.NoError(wal.Append(r2))
|
|
|
|
readRecords, err = wal.ReadAll()
|
|
require.NoError(err)
|
|
require.Equal(records, readRecords)
|
|
}
|
|
|
|
// Write 3 records, corrupt 4th
|
|
func TestCorruptedFile(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
fileName := filepath.Join(t.TempDir(), "engine.wal")
|
|
wal := New(fileName)
|
|
defer func() {
|
|
require.NoError(wal.Close())
|
|
}()
|
|
|
|
const n = 4
|
|
records := make([][]byte, n)
|
|
for i := range records {
|
|
records[i] = []byte{byte(i), byte(i), byte(i)}
|
|
require.NoError(wal.Append(records[i]))
|
|
}
|
|
|
|
// Corrupt the last record
|
|
file, err := os.OpenFile(fileName, os.O_RDWR, 0666)
|
|
require.NoError(err)
|
|
|
|
recordSize := recordSizeLen + len(records[0]) + recordChecksumLen
|
|
_, err = file.WriteAt([]byte{0, 1, 2}, int64(3*recordSize))
|
|
require.NoError(err)
|
|
|
|
// Close the file to ensure the changes are flushed
|
|
require.NoError(file.Close())
|
|
|
|
// Because the last record is corrupted, it should not be read
|
|
readRecords, err := wal.ReadAll()
|
|
require.NoError(err)
|
|
require.Equal(records[:n-1], readRecords)
|
|
}
|
|
|
|
func TestDelete(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
r := []byte{3, 4, 5}
|
|
|
|
dir, err := os.MkdirTemp("", t.Name())
|
|
require.NoError(err)
|
|
|
|
fileName := filepath.Join(dir, "engine.wal")
|
|
wal := New(fileName)
|
|
require.NoError(err)
|
|
|
|
require.NoError(wal.Append(r))
|
|
|
|
require.NoError(wal.Delete())
|
|
|
|
_, err = os.Stat(fileName)
|
|
require.ErrorIs(err, os.ErrNotExist)
|
|
}
|
|
|
|
func TestDeleteWhenFileNil(t *testing.T) {
|
|
t.Run("never opened", func(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
fileName := filepath.Join(t.TempDir(), "engine.wal")
|
|
require.NoError(os.WriteFile(fileName, nil, WalPermissions))
|
|
|
|
wal := New(fileName)
|
|
|
|
require.NotPanics(func() {
|
|
require.NoError(wal.Delete())
|
|
})
|
|
|
|
_, err := os.Stat(fileName)
|
|
require.ErrorIs(err, os.ErrNotExist)
|
|
})
|
|
|
|
t.Run("closed before delete", func(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
fileName := filepath.Join(t.TempDir(), "engine.wal")
|
|
wal := New(fileName)
|
|
|
|
require.NoError(wal.Append([]byte{3, 4, 5}))
|
|
require.NoError(wal.Close()) // w.file is now nil
|
|
|
|
require.NotPanics(func() {
|
|
require.NoError(wal.Delete())
|
|
})
|
|
|
|
_, err := os.Stat(fileName)
|
|
require.ErrorIs(err, os.ErrNotExist)
|
|
})
|
|
}
|
|
|
|
func TestReadWriteAfterTruncate(t *testing.T) {
|
|
require := require.New(t)
|
|
|
|
r := []byte{3, 4, 5}
|
|
|
|
wal := new(t)
|
|
defer func() {
|
|
require.NoError(wal.Close())
|
|
}()
|
|
|
|
require.NoError(wal.Append(r))
|
|
|
|
readRecords, err := wal.ReadAll()
|
|
require.NoError(err)
|
|
require.Equal(
|
|
[][]byte{r},
|
|
readRecords,
|
|
)
|
|
}
|