prysm-pulse/testing/spectest/shared/capella/ssz_static/ssz_static.go
terencechain d17996f8b0
Update to V4 🚀 (#12134)
* Update V3 from V4

* Fix build v3 -> v4

* Update ssz

* Update beacon_chain.pb.go

* Fix formatter import

* Update update-mockgen.sh comment to v4

* Fix conflicts. Pass build and tests

* Fix test
2023-03-17 18:52:56 +00:00

149 lines
4.5 KiB
Go

package ssz_static
import (
"context"
"errors"
"testing"
fssz "github.com/prysmaticlabs/fastssz"
state_native "github.com/prysmaticlabs/prysm/v4/beacon-chain/state/state-native"
enginev1 "github.com/prysmaticlabs/prysm/v4/proto/engine/v1"
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v4/testing/require"
common "github.com/prysmaticlabs/prysm/v4/testing/spectest/shared/common/ssz_static"
)
// RunSSZStaticTests executes "ssz_static" tests.
func RunSSZStaticTests(t *testing.T, config string) {
common.RunSSZStaticTests(t, config, "capella", unmarshalledSSZ, customHtr)
}
func customHtr(t *testing.T, htrs []common.HTR, object interface{}) []common.HTR {
switch object.(type) {
case *ethpb.BeaconStateCapella:
htrs = append(htrs, func(s interface{}) ([32]byte, error) {
beaconState, err := state_native.InitializeFromProtoCapella(s.(*ethpb.BeaconStateCapella))
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 "ExecutionPayload":
obj = &enginev1.ExecutionPayloadCapella{}
case "ExecutionPayloadHeader":
obj = &enginev1.ExecutionPayloadHeaderCapella{}
case "Attestation":
obj = &ethpb.Attestation{}
case "AttestationData":
obj = &ethpb.AttestationData{}
case "AttesterSlashing":
obj = &ethpb.AttesterSlashing{}
case "AggregateAndProof":
obj = &ethpb.AggregateAttestationAndProof{}
case "BeaconBlock":
obj = &ethpb.BeaconBlockCapella{}
case "BeaconBlockBody":
obj = &ethpb.BeaconBlockBodyCapella{}
case "BeaconBlockHeader":
obj = &ethpb.BeaconBlockHeader{}
case "BeaconState":
obj = &ethpb.BeaconStateCapella{}
case "Checkpoint":
obj = &ethpb.Checkpoint{}
case "Deposit":
obj = &ethpb.Deposit{}
case "DepositMessage":
obj = &ethpb.DepositMessage{}
case "DepositData":
obj = &ethpb.Deposit_Data{}
case "Eth1Data":
obj = &ethpb.Eth1Data{}
case "Eth1Block":
t.Skip("Unused type")
return nil, nil
case "Fork":
obj = &ethpb.Fork{}
case "ForkData":
obj = &ethpb.ForkData{}
case "HistoricalBatch":
obj = &ethpb.HistoricalBatch{}
case "IndexedAttestation":
obj = &ethpb.IndexedAttestation{}
case "LightClientHeader":
t.Skip("not a beacon node type, this is a light node type")
return nil, nil
case "PendingAttestation":
obj = &ethpb.PendingAttestation{}
case "ProposerSlashing":
obj = &ethpb.ProposerSlashing{}
case "SignedAggregateAndProof":
obj = &ethpb.SignedAggregateAttestationAndProof{}
case "SignedBeaconBlock":
obj = &ethpb.SignedBeaconBlockCapella{}
case "SignedBeaconBlockHeader":
obj = &ethpb.SignedBeaconBlockHeader{}
case "SignedVoluntaryExit":
obj = &ethpb.SignedVoluntaryExit{}
case "SigningData":
obj = &ethpb.SigningData{}
case "Validator":
obj = &ethpb.Validator{}
case "VoluntaryExit":
obj = &ethpb.VoluntaryExit{}
case "SyncCommitteeMessage":
obj = &ethpb.SyncCommitteeMessage{}
case "SyncCommitteeContribution":
obj = &ethpb.SyncCommitteeContribution{}
case "ContributionAndProof":
obj = &ethpb.ContributionAndProof{}
case "SignedContributionAndProof":
obj = &ethpb.SignedContributionAndProof{}
case "SyncAggregate":
obj = &ethpb.SyncAggregate{}
case "SyncAggregatorSelectionData":
obj = &ethpb.SyncAggregatorSelectionData{}
case "SyncCommittee":
obj = &ethpb.SyncCommittee{}
case "HistoricalSummary":
obj = &ethpb.HistoricalSummary{}
case "LightClientOptimisticUpdate":
t.Skip("not a beacon node type, this is a light node type")
return nil, nil
case "LightClientFinalityUpdate":
t.Skip("not a beacon node type, this is a light node type")
return nil, nil
case "LightClientBootstrap":
t.Skip("not a beacon node type, this is a light node type")
return nil, nil
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
case "PowBlock":
obj = &ethpb.PowBlock{}
case "Withdrawal":
obj = &enginev1.Withdrawal{}
case "BLSToExecutionChange":
obj = &ethpb.BLSToExecutionChange{}
case "SignedBLSToExecutionChange":
obj = &ethpb.SignedBLSToExecutionChange{}
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
}