2023-03-02 15:42:11 +00:00
|
|
|
package consensustests
|
2023-02-17 23:00:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
2023-03-11 19:27:21 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
2023-02-17 23:00:07 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/transition"
|
|
|
|
)
|
|
|
|
|
2023-03-02 15:42:11 +00:00
|
|
|
func testSanityFunction(context testContext) error {
|
|
|
|
testState, err := decodeStateFromFile(context, "pre.ssz_snappy")
|
2023-02-17 23:00:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-11 19:27:21 +00:00
|
|
|
testState.HashSSZ()
|
2023-02-17 23:00:07 +00:00
|
|
|
var expectedError bool
|
2023-03-02 15:42:11 +00:00
|
|
|
expectedState, err := decodeStateFromFile(context, "post.ssz_snappy")
|
2023-02-17 23:00:07 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
expectedError = true
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-02 15:42:11 +00:00
|
|
|
blocks, err := testBlocks(context)
|
2023-02-17 23:00:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-11 19:27:21 +00:00
|
|
|
startSlot := testState.Slot()
|
|
|
|
var block *cltypes.SignedBeaconBlock
|
|
|
|
for _, block = range blocks {
|
2023-03-04 21:14:45 +00:00
|
|
|
err = transition.TransitionState(testState, block, true)
|
2023-02-17 23:00:07 +00:00
|
|
|
if err != nil {
|
2023-02-24 21:07:32 +00:00
|
|
|
break
|
2023-02-17 23:00:07 +00:00
|
|
|
}
|
2023-02-24 21:07:32 +00:00
|
|
|
}
|
|
|
|
// Deal with transition error
|
|
|
|
if expectedError && err == nil {
|
|
|
|
return fmt.Errorf("expected error")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
if expectedError {
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-11 19:27:21 +00:00
|
|
|
return fmt.Errorf("cannot transition state: %s. slot=%d. start_slot=%d", err, block.Block.Slot, startSlot)
|
2023-02-17 23:00:07 +00:00
|
|
|
}
|
|
|
|
expectedRoot, err := expectedState.HashSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
haveRoot, err := testState.HashSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if haveRoot != expectedRoot {
|
2023-02-24 21:07:32 +00:00
|
|
|
return fmt.Errorf("mismatching state roots")
|
2023-02-17 23:00:07 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-24 21:43:58 +00:00
|
|
|
|
2023-03-02 15:42:11 +00:00
|
|
|
func testSanityFunctionSlot(context testContext) error {
|
|
|
|
testState, err := decodeStateFromFile(context, "pre.ssz_snappy")
|
2023-02-24 21:43:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-02 15:42:11 +00:00
|
|
|
expectedState, err := decodeStateFromFile(context, "post.ssz_snappy")
|
2023-02-24 21:43:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-04 21:14:45 +00:00
|
|
|
if err := transition.ProcessSlots(testState, expectedState.Slot()); err != nil {
|
2023-02-24 21:43:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
expectedRoot, err := expectedState.HashSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
haveRoot, err := testState.HashSSZ()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if haveRoot != expectedRoot {
|
|
|
|
return fmt.Errorf("mismatching state roots")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|