mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2025-01-04 00:44:27 +00:00
2ad0ad5877
* Copy over refactoring efforts from PR #9402 * Remove unused type * godoc commentary * Better example err msg * Better example err msg
122 lines
3.6 KiB
Go
122 lines
3.6 KiB
Go
package ssz_static
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
|
|
fssz "github.com/ferranbt/fastssz"
|
|
stateAltair "github.com/prysmaticlabs/prysm/beacon-chain/state/v2"
|
|
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, "altair", unmarshalledSSZ, customHtr)
|
|
}
|
|
|
|
func customHtr(t *testing.T, htrs []common.HTR, object interface{}) []common.HTR {
|
|
switch object.(type) {
|
|
case *ethpb.BeaconStateAltair:
|
|
htrs = append(htrs, func(s interface{}) ([32]byte, error) {
|
|
beaconState, err := stateAltair.InitializeFromProto(s.(*ethpb.BeaconStateAltair))
|
|
require.NoError(t, err)
|
|
return beaconState.HashTreeRoot(context.Background())
|
|
})
|
|
}
|
|
return htrs
|
|
}
|
|
|
|
// 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.BeaconBlockAltair{}
|
|
case "BeaconBlockBody":
|
|
obj = ðpb.BeaconBlockBodyAltair{}
|
|
case "BeaconBlockHeader":
|
|
obj = ðpb.BeaconBlockHeader{}
|
|
case "BeaconState":
|
|
obj = ðpb.BeaconStateAltair{}
|
|
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.SignedBeaconBlockAltair{}
|
|
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{}
|
|
case "SyncCommitteeMessage":
|
|
obj = ðpb.SyncCommitteeMessage{}
|
|
case "SyncCommitteeContribution":
|
|
obj = ðpb.SyncCommitteeContribution{}
|
|
case "ContributionAndProof":
|
|
obj = ðpb.ContributionAndProof{}
|
|
case "SignedContributionAndProof":
|
|
obj = ðpb.SignedContributionAndProof{}
|
|
case "SyncAggregate":
|
|
obj = ðpb.SyncAggregate{}
|
|
case "SyncAggregatorSelectionData":
|
|
obj = ðpb.SyncAggregatorSelectionData{}
|
|
case "SyncCommittee":
|
|
obj = ðpb.SyncCommittee{}
|
|
case "LightClientSnapshot":
|
|
t.Skip("not a beacon node type, this is a light node type")
|
|
return nil, nil
|
|
case "LightClientUpdate":
|
|
t.Skip("not a beacon node type, this is a light node type")
|
|
return nil, nil
|
|
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
|
|
}
|