Files
go/cmd/zapgen/testdata/base_tx_zap.hpp.golden
zeekay e3f53c7df1 A struct may reserve more than its fields fill, and a package may nest
Two things the X-chain's wire needs to be sayable.

NFTMintOperation reserves 36 bytes for fields that end at 32. Emitting
32 puts the byte tail four bytes early and moves everything after it, so
`struct Name @N` states the width the wire reserves. Narrower than the
fields is a contradiction and is refused.

`package lux.xvm.wire` is one path, rendered by each backend the way that
language spells a namespace: C++ opens all of it, Go takes the last
segment, which is what a Go package name is. Without it the generated
header can only land at global scope, and a chain whose own wire
namespace is three deep has to either move or alias — and an alias is a
second name for one thing.
2026-09-06 09:39:41 -07:00

152 lines
5.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>
#include "transfer_zap.hpp"
#include "spend_zap.hpp"
namespace xvm {
class BaseTx;
class Transfer;
class Spend;
// Field byte offsets for BaseTx, and the size of its fixed section.
inline constexpr std::int64_t kBaseTxNetworkIDOff = 0;
inline constexpr std::int64_t kBaseTxBlockchainIDOff = 8;
inline constexpr std::int64_t kBaseTxOutsOff = 40;
inline constexpr std::int64_t kBaseTxInsOff = 48;
inline constexpr std::int64_t kBaseTxMemoOff = 56;
inline constexpr std::int64_t kBaseTxSize = 64;
// BaseTx is a zero-copy view into a ZAP-encoded BaseTx message. It borrows the
// bytes it was built over, which must outlive it, and copies no field.
class BaseTx {
public:
BaseTx() = default;
explicit BaseTx(zap::Object o) : o_(o) {}
bool is_null() const { return o_.is_null(); }
zap::Object object() const { return o_; }
std::uint32_t NetworkID() const { return o_.u32(kBaseTxNetworkIDOff); }
std::span<const std::uint8_t> BlockchainID() const {
static constexpr std::array<std::uint8_t, 32> zero{};
const auto b = o_.bytes_fixed(kBaseTxBlockchainIDOff, 32);
return b.size() == 32 ? b : std::span<const std::uint8_t>(zero);
}
zap::List Outs() const { return o_.list_stride(kBaseTxOutsOff, 4); }
::xvm::Transfer OutsAt(std::int64_t i) const;
zap::List Ins() const { return o_.list_stride(kBaseTxInsOff, 4); }
::xvm::Spend InsAt(std::int64_t i) const;
std::span<const std::uint8_t> Memo() const { return o_.bytes(kBaseTxMemoOff); }
private:
zap::Object o_;
};
// WrapBaseTx parses b and returns a typed view over it, or says why b is not a
// message. The view borrows b.
inline std::expected<BaseTx, zap::Error> WrapBaseTx(std::span<const std::uint8_t> b) {
auto m = zap::Message::parse(b);
if (!m) return std::unexpected(m.error());
return BaseTx(m->root());
}
// Accessors that return another class, defined once every class above
// is complete.
inline ::xvm::Transfer BaseTx::OutsAt(std::int64_t i) const { return ::xvm::Transfer(Outs().object_ptr(i)); }
inline ::xvm::Spend BaseTx::InsAt(std::int64_t i) const { return ::xvm::Spend(Ins().object_ptr(i)); }
// The value records, declared before they are defined.
struct BaseTxInput;
// BaseTxInput collects the field values for NewBaseTx. A borrowed field is a view:
// it must outlive the call, not the buffer the call returns.
struct BaseTxInput {
std::uint32_t NetworkID{};
std::array<std::uint8_t, 32> BlockchainID{};
std::vector<TransferInput> Outs{};
std::vector<SpendInput> Ins{};
std::span<const std::uint8_t> Memo{};
};
// 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> NewBaseTx(const BaseTxInput& in);
inline std::int64_t AppendBaseTx(zap::Builder& b, const BaseTxInput& in);
// AppendBaseTx writes one BaseTx object into b — every payload it points at first,
// then the object itself — and returns the object's offset.
inline std::int64_t AppendBaseTx(zap::Builder& b, const BaseTxInput& in) {
std::vector<std::int64_t> outs_ptrs;
outs_ptrs.reserve(in.Outs.size());
for (const auto& elem : in.Outs) outs_ptrs.push_back(AppendTransfer(b, elem));
std::vector<std::int64_t> ins_ptrs;
ins_ptrs.reserve(in.Ins.size());
for (const auto& elem : in.Ins) ins_ptrs.push_back(AppendSpend(b, elem));
std::int64_t outs_off = 0;
if (!in.Outs.empty()) {
auto lb = b.start_list(4);
for (const auto off : outs_ptrs) lb.add_object_ptr(off);
outs_off = lb.finish().first;
}
std::int64_t ins_off = 0;
if (!in.Ins.empty()) {
auto lb = b.start_list(4);
for (const auto off : ins_ptrs) lb.add_object_ptr(off);
ins_off = lb.finish().first;
}
auto ob = b.start_object(kBaseTxSize);
ob.set_u32(kBaseTxNetworkIDOff, in.NetworkID);
ob.set_bytes_fixed(kBaseTxBlockchainIDOff, std::span<const std::uint8_t>(in.BlockchainID));
ob.set_list(kBaseTxOutsOff, outs_off, static_cast<std::int64_t>(in.Outs.size()));
ob.set_list(kBaseTxInsOff, ins_off, static_cast<std::int64_t>(in.Ins.size()));
ob.set_bytes(kBaseTxMemoOff, in.Memo);
return ob.finish();
}
// NewBaseTx writes a ZAP-encoded BaseTx message into a fresh buffer and returns it.
inline std::vector<std::uint8_t> NewBaseTx(const BaseTxInput& in) {
zap::Builder b(256, zap::kVersion2);
std::vector<std::int64_t> outs_ptrs;
outs_ptrs.reserve(in.Outs.size());
for (const auto& elem : in.Outs) outs_ptrs.push_back(AppendTransfer(b, elem));
std::vector<std::int64_t> ins_ptrs;
ins_ptrs.reserve(in.Ins.size());
for (const auto& elem : in.Ins) ins_ptrs.push_back(AppendSpend(b, elem));
std::int64_t outs_off = 0;
if (!in.Outs.empty()) {
auto lb = b.start_list(4);
for (const auto off : outs_ptrs) lb.add_object_ptr(off);
outs_off = lb.finish().first;
}
std::int64_t ins_off = 0;
if (!in.Ins.empty()) {
auto lb = b.start_list(4);
for (const auto off : ins_ptrs) lb.add_object_ptr(off);
ins_off = lb.finish().first;
}
auto ob = b.start_object(kBaseTxSize);
ob.set_u32(kBaseTxNetworkIDOff, in.NetworkID);
ob.set_bytes_fixed(kBaseTxBlockchainIDOff, std::span<const std::uint8_t>(in.BlockchainID));
ob.set_list(kBaseTxOutsOff, outs_off, static_cast<std::int64_t>(in.Outs.size()));
ob.set_list(kBaseTxInsOff, ins_off, static_cast<std::int64_t>(in.Ins.size()));
ob.set_bytes(kBaseTxMemoOff, in.Memo);
ob.finish_as_root();
return b.finish();
}
} // namespace xvm