mirror of
https://github.com/luxfi/node.git
synced 2026-10-30 15:24:05 +00:00
The module already called itself github.com/luxfi/node while containing no node in Go: cmd/luxd was twenty-one lines that printed the size of a few types, and the Go column of the matrix measured a VM plugin. A repo that fetches the thing it claims to be has not replaced it, so the Go sources move in — 1515 files, the daemon and everything under it — and the build reaches outside this module for nothing. Rust and C++ sit beside it unchanged and are still measured against it by the differential. That is the point of one repo: the reference implementation and the two ports are the same checkout, so parity is a thing you can run rather than a thing you assert. Verified: builds, and the closure carries no lux-private — checked at build time rather than believed. The daemon starts twelve chains on a local network (P X C Q Z A B G K M F, plus D through its plugin) and reports healthy.
113 lines
3.8 KiB
Go
113 lines
3.8 KiB
Go
// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package chain
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/luxfi/runtime"
|
|
consensuschain "github.com/luxfi/vm/chain"
|
|
)
|
|
|
|
var (
|
|
_ consensuschain.Block = (*BlockWrapper)(nil)
|
|
_ consensuschain.WithVerifyRuntime = (*BlockWrapper)(nil)
|
|
)
|
|
|
|
// BlockWrapper wraps a linear Block while adding a smart caching layer to improve
|
|
// VM performance.
|
|
type BlockWrapper struct {
|
|
consensuschain.Block
|
|
|
|
state *State
|
|
}
|
|
|
|
// Verify verifies the underlying block, evicts from the unverified block cache
|
|
// and if the block passes verification, adds it to [cache.verifiedBlocks].
|
|
// Note: it is guaranteed that if a block passes verification it will be added to
|
|
// consensus and eventually be decided ie. either Accept/Reject will be called
|
|
// on [bw] removing it from [verifiedBlocks].
|
|
func (bw *BlockWrapper) Verify(ctx context.Context) error {
|
|
if err := bw.Block.Verify(ctx); err != nil {
|
|
// Note: we cannot cache blocks failing verification in case
|
|
// the error is temporary and the block could become valid in
|
|
// the future.
|
|
return err
|
|
}
|
|
|
|
blkID := bw.ID()
|
|
bw.state.unverifiedBlocks.Evict(blkID)
|
|
bw.state.verifiedBlocks[blkID] = bw
|
|
return nil
|
|
}
|
|
|
|
// VerifyWithRuntime verifies the underlying block with runtime
|
|
func (bw *BlockWrapper) VerifyWithRuntime(ctx context.Context, blockCtx *runtime.Runtime) error {
|
|
// If the embedded block supports context verification, use it
|
|
if withCtx, ok := bw.Block.(consensuschain.WithVerifyRuntime); ok {
|
|
shouldVerify, err := withCtx.ShouldVerifyWithRuntime(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if shouldVerify {
|
|
return withCtx.VerifyWithRuntime(ctx, blockCtx)
|
|
}
|
|
}
|
|
// Otherwise fall back to regular Verify
|
|
return bw.Verify(ctx)
|
|
}
|
|
|
|
// ShouldVerifyWithRuntime checks if the underlying block should be verified
|
|
// with a block context. If the underlying block does not implement the
|
|
// block.WithVerifyRuntime interface, returns false without an error. Does not
|
|
// touch any block cache.
|
|
func (bw *BlockWrapper) ShouldVerifyWithRuntime(ctx context.Context) (bool, error) {
|
|
blkWithCtx, ok := bw.Block.(consensuschain.WithVerifyRuntime)
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
return blkWithCtx.ShouldVerifyWithRuntime(ctx)
|
|
}
|
|
|
|
// VerifyWithRuntime verifies the underlying block with the given block context,
|
|
// evicts from the unverified block cache and if the block passes verification,
|
|
// adds it to [cache.verifiedBlocks].
|
|
// Note: it is guaranteed that if a block passes verification it will be added
|
|
// to consensus and eventually be decided ie. either Accept/Reject will be
|
|
// called on [bw] removing it from [verifiedBlocks].
|
|
//
|
|
// Note: If the underlying block does not implement the block.WithVerifyRuntime
|
|
// interface, an error is always returned because ShouldVerifyWithRuntime will
|
|
// always return false in this case and VerifyWithRuntime should never be
|
|
// called.
|
|
|
|
// Accept accepts the underlying block, removes it from verifiedBlocks, caches it as a decided
|
|
// block, and updates the last accepted block.
|
|
func (bw *BlockWrapper) Accept(ctx context.Context) error {
|
|
blkID := bw.ID()
|
|
delete(bw.state.verifiedBlocks, blkID)
|
|
bw.state.decidedBlocks.Put(blkID, bw)
|
|
bw.state.lastAcceptedBlock = bw
|
|
|
|
return bw.Block.Accept(ctx)
|
|
}
|
|
|
|
// Reject rejects the underlying block, removes it from processing blocks, and caches it as a
|
|
// decided block.
|
|
func (bw *BlockWrapper) Reject(ctx context.Context) error {
|
|
blkID := bw.ID()
|
|
delete(bw.state.verifiedBlocks, blkID)
|
|
bw.state.decidedBlocks.Put(blkID, bw)
|
|
return bw.Block.Reject(ctx)
|
|
}
|
|
|
|
// OracleBlock is a block that can have multiple valid children, and one needs
|
|
// to be chosen by an oracle.
|
|
type OracleBlock interface {
|
|
consensuschain.Block
|
|
|
|
// Options returns the block options that may be chosen by the oracle.
|
|
Options(context.Context) ([2]consensuschain.Block, error)
|
|
}
|