Benchmark Process Block with Attestations (#2758)

This commit is contained in:
terence tsao
2019-06-06 07:32:32 -04:00
committed by nisdas
parent 4a0b3710ef
commit 32975baa3c
2 changed files with 164 additions and 39 deletions

View File

@@ -40,6 +40,7 @@ go_test(
"//shared/hashutil:go_default_library",
"//shared/params:go_default_library",
"//shared/ssz:go_default_library",
"//shared/trieutil:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)

View File

@@ -13,8 +13,10 @@ import (
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bls"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/hashutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/ssz"
"github.com/prysmaticlabs/prysm/shared/trieutil"
"github.com/sirupsen/logrus"
)
@@ -612,36 +614,31 @@ func BenchmarkProcessEpoch65536Validators(b *testing.B) {
}
}
func BenchmarkProcessBlock_65536Validators(b *testing.B) {
func BenchmarkProcessBlk_65536Validators_FullBlock(b *testing.B) {
logrus.SetLevel(logrus.PanicLevel)
helpers.ClearAllCaches()
epoch := uint64(1)
testConfig := params.BeaconConfig()
testConfig.MaxTransfers = 1
validatorCount := params.BeaconConfig().DepositsForChainStart * 4
shardCount := validatorCount / params.BeaconConfig().TargetCommitteeSize
validators := make([]*pb.Validator, validatorCount)
balances := make([]uint64, validatorCount)
for i := 0; i < len(validators); i++ {
validators[i] = &pb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
EffectiveBalance: params.BeaconConfig().MaxDepositAmount,
EffectiveBalance: params.BeaconConfig().MaxDepositAmount,
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
WithdrawableEpoch: params.BeaconConfig().FarFutureEpoch,
ActivationEligibilityEpoch: params.BeaconConfig().FarFutureEpoch,
}
balances[i] = params.BeaconConfig().MaxDepositAmount
}
validatorBalances := make([]uint64, len(validators))
for i := 0; i < len(validatorBalances); i++ {
validatorBalances[i] = params.BeaconConfig().MaxDepositAmount
}
var atts []*pb.PendingAttestation
for i := uint64(0); i < shardCount; i++ {
atts = append(atts, &pb.PendingAttestation{
Data: &pb.AttestationData{
Crosslink: &pb.Crosslink{
Shard: i,
},
},
AggregationBitfield: []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
InclusionDelay: 1,
})
randaoMixes := make([][]byte, params.BeaconConfig().LatestRandaoMixesLength)
for i := 0; i < len(randaoMixes); i++ {
randaoMixes[i] = params.BeaconConfig().ZeroHash[:]
}
var crosslinks []*pb.Crosslink
@@ -652,46 +649,173 @@ func BenchmarkProcessBlock_65536Validators(b *testing.B) {
})
}
randaoMixes := make([][]byte, params.BeaconConfig().LatestRandaoMixesLength)
for i := 0; i < len(randaoMixes); i++ {
randaoMixes[i] = params.BeaconConfig().ZeroHash[:]
}
s := &pb.BeaconState{
Slot: epoch*params.BeaconConfig().SlotsPerEpoch + 1,
ValidatorRegistry: validators,
Balances: balances,
LatestStartShard: 512,
LatestBlockRoots: make([][]byte, 254),
LatestSlashedBalances: []uint64{0, 1e9, 0},
LatestRandaoMixes: randaoMixes,
LatestActiveIndexRoots: make([][]byte, params.BeaconConfig().LatestActiveIndexRootsLength),
CurrentCrosslinks: crosslinks,
PreviousEpochAttestations: atts,
Slot: 20,
LatestBlockRoots: make([][]byte, 254),
LatestRandaoMixes: randaoMixes,
ValidatorRegistry: validators,
Balances: validatorBalances,
LatestSlashedBalances: make([]uint64, params.BeaconConfig().LatestSlashedExitLength),
LatestActiveIndexRoots: make([][]byte, params.BeaconConfig().LatestActiveIndexRootsLength),
CurrentJustifiedRoot: []byte("tron-sucks"),
Fork: &pb.Fork{
PreviousVersion: []byte{0, 0, 0, 0},
CurrentVersion: []byte{0, 0, 0, 0},
},
CurrentCrosslinks: crosslinks,
}
c := &state.TransitionConfig{
VerifySignatures: false, // We disable signature verification for now.
VerifySignatures: true,
Logging: false, // We enable logging in this state transition call.
}
// Set up proposer slashing object for block
proposerSlashings := []*pb.ProposerSlashing{
{
ProposerIndex: 1,
Header_1: &pb.BeaconBlockHeader{
Slot: 0,
Signature: []byte("A"),
},
Header_2: &pb.BeaconBlockHeader{
Slot: 0,
Signature: []byte("B"),
},
},
}
// Set up attester slashing object for block
attesterSlashings := []*pb.AttesterSlashing{
{
Attestation_1: &pb.IndexedAttestation{
Data: &pb.AttestationData{
Crosslink: &pb.Crosslink{
Shard: 5,
},
},
CustodyBit_0Indices: []uint64{2, 3},
},
Attestation_2: &pb.IndexedAttestation{
Data: &pb.AttestationData{
Crosslink: &pb.Crosslink{
Shard: 5,
},
},
CustodyBit_0Indices: []uint64{2, 3},
},
},
}
// Set up deposit object for block
deposit := &pb.Deposit{
Data: &pb.DepositData{
Pubkey: []byte{1, 2, 3},
Amount: params.BeaconConfig().MaxDepositAmount,
},
}
leaf, err := ssz.TreeHash(deposit.Data)
if err != nil {
b.Fatal(err)
}
depositTrie, err := trieutil.GenerateTrieFromItems([][]byte{leaf[:]}, int(params.BeaconConfig().DepositContractTreeDepth))
if err != nil {
b.Fatalf("Could not generate trie: %v", err)
}
proof, err := depositTrie.MerkleProof(0)
if err != nil {
b.Fatalf("Could not generate proof: %v", err)
}
deposit.Proof = proof
deposit.Index = 0
root := depositTrie.Root()
// Set up randao reveal object for block
proposerIdx, err := helpers.BeaconProposerIndex(s)
if err != nil {
b.Fatal(err)
}
priv, err := bls.RandKey(rand.Reader)
if err != nil {
b.Fatal(err)
}
s.ValidatorRegistry[proposerIdx].Pubkey = priv.PublicKey().Marshal()
buf := make([]byte, 32)
binary.LittleEndian.PutUint64(buf, 0)
domain := helpers.DomainVersion(s, 0, params.BeaconConfig().DomainRandao)
epochSignature := priv.Sign(buf, domain)
// Set up transfer object for block
transfers := []*pb.Transfer{
{
Slot: s.Slot,
Sender: 3,
Recipient: 4,
Fee: params.BeaconConfig().MinDepositAmount,
Amount: params.BeaconConfig().MinDepositAmount,
Pubkey: []byte("A"),
},
}
buf = []byte{params.BeaconConfig().BLSWithdrawalPrefixByte}
pubKey := []byte("A")
hashed := hashutil.Hash(pubKey)
buf = append(buf, hashed[:]...)
s.ValidatorRegistry[3].WithdrawalCredentials = buf
// Set up attestations obj for block.
encoded, err := ssz.TreeHash(s.CurrentCrosslinks[0])
if err != nil {
b.Fatal(err)
}
attestations := make([]*pb.Attestation, 128)
for i := 0; i < len(attestations); i++ {
attestations[i] = &pb.Attestation{
Data: &pb.AttestationData{
SourceRoot: []byte("tron-sucks"),
Crosslink: &pb.Crosslink{
Shard: uint64(i),
ParentRoot: encoded[:],
DataRoot: params.BeaconConfig().ZeroHash[:],
},
},
AggregationBitfield: []byte{0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0,
0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0},
CustodyBitfield: []byte{},
}
}
blk := &pb.BeaconBlock{
Slot: s.Slot + 1,
Body: &pb.BeaconBlockBody{
Eth1Data: &pb.Eth1Data{
DepositRoot: []byte("a"),
BlockRoot: []byte("b"),
DepositRoot: root[:],
BlockRoot: root[:],
},
RandaoReveal: []byte{},
Attestations: nil,
RandaoReveal: epochSignature.Marshal(),
Attestations: attestations,
ProposerSlashings: proposerSlashings,
AttesterSlashings: attesterSlashings,
Transfers: transfers,
},
}
// Precache the shuffled indices
for i := uint64(0); i < shardCount; i++ {
if _, err := helpers.CrosslinkCommitteeAtEpoch(s, 0, i); err != nil {
b.Fatal(err)
}
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_, err := state.ProcessBlock(context.Background(), s, blk, c)
if err != nil {
b.Fatal(err)
}
// Reset state fields to process block again
s.ValidatorRegistry[1].Slashed = false
s.ValidatorRegistry[2].Slashed = false
s.Balances[3] += 2 * params.BeaconConfig().MinDepositAmount
}
}