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]>
113 lines
2.7 KiB
Go
113 lines
2.7 KiB
Go
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package host
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"math/big"
|
|
"time"
|
|
)
|
|
|
|
// This file contains implementations of utility methods and structures that exists in the host node,
|
|
// but are not imported here to prevent us from importing the entire the host node codebase.
|
|
// Once we incorporate BFT into the host node, we can remove this file and import the relevant code from the host node instead.
|
|
|
|
var errOverflow = errors.New("overflow")
|
|
|
|
const (
|
|
MiB = 1024 * 1024
|
|
|
|
DefaultMaxMessageSize = 2 * MiB
|
|
|
|
MaxContainersLen = int(4 * DefaultMaxMessageSize / 5)
|
|
)
|
|
|
|
func safeAdd(a, b uint64) (uint64, error) {
|
|
if a > math.MaxUint64-b {
|
|
return 0, fmt.Errorf("%w: %d + %d > maxuint64", errOverflow, a, b)
|
|
}
|
|
return a + b, nil
|
|
}
|
|
|
|
type NodeID [20]byte
|
|
|
|
type VMBlock interface {
|
|
// Digest returns a succinct representation of this block.
|
|
Digest() [32]byte
|
|
|
|
// Height returns the height of this block in the chain.
|
|
Height() uint64
|
|
|
|
// Time this block was proposed at. This value should be consistent across
|
|
// all nodes. If this block hasn't been successfully verified, any value can
|
|
// be returned. If this block is the last accepted block, the timestamp must
|
|
// be returned correctly. Otherwise, accepted blocks can return any value.
|
|
Timestamp() time.Time
|
|
|
|
// Verify that the state transition this block would make if accepted is
|
|
// valid. If the state transition is invalid, a non-nil error should be
|
|
// returned.
|
|
//
|
|
// It is guaranteed that the Parent has been successfully verified.
|
|
//
|
|
// If nil is returned, it is guaranteed that either Accept or Reject will be
|
|
// called on this block, unless the VM is shut down.
|
|
Verify(ctx context.Context, pChainHeight uint64) error
|
|
|
|
// Bytes returns the byte representation of this block.
|
|
Bytes() []byte
|
|
}
|
|
|
|
type Bitmask big.Int
|
|
|
|
func (bm *Bitmask) Bytes() []byte {
|
|
return (*big.Int)(bm).Bytes()
|
|
}
|
|
|
|
func (bm *Bitmask) Clone() Bitmask {
|
|
var newBM Bitmask
|
|
(*big.Int)(&newBM).Set((*big.Int)(bm))
|
|
return newBM
|
|
}
|
|
|
|
func (bm *Bitmask) Contains(i int) bool {
|
|
return (*big.Int)(bm).Bit(i) == 1
|
|
}
|
|
|
|
func (bm *Bitmask) Add(i int) {
|
|
bits := (*big.Int)(bm)
|
|
bits.SetBit(bits, i, 1)
|
|
}
|
|
|
|
func (bm *Bitmask) Difference(bm2 *Bitmask) {
|
|
bits := (*big.Int)(bm)
|
|
bits2 := (*big.Int)(bm2)
|
|
bits.AndNot(bits, bits2)
|
|
}
|
|
|
|
func (bm *Bitmask) Len() int {
|
|
bmAsBigInt := (*big.Int)(bm)
|
|
bits := new(big.Int).Set(bmAsBigInt)
|
|
|
|
result := 0
|
|
var zero big.Int
|
|
for bits.Cmp(&zero) > 0 {
|
|
lsb := bits.Bit(0)
|
|
if lsb == 1 {
|
|
result++
|
|
}
|
|
bits.Rsh(bits, 1)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func BitmaskFromBytes(bytes []byte) Bitmask {
|
|
var bm Bitmask
|
|
(*big.Int)(&bm).SetBytes(bytes)
|
|
return bm
|
|
}
|