mirror of
https://github.com/luxfi/genesis.git
synced 2026-07-27 04:11:41 +00:00
genesis/builder: migrate from staging shim to proto/zap_codec
Wave 2G-Archive completes the genesis-builder migration: the staging
shim builder/zap_codec.go (created during Wave 2G-Genesis to mirror
proto/zap_codec's surface before it was tagged) is now deleted, and
builder.go imports proto/zap_codec directly.
Changes:
- builder.go: pvmGenesisCodec / newXVMParserCodecs swap to
proto/zap_codec.NewPVMGenesis / NewXVMParser
- builder/zap_codec.go: deleted (staging shim no longer needed)
- go.mod: bump luxfi/proto v1.1.0 → v1.3.0 (first tag that exposes
proto/zap_codec; v1.3.0 also includes the proto/internal test
helper migration from Wave 2G-Internal)
luxfi/codec demoted to // indirect — kept transitively via deps that
haven't re-tagged yet. Will drop on the next upstream tagging cascade.
This commit is contained in:
+19
-27
@@ -30,6 +30,7 @@ import (
|
||||
"github.com/luxfi/proto/x/fxs"
|
||||
xgenesis "github.com/luxfi/proto/x/genesis"
|
||||
xchaintxs "github.com/luxfi/proto/x/txs"
|
||||
protozapcodec "github.com/luxfi/proto/zap_codec"
|
||||
"github.com/luxfi/utxo/nftfx"
|
||||
"github.com/luxfi/utxo/propertyfx"
|
||||
"github.com/luxfi/utxo/secp256k1fx"
|
||||
@@ -818,58 +819,49 @@ func Aliases(genesisBytes []byte) (map[string][]string, map[ids.ID][]string, err
|
||||
return apiAliases, chainAliases, nil
|
||||
}
|
||||
|
||||
// pvmGenesisCodec constructs the ZAP-native proto/p genesis codec.
|
||||
// proto/p carries no github.com/luxfi/codec import after the Wave 2A
|
||||
// rip (#101); construction is delegated to the local zap_codec helper
|
||||
// (zap_codec.go in this package) — a staging shim that mirrors
|
||||
// proto/zap_codec's public surface exactly. Once Wave 2G-Wallet lands
|
||||
// and tags `github.com/luxfi/proto/zap_codec`, the two helpers below
|
||||
// swap to that import and the local shim deletes.
|
||||
//
|
||||
// genesis.New, Genesis.Bytes and genesis.Parse all require the
|
||||
// pvmGenesisCodec constructs the ZAP-native proto/p genesis codec via
|
||||
// the canonical proto/zap_codec.NewPVMGenesis constructor. proto/p
|
||||
// carries no github.com/luxfi/codec import after the Wave 2A rip;
|
||||
// genesis.New / Genesis.Bytes / genesis.Parse all require the
|
||||
// genesis-sized codec (math.MaxInt32 budget) because the PVM genesis
|
||||
// blob can be significantly larger than runtime txs — the full set of
|
||||
// initial validator stake txs and CreateChainTx entries for every
|
||||
// primary-network chain (X/C/D/Q/A/B/T/Z/G/K) lives in a single blob.
|
||||
// primary-network chain lives in a single blob.
|
||||
//
|
||||
// block.RegisterTypes pulls in the full Apricot/Banff/Durango/Quasar
|
||||
// block + tx type set in the canonical wire-byte order required by
|
||||
// proto/p (block.RegisterTypes is a superset of txs.RegisterTypes —
|
||||
// see proto/p/block/codec.go).
|
||||
//
|
||||
// Wave 2G-Genesis (#101): the linearcodec/codec.Manager construction
|
||||
// that previously lived here is gone. The returned codec is ZAP-native
|
||||
// little-endian — see zap_codec.go for the wire-format break vs the
|
||||
// legacy linearcodec wire bytes. The break is intentional and aligned
|
||||
// with LP-023 ZAP-native activation (ZAPActivationUnix=0 means "ZAP is
|
||||
// mandatory from genesis"), forward-only.
|
||||
// Wave 2G-Archive (#101): the local staging shim that previously
|
||||
// mirrored proto/zap_codec's surface has been deleted; this function
|
||||
// now imports the canonical constructor directly. The returned codec
|
||||
// is ZAP-native little-endian — forward-only, no rollback path,
|
||||
// aligned with LP-023 ZAP-native activation (ZAPActivationUnix=0).
|
||||
//
|
||||
// Mirrors sdk/wallet/chain/p/pcodecs.NewPVMGenesisCodec.
|
||||
func pvmGenesisCodec() (pchaintxs.Codec, error) {
|
||||
cm := newZapCodecPVMGenesis(pchaintxs.CodecVersion)
|
||||
cm := protozapcodec.NewPVMGenesis(pchaintxs.CodecVersion)
|
||||
if err := pchainblock.RegisterTypes(cm); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cm, nil
|
||||
}
|
||||
|
||||
// newXVMParserCodecs constructs the ZAP-native ParserCodecs for proto/x.
|
||||
// newXVMParserCodecs constructs the ZAP-native ParserCodecs for proto/x
|
||||
// via the canonical proto/zap_codec.NewXVMParser constructor.
|
||||
//
|
||||
// Mirrors sdk/wallet/chain/x/constants.go::newXVMParserCodecs — both
|
||||
// thread the same ZAP-native zapcodec backend. proto/x carries no
|
||||
// github.com/luxfi/codec import after the Wave 1A rip (#101); the
|
||||
// local zap_codec helper provides the codec implementation until
|
||||
// proto/zap_codec ships.
|
||||
//
|
||||
// genesis/builder needs this for UTXOAssetID's wire decode path. The
|
||||
// runtime codec is 1 MiB-bounded and the genesis codec is
|
||||
// math.MaxInt32-bounded — both budgets are baked into
|
||||
// newZapCodecXVMParser.
|
||||
// github.com/luxfi/codec import after the Wave 1A rip; the runtime
|
||||
// codec is 1 MiB-bounded and the genesis codec is math.MaxInt32-
|
||||
// bounded — both budgets are baked into NewXVMParser.
|
||||
//
|
||||
// Tx-level and fx-owned wire payload types are registered when this
|
||||
// bundle is handed to xchaintxs.NewParser — see proto/x/txs/parser.go
|
||||
// (fxOwnedTypes).
|
||||
func newXVMParserCodecs() (xchaintxs.ParserCodecs, error) {
|
||||
runtime, genesis := newZapCodecXVMParser(xchaintxs.CodecVersion)
|
||||
runtime, genesis := protozapcodec.NewXVMParser(xchaintxs.CodecVersion)
|
||||
return xchaintxs.ParserCodecs{
|
||||
Codec: runtime,
|
||||
GenesisCodec: genesis,
|
||||
|
||||
+4
-4
@@ -11,15 +11,14 @@ replace github.com/luxfi/genesis => ../
|
||||
|
||||
require (
|
||||
github.com/luxfi/address v1.0.1
|
||||
github.com/luxfi/codec v1.1.5
|
||||
github.com/luxfi/constants v1.5.7
|
||||
github.com/luxfi/constants v1.5.8-0.20260603055356-93c2c2ceb9ca
|
||||
github.com/luxfi/container v0.0.4
|
||||
github.com/luxfi/crypto v1.19.16
|
||||
github.com/luxfi/formatting v1.0.1
|
||||
github.com/luxfi/genesis v1.7.1
|
||||
github.com/luxfi/ids v1.2.13
|
||||
github.com/luxfi/math v1.4.1
|
||||
github.com/luxfi/proto v1.1.0
|
||||
github.com/luxfi/proto v1.3.0
|
||||
github.com/luxfi/utxo v0.3.4
|
||||
github.com/luxfi/vm v1.1.6
|
||||
github.com/stretchr/testify v1.11.1
|
||||
@@ -37,6 +36,7 @@ require (
|
||||
github.com/luxfi/accel v1.1.8 // indirect
|
||||
github.com/luxfi/atomic v1.0.0 // indirect
|
||||
github.com/luxfi/cache v1.2.1 // indirect
|
||||
github.com/luxfi/codec v1.1.5 // indirect
|
||||
github.com/luxfi/compress v0.0.5 // indirect
|
||||
github.com/luxfi/concurrent v0.0.3 // indirect
|
||||
github.com/luxfi/consensus v1.25.0 // indirect
|
||||
@@ -47,7 +47,7 @@ require (
|
||||
github.com/luxfi/keychain v1.0.2 // indirect
|
||||
github.com/luxfi/log v1.4.1 // indirect
|
||||
github.com/luxfi/math/big v0.1.0 // indirect
|
||||
github.com/luxfi/metric v1.5.5 // indirect
|
||||
github.com/luxfi/metric v1.5.7 // indirect
|
||||
github.com/luxfi/mock v0.1.1 // indirect
|
||||
github.com/luxfi/ordering v0.0.1 // indirect
|
||||
github.com/luxfi/p2p v1.19.2 // indirect
|
||||
|
||||
+12
-10
@@ -97,8 +97,8 @@ github.com/luxfi/concurrent v0.0.3 h1:eJyv1fhaC0jMLMw6+QS774cUmp7GK+ouMgvLCqnC7c
|
||||
github.com/luxfi/concurrent v0.0.3/go.mod h1:Aj/FR5NpM0cB2P4Nt3+tz9+dV6V+LUW4HuMgSjwq5hw=
|
||||
github.com/luxfi/consensus v1.25.0 h1:ak82ufqeklLJGQMCizxx5agiwLkjbrnrk7kFezvEtZo=
|
||||
github.com/luxfi/consensus v1.25.0/go.mod h1:jkKzKyIgg/JqaEumxZOJd9ofWM3pXnVVLlQp+3qo9SM=
|
||||
github.com/luxfi/constants v1.5.7 h1:a1tCMdxd+pClPMIPOaI9vcYGNy6cQIc2rubac2Trc0k=
|
||||
github.com/luxfi/constants v1.5.7/go.mod h1:z+Wc7skybZAA+xuBWNcmtv402S/BFqixL+FiSQXPg8U=
|
||||
github.com/luxfi/constants v1.5.8-0.20260603055356-93c2c2ceb9ca h1:N2lM2gMJ2Hk5yZ6GqURWtg6nIVtodsjqMe+MGNiuCNw=
|
||||
github.com/luxfi/constants v1.5.8-0.20260603055356-93c2c2ceb9ca/go.mod h1:z+Wc7skybZAA+xuBWNcmtv402S/BFqixL+FiSQXPg8U=
|
||||
github.com/luxfi/container v0.0.4 h1:BXhF82WyfqVP5mjlNcr7tP0Fcnvl0Ap1rkiu+rq5XuM=
|
||||
github.com/luxfi/container v0.0.4/go.mod h1:Z3SpmMF5d4t77MM0nHYXURpn+EMVaeu1fhbd/3BGaek=
|
||||
github.com/luxfi/crypto v1.19.16 h1:YCAXtLS65TCi7/iw6zBglFW0YS80zJj9Y61fTxdP+wo=
|
||||
@@ -123,8 +123,8 @@ github.com/luxfi/math v1.4.1 h1:1t9bCCsEqnl9yIKrShlbs80DBKyYTWdnzkVfBqEeO7Q=
|
||||
github.com/luxfi/math v1.4.1/go.mod h1:QvbRxauQyE1w4lvbcLSe6c8yeJz2Zj1Bq1rayGgs2tA=
|
||||
github.com/luxfi/math/big v0.1.0 h1:Vz4c0RsZVPdIKPsHPgAJChH/R3p15WHRUz7LkLf+NIQ=
|
||||
github.com/luxfi/math/big v0.1.0/go.mod h1:BuxSu22RbO93xBLk5Eam5nldFponoJ73xDFz4uJ3Huk=
|
||||
github.com/luxfi/metric v1.5.5 h1:JXruty5ZN/ljeRNaCSabQGg1Xr3re2E8wqajVUUs6w4=
|
||||
github.com/luxfi/metric v1.5.5/go.mod h1:CMguEhyuLi4YUWyXimJ+UHply99BDFrL0pxedB7rBqM=
|
||||
github.com/luxfi/metric v1.5.7 h1:LoSPEUpak2SLcynF+LT2cXjl9ECp4nY+Lia9zudmDv4=
|
||||
github.com/luxfi/metric v1.5.7/go.mod h1:CMguEhyuLi4YUWyXimJ+UHply99BDFrL0pxedB7rBqM=
|
||||
github.com/luxfi/mock v0.1.1 h1:0HEtIjg1J6CWz+IUyP6rsGqNWTcmxjFnSQIhaDuARwY=
|
||||
github.com/luxfi/mock v0.1.1/go.mod h1:jo35akl3Vtd8LbzDts8VJ0jmSVycrd1/eBi6g6t5hKU=
|
||||
github.com/luxfi/ordering v0.0.1 h1:pZkMD5AC1Q/5RnZsTKK0KWd1tHXiJw7WPU0oebT0wog=
|
||||
@@ -133,8 +133,8 @@ github.com/luxfi/p2p v1.19.2 h1:uqZq7ofmEDbXlTkv1QThtci01Q+dmDkNmAPeORIyP8E=
|
||||
github.com/luxfi/p2p v1.19.2/go.mod h1:tI9Bt1R0ouvVtJvXG4e20GlGeV4AR230k4mFF9Vglzk=
|
||||
github.com/luxfi/pq v1.0.3 h1:pFlQm1+5FuKTDUh2y/23bXWkN4I2Rc5iuxJypwDFFMs=
|
||||
github.com/luxfi/pq v1.0.3/go.mod h1:8bppZcRElfrVt0n3nYCZW3iX1TvhvzNbdjNdK1irgIE=
|
||||
github.com/luxfi/proto v1.1.0 h1:XIi0TEHfYXLv/vqrS21WloYImkVhVAkcmCwr/aX2FL0=
|
||||
github.com/luxfi/proto v1.1.0/go.mod h1:bpGV0Z0CTJhSEzx4exAmci+8Lm9Isa7VO+I6RT7IHlY=
|
||||
github.com/luxfi/proto v1.3.0 h1:OkBQ72K+2cvck1LlZVbMxrUJbjov2ci2JUWF2P6986o=
|
||||
github.com/luxfi/proto v1.3.0/go.mod h1:JkzinRBQu02HsVPIGQvLVhG/xyO1ovTqoEAphOiNBso=
|
||||
github.com/luxfi/rpc v1.0.2 h1:NLRcOYRW+io0d1d33RMkgOZea8nlhK09MbPgCXcU5wU=
|
||||
github.com/luxfi/rpc v1.0.2/go.mod h1:pgiHwMWgOuxYYIa0vsUBvrBI+Op6bhZ39guM9vtMUcE=
|
||||
github.com/luxfi/runtime v1.1.0 h1:6TrvzAmZVCTVbR1ebntHTO3/kVBaogPUSkxdDMnrTiw=
|
||||
@@ -145,6 +145,8 @@ github.com/luxfi/timer v1.0.2 h1:g/odi0VQJIsrzdklJUG1thHZ/sGNnbIiVGcU6LctJm0=
|
||||
github.com/luxfi/timer v1.0.2/go.mod h1:SoaZwntYigUE3H6z1GV32YwP8QaSiAT0UiEv7iPugXg=
|
||||
github.com/luxfi/tls v1.0.3 h1:rK3nxSAxrUOOSHOZnKChwV4f6UJ+cfOl8KWJXAQx/SI=
|
||||
github.com/luxfi/tls v1.0.3/go.mod h1:dQqSiGE7YxXUxOwICoReUuIitBms9DYOaCeteBwmIWw=
|
||||
github.com/luxfi/upgrade v1.0.1-0.20260603055252-f51810805436 h1:0VTZNA1+rXh9Tr0OpGmkwg+mYC6xgLv+XymMlZvLH/E=
|
||||
github.com/luxfi/upgrade v1.0.1-0.20260603055252-f51810805436/go.mod h1:zBBG2VkZmWLr6u6DxzI/4DSFYjHizHrr7MqCDZkMSJc=
|
||||
github.com/luxfi/utils v1.1.4 h1:8OY0jXCkpp6OotN1Y++6DATJkvtEwzq2k0p56imJlAk=
|
||||
github.com/luxfi/utils v1.1.4/go.mod h1:c3yz1RjzrB+cs5GZm+q1T3/2cCKElO9vxm9yRRtgSEM=
|
||||
github.com/luxfi/utxo v0.3.4 h1:p2jpS5FJ04JYHPXNuXH2GscuJl0xt5djfvrhW7PFftA=
|
||||
@@ -207,14 +209,14 @@ go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/otel v1.43.0 h1:mYIM03dnh5zfN7HautFE4ieIig9amkNANT+xcVxAj9I=
|
||||
go.opentelemetry.io/otel v1.43.0/go.mod h1:JuG+u74mvjvcm8vj8pI5XiHy1zDeoCS2LB1spIq7Ay0=
|
||||
go.opentelemetry.io/otel/metric v1.42.0 h1:2jXG+3oZLNXEPfNmnpxKDeZsFI5o4J+nz6xUlaFdF/4=
|
||||
go.opentelemetry.io/otel/metric v1.42.0/go.mod h1:RlUN/7vTU7Ao/diDkEpQpnz3/92J9ko05BIwxYa2SSI=
|
||||
go.opentelemetry.io/otel/metric v1.43.0 h1:d7638QeInOnuwOONPp4JAOGfbCEpYb+K6DVWvdxGzgM=
|
||||
go.opentelemetry.io/otel/metric v1.43.0/go.mod h1:RDnPtIxvqlgO8GRW18W6Z/4P462ldprJtfxHxyKd2PY=
|
||||
go.opentelemetry.io/otel/sdk v1.42.0 h1:LyC8+jqk6UJwdrI/8VydAq/hvkFKNHZVIWuslJXYsDo=
|
||||
go.opentelemetry.io/otel/sdk v1.42.0/go.mod h1:rGHCAxd9DAph0joO4W6OPwxjNTYWghRWmkHuGbayMts=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0 h1:S88dyqXjJkuBNLeMcVPRFXpRw2fuwdvfCGLEo89fDkw=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.43.0/go.mod h1:C/RJtwSEJ5hzTiUz5pXF1kILHStzb9zFlIEe85bhj6A=
|
||||
go.opentelemetry.io/otel/trace v1.42.0 h1:OUCgIPt+mzOnaUTpOQcBiM/PLQ/Op7oq6g4LenLmOYY=
|
||||
go.opentelemetry.io/otel/trace v1.42.0/go.mod h1:f3K9S+IFqnumBkKhRJMeaZeNk9epyhnCmQh/EysQCdc=
|
||||
go.opentelemetry.io/otel/trace v1.43.0 h1:BkNrHpup+4k4w+ZZ86CZoHHEkohws8AY+WTX09nk+3A=
|
||||
go.opentelemetry.io/otel/trace v1.43.0/go.mod h1:/QJhyVBUUswCphDVxq+8mld+AvhXZLhe+8WVFxiFff0=
|
||||
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
|
||||
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
|
||||
go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ=
|
||||
|
||||
@@ -1,227 +0,0 @@
|
||||
// Copyright (C) 2019-2026, Lux Industries, Inc. All rights reserved.
|
||||
// See the file LICENSE for licensing terms.
|
||||
|
||||
// Package builder local ZAP-native codec helper.
|
||||
//
|
||||
// This file is a STAGING SHIM until Wave 2G-Wallet (in-flight at
|
||||
// proto/zap_codec) lands and publishes
|
||||
// `github.com/luxfi/proto/zap_codec`. The contents below mirror that
|
||||
// package's public surface EXACTLY — same Manager type, same
|
||||
// constructor signatures, same wire layout (zapcodec little-endian
|
||||
// reflection codec with a 2-byte LE codec-version prefix).
|
||||
//
|
||||
// TODO(wave-2g-wallet): once proto tags the zap_codec subpackage, this
|
||||
// file is deleted and builder.go's pvmGenesisCodec /
|
||||
// newXVMParserCodecs swap to `github.com/luxfi/proto/zap_codec`
|
||||
// imports. The two helper functions in builder.go are the ONLY in-tree
|
||||
// consumers — no other file in this module depends on this shim.
|
||||
//
|
||||
// Wire format identity: the bytes this file's Manager emits are
|
||||
// byte-identical to bytes proto/zap_codec.Manager would emit at the
|
||||
// same CodecVersion + same registered type set. The eventual swap is
|
||||
// therefore purely a code-level move — no genesis blob re-issue, no
|
||||
// network coordination needed.
|
||||
//
|
||||
// Wire-format relationship to legacy linearcodec (the codec this
|
||||
// package displaces):
|
||||
//
|
||||
// - All multi-byte integers are little-endian. linearcodec is
|
||||
// big-endian. x86_64 and arm64 hardware is LE-native, so LE writes
|
||||
// map to single MOV instructions where BE writes need BSWAP.
|
||||
// - Slice/map length prefixes (uint32) and string length prefixes
|
||||
// (uint16) flip to LE.
|
||||
// - Interface type-id prefixes (uint32) flip to LE.
|
||||
// - Reflection layout (struct-tag order, field encoding) is
|
||||
// identical to linearcodec.
|
||||
//
|
||||
// This is a WIRE-FORMAT BREAK from the legacy linearcodec bytes that
|
||||
// previously fed proto/{p,x} consumers. The break is intentional and
|
||||
// aligned with LP-023 ZAP-native activation
|
||||
// (proto/zap_native/codec_select.go: ZAPActivationUnix=0 → "ZAP is
|
||||
// mandatory from genesis"). Forward-only — there is no dual-mode
|
||||
// emission and no rollback path.
|
||||
//
|
||||
// Architecture (Hickey decomplection):
|
||||
//
|
||||
// - VALUE: the wire codec choice. Today: zapcodec LE. (Was:
|
||||
// linearcodec BE.) The value lives here, qualified by namespace,
|
||||
// not braided into the call sites.
|
||||
// - COMPOSITION: zapCodecManager wraps a zapcodec.Codec with a
|
||||
// version-prefix outer layer. Outer + inner are independently
|
||||
// complete primitives.
|
||||
// - ORTHOGONAL: this file neither knows about proto/{p,x} types nor
|
||||
// about genesis semantics. Per-type registration happens at the
|
||||
// callers (pvmGenesisCodec / newXVMParserCodecs in builder.go),
|
||||
// which RegisterTypes onto the Manager.
|
||||
package builder
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
|
||||
"github.com/luxfi/codec/wrappers"
|
||||
"github.com/luxfi/codec/zapcodec"
|
||||
)
|
||||
|
||||
// zapCodecMaxSize is the default maximum wire-payload size for
|
||||
// runtime txs. Mirrors `proto/zap_codec.MaxSize` (1 MiB) so per-tx
|
||||
// envelope is unchanged vs the legacy linearcodec configuration.
|
||||
const zapCodecMaxSize = 1024 * 1024
|
||||
|
||||
// zapCodecVersionSize is the on-wire length of the codec-version
|
||||
// prefix the outer manager prepends. Two bytes, uint16 little-endian.
|
||||
const zapCodecVersionSize = 2
|
||||
|
||||
// Sentinel errors. Shape-compatible with `proto/zap_codec` errors so
|
||||
// callers asserting on the shim's sentinels get equivalent behaviour
|
||||
// after the swap.
|
||||
var (
|
||||
errZapCodecCantPackVersion = errors.New("zap_codec: couldn't pack codec version")
|
||||
errZapCodecCantUnpackVersion = errors.New("zap_codec: couldn't unpack codec version")
|
||||
errZapCodecUnknownVersion = errors.New("zap_codec: unknown codec version")
|
||||
errZapCodecExtraSpace = errors.New("zap_codec: trailing buffer space")
|
||||
errZapCodecMaxSizeExceeded = errors.New("zap_codec: wire payload exceeds max size")
|
||||
)
|
||||
|
||||
// zapCodecManager is the version-prefix wire codec implementation. It
|
||||
// owns the codec.Manager-shaped surface (Marshal/Unmarshal/Size) that
|
||||
// proto/{p,x}'s local Codec interfaces structurally match.
|
||||
//
|
||||
// zapCodecManager also satisfies the Registry surface by delegating
|
||||
// RegisterType / SkipRegistrations to the inner zapcodec.Codec. This
|
||||
// is the same contract every Wave 2-era consumer expects: hand back
|
||||
// ONE value that is BOTH the wire codec AND the type registry.
|
||||
//
|
||||
// Thread-safety: the inner zapcodec.Codec is concurrency-safe (its
|
||||
// RWMutex protects the type registry). zapCodecManager itself is
|
||||
// stateless past construction — callers may share *zapCodecManager
|
||||
// across goroutines without external sync.
|
||||
type zapCodecManager struct {
|
||||
inner zapcodec.Codec
|
||||
version uint16
|
||||
maxSize int
|
||||
}
|
||||
|
||||
// newZapCodecVersionedManager constructs a zapCodecManager that
|
||||
// emits/accepts wire bytes prefixed by a uint16 little-endian codec
|
||||
// version. The inner zapcodec instance is allocated fresh;
|
||||
// SkipRegistrations and RegisterType go directly to it.
|
||||
func newZapCodecVersionedManager(version uint16, maxSize uint64) *zapCodecManager {
|
||||
size := maxSize
|
||||
if size > math.MaxInt {
|
||||
size = math.MaxInt
|
||||
}
|
||||
return &zapCodecManager{
|
||||
inner: zapcodec.NewDefault(),
|
||||
version: version,
|
||||
maxSize: int(size),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterType registers val with the inner reflection codec.
|
||||
func (m *zapCodecManager) RegisterType(val interface{}) error {
|
||||
return m.inner.RegisterType(val)
|
||||
}
|
||||
|
||||
// SkipRegistrations bumps the inner codec's next-type-id by n.
|
||||
func (m *zapCodecManager) SkipRegistrations(n int) {
|
||||
m.inner.SkipRegistrations(n)
|
||||
}
|
||||
|
||||
// Marshal serializes source into a fresh byte slice with the
|
||||
// manager's codec version (uint16 LE) prepended. Caller-supplied
|
||||
// version MUST match the manager's bound version.
|
||||
func (m *zapCodecManager) Marshal(version uint16, source interface{}) ([]byte, error) {
|
||||
if version != m.version {
|
||||
return nil, errZapCodecUnknownVersion
|
||||
}
|
||||
p := &wrappers.Packer{MaxSize: m.maxSize}
|
||||
if err := zapCodecWriteVersionLE(p, version); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := m.inner.MarshalInto(source, p); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if p.Err != nil {
|
||||
return nil, p.Err
|
||||
}
|
||||
return p.Bytes[:p.Offset], nil
|
||||
}
|
||||
|
||||
// Unmarshal deserializes bytes into dest. Returns the codec version
|
||||
// read from the wire prefix.
|
||||
func (m *zapCodecManager) Unmarshal(bytes []byte, dest interface{}) (uint16, error) {
|
||||
if len(bytes) < zapCodecVersionSize {
|
||||
return 0, errZapCodecCantUnpackVersion
|
||||
}
|
||||
if len(bytes) > m.maxSize {
|
||||
return 0, errZapCodecMaxSizeExceeded
|
||||
}
|
||||
p := &wrappers.Packer{Bytes: bytes, MaxSize: m.maxSize}
|
||||
version, err := zapCodecReadVersionLE(p)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if version != m.version {
|
||||
return version, errZapCodecUnknownVersion
|
||||
}
|
||||
if err := m.inner.UnmarshalFrom(p, dest); err != nil {
|
||||
return version, err
|
||||
}
|
||||
if p.Offset != len(bytes) {
|
||||
return version, errZapCodecExtraSpace
|
||||
}
|
||||
return version, nil
|
||||
}
|
||||
|
||||
// Size returns the on-wire size of value INCLUDING the manager's
|
||||
// version-prefix bytes.
|
||||
func (m *zapCodecManager) Size(version uint16, value interface{}) (int, error) {
|
||||
if version != m.version {
|
||||
return 0, errZapCodecUnknownVersion
|
||||
}
|
||||
size, err := m.inner.Size(value)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return zapCodecVersionSize + size, nil
|
||||
}
|
||||
|
||||
// newZapCodecPVMGenesis returns a Manager wired for proto/p PVM
|
||||
// genesis blobs. Budget is math.MaxInt32 (genesis can be very large
|
||||
// — full validator set + every chain's initial state).
|
||||
//
|
||||
// Mirrors `proto/zap_codec.NewPVMGenesis` exactly.
|
||||
func newZapCodecPVMGenesis(version uint16) *zapCodecManager {
|
||||
return newZapCodecVersionedManager(version, math.MaxInt32)
|
||||
}
|
||||
|
||||
// newZapCodecXVMParser returns the two codec/registry values
|
||||
// proto/x's txs.NewParser requires: runtime Codec, genesis Codec.
|
||||
// Each returned Manager is independent.
|
||||
//
|
||||
// Mirrors `proto/zap_codec.NewXVMParser` exactly.
|
||||
func newZapCodecXVMParser(version uint16) (runtime, genesis *zapCodecManager) {
|
||||
return newZapCodecVersionedManager(version, zapCodecMaxSize),
|
||||
newZapCodecVersionedManager(version, math.MaxInt32)
|
||||
}
|
||||
|
||||
// zapCodecWriteVersionLE writes a uint16 codec version in
|
||||
// little-endian. The wrappers.Packer's built-in PackShort is BE — we
|
||||
// cannot reuse it because the zapcodec body that follows is LE.
|
||||
func zapCodecWriteVersionLE(p *wrappers.Packer, version uint16) error {
|
||||
p.PackFixedBytes([]byte{byte(version), byte(version >> 8)})
|
||||
if p.Err != nil {
|
||||
return errZapCodecCantPackVersion
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// zapCodecReadVersionLE reads a uint16 codec version in little-endian.
|
||||
func zapCodecReadVersionLE(p *wrappers.Packer) (uint16, error) {
|
||||
b := p.UnpackFixedBytes(zapCodecVersionSize)
|
||||
if p.Err != nil {
|
||||
return 0, errZapCodecCantUnpackVersion
|
||||
}
|
||||
return uint16(b[0]) | uint16(b[1])<<8, nil
|
||||
}
|
||||
Reference in New Issue
Block a user