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

147 lines
2.8 KiB
Go

// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package random_network
import (
"context"
"crypto/sha256"
"encoding/asn1"
"fmt"
"github.com/luxfi/bft/common"
)
var _ common.Block = (*Block)(nil)
type Block struct {
blacklist common.Blacklist
// contents
txs []*TX
// protocol metadata
metadata common.ProtocolMetadata
digest common.Digest
// mempool access
mempool *Mempool
}
func NewBlock(metadata common.ProtocolMetadata, blacklist common.Blacklist, mempool *Mempool, txs []*TX) *Block {
b := &Block{
mempool: mempool,
txs: txs,
metadata: metadata,
blacklist: blacklist,
}
b.ComputeAndSetDigest()
return b
}
func (b *Block) Verify(ctx context.Context) (common.VerifiedBlock, error) {
return b, b.mempool.VerifyBlock(ctx, b)
}
func (b *Block) Blacklist() common.Blacklist {
return b.blacklist
}
func (b *Block) BlockHeader() common.BlockHeader {
return common.BlockHeader{
ProtocolMetadata: b.metadata,
Digest: b.digest,
}
}
func (b *Block) SealingBlockInfo() *common.SealingBlockInfo {
return nil
}
type encodedBlock struct {
ProtocolMetadata []byte
TXs []asn1TX
Blacklist []byte
}
func (b *Block) Bytes() []byte {
mdBytes := b.metadata.Bytes()
var asn1TXs []asn1TX
for _, tx := range b.txs {
asn1TXs = append(asn1TXs, asn1TX{ID: tx.ID[:], ShouldFailVerification: tx.shouldFailVerification})
}
blacklistBytes := b.blacklist.Bytes()
encodedB := encodedBlock{
ProtocolMetadata: mdBytes,
TXs: asn1TXs,
Blacklist: blacklistBytes,
}
raw, err := asn1.Marshal(encodedB)
if err != nil {
panic(fmt.Sprintf("failed to serialize block: %v", err))
}
return raw
}
func (b *Block) Size() int {
return len(b.Bytes())
}
func (b *Block) containsTX(txID txID) bool {
for _, tx := range b.txs {
if tx.ID == txID {
return true
}
}
return false
}
func (b *Block) ComputeAndSetDigest() {
b.digest = sha256.Sum256(b.Bytes())
}
type BlockDeserializer struct {
mempool *Mempool
}
var _ common.BlockDeserializer = (*BlockDeserializer)(nil)
func (bd *BlockDeserializer) DeserializeBlock(ctx context.Context, buff []byte) (common.Block, error) {
var encodedBlock encodedBlock
_, err := asn1.Unmarshal(buff, &encodedBlock)
if err != nil {
return nil, err
}
md, err := common.ProtocolMetadataFromBytes(encodedBlock.ProtocolMetadata)
if err != nil {
return nil, err
}
var blacklist common.Blacklist
if err := blacklist.FromBytes(encodedBlock.Blacklist); err != nil {
return nil, err
}
txs := make([]*TX, len(encodedBlock.TXs))
for i, asn1Tx := range encodedBlock.TXs {
tx := asn1Tx.toTX()
txs[i] = tx
}
b := &Block{
metadata: *md,
txs: txs,
blacklist: blacklist,
mempool: bd.mempool,
}
b.ComputeAndSetDigest()
return b, nil
}