mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
bbde2a6820
* add reset funcs * init -> TestMain for beacon config params * fixes build file * core/epoch spectests updated * core/blocks spectests updated * fix the rest of spectests * SetupTestConfigCleanup * revert copy() api * rever all cases of copy() * fixes spectests * updates fork_test * config_test update * fixes spectest/config_test * Merge refs/heads/master into assert-no-side-effects-occur-in-tests * Merge branch 'master' into assert-no-side-effects-occur-in-tests * Merge refs/heads/master into assert-no-side-effects-occur-in-tests * Merge refs/heads/master into assert-no-side-effects-occur-in-tests * Merge refs/heads/master into assert-no-side-effects-occur-in-tests * Merge refs/heads/master into assert-no-side-effects-occur-in-tests * Merge refs/heads/master into assert-no-side-effects-occur-in-tests * Merge refs/heads/master into assert-no-side-effects-occur-in-tests * Merge refs/heads/master into assert-no-side-effects-occur-in-tests * Merge branch 'master' into assert-no-side-effects-occur-in-tests * Merge refs/heads/master into assert-no-side-effects-occur-in-tests * Merge refs/heads/master into assert-no-side-effects-occur-in-tests
174 lines
4.6 KiB
Go
174 lines
4.6 KiB
Go
package testing
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"errors"
|
|
"path"
|
|
"testing"
|
|
|
|
fssz "github.com/ferranbt/fastssz"
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
|
"github.com/prysmaticlabs/go-ssz"
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
|
|
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
|
|
"github.com/prysmaticlabs/prysm/shared/params/spectest"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
)
|
|
|
|
// SSZRoots --
|
|
type SSZRoots struct {
|
|
Root string `json:"root"`
|
|
SigningRoot string `json:"signing_root"`
|
|
}
|
|
|
|
func runSSZStaticTests(t *testing.T, config string) {
|
|
if err := spectest.SetConfig(t, config); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
testFolders, _ := testutil.TestFolders(t, config, "ssz_static")
|
|
for _, folder := range testFolders {
|
|
innerPath := path.Join("ssz_static", folder.Name(), "ssz_random")
|
|
innerTestFolders, innerTestsFolderPath := testutil.TestFolders(t, config, innerPath)
|
|
|
|
for _, innerFolder := range innerTestFolders {
|
|
t.Run(path.Join(folder.Name(), innerFolder.Name()), func(t *testing.T) {
|
|
serializedBytes, err := testutil.BazelFileBytes(innerTestsFolderPath, innerFolder.Name(), "serialized.ssz")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
object, err := UnmarshalledSSZ(t, serializedBytes, folder.Name())
|
|
if err != nil {
|
|
t.Fatalf("Could not unmarshall serialized SSZ: %v", err)
|
|
}
|
|
|
|
rootsYamlFile, err := testutil.BazelFileBytes(innerTestsFolderPath, innerFolder.Name(), "roots.yaml")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rootsYaml := &SSZRoots{}
|
|
if err := testutil.UnmarshalYaml(rootsYamlFile, rootsYaml); err != nil {
|
|
t.Fatalf("Failed to Unmarshal: %v", err)
|
|
}
|
|
|
|
// Custom hash tree root for beacon state.
|
|
var htr func(interface{}) ([32]byte, error)
|
|
if _, ok := object.(*pb.BeaconState); ok {
|
|
htr = func(s interface{}) ([32]byte, error) {
|
|
return stateutil.HashTreeRootState(s.(*pb.BeaconState))
|
|
}
|
|
} else {
|
|
htr = ssz.HashTreeRoot
|
|
}
|
|
|
|
root, err := htr(object)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rootBytes, err := hex.DecodeString(rootsYaml.Root[2:])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Equal(root[:], rootBytes) {
|
|
t.Fatalf(
|
|
"Did not receive expected hash tree root, received: %#x, expected: %#x",
|
|
root[:],
|
|
rootBytes,
|
|
)
|
|
}
|
|
|
|
if rootsYaml.SigningRoot == "" {
|
|
return
|
|
}
|
|
signingRoot, err := ssz.HashTreeRoot(object)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
signingRootBytes, err := hex.DecodeString(rootsYaml.SigningRoot[2:])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !bytes.Equal(signingRoot[:], signingRootBytes) {
|
|
t.Fatalf(
|
|
"Did not receive expected signing root, received: %#x, expected: %#x",
|
|
signingRoot[:],
|
|
signingRootBytes,
|
|
)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
func UnmarshalledSSZ(t *testing.T, serializedBytes []byte, folderName string) (interface{}, error) {
|
|
var obj interface{}
|
|
switch folderName {
|
|
case "Attestation":
|
|
obj = ðpb.Attestation{}
|
|
case "AttestationData":
|
|
obj = ðpb.AttestationData{}
|
|
case "AttesterSlashing":
|
|
obj = ðpb.AttesterSlashing{}
|
|
case "AggregateAndProof":
|
|
obj = ðpb.AggregateAttestationAndProof{}
|
|
case "BeaconBlock":
|
|
obj = ðpb.BeaconBlock{}
|
|
case "BeaconBlockBody":
|
|
obj = ðpb.BeaconBlockBody{}
|
|
case "BeaconBlockHeader":
|
|
obj = ðpb.BeaconBlockHeader{}
|
|
case "BeaconState":
|
|
obj = &pb.BeaconState{}
|
|
case "Checkpoint":
|
|
obj = ðpb.Checkpoint{}
|
|
case "Deposit":
|
|
obj = ðpb.Deposit{}
|
|
case "DepositData":
|
|
obj = ðpb.Deposit_Data{}
|
|
case "DepositMessage":
|
|
t.Skip("Unused type")
|
|
return nil, nil
|
|
case "Eth1Data":
|
|
obj = ðpb.Eth1Data{}
|
|
case "Eth1Block":
|
|
t.Skip("Unused type")
|
|
return nil, nil
|
|
case "Fork":
|
|
obj = &pb.Fork{}
|
|
case "ForkData":
|
|
obj = &pb.ForkData{}
|
|
case "HistoricalBatch":
|
|
obj = &pb.HistoricalBatch{}
|
|
case "IndexedAttestation":
|
|
obj = ðpb.IndexedAttestation{}
|
|
case "PendingAttestation":
|
|
obj = &pb.PendingAttestation{}
|
|
case "ProposerSlashing":
|
|
obj = ðpb.ProposerSlashing{}
|
|
case "SignedAggregateAndProof":
|
|
obj = &pb.SignedAggregateAndProof{}
|
|
case "SignedBeaconBlock":
|
|
obj = ðpb.SignedBeaconBlock{}
|
|
case "SignedBeaconBlockHeader":
|
|
obj = ðpb.SignedBeaconBlockHeader{}
|
|
case "SignedVoluntaryExit":
|
|
obj = ðpb.SignedVoluntaryExit{}
|
|
case "SigningRoot":
|
|
obj = &pb.SigningRoot{}
|
|
case "Validator":
|
|
obj = ðpb.Validator{}
|
|
case "VoluntaryExit":
|
|
obj = ðpb.VoluntaryExit{}
|
|
default:
|
|
return nil, errors.New("type not found")
|
|
}
|
|
var err error
|
|
if o, ok := obj.(fssz.Unmarshaler); ok {
|
|
err = o.UnmarshalSSZ(serializedBytes)
|
|
} else {
|
|
err = ssz.Unmarshal(serializedBytes, obj)
|
|
}
|
|
return obj, err
|
|
}
|