Files
zeekayandHanzo Dev d678c2d213 transform: publish upstream simplex as luxfi/bft on luxfi/log
Reapplies the fork's whole delta on top of the merge, as one reviewable diff
rather than 43 conflict resolutions. Nothing here changes consensus behaviour.

- module github.com/luxfi/bft, Go 1.26.4; root package simplex -> bft, which
  would otherwise collide with the simplex/ subpackage under this module name.
- go.uber.org/zap -> github.com/luxfi/log, so common.Logger takes ...log.Field.
  zap.Error maps to log.Err: log.Error is luxfi/log's package-level logging
  function, not a field constructor, and the compiler is the only thing that
  catches that. Needs luxfi/log v1.6.1 for the Stringers constructor.
- avalanchego/ -> host/. The package holds NodeID, VMBlock, Bitmask and size
  limits — the host node's types, vendored to avoid importing a whole node — so
  it is named for what it holds. Renaming it luxd/ would have swapped one brand
  for another and said just as little.
- testutil/logger.go reimplemented without zapcore. Upstream layers a
  keyword-filtering Core, a debug-swallowing Encoder and a Tee because that is
  zap's extension model; luxfi/log has no such seam, so every policy is applied
  in one emit. That also fixes two defects the layering caused:
  SilenceExceptKeywords rebuilt the logger and discarded an installed Intercept
  hook, and the two sinks read different env vars (LOG_LEVEL vs log_level) so
  they disagreed about suppressing debug. Hooks run outside the lock, so a hook
  that logs fails the test instead of deadlocking it. Fatal panics rather than
  exiting, because exiting takes the suite down with no attribution.
- simplex/monitor_test.go keeps a local logger: testutil imports simplex, so an
  internal test cannot import testutil back without closing a cycle.
- README keeps the name Simplex throughout — it is the protocol's own name and
  the subject of reference [1]. Only the deployment target changed. The HyperSDK
  sentence still says Avalanche because that sentence is true.
- Drops the fork's Makefile: it duplicated unit-tests.sh and fuzz.sh, and ran the
  suite with -timeout 30s when simplex alone needs over 100s.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-06 04:21:17 -07:00

179 lines
3.4 KiB
Go

// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package testutil
import (
"bytes"
"context"
"crypto/sha256"
"encoding/asn1"
"fmt"
"time"
"github.com/luxfi/bft/common"
)
type TestBlock struct {
Data []byte
Metadata common.ProtocolMetadata
blacklist common.Blacklist
Digest [32]byte
OnVerify func()
VerificationDelay chan struct{}
VerificationError error
// SealingInfo, when non-nil, is returned by SealingBlockInfo() to mark this block as a sealing block.
SealingInfo *common.SealingBlockInfo
}
func NewTestBlock(metadata common.ProtocolMetadata, blacklist common.Blacklist) *TestBlock {
tb := TestBlock{
blacklist: blacklist,
Metadata: metadata,
}
tb.ComputeDigest()
return &tb
}
func (tb *TestBlock) Blacklist() common.Blacklist {
return tb.blacklist
}
func (tb *TestBlock) Verify(context.Context) (common.VerifiedBlock, error) {
defer func() {
if tb.OnVerify != nil {
tb.OnVerify()
}
}()
if tb.VerificationError != nil {
return nil, tb.VerificationError
}
if tb.VerificationDelay == nil {
return tb, nil
}
<-tb.VerificationDelay
return tb, nil
}
func (tb *TestBlock) SealingBlockInfo() *common.SealingBlockInfo {
return tb.SealingInfo
}
func (tb *TestBlock) ComputeDigest() {
var bb bytes.Buffer
tbBytes := tb.Bytes()
bb.Write(tbBytes)
tb.Digest = sha256.Sum256(bb.Bytes())
}
func (t *TestBlock) BlockHeader() common.BlockHeader {
return common.BlockHeader{
ProtocolMetadata: t.Metadata,
Digest: t.Digest,
}
}
func (t *TestBlock) Bytes() []byte {
bh := common.BlockHeader{
ProtocolMetadata: t.Metadata,
}
mdBuff := bh.ProtocolMetadata.Bytes()
blBytes := t.blacklist.Bytes()
if bytes.Equal(blBytes, t.Data) {
t.Data = nil
}
rawBytes, err := asn1.Marshal(EncodedTestBlock{
Data: t.Data,
Metadata: mdBuff,
Blacklist: blBytes,
})
if err != nil {
panic(fmt.Sprintf("failed to serialize test block: %v", err))
}
return rawBytes
}
func (t *TestBlock) Size() int {
return len(t.Bytes())
}
type EncodedTestBlock struct {
Data []byte
Metadata []byte
Blacklist []byte
}
type BlockDeserializer struct {
// DelayedVerification will block verifying any deserialized blocks until we send to the channel
DelayedVerification chan struct{}
}
func (b *BlockDeserializer) DeserializeBlock(ctx context.Context, buff []byte) (common.Block, error) {
var encodedBlock EncodedTestBlock
_, err := asn1.Unmarshal(buff, &encodedBlock)
if err != nil {
return nil, err
}
md, err := common.ProtocolMetadataFromBytes(encodedBlock.Metadata)
if err != nil {
return nil, err
}
var blacklist common.Blacklist
if err := blacklist.FromBytes(encodedBlock.Blacklist); err != nil {
return nil, err
}
tb := TestBlock{
blacklist: blacklist,
Metadata: *md,
VerificationDelay: b.DelayedVerification,
}
if len(encodedBlock.Data) > 0 {
tb.Data = encodedBlock.Data
}
tb.ComputeDigest()
return &tb, nil
}
type InnerBlock struct {
TS time.Time
BlockHeight uint64
Content []byte
}
func (i *InnerBlock) Bytes() []byte {
return i.Content
}
func (i *InnerBlock) Digest() [32]byte {
return sha256.Sum256(i.Content)
}
func (i *InnerBlock) Height() uint64 {
return i.BlockHeight
}
func (i *InnerBlock) Timestamp() time.Time {
return i.TS
}
func (i *InnerBlock) Verify(_ context.Context, _ uint64) error {
return nil
}