diff --git a/node/Cargo.toml b/node/Cargo.toml index 0abfdbfd53..33a7c7fc9b 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -41,9 +41,15 @@ node-data = { version = "0.1", path = "../node-data", features = ["faker"] } rand = "0.8" rand_core = "0.6" tempdir = "0.3" +criterion = { version = "0.5", features = ["async_futures"] } [build-dependencies] rustc_tools_util = "=0.2.0" [features] with_telemetry = ["dep:console-subscriber"] + + +[[bench]] +name = "accept" +harness = false diff --git a/node/benches/accept.rs b/node/benches/accept.rs new file mode 100644 index 0000000000..c1fb165e22 --- /dev/null +++ b/node/benches/accept.rs @@ -0,0 +1,122 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. +// +// Copyright (c) DUSK NETWORK. All rights reserved. + +use node::chain; + +use criterion::async_executor::FuturesExecutor; +use criterion::{criterion_group, criterion_main, Criterion}; + +use dusk_bls12_381_sign::{ + PublicKey as BlsPublicKey, SecretKey as BlsSecretKey, + Signature as BlsSignature, +}; +use dusk_bytes::Serializable; +use dusk_consensus::user::{ + cluster::Cluster, committee::Committee, provisioners::Provisioners, + sortition::Config as SortitionConfig, +}; +use node_data::{ + bls::PublicKey, + ledger::{Certificate, Signature, StepVotes}, + message, +}; +use rand::rngs::StdRng; +use rand::SeedableRng; + +fn create_step_votes( + seed: Signature, + round: u64, + block_hash: [u8; 32], + step: u8, + iteration: u8, + provisioners: Provisioners, + keys: &[(PublicKey, BlsSecretKey)], +) -> StepVotes { + let sortition_config = + SortitionConfig::new(seed, round, iteration * 3 + step, 64); + + let committee = Committee::new( + PublicKey::default(), + &mut provisioners.clone(), + sortition_config, + ); + + let hdr = message::Header { + round, + step: step % 3, + block_hash, + ..Default::default() + }; + let mut signatures = vec![]; + let mut cluster = Cluster::::default(); + for (pk, sk) in keys.iter() { + if let Some(weight) = committee.votes_for(pk) { + let sig = hdr.sign(sk, pk.inner()); + signatures.push(BlsSignature::from_bytes(&sig).unwrap()); + cluster.set_weight(pk, weight); + } + } + + let bitset = committee.bits(&cluster); + + let (first, rest) = signatures.split_first().unwrap(); + let aggregate_signature = first.aggregate(rest).to_bytes(); + StepVotes::new(aggregate_signature, bitset) +} + +pub fn verify_block_cert(c: &mut Criterion) { + let mut keys = vec![]; + let mut provisioners = Provisioners::new(); + let rng = &mut StdRng::seed_from_u64(0xbeef); + for _ in 0..1000 { + let sk = BlsSecretKey::random(rng); + let pk = BlsPublicKey::from(&sk); + let pk = PublicKey::new(pk); + keys.push((pk.clone(), sk)); + provisioners.add_member_with_value(pk, 1000000000000) + } + let height = 1; + let seed = Signature([5; 48]); + let block_hash = [1; 32]; + let curr_public_key = keys.first().unwrap().0.clone(); + let iteration = 0; + let mut cert = Certificate::default(); + + cert.first_reduction = create_step_votes( + seed, + height, + block_hash, + 1, + iteration, + provisioners.clone(), + &keys[..], + ); + cert.second_reduction = create_step_votes( + seed, + height, + block_hash, + 2, + iteration, + provisioners.clone(), + &keys[..], + ); + c.bench_function("verify_block_cert", |f| { + f.to_async(FuturesExecutor).iter(|| { + chain::verify_block_cert( + seed, + &provisioners, + &curr_public_key, + block_hash, + height, + &cert, + iteration, + ) + }) + }); +} + +criterion_group!(benches, verify_block_cert); +criterion_main!(benches); diff --git a/node/src/chain.rs b/node/src/chain.rs index e8d376657e..e443916936 100644 --- a/node/src/chain.rs +++ b/node/src/chain.rs @@ -43,6 +43,8 @@ use self::acceptor::{Acceptor, RevertTarget}; use self::consensus::Task; use self::fsm::SimpleFSM; +pub use acceptor::verify_block_cert; + const TOPICS: &[u8] = &[ Topics::Block as u8, Topics::NewBlock as u8, diff --git a/node/src/chain/acceptor.rs b/node/src/chain/acceptor.rs index 3a65769500..d224433d5e 100644 --- a/node/src/chain/acceptor.rs +++ b/node/src/chain/acceptor.rs @@ -531,7 +531,7 @@ pub(crate) async fn verify_block_header( .await } -async fn verify_block_cert( +pub async fn verify_block_cert( curr_seed: Signature, curr_eligible_provisioners: &Provisioners, curr_public_key: &node_data::bls::PublicKey,