dynssz

package module
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2025 License: Apache-2.0 Imports: 13 Imported by: 16

README

Dynamic SSZ

Go Reference Go Report Card codecov License

Dynamic SSZ is a Go library for SSZ encoding/decoding with support for dynamic field sizes and code generation. It provides runtime flexibility while maintaining high performance through optional static code generation.

Features

  • 🔧 Dynamic Field Sizes - Support for runtime-determined field sizes based on configuration
  • ⚡ Reflection-Based Processing - Works instantly with any SSZ-compatible types - no code generation required for prototyping
  • 🏗️ Code Generation - Optional static code generation for maximum performance (2-3x faster than dynamic processing)
  • 🚀 CLI Tool - Standalone dynssz-gen command for easy code generation from any Go package
  • 🔄 Hybrid Approach - Seamlessly combines with fastssz for optimal efficiency
  • 📦 Minimal Dependencies - Core library has minimal external dependencies
  • ✅ Spec Compliant - Fully compliant with SSZ specification and Ethereum consensus tests

Production Readiness

  • ✅ Reflection-based dynamic marshaling/unmarshaling/HTR: Production ready - battle-tested in various toolings and stable
  • 🚧 Code generator: Feature complete but in beta stage - hasn't been extensively tested in production environments

Quick Start

Installation
go get github.com/pk910/dynamic-ssz
Basic Usage
import "github.com/pk910/dynamic-ssz"

// Define your types with SSZ tags
type MyStruct struct {
    FixedArray [32]byte
    DynamicList []uint64 `ssz-max:"1000"`
    ConfigBased []byte   `ssz-max:"1024" dynssz-max:"MAX_SIZE"`
}

// Create a DynSsz instance with your configuration
specs := map[string]any{
    "MAX_SIZE": uint64(2048),
}
ds := dynssz.NewDynSsz(specs)

// Marshal
data, err := ds.MarshalSSZ(myObject)

// Unmarshal
err = ds.UnmarshalSSZ(&myObject, data)

// Hash Tree Root
root, err := ds.HashTreeRoot(myObject)

For maximum performance, use code generation. You can use either the CLI tool or the programmatic API:

Install the CLI tool:

go install github.com/pk910/dynamic-ssz/dynssz-gen@latest

Generate SSZ methods:

# Generate for types in current package
dynssz-gen -package . -types "MyStruct,OtherType" -output generated.go

# Generate for types in external package
dynssz-gen -package github.com/example/types -types "Block" -output block_ssz.go
Option 2: Programmatic API

For integration with build systems:

//go:generate go run codegen.go

// codegen.go
package main

import (
    "github.com/pk910/dynamic-ssz/codegen"
    "reflect"
)

func main() {
    generator := codegen.NewCodeGenerator(nil)
    generator.BuildFile(
        "generated.go",
        codegen.WithReflectType(reflect.TypeOf(MyStruct{})),
    )
    generator.Generate()
}

Both approaches generate optimized SSZ methods that are faster than reflection-based encoding.

Performance

Dynamic SSZ is benchmarked against other SSZ libraries (including fastssz) in a dedicated benchmark repository: pk910/ssz-benchmark.

The benchmarks compare encoding, decoding, and hash tree root performance across different SSZ libraries using real Ethereum consensus data structures.

Testing

The library includes comprehensive testing infrastructure:

  • Unit Tests: Fast, isolated tests for core functionality
  • Spec Tests: Ethereum consensus specification compliance tests
  • Examples: Working examples that are automatically tested
  • Performance Tests: Benchmarking and regression testing

Documentation

Examples

Check out the examples directory for:

  • Basic encoding/decoding
  • Code generation setup
  • Ethereum types integration
  • Custom specifications
  • Multi-dimensional arrays

Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

License

Dynamic SSZ is licensed under the Apache 2.0 License.

Documentation

Overview

Package dynssz provides dynamic SSZ encoding and decoding with runtime reflection support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractUnionDescriptorInfo added in v1.0.2

func ExtractUnionDescriptorInfo(descriptorType reflect.Type, dynssz *DynSsz) (map[uint8]UnionVariantInfo, error)

ExtractUnionDescriptorInfo extracts variant information from a union descriptor type. This function is used by the type cache to extract variant information including SSZ annotations.

func SetGlobalSpecs added in v1.0.2

func SetGlobalSpecs(specs map[string]any)

Types

type CompatibleUnion added in v1.0.2

type CompatibleUnion[T any] struct {
	Variant uint8
	Data    interface{}
}

CompatibleUnion represents a union type that can hold one of several possible types. It uses Go generics where T is a descriptor struct that defines the union's possible types. The descriptor struct is never instantiated but provides type information through its fields.

The union stores: - unionType: uint8 field index indicating which variant is active - data: interface{} holding the actual value

Usage:

type UnionExecutionPayload = dynssz.CompatibleUnion[struct {
    ExecutionPayload
    ExecutionPayloadWithBlobs
}]

type BlockWithPayload struct {
    Slot          uint64
    ExecutionData UnionExecutionPayload
}

block := BlockWithPayload{
    Slot: 123,
    ExecutionData: UnionExecutionPayload{
        Variant: 0,
        Data: ExecutionPayload{
            ...
        },
    },
}

func NewCompatibleUnion added in v1.0.2

func NewCompatibleUnion[T any](variantIndex uint8, data interface{}) (*CompatibleUnion[T], error)

NewCompatibleUnion creates a new CompatibleUnion with the specified variant type and data. The variantIndex corresponds to the field index in the descriptor struct T.

func (*CompatibleUnion[T]) GetDescriptorType added in v1.0.2

func (u *CompatibleUnion[T]) GetDescriptorType() reflect.Type

GetDescriptorType returns the reflect.Type of the descriptor struct T. This allows external code to access the descriptor type information.

type ContainerDescriptor added in v1.0.2

type ContainerDescriptor struct {
	Fields    []FieldDescriptor    `json:"fields"`     // For structs
	DynFields []DynFieldDescriptor `json:"dyn_fields"` // Dynamic struct fields
}

FieldDescriptor represents a cached descriptor for a struct field

type DynFieldDescriptor added in v1.0.0

type DynFieldDescriptor struct {
	Field  *FieldDescriptor `json:"field"`
	Offset uint32           `json:"offset"`
	Index  int16            `json:"index"` // Index of the field in the struct
}

DynFieldDescriptor represents a dynamic field descriptor for a struct

type DynSsz

type DynSsz struct {

	// NoFastSsz disables the use of fastssz for static types.
	// When true, all encoding/decoding uses reflection-based processing.
	// Generally not recommended unless you need consistent behavior across all types.
	NoFastSsz bool

	// NoFastHash disables the use of optimized hash tree root calculation.
	// When true, uses the standard hasher instead of the fast gohashtree implementation.
	NoFastHash bool

	// Verbose enables detailed logging of encoding/decoding operations.
	// Useful for debugging but impacts performance.
	Verbose bool
	// contains filtered or unexported fields
}

DynSsz is a dynamic SSZ encoder/decoder that uses runtime reflection to handle dynamic field sizes. It provides flexible SSZ encoding/decoding for any Go data structures that can adapt to different specifications through dynamic field sizing. While commonly used with Ethereum data structures and presets (mainnet, minimal, custom), it works with any SSZ-compatible types.

The instance maintains caches for type descriptors and specification values to optimize performance. It's recommended to reuse the same DynSsz instance across operations to benefit from caching.

Key features:

  • Hybrid approach: automatically uses fastssz for static types, reflection for dynamic types
  • Type caching: reduces overhead for repeated operations on the same types
  • Specification support: handles dynamic field sizes based on runtime specifications
  • Thread-safe: can be safely used from multiple goroutines

Example usage:

specs := map[string]any{
    "SLOTS_PER_HISTORICAL_ROOT": uint64(8192),
    "SYNC_COMMITTEE_SIZE":       uint64(512),
}
ds := dynssz.NewDynSsz(specs)

// Marshal
data, err := ds.MarshalSSZ(myStruct)

// Unmarshal
err = ds.UnmarshalSSZ(&myStruct, data)

// Hash tree root
root, err := ds.HashTreeRoot(myStruct)

func GetGlobalDynSsz added in v1.0.2

func GetGlobalDynSsz() *DynSsz

func NewDynSsz

func NewDynSsz(specs map[string]any) *DynSsz

NewDynSsz creates a new instance of the DynSsz encoder/decoder.

The specs map contains dynamic properties and configurations that control SSZ serialization and deserialization. These specifications allow the library to handle different configurations by defining dynamic field sizes at runtime. While commonly used with Ethereum presets (mainnet, minimal, custom), they can define any dynamic sizing parameters for your data structures.

For non-Ethereum use cases, you can define any specifications relevant to your data structures.

The library supports mathematical expressions in dynssz-size tags that reference these specification values, enabling complex dynamic sizing behavior.

Parameters:

  • specs: A map of specification names to their values. Can be nil for default behavior.

Returns:

  • *DynSsz: A new DynSsz instance ready for encoding/decoding operations

Example:

// Ethereum mainnet specifications
specs := map[string]any{
    "SLOTS_PER_HISTORICAL_ROOT": uint64(8192),
    "SYNC_COMMITTEE_SIZE":       uint64(512),
}
ds := dynssz.NewDynSsz(specs)

// Custom application specifications
customSpecs := map[string]any{
    "MAX_ITEMS":           uint64(1000),
    "BUFFER_SIZE":         uint64(4096),
    "CUSTOM_ARRAY_LENGTH": uint64(256),
}
dsCustom := dynssz.NewDynSsz(customSpecs)

func (*DynSsz) GetTree added in v1.1.1

func (d *DynSsz) GetTree(source any) (*treeproof.Node, error)

GetTree builds and returns the complete Merkle tree for the given value.

This method constructs a full Merkle tree representation of the SSZ-encoded structure, which is useful for proof generation, debugging, and understanding the internal tree structure. The returned tree can be used to generate Merkle proofs for any field or value within the structure.

The tree construction follows the same SSZ merkleization rules as HashTreeRoot, but instead of returning just the root hash, it provides access to the complete tree with all intermediate nodes. This enables:

  • Generating Merkle proofs for specific fields using tree.Prove(index)
  • Debugging tree structure with tree.Show(maxDepth)
  • Understanding how different fields map to generalized indices
  • Analyzing the progressive vs binary tree structures

Parameters:

  • source: Any Go value to be converted to a Merkle tree. Must be SSZ-compatible.

Returns:

  • *treeproof.Node: The root node of the complete Merkle tree
  • error: An error if tree construction fails due to unsupported types or encoding errors

The returned tree supports:

  • Navigation: Use Get(index) to fetch nodes by generalized index
  • Proof generation: Use Prove(index) to generate Merkle proofs
  • Debugging: Use Show(maxDepth) to visualize the tree structure
  • Multi-proofs: Use ProveMulti(indices) for efficient batch proofs

Example:

// Build tree for a beacon block
tree, err := ds.GetTree(beaconBlock)
if err != nil {
    log.Fatal("Failed to build tree:", err)
}

// Show tree structure (limited to 3 levels deep)
tree.Show(3)

// Generate proof for a specific field at generalized index 25
proof, err := tree.Prove(25)
if err != nil {
    log.Fatal("Failed to generate proof:", err)
}

// Verify the proof against the tree root
isValid, err := treeproof.VerifyProof(tree.Hash(), proof)

Note: For progressive containers (with ssz-index tags), the tree structure will be progressive rather than binary, which affects the generalized indices of fields.

func (*DynSsz) GetTypeCache added in v1.0.0

func (d *DynSsz) GetTypeCache() *TypeCache

GetTypeCache returns the type cache for the DynSsz instance.

The type cache stores computed type descriptors for types used in encoding/decoding operations. Type descriptors contain optimized information about how to serialize/deserialize specific types, including field offsets, size information, and whether fastssz can be used.

This method is primarily useful for debugging, performance analysis, or advanced use cases where you need to inspect or manage the cached type information.

Returns:

  • *TypeCache: The type cache instance containing all cached type descriptors

Example:

ds := dynssz.NewDynSsz(specs)
cache := ds.GetTypeCache()

// Inspect cached types
types := cache.GetAllTypes()
fmt.Printf("Cache contains %d types\n", len(types))

func (*DynSsz) HashTreeRoot added in v0.0.6

func (d *DynSsz) HashTreeRoot(source any) ([32]byte, error)

HashTreeRoot computes the hash tree root of the given source object according to SSZ specifications.

The hash tree root is a cryptographic commitment to the entire data structure, used extensively in Ethereum's consensus layer for creating Merkle proofs and maintaining state roots. This method implements the SSZ hash tree root algorithm, which recursively hashes all fields and combines them using binary Merkle trees.

For optimal performance, the method uses a hasher pool to reuse hasher instances across calls. When NoFastHash is false (default), it uses the optimized gohashtree implementation. For types without dynamic fields, it automatically delegates to fastssz's HashTreeRoot method when available.

Parameters:

  • source: Any Go value for which to compute the hash tree root

Returns:

  • [32]byte: The computed hash tree root
  • error: An error if the computation fails due to unsupported types or hashing errors

The method handles all SSZ-supported types including:

  • Basic types (bool, uint8, uint16, uint32, uint64)
  • Fixed-size and variable-size arrays
  • Structs with nested fields
  • Slices with proper limit handling
  • Bitlists with maximum size constraints

Example:

block := &phase0.BeaconBlock{
    Slot:          12345,
    ProposerIndex: 42,
    // ... other fields
}

root, err := ds.HashTreeRoot(block)
if err != nil {
    log.Fatal("Failed to compute root:", err)
}
fmt.Printf("Block root: %x\n", root)

func (*DynSsz) HashTreeRootWith added in v1.1.1

func (d *DynSsz) HashTreeRootWith(source any, hh sszutils.HashWalker) error

HashTreeRootWith computes the hash tree root of the given source object according to SSZ specifications.

This method is similar to HashTreeRoot, but allows for custom hasher instances to be used. It dynamically handles hashing for types with both static and dynamic field sizes, automatically using fastssz for optimal performance when applicable.

Parameters:

  • source: Any Go value for which to compute the hash tree root
  • hh: The HashWalker instance to use for hashing

Returns:

  • error: An error if the computation fails due to unsupported types or hashing errors

The method handles all SSZ-supported types including:

  • Basic types (bool, uint8, uint16, uint32, uint64)
  • Fixed-size and variable-size arrays
  • Structs with nested fields
  • Slices with proper limit handling
  • Bitlists with maximum size constraints

Example:

block := &phase0.BeaconBlock{
    Slot:          12345,
    ProposerIndex: 42,
    // ... other fields
}

hh := &hasher.Hasher{}
err := ds.HashTreeRootWith(block, hh)
if err != nil {
    log.Fatal("Failed to compute root:", err)
}
fmt.Printf("Block root: %x\n", hh.HashRoot())

func (*DynSsz) MarshalSSZ

func (d *DynSsz) MarshalSSZ(source any) ([]byte, error)

MarshalSSZ serializes the given source into its SSZ (Simple Serialize) representation.

This method dynamically handles the serialization of Go types to SSZ format, supporting both static and dynamic field sizes. For types without dynamic specifications, it automatically uses fastssz for optimal performance. For types with dynamic field sizes (based on runtime specifications), it uses reflection-based processing.

The method allocates a new byte slice for the result. For high-performance scenarios with frequent allocations, consider using MarshalSSZTo with a pre-allocated buffer.

Parameters:

  • source: Any Go value to be serialized. Must be a type supported by SSZ encoding.

Returns:

  • []byte: The SSZ-encoded data as a new byte slice
  • error: An error if serialization fails due to unsupported types, encoding errors, or size mismatches

Supported types include:

  • Basic types: bool, uint8, uint16, uint32, uint64
  • Arrays and slices of supported types
  • Structs with appropriate SSZ tags
  • Pointers to supported types
  • Types implementing fastssz.Marshaler interface

Example:

header := &phase0.BeaconBlockHeader{
    Slot:          12345,
    ProposerIndex: 42,
    // ... other fields
}

data, err := ds.MarshalSSZ(header)
if err != nil {
    log.Fatal("Failed to marshal:", err)
}
fmt.Printf("Encoded %d bytes\n", len(data))

func (*DynSsz) MarshalSSZTo

func (d *DynSsz) MarshalSSZTo(source any, buf []byte) ([]byte, error)

MarshalSSZTo serializes the given source into its SSZ (Simple Serialize) representation and writes the output to the provided buffer.

This method provides direct control over the output buffer, enabling performance optimizations such as buffer reuse across multiple serialization operations. Like MarshalSSZ, it dynamically handles serialization for types with both static and dynamic field sizes, automatically using fastssz when possible for optimal performance.

The method appends the serialized data to the provided buffer, which allows for efficient concatenation of multiple serialized objects without additional allocations.

Parameters:

  • source: Any Go value to be serialized. Must be a type supported by SSZ encoding.
  • buf: Pre-allocated byte slice where the serialized data will be appended. Can be nil or empty.

Returns:

  • []byte: The updated buffer containing the original data plus the newly serialized data
  • error: An error if serialization fails due to unsupported types, encoding errors, or size mismatches

Example:

buf := make([]byte, 0, 1024) // Pre-allocate with expected capacity
for _, block := range blocks {
    buf, err = ds.MarshalSSZTo(block, buf)
    if err != nil {
        log.Fatal("Failed to marshal block:", err)
    }
}
fmt.Printf("Serialized %d blocks into %d bytes\n", len(blocks), len(buf))

func (*DynSsz) ResolveSpecValue added in v1.0.2

func (d *DynSsz) ResolveSpecValue(name string) (bool, uint64, error)

func (*DynSsz) SizeSSZ

func (d *DynSsz) SizeSSZ(source any) (int, error)

SizeSSZ calculates the size of the given source object when serialized using SSZ encoding.

This method is useful for pre-allocating buffers with the exact size needed for serialization, avoiding unnecessary allocations and resizing. It dynamically evaluates the size based on the actual values in the source object, accurately handling variable-length fields such as slices and dynamic arrays.

For types without dynamic fields, the size is calculated using the optimized fastssz SizeSSZ method when available. For types with dynamic fields, it traverses the entire structure to compute the exact serialized size.

Parameters:

  • source: Any Go value whose SSZ-encoded size needs to be calculated

Returns:

  • int: The exact number of bytes that would be produced by MarshalSSZ for this source
  • error: An error if the size calculation fails due to unsupported types or invalid data

Example:

state := &phase0.BeaconState{
    // ... populated state fields
}

size, err := ds.SizeSSZ(state)
if err != nil {
    log.Fatal("Failed to calculate size:", err)
}

// Pre-allocate buffer with exact size
buf := make([]byte, 0, size)
buf, err = ds.MarshalSSZTo(state, buf)

func (*DynSsz) UnmarshalSSZ

func (d *DynSsz) UnmarshalSSZ(target any, ssz []byte) error

UnmarshalSSZ decodes the given SSZ-encoded data into the target object.

This method is the counterpart to MarshalSSZ, reconstructing Go values from their SSZ representation. It dynamically handles decoding for types with both static and dynamic field sizes, automatically using fastssz for optimal performance when applicable.

The target must be a pointer to a value of the appropriate type. The method will allocate memory for slices and initialize pointer fields as needed during decoding.

Parameters:

  • target: A pointer to the Go value where the decoded data will be stored. Must be a pointer.
  • ssz: The SSZ-encoded data to decode

Returns:

  • error: An error if decoding fails due to:
  • Invalid SSZ format
  • Type mismatches between the data and target
  • Insufficient or excess data
  • Unsupported types

The method ensures that all bytes in the ssz parameter are consumed during decoding. If there are leftover bytes, an error is returned indicating incomplete consumption.

Example:

var header phase0.BeaconBlockHeader
err := ds.UnmarshalSSZ(&header, encodedData)
if err != nil {
    log.Fatal("Failed to unmarshal:", err)
}
fmt.Printf("Decoded header for slot %d\n", header.Slot)

func (*DynSsz) ValidateType added in v1.0.2

func (d *DynSsz) ValidateType(t reflect.Type) error

ValidateType validates whether a given type is compatible with SSZ encoding/decoding.

This method performs a comprehensive analysis of the provided type to determine if it can be successfully serialized and deserialized according to SSZ specifications. It recursively validates all nested types within structs, arrays, and slices, ensuring complete compatibility throughout the type hierarchy.

The validation process checks for:

  • Supported primitive types (bool, uint8, uint16, uint32, uint64)
  • Valid composite types (arrays, slices, structs)
  • Proper SSZ tags on slice fields (ssz-size, ssz-max, dynssz-size, dynssz-max)
  • Correct tag syntax and values
  • No unsupported types (strings, maps, channels, signed integers, floats, etc.)

This method is particularly useful for:

  • Pre-validation before attempting marshalling/unmarshalling operations
  • Development-time type checking to catch errors early
  • Runtime validation of dynamically constructed types
  • Ensuring type compatibility when integrating with external systems

Parameters:

  • t: The reflect.Type to validate for SSZ compatibility

Returns:

  • error: nil if the type is valid for SSZ encoding/decoding, or a descriptive error explaining why the type is incompatible. The error message includes details about the specific field or type that caused the validation failure.

Example usage:

type MyStruct struct {
    ValidField   uint64
    InvalidField string  // This will cause validation to fail
}

err := ds.ValidateType(reflect.TypeOf(MyStruct{}))
if err != nil {
    log.Fatal("Type validation failed:", err)
    // Output: Type validation failed: field 'InvalidField': unsupported type 'string'
}

The method validates at the type level without requiring an instance of the type, making it suitable for early validation scenarios. For performance-critical paths, validation results can be cached as type compatibility doesn't change at runtime.

type FieldDescriptor added in v1.0.0

type FieldDescriptor struct {
	Name     string          `json:"name"`            // Name of the field
	Type     *TypeDescriptor `json:"type"`            // Type descriptor
	SszIndex uint16          `json:"index,omitempty"` // SSZ index for progressive containers
}

FieldDescriptor represents a cached descriptor for a struct field

type GoTypeFlag added in v1.0.2

type GoTypeFlag uint8
const (
	GoTypeFlagIsPointer   GoTypeFlag = 1 << iota // Whether the type is a pointer type
	GoTypeFlagIsByteArray                        // Whether the type is a byte array
	GoTypeFlagIsString                           // Whether the type is a string type
	GoTypeFlagIsTime                             // Whether the type is a time.Time type
)

type SszCompatFlag added in v1.0.2

type SszCompatFlag uint8

SszCompatFlag is a flag indicating whether a type implements a specific SSZ compatibility interface

const (
	SszCompatFlagFastSSZMarshaler   SszCompatFlag = 1 << iota // Whether the type implements fastssz.Marshaler
	SszCompatFlagFastSSZHasher                                // Whether the type implements fastssz.HashRoot
	SszCompatFlagHashTreeRootWith                             // Whether the type implements HashTreeRootWith
	SszCompatFlagDynamicMarshaler                             // Whether the type implements DynamicMarshaler
	SszCompatFlagDynamicUnmarshaler                           // Whether the type implements DynamicUnmarshaler
	SszCompatFlagDynamicSizer                                 // Whether the type implements DynamicSizer
	SszCompatFlagDynamicHashRoot                              // Whether the type implements DynamicHashRoot
)

type SszMaxSizeHint added in v1.0.0

type SszMaxSizeHint struct {
	Size    uint64
	NoValue bool
	Custom  bool
	Expr    string
}

SszMaxSizeHint encapsulates max size information for SSZ encoding and decoding, derived from 'ssz-max'/'ssz-bitmax' and 'dynssz-max'/'dynssz-bitmax' tag annotations. It provides detailed insights into the max size attributes of fields or types, particularly noting whether max sizes are fixed or dynamic, and if special specification values are applied, differing from default assumptions.

Fields:

  • size: A uint64 value indicating the statically annotated max size of the type or field, as specified by 'ssz-max'/'ssz-bitmax' tag annotations. For dynamic fields, where the max size may vary depending on the instance of the data, this field is set to 0, and the dynamic flag is used to indicate its dynamic nature.
  • dynamic: A boolean flag indicating whether the field's max size is dynamic, set to true for fields whose max size can change or is not fixed at compile time. This determination is based on the presence of 'dynssz-max'/'dynssz-bitmax' annotations or the inherent variability of the type.
  • custom: A boolean indicating whether a non-default specification value has been applied to the type or field, typically through 'dynssz-max'/'dynssz-bitmax' annotations, suggesting a deviation from standard max size expectations that might influence the encoding or decoding process.
  • expr: The dynamic expression used to calculate the max size of the field, typically through 'dynssz-max'/'dynssz-bitmax' annotations.

type SszSizeHint added in v1.0.0

type SszSizeHint struct {
	Size    uint32
	Dynamic bool
	Custom  bool
	Bits    bool
	Expr    string
}

SszSizeHint encapsulates size information for SSZ encoding and decoding, derived from 'ssz-size' and 'dynssz-size' tag annotations. It provides detailed insights into the size attributes of fields or types, particularly noting whether sizes are fixed or dynamic, and if special specification values are applied, differing from default assumptions.

Fields:

  • size: A uint64 value indicating the statically annotated size of the type or field, as specified by 'ssz-size' tag annotations. For dynamic fields, where the size may vary depending on the instance of the data, this field is set to 0, and the dynamic flag is used to indicate its dynamic nature.
  • dynamic: A boolean flag indicating whether the field's size is dynamic, set to true for fields whose size can change or is not fixed at compile time. This determination is based on the presence of 'dynssz-size' annotations or the inherent variability of the type.
  • custom: A boolean indicating whether a non-default specification value has been applied to the type or field, typically through 'dynssz-size' annotations, suggesting a deviation from standard size expectations that might influence the encoding or decoding process.
  • bits: A boolean flag indicating whether the size is in bits rather than bytes.
  • expr: The dynamic expression used to calculate the size of the field, typically through 'dynssz-size' annotations.

type SszType added in v1.0.2

type SszType uint8
const (
	SszUnspecifiedType SszType = iota
	SszCustomType
	SszTypeWrapperType

	// basic types
	SszBoolType
	SszUint8Type
	SszUint16Type
	SszUint32Type
	SszUint64Type
	SszUint128Type
	SszUint256Type

	// complex types
	SszContainerType
	SszListType
	SszVectorType
	SszBitlistType
	SszBitvectorType
	SszProgressiveListType
	SszProgressiveBitlistType
	SszProgressiveContainerType
	SszCompatibleUnionType
)

func ParseSszType added in v1.1.0

func ParseSszType(typeStr string) (SszType, error)

type SszTypeFlag added in v1.0.2

type SszTypeFlag uint8

SszTypeFlag is a flag indicating whether a type has a specific SSZ type feature

const (
	SszTypeFlagIsDynamic      SszTypeFlag = 1 << iota // Whether the type is a dynamic type (or has nested dynamic types)
	SszTypeFlagHasLimit                               // Whether the type has a max size tag
	SszTypeFlagHasDynamicSize                         // Whether this type or any of its nested types uses dynamic spec size value that differs from the default
	SszTypeFlagHasDynamicMax                          // Whether this type or any of its nested types uses dynamic spec max value that differs from the default
	SszTypeFlagHasSizeExpr                            // Whether this type or any of its nested types uses a dynamic expression to calculate the size or max size
	SszTypeFlagHasMaxExpr                             // Whether this type or any of its nested types uses a dynamic expression to calculate the max size
	SszTypeFlagHasBitSize                             // Whether the type has a bit size tag
)

type SszTypeHint added in v1.0.2

type SszTypeHint struct {
	Type SszType
}

type TypeCache added in v1.0.0

type TypeCache struct {
	CompatFlags map[string]SszCompatFlag
	// contains filtered or unexported fields
}

TypeCache manages cached type descriptors

func NewTypeCache added in v1.0.0

func NewTypeCache(dynssz *DynSsz) *TypeCache

NewTypeCache creates a new type cache

func (*TypeCache) GetAllTypes added in v1.0.1

func (tc *TypeCache) GetAllTypes() []reflect.Type

GetAllTypes returns a slice of all types currently cached in the TypeCache.

This method is useful for cache inspection, debugging, and understanding which types have been processed and cached during the application's lifetime. The returned slice contains the reflect.Type values in no particular order.

The method acquires a read lock to ensure thread-safe access to the cache.

Returns:

  • []reflect.Type: A slice containing all cached types

Example:

cachedTypes := cache.GetAllTypes()
fmt.Printf("TypeCache contains %d types\n", len(cachedTypes))
for _, t := range cachedTypes {
    fmt.Printf("  - %s\n", t.String())
}

func (*TypeCache) GetTypeDescriptor added in v1.0.0

func (tc *TypeCache) GetTypeDescriptor(t reflect.Type, sizeHints []SszSizeHint, maxSizeHints []SszMaxSizeHint, typeHints []SszTypeHint) (*TypeDescriptor, error)

GetTypeDescriptor returns a cached type descriptor for the given type, computing it if necessary.

This method is the primary interface for obtaining type descriptors, which contain optimized metadata about how to serialize, deserialize, and hash types according to SSZ specifications. Type descriptors are cached for performance, avoiding repeated reflection and analysis of the same types.

The method is thread-safe and ensures sequential processing to prevent duplicate computation of type descriptors when called concurrently for the same type.

Parameters:

  • t: The reflect.Type for which to obtain a descriptor
  • sizeHints: Optional size hints from parent structures' tags. Pass nil for top-level types.
  • maxSizeHints: Optional max size hints from parent structures' tags. Pass nil for top-level types.
  • typeHints: Optional type hints from parent structures' tags. Pass nil for top-level types.

Returns:

  • *TypeDescriptor: The type descriptor containing metadata for SSZ operations
  • error: An error if the type cannot be analyzed or contains unsupported features

Type descriptors are only cached when no size hints are provided (i.e., for root types). When size hints are present, the descriptor is computed dynamically to accommodate the specific constraints.

Example:

typeDesc, err := cache.GetTypeDescriptor(reflect.TypeOf(myStruct), nil, nil)
if err != nil {
    log.Fatal("Failed to get type descriptor:", err)
}
fmt.Printf("Type size: %d bytes (dynamic: %v)\n", typeDesc.Size, typeDesc.Size < 0)

func (*TypeCache) RemoveAllTypes added in v1.0.1

func (tc *TypeCache) RemoveAllTypes()

RemoveAllTypes clears all cached type descriptors from the cache.

This method is useful for:

  • Resetting the cache after configuration changes
  • Memory management in long-running applications
  • Testing scenarios requiring a clean cache state

The method acquires a write lock to ensure thread-safe clearing. After calling this method, all subsequent type descriptor requests will trigger recomputation.

Example:

// Clear cache after updating specifications
ds.UpdateSpecs(newSpecs)
cache.RemoveAllTypes()

// All types will be recomputed with new specs
desc, err := cache.GetTypeDescriptor(reflect.TypeOf(MyStruct{}), nil, nil)

func (*TypeCache) RemoveType added in v1.0.1

func (tc *TypeCache) RemoveType(t reflect.Type)

RemoveType removes a specific type from the cache.

This method is useful for cache management scenarios where you need to force recomputation of a type descriptor, such as after configuration changes or when testing different type configurations.

The method acquires a write lock to ensure thread-safe removal.

Parameters:

  • t: The reflect.Type to remove from the cache

Example:

// Remove a type to force recomputation
cache.RemoveType(reflect.TypeOf(MyStruct{}))

// Next call to GetTypeDescriptor will rebuild the descriptor
desc, err := cache.GetTypeDescriptor(reflect.TypeOf(MyStruct{}), nil, nil)

type TypeDescriptor added in v1.0.0

type TypeDescriptor struct {
	Type                   reflect.Type              `json:"-"`                   // Reflect type
	CodegenInfo            *any                      `json:"-"`                   // Codegen information
	Kind                   reflect.Kind              `json:"kind"`                // Reflect kind of the type
	Size                   uint32                    `json:"size"`                // SSZ size (-1 if dynamic)
	Len                    uint32                    `json:"len"`                 // Length of array/slice
	Limit                  uint64                    `json:"limit"`               // Limit of array/slice (ssz-max tag)
	ContainerDesc          *ContainerDescriptor      `json:"container,omitempty"` // For structs
	UnionVariants          map[uint8]*TypeDescriptor `json:"union,omitempty"`     // Union variant types by index (for CompatibleUnion)
	ElemDesc               *TypeDescriptor           `json:"field,omitempty"`     // For slices/arrays
	HashTreeRootWithMethod *reflect.Method           `json:"-"`                   // Cached HashTreeRootWith method for performance
	SizeExpression         *string                   `json:"size_expr,omitempty"` // The dynamic expression used to calculate the size of the type
	MaxExpression          *string                   `json:"max_expr,omitempty"`  // The dynamic expression used to calculate the max size of the type
	BitSize                uint32                    `json:"bit_size,omitempty"`  // Bit size for bit vector types (ssz-bitsize tag)
	SszType                SszType                   `json:"type"`                // SSZ type of the type
	SszTypeFlags           SszTypeFlag               `json:"flags"`               // SSZ type flags
	SszCompatFlags         SszCompatFlag             `json:"compat"`              // SSZ compatibility flags
	GoTypeFlags            GoTypeFlag                `json:"go_flags"`            // Additional go type flags
}

TypeDescriptor represents a cached, optimized descriptor for a type's SSZ encoding/decoding

func (*TypeDescriptor) GetTypeHash added in v1.1.0

func (td *TypeDescriptor) GetTypeHash() ([32]byte, error)

type TypeWrapper added in v1.0.2

type TypeWrapper[D, T any] struct {
	Data T
}

TypeWrapper represents a wrapper type that can provide SSZ annotations for non-struct types. It uses Go generics where D is a WrapperDescriptor struct that must have exactly 1 field, and T is the actual value type. The descriptor struct is never instantiated but provides type information with annotations.

The wrapper stores: - data: the actual value of type T

Usage:

type ByteSliceDescriptor struct {
    Data []byte `ssz-size:"32"`
}
type WrappedByteSlice = dynssz.TypeWrapper[ByteSliceDescriptor, []byte]

// Use in a struct or standalone
wrapped := WrappedByteSlice{}
wrapped.Set([]byte{1, 2, 3, 4})
data := wrapped.Get() // returns []byte

func NewTypeWrapper added in v1.0.2

func NewTypeWrapper[D, T any](data T) (*TypeWrapper[D, T], error)

NewTypeWrapper creates a new TypeWrapper with the specified data.

func (*TypeWrapper[D, T]) Get added in v1.0.2

func (w *TypeWrapper[D, T]) Get() T

Get returns the wrapped value.

func (*TypeWrapper[D, T]) GetDescriptorType added in v1.0.2

func (w *TypeWrapper[D, T]) GetDescriptorType() reflect.Type

GetDescriptorType returns the reflect.Type of the descriptor struct D. This allows external code to access the descriptor type information.

func (*TypeWrapper[D, T]) Set added in v1.0.2

func (w *TypeWrapper[D, T]) Set(value T)

Set sets the wrapped value.

type UnionVariantInfo added in v1.0.2

type UnionVariantInfo struct {
	Type         reflect.Type
	SizeHints    []SszSizeHint
	MaxSizeHints []SszMaxSizeHint
	TypeHints    []SszTypeHint
}

UnionVariantInfo contains type and annotation information for a union variant

Directories

Path Synopsis
Package codegen provides code generation for dynamic SSZ types.
Package codegen provides code generation for dynamic SSZ types.
Package main implements the dynssz-gen command.
Package main implements the dynssz-gen command.
test module
Package treeproof provides Merkle tree construction and proof generation for SSZ structures.
Package treeproof provides Merkle tree construction and proof generation for SSZ structures.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL