2023-05-02 14:19:22 +00:00
|
|
|
package consensus_tests
|
2023-02-17 23:00:07 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-02 14:19:22 +00:00
|
|
|
"io/fs"
|
2023-02-17 23:00:07 +00:00
|
|
|
"os"
|
2023-05-02 14:19:22 +00:00
|
|
|
"testing"
|
2023-02-17 23:00:07 +00:00
|
|
|
|
2023-12-06 09:48:36 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/transition/machine"
|
|
|
|
"github.com/ledgerwatch/erigon/spectest"
|
|
|
|
|
2023-03-11 19:27:21 +00:00
|
|
|
"github.com/ledgerwatch/erigon/cl/cltypes"
|
2023-05-02 14:19:22 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-02-17 23:00:07 +00:00
|
|
|
)
|
|
|
|
|
2023-05-02 14:19:22 +00:00
|
|
|
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)
|
|
|
|
|
2023-06-11 21:50:02 +00:00
|
|
|
err = c.Machine.ProcessSlots(testState, expectedState.Slot())
|
2023-05-02 14:19:22 +00:00
|
|
|
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"`
|
2023-02-17 23:00:07 +00:00
|
|
|
}
|
2023-05-02 14:19:22 +00:00
|
|
|
|
|
|
|
err = spectest.ReadMeta(root, "meta.yaml", &meta)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
testState, err := spectest.ReadBeaconState(root, c.Version(), spectest.PreSsz)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-02-17 23:00:07 +00:00
|
|
|
var expectedError bool
|
2023-05-02 14:19:22 +00:00
|
|
|
expectedState, err := spectest.ReadBeaconState(root, c.Version(), spectest.PostSsz)
|
2023-02-17 23:00:07 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
expectedError = true
|
2023-05-10 19:37:50 +00:00
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2023-02-17 23:00:07 +00:00
|
|
|
}
|
2023-05-02 14:19:22 +00:00
|
|
|
|
|
|
|
blocks, err := spectest.ReadBlocks(root, c.Version())
|
|
|
|
require.NoError(t, err)
|
2023-03-26 22:25:08 +00:00
|
|
|
|
2023-03-11 19:27:21 +00:00
|
|
|
var block *cltypes.SignedBeaconBlock
|
|
|
|
for _, block = range blocks {
|
2023-06-11 21:50:02 +00:00
|
|
|
err = machine.TransitionState(c.Machine, testState, block)
|
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
|
2023-05-02 14:19:22 +00:00
|
|
|
if expectedError {
|
|
|
|
require.Error(t, err)
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2023-02-17 23:00:07 +00:00
|
|
|
}
|
2023-05-02 14:19:22 +00:00
|
|
|
|
2023-03-26 22:25:08 +00:00
|
|
|
finalRoot, err := expectedState.HashSSZ()
|
2023-05-02 14:19:22 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-17 23:00:07 +00:00
|
|
|
haveRoot, err := testState.HashSSZ()
|
2023-05-02 14:19:22 +00:00
|
|
|
require.NoError(t, err)
|
2023-02-24 21:43:58 +00:00
|
|
|
|
2023-05-02 14:19:22 +00:00
|
|
|
assert.EqualValues(t, finalRoot, haveRoot)
|
2023-02-24 21:43:58 +00:00
|
|
|
|
|
|
|
return nil
|
2023-05-02 14:19:22 +00:00
|
|
|
})
|