Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rkapka committed Jan 2, 2025
1 parent 9345e66 commit bd82335
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 29 deletions.
7 changes: 7 additions & 0 deletions beacon-chain/core/helpers/beacon_committee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
87 changes: 76 additions & 11 deletions beacon-chain/sync/pending_attestations_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 := &ethpb.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())))

Expand Down Expand Up @@ -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{&ethpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof, Signature: aggreSig}}
r.blkRootToPendingAtts[root] = []ethpb.SignedAggregateAttAndProof{&ethpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof}}
require.NoError(t, r.processPendingAtts(context.Background()))

atts, err := r.cfg.attPool.UnaggregatedAttestations()
Expand All @@ -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 := &ethpb.SingleAttestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: root[:],
Source: &ethpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
Target: &ethpb.Checkpoint{Epoch: 0, Root: root[:]},
},
}
aggregateAndProof := &ethpb.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: &ethpb.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{&ethpb.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)
Expand Down
9 changes: 1 addition & 8 deletions beacon-chain/sync/validate_aggregate_proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 1 addition & 3 deletions beacon-chain/sync/validate_beacon_attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -261,16 +262,13 @@ 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 {
return pubsub.ValidationIgnore, fmt.Errorf("attestation has wrong type (expected %T, got %T)", &eth.SingleAttestation{}, a)
}
result, err := validateAttestingIndex(ctx, singleAtt.AttesterIndex, committee)
if result != pubsub.ValidationAccept {
log.Info("validateAttestingIndex")
return result, err
}
} else {
Expand Down
173 changes: 166 additions & 7 deletions beacon-chain/sync/validate_beacon_attestation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = &ethpb.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: &ethpb.SingleAttestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: validBlockRoot[:],
CommitteeIndex: 0,
Slot: 1,
Target: &ethpb.Checkpoint{
Epoch: 0,
Root: validBlockRoot[:],
},
Source: &ethpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)},
},
AttesterIndex: committee[0],
},
want: true,
},
{
name: "non-zero committee index in att data",
msg: &ethpb.SingleAttestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: validBlockRoot[:],
CommitteeIndex: 1,
Slot: 1,
Target: &ethpb.Checkpoint{
Epoch: 0,
Root: validBlockRoot[:],
},
Source: &ethpb.Checkpoint{Root: make([]byte, fieldparams.RootLength)},
},
AttesterIndex: committee[0],
},
want: false,
},
{
name: "attesting index not in committee",
msg: &ethpb.SingleAttestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: validBlockRoot[:],
CommitteeIndex: 1,
Slot: 1,
Target: &ethpb.Checkpoint{
Epoch: 0,
Root: validBlockRoot[:],
},
Source: &ethpb.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()
Expand Down

0 comments on commit bd82335

Please sign in to comment.