2020-09-14 18:42:08 +00:00
|
|
|
package fuzz
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-04-28 10:17:36 +00:00
|
|
|
"fmt"
|
2020-09-14 18:42:08 +00:00
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
|
|
|
|
stateutil "github.com/prysmaticlabs/prysm/beacon-chain/core/state"
|
2021-07-22 17:13:18 +00:00
|
|
|
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
2021-07-29 21:45:17 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
2020-09-14 18:42:08 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/featureconfig"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
featureconfig.Init(&featureconfig.Flags{
|
|
|
|
EnableSSZCache: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// BeaconStateFuzz --
|
|
|
|
func BeaconStateFuzz(input []byte) {
|
|
|
|
params.UseMainnetConfig()
|
2021-07-29 21:45:17 +00:00
|
|
|
st := ðpb.BeaconState{}
|
2020-09-14 18:42:08 +00:00
|
|
|
if err := st.UnmarshalSSZ(input); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2021-06-30 15:06:19 +00:00
|
|
|
s, err := v1.InitializeFromProtoUnsafe(st)
|
2020-09-14 18:42:08 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-04-28 10:17:36 +00:00
|
|
|
validateStateHTR(s)
|
2020-09-14 18:42:08 +00:00
|
|
|
nextEpoch := helpers.SlotToEpoch(s.Slot()) + 1
|
|
|
|
slot, err := helpers.StartSlot(nextEpoch)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := stateutil.ProcessSlots(context.Background(), s, slot); err != nil {
|
|
|
|
_ = err
|
|
|
|
return
|
|
|
|
}
|
2021-04-28 10:17:36 +00:00
|
|
|
validateStateHTR(s)
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:06:19 +00:00
|
|
|
func validateStateHTR(s *v1.BeaconState) {
|
2021-07-29 21:45:17 +00:00
|
|
|
rawState, ok := s.InnerStateUnsafe().(*ethpb.BeaconState)
|
2021-04-28 10:17:36 +00:00
|
|
|
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))
|
|
|
|
}
|
2020-09-14 18:42:08 +00:00
|
|
|
}
|