diff --git a/beacon-chain/core/helpers/beacon_committee_test.go b/beacon-chain/core/helpers/beacon_committee_test.go index 007685617e2..a9a95facc43 100644 --- a/beacon-chain/core/helpers/beacon_committee_test.go +++ b/beacon-chain/core/helpers/beacon_committee_test.go @@ -97,6 +97,13 @@ func TestVerifyBitfieldLength_OK(t *testing.T) { assert.NoError(t, helpers.VerifyBitfieldLength(bf, committeeSize), "Bitfield is not validated when it was supposed to be") } +func TestVerifyBitfieldLength_Incorrect(t *testing.T) { + helpers.ClearCache() + + bf := bitfield.NewBitlist(1) + require.ErrorContains(t, "wanted participants bitfield length 2, got: 1", helpers.VerifyBitfieldLength(bf, 2)) +} + func TestCommitteeAssignments_CannotRetrieveFutureEpoch(t *testing.T) { helpers.ClearCache() diff --git a/beacon-chain/sync/pending_attestations_queue_test.go b/beacon-chain/sync/pending_attestations_queue_test.go index cccdefa1fbc..702cff39474 100644 --- a/beacon-chain/sync/pending_attestations_queue_test.go +++ b/beacon-chain/sync/pending_attestations_queue_test.go @@ -92,18 +92,9 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) { att.Signature = privKeys[i].Sign(hashTreeRoot[:]).Marshal() } - // Arbitrary aggregator index for testing purposes. - aggregatorIndex := committee[0] - sszUint := primitives.SSZUint64(att.Data.Slot) - sig, err := signing.ComputeDomainAndSign(beaconState, 0, &sszUint, params.BeaconConfig().DomainSelectionProof, privKeys[aggregatorIndex]) - require.NoError(t, err) aggregateAndProof := ðpb.AggregateAttestationAndProof{ - SelectionProof: sig, - Aggregate: att, - AggregatorIndex: aggregatorIndex, + Aggregate: att, } - aggreSig, err := signing.ComputeDomainAndSign(beaconState, 0, aggregateAndProof, params.BeaconConfig().DomainAggregateAndProof, privKeys[aggregatorIndex]) - require.NoError(t, err) require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix()))) @@ -134,7 +125,7 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) { require.NoError(t, err) require.NoError(t, r.cfg.beaconDB.SaveState(context.Background(), s, root)) - r.blkRootToPendingAtts[root] = []ethpb.SignedAggregateAttAndProof{ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof, Signature: aggreSig}} + r.blkRootToPendingAtts[root] = []ethpb.SignedAggregateAttAndProof{ðpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof}} require.NoError(t, r.processPendingAtts(context.Background())) atts, err := r.cfg.attPool.UnaggregatedAttestations() @@ -146,6 +137,80 @@ func TestProcessPendingAtts_HasBlockSaveUnAggregatedAtt(t *testing.T) { cancel() } +func TestProcessPendingAtts_HasBlockSaveUnAggregatedAttElectra(t *testing.T) { + hook := logTest.NewGlobal() + db := dbtest.SetupDB(t) + p1 := p2ptest.NewTestP2P(t) + validators := uint64(256) + + beaconState, privKeys := util.DeterministicGenesisStateElectra(t, validators) + + sb := util.NewBeaconBlockElectra() + util.SaveBlock(t, context.Background(), db, sb) + root, err := sb.Block.HashTreeRoot() + require.NoError(t, err) + + att := ðpb.SingleAttestation{ + Data: ðpb.AttestationData{ + BeaconBlockRoot: root[:], + Source: ðpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)}, + Target: ðpb.Checkpoint{Epoch: 0, Root: root[:]}, + }, + } + aggregateAndProof := ðpb.AggregateAttestationAndProofSingle{ + Aggregate: att, + } + + committee, err := helpers.BeaconCommitteeFromState(context.Background(), beaconState, att.Data.Slot, att.Data.CommitteeIndex) + assert.NoError(t, err) + att.AttesterIndex = committee[0] + attesterDomain, err := signing.Domain(beaconState.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, beaconState.GenesisValidatorsRoot()) + require.NoError(t, err) + hashTreeRoot, err := signing.ComputeSigningRoot(att.Data, attesterDomain) + assert.NoError(t, err) + att.Signature = privKeys[committee[0]].Sign(hashTreeRoot[:]).Marshal() + + require.NoError(t, beaconState.SetGenesisTime(uint64(time.Now().Unix()))) + + chain := &mock.ChainService{Genesis: time.Now(), + State: beaconState, + FinalizedCheckPoint: ðpb.Checkpoint{ + Root: aggregateAndProof.Aggregate.Data.BeaconBlockRoot, + Epoch: 0, + }, + } + ctx, cancel := context.WithCancel(context.Background()) + r := &Service{ + ctx: ctx, + cfg: &config{ + p2p: p1, + beaconDB: db, + chain: chain, + clock: startup.NewClock(chain.Genesis, chain.ValidatorsRoot), + attPool: attestations.NewPool(), + }, + blkRootToPendingAtts: make(map[[32]byte][]ethpb.SignedAggregateAttAndProof), + seenUnAggregatedAttestationCache: lruwrpr.New(10), + signatureChan: make(chan *signatureVerifier, verifierLimit), + } + go r.verifierRoutine() + + s, err := util.NewBeaconStateElectra() + require.NoError(t, err) + require.NoError(t, r.cfg.beaconDB.SaveState(context.Background(), s, root)) + + r.blkRootToPendingAtts[root] = []ethpb.SignedAggregateAttAndProof{ðpb.SignedAggregateAttestationAndProofSingle{Message: aggregateAndProof}} + require.NoError(t, r.processPendingAtts(context.Background())) + + atts, err := r.cfg.attPool.UnaggregatedAttestations() + require.NoError(t, err) + require.Equal(t, 1, len(atts), "Did not save unaggregated att") + assert.DeepEqual(t, att.ToAttestationElectra(committee), atts[0], "Incorrect saved att") + assert.Equal(t, 0, len(r.cfg.attPool.AggregatedAttestations()), "Did save aggregated att") + require.LogsContain(t, hook, "Verified and saved pending attestations to pool") + cancel() +} + func TestProcessPendingAtts_NoBroadcastWithBadSignature(t *testing.T) { db := dbtest.SetupDB(t) p1 := p2ptest.NewTestP2P(t) diff --git a/beacon-chain/sync/validate_aggregate_proof_test.go b/beacon-chain/sync/validate_aggregate_proof_test.go index 6376e3f42d6..e07366a273e 100644 --- a/beacon-chain/sync/validate_aggregate_proof_test.go +++ b/beacon-chain/sync/validate_aggregate_proof_test.go @@ -94,15 +94,8 @@ func TestVerifyIndexInCommittee_ExistsInBeaconCommittee(t *testing.T) { assert.ErrorContains(t, wanted, err) assert.Equal(t, pubsub.ValidationReject, result) - att.AggregationBits = bitfield.NewBitlist(1) - committeeIndex, err := att.GetCommitteeIndex() - require.NoError(t, err) - _, result, err = service.validateBitLength(ctx, s, att.Data.Slot, committeeIndex, att.AggregationBits) - require.ErrorContains(t, "wanted participants bitfield length 4, got: 1", err) - assert.Equal(t, pubsub.ValidationReject, result) - att.Data.CommitteeIndex = 10000 - _, _, result, err = service.validateCommitteeIndexAndCount(ctx, att, s) + _, _, result, err = service.validateCommitteeIndex(ctx, att, s) require.ErrorContains(t, "committee index 10000 > 2", err) assert.Equal(t, pubsub.ValidationReject, result) } diff --git a/beacon-chain/sync/validate_beacon_attestation.go b/beacon-chain/sync/validate_beacon_attestation.go index c65ecaec78b..f7dc4c6c4fc 100644 --- a/beacon-chain/sync/validate_beacon_attestation.go +++ b/beacon-chain/sync/validate_beacon_attestation.go @@ -241,6 +241,7 @@ func (s *Service) validateCommitteeIndex( a eth.Att, bs state.ReadOnlyBeaconState, ) (primitives.CommitteeIndex, uint64, pubsub.ValidationResult, error) { + // - [REJECT] attestation.data.index == 0 if a.Version() >= version.Electra && a.GetData().CommitteeIndex != 0 { return 0, 0, pubsub.ValidationReject, errors.New("attestation data's committee index must be 0") } @@ -261,8 +262,6 @@ func validateAttesterData( a eth.Att, committee []primitives.ValidatorIndex, ) (pubsub.ValidationResult, error) { - // TODO: add test - // - [REJECT] attestation.data.index == 0 if a.Version() >= version.Electra { singleAtt, ok := a.(*eth.SingleAttestation) if !ok { @@ -270,7 +269,6 @@ func validateAttesterData( } result, err := validateAttestingIndex(ctx, singleAtt.AttesterIndex, committee) if result != pubsub.ValidationAccept { - log.Info("validateAttestingIndex") return result, err } } else { diff --git a/beacon-chain/sync/validate_beacon_attestation_test.go b/beacon-chain/sync/validate_beacon_attestation_test.go index 2451352fb42..f43566259f1 100644 --- a/beacon-chain/sync/validate_beacon_attestation_test.go +++ b/beacon-chain/sync/validate_beacon_attestation_test.go @@ -20,6 +20,7 @@ import ( lruwrpr "github.com/prysmaticlabs/prysm/v5/cache/lru" fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams" "github.com/prysmaticlabs/prysm/v5/config/params" + "github.com/prysmaticlabs/prysm/v5/consensus-types/primitives" "github.com/prysmaticlabs/prysm/v5/encoding/bytesutil" ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1" "github.com/prysmaticlabs/prysm/v5/testing/require" @@ -81,7 +82,7 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { tests := []struct { name string - msg *ethpb.Attestation + msg ethpb.Att topic string validAttestationSignature bool want bool @@ -262,20 +263,20 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { helpers.ClearCache() chain.ValidAttestation = tt.validAttestationSignature if tt.validAttestationSignature { - com, err := helpers.BeaconCommitteeFromState(context.Background(), savedState, tt.msg.Data.Slot, tt.msg.Data.CommitteeIndex) + com, err := helpers.BeaconCommitteeFromState(context.Background(), savedState, tt.msg.GetData().Slot, tt.msg.GetData().CommitteeIndex) require.NoError(t, err) - domain, err := signing.Domain(savedState.Fork(), tt.msg.Data.Target.Epoch, params.BeaconConfig().DomainBeaconAttester, savedState.GenesisValidatorsRoot()) + domain, err := signing.Domain(savedState.Fork(), tt.msg.GetData().Target.Epoch, params.BeaconConfig().DomainBeaconAttester, savedState.GenesisValidatorsRoot()) require.NoError(t, err) - attRoot, err := signing.ComputeSigningRoot(tt.msg.Data, domain) + attRoot, err := signing.ComputeSigningRoot(tt.msg.GetData(), domain) require.NoError(t, err) for i := 0; ; i++ { - if tt.msg.AggregationBits.BitAt(uint64(i)) { - tt.msg.Signature = keys[com[i]].Sign(attRoot[:]).Marshal() + if tt.msg.GetAggregationBits().BitAt(uint64(i)) { + tt.msg.SetSignature(keys[com[i]].Sign(attRoot[:]).Marshal()) break } } } else { - tt.msg.Signature = make([]byte, 96) + tt.msg.SetSignature(make([]byte, 96)) } buf := new(bytes.Buffer) _, err := p.Encoding().EncodeGossip(buf, tt.msg) @@ -305,6 +306,164 @@ func TestService_validateCommitteeIndexBeaconAttestation(t *testing.T) { } } +func TestService_validateCommitteeIndexBeaconAttestationElectra(t *testing.T) { + params.SetupTestConfigCleanup(t) + cfg := params.BeaconConfig() + fvs := map[[fieldparams.VersionLength]byte]primitives.Epoch{} + fvs[bytesutil.ToBytes4(cfg.GenesisForkVersion)] = 1 + fvs[bytesutil.ToBytes4(cfg.AltairForkVersion)] = 2 + fvs[bytesutil.ToBytes4(cfg.BellatrixForkVersion)] = 3 + fvs[bytesutil.ToBytes4(cfg.CapellaForkVersion)] = 4 + fvs[bytesutil.ToBytes4(cfg.DenebForkVersion)] = 5 + fvs[bytesutil.ToBytes4(cfg.ElectraForkVersion)] = 0 + cfg.ForkVersionSchedule = fvs + params.OverrideBeaconConfig(cfg) + + p := p2ptest.NewTestP2P(t) + db := dbtest.SetupDB(t) + chain := &mockChain.ChainService{ + // 1 slot ago. + Genesis: time.Now().Add(time.Duration(-1*int64(params.BeaconConfig().SecondsPerSlot)) * time.Second), + ValidatorsRoot: [32]byte{'A'}, + ValidAttestation: true, + DB: db, + Optimistic: true, + } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + s := &Service{ + ctx: ctx, + cfg: &config{ + initialSync: &mockSync.Sync{IsSyncing: false}, + p2p: p, + beaconDB: db, + chain: chain, + clock: startup.NewClock(chain.Genesis, chain.ValidatorsRoot), + attestationNotifier: (&mockChain.ChainService{}).OperationNotifier(), + }, + blkRootToPendingAtts: make(map[[32]byte][]ethpb.SignedAggregateAttAndProof), + seenUnAggregatedAttestationCache: lruwrpr.New(10), + signatureChan: make(chan *signatureVerifier, verifierLimit), + } + s.initCaches() + go s.verifierRoutine() + + digest, err := s.currentForkDigest() + require.NoError(t, err) + + blk := util.NewBeaconBlock() + blk.Block.Slot = 1 + util.SaveBlock(t, ctx, db, blk) + + validBlockRoot, err := blk.Block.HashTreeRoot() + require.NoError(t, err) + chain.FinalizedCheckPoint = ðpb.Checkpoint{ + Root: validBlockRoot[:], + Epoch: 0, + } + + validators := uint64(64) + savedState, keys := util.DeterministicGenesisState(t, validators) + require.NoError(t, savedState.SetSlot(1)) + require.NoError(t, db.SaveState(context.Background(), savedState, validBlockRoot)) + chain.State = savedState + committee, err := helpers.BeaconCommitteeFromState(ctx, savedState, 1, 0) + require.NoError(t, err) + + tests := []struct { + name string + msg ethpb.Att + want bool + }{ + { + name: "valid", + msg: ðpb.SingleAttestation{ + Data: ðpb.AttestationData{ + BeaconBlockRoot: validBlockRoot[:], + CommitteeIndex: 0, + Slot: 1, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: validBlockRoot[:], + }, + Source: ðpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)}, + }, + AttesterIndex: committee[0], + }, + want: true, + }, + { + name: "non-zero committee index in att data", + msg: ðpb.SingleAttestation{ + Data: ðpb.AttestationData{ + BeaconBlockRoot: validBlockRoot[:], + CommitteeIndex: 1, + Slot: 1, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: validBlockRoot[:], + }, + Source: ðpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)}, + }, + AttesterIndex: committee[0], + }, + want: false, + }, + { + name: "attesting index not in committee", + msg: ðpb.SingleAttestation{ + Data: ðpb.AttestationData{ + BeaconBlockRoot: validBlockRoot[:], + CommitteeIndex: 1, + Slot: 1, + Target: ðpb.Checkpoint{ + Epoch: 0, + Root: validBlockRoot[:], + }, + Source: ðpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)}, + }, + AttesterIndex: 999999, + }, + want: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + helpers.ClearCache() + com, err := helpers.BeaconCommitteeFromState(context.Background(), savedState, tt.msg.GetData().Slot, tt.msg.GetData().CommitteeIndex) + require.NoError(t, err) + domain, err := signing.Domain(savedState.Fork(), tt.msg.GetData().Target.Epoch, params.BeaconConfig().DomainBeaconAttester, savedState.GenesisValidatorsRoot()) + require.NoError(t, err) + attRoot, err := signing.ComputeSigningRoot(tt.msg.GetData(), domain) + require.NoError(t, err) + tt.msg.SetSignature(keys[com[0]].Sign(attRoot[:]).Marshal()) + buf := new(bytes.Buffer) + _, err = p.Encoding().EncodeGossip(buf, tt.msg) + require.NoError(t, err) + topic := fmt.Sprintf("/eth2/%x/beacon_attestation_1", digest) + m := &pubsub.Message{ + Message: &pubsubpb.Message{ + Data: buf.Bytes(), + Topic: &topic, + }, + } + + res, err := s.validateCommitteeIndexBeaconAttestation(ctx, "", m) + received := res == pubsub.ValidationAccept + if received != tt.want { + t.Fatalf("Did not received wanted validation. Got %v, wanted %v", !tt.want, tt.want) + } + if tt.want && err != nil { + t.Errorf("Non nil error returned: %v", err) + } + if tt.want && m.ValidatorData == nil { + t.Error("Expected validator data to be set") + } + }) + } +} + func TestService_setSeenCommitteeIndicesSlot(t *testing.T) { s := NewService(context.Background(), WithP2P(p2ptest.NewTestP2P(t))) s.initCaches()