prysm-pulse/testing/spectest/shared/capella/fork/transition.go
terence 5a66807989
Update to V5 (#13622)
* First take at updating everything to v5

* Patch gRPC gateway to use prysm v5

Fix patch

* Update go ssz

---------

Co-authored-by: Preston Van Loon <pvanloon@offchainlabs.com>
2024-02-15 05:46:47 +00:00

129 lines
5.2 KiB
Go

package fork
import (
"context"
"fmt"
"testing"
"github.com/golang/snappy"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/core/transition"
state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native"
"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/consensus-types/blocks"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/testing/require"
"github.com/prysmaticlabs/prysm/v5/testing/spectest/utils"
"github.com/prysmaticlabs/prysm/v5/testing/util"
)
type Config struct {
PostFork string `json:"post_fork"`
ForkEpoch int `json:"fork_epoch"`
ForkBlock *int `json:"fork_block"`
BlocksCount int `json:"blocks_count"`
}
// RunForkTransitionTest is a helper function that runs capella's transition core tests.
func RunForkTransitionTest(t *testing.T, config string) {
params.SetupTestConfigCleanup(t)
require.NoError(t, utils.SetConfig(t, config))
testFolders, testsFolderPath := utils.TestFolders(t, config, "capella", "transition/core/pyspec_tests")
if len(testFolders) == 0 {
t.Fatalf("No test folders found for %s/%s/%s", config, "capella", "transition/core/pyspec_tests")
}
for _, folder := range testFolders {
t.Run(folder.Name(), func(t *testing.T) {
helpers.ClearCache()
file, err := util.BazelFileBytes(testsFolderPath, folder.Name(), "meta.yaml")
require.NoError(t, err)
config := &Config{}
require.NoError(t, utils.UnmarshalYaml(file, config), "Failed to Unmarshal")
preforkBlocks := make([]*ethpb.SignedBeaconBlockBellatrix, 0)
postforkBlocks := make([]*ethpb.SignedBeaconBlockCapella, 0)
// Fork happens without any pre-fork blocks.
if config.ForkBlock == nil {
for i := 0; i < config.BlocksCount; i++ {
fileName := fmt.Sprint("blocks_", i, ".ssz_snappy")
blockFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), fileName)
require.NoError(t, err)
blockSSZ, err := snappy.Decode(nil /* dst */, blockFile)
require.NoError(t, err, "Failed to decompress")
block := &ethpb.SignedBeaconBlockCapella{}
require.NoError(t, block.UnmarshalSSZ(blockSSZ), "Failed to unmarshal")
postforkBlocks = append(postforkBlocks, block)
}
// Fork happens with pre-fork blocks.
} else {
for i := 0; i <= *config.ForkBlock; i++ {
fileName := fmt.Sprint("blocks_", i, ".ssz_snappy")
blockFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), fileName)
require.NoError(t, err)
blockSSZ, err := snappy.Decode(nil /* dst */, blockFile)
require.NoError(t, err, "Failed to decompress")
block := &ethpb.SignedBeaconBlockBellatrix{}
require.NoError(t, block.UnmarshalSSZ(blockSSZ), "Failed to unmarshal")
preforkBlocks = append(preforkBlocks, block)
}
for i := *config.ForkBlock + 1; i < config.BlocksCount; i++ {
fileName := fmt.Sprint("blocks_", i, ".ssz_snappy")
blockFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), fileName)
require.NoError(t, err)
blockSSZ, err := snappy.Decode(nil /* dst */, blockFile)
require.NoError(t, err, "Failed to decompress")
block := &ethpb.SignedBeaconBlockCapella{}
require.NoError(t, block.UnmarshalSSZ(blockSSZ), "Failed to unmarshal")
postforkBlocks = append(postforkBlocks, block)
}
}
preBeaconStateFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), "pre.ssz_snappy")
require.NoError(t, err)
preBeaconStateSSZ, err := snappy.Decode(nil /* dst */, preBeaconStateFile)
require.NoError(t, err, "Failed to decompress")
beaconStateBase := &ethpb.BeaconStateBellatrix{}
require.NoError(t, beaconStateBase.UnmarshalSSZ(preBeaconStateSSZ), "Failed to unmarshal")
beaconState, err := state_native.InitializeFromProtoBellatrix(beaconStateBase)
require.NoError(t, err)
bc := params.BeaconConfig().Copy()
bc.CapellaForkEpoch = primitives.Epoch(config.ForkEpoch)
params.OverrideBeaconConfig(bc)
ctx := context.Background()
var ok bool
for _, b := range preforkBlocks {
wsb, err := blocks.NewSignedBeaconBlock(b)
require.NoError(t, err)
st, err := transition.ExecuteStateTransition(ctx, beaconState, wsb)
require.NoError(t, err)
beaconState, ok = st.(*state_native.BeaconState)
require.Equal(t, true, ok)
}
for _, b := range postforkBlocks {
wsb, err := blocks.NewSignedBeaconBlock(b)
require.NoError(t, err)
st, err := transition.ExecuteStateTransition(ctx, beaconState, wsb)
require.NoError(t, err)
beaconState, ok = st.(*state_native.BeaconState)
require.Equal(t, true, ok)
}
postBeaconStateFile, err := util.BazelFileBytes(testsFolderPath, folder.Name(), "post.ssz_snappy")
require.NoError(t, err)
postBeaconStateSSZ, err := snappy.Decode(nil /* dst */, postBeaconStateFile)
require.NoError(t, err, "Failed to decompress")
postBeaconState := &ethpb.BeaconStateCapella{}
require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal")
pbState, err := state_native.ProtobufBeaconStateCapella(beaconState.ToProto())
require.NoError(t, err)
require.DeepSSZEqual(t, pbState, postBeaconState)
})
}
}