Files
utxo/wire/locked_output.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

86 lines
2.8 KiB
Go

// Copyright (C) 2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package wire
import "github.com/luxfi/zap"
// LockedOutput is the cross-fx ZAP schema for stakeable-locked outputs
// (the platformvm/stakeable.LockOut primitive). Carries a Locktime and
// an inner TransferableOut wire envelope (any fx's TransferOutput).
// Operationally: an output that cannot be spent until block.Timestamp
// >= Locktime, wrapping any fx's transfer output.
//
// Fixed-section layout (size 16 bytes):
//
// Locktime uint64 @ 0 (8 bytes — unlock unix time)
// TransferOutBytes bytes @ 8 (8 bytes — inner wire envelope)
//
// Wire prefix: TypeKind is TypeKindReserved (lock is fx-agnostic; inner
// envelope carries its own fx TypeKind). ShapeKind is
// ShapeKindLockedOutput (0x0F). The inner TransferOutBytes carries its
// own discriminator pair — consumers dispatch via WrapTransferOutput
// or the fx-specific wrap.
const (
OffsetLockedOutput_Locktime = 0 // uint64
OffsetLockedOutput_TransferOutBytes = 8 // bytes (8 bytes)
SizeLockedOutput = 16
)
// LockedOutput is the zero-copy typed accessor.
type LockedOutput struct {
msg *zap.Message
obj zap.Object
}
// Locktime returns the unix timestamp before which the inner output
// cannot be spent.
func (l LockedOutput) Locktime() uint64 {
return l.obj.Uint64(OffsetLockedOutput_Locktime)
}
// TransferOutBytes returns the inner TransferableOut wire envelope.
// Pass to the appropriate fx's WrapTransferOutput for typed access.
//
// READ-ONLY: aliases the underlying buffer.
func (l LockedOutput) TransferOutBytes() []byte {
return l.obj.Bytes(OffsetLockedOutput_TransferOutBytes)
}
// IsZero reports whether the accessor wraps a parsed message.
func (l LockedOutput) IsZero() bool { return l.msg == nil }
// WrapLockedOutput parses a LockedOutput wire envelope.
func WrapLockedOutput(b []byte) (LockedOutput, error) {
_, sk, zapBytes, err := readEnvelopePrefix(b)
if err != nil {
return LockedOutput{}, err
}
if sk != ShapeKindLockedOutput {
return LockedOutput{}, ErrWrongShapeKind
}
msg, err := zap.Parse(zapBytes)
if err != nil {
return LockedOutput{}, err
}
return LockedOutput{msg: msg, obj: msg.Root()}, nil
}
// LockedOutputInput is the constructor input.
type LockedOutputInput struct {
Locktime uint64
TransferOutBytes []byte // wire envelope from any fx's NewTransferOutput
}
// NewLockedOutput builds a LockedOutput wire envelope.
func NewLockedOutput(in LockedOutputInput) []byte {
b := zap.GetBuilder()
defer zap.PutBuilder(b)
ob := b.StartObject(SizeLockedOutput)
ob.SetUint64(OffsetLockedOutput_Locktime, in.Locktime)
ob.SetBytes(OffsetLockedOutput_TransferOutBytes, in.TransferOutBytes)
ob.FinishAsRoot()
return writeEnvelopePrefix(TypeKindReserved, ShapeKindLockedOutput, b.Finish())
}