mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-05 17:22:18 +00:00
2ad0ad5877
* Copy over refactoring efforts from PR #9402 * Remove unused type * godoc commentary * Better example err msg * Better example err msg
102 lines
2.8 KiB
Go
102 lines
2.8 KiB
Go
package ssz_static
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
fssz "github.com/ferranbt/fastssz"
|
|
v1 "github.com/prysmaticlabs/prysm/beacon-chain/state/v1"
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/prysm/v1alpha1"
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
common "github.com/prysmaticlabs/prysm/testing/spectest/shared/common/ssz_static"
|
|
)
|
|
|
|
// RunSSZStaticTests executes "ssz_static" tests.
|
|
func RunSSZStaticTests(t *testing.T, config string) {
|
|
common.RunSSZStaticTests(t, config, "phase0", unmarshalledSSZ, customHtr)
|
|
}
|
|
|
|
func customHtr(t *testing.T, htrs []common.HTR, object interface{}) []common.HTR {
|
|
switch object.(type) {
|
|
case *ethpb.BeaconState:
|
|
htrs = append(htrs, func(s interface{}) ([32]byte, error) {
|
|
beaconState, err := v1.InitializeFromProto(s.(*ethpb.BeaconState))
|
|
require.NoError(t, err)
|
|
return beaconState.HashTreeRoot(context.TODO())
|
|
})
|
|
}
|
|
return htrs
|
|
}
|
|
|
|
// unmarshalledSSZ unmarshalls serialized input.
|
|
func unmarshalledSSZ(t *testing.T, serializedBytes []byte, objectName string) (interface{}, error) {
|
|
var obj interface{}
|
|
switch objectName {
|
|
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
|
|
}
|