- Notifications
You must be signed in to change notification settings - Fork52
Fast Ethereum2.0 SSZ encoder/decoder
License
ferranbt/fastssz
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The FastSSZ project in this repository is a combination of two things: a high performant low level library to work with SSZ encodings (root of this project) and the (sszgen) code generator that generates the SSZ encodings for Go structs using the SSZ library. By combining both, this library achieves peak Go native performance and zero memory allocation. The repository uses as test the official Ethereum SSZ tests (spectests) for the Consensus Spec data structures.
If you are only looking for the Consensus data structures and types with the SSZ support, it is recommended to usego-eth-consensus instead, since it is already integrated with other parts of the Consensus stack, like the Beacon http API.
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: linuxgoarch: amd64pkg: github.com/ferranbt/fastssz/spectestscpu: AMD Ryzen 5 2400G with Radeon Vega GraphicsBenchmarkMarshal_FastBenchmarkMarshal_Fast-8 291054 4088 ns/op 8192 B/op 1 allocs/opBenchmarkMarshal_SuperFastBenchmarkMarshal_SuperFast-8 798883 1354 ns/op 0 B/op 0 allocs/opBenchmarkUnMarshal_FastBenchmarkUnMarshal_Fast-8 64065 17614 ns/op 11900 B/op 210 allocs/opBenchmarkHashTreeRoot_FastBenchmarkHashTreeRoot_Fast-8 25863 45932 ns/op 0 B/op 0 allocs/opBenchmarkHashTreeRoot_SuperFastBenchmarkHashTreeRoot_SuperFast-8 54078 21999 ns/op 0 B/op 0 allocs/opBenchmarkProof_TreeBenchmarkProof_Tree-8 3649 312761 ns/op 118145 B/op 1605 allocs/opPASSok github.com/ferranbt/fastssz/spectests5.501s
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.
Fastssz
integrates with Prysmgohashtree library to do high performance and concurrent Sha256 hashing. It achieves a 2x performance improvement with respect to the normal sequential hashing.
In order to use this feature, enable manually the hash function in the Hasher like in the benchmark example.
About
Fast Ethereum2.0 SSZ encoder/decoder