Skip to content

Commit b6c50df

Browse files
authored
Add New Method to Fuzz State (#8823)
1 parent 9b697d2 commit b6c50df

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

fuzz/state_fuzz.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package fuzz
22

33
import (
44
"context"
5+
"fmt"
56

67
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
78
stateutil "github.com/prysmaticlabs/prysm/beacon-chain/core/state"
@@ -34,6 +35,7 @@ func BeaconStateFuzz(input []byte) {
3435
if err != nil {
3536
panic(err)
3637
}
38+
validateStateHTR(s)
3739
nextEpoch := helpers.SlotToEpoch(s.Slot()) + 1
3840
slot, err := helpers.StartSlot(nextEpoch)
3941
if err != nil {
@@ -43,4 +45,27 @@ func BeaconStateFuzz(input []byte) {
4345
_ = err
4446
return
4547
}
48+
validateStateHTR(s)
49+
}
50+
51+
func validateStateHTR(s *stateV0.BeaconState) {
52+
rawState, ok := s.InnerStateUnsafe().(*pb.BeaconState)
53+
if !ok {
54+
panic("non valid type assertion")
55+
}
56+
rt, err := s.HashTreeRoot(context.Background())
57+
nxtRt, err2 := rawState.HashTreeRoot()
58+
59+
if err == nil && err2 != nil {
60+
panic("HTR from state had only and error from cached state HTR method")
61+
}
62+
if err != nil && err2 == nil {
63+
panic("HTR from state had only and error from fast-ssz HTR method")
64+
}
65+
if err != nil && err2 != nil {
66+
return
67+
}
68+
if rt != nxtRt {
69+
panic(fmt.Sprintf("cached HTR gave a root of %#x while fast-ssz gave a root of %#x", rt, nxtRt))
70+
}
4671
}

0 commit comments

Comments
 (0)