Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing finality spec tests #8939

Merged
merged 7 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions spectest/mainnet/phase0/finality/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")

go_test(
name = "go_default_test",
size = "small",
srcs = ["finality_test.go"],
data = glob(["*.yaml"]) + [
"@eth2_spec_tests_mainnet//:test_data",
],
shard_count = 4,
tags = ["spectest"],
deps = ["//spectest/shared/phase0/finality:go_default_library"],
)
11 changes: 11 additions & 0 deletions spectest/mainnet/phase0/finality/finality_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package finality

import (
"testing"

"github.com/prysmaticlabs/prysm/spectest/shared/phase0/finality"
)

func TestMainnet_Phase0_Finality(t *testing.T) {
finality.RunFinalityTest(t, "mainnet")
}
18 changes: 18 additions & 0 deletions spectest/minimal/phase0/finality/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
load("@io_bazel_rules_go//go:def.bzl", "go_test")

# Requires --define ssz=minimal
go_test(
name = "go_default_test",
size = "small",
srcs = ["finality_test.go"],
data = glob(["*.yaml"]) + [
"@eth2_spec_tests_minimal//:test_data",
],
shard_count = 4,
tags = [
"manual",
"minimal",
"spectest",
],
deps = ["//spectest/shared/phase0/finality:go_default_library"],
)
11 changes: 11 additions & 0 deletions spectest/minimal/phase0/finality/finality_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package finality

import (
"testing"

"github.com/prysmaticlabs/prysm/spectest/shared/phase0/finality"
)

func TestMinimal_Phase0_Finality(t *testing.T) {
finality.RunFinalityTest(t, "minimal")
}
23 changes: 23 additions & 0 deletions spectest/shared/phase0/finality/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@prysm//tools/go:def.bzl", "go_library")

go_library(
name = "go_default_library",
testonly = True,
srcs = ["runner.go"],
importpath = "github.com/prysmaticlabs/prysm/spectest/shared/phase0/finality",
visibility = ["//visibility:public"],
deps = [
"//beacon-chain/core/helpers:go_default_library",
"//beacon-chain/core/state:go_default_library",
"//beacon-chain/state/interface:go_default_library",
"//beacon-chain/state/stateV0:go_default_library",
"//proto/beacon/p2p/v1:go_default_library",
"//shared/testutil:go_default_library",
"//shared/testutil/require:go_default_library",
"//spectest/utils:go_default_library",
"@com_github_golang_snappy//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1alpha1:go_default_library",
"@in_gopkg_d4l3k_messagediff_v1//:go_default_library",
"@org_golang_google_protobuf//proto:go_default_library",
],
)
84 changes: 84 additions & 0 deletions spectest/shared/phase0/finality/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package finality

import (
"context"
"fmt"
"testing"

"github.com/golang/snappy"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
"github.com/prysmaticlabs/prysm/beacon-chain/core/state"
iface "github.com/prysmaticlabs/prysm/beacon-chain/state/interface"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateV0"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/spectest/utils"
"google.golang.org/protobuf/proto"
"gopkg.in/d4l3k/messagediff.v1"
)

func init() {
state.SkipSlotCache.Disable()
}

type Config struct {
BlocksCount int `json:"blocks_count"`
}

// RunFinalityTest executes finality spec tests.
func RunFinalityTest(t *testing.T, config string) {
require.NoError(t, utils.SetConfig(t, config))

testFolders, testsFolderPath := utils.TestFolders(t, config, "phase0", "finality/finality/pyspec_tests")
for _, folder := range testFolders {
t.Run(folder.Name(), func(t *testing.T) {
helpers.ClearCache()
preBeaconStateFile, err := testutil.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 := &pb.BeaconState{}
require.NoError(t, beaconStateBase.UnmarshalSSZ(preBeaconStateSSZ), "Failed to unmarshal")
beaconState, err := stateV0.InitializeFromProto(beaconStateBase)
require.NoError(t, err)

file, err := testutil.BazelFileBytes(testsFolderPath, folder.Name(), "meta.yaml")
require.NoError(t, err)

metaYaml := &Config{}
require.NoError(t, utils.UnmarshalYaml(file, metaYaml), "Failed to Unmarshal")

var processedState iface.BeaconState
var ok bool
for i := 0; i < metaYaml.BlocksCount; i++ {
filename := fmt.Sprintf("blocks_%d.ssz_snappy", i)
blockFile, err := testutil.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.SignedBeaconBlock{}
require.NoError(t, block.UnmarshalSSZ(blockSSZ), "Failed to unmarshal")
processedState, err = state.ExecuteStateTransition(context.Background(), beaconState, block)
require.NoError(t, err)
beaconState, ok = processedState.(*stateV0.BeaconState)
require.Equal(t, true, ok)
}

postBeaconStateFile, err := testutil.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 := &pb.BeaconState{}
require.NoError(t, postBeaconState.UnmarshalSSZ(postBeaconStateSSZ), "Failed to unmarshal")
pbState, err := stateV0.ProtobufBeaconState(beaconState.InnerStateUnsafe())
require.NoError(t, err)
if !proto.Equal(pbState, postBeaconState) {
diff, _ := messagediff.PrettyDiff(beaconState.InnerStateUnsafe(), postBeaconState)
t.Log(diff)
t.Fatal("Post state does not match expected")
}
})
}
}