prysm-pulse/shared/stateutil/blocks.go
Preston Van Loon 2179ac683e Fuzz testing for custom state ssz (#4234)
* Add a random fuzz test to ssz to capture panics and compare the effectiveness of the cache. This comment shows a difference in state root calculation 52% of the time and what is even more concering is that spec tests pass with the flag on.
* added case for one
* bring down failure rate
* prevent caching operations if no cache enabled
* unit test and pretty printer
* identify further sources of problems
* no more panics
* not panicking anymore
* fix lint
* Merge branch 'master' into fuzz-ssz
* Merge branch 'master' into fuzz-ssz
* passing up to 68
* Merge branch 'fuzz-ssz' of github.com:prysmaticlabs/prysm into fuzz-ssz
* need to find the culprit for 100
* 100 passes, now only 16 out of 1000
* state roots being mutated
* one out of 10k
* fuzzing stuff
* fix up lint
* Merge branch 'master' into fuzz-ssz
* cleanup
* fixing more comments
* Merge branch 'master' into fuzz-ssz
2019-12-15 04:32:19 +00:00

82 lines
2.8 KiB
Go

package stateutil
import (
"bytes"
"encoding/binary"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
)
func blockHeaderRoot(header *ethpb.BeaconBlockHeader) ([32]byte, error) {
fieldRoots := make([][]byte, 5)
if header != nil {
headerSlotBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(headerSlotBuf, header.Slot)
headerSlotRoot := bytesutil.ToBytes32(headerSlotBuf)
fieldRoots[0] = headerSlotRoot[:]
fieldRoots[1] = header.ParentRoot
fieldRoots[2] = header.StateRoot
fieldRoots[3] = header.BodyRoot
signatureChunks, err := pack([][]byte{header.Signature})
if err != nil {
return [32]byte{}, err
}
sigRoot, err := bitwiseMerkleize(signatureChunks, uint64(len(signatureChunks)), uint64(len(signatureChunks)))
if err != nil {
return [32]byte{}, err
}
fieldRoots[4] = sigRoot[:]
}
return bitwiseMerkleize(fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}
func eth1Root(eth1Data *ethpb.Eth1Data) ([32]byte, error) {
fieldRoots := make([][]byte, 3)
for i := 0; i < len(fieldRoots); i++ {
fieldRoots[i] = make([]byte, 32)
}
if eth1Data != nil {
if len(eth1Data.DepositRoot) > 0 {
fieldRoots[0] = eth1Data.DepositRoot
}
eth1DataCountBuf := make([]byte, 8)
binary.LittleEndian.PutUint64(eth1DataCountBuf, eth1Data.DepositCount)
eth1CountRoot := bytesutil.ToBytes32(eth1DataCountBuf)
fieldRoots[1] = eth1CountRoot[:]
if len(eth1Data.BlockHash) > 0 {
fieldRoots[2] = eth1Data.BlockHash
}
}
return bitwiseMerkleize(fieldRoots, uint64(len(fieldRoots)), uint64(len(fieldRoots)))
}
func eth1DataVotesRoot(eth1DataVotes []*ethpb.Eth1Data) ([32]byte, error) {
eth1VotesRoots := make([][]byte, 0)
for i := 0; i < len(eth1DataVotes); i++ {
eth1, err := eth1Root(eth1DataVotes[i])
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute eth1data merkleization")
}
eth1VotesRoots = append(eth1VotesRoots, eth1[:])
}
eth1Chunks, err := pack(eth1VotesRoots)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not chunk eth1 votes roots")
}
eth1VotesRootsRoot, err := bitwiseMerkleize(eth1Chunks, uint64(len(eth1Chunks)), params.BeaconConfig().SlotsPerEth1VotingPeriod)
if err != nil {
return [32]byte{}, errors.Wrap(err, "could not compute eth1data votes merkleization")
}
eth1VotesRootBuf := new(bytes.Buffer)
if err := binary.Write(eth1VotesRootBuf, binary.LittleEndian, uint64(len(eth1DataVotes))); err != nil {
return [32]byte{}, errors.Wrap(err, "could not marshal eth1data votes length")
}
// We need to mix in the length of the slice.
eth1VotesRootBufRoot := make([]byte, 32)
copy(eth1VotesRootBufRoot, eth1VotesRootBuf.Bytes())
return mixInLength(eth1VotesRootsRoot, eth1VotesRootBufRoot), nil
}