Files
bft/common/metadata.canoto.go
2026-08-04 07:24:25 +02:00

495 lines
14 KiB
Go

// Code generated by canoto. DO NOT EDIT.
// versions:
// canoto v0.19.0
// source: metadata.go
package common
import (
"io"
"reflect"
"sync/atomic"
"github.com/StephenButtolph/canoto"
)
// Ensure that the generated code is compatible with the library version.
const (
_ uint = canoto.VersionCompatibility - 1
_ uint = 1 - canoto.VersionCompatibility
)
// Ensure that unused imports do not error
var _ = io.ErrUnexpectedEOF
const (
canotoNumber_ProtocolMetadata__Version = 1
canotoNumber_ProtocolMetadata__Epoch = 2
canotoNumber_ProtocolMetadata__Round = 3
canotoNumber_ProtocolMetadata__Seq = 4
canotoNumber_ProtocolMetadata__Prev = 5
canotoTag_ProtocolMetadata__Version = "\x08" // canoto.Tag(canotoNumber_ProtocolMetadata__Version, canoto.Varint)
canotoTag_ProtocolMetadata__Epoch = "\x10" // canoto.Tag(canotoNumber_ProtocolMetadata__Epoch, canoto.Varint)
canotoTag_ProtocolMetadata__Round = "\x18" // canoto.Tag(canotoNumber_ProtocolMetadata__Round, canoto.Varint)
canotoTag_ProtocolMetadata__Seq = "\x20" // canoto.Tag(canotoNumber_ProtocolMetadata__Seq, canoto.Varint)
canotoTag_ProtocolMetadata__Prev = "\x2a" // canoto.Tag(canotoNumber_ProtocolMetadata__Prev, canoto.Len)
)
type canotoData_ProtocolMetadata struct {
size uint64
}
// CanotoSpec returns the specification of this canoto message.
func (*ProtocolMetadata) CanotoSpec(...reflect.Type) *canoto.Spec {
var zero ProtocolMetadata
s := &canoto.Spec{
Name: "ProtocolMetadata",
Fields: []canoto.FieldType{
{
FieldNumber: canotoNumber_ProtocolMetadata__Version,
Name: "Version",
OneOf: "",
TypeUint: canoto.SizeOf(zero.Version),
},
{
FieldNumber: canotoNumber_ProtocolMetadata__Epoch,
Name: "Epoch",
OneOf: "",
TypeUint: canoto.SizeOf(zero.Epoch),
},
{
FieldNumber: canotoNumber_ProtocolMetadata__Round,
Name: "Round",
OneOf: "",
TypeUint: canoto.SizeOf(zero.Round),
},
{
FieldNumber: canotoNumber_ProtocolMetadata__Seq,
Name: "Seq",
OneOf: "",
TypeUint: canoto.SizeOf(zero.Seq),
},
{
FieldNumber: canotoNumber_ProtocolMetadata__Prev,
Name: "Prev",
OneOf: "",
TypeFixedBytes: uint64(len(zero.Prev)),
},
},
}
s.CalculateCanotoCache()
return s
}
// UnmarshalCanoto unmarshals a Canoto-encoded byte slice into the struct.
//
// During parsing, the canoto cache is saved.
func (c *ProtocolMetadata) UnmarshalCanoto(bytes []byte) error {
r := canoto.Reader{
B: bytes,
}
return c.UnmarshalCanotoFrom(r)
}
// UnmarshalCanotoFrom populates the struct from a [canoto.Reader]. Most users
// should just use UnmarshalCanoto.
//
// During parsing, the canoto cache is saved.
//
// This function enables configuration of reader options.
func (c *ProtocolMetadata) UnmarshalCanotoFrom(r canoto.Reader) error {
// Zero the struct before unmarshaling.
*c = ProtocolMetadata{}
atomic.StoreUint64(&c.canotoData.size, uint64(len(r.B)))
var minField uint32
for canoto.HasNext(&r) {
field, wireType, err := canoto.ReadTag(&r)
if err != nil {
return err
}
if field < minField {
return canoto.ErrInvalidFieldOrder
}
switch field {
case canotoNumber_ProtocolMetadata__Version:
if wireType != canoto.Varint {
return canoto.ErrUnexpectedWireType
}
if err := canoto.ReadUint(&r, &c.Version); err != nil {
return err
}
if canoto.IsZero(c.Version) {
return canoto.ErrZeroValue
}
case canotoNumber_ProtocolMetadata__Epoch:
if wireType != canoto.Varint {
return canoto.ErrUnexpectedWireType
}
if err := canoto.ReadUint(&r, &c.Epoch); err != nil {
return err
}
if canoto.IsZero(c.Epoch) {
return canoto.ErrZeroValue
}
case canotoNumber_ProtocolMetadata__Round:
if wireType != canoto.Varint {
return canoto.ErrUnexpectedWireType
}
if err := canoto.ReadUint(&r, &c.Round); err != nil {
return err
}
if canoto.IsZero(c.Round) {
return canoto.ErrZeroValue
}
case canotoNumber_ProtocolMetadata__Seq:
if wireType != canoto.Varint {
return canoto.ErrUnexpectedWireType
}
if err := canoto.ReadUint(&r, &c.Seq); err != nil {
return err
}
if canoto.IsZero(c.Seq) {
return canoto.ErrZeroValue
}
case canotoNumber_ProtocolMetadata__Prev:
if wireType != canoto.Len {
return canoto.ErrUnexpectedWireType
}
const (
expectedLength = len(c.Prev)
expectedLengthUint64 = uint64(expectedLength)
)
var length uint64
if err := canoto.ReadUint(&r, &length); err != nil {
return err
}
if length != expectedLengthUint64 {
return canoto.ErrInvalidLength
}
if expectedLength > len(r.B) {
return io.ErrUnexpectedEOF
}
copy((&c.Prev)[:], r.B)
if canoto.IsZero(c.Prev) {
return canoto.ErrZeroValue
}
r.B = r.B[expectedLength:]
default:
return canoto.ErrUnknownField
}
minField = field + 1
}
return nil
}
// ValidCanoto validates that the struct can be correctly marshaled into the
// Canoto format.
//
// Specifically, ValidCanoto ensures:
// 1. All OneOfs are specified at most once.
// 2. All strings are valid utf-8.
// 3. All custom fields are ValidCanoto.
func (c *ProtocolMetadata) ValidCanoto() bool {
return true
}
// CalculateCanotoCache populates size and OneOf caches based on the current
// values in the struct.
//
// It is not safe to copy this struct concurrently.
func (c *ProtocolMetadata) CalculateCanotoCache() {
var size uint64
if !canoto.IsZero(c.Version) {
size += uint64(len(canotoTag_ProtocolMetadata__Version)) + canoto.SizeUint(c.Version)
}
if !canoto.IsZero(c.Epoch) {
size += uint64(len(canotoTag_ProtocolMetadata__Epoch)) + canoto.SizeUint(c.Epoch)
}
if !canoto.IsZero(c.Round) {
size += uint64(len(canotoTag_ProtocolMetadata__Round)) + canoto.SizeUint(c.Round)
}
if !canoto.IsZero(c.Seq) {
size += uint64(len(canotoTag_ProtocolMetadata__Seq)) + canoto.SizeUint(c.Seq)
}
if !canoto.IsZero(c.Prev) {
size += uint64(len(canotoTag_ProtocolMetadata__Prev)) + canoto.SizeBytes((&c.Prev)[:])
}
atomic.StoreUint64(&c.canotoData.size, size)
}
// CachedCanotoSize returns the previously calculated size of the Canoto
// representation from CalculateCanotoCache.
//
// If CalculateCanotoCache has not yet been called, it will return 0.
//
// If the struct has been modified since the last call to CalculateCanotoCache,
// the returned size may be incorrect.
func (c *ProtocolMetadata) CachedCanotoSize() uint64 {
return atomic.LoadUint64(&c.canotoData.size)
}
// MarshalCanoto returns the Canoto representation of this struct.
//
// It is assumed that this struct is ValidCanoto.
//
// It is not safe to copy this struct concurrently.
func (c *ProtocolMetadata) MarshalCanoto() []byte {
c.CalculateCanotoCache()
w := canoto.Writer{
B: make([]byte, 0, c.CachedCanotoSize()),
}
w = c.MarshalCanotoInto(w)
return w.B
}
// MarshalCanotoInto writes the struct into a [canoto.Writer] and returns the
// resulting [canoto.Writer]. Most users should just use MarshalCanoto.
//
// It is assumed that CalculateCanotoCache has been called since the last
// modification to this struct.
//
// It is assumed that this struct is ValidCanoto.
//
// It is not safe to copy this struct concurrently.
func (c *ProtocolMetadata) MarshalCanotoInto(w canoto.Writer) canoto.Writer {
if !canoto.IsZero(c.Version) {
canoto.Append(&w, canotoTag_ProtocolMetadata__Version)
canoto.AppendUint(&w, c.Version)
}
if !canoto.IsZero(c.Epoch) {
canoto.Append(&w, canotoTag_ProtocolMetadata__Epoch)
canoto.AppendUint(&w, c.Epoch)
}
if !canoto.IsZero(c.Round) {
canoto.Append(&w, canotoTag_ProtocolMetadata__Round)
canoto.AppendUint(&w, c.Round)
}
if !canoto.IsZero(c.Seq) {
canoto.Append(&w, canotoTag_ProtocolMetadata__Seq)
canoto.AppendUint(&w, c.Seq)
}
if !canoto.IsZero(c.Prev) {
canoto.Append(&w, canotoTag_ProtocolMetadata__Prev)
canoto.AppendBytes(&w, (&c.Prev)[:])
}
return w
}
const (
canotoNumber_BlockHeader__ProtocolMetadata = 1
canotoNumber_BlockHeader__Digest = 2
canotoTag_BlockHeader__ProtocolMetadata = "\x0a" // canoto.Tag(canotoNumber_BlockHeader__ProtocolMetadata, canoto.Len)
canotoTag_BlockHeader__Digest = "\x12" // canoto.Tag(canotoNumber_BlockHeader__Digest, canoto.Len)
)
type canotoData_BlockHeader struct {
size uint64
}
// CanotoSpec returns the specification of this canoto message.
func (*BlockHeader) CanotoSpec(types ...reflect.Type) *canoto.Spec {
types = append(types, reflect.TypeFor[BlockHeader]())
var zero BlockHeader
s := &canoto.Spec{
Name: "BlockHeader",
Fields: []canoto.FieldType{
canoto.FieldTypeFromField(
/*type inference:*/ (&zero.ProtocolMetadata),
/*FieldNumber: */ canotoNumber_BlockHeader__ProtocolMetadata,
/*Name: */ "ProtocolMetadata",
/*FixedLength: */ 0,
/*Repeated: */ false,
/*OneOf: */ "",
/*Pointer: */ false,
/*types: */ types,
),
{
FieldNumber: canotoNumber_BlockHeader__Digest,
Name: "Digest",
OneOf: "",
TypeFixedBytes: uint64(len(zero.Digest)),
},
},
}
s.CalculateCanotoCache()
return s
}
// UnmarshalCanoto unmarshals a Canoto-encoded byte slice into the struct.
//
// During parsing, the canoto cache is saved.
func (c *BlockHeader) UnmarshalCanoto(bytes []byte) error {
r := canoto.Reader{
B: bytes,
}
return c.UnmarshalCanotoFrom(r)
}
// UnmarshalCanotoFrom populates the struct from a [canoto.Reader]. Most users
// should just use UnmarshalCanoto.
//
// During parsing, the canoto cache is saved.
//
// This function enables configuration of reader options.
func (c *BlockHeader) UnmarshalCanotoFrom(r canoto.Reader) error {
// Zero the struct before unmarshaling.
*c = BlockHeader{}
atomic.StoreUint64(&c.canotoData.size, uint64(len(r.B)))
var minField uint32
for canoto.HasNext(&r) {
field, wireType, err := canoto.ReadTag(&r)
if err != nil {
return err
}
if field < minField {
return canoto.ErrInvalidFieldOrder
}
switch field {
case canotoNumber_BlockHeader__ProtocolMetadata:
if wireType != canoto.Len {
return canoto.ErrUnexpectedWireType
}
// Read the bytes for the field.
originalUnsafe := r.Unsafe
r.Unsafe = true
var msgBytes []byte
if err := canoto.ReadBytes(&r, &msgBytes); err != nil {
return err
}
if len(msgBytes) == 0 {
return canoto.ErrZeroValue
}
r.Unsafe = originalUnsafe
// Unmarshal the field from the bytes.
remainingBytes := r.B
r.B = msgBytes
if err := (&c.ProtocolMetadata).UnmarshalCanotoFrom(r); err != nil {
return err
}
r.B = remainingBytes
case canotoNumber_BlockHeader__Digest:
if wireType != canoto.Len {
return canoto.ErrUnexpectedWireType
}
const (
expectedLength = len(c.Digest)
expectedLengthUint64 = uint64(expectedLength)
)
var length uint64
if err := canoto.ReadUint(&r, &length); err != nil {
return err
}
if length != expectedLengthUint64 {
return canoto.ErrInvalidLength
}
if expectedLength > len(r.B) {
return io.ErrUnexpectedEOF
}
copy((&c.Digest)[:], r.B)
if canoto.IsZero(c.Digest) {
return canoto.ErrZeroValue
}
r.B = r.B[expectedLength:]
default:
return canoto.ErrUnknownField
}
minField = field + 1
}
return nil
}
// ValidCanoto validates that the struct can be correctly marshaled into the
// Canoto format.
//
// Specifically, ValidCanoto ensures:
// 1. All OneOfs are specified at most once.
// 2. All strings are valid utf-8.
// 3. All custom fields are ValidCanoto.
func (c *BlockHeader) ValidCanoto() bool {
if !(&c.ProtocolMetadata).ValidCanoto() {
return false
}
return true
}
// CalculateCanotoCache populates size and OneOf caches based on the current
// values in the struct.
//
// It is not safe to copy this struct concurrently.
func (c *BlockHeader) CalculateCanotoCache() {
var size uint64
(&c.ProtocolMetadata).CalculateCanotoCache()
if fieldSize := (&c.ProtocolMetadata).CachedCanotoSize(); fieldSize != 0 {
size += uint64(len(canotoTag_BlockHeader__ProtocolMetadata)) + canoto.SizeUint(fieldSize) + fieldSize
}
if !canoto.IsZero(c.Digest) {
size += uint64(len(canotoTag_BlockHeader__Digest)) + canoto.SizeBytes((&c.Digest)[:])
}
atomic.StoreUint64(&c.canotoData.size, size)
}
// CachedCanotoSize returns the previously calculated size of the Canoto
// representation from CalculateCanotoCache.
//
// If CalculateCanotoCache has not yet been called, it will return 0.
//
// If the struct has been modified since the last call to CalculateCanotoCache,
// the returned size may be incorrect.
func (c *BlockHeader) CachedCanotoSize() uint64 {
return atomic.LoadUint64(&c.canotoData.size)
}
// MarshalCanoto returns the Canoto representation of this struct.
//
// It is assumed that this struct is ValidCanoto.
//
// It is not safe to copy this struct concurrently.
func (c *BlockHeader) MarshalCanoto() []byte {
c.CalculateCanotoCache()
w := canoto.Writer{
B: make([]byte, 0, c.CachedCanotoSize()),
}
w = c.MarshalCanotoInto(w)
return w.B
}
// MarshalCanotoInto writes the struct into a [canoto.Writer] and returns the
// resulting [canoto.Writer]. Most users should just use MarshalCanoto.
//
// It is assumed that CalculateCanotoCache has been called since the last
// modification to this struct.
//
// It is assumed that this struct is ValidCanoto.
//
// It is not safe to copy this struct concurrently.
func (c *BlockHeader) MarshalCanotoInto(w canoto.Writer) canoto.Writer {
if fieldSize := (&c.ProtocolMetadata).CachedCanotoSize(); fieldSize != 0 {
canoto.Append(&w, canotoTag_BlockHeader__ProtocolMetadata)
canoto.AppendUint(&w, fieldSize)
w = (&c.ProtocolMetadata).MarshalCanotoInto(w)
}
if !canoto.IsZero(c.Digest) {
canoto.Append(&w, canotoTag_BlockHeader__Digest)
canoto.AppendBytes(&w, (&c.Digest)[:])
}
return w
}