Files
prysm/proto/testing/ssz_minimal_compatibility_test.go
Preston Van Loon 7d47be84ed Template based protobuf parameters for ssz configurations (#3062)
* WIP on build time configuration changes

* add ssz_minimal tests

* split up spec tests into mainnet and minimal, skip any minimal test that are failing without --define ssz=minimal

* lint

* add commentary to ssz_proto_library
2019-07-24 22:03:05 -04:00

357 lines
11 KiB
Go

package testing
import (
"bytes"
"io/ioutil"
"path"
"testing"
"github.com/bazelbuild/rules_go/go/tools/bazel"
"github.com/ghodss/yaml"
"github.com/prysmaticlabs/go-ssz"
sszspectest "github.com/prysmaticlabs/go-ssz/spectests"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/shared/testutil"
)
func TestYamlStatic_Minimal(t *testing.T) {
t.Skip("This test suite requires --define ssz=minimal to be provided and there isn't a great way to do that without breaking //...")
topPath := "tests/ssz_static/core/"
yamlFileNames := []string{
"ssz_minimal_random.yaml",
}
for _, f := range yamlFileNames {
fullPath := path.Join(topPath, f)
filepath, err := bazel.Runfile(fullPath)
if err != nil {
t.Fatal(err)
}
file, err := ioutil.ReadFile(filepath)
if err != nil {
t.Fatalf("Could not load file %v", err)
}
s := &sszspectest.SszMinimalTest{}
if err := yaml.Unmarshal(file, s); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
runTestCasesMinimal(t, s)
}
}
func runTestCasesMinimal(t *testing.T, s *sszspectest.SszMinimalTest) {
for _, testCase := range s.TestCases {
if !testutil.IsEmpty(testCase.Attestation.Value) {
p := &ethpb.Attestation{}
if err := testutil.ConvertToPb(testCase.Attestation.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.Attestation.Root) {
t.Errorf("Expected attestation root %#x, received %#x", testCase.Attestation.Root, root[:])
}
root, err = ssz.SigningRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.Attestation.SigningRoot) {
t.Errorf("Expected attestation signing root data %#x, received %#x", testCase.AttestationData.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.AttestationData.Value) {
p := &ethpb.AttestationData{}
if err := testutil.ConvertToPb(testCase.AttestationData.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.AttestationData.Root) {
t.Errorf("Expected attestation data %#x, received %#x", testCase.AttestationData.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.AttestationDataAndCustodyBit.Value) {
p := &pb.AttestationDataAndCustodyBit{}
if err := testutil.ConvertToPb(testCase.AttestationDataAndCustodyBit.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.AttestationDataAndCustodyBit.Root) {
t.Errorf("Expected attestation data and custody bit %#x, received %#x", testCase.AttestationDataAndCustodyBit.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.AttesterSlashing.Value) {
p := &ethpb.AttesterSlashing{}
if err := testutil.ConvertToPb(testCase.AttesterSlashing.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.AttesterSlashing.Root) {
t.Errorf("Expected attester slashing bit %#x, received %#x", testCase.AttesterSlashing.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.BeaconBlock.Value) {
p := &ethpb.BeaconBlock{}
if err := testutil.ConvertToPb(testCase.BeaconBlock.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.BeaconBlock.Root) {
t.Errorf("Expected beacon block root %#x, received %#x", testCase.BeaconBlock.Root, root[:])
}
root, err = ssz.SigningRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.BeaconBlock.SigningRoot) {
t.Errorf("Expected beacon block signing root %#x, received %#x", testCase.BeaconBlock.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.BeaconBlockBody.Value) {
p := &ethpb.BeaconBlockBody{}
if err := testutil.ConvertToPb(testCase.BeaconBlockBody.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.BeaconBlockBody.Root) {
t.Errorf("Expected beacon block body %#x, received %#x", testCase.BeaconBlockBody.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.BeaconBlockHeader.Value) {
p := &ethpb.BeaconBlockHeader{}
if err := testutil.ConvertToPb(testCase.BeaconBlockHeader.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.BeaconBlockHeader.Root) {
t.Errorf("Expected beacon block header root %#x, received %#x", testCase.BeaconBlockHeader.Root, root[:])
}
root, err = ssz.SigningRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.BeaconBlockHeader.SigningRoot) {
t.Errorf("Expected beacon block header signing root %#x, received %#x", testCase.BeaconBlock.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.BeaconState.Value) {
p := &pb.BeaconState{}
if err := testutil.ConvertToPb(testCase.BeaconState.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.BeaconState.Root) {
t.Errorf("Expected beacon state %#x, received %#x", testCase.BeaconState.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.Crosslink.Value) {
c := &ethpb.Crosslink{}
if err := testutil.ConvertToPb(testCase.Crosslink.Value, c); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(c)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.Crosslink.Root) {
t.Errorf("Expected crosslink %#x, received %#x", testCase.Crosslink.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.Deposit.Value) {
p := &ethpb.Deposit{}
if err := testutil.ConvertToPb(testCase.Deposit.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.Deposit.Root) {
t.Errorf("Expected deposit root %#x, received %#x", testCase.Deposit.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.DepositData.Value) {
p := &ethpb.Deposit_Data{}
if err := testutil.ConvertToPb(testCase.DepositData.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.DepositData.Root) {
t.Errorf("Expected deposit data root %#x, received %#x", testCase.DepositData.Root, root[:])
}
root, err = ssz.SigningRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.DepositData.SigningRoot) {
t.Errorf("Expected deposit data signing root %#x, received %#x", testCase.BeaconBlock.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.Eth1Data.Value) {
p := &ethpb.Eth1Data{}
if err := testutil.ConvertToPb(testCase.Eth1Data.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.Eth1Data.Root) {
t.Errorf("Expected pb data %#x, received %#x", testCase.Eth1Data.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.Fork.Value) {
p := &pb.Fork{}
if err := testutil.ConvertToPb(testCase.Fork.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.Fork.Root) {
t.Errorf("Expected fork %#x, received %#x", testCase.Fork.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.HistoricalBatch.Value) {
p := &pb.HistoricalBatch{}
if err := testutil.ConvertToPb(testCase.HistoricalBatch.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.HistoricalBatch.Root) {
t.Errorf("Expected historical batch %#x, received %#x", testCase.HistoricalBatch.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.IndexedAttestation.Value) {
p := &ethpb.IndexedAttestation{}
if err := testutil.ConvertToPb(testCase.IndexedAttestation.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.IndexedAttestation.Root) {
t.Errorf("Expected indexed attestation root %#x, received %#x", testCase.IndexedAttestation.Root, root[:])
}
root, err = ssz.SigningRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.IndexedAttestation.SigningRoot) {
t.Errorf("Expected indexed attestation signing root %#x, received %#x", testCase.BeaconBlock.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.PendingAttestation.Value) {
p := &pb.PendingAttestation{}
if err := testutil.ConvertToPb(testCase.PendingAttestation.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.PendingAttestation.Root) {
t.Errorf("Expected pending attestation %#x, received %#x", testCase.PendingAttestation.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.ProposerSlashing.Value) {
p := &ethpb.ProposerSlashing{}
if err := testutil.ConvertToPb(testCase.ProposerSlashing.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.ProposerSlashing.Root) {
t.Errorf("Expected proposer slashing %#x, received %#x", testCase.ProposerSlashing.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.Transfer.Value) {
p := &ethpb.Transfer{}
if err := testutil.ConvertToPb(testCase.Transfer.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.Transfer.Root) {
t.Errorf("Expected transfer root %#x, received %#x", testCase.Transfer.Root, root[:])
}
root, err = ssz.SigningRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.Transfer.SigningRoot) {
t.Errorf("Expected transfer signing root %#x, received %#x", testCase.BeaconBlock.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.Validator.Value) {
p := &ethpb.Validator{}
if err := testutil.ConvertToPb(testCase.Validator.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.Validator.Root) {
t.Errorf("Expected validator %#x, received %#x", testCase.Validator.Root, root[:])
}
}
if !testutil.IsEmpty(testCase.VoluntaryExit.Value) {
p := &ethpb.VoluntaryExit{}
if err := testutil.ConvertToPb(testCase.VoluntaryExit.Value, p); err != nil {
t.Fatal(err)
}
root, err := ssz.HashTreeRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.VoluntaryExit.Root) {
t.Errorf("Expected voluntary exit root %#x, received %#x", testCase.VoluntaryExit.Root, root[:])
}
root, err = ssz.SigningRoot(p)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(root[:], testCase.VoluntaryExit.SigningRoot) {
t.Errorf("Expected voluntary exit signing root %#x, received %#x", testCase.BeaconBlock.Root, root[:])
}
}
}
}