Files
node/vms/xvm/txs/executor/backend.go
T
zeekayandHanzo Dev 4d712c3798 xvm: drop reflect from fx dispatch — closed-sum type switch + dense tag array
The fx set is a CLOSED sum type (secp256k1fx | nftfx | propertyfx), fixed at
compile time — no hot-loading, no genesis-configured fx. So the runtime
'which fx owns this value' dispatch needs no reflection: it is a total match on
the variant tag. Replaced reflect.TypeOf(val) + map[reflect.Type]int with:
  - fxKindOf(val): a Go type switch over the closed set of fx primitive types
    (compiler-checked exhaustive; lowers to a jump on the interface type tag),
    returning the value's wire.TypeKind — the same family tag the wire envelope
    already carries.
  - FxIndex: a dense [16]int array indexed by that TypeKind (one bounds-checked
    load; -1 = unregistered), filled by the SAME fx.(type) switch NewCustomParser
    already ran — no separate reflect registration.
getFx (semantic verifier + tx_init) is now fxKindOf → array index: zero reflect,
zero map-hash, compile-time-checked. Deleted registerFxTypes + all
map[reflect.Type]int fields/params (parser, block/parser, vm, backend). Node-only
(uses already-imported utxo fx types + wire.TypeKind); no dep cascade.

Full xvm suite green (11 pkgs — secp/nft/property verify dispatch exercised).
This removes the LAST reflection from the X-chain tx/verify path.

Co-authored-by: Hanzo Dev <dev@hanzo.ai>
2026-07-14 10:02:52 -07:00

70 lines
1.7 KiB
Go

// Copyright (C) 2019-2025, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package executor
import (
"context"
"github.com/luxfi/ids"
"github.com/luxfi/log"
"github.com/luxfi/node/vms/components/verify"
"github.com/luxfi/node/vms/xvm/config"
"github.com/luxfi/node/vms/xvm/fxs"
"github.com/luxfi/node/vms/xvm/txs"
"github.com/luxfi/runtime"
)
type Backend struct {
Ctx context.Context
Runtime *runtime.Runtime
Config *config.Config
Fxs []*fxs.ParsedFx
FxIndex *txs.FxIndex
// Note: FeeAssetID may be different than ctx.UTXOAssetID if this XVM is
// running in a chain.
FeeAssetID ids.ID
Bootstrapped bool
// Chain IDs for cross-chain operations
XChainID ids.ID
CChainID ids.ID
// Logger for this backend
Log log.Logger
// SharedMemory provides cross-chain atomic operations
SharedMemory SharedMemory
}
// SharedMemory interface for cross-chain operations
type SharedMemory interface {
Get(peerChainID ids.ID, keys [][]byte) ([][]byte, error)
Apply(requests map[ids.ID]interface{}, batch ...interface{}) error
}
// ToChainContext creates a verify.ChainContext from this backend
func (b *Backend) ToChainContext() *verify.ChainContext {
var (
chainID ids.ID
netID ids.ID
vs runtime.ValidatorState
)
if b.Runtime != nil {
chainID = b.Runtime.ChainID
if runtimeVS, ok := b.Runtime.ValidatorState.(runtime.ValidatorState); ok {
vs = runtimeVS
if resolvedNetID, err := runtimeVS.GetNetworkID(chainID); err == nil {
netID = resolvedNetID
}
}
}
return &verify.ChainContext{
ChainID: chainID,
NetID: netID,
ValidatorState: vs,
}
}