mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
012d279663
* Fix duplicated imports * Fix metrics test
162 lines
5.0 KiB
Go
162 lines
5.0 KiB
Go
package ssz_static
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"errors"
|
|
"path"
|
|
"testing"
|
|
|
|
fssz "github.com/ferranbt/fastssz"
|
|
"github.com/golang/snappy"
|
|
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
|
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"
|
|
)
|
|
|
|
// SSZRoots --
|
|
type SSZRoots struct {
|
|
Root string `json:"root"`
|
|
SigningRoot string `json:"signing_root"`
|
|
}
|
|
|
|
// RunSSZStaticTests executes "ssz_static" tests.
|
|
func RunSSZStaticTests(t *testing.T, config string) {
|
|
require.NoError(t, utils.SetConfig(t, config))
|
|
|
|
testFolders, _ := utils.TestFolders(t, config, "phase0", "ssz_static")
|
|
for _, folder := range testFolders {
|
|
innerPath := path.Join("ssz_static", folder.Name(), "ssz_random")
|
|
innerTestFolders, innerTestsFolderPath := utils.TestFolders(t, config, "phase0", 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_snappy")
|
|
require.NoError(t, err)
|
|
serializedSSZ, err := snappy.Decode(nil /* dst */, serializedBytes)
|
|
require.NoError(t, err, "Failed to decompress")
|
|
object, err := UnmarshalledSSZ(t, serializedSSZ, folder.Name())
|
|
require.NoError(t, err, "Could not unmarshall serialized SSZ")
|
|
|
|
rootsYamlFile, err := testutil.BazelFileBytes(innerTestsFolderPath, innerFolder.Name(), "roots.yaml")
|
|
require.NoError(t, err)
|
|
rootsYaml := &SSZRoots{}
|
|
require.NoError(t, utils.UnmarshalYaml(rootsYamlFile, rootsYaml), "Failed to Unmarshal")
|
|
|
|
// Custom hash tree root for beacon state.
|
|
var htr func(interface{}) ([32]byte, error)
|
|
if _, ok := object.(*ethpb.BeaconState); ok {
|
|
htr = func(s interface{}) ([32]byte, error) {
|
|
beaconState, err := v1.InitializeFromProto(s.(*ethpb.BeaconState))
|
|
require.NoError(t, err)
|
|
return beaconState.HashTreeRoot(context.Background())
|
|
}
|
|
} else {
|
|
htr = func(s interface{}) ([32]byte, error) {
|
|
sszObj, ok := s.(fssz.HashRoot)
|
|
if !ok {
|
|
return [32]byte{}, errors.New("could not get hash root, not compatible object")
|
|
}
|
|
return sszObj.HashTreeRoot()
|
|
}
|
|
}
|
|
|
|
root, err := htr(object)
|
|
require.NoError(t, err)
|
|
rootBytes, err := hex.DecodeString(rootsYaml.Root[2:])
|
|
require.NoError(t, err)
|
|
require.DeepEqual(t, rootBytes, root[:], "Did not receive expected hash tree root")
|
|
|
|
if rootsYaml.SigningRoot == "" {
|
|
return
|
|
}
|
|
|
|
var signingRoot [32]byte
|
|
if v, ok := object.(fssz.HashRoot); ok {
|
|
signingRoot, err = v.HashTreeRoot()
|
|
} else {
|
|
t.Fatal("object does not meet fssz.HashRoot")
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
signingRootBytes, err := hex.DecodeString(rootsYaml.SigningRoot[2:])
|
|
require.NoError(t, err)
|
|
require.DeepEqual(t, signingRootBytes, signingRoot[:], "Did not receive expected signing root")
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// UnmarshalledSSZ unmarshalls serialized input.
|
|
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 "DepositMessage":
|
|
obj = ðpb.DepositMessage{}
|
|
case "DepositData":
|
|
obj = ðpb.Deposit_Data{}
|
|
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.SignedAggregateAttestationAndProof{}
|
|
case "SignedBeaconBlock":
|
|
obj = ðpb.SignedBeaconBlock{}
|
|
case "SignedBeaconBlockHeader":
|
|
obj = ðpb.SignedBeaconBlockHeader{}
|
|
case "SignedVoluntaryExit":
|
|
obj = ðpb.SignedVoluntaryExit{}
|
|
case "SigningData":
|
|
obj = ðpb.SigningData{}
|
|
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 = errors.New("could not unmarshal object, not a fastssz compatible object")
|
|
}
|
|
return obj, err
|
|
}
|