Documentation ¶
Overview ¶
Package ssz implements the Simple Serialize algorithm specified at https://github.com/ethereum/eth2.0-specs/blob/master/specs/simple-serialize.md
Currently directly supported types:
bool uint8 uint16 uint32 uint64 bytes slice struct ptr
Code generated by protoc-gen-gogo. DO NOT EDIT.
Index ¶
- func DeepEqual(x, y interface{}) bool
- func HashTreeRoot(val interface{}) ([32]byte, error)
- func HashTreeRootBitfield(bfield bitfield.Bitfield, maxCapacity uint64) ([32]byte, error)
- func HashTreeRootWithCapacity(val interface{}, maxCapacity uint64) ([32]byte, error)
- func Marshal(val interface{}) ([]byte, error)
- func SigningRoot(val interface{}) ([32]byte, error)deprecated
- func Unmarshal(input []byte, val interface{}) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeepEqual ¶
func DeepEqual(x, y interface{}) bool
DeepEqual reports whether two SSZ-able values x and y are “deeply equal,” defined as follows: Two values of identical type are deeply equal if one of the following cases applies:
Values of distinct types are never deeply equal.
Array values are deeply equal when their corresponding elements are deeply equal.
Struct values are deeply equal if their corresponding fields, both exported and unexported, are deeply equal.
Interface values are deeply equal if they hold deeply equal concrete values.
Pointer values are deeply equal if they are equal using Go's == operator or if they point to deeply equal values.
Slice values are deeply equal when all of the following are true: they are both nil, one is nil and the other is empty or vice-versa, they have the same length, and either they point to the same initial entry of the same array (that is, &x[0] == &y[0]) or their corresponding elements (up to length) are deeply equal.
Other values - numbers, bools, strings, and channels - are deeply equal if they are equal using Go's == operator.
In general DeepEqual is a recursive relaxation of Go's == operator. However, this idea is impossible to implement without some inconsistency. Specifically, it is possible for a value to be unequal to itself, either because it is of func type (uncomparable in general) or because it is a floating-point NaN value (not equal to itself in floating-point comparison), or because it is an array, struct, or interface containing such a value.
On the other hand, pointer values are always equal to themselves, even if they point at or contain such problematic values, because they compare equal using Go's == operator, and that is a sufficient condition to be deeply equal, regardless of content. DeepEqual has been defined so that the same short-cut applies to slices and maps: if x and y are the same slice or the same map, they are deeply equal regardless of content.
As DeepEqual traverses the data values it may find a cycle. The second and subsequent times that DeepEqual compares two pointer values that have been compared before, it treats the values as equal rather than examining the values to which they point. This ensures that DeepEqual terminates.
Credits go to the Go team as this is an extension of the official Go source code's reflect.DeepEqual function to handle special SSZ edge cases.
func HashTreeRoot ¶
HashTreeRoot determines the root hash using SSZ's Merkleization. Given a struct with the following fields, one can tree hash it as follows:
type exampleStruct struct { Field1 uint8 Field2 []byte } ex := exampleStruct{ Field1: 10, Field2: []byte{1, 2, 3, 4}, } root, err := HashTreeRoot(ex) if err != nil { return errors.Wrap(err, "failed to compute root") }
func HashTreeRootBitfield ¶
HashTreeRootBitfield determines the root hash of a bitfield type using SSZ's Merkleization.
func HashTreeRootWithCapacity ¶
HashTreeRootWithCapacity determines the root hash of a dynamic list using SSZ's Merkleization and applies a max capacity value when computing the root. If the input is not a slice, the function returns an error.
accountBalances := []uint64{1, 2, 3, 4} root, err := HashTreeRootWithCapacity(accountBalances, 100) // Max 100 accounts. if err != nil { return errors.Wrap(err, "failed to compute root") }
func Marshal ¶
Marshal a value and output the result into a byte slice. Given a struct with the following fields, one can marshal it as follows:
type exampleStruct struct { Field1 uint8 Field2 []byte } ex := exampleStruct{ Field1: 10, Field2: []byte{1, 2, 3, 4}, } encoded, err := Marshal(ex) if err != nil { return fmt.Errorf("failed to marshal: %v", err) }
One can also specify the specific size of a struct's field by using ssz-specific field tags as follows:
type exampleStruct struct { Field1 uint8 Field2 []byte `ssz:"size=32"` }
This will treat `Field2` as as [32]byte array when marshaling. For unbounded fields or multidimensional slices, ssz size tags can also be used as follows:
type exampleStruct struct { Field1 uint8 Field2 [][]byte `ssz:"size=?,32"` }
This will treat `Field2` as type [][32]byte when marshaling a struct of that type.
func SigningRoot
deprecated
SigningRoot truncates the last property of the struct passed in and returns its tree hash. This is done because the last property usually contains the signature that which this data is the root for.
Deprecated: Prefer signed container objects rather than using signing root.
func Unmarshal ¶
Unmarshal SSZ encoded data and output it into the object pointed by pointer val. Given a struct with the following fields, and some encoded bytes of type []byte, one can then unmarshal the bytes into a pointer of the struct as follows:
type exampleStruct1 struct { Field1 uint8 Field2 []byte } var targetStruct exampleStruct1 if err := Unmarshal(encodedBytes, &targetStruct); err != nil { return fmt.Errorf("failed to unmarshal: %v", err) }
Types ¶
This section is empty.