mirror of
https://github.com/luxfi/utxo.git
synced 2026-10-30 15:13:57 +00:00
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]>
118 lines
3.4 KiB
Go
118 lines
3.4 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"
|
|
|
|
// TransferInput is the cross-fx ZAP schema for the TransferInput
|
|
// primitive (`Amt uint64 + SigIndices []uint32`). The semantics are
|
|
// uniform across classical and PQ fxs — only the matching Credential's
|
|
// signature size varies.
|
|
//
|
|
// Fixed-section layout (size 16 bytes):
|
|
//
|
|
// Amount uint64 @ 0
|
|
// SigIndicesList list @ 8 (4-byte relOffset + 4-byte length, 8 bytes)
|
|
//
|
|
// SigIndicesList payload is stride-4 (each entry is a uint32).
|
|
//
|
|
// Wire prefix discriminator: TypeKind names the fx; ShapeKind is
|
|
// ShapeKindTransferInput (0x02).
|
|
const (
|
|
OffsetTransferInput_Amount = 0 // uint64
|
|
OffsetTransferInput_SigIndicesList = 8 // list (8 bytes)
|
|
SizeTransferInput = 16
|
|
)
|
|
|
|
// SigIndexStride is the per-element width of the SigIndices list (one
|
|
// uint32 per index).
|
|
const SigIndexStride = 4
|
|
|
|
// TransferInput is the zero-copy typed accessor.
|
|
type TransferInput struct {
|
|
tk TypeKind
|
|
msg *zap.Message
|
|
obj zap.Object
|
|
}
|
|
|
|
// TypeKind returns the fx family that owns this input.
|
|
func (t TransferInput) TypeKind() TypeKind { return t.tk }
|
|
|
|
// Amount returns the amount being spent.
|
|
func (t TransferInput) Amount() uint64 {
|
|
return t.obj.Uint64(OffsetTransferInput_Amount)
|
|
}
|
|
|
|
// SigIndices returns the SigIndices view as a fresh []uint32.
|
|
func (t TransferInput) SigIndices() []uint32 {
|
|
l := t.obj.ListStride(OffsetTransferInput_SigIndicesList, SigIndexStride)
|
|
n := l.Len()
|
|
out := make([]uint32, n)
|
|
for i := 0; i < n; i++ {
|
|
out[i] = l.Uint32(i)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// SigIndicesLen returns the number of signature indices.
|
|
func (t TransferInput) SigIndicesLen() int {
|
|
return t.obj.ListStride(OffsetTransferInput_SigIndicesList, SigIndexStride).Len()
|
|
}
|
|
|
|
// IsZero reports whether the accessor wraps a parsed message.
|
|
func (t TransferInput) IsZero() bool { return t.msg == nil }
|
|
|
|
// WrapTransferInput parses a TransferInput wire envelope.
|
|
func WrapTransferInput(b []byte) (TransferInput, error) {
|
|
tk, sk, zapBytes, err := readEnvelopePrefix(b)
|
|
if err != nil {
|
|
return TransferInput{}, err
|
|
}
|
|
if sk != ShapeKindTransferInput {
|
|
return TransferInput{}, ErrWrongShapeKind
|
|
}
|
|
if tk == TypeKindReserved {
|
|
return TransferInput{}, ErrWrongTypeKind
|
|
}
|
|
msg, err := zap.Parse(zapBytes)
|
|
if err != nil {
|
|
return TransferInput{}, err
|
|
}
|
|
return TransferInput{tk: tk, msg: msg, obj: msg.Root()}, nil
|
|
}
|
|
|
|
// TransferInputInput is the constructor input.
|
|
type TransferInputInput struct {
|
|
TypeKind TypeKind
|
|
Amount uint64
|
|
SigIndices []uint32
|
|
}
|
|
|
|
// NewTransferInput builds a TransferInput wire envelope.
|
|
func NewTransferInput(in TransferInputInput) []byte {
|
|
b := zap.GetBuilder()
|
|
defer zap.PutBuilder(b)
|
|
|
|
sigIdxOff, sigIdxCount := writeSigIndices(b, in.SigIndices)
|
|
|
|
ob := b.StartObject(SizeTransferInput)
|
|
ob.SetUint64(OffsetTransferInput_Amount, in.Amount)
|
|
ob.SetList(OffsetTransferInput_SigIndicesList, sigIdxOff, sigIdxCount)
|
|
ob.FinishAsRoot()
|
|
return writeEnvelopePrefix(in.TypeKind, ShapeKindTransferInput, b.Finish())
|
|
}
|
|
|
|
// writeSigIndices writes a stride-4 list of uint32 signature indices.
|
|
func writeSigIndices(b *zap.Builder, sigs []uint32) (offset, entryCount int) {
|
|
if len(sigs) == 0 {
|
|
return 0, 0
|
|
}
|
|
lb := b.StartList(SigIndexStride)
|
|
for _, s := range sigs {
|
|
lb.AddUint32(s)
|
|
}
|
|
off, _ := lb.Finish()
|
|
return off, len(sigs)
|
|
}
|