mirror of
https://github.com/zap-proto/go.git
synced 2026-10-30 15:05:00 +00:00
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.
89 lines
3.0 KiB
Plaintext
89 lines
3.0 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 Transfer;
|
|
|
|
// Field byte offsets for Transfer, and the size of its fixed section.
|
|
inline constexpr std::int64_t kTransferAssetIDOff = 0;
|
|
inline constexpr std::int64_t kTransferOutputOff = 32;
|
|
inline constexpr std::int64_t kTransferSize = 40;
|
|
|
|
// Transfer is a zero-copy view into a ZAP-encoded Transfer message. It borrows the
|
|
// bytes it was built over, which must outlive it, and copies no field.
|
|
class Transfer {
|
|
public:
|
|
Transfer() = default;
|
|
explicit Transfer(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> AssetID() const {
|
|
static constexpr std::array<std::uint8_t, 32> zero{};
|
|
const auto b = o_.bytes_fixed(kTransferAssetIDOff, 32);
|
|
return b.size() == 32 ? b : std::span<const std::uint8_t>(zero);
|
|
}
|
|
std::span<const std::uint8_t> Output() const { return o_.bytes(kTransferOutputOff); }
|
|
|
|
private:
|
|
zap::Object o_;
|
|
};
|
|
|
|
// WrapTransfer parses b and returns a typed view over it, or says why b is not a
|
|
// message. The view borrows b.
|
|
inline std::expected<Transfer, zap::Error> WrapTransfer(std::span<const std::uint8_t> b) {
|
|
auto m = zap::Message::parse(b);
|
|
if (!m) return std::unexpected(m.error());
|
|
return Transfer(m->root());
|
|
}
|
|
|
|
// The value records, declared before they are defined.
|
|
struct TransferInput;
|
|
|
|
// TransferInput collects the field values for NewTransfer. A borrowed field is a view:
|
|
// it must outlive the call, not the buffer the call returns.
|
|
struct TransferInput {
|
|
std::array<std::uint8_t, 32> AssetID{};
|
|
std::span<const std::uint8_t> Output{};
|
|
};
|
|
|
|
// 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> NewTransfer(const TransferInput& in);
|
|
inline std::int64_t AppendTransfer(zap::Builder& b, const TransferInput& in);
|
|
|
|
// AppendTransfer writes one Transfer object into b — every payload it points at first,
|
|
// then the object itself — and returns the object's offset.
|
|
inline std::int64_t AppendTransfer(zap::Builder& b, const TransferInput& in) {
|
|
auto ob = b.start_object(kTransferSize);
|
|
ob.set_bytes_fixed(kTransferAssetIDOff, std::span<const std::uint8_t>(in.AssetID));
|
|
ob.set_bytes(kTransferOutputOff, in.Output);
|
|
return ob.finish();
|
|
}
|
|
|
|
// NewTransfer writes a ZAP-encoded Transfer message into a fresh buffer and returns it.
|
|
inline std::vector<std::uint8_t> NewTransfer(const TransferInput& in) {
|
|
zap::Builder b(256, zap::kVersion2);
|
|
auto ob = b.start_object(kTransferSize);
|
|
ob.set_bytes_fixed(kTransferAssetIDOff, std::span<const std::uint8_t>(in.AssetID));
|
|
ob.set_bytes(kTransferOutputOff, in.Output);
|
|
ob.finish_as_root();
|
|
return b.finish();
|
|
}
|
|
|
|
} // namespace xvm
|