Files
utxo/wire/pchain_owner.go
zeekayandHanzo Dev 0b2ce65281 wire: pool builders + SetBytesFixed for IDs — 1.9x faster X-tx composition
Write-path hot fix across all 21 wire constructors:
- zap.NewBuilder(capEstimate) -> zap.GetBuilder()/defer PutBuilder: recycle the
  Builder + its grown backing array (needs zap v1.2.2). All 21 return
  writeEnvelopePrefix(...b.Finish()), which COPIES out before the deferred Put,
  so pooling is safe.
- 7 per-byte ID loops (for i<32 { SetUint8(off+i, id[i]) }) -> one SetBytesFixed
  memcpy (TxID/AssetID/BlockchainID in utxo, transferable, xvm_base_tx;
  AttestedHash in attestation).
- rides zap v1.2.2's zero-copy SetBytes (no per-field heap copy).

Composite X-chain money-move (2out+2in, secp256k1fx) wire build:
2551ns/5328B/37allocs -> 1345ns/2401B/19allocs (1.9x faster, 2.2x less mem,
48% fewer allocs). Byte output UNCHANGED — full round-trip suite (11 pkgs) green.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-13 03:43:05 -07:00

111 lines
3.1 KiB
Go

// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package wire
import (
"github.com/luxfi/ids"
"github.com/luxfi/zap"
)
// PChainOwner is the cross-VM ZAP schema for
// github.com/luxfi/node/vms/platformvm/warp/message.PChainOwner. Strict
// subset of OutputOwners: Threshold + Addresses, no Locktime.
//
// Used in warp validator registration where locktime is not a meaningful
// gate (validator weight changes happen at known points in the
// permissionless validator pipeline).
//
// Fixed-section layout (size 12 bytes):
//
// Threshold uint32 @ 0
// AddressList list @ 4 (4-byte relOffset + 4-byte length, 8 bytes)
//
// Total fixed section = 12 bytes.
const (
OffsetPChainOwner_Threshold = 0 // uint32
OffsetPChainOwner_AddressList = 4 // list (8 bytes)
SizePChainOwner = 12
)
// PChainOwner is the zero-copy typed accessor over a ZAP-encoded
// PChainOwner wire envelope.
type PChainOwner struct {
msg *zap.Message
obj zap.Object
}
// Threshold returns the signatures-required count.
func (p PChainOwner) Threshold() uint32 {
return p.obj.Uint32(OffsetPChainOwner_Threshold)
}
// AddressList returns the variable-stride address list view.
func (p PChainOwner) AddressList() AddressList {
return AddressList{list: p.obj.ListStride(OffsetPChainOwner_AddressList, AddressStride)}
}
// IsZero reports whether the accessor wraps a parsed message.
func (p PChainOwner) IsZero() bool { return p.msg == nil }
// SyntacticVerify enforces the executor-side semantic gates: non-empty
// addresses, non-zero threshold, threshold <= len(addresses), every
// address non-zero. Returns one of the OutputOwners error sentinels.
func (p PChainOwner) SyntacticVerify() error {
addrs := p.AddressList()
n := addrs.Len()
if n == 0 {
return ErrOwnerAddrsEmpty
}
t := p.Threshold()
if t == 0 {
return ErrOwnerThresholdZero
}
if uint64(t) > uint64(n) {
return ErrOwnerThresholdExceedsAddrs
}
for i := 0; i < n; i++ {
if addrs.At(i) == (ids.ShortID{}) {
return ErrOwnerAddrZero
}
}
return nil
}
// WrapPChainOwner parses a PChainOwner wire envelope into a typed
// accessor.
func WrapPChainOwner(b []byte) (PChainOwner, error) {
_, sk, zapBytes, err := readEnvelopePrefix(b)
if err != nil {
return PChainOwner{}, err
}
if sk != ShapeKindPChainOwner {
return PChainOwner{}, ErrWrongShapeKind
}
msg, err := zap.Parse(zapBytes)
if err != nil {
return PChainOwner{}, err
}
return PChainOwner{msg: msg, obj: msg.Root()}, nil
}
// PChainOwnerInput is the constructor input for NewPChainOwner.
type PChainOwnerInput struct {
Threshold uint32
Addresses []ids.ShortID
}
// NewPChainOwner builds a PChainOwner wire envelope.
func NewPChainOwner(in PChainOwnerInput) []byte {
b := zap.GetBuilder()
defer zap.PutBuilder(b)
addrListOff, addrListCount := writeAddressList(b, in.Addresses)
ob := b.StartObject(SizePChainOwner)
ob.SetUint32(OffsetPChainOwner_Threshold, in.Threshold)
ob.SetList(OffsetPChainOwner_AddressList, addrListOff, addrListCount)
ob.FinishAsRoot()
return writeEnvelopePrefix(TypeKindReserved, ShapeKindPChainOwner, b.Finish())
}