erigon-pulse/cmd/ef-tests-cl/consensus_tests/transition.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

71 lines
2.1 KiB
Go

package consensus_tests
import (
"fmt"
"io/fs"
"testing"
"github.com/ledgerwatch/erigon/cl/clparams"
"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"
)
type TransitionCore struct {
}
func (b *TransitionCore) Run(t *testing.T, root fs.FS, c spectest.TestCase) (err error) {
var meta struct {
PostFork string `yaml:"post_fork"`
ForkEpoch uint64 `yaml:"fork_epoch"`
BlockCount uint64 `yaml:"blocks_count"`
ForkBlock *uint64 `yaml:"fork_block,omitempty"`
}
if err := spectest.ReadMeta(root, "meta.yaml", &meta); err != nil {
return err
}
startState, err := spectest.ReadBeaconState(root, c.Version()-1, spectest.PreSsz)
require.NoError(t, err)
stopState, err := spectest.ReadBeaconState(root, c.Version(), spectest.PostSsz)
require.NoError(t, err)
switch c.Version() {
case clparams.AltairVersion:
startState.BeaconConfig().AltairForkEpoch = meta.ForkEpoch
case clparams.BellatrixVersion:
startState.BeaconConfig().BellatrixForkEpoch = meta.ForkEpoch
case clparams.CapellaVersion:
startState.BeaconConfig().CapellaForkEpoch = meta.ForkEpoch
}
startSlot := startState.Slot()
blockIndex := 0
for {
testSlot, err := spectest.ReadBlockSlot(root, blockIndex)
require.NoError(t, err)
var block *cltypes.SignedBeaconBlock
if testSlot/clparams.MainnetBeaconConfig.SlotsPerEpoch >= meta.ForkEpoch {
block, err = spectest.ReadBlock(root, c.Version(), blockIndex)
require.NoError(t, err)
} else {
block, err = spectest.ReadBlock(root, c.Version()-1, blockIndex)
require.NoError(t, err)
}
if block == nil {
break
}
blockIndex++
if err := transition.TransitionState(startState, block, true); err != nil {
return fmt.Errorf("cannot transition state: %s. slot=%d. start_slot=%d", err, block.Block.Slot, startSlot)
}
}
expectedRoot, err := stopState.HashSSZ()
assert.NoError(t, err)
haveRoot, err := startState.HashSSZ()
assert.NoError(t, err)
assert.EqualValues(t, haveRoot, expectedRoot, "state root")
return nil
}