From d2628f34b3aa6a6af24fb770a214e75a090343ff Mon Sep 17 00:00:00 2001 From: rkapka Date: Tue, 30 Apr 2024 21:46:11 +0900 Subject: [PATCH] test fixes --- beacon-chain/blockchain/head_test.go | 7 +- .../blockchain/process_attestation_test.go | 3 +- beacon-chain/core/helpers/BUILD.bazel | 1 + beacon-chain/core/helpers/attestation_test.go | 3 +- .../attestations/kv/aggregated_test.go | 8 +- .../operations/attestations/kv/block_test.go | 7 +- .../attestations/kv/forkchoice_test.go | 7 +- .../attestations/kv/unaggregated_test.go | 16 +-- beacon-chain/operations/slashings/BUILD.bazel | 1 + .../slashings/service_attester_test.go | 13 +- .../v1alpha1/beacon/attestations_test.go | 16 ++- .../validator/proposer_slashings_test.go | 3 +- .../subscriber_beacon_aggregate_proof_test.go | 5 +- consensus-types/blocks/factory.go | 120 +++++++++++------- consensus-types/blocks/getters.go | 12 ++ .../aggregation/attestations/maxcover_test.go | 52 ++++---- 16 files changed, 163 insertions(+), 111 deletions(-) diff --git a/beacon-chain/blockchain/head_test.go b/beacon-chain/blockchain/head_test.go index c5fa824f9643..d1e5b1c1de51 100644 --- a/beacon-chain/blockchain/head_test.go +++ b/beacon-chain/blockchain/head_test.go @@ -13,6 +13,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/beacon-chain/operations/blstoexec" "github.com/prysmaticlabs/prysm/v5/config/params" "github.com/prysmaticlabs/prysm/v5/consensus-types/blocks" + "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" "github.com/prysmaticlabs/prysm/v5/encoding/bytesutil" ethpbv1 "github.com/prysmaticlabs/prysm/v5/proto/eth/v1" @@ -312,7 +313,7 @@ func TestSaveOrphanedAtts(t *testing.T) { require.NoError(t, service.saveOrphanedOperations(ctx, r3, r4)) require.Equal(t, 3, service.cfg.AttPool.AggregatedAttestationCount()) - wantAtts := []*ethpb.Attestation{ + wantAtts := []interfaces.Attestation{ blk3.Block.Body.Attestations[0], blk2.Block.Body.Attestations[0], blk1.Block.Body.Attestations[0], @@ -389,7 +390,7 @@ func TestSaveOrphanedOps(t *testing.T) { require.NoError(t, service.saveOrphanedOperations(ctx, r3, r4)) require.Equal(t, 3, service.cfg.AttPool.AggregatedAttestationCount()) - wantAtts := []*ethpb.Attestation{ + wantAtts := []interfaces.Attestation{ blk3.Block.Body.Attestations[0], blk2.Block.Body.Attestations[0], blk1.Block.Body.Attestations[0], @@ -517,7 +518,7 @@ func TestSaveOrphanedAtts_DoublyLinkedTrie(t *testing.T) { require.NoError(t, service.saveOrphanedOperations(ctx, r3, r4)) require.Equal(t, 3, service.cfg.AttPool.AggregatedAttestationCount()) - wantAtts := []*ethpb.Attestation{ + wantAtts := []interfaces.Attestation{ blk3.Block.Body.Attestations[0], blk2.Block.Body.Attestations[0], blk1.Block.Body.Attestations[0], diff --git a/beacon-chain/blockchain/process_attestation_test.go b/beacon-chain/blockchain/process_attestation_test.go index b84298ccb1ed..9b0f722051b1 100644 --- a/beacon-chain/blockchain/process_attestation_test.go +++ b/beacon-chain/blockchain/process_attestation_test.go @@ -9,6 +9,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/transition" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" "github.com/prysmaticlabs/prysm/v5/config/params" + "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" "github.com/prysmaticlabs/prysm/v5/encoding/bytesutil" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" @@ -73,7 +74,7 @@ func TestStore_OnAttestation_ErrorConditions(t *testing.T) { tests := []struct { name string - a *ethpb.Attestation + a interfaces.Attestation wantedErr string }{ { diff --git a/beacon-chain/core/helpers/BUILD.bazel b/beacon-chain/core/helpers/BUILD.bazel index c8b45a534c18..1912d6c2998c 100644 --- a/beacon-chain/core/helpers/BUILD.bazel +++ b/beacon-chain/core/helpers/BUILD.bazel @@ -73,6 +73,7 @@ go_test( "//beacon-chain/state/state-native:go_default_library", "//config/fieldparams:go_default_library", "//config/params:go_default_library", + "//consensus-types/interfaces:go_default_library", "//consensus-types/primitives:go_default_library", "//container/slice:go_default_library", "//crypto/hash:go_default_library", diff --git a/beacon-chain/core/helpers/attestation_test.go b/beacon-chain/core/helpers/attestation_test.go index 5c3a34a5087a..6ffb5f447695 100644 --- a/beacon-chain/core/helpers/attestation_test.go +++ b/beacon-chain/core/helpers/attestation_test.go @@ -9,6 +9,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/helpers" state_native "github.com/prysmaticlabs/prysm/v5/beacon-chain/state/state-native" "github.com/prysmaticlabs/prysm/v5/config/params" + "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/testing/assert" @@ -238,7 +239,7 @@ func TestVerifyCheckpointEpoch_Ok(t *testing.T) { func TestValidateNilAttestation(t *testing.T) { tests := []struct { name string - attestation *ethpb.Attestation + attestation interfaces.Attestation errString string }{ { diff --git a/beacon-chain/operations/attestations/kv/aggregated_test.go b/beacon-chain/operations/attestations/kv/aggregated_test.go index b3abc4217a29..37a8e5013ddd 100644 --- a/beacon-chain/operations/attestations/kv/aggregated_test.go +++ b/beacon-chain/operations/attestations/kv/aggregated_test.go @@ -42,7 +42,7 @@ func TestKV_Aggregated_AggregateUnaggregatedAttestations(t *testing.T) { func TestKV_Aggregated_SaveAggregatedAttestation(t *testing.T) { tests := []struct { name string - att *ethpb.Attestation + att interfaces.Attestation count int wantErrString string }{ @@ -192,7 +192,7 @@ func TestKV_Aggregated_AggregatedAttestations(t *testing.T) { att1 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}}) att2 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}}) att3 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}}) - atts := []*ethpb.Attestation{att1, att2, att3} + atts := []interfaces.Attestation{att1, att2, att3} for _, att := range atts { require.NoError(t, cache.SaveAggregatedAttestation(att)) @@ -253,7 +253,7 @@ func TestKV_Aggregated_DeleteAggregatedAttestation(t *testing.T) { require.NoError(t, cache.DeleteAggregatedAttestation(att3)) returned := cache.AggregatedAttestations() - wanted := []*ethpb.Attestation{att2} + wanted := []interfaces.Attestation{att2} assert.DeepEqual(t, wanted, returned) }) @@ -270,7 +270,7 @@ func TestKV_Aggregated_DeleteAggregatedAttestation(t *testing.T) { require.NoError(t, cache.DeleteAggregatedAttestation(att4)) returned := cache.AggregatedAttestations() - wanted := []*ethpb.Attestation{att1, att2} + wanted := []interfaces.Attestation{att1, att2} sort.Slice(returned, func(i, j int) bool { return string(returned[i].GetAggregationBits()) < string(returned[j].GetAggregationBits()) }) diff --git a/beacon-chain/operations/attestations/kv/block_test.go b/beacon-chain/operations/attestations/kv/block_test.go index 6b6f170ee1f6..c1a26181e925 100644 --- a/beacon-chain/operations/attestations/kv/block_test.go +++ b/beacon-chain/operations/attestations/kv/block_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/prysmaticlabs/go-bitfield" + "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/testing/assert" "github.com/prysmaticlabs/prysm/v5/testing/require" @@ -17,7 +18,7 @@ func TestKV_BlockAttestation_CanSaveRetrieve(t *testing.T) { att1 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}}) att2 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}}) att3 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}}) - atts := []*ethpb.Attestation{att1, att2, att3} + atts := []interfaces.Attestation{att1, att2, att3} for _, att := range atts { require.NoError(t, cache.SaveBlockAttestation(att)) @@ -43,7 +44,7 @@ func TestKV_BlockAttestation_CanDelete(t *testing.T) { att1 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}}) att2 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}}) att3 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}}) - atts := []*ethpb.Attestation{att1, att2, att3} + atts := []interfaces.Attestation{att1, att2, att3} for _, att := range atts { require.NoError(t, cache.SaveBlockAttestation(att)) @@ -53,6 +54,6 @@ func TestKV_BlockAttestation_CanDelete(t *testing.T) { require.NoError(t, cache.DeleteBlockAttestation(att3)) returned := cache.BlockAttestations() - wanted := []*ethpb.Attestation{att2} + wanted := []interfaces.Attestation{att2} assert.DeepEqual(t, wanted, returned) } diff --git a/beacon-chain/operations/attestations/kv/forkchoice_test.go b/beacon-chain/operations/attestations/kv/forkchoice_test.go index aa5e89ff24d1..005c53f79b70 100644 --- a/beacon-chain/operations/attestations/kv/forkchoice_test.go +++ b/beacon-chain/operations/attestations/kv/forkchoice_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/prysmaticlabs/go-bitfield" + "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/testing/assert" "github.com/prysmaticlabs/prysm/v5/testing/require" @@ -17,7 +18,7 @@ func TestKV_Forkchoice_CanSaveRetrieve(t *testing.T) { att1 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}}) att2 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}}) att3 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}}) - atts := []*ethpb.Attestation{att1, att2, att3} + atts := []interfaces.Attestation{att1, att2, att3} for _, att := range atts { require.NoError(t, cache.SaveForkchoiceAttestation(att)) @@ -38,7 +39,7 @@ func TestKV_Forkchoice_CanDelete(t *testing.T) { att1 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 1}, AggregationBits: bitfield.Bitlist{0b1101}}) att2 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 2}, AggregationBits: bitfield.Bitlist{0b1101}}) att3 := util.HydrateAttestation(ðpb.Attestation{Data: ðpb.AttestationData{Slot: 3}, AggregationBits: bitfield.Bitlist{0b1101}}) - atts := []*ethpb.Attestation{att1, att2, att3} + atts := []interfaces.Attestation{att1, att2, att3} for _, att := range atts { require.NoError(t, cache.SaveForkchoiceAttestation(att)) @@ -48,7 +49,7 @@ func TestKV_Forkchoice_CanDelete(t *testing.T) { require.NoError(t, cache.DeleteForkchoiceAttestation(att3)) returned := cache.ForkchoiceAttestations() - wanted := []*ethpb.Attestation{att2} + wanted := []interfaces.Attestation{att2} assert.DeepEqual(t, wanted, returned) } diff --git a/beacon-chain/operations/attestations/kv/unaggregated_test.go b/beacon-chain/operations/attestations/kv/unaggregated_test.go index f699fca6bee7..115199d1b1e5 100644 --- a/beacon-chain/operations/attestations/kv/unaggregated_test.go +++ b/beacon-chain/operations/attestations/kv/unaggregated_test.go @@ -20,7 +20,7 @@ import ( func TestKV_Unaggregated_SaveUnaggregatedAttestation(t *testing.T) { tests := []struct { name string - att *ethpb.Attestation + att interfaces.Attestation count int wantErrString string }{ @@ -67,8 +67,8 @@ func TestKV_Unaggregated_SaveUnaggregatedAttestation(t *testing.T) { cache.seenAtt.Set(string(r[:]), []bitfield.Bitlist{{0xff}}, c.DefaultExpiration) assert.Equal(t, 0, len(cache.unAggregatedAtt), "Invalid start pool, atts: %d", len(cache.unAggregatedAtt)) - if tt.att != nil && tt.att.Signature == nil { - tt.att.Signature = make([]byte, fieldparams.BLSSignatureLength) + if tt.att != nil && tt.att.GetSignature() == nil { + tt.att.(*ethpb.Attestation).Signature = make([]byte, fieldparams.BLSSignatureLength) } err := cache.SaveUnaggregatedAttestation(tt.att) @@ -153,7 +153,7 @@ func TestKV_Unaggregated_DeleteUnaggregatedAttestation(t *testing.T) { } returned, err := cache.UnaggregatedAttestations() require.NoError(t, err) - assert.DeepEqual(t, []*ethpb.Attestation{}, returned) + assert.DeepEqual(t, []interfaces.Attestation{}, returned) }) } @@ -230,7 +230,7 @@ func TestKV_Unaggregated_DeleteSeenUnaggregatedAttestations(t *testing.T) { assert.Equal(t, 0, cache.UnaggregatedAttestationCount()) returned, err := cache.UnaggregatedAttestations() require.NoError(t, err) - assert.DeepEqual(t, []*ethpb.Attestation{}, returned) + assert.DeepEqual(t, []interfaces.Attestation{}, returned) }) } @@ -247,9 +247,9 @@ func TestKV_Unaggregated_UnaggregatedAttestationsBySlotIndex(t *testing.T) { } ctx := context.Background() returned := cache.UnaggregatedAttestationsBySlotIndex(ctx, 1, 1) - assert.DeepEqual(t, []*ethpb.Attestation{att1}, returned) + assert.DeepEqual(t, []interfaces.Attestation{att1}, returned) returned = cache.UnaggregatedAttestationsBySlotIndex(ctx, 1, 2) - assert.DeepEqual(t, []*ethpb.Attestation{att2}, returned) + assert.DeepEqual(t, []interfaces.Attestation{att2}, returned) returned = cache.UnaggregatedAttestationsBySlotIndex(ctx, 2, 1) - assert.DeepEqual(t, []*ethpb.Attestation{att3}, returned) + assert.DeepEqual(t, []interfaces.Attestation{att3}, returned) } diff --git a/beacon-chain/operations/slashings/BUILD.bazel b/beacon-chain/operations/slashings/BUILD.bazel index 82a6142e3bd2..3d158ccce832 100644 --- a/beacon-chain/operations/slashings/BUILD.bazel +++ b/beacon-chain/operations/slashings/BUILD.bazel @@ -48,6 +48,7 @@ go_test( "//beacon-chain/state:go_default_library", "//config/fieldparams:go_default_library", "//config/params:go_default_library", + "//consensus-types/interfaces:go_default_library", "//consensus-types/primitives:go_default_library", "//crypto/bls:go_default_library", "//proto/prysm/v1alpha1:go_default_library", diff --git a/beacon-chain/operations/slashings/service_attester_test.go b/beacon-chain/operations/slashings/service_attester_test.go index 3abd20cf10df..a032d52d2732 100644 --- a/beacon-chain/operations/slashings/service_attester_test.go +++ b/beacon-chain/operations/slashings/service_attester_test.go @@ -6,6 +6,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/beacon-chain/state" "github.com/prysmaticlabs/prysm/v5/config/params" + "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" "github.com/prysmaticlabs/prysm/v5/crypto/bls" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" @@ -455,7 +456,7 @@ func TestPool_PendingAttesterSlashings(t *testing.T) { params.SetupTestConfigCleanup(t) beaconState, privKeys := util.DeterministicGenesisState(t, 64) pendingSlashings := make([]*PendingAttesterSlashing, 20) - slashings := make([]*ethpb.AttesterSlashing, 20) + slashings := make([]interfaces.AttesterSlashing, 20) for i := 0; i < len(pendingSlashings); i++ { sl, err := util.GenerateAttesterSlashingForValidator(beaconState, privKeys[i], primitives.ValidatorIndex(i)) require.NoError(t, err) @@ -468,14 +469,14 @@ func TestPool_PendingAttesterSlashings(t *testing.T) { tests := []struct { name string fields fields - want []*ethpb.AttesterSlashing + want []interfaces.AttesterSlashing }{ { name: "Empty list", fields: fields{ pending: []*PendingAttesterSlashing{}, }, - want: []*ethpb.AttesterSlashing{}, + want: []interfaces.AttesterSlashing{}, }, { name: "All pending", @@ -530,7 +531,7 @@ func TestPool_PendingAttesterSlashings_Slashed(t *testing.T) { require.NoError(t, beaconState.UpdateValidatorAtIndex(5, val)) pendingSlashings := make([]*PendingAttesterSlashing, 20) pendingSlashings2 := make([]*PendingAttesterSlashing, 20) - slashings := make([]*ethpb.AttesterSlashing, 20) + slashings := make([]interfaces.AttesterSlashing, 20) for i := 0; i < len(pendingSlashings); i++ { sl, err := util.GenerateAttesterSlashingForValidator(beaconState, privKeys[i], primitives.ValidatorIndex(i)) require.NoError(t, err) @@ -548,7 +549,7 @@ func TestPool_PendingAttesterSlashings_Slashed(t *testing.T) { tests := []struct { name string fields fields - want []*ethpb.AttesterSlashing + want []interfaces.AttesterSlashing }{ { name: "One item", @@ -588,7 +589,7 @@ func TestPool_PendingAttesterSlashings_NoDuplicates(t *testing.T) { params.OverrideBeaconConfig(conf) beaconState, privKeys := util.DeterministicGenesisState(t, 64) pendingSlashings := make([]*PendingAttesterSlashing, 3) - slashings := make([]*ethpb.AttesterSlashing, 3) + slashings := make([]interfaces.AttesterSlashing, 3) for i := 0; i < 2; i++ { sl, err := util.GenerateAttesterSlashingForValidator(beaconState, privKeys[i], primitives.ValidatorIndex(i)) require.NoError(t, err) diff --git a/beacon-chain/rpc/prysm/v1alpha1/beacon/attestations_test.go b/beacon-chain/rpc/prysm/v1alpha1/beacon/attestations_test.go index 6d9fc9a4e409..bcc63c7c4705 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/beacon/attestations_test.go +++ b/beacon-chain/rpc/prysm/v1alpha1/beacon/attestations_test.go @@ -545,14 +545,16 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { state, _ := util.DeterministicGenesisState(t, numValidators) // Next up we convert the test attestations to indexed form: - indexedAtts := make([]ethpb.IndexedAtt, len(atts)+len(atts2)) + indexedAtts := make([]*ethpb.IndexedAttestation, len(atts)+len(atts2)) for i := 0; i < len(atts); i++ { att := atts[i] committee, err := helpers.BeaconCommitteeFromState(context.Background(), state, att.Data.Slot, att.Data.CommitteeIndex) require.NoError(t, err) idxAtt, err := attestation.ConvertToIndexed(ctx, atts[i], committee) require.NoError(t, err, "Could not convert attestation to indexed") - indexedAtts[i] = idxAtt + a, ok := idxAtt.(*ethpb.IndexedAttestation) + require.Equal(t, true, ok, "unexpected type of indexed attestation") + indexedAtts[i] = a } for i := 0; i < len(atts2); i++ { att := atts2[i] @@ -560,7 +562,9 @@ func TestServer_ListIndexedAttestations_GenesisEpoch(t *testing.T) { require.NoError(t, err) idxAtt, err := attestation.ConvertToIndexed(ctx, atts2[i], committee) require.NoError(t, err, "Could not convert attestation to indexed") - indexedAtts[i+len(atts)] = idxAtt + a, ok := idxAtt.(*ethpb.IndexedAttestation) + require.Equal(t, true, ok, "unexpected type of indexed attestation") + indexedAtts[i+len(atts)] = a } bs := &Server{ @@ -652,14 +656,16 @@ func TestServer_ListIndexedAttestations_OldEpoch(t *testing.T) { require.NoError(t, state.SetSlot(startSlot)) // Next up we convert the test attestations to indexed form: - indexedAtts := make([]ethpb.IndexedAtt, len(atts)) + indexedAtts := make([]*ethpb.IndexedAttestation, len(atts)) for i := 0; i < len(atts); i++ { att := atts[i] committee, err := helpers.BeaconCommitteeFromState(context.Background(), state, att.Data.Slot, att.Data.CommitteeIndex) require.NoError(t, err) idxAtt, err := attestation.ConvertToIndexed(ctx, atts[i], committee) require.NoError(t, err, "Could not convert attestation to indexed") - indexedAtts[i] = idxAtt + a, ok := idxAtt.(*ethpb.IndexedAttestation) + require.Equal(t, true, ok, "unexpected type of indexed attestation") + indexedAtts[i] = a } bs := &Server{ diff --git a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_slashings_test.go b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_slashings_test.go index 78f2351c0aee..3411a8b730cd 100644 --- a/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_slashings_test.go +++ b/beacon-chain/rpc/prysm/v1alpha1/validator/proposer_slashings_test.go @@ -6,6 +6,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/beacon-chain/operations/slashings" "github.com/prysmaticlabs/prysm/v5/config/params" + "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/testing/require" @@ -28,7 +29,7 @@ func TestServer_getSlashings(t *testing.T) { require.NoError(t, err) } - attSlashings := make([]*ethpb.AttesterSlashing, params.BeaconConfig().MaxAttesterSlashings) + attSlashings := make([]interfaces.AttesterSlashing, params.BeaconConfig().MaxAttesterSlashings) for i := uint64(0); i < params.BeaconConfig().MaxAttesterSlashings; i++ { attesterSlashing, err := util.GenerateAttesterSlashingForValidator( beaconState, diff --git a/beacon-chain/sync/subscriber_beacon_aggregate_proof_test.go b/beacon-chain/sync/subscriber_beacon_aggregate_proof_test.go index 3c4e2e957bec..eecc49fb9951 100644 --- a/beacon-chain/sync/subscriber_beacon_aggregate_proof_test.go +++ b/beacon-chain/sync/subscriber_beacon_aggregate_proof_test.go @@ -9,6 +9,7 @@ import ( "github.com/prysmaticlabs/prysm/v5/beacon-chain/operations/attestations" lruwrpr "github.com/prysmaticlabs/prysm/v5/cache/lru" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" + "github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/testing/assert" "github.com/prysmaticlabs/prysm/v5/testing/require" @@ -34,7 +35,7 @@ func TestBeaconAggregateProofSubscriber_CanSaveAggregatedAttestation(t *testing. Signature: make([]byte, fieldparams.BLSSignatureLength), } require.NoError(t, r.beaconAggregateProofSubscriber(context.Background(), a)) - assert.DeepSSZEqual(t, []*ethpb.Attestation{a.Message.Aggregate}, r.cfg.attPool.AggregatedAttestations(), "Did not save aggregated attestation") + assert.DeepSSZEqual(t, []interfaces.Attestation{a.Message.Aggregate}, r.cfg.attPool.AggregatedAttestations(), "Did not save aggregated attestation") } func TestBeaconAggregateProofSubscriber_CanSaveUnaggregatedAttestation(t *testing.T) { @@ -59,5 +60,5 @@ func TestBeaconAggregateProofSubscriber_CanSaveUnaggregatedAttestation(t *testin atts, err := r.cfg.attPool.UnaggregatedAttestations() require.NoError(t, err) - assert.DeepEqual(t, []*ethpb.Attestation{a.Message.Aggregate}, atts, "Did not save unaggregated attestation") + assert.DeepEqual(t, []interfaces.Attestation{a.Message.Aggregate}, atts, "Did not save unaggregated attestation") } diff --git a/consensus-types/blocks/factory.go b/consensus-types/blocks/factory.go index 3c0586b6e324..80d266a45e15 100644 --- a/consensus-types/blocks/factory.go +++ b/consensus-types/blocks/factory.go @@ -305,21 +305,27 @@ func BuildSignedBeaconBlockFromExecutionPayload(blk interfaces.ReadOnlySignedBea var fullBlock interface{} switch p := payload.(type) { case *enginev1.ExecutionPayload: - atts := make([]*eth.Attestation, len(b.Body().Attestations())) - for i, att := range b.Body().Attestations() { - a, ok := att.(*eth.Attestation) - if !ok { - return nil, fmt.Errorf("attestation has wrong type (expected %T, got %T)", ð.Attestation{}, att) + var atts []*eth.Attestation + if b.Body().Attestations() != nil { + atts = make([]*eth.Attestation, len(b.Body().Attestations())) + for i, att := range b.Body().Attestations() { + a, ok := att.(*eth.Attestation) + if !ok { + return nil, fmt.Errorf("attestation has wrong type (expected %T, got %T)", ð.Attestation{}, att) + } + atts[i] = a } - atts[i] = a } - attSlashings := make([]*eth.AttesterSlashing, len(b.Body().AttesterSlashings())) - for i, slashing := range b.Body().AttesterSlashings() { - s, ok := slashing.(*eth.AttesterSlashing) - if !ok { - return nil, fmt.Errorf("attester slashing has wrong type (expected %T, got %T)", ð.AttesterSlashing{}, slashing) + var attSlashings []*eth.AttesterSlashing + if b.Body().AttesterSlashings() != nil { + attSlashings = make([]*eth.AttesterSlashing, len(b.Body().AttesterSlashings())) + for i, slashing := range b.Body().AttesterSlashings() { + s, ok := slashing.(*eth.AttesterSlashing) + if !ok { + return nil, fmt.Errorf("attester slashing has wrong type (expected %T, got %T)", ð.AttesterSlashing{}, slashing) + } + attSlashings[i] = s } - attSlashings[i] = s } fullBlock = ð.SignedBeaconBlockBellatrix{ Block: ð.BeaconBlockBellatrix{ @@ -347,21 +353,27 @@ func BuildSignedBeaconBlockFromExecutionPayload(blk interfaces.ReadOnlySignedBea if err != nil { return nil, err } - atts := make([]*eth.Attestation, len(b.Body().Attestations())) - for i, att := range b.Body().Attestations() { - a, ok := att.(*eth.Attestation) - if !ok { - return nil, fmt.Errorf("attestation has wrong type (expected %T, got %T)", ð.Attestation{}, att) + var atts []*eth.Attestation + if b.Body().Attestations() != nil { + atts = make([]*eth.Attestation, len(b.Body().Attestations())) + for i, att := range b.Body().Attestations() { + a, ok := att.(*eth.Attestation) + if !ok { + return nil, fmt.Errorf("attestation has wrong type (expected %T, got %T)", ð.Attestation{}, att) + } + atts[i] = a } - atts[i] = a } - attSlashings := make([]*eth.AttesterSlashing, len(b.Body().AttesterSlashings())) - for i, slashing := range b.Body().AttesterSlashings() { - s, ok := slashing.(*eth.AttesterSlashing) - if !ok { - return nil, fmt.Errorf("attester slashing has wrong type (expected %T, got %T)", ð.AttesterSlashing{}, slashing) + var attSlashings []*eth.AttesterSlashing + if b.Body().AttesterSlashings() != nil { + attSlashings = make([]*eth.AttesterSlashing, len(b.Body().AttesterSlashings())) + for i, slashing := range b.Body().AttesterSlashings() { + s, ok := slashing.(*eth.AttesterSlashing) + if !ok { + return nil, fmt.Errorf("attester slashing has wrong type (expected %T, got %T)", ð.AttesterSlashing{}, slashing) + } + attSlashings[i] = s } - attSlashings[i] = s } fullBlock = ð.SignedBeaconBlockCapella{ Block: ð.BeaconBlockCapella{ @@ -394,21 +406,27 @@ func BuildSignedBeaconBlockFromExecutionPayload(blk interfaces.ReadOnlySignedBea if err != nil { return nil, err } - atts := make([]*eth.Attestation, len(b.Body().Attestations())) - for i, att := range b.Body().Attestations() { - a, ok := att.(*eth.Attestation) - if !ok { - return nil, fmt.Errorf("attestation has wrong type (expected %T, got %T)", ð.Attestation{}, att) + var atts []*eth.Attestation + if b.Body().Attestations() != nil { + atts = make([]*eth.Attestation, len(b.Body().Attestations())) + for i, att := range b.Body().Attestations() { + a, ok := att.(*eth.Attestation) + if !ok { + return nil, fmt.Errorf("attestation has wrong type (expected %T, got %T)", ð.Attestation{}, att) + } + atts[i] = a } - atts[i] = a } - attSlashings := make([]*eth.AttesterSlashing, len(b.Body().AttesterSlashings())) - for i, slashing := range b.Body().AttesterSlashings() { - s, ok := slashing.(*eth.AttesterSlashing) - if !ok { - return nil, fmt.Errorf("attester slashing has wrong type (expected %T, got %T)", ð.AttesterSlashing{}, slashing) + var attSlashings []*eth.AttesterSlashing + if b.Body().AttesterSlashings() != nil { + attSlashings = make([]*eth.AttesterSlashing, len(b.Body().AttesterSlashings())) + for i, slashing := range b.Body().AttesterSlashings() { + s, ok := slashing.(*eth.AttesterSlashing) + if !ok { + return nil, fmt.Errorf("attester slashing has wrong type (expected %T, got %T)", ð.AttesterSlashing{}, slashing) + } + attSlashings[i] = s } - attSlashings[i] = s } fullBlock = ð.SignedBeaconBlockDeneb{ Block: ð.BeaconBlockDeneb{ @@ -446,21 +464,27 @@ func BuildSignedBeaconBlockFromExecutionPayload(blk interfaces.ReadOnlySignedBea if err != nil { return nil, err } - atts := make([]*eth.AttestationElectra, len(b.Body().Attestations())) - for i, att := range b.Body().Attestations() { - a, ok := att.(*eth.AttestationElectra) - if !ok { - return nil, fmt.Errorf("attestation has wrong type (expected %T, got %T)", ð.AttestationElectra{}, att) + var atts []*eth.AttestationElectra + if b.Body().Attestations() != nil { + atts = make([]*eth.AttestationElectra, len(b.Body().Attestations())) + for i, att := range b.Body().Attestations() { + a, ok := att.(*eth.AttestationElectra) + if !ok { + return nil, fmt.Errorf("attestation has wrong type (expected %T, got %T)", ð.Attestation{}, att) + } + atts[i] = a } - atts[i] = a } - attSlashings := make([]*eth.AttesterSlashingElectra, len(b.Body().AttesterSlashings())) - for i, slashing := range b.Body().AttesterSlashings() { - s, ok := slashing.(*eth.AttesterSlashingElectra) - if !ok { - return nil, fmt.Errorf("attester slashing has wrong type (expected %T, got %T)", ð.AttesterSlashingElectra{}, slashing) + var attSlashings []*eth.AttesterSlashingElectra + if b.Body().AttesterSlashings() != nil { + attSlashings = make([]*eth.AttesterSlashingElectra, len(b.Body().AttesterSlashings())) + for i, slashing := range b.Body().AttesterSlashings() { + s, ok := slashing.(*eth.AttesterSlashingElectra) + if !ok { + return nil, fmt.Errorf("attester slashing has wrong type (expected %T, got %T)", ð.AttesterSlashing{}, slashing) + } + attSlashings[i] = s } - attSlashings[i] = s } fullBlock = ð.SignedBeaconBlockElectra{ Block: ð.BeaconBlockElectra{ diff --git a/consensus-types/blocks/getters.go b/consensus-types/blocks/getters.go index 615b19a8d398..4cc0f2c20398 100644 --- a/consensus-types/blocks/getters.go +++ b/consensus-types/blocks/getters.go @@ -1174,11 +1174,17 @@ func (b *BeaconBlockBody) ProposerSlashings() []*eth.ProposerSlashing { func (b *BeaconBlockBody) AttesterSlashings() []interfaces.AttesterSlashing { var slashings []interfaces.AttesterSlashing if b.version < version.Electra { + if b.attesterSlashings == nil { + return nil + } slashings = make([]interfaces.AttesterSlashing, len(b.attesterSlashings)) for i, s := range b.attesterSlashings { slashings[i] = s } } else { + if b.attesterSlashingsElectra == nil { + return nil + } slashings = make([]interfaces.AttesterSlashing, len(b.attesterSlashingsElectra)) for i, s := range b.attesterSlashingsElectra { slashings[i] = s @@ -1191,11 +1197,17 @@ func (b *BeaconBlockBody) AttesterSlashings() []interfaces.AttesterSlashing { func (b *BeaconBlockBody) Attestations() []interfaces.Attestation { var atts []interfaces.Attestation if b.version < version.Electra { + if b.attestations == nil { + return nil + } atts = make([]interfaces.Attestation, len(b.attestations)) for i, a := range b.attestations { atts[i] = a } } else { + if b.attestationsElectra == nil { + return nil + } atts = make([]interfaces.Attestation, len(b.attestationsElectra)) for i, a := range b.attestations { atts[i] = a diff --git a/proto/prysm/v1alpha1/attestation/aggregation/attestations/maxcover_test.go b/proto/prysm/v1alpha1/attestation/aggregation/attestations/maxcover_test.go index 318cbfca5bc4..638c85ded6b1 100644 --- a/proto/prysm/v1alpha1/attestation/aggregation/attestations/maxcover_test.go +++ b/proto/prysm/v1alpha1/attestation/aggregation/attestations/maxcover_test.go @@ -147,7 +147,7 @@ func TestAggregateAttestations_rearrangeProcessedAttestations(t *testing.T) { name string atts []interfaces.Attestation keys []int - wantAtts []*ethpb.Attestation + wantAtts []interfaces.Attestation }{ { name: "nil attestations", @@ -157,8 +157,8 @@ func TestAggregateAttestations_rearrangeProcessedAttestations(t *testing.T) { atts: []interfaces.Attestation{ ðpb.Attestation{}, }, - wantAtts: []*ethpb.Attestation{ - {}, + wantAtts: []interfaces.Attestation{ + ðpb.Attestation{}, }, }, { @@ -167,7 +167,7 @@ func TestAggregateAttestations_rearrangeProcessedAttestations(t *testing.T) { ðpb.Attestation{}, }, keys: []int{0}, - wantAtts: []*ethpb.Attestation{ + wantAtts: []interfaces.Attestation{ nil, }, }, @@ -181,10 +181,10 @@ func TestAggregateAttestations_rearrangeProcessedAttestations(t *testing.T) { ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x04}}, }, keys: []int{1, 4}, // Only attestation at index 1, should be moved, att at 4 is already at the end. - wantAtts: []*ethpb.Attestation{ - {AggregationBits: bitfield.Bitlist{0x00}}, - {AggregationBits: bitfield.Bitlist{0x03}}, - {AggregationBits: bitfield.Bitlist{0x02}}, + wantAtts: []interfaces.Attestation{ + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x00}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x03}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x02}}, nil, nil, }, }, @@ -198,7 +198,7 @@ func TestAggregateAttestations_rearrangeProcessedAttestations(t *testing.T) { ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x04}}, }, keys: []int{0, 1, 2, 3, 4}, - wantAtts: []*ethpb.Attestation{ + wantAtts: []interfaces.Attestation{ nil, nil, nil, nil, nil, }, }, @@ -214,11 +214,11 @@ func TestAggregateAttestations_rearrangeProcessedAttestations(t *testing.T) { nil, nil, nil, }, keys: []int{2}, - wantAtts: []*ethpb.Attestation{ - {AggregationBits: bitfield.Bitlist{0x00}}, - {AggregationBits: bitfield.Bitlist{0x01}}, - {AggregationBits: bitfield.Bitlist{0x04}}, - {AggregationBits: bitfield.Bitlist{0x03}}, + wantAtts: []interfaces.Attestation{ + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x00}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x01}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x04}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x03}}, nil, nil, nil, nil, }, }, @@ -235,11 +235,11 @@ func TestAggregateAttestations_rearrangeProcessedAttestations(t *testing.T) { nil, nil, nil, }, keys: []int{2, 3}, - wantAtts: []*ethpb.Attestation{ - {AggregationBits: bitfield.Bitlist{0x00}}, - {AggregationBits: bitfield.Bitlist{0x01}}, - {AggregationBits: bitfield.Bitlist{0x05}}, - {AggregationBits: bitfield.Bitlist{0x04}}, + wantAtts: []interfaces.Attestation{ + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x00}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x01}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x05}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x04}}, nil, nil, nil, nil, nil, }, }, @@ -255,10 +255,10 @@ func TestAggregateAttestations_rearrangeProcessedAttestations(t *testing.T) { nil, nil, nil, }, keys: []int{2, 4}, - wantAtts: []*ethpb.Attestation{ - {AggregationBits: bitfield.Bitlist{0x00}}, - {AggregationBits: bitfield.Bitlist{0x01}}, - {AggregationBits: bitfield.Bitlist{0x03}}, + wantAtts: []interfaces.Attestation{ + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x00}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x01}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x03}}, nil, nil, nil, nil, nil, }, }, @@ -274,9 +274,9 @@ func TestAggregateAttestations_rearrangeProcessedAttestations(t *testing.T) { ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x06}}, }, keys: []int{4, 1, 2, 5, 6}, - wantAtts: []*ethpb.Attestation{ - {AggregationBits: bitfield.Bitlist{0x00}}, - {AggregationBits: bitfield.Bitlist{0x03}}, + wantAtts: []interfaces.Attestation{ + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x00}}, + ðpb.Attestation{AggregationBits: bitfield.Bitlist{0x03}}, nil, nil, nil, nil, nil, }, },