Files
go/cmd/zapgen/testdata/spend_zap.hpp.golden
zeekay d9bcc8711f The generator can say what the chains actually put on the wire
A list's encoding was one thing for every element type: a run of
{length, payload} pairs. The Lux chains write four different runs — a
run of numbers, a run of fixed-width byte records, a run of struct
payloads back to back, and a run of relative pointers to payloads
written elsewhere — and none of them was that one. So both chains that
have a C++ port wrote their own accessors by hand, and the generator
could not have replaced them.

The shape is now derived from the element type rather than fixed. A
number is a run of numbers at its own width; bytes_fixed[N] is those
runs back to back; a struct with no tail is its payload back to back at
its own width; a struct WITH a tail cannot sit in a run at all, because
its tail has nowhere to go between two neighbours, so it becomes a
pointer. The schema never spells any of this, which is what stops two
authors spelling one list two ways. bytes and text have no width, so a
run of them is refused rather than invented.

Order is the wire, too. Every payload a struct points at is written
before the struct's own object, because SetList needs an offset that
already exists — the order the reference has always written in, and the
one the emitted builder had backwards. An empty list now writes nothing
at all: the alignment pad that a start-then-discard would leave behind
moves every byte after it.

Three entry points per struct, because there are three places a struct
sits: New for a message of its own, Append for an object inside somebody
else's message, Encode for one element of an inline run. A nested struct
field is the nested type's own Input, not a pre-built buffer the caller
hands over unchecked.

Readers take the stride the schema knows, so a lying length word is
refused once instead of at every element, and a list of records answers
with the record's type instead of an offset the caller has to know.

Both backends emit version 2. That is the header every vector in the
chain corpus opens with, and the one the hardened runtime writes; a
generated builder that wrote version 1 wrote a header no chain has.

The Go runtime gains the pointer list it was missing — AddObjectPtr,
ObjectPtr and the strided read — so both backends can express the same
wire rather than one of them refusing.

Proven: one schema, both backends, byte-identical output over all four
list shapes, a bytes tail and a pointer list.
2026-09-06 09:09:54 -07:00

103 lines
3.7 KiB
Plaintext

// Code generated by zapgen; DO NOT EDIT.
// source: basetx.zap
// SPDX-License-Identifier: BSD-3-Clause-Eco
#pragma once
#include <array>
#include <cstdint>
#include <cstring>
#include <expected>
#include <span>
#include <vector>
#include <zap/zap.hpp>
namespace xvm {
class Spend;
// Field byte offsets for Spend, and the size of its fixed section.
inline constexpr std::int64_t kSpendTxIDOff = 0;
inline constexpr std::int64_t kSpendOutputIndexOff = 32;
inline constexpr std::int64_t kSpendAssetIDOff = 36;
inline constexpr std::int64_t kSpendInputOff = 68;
inline constexpr std::int64_t kSpendSize = 76;
// Spend is a zero-copy view into a ZAP-encoded Spend message. It borrows the
// bytes it was built over, which must outlive it, and copies no field.
class Spend {
public:
Spend() = default;
explicit Spend(zap::Object o) : o_(o) {}
bool is_null() const { return o_.is_null(); }
zap::Object object() const { return o_; }
std::span<const std::uint8_t> TxID() const {
static constexpr std::array<std::uint8_t, 32> zero{};
const auto b = o_.bytes_fixed(kSpendTxIDOff, 32);
return b.size() == 32 ? b : std::span<const std::uint8_t>(zero);
}
std::uint32_t OutputIndex() const { return o_.u32(kSpendOutputIndexOff); }
std::span<const std::uint8_t> AssetID() const {
static constexpr std::array<std::uint8_t, 32> zero{};
const auto b = o_.bytes_fixed(kSpendAssetIDOff, 32);
return b.size() == 32 ? b : std::span<const std::uint8_t>(zero);
}
std::span<const std::uint8_t> Input() const { return o_.bytes(kSpendInputOff); }
private:
zap::Object o_;
};
// WrapSpend parses b and returns a typed view over it, or says why b is not a
// message. The view borrows b.
inline std::expected<Spend, zap::Error> WrapSpend(std::span<const std::uint8_t> b) {
auto m = zap::Message::parse(b);
if (!m) return std::unexpected(m.error());
return Spend(m->root());
}
// The value records, declared before they are defined.
struct SpendInput;
// SpendInput collects the field values for NewSpend. A borrowed field is a view:
// it must outlive the call, not the buffer the call returns.
struct SpendInput {
std::array<std::uint8_t, 32> TxID{};
std::uint32_t OutputIndex{};
std::array<std::uint8_t, 32> AssetID{};
std::span<const std::uint8_t> Input{};
};
// The write side, declared before it is defined so a struct may point at
// one that comes later in the schema.
inline std::vector<std::uint8_t> NewSpend(const SpendInput& in);
inline std::int64_t AppendSpend(zap::Builder& b, const SpendInput& in);
// AppendSpend writes one Spend object into b — every payload it points at first,
// then the object itself — and returns the object's offset.
inline std::int64_t AppendSpend(zap::Builder& b, const SpendInput& in) {
auto ob = b.start_object(kSpendSize);
ob.set_bytes_fixed(kSpendTxIDOff, std::span<const std::uint8_t>(in.TxID));
ob.set_u32(kSpendOutputIndexOff, in.OutputIndex);
ob.set_bytes_fixed(kSpendAssetIDOff, std::span<const std::uint8_t>(in.AssetID));
ob.set_bytes(kSpendInputOff, in.Input);
return ob.finish();
}
// NewSpend writes a ZAP-encoded Spend message into a fresh buffer and returns it.
inline std::vector<std::uint8_t> NewSpend(const SpendInput& in) {
zap::Builder b(256, zap::kVersion2);
auto ob = b.start_object(kSpendSize);
ob.set_bytes_fixed(kSpendTxIDOff, std::span<const std::uint8_t>(in.TxID));
ob.set_u32(kSpendOutputIndexOff, in.OutputIndex);
ob.set_bytes_fixed(kSpendAssetIDOff, std::span<const std::uint8_t>(in.AssetID));
ob.set_bytes(kSpendInputOff, in.Input);
ob.finish_as_root();
return b.finish();
}
} // namespace xvm