Skip to content

Instantly share code, notes, and snippets.

@terencechain
Created November 6, 2020 23:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terencechain/412ef767ff37562a2735237c7ffc8b71 to your computer and use it in GitHub Desktop.
Save terencechain/412ef767ff37562a2735237c7ffc8b71 to your computer and use it in GitHub Desktop.
func BenchmarkValidateAggregateAndProof(b *testing.B) {
db, _ := dbtest.SetupDB(b)
var testing *testing.T
p := p2ptest.NewTestP2P(testing)
validators := uint64(256)
beaconState, privKeys := testutil.DeterministicGenesisState(b, validators)
blk := testutil.NewBeaconBlock()
require.NoError(b, db.SaveBlock(context.Background(), blk))
root, err := blk.Block.HashTreeRoot()
require.NoError(b, err)
s := testutil.NewBeaconState()
require.NoError(b, db.SaveState(context.Background(), s, root))
aggBits := bitfield.NewBitlist(3)
aggBits.SetBitAt(0, true)
att := &ethpb.Attestation{
Data: &ethpb.AttestationData{
BeaconBlockRoot: root[:],
Source: &ethpb.Checkpoint{Epoch: 0, Root: bytesutil.PadTo([]byte("hello-world"), 32)},
Target: &ethpb.Checkpoint{Epoch: 0, Root: root[:]},
},
AggregationBits: aggBits,
}
committee, err := helpers.BeaconCommitteeFromState(beaconState, att.Data.Slot, att.Data.CommitteeIndex)
assert.NoError(b, err)
attestingIndices := attestationutil.AttestingIndices(att.AggregationBits, committee)
assert.NoError(b, err)
attesterDomain, err := helpers.Domain(beaconState.Fork(), 0, params.BeaconConfig().DomainBeaconAttester, beaconState.GenesisValidatorRoot())
assert.NoError(b, err)
hashTreeRoot, err := helpers.ComputeSigningRoot(att.Data, attesterDomain)
assert.NoError(b, err)
sigs := make([]bls.Signature, len(attestingIndices))
for i, indice := range attestingIndices {
sig := privKeys[indice].Sign(hashTreeRoot[:])
sigs[i] = sig
}
att.Signature = bls.AggregateSignatures(sigs).Marshal()
ai := committee[0]
sig, err := helpers.ComputeDomainAndSign(beaconState, 0, att.Data.Slot, params.BeaconConfig().DomainSelectionProof, privKeys[ai])
require.NoError(b, err)
aggregateAndProof := &ethpb.AggregateAttestationAndProof{
SelectionProof: sig,
Aggregate: att,
AggregatorIndex: ai,
}
signedAggregateAndProof := &ethpb.SignedAggregateAttestationAndProof{Message: aggregateAndProof}
signedAggregateAndProof.Signature, err = helpers.ComputeDomainAndSign(beaconState, 0, signedAggregateAndProof.Message, params.BeaconConfig().DomainAggregateAndProof, privKeys[ai])
require.NoError(b, err)
require.NoError(b, beaconState.SetGenesisTime(uint64(time.Now().Unix())))
c, err := lru.New(10)
require.NoError(b, err)
r := &Service{
p2p: p,
db: db,
initialSync: &mockSync.Sync{IsSyncing: false},
chain: &mock.ChainService{Genesis: time.Now(),
State: beaconState,
ValidAttestation: true,
FinalizedCheckPoint: &ethpb.Checkpoint{
Epoch: 0,
Root: att.Data.BeaconBlockRoot,
}},
attPool: attestations.NewPool(),
seenAttestationCache: c,
stateSummaryCache: cache.NewStateSummaryCache(),
}
err = r.initCaches()
b.ResetTimer()
for i := 0; i < b.N; i++ {
require.Equal(b, pubsub.ValidationAccept, r.validateAggregatedAtt(context.Background(), signedAggregateAndProof))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment