erigon-pulse/cmd/ef-tests-cl/consensus_tests/sanity.go
a 9644e6d220
Implement SpecTests in native go, add fork_choice handler (#7422)
a few TODO: remain to make this not a draft

---------

Co-authored-by: Giulio <giulio.rebuffo@gmail.com>
2023-05-02 16:19:22 +02:00

87 lines
2.3 KiB
Go

package consensus_tests
import (
"io/fs"
"os"
"testing"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cmd/ef-tests-cl/spectest"
"github.com/ledgerwatch/erigon/cmd/erigon-cl/core/transition"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var SanitySlots = spectest.HandlerFunc(func(t *testing.T, root fs.FS, c spectest.TestCase) (err error) {
// TODO: this is unused, why?
var slots int
err = spectest.ReadMeta(root, "slots.yaml", &slots)
require.NoError(t, err)
testState, err := spectest.ReadBeaconState(root, c.Version(), spectest.PreSsz)
require.NoError(t, err)
expectedState, err := spectest.ReadBeaconState(root, c.Version(), spectest.PostSsz)
require.NoError(t, err)
err = transition.ProcessSlots(testState, expectedState.Slot())
require.NoError(t, err)
expectedRoot, err := expectedState.HashSSZ()
require.NoError(t, err)
haveRoot, err := testState.HashSSZ()
require.NoError(t, err)
assert.EqualValues(t, expectedRoot, haveRoot)
return nil
})
var SanityBlocks = spectest.HandlerFunc(func(t *testing.T, root fs.FS, c spectest.TestCase) (err error) {
var meta struct {
Description string `yaml:"description"`
BlsSetting int `yaml:"bls_settings"`
RevealDeadlinesSetting int `yaml:"reveal_deadlines_setting"`
BlocksCount int `yaml:"blocks_count"`
}
err = spectest.ReadMeta(root, "meta.yaml", &meta)
require.NoError(t, err)
testState, err := spectest.ReadBeaconState(root, c.Version(), spectest.PreSsz)
require.NoError(t, err)
var expectedError bool
expectedState, err := spectest.ReadBeaconState(root, c.Version(), spectest.PostSsz)
if os.IsNotExist(err) {
expectedError = true
}
blocks, err := spectest.ReadBlocks(root, c.Version())
require.NoError(t, err)
var block *cltypes.SignedBeaconBlock
for _, block = range blocks {
err = transition.TransitionState(testState, block, true)
if err != nil {
break
}
}
// Deal with transition error
if expectedError {
require.Error(t, err)
return nil
} else {
require.NoError(t, err)
}
finalRoot, err := expectedState.HashSSZ()
require.NoError(t, err)
haveRoot, err := testState.HashSSZ()
require.NoError(t, err)
assert.EqualValues(t, finalRoot, haveRoot)
return nil
})