ssz

package module
v0.0.0-...-421bd77 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2020 License: MIT Imports: 11 Imported by: 0

README

FastSSZ

Clone:

$ git clone git@github.com:ferranbt/fastssz.git

Download the eth2.0 spec tests

$ make get-spec-tests

Regenerate the test spec encodings:

$ make build-spec-tests

Generate encodings for a specific package:

$ go run sszgen/*.go --path ./ethereumapis/eth/v1alpha1 [--objs BeaconBlock,Eth1Data]

Optionally, you can specify the objs you want to generate. Otherwise, it will generate encodings for all structs in the package. Note that if a struct does not have 'ssz' tags when required (i.e size of arrays), the generator will fail.

By default, it generates a file with the prefix '_encoding.go' for each file that contains a generated struct. Optionally, you can combine all the outputs in a single file with the 'output' flag.

$ go run sszgen/*.go --path ./ethereumapis/eth/v1alpha1 --output ./ethereumapis/eth/v1alpha1/encoding.go

Test the spectests:

$ go test -v ./spectests/... -run TestSpec

Run the fuzzer:

$ FUZZ_TESTS=True go test -v ./spectests/... -run TestFuzz

To install the generator run:

$ go get github.com/ferranbt/fastssz/sszgen

Benchmark (BeaconBlock):

$ go test -v ./spectests/... -run=XXX -bench=.
goos: linux
goarch: amd64
pkg: github.com/ferranbt/fastssz/spectests
BenchmarkMarshalGoSSZ-4       	    1366	    753160 ns/op	  115112 B/op	    8780 allocs/op
BenchmarkMarshalFast-4        	  240765	      5093 ns/op	   18432 B/op	       1 allocs/op
BenchmarkMarshalSuperFast-4   	  377835	      3041 ns/op	       0 B/op	       0 allocs/op
BenchmarkUnMarshalGoSSZ-4     	     847	   1395097 ns/op	  144608 B/op	    8890 allocs/op
BenchmarkUnMarshalFast-4      	   43824	     27190 ns/op	   31024 B/op	     577 allocs/op
PASS
ok  	github.com/ferranbt/fastssz/spectests	6.608s

Package reference

To reference a struct from another package use the '--include' flag to point to that package.

Example:

$ go run sszgen/*.go --path ./example2
$ go run sszgen/*.go --path ./example 
[ERR]: could not find struct with name 'Checkpoint'
$ go run sszgen/*.go --path ./example --include ./example2

There are some caveats required to use this functionality.

  • If multiple input paths import the same package, all of them need to import it with the same alias if any.
  • If the folder of the package is not the same as the name of the package, any input file that imports this package needs to do it with an alias.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrOffset       = fmt.Errorf("incorrect offset")
	ErrSize         = fmt.Errorf("incorrect size")
	ErrBytesLength  = fmt.Errorf("bytes array does not have the correct length")
	ErrVectorLength = fmt.Errorf("vector does not have the correct length")
	ErrListTooBig   = fmt.Errorf("list length is higher than max value")
	ErrEmptyBitlist = fmt.Errorf("bitlist is empty")
)
View Source
var (
	// ErrIncorrectByteSize means that the byte size is incorrect
	ErrIncorrectByteSize = fmt.Errorf("incorrect byte size")

	// ErrIncorrectListSize means that the size of the list is incorrect
	ErrIncorrectListSize = fmt.Errorf("incorrect list size")
)

Functions

func CalculateLimit

func CalculateLimit(maxCapacity, numItems, size uint64) uint64

func DecodeDynamicLength

func DecodeDynamicLength(buf []byte, maxSize int) (int, error)

DecodeDynamicLength decodes the length from the dynamic input

func DivideInt

func DivideInt(a, b int) (int, bool)

DivideInt divides the int fully

func DivideInt2

func DivideInt2(a, b, max int) (int, error)

func ExtendUint16

func ExtendUint16(b []uint16, needLen int) []uint16

ExtendUint16 extends a uint16 buffer to a given size

func ExtendUint64

func ExtendUint64(b []uint64, needLen int) []uint64

ExtendUint64 extends a uint64 buffer to a given size

func HashWithDefaultHasher

func HashWithDefaultHasher(v HashRoot) ([32]byte, error)

HashWithDefaultHasher hashes a HashRoot object with a Hasher from the default HasherPool

func MarshalBool

func MarshalBool(dst []byte, b bool) []byte

MarshalBool marshals a boolean to dst

func MarshalSSZ

func MarshalSSZ(m Marshaler) ([]byte, error)

MarshalSSZ marshals an object

func MarshalUint16

func MarshalUint16(dst []byte, i uint16) []byte

MarshalUint16 marshals a little endian uint16 to dst

func MarshalUint32

func MarshalUint32(dst []byte, i uint32) []byte

MarshalUint32 marshals a little endian uint32 to dst

func MarshalUint64

func MarshalUint64(dst []byte, i uint64) []byte

MarshalUint64 marshals a little endian uint64 to dst

func MarshalUint8

func MarshalUint8(dst []byte, i uint8) []byte

MarshalUint8 marshals a little endian uint8 to dst

func ReadOffset

func ReadOffset(buf []byte) uint64

ReadOffset reads an offset from buf

func UnmarshalBool

func UnmarshalBool(src []byte) bool

UnmarshalBool unmarshals a boolean from the src input

func UnmarshalDynamic

func UnmarshalDynamic(src []byte, length int, f func(indx int, b []byte) error) error

UnmarshalDynamic unmarshals the dynamic items from the input

func UnmarshalSSZTest

func UnmarshalSSZTest(content []byte, result interface{}) error

func UnmarshallUint16

func UnmarshallUint16(src []byte) uint16

UnmarshallUint16 unmarshals a little endian uint16 from the src input

func UnmarshallUint32

func UnmarshallUint32(src []byte) uint32

UnmarshallUint32 unmarshals a little endian uint32 from the src input

func UnmarshallUint64

func UnmarshallUint64(src []byte) uint64

UnmarshallUint64 unmarshals a little endian uint64 from the src input

func UnmarshallUint8

func UnmarshallUint8(src []byte) uint8

UnmarshallUint8 unmarshals a little endian uint8 from the src input

func ValidateBitlist

func ValidateBitlist(buf []byte, bitLimit uint64) error

ValidateBitlist validates that the bitlist is correct

func WriteOffset

func WriteOffset(dst []byte, i int) []byte

WriteOffset writes an offset to dst

Types

type HashRoot

type HashRoot interface {
	HashTreeRoot() ([32]byte, error)
	HashTreeRootWith(hh *Hasher) error
}

type Hasher

type Hasher struct {
	// contains filtered or unexported fields
}

Hasher is a utility tool to hash SSZ structs

func NewHasher

func NewHasher() *Hasher

NewHasher creates a new Hasher object

func NewHasherWithHash

func NewHasherWithHash(hh hash.Hash) *Hasher

NewHasher creates a new Hasher object with a custom hash function

func (*Hasher) Append

func (h *Hasher) Append(i []byte)

func (*Hasher) AppendUint64

func (h *Hasher) AppendUint64(i uint64)

func (*Hasher) FillUpTo32

func (h *Hasher) FillUpTo32()

func (*Hasher) HashRoot

func (h *Hasher) HashRoot() (res [32]byte, err error)

HashRoot creates the hash final hash root

func (*Hasher) Index

func (h *Hasher) Index() int

Index marks the current buffer index

func (*Hasher) Merkleize

func (h *Hasher) Merkleize(indx int)

Merkleize is used to merkleize the last group of the hasher

func (*Hasher) MerkleizeWithMixin

func (h *Hasher) MerkleizeWithMixin(indx int, num, limit uint64)

MerkleizeWithMixin is used to merkleize the last group of the hasher

func (*Hasher) PutBitlist

func (h *Hasher) PutBitlist(bb []byte, maxSize uint64)

PutBitlist appends a ssz bitlist

func (*Hasher) PutBool

func (h *Hasher) PutBool(b bool)

PutBool appends a boolean

func (*Hasher) PutBytes

func (h *Hasher) PutBytes(b []byte)

PutBytes appends bytes

func (*Hasher) PutRootVector

func (h *Hasher) PutRootVector(b [][]byte, maxCapacity ...uint64) error

PutRootVector appends an array of roots

func (*Hasher) PutUint16

func (h *Hasher) PutUint16(i uint16)

PutUint16 appends a uint16 in 32 bytes

func (*Hasher) PutUint32

func (h *Hasher) PutUint32(i uint32)

PutUint32 appends a uint32 in 32 bytes

func (*Hasher) PutUint64

func (h *Hasher) PutUint64(i uint64)

PutUint64 appends a uint64 in 32 bytes

func (*Hasher) PutUint64Array

func (h *Hasher) PutUint64Array(b []uint64, maxCapacity ...uint64)

PutUint64Array appends an array of uint64

func (*Hasher) PutUint8

func (h *Hasher) PutUint8(i uint8)

PutUint16 appends a uint16 in 32 bytes

func (*Hasher) Reset

func (h *Hasher) Reset()

Reset resets the Hasher obj

type HasherPool

type HasherPool struct {
	// contains filtered or unexported fields
}

HasherPool may be used for pooling Hashers for similarly typed SSZs.

var DefaultHasherPool HasherPool

DefaultHasherPool is a default hasher pool

func (*HasherPool) Get

func (hh *HasherPool) Get() *Hasher

Get acquires a Hasher from the pool.

func (*HasherPool) Put

func (hh *HasherPool) Put(h *Hasher)

Put releases the Hasher to the pool.

type Marshaler

type Marshaler interface {
	MarshalSSZTo(dst []byte) ([]byte, error)
	MarshalSSZ() ([]byte, error)
	SizeSSZ() int
}

Marshaler is the interface implemented by types that can marshal themselves into valid SZZ.

type Unmarshaler

type Unmarshaler interface {
	UnmarshalSSZ(buf []byte) error
}

Unmarshaler is the interface implemented by types that can unmarshal a SSZ description of themselves

Directories

Path Synopsis
Code generated by fastssz.
Code generated by fastssz.

Jump to

Keyboard shortcuts

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