prysm-pulse/testing/fuzz/state_fuzz.go
Raul Jordan f3d6dbcc1e
Move Shared/Params Into Config/Params (#9642)
* config params into pkg

* gaz

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
2021-09-21 19:59:25 +00:00

66 lines
1.6 KiB
Go

package fuzz
import (
"context"
"fmt"
"github.com/prysmaticlabs/prysm/beacon-chain/core"
stateutil "github.com/prysmaticlabs/prysm/beacon-chain/core/transition"
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
"github.com/prysmaticlabs/prysm/config/features"
"github.com/prysmaticlabs/prysm/config/params"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
)
func init() {
features.Init(&features.Flags{
EnableSSZCache: false,
})
}
// BeaconStateFuzz --
func BeaconStateFuzz(input []byte) {
params.UseMainnetConfig()
st := &ethpb.BeaconState{}
if err := st.UnmarshalSSZ(input); err != nil {
return
}
s, err := v1.InitializeFromProtoUnsafe(st)
if err != nil {
panic(err)
}
validateStateHTR(s)
nextEpoch := core.SlotToEpoch(s.Slot()) + 1
slot, err := core.StartSlot(nextEpoch)
if err != nil {
return
}
if _, err := stateutil.ProcessSlots(context.Background(), s, slot); err != nil {
_ = err
return
}
validateStateHTR(s)
}
func validateStateHTR(s *v1.BeaconState) {
rawState, ok := s.InnerStateUnsafe().(*ethpb.BeaconState)
if !ok {
panic("non valid type assertion")
}
rt, err := s.HashTreeRoot(context.Background())
nxtRt, err2 := rawState.HashTreeRoot()
if err == nil && err2 != nil {
panic("HTR from state had only and error from cached state HTR method")
}
if err != nil && err2 == nil {
panic("HTR from state had only and error from fast-ssz HTR method")
}
if err != nil && err2 != nil {
return
}
if rt != nxtRt {
panic(fmt.Sprintf("cached HTR gave a root of %#x while fast-ssz gave a root of %#x", rt, nxtRt))
}
}