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

Shuffle tests revisited #2829

Merged
merged 14 commits into from
Jun 25, 2019
24 changes: 0 additions & 24 deletions beacon-chain/chaintest/backend/simulated_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,30 +173,6 @@ func (sb *SimulatedBackend) RunForkChoiceTest(testCase *ForkChoiceTestCase) erro
return nil
}

// RunShuffleTest uses validator set specified from a YAML file, runs the validator shuffle
// algorithm, then compare the output with the expected output from the YAML file.
func (sb *SimulatedBackend) RunShuffleTest(testCase *ShuffleTestCase) error {
defer db.TeardownDB(sb.beaconDB)
seed := common.HexToHash(testCase.Seed)
testIndices := make([]uint64, testCase.Count, testCase.Count)
for i := uint64(0); i < testCase.Count; i++ {
testIndices[i] = i
}
shuffledList := make([]uint64, testCase.Count)
for i := uint64(0); i < testCase.Count; i++ {
si, err := utils.ShuffledIndex(i, testCase.Count, seed)
if err != nil {
log.Error(err)
return err
}
shuffledList[i] = si
}
if !reflect.DeepEqual(shuffledList, testCase.Shuffled) {
return fmt.Errorf("shuffle result error: expected %v, actual %v", testCase.Shuffled, shuffledList)
}
return nil
}

// RunStateTransitionTest advances a beacon chain state transition an N amount of
// slots from a genesis state, with a block being processed at every iteration
// of the state transition function.
Expand Down
127 changes: 0 additions & 127 deletions beacon-chain/chaintest/tests/config/mainnet.yaml

This file was deleted.

126 changes: 0 additions & 126 deletions beacon-chain/chaintest/tests/config/minimal.yaml

This file was deleted.

20 changes: 20 additions & 0 deletions beacon-chain/utils/spectest/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["shuffle_test_format.go"],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/utils/spectest",
visibility = ["//visibility:public"],
)

go_test(
name = "go_default_test",
srcs = ["shuffle_yaml_test.go"],
embed = [":go_default_library"],
deps = [
"//beacon-chain/utils:go_default_library",
"//shared/params/spectest:go_default_library",
"@com_github_ethereum_go_ethereum//common:go_default_library",
"@com_github_go_yaml_yaml//:go_default_library",
],
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package backend
package spectest

// ShuffleTest --
type ShuffleTest struct {
Expand Down
84 changes: 84 additions & 0 deletions beacon-chain/utils/spectest/shuffle_yaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package spectest

import (
"fmt"
"io/ioutil"
"path"
"reflect"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/go-yaml/yaml"
"github.com/prysmaticlabs/prysm/beacon-chain/utils"
"github.com/prysmaticlabs/prysm/shared/params/spectest"
)

func TestShuffleYaml(t *testing.T) {
yamlDir := "./"
ext := ".yaml"
files, err := ioutil.ReadDir(yamlDir)
if err != nil {
t.Fatalf("could not read YAML tests directory: %v", err)
}

for _, file := range files {
if file.IsDir() {
continue
}
extension := string(file.Name()[len(file.Name())-5:])
if err != nil || extension != ext {
continue
}
filePath := path.Join(yamlDir, file.Name())
// #nosec G304
data, err := ioutil.ReadFile(filePath)
if err != nil {
t.Fatalf("could not read YAML tests directory: %v", err)
}
shuffleTest := &ShuffleTest{}
if err := yaml.Unmarshal(data, shuffleTest); err != nil {
t.Fatalf("could not unmarshal YAML file into test struct: %v", err)
}
if shuffleTest.Config == "minimal" {
shayzluf marked this conversation as resolved.
Show resolved Hide resolved
if err := spectest.SetConfig("minimal"); err != nil {
t.Fatal(err)
}
} else {
if err := spectest.SetConfig("mainnet"); err != nil {
t.Fatal(err)
}
}
t.Logf("Title: %v", shuffleTest.Title)
t.Logf("Summary: %v", shuffleTest.Summary)
t.Logf("Fork: %v", shuffleTest.Forks)
t.Logf("Config: %v", shuffleTest.Config)
for _, testCase := range shuffleTest.TestCases {
if err := runShuffleTest(testCase); err != nil {
t.Fatalf("shuffle test failed: %v", err)
}
}

}
}

// RunShuffleTest uses validator set specified from a YAML file, runs the validator shuffle
// algorithm, then compare the output with the expected output from the YAML file.
func runShuffleTest(testCase *ShuffleTestCase) error {
seed := common.HexToHash(testCase.Seed)
testIndices := make([]uint64, testCase.Count, testCase.Count)
for i := uint64(0); i < testCase.Count; i++ {
testIndices[i] = i
}
shuffledList := make([]uint64, testCase.Count)
for i := uint64(0); i < testCase.Count; i++ {
si, err := utils.ShuffledIndex(i, testCase.Count, seed)
if err != nil {
return err
}
shuffledList[i] = si
}
if !reflect.DeepEqual(shuffledList, testCase.Shuffled) {
return fmt.Errorf("shuffle result error: expected %v, actual %v", testCase.Shuffled, shuffledList)
}
return nil
}