ssz2

package
v0.0.0-...-1f8a15b Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalSSZ

func MarshalSSZ(buf []byte, schema ...any) (dst []byte, err error)

The function takes the initial byte slice buf and the schema as variadic arguments.

It initializes the dst slice with the contents of buf and sets the currentOffset to 0.

It creates two empty slices: dynamicComponents to store dynamic objects that require offsets, and offsetsStarts to store the start positions of the offsets.

It iterates over each element in the schema using a for loop.

For each element, it performs the following actions based on its type:

If the element is a uint64, it encodes the value using SSZ and appends it to the dst. It then increments the currentOffset by 8. If the element is a pointer to uint64, it dereferences the pointer, encodes the value using SSZ, and appends it to he dst. It then increments the currentOffset by 8. If the element is a byte slice ([]byte), it appends the slice to the dst. It then increments the currentOffset by the length of the slice.

If the element implements the SizedObjectSSZ interface, it checks if the object is static (fixed size) or dynamic (variable size). If it's static, it calls obj.EncodeSSZ to encode the object and updates the dst accordingly. If it's dynamic, it stores the start offset in the offsetsStarts slice, appends a placeholder 4-byte offset to the dst, and adds the object to the dynamicComponents slice.

After processing all elements in the schema, the function iterates over the dynamic components stored in the dynamicComponents slice.

For each dynamic component, it retrieves the corresponding start offset from the offsetsStarts slice and sets the offset value in the dst using binary.LittleEndian.PutUint32. It then calls dynamicComponent.EncodeSSZ on the sub-slice of the dst starting from the offset position to encode the dynamic component. Finally, the function returns the updated dst slice and nil if the encoding process is successful, or an error if an error occurs during encoding. The Encode function is used to encode objects into an SSZ-encoded byte slice based on the provided schema. It supports encoding of various types such as uint64, []byte, and objects that implement the SizedObjectSSZ interface. It handles both static (fixed size) and dynamic (variable size) objects, including the calculation and placement of offsets for dynamic objects.

func UnmarshalSSZ

func UnmarshalSSZ(buf []byte, version int, schema ...interface{}) (err error)

The function takes the input byte slice buf, the SSZ version, and the schema as variadic arguments. It initializes a position pointer position to keep track of the current position in the buf.

It creates two empty slices: offsets to store the offsets for dynamic objects, and dynamicObjs to store the dynamic objects themselves. It iterates over each element in the schema using a for loop. For each element, it performs the following actions based on its type:

If the element is a pointer to uint64, it decodes the corresponding value from the buf using little-endian encoding and assigns it to the pointer. It then increments the position by 8 bytes.

If the element is a byte slice ([]byte), it copies the corresponding data from the buf to the slice. It then increments the position by the length of the slice.

If the element implements the SizedObjectSSZ interface, it checks if the object is static (fixed size) or dynamic (variable size).

  • If it's static, it decodes the object from the buf and updates the position accordingly by calling obj.DecodeSSZ and obj.EncodingSizeSSZ.
  • If it's dynamic, it stores the offset (as a 32-bit little-endian integer) in the offsets slice and stores the object itself in the dynamicObjs slice. It then increments the position by 4 bytes.

After processing all elements in the schema, the function iterates over the dynamic objects stored in the dynamicObjs slice. For each dynamic object, it retrieves the corresponding offset and the end offset. If it's the last dynamic object, the end offset is set to the length of the buf. It calls obj.DecodeSSZ on the sub-slice of the buf from the offset to the end offset and passes the SSZ version. This decodes the dynamic object. Finally, the function returns nil if the decoding process is successful, or an error if an error occurs during decoding. The Decode function is used to decode an SSZ-encoded byte slice into the specified schema. It supports decoding of various types such as uint64, []byte, and objects that implement the SizedObjectSSZ interface. It handles both static (fixed size) and dynamic (variable size) objects based on their respective decoding methods and offsets.

Types

type ObjectSSZ

type ObjectSSZ interface {
	ssz.EncodableSSZ
	ssz.Marshaler
}

type Sized

type Sized interface {
	Static() bool
}

type SizedObjectSSZ

type SizedObjectSSZ interface {
	ObjectSSZ
	Sized
}

Jump to

Keyboard shortcuts

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