prysm-pulse/spectest/shared/altair/fork/upgrade_to_altair.go
terence tsao 25bc3f2aeb
More altair spec tests (#9436)
* Add fork and ssz static spec tests

* Fix build

* Remove minimal tags
2021-08-20 16:47:37 +00:00

61 lines
2.3 KiB
Go

package fork
import (
"context"
"path"
"testing"
"github.com/golang/snappy"
"github.com/prysmaticlabs/prysm/beacon-chain/core/altair"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
statealtair "github.com/prysmaticlabs/prysm/beacon-chain/state/v2"
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/spectest/utils"
"google.golang.org/protobuf/proto"
)
// RunUpgradeToAltair is a helper function that runs Altair's fork spec tests.
// It unmarshals a pre and post state to check `UpgradeToAltair` comply with spec implementation.
func RunUpgradeToAltair(t *testing.T, config string) {
require.NoError(t, utils.SetConfig(t, config))
testFolders, testsFolderPath := utils.TestFolders(t, config, "altair", "fork/fork/pyspec_tests")
for _, folder := range testFolders {
t.Run(folder.Name(), func(t *testing.T) {
helpers.ClearCache()
folderPath := path.Join(testsFolderPath, folder.Name())
preStateFile, err := testutil.BazelFileBytes(path.Join(folderPath, "pre.ssz_snappy"))
require.NoError(t, err)
preStateSSZ, err := snappy.Decode(nil /* dst */, preStateFile)
require.NoError(t, err, "Failed to decompress")
preStateBase := &ethpb.BeaconState{}
if err := preStateBase.UnmarshalSSZ(preStateSSZ); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
preState, err := v1.InitializeFromProto(preStateBase)
require.NoError(t, err)
postState, err := altair.UpgradeToAltair(context.Background(), preState)
require.NoError(t, err)
postStateFromFunction, err := statealtair.ProtobufBeaconState(postState.InnerStateUnsafe())
require.NoError(t, err)
postStateFile, err := testutil.BazelFileBytes(path.Join(folderPath, "post.ssz_snappy"))
require.NoError(t, err)
postStateSSZ, err := snappy.Decode(nil /* dst */, postStateFile)
require.NoError(t, err, "Failed to decompress")
postStateFromFile := &ethpb.BeaconStateAltair{}
if err := postStateFromFile.UnmarshalSSZ(postStateSSZ); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
if !proto.Equal(postStateFromFile, postStateFromFunction) {
t.Fatal("Post state does not match expected")
}
})
}
}