diff --git a/crates/benches/Cargo.toml b/crates/benches/Cargo.toml index 3fde0768d6..6fff64480d 100644 --- a/crates/benches/Cargo.toml +++ b/crates/benches/Cargo.toml @@ -17,11 +17,6 @@ name = "allowed_txs" harness = false path = "txs.rs" -[[bench]] -name = "allowed_vps" -harness = false -path = "vps.rs" - [[bench]] name = "native_vps" harness = false diff --git a/crates/benches/txs.rs b/crates/benches/txs.rs index 9bd696e125..d5f55fe985 100644 --- a/crates/benches/txs.rs +++ b/crates/benches/txs.rs @@ -1,22 +1,8 @@ use std::str::FromStr; use criterion::{criterion_group, criterion_main, Criterion}; -use namada::account::{InitAccount, UpdateAccount}; -use namada::core::address::{self, Address}; -use namada::core::collections::HashMap; -use namada::core::eth_bridge_pool::{GasFee, PendingTransfer}; -use namada::core::hash::Hash; -use namada::core::key::{ - common, ed25519, secp256k1, PublicKey, RefTo, - SecretKey as SecretKeyInterface, SigScheme, -}; +use namada::core::address::Address; use namada::core::masp::{TransferSource, TransferTarget}; -use namada::core::storage::Key; -use namada::eth_bridge::storage::eth_bridge_queries::is_bridge_comptime_enabled; -use namada::governance::pgf::storage::steward::StewardDetail; -use namada::governance::storage::proposal::ProposalType; -use namada::governance::storage::vote::ProposalVote; -use namada::governance::{InitProposalData, VoteProposalData}; use namada::ibc::core::channel::types::channel::Order; use namada::ibc::core::channel::types::msgs::MsgChannelOpenInit; use namada::ibc::core::channel::types::Version as ChannelVersion; @@ -28,715 +14,12 @@ use namada::ibc::core::host::types::identifiers::{ ClientId, ConnectionId, PortId, }; use namada::ibc::primitives::ToProto; -use namada::ledger::eth_bridge::read_native_erc20_address; -use namada::proof_of_stake::storage::read_pos_params; -use namada::proof_of_stake::types::SlashType; -use namada::proof_of_stake::{self, KeySeg}; -use namada::state::{StorageRead, StorageWrite}; +use namada::proof_of_stake::KeySeg; use namada::token::Amount; -use namada::tx::data::pos::{ - BecomeValidator, Bond, CommissionChange, ConsensusKeyChange, - MetaDataChange, Redelegation, Withdraw, -}; -use namada::tx::{Code, Section}; use namada_apps::bench_utils::{ - BenchShell, BenchShieldedCtx, ALBERT_PAYMENT_ADDRESS, ALBERT_SPENDING_KEY, - BERTHA_PAYMENT_ADDRESS, TX_BECOME_VALIDATOR_WASM, TX_BOND_WASM, - TX_BRIDGE_POOL_WASM, TX_CHANGE_CONSENSUS_KEY_WASM, - TX_CHANGE_VALIDATOR_COMMISSION_WASM, TX_CHANGE_VALIDATOR_METADATA_WASM, - TX_CLAIM_REWARDS_WASM, TX_DEACTIVATE_VALIDATOR_WASM, TX_IBC_WASM, - TX_INIT_ACCOUNT_WASM, TX_INIT_PROPOSAL_WASM, TX_REACTIVATE_VALIDATOR_WASM, - TX_REDELEGATE_WASM, TX_RESIGN_STEWARD, TX_REVEAL_PK_WASM, TX_UNBOND_WASM, - TX_UNJAIL_VALIDATOR_WASM, TX_UPDATE_ACCOUNT_WASM, - TX_UPDATE_STEWARD_COMMISSION, TX_VOTE_PROPOSAL_WASM, TX_WITHDRAW_WASM, - VP_USER_WASM, + BenchShieldedCtx, ALBERT_PAYMENT_ADDRESS, ALBERT_SPENDING_KEY, TX_IBC_WASM, }; use namada_apps::wallet::defaults; -use sha2::Digest; - -fn transfer(c: &mut Criterion) { - let mut group = c.benchmark_group("transfer"); - let amount = Amount::native_whole(500); - - for bench_name in ["transparent", "shielding", "unshielding", "shielded"] { - group.bench_function(bench_name, |b| { - b.iter_batched_ref( - || { - let mut shielded_ctx = BenchShieldedCtx::default(); - - let albert_spending_key = shielded_ctx - .wallet - .find_spending_key(ALBERT_SPENDING_KEY, None) - .unwrap() - .to_owned(); - let albert_payment_addr = shielded_ctx - .wallet - .find_payment_addr(ALBERT_PAYMENT_ADDRESS) - .unwrap() - .to_owned(); - let bertha_payment_addr = shielded_ctx - .wallet - .find_payment_addr(BERTHA_PAYMENT_ADDRESS) - .unwrap() - .to_owned(); - - // Shield some tokens for Albert - let (mut shielded_ctx, shield_tx) = shielded_ctx - .generate_masp_tx( - amount, - TransferSource::Address(defaults::albert_address()), - TransferTarget::PaymentAddress(albert_payment_addr), - ); - shielded_ctx.shell.execute_tx(&shield_tx); - shielded_ctx.shell.commit_masp_tx(shield_tx); - shielded_ctx.shell.commit_block(); - - let (shielded_ctx, signed_tx) = match bench_name { - "transparent" => shielded_ctx.generate_masp_tx( - amount, - TransferSource::Address(defaults::albert_address()), - TransferTarget::Address(defaults::bertha_address()), - ), - "shielding" => shielded_ctx.generate_masp_tx( - amount, - TransferSource::Address(defaults::albert_address()), - TransferTarget::PaymentAddress(albert_payment_addr), - ), - "unshielding" => shielded_ctx.generate_masp_tx( - amount, - TransferSource::ExtendedSpendingKey( - albert_spending_key, - ), - TransferTarget::Address(defaults::albert_address()), - ), - "shielded" => shielded_ctx.generate_masp_tx( - amount, - TransferSource::ExtendedSpendingKey( - albert_spending_key, - ), - TransferTarget::PaymentAddress(bertha_payment_addr), - ), - _ => panic!("Unexpected bench test"), - }; - - (shielded_ctx, signed_tx) - }, - |(shielded_ctx, signed_tx)| { - shielded_ctx.shell.execute_tx(signed_tx); - }, - criterion::BatchSize::SmallInput, - ) - }); - } - - group.finish(); -} - -fn bond(c: &mut Criterion) { - let mut group = c.benchmark_group("bond"); - - let shell = BenchShell::default(); - let bond = shell.generate_tx( - TX_BOND_WASM, - Bond { - validator: defaults::validator_address(), - amount: Amount::native_whole(1000), - source: Some(defaults::albert_address()), - }, - None, - None, - vec![&defaults::albert_keypair()], - ); - - let self_bond = shell.generate_tx( - TX_BOND_WASM, - Bond { - validator: defaults::validator_address(), - amount: Amount::native_whole(1000), - source: None, - }, - None, - None, - vec![&defaults::validator_keypair()], - ); - - for (signed_tx, bench_name) in - [bond, self_bond].iter().zip(["bond", "self_bond"]) - { - group.bench_function(bench_name, |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(signed_tx), - criterion::BatchSize::SmallInput, - ) - }); - } - - group.finish(); -} - -fn unbond(c: &mut Criterion) { - let mut group = c.benchmark_group("unbond"); - - let shell = BenchShell::default(); - let unbond = shell.generate_tx( - TX_UNBOND_WASM, - Bond { - validator: defaults::validator_address(), - amount: Amount::native_whole(1000), - source: Some(defaults::albert_address()), - }, - None, - None, - vec![&defaults::albert_keypair()], - ); - - let self_unbond = shell.generate_tx( - TX_UNBOND_WASM, - Bond { - validator: defaults::validator_address(), - amount: Amount::native_whole(1000), - source: None, - }, - None, - None, - vec![&defaults::validator_keypair()], - ); - - for (signed_tx, bench_name) in - [unbond, self_unbond].iter().zip(["unbond", "self_unbond"]) - { - group.bench_function(bench_name, |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(signed_tx), - criterion::BatchSize::SmallInput, - ) - }); - } - - group.finish(); -} - -fn withdraw(c: &mut Criterion) { - let mut group = c.benchmark_group("withdraw"); - let shell = BenchShell::default(); - - let withdraw = shell.generate_tx( - TX_WITHDRAW_WASM, - Withdraw { - validator: defaults::validator_address(), - source: Some(defaults::albert_address()), - }, - None, - None, - vec![&defaults::albert_keypair()], - ); - - let self_withdraw = shell.generate_tx( - TX_WITHDRAW_WASM, - Withdraw { - validator: defaults::validator_address(), - source: None, - }, - None, - None, - vec![&defaults::validator_keypair()], - ); - - for (signed_tx, bench_name) in [withdraw, self_withdraw] - .iter() - .zip(["withdraw", "self_withdraw"]) - { - group.bench_function(bench_name, |b| { - b.iter_batched_ref( - || { - let mut shell = BenchShell::default(); - - // Unbond funds - let unbond_tx = match bench_name { - "withdraw" => shell.generate_tx( - TX_UNBOND_WASM, - Bond { - validator: defaults::validator_address(), - amount: Amount::native_whole(1000), - source: Some(defaults::albert_address()), - }, - None, - None, - vec![&defaults::albert_keypair()], - ), - "self_withdraw" => shell.generate_tx( - TX_UNBOND_WASM, - Bond { - validator: defaults::validator_address(), - amount: Amount::native_whole(1000), - source: None, - }, - None, - None, - vec![&defaults::validator_keypair()], - ), - _ => panic!("Unexpected bench test"), - }; - - shell.execute_tx(&unbond_tx); - shell.state.commit_tx(); - - // Advance Epoch for pipeline and unbonding length - let params = - proof_of_stake::storage::read_pos_params(&shell.state) - .unwrap(); - let advance_epochs = - params.pipeline_len + params.unbonding_len; - - for _ in 0..=advance_epochs { - shell.advance_epoch(); - } - - shell - }, - |shell| shell.execute_tx(signed_tx), - criterion::BatchSize::SmallInput, - ) - }); - } - - group.finish(); -} - -fn redelegate(c: &mut Criterion) { - let shell = BenchShell::default(); - - let redelegation = |dest_validator| { - shell.generate_tx( - TX_REDELEGATE_WASM, - Redelegation { - src_validator: defaults::validator_address(), - dest_validator, - owner: defaults::albert_address(), - amount: Amount::from(1), - }, - None, - None, - vec![&defaults::albert_keypair()], - ) - }; - - c.bench_function("redelegate", |b| { - b.iter_batched_ref( - || { - let shell = BenchShell::default(); - // Find the other genesis validator - let current_epoch = shell.state.get_block_epoch().unwrap(); - let validators = namada::proof_of_stake::storage::read_consensus_validator_set_addresses(&shell.inner.state, current_epoch).unwrap(); - let validator_2 = validators.into_iter().find(|addr| addr != &defaults::validator_address()).expect("There must be another validator to redelegate to"); - // Prepare the redelegation tx - (shell, redelegation(validator_2)) - }, - |(shell, tx)| shell.execute_tx(tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn reveal_pk(c: &mut Criterion) { - let mut csprng = rand::rngs::OsRng {}; - let new_implicit_account: common::SecretKey = - ed25519::SigScheme::generate(&mut csprng) - .try_to_sk() - .unwrap(); - let shell = BenchShell::default(); - - let tx = shell.generate_tx( - TX_REVEAL_PK_WASM, - new_implicit_account.to_public(), - None, - None, - vec![], - ); - - c.bench_function("reveal_pk", |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(&tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn update_account(c: &mut Criterion) { - let shell = BenchShell::default(); - let vp_code_hash: Hash = shell - .read_storage_key(&Key::wasm_hash(VP_USER_WASM)) - .unwrap(); - let extra_section = Section::ExtraData(Code::from_hash( - vp_code_hash, - Some(VP_USER_WASM.to_string()), - )); - let data = UpdateAccount { - addr: defaults::albert_address(), - vp_code_hash: Some(Hash( - extra_section - .hash(&mut sha2::Sha256::new()) - .finalize_reset() - .into(), - )), - public_keys: vec![defaults::albert_keypair().ref_to()], - threshold: None, - }; - let vp = shell.generate_tx( - TX_UPDATE_ACCOUNT_WASM, - data, - None, - Some(vec![extra_section]), - vec![&defaults::albert_keypair()], - ); - - c.bench_function("update_account", |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(&vp), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn init_account(c: &mut Criterion) { - let mut csprng = rand::rngs::OsRng {}; - let new_account: common::SecretKey = - ed25519::SigScheme::generate(&mut csprng) - .try_to_sk() - .unwrap(); - - let shell = BenchShell::default(); - let vp_code_hash: Hash = shell - .read_storage_key(&Key::wasm_hash(VP_USER_WASM)) - .unwrap(); - let extra_section = Section::ExtraData(Code::from_hash( - vp_code_hash, - Some(VP_USER_WASM.to_string()), - )); - let extra_hash = Hash( - extra_section - .hash(&mut sha2::Sha256::new()) - .finalize_reset() - .into(), - ); - let data = InitAccount { - public_keys: vec![new_account.to_public()], - vp_code_hash: extra_hash, - threshold: 1, - }; - let tx = shell.generate_tx( - TX_INIT_ACCOUNT_WASM, - data, - None, - Some(vec![extra_section]), - vec![&defaults::albert_keypair()], - ); - - c.bench_function("init_account", |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(&tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn init_proposal(c: &mut Criterion) { - let mut group = c.benchmark_group("init_proposal"); - - for bench_name in ["minimal_proposal", "complete_proposal"] { - group.bench_function(bench_name, |b| { - b.iter_batched_ref( - || { - let shell = BenchShell::default(); - - let signed_tx = match bench_name { - "minimal_proposal" => { - let content_section = - Section::ExtraData(Code::new(vec![], None)); - shell.generate_tx( - TX_INIT_PROPOSAL_WASM, - InitProposalData { - content: content_section.get_hash(), - author: defaults::albert_address(), - r#type: ProposalType::Default, - voting_start_epoch: 12.into(), - voting_end_epoch: 15.into(), - activation_epoch: 18.into(), - }, - None, - Some(vec![content_section]), - vec![&defaults::albert_keypair()], - ) - } - "complete_proposal" => { - let max_code_size_key = - namada::governance::storage::keys::get_max_proposal_code_size_key(); - let max_proposal_content_key = - namada::governance::storage::keys::get_max_proposal_content_key(); - let max_code_size: u64 = shell - .state - .read(&max_code_size_key) - .expect("Error while reading from storage") - .expect( - "Missing max_code_size parameter in \ - storage", - ); - let max_proposal_content_size: u64 = shell - .state - .read(&max_proposal_content_key) - .expect("Error while reading from storage") - .expect( - "Missing max_proposal_content parameter \ - in storage", - ); - let content_section = - Section::ExtraData(Code::new(vec![ - 0; - max_proposal_content_size - as _ - ], None)); - let wasm_code_section = - Section::ExtraData(Code::new(vec![ - 0; - max_code_size - as _ - ], None)); - - shell.generate_tx( - TX_INIT_PROPOSAL_WASM, - InitProposalData { - content: content_section.get_hash(), - author: defaults::albert_address(), - r#type: ProposalType::DefaultWithWasm( - wasm_code_section.get_hash(), - ), - voting_start_epoch: 12.into(), - voting_end_epoch: 15.into(), - activation_epoch: 18.into(), - }, - None, - Some(vec![content_section, wasm_code_section]), - vec![&defaults::albert_keypair()], - ) - } - _ => panic!("unexpected bench test"), - }; - - (shell, signed_tx) - }, - |(shell, signed_tx)| shell.execute_tx(signed_tx), - criterion::BatchSize::SmallInput, - ) - }); - } - - group.finish(); -} - -fn vote_proposal(c: &mut Criterion) { - let mut group = c.benchmark_group("vote_proposal"); - let shell = BenchShell::default(); - let delegator_vote = shell.generate_tx( - TX_VOTE_PROPOSAL_WASM, - VoteProposalData { - id: 0, - vote: ProposalVote::Yay, - voter: defaults::albert_address(), - }, - None, - None, - vec![&defaults::albert_keypair()], - ); - - let validator_vote = shell.generate_tx( - TX_VOTE_PROPOSAL_WASM, - VoteProposalData { - id: 0, - vote: ProposalVote::Nay, - voter: defaults::validator_address(), - }, - None, - None, - vec![&defaults::validator_keypair()], - ); - - for (signed_tx, bench_name) in [delegator_vote, validator_vote] - .iter() - .zip(["delegator_vote", "validator_vote"]) - { - group.bench_function(bench_name, |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(signed_tx), - criterion::BatchSize::SmallInput, - ) - }); - } - - group.finish(); -} - -fn become_validator(c: &mut Criterion) { - let mut csprng = rand::rngs::OsRng {}; - let address = address::testing::established_address_1(); - let consensus_key_sk = ed25519::SigScheme::generate(&mut csprng) - .try_to_sk::() - .unwrap(); - let consensus_key = consensus_key_sk.to_public(); - - let eth_cold_key_sk = &secp256k1::SigScheme::generate(&mut csprng) - .try_to_sk::() - .unwrap(); - let eth_cold_key = - secp256k1::PublicKey::try_from_pk(ð_cold_key_sk.to_public()) - .unwrap(); - - let eth_hot_key_sk = &secp256k1::SigScheme::generate(&mut csprng) - .try_to_sk::() - .unwrap(); - let eth_hot_key = - secp256k1::PublicKey::try_from_pk(ð_hot_key_sk.to_public()).unwrap(); - - let protocol_key_sk = ed25519::SigScheme::generate(&mut csprng) - .try_to_sk::() - .unwrap(); - let protocol_key = protocol_key_sk.to_public(); - - let shell = BenchShell::default(); - let data = BecomeValidator { - address: address.clone(), - consensus_key, - eth_cold_key, - eth_hot_key, - protocol_key, - commission_rate: namada::core::dec::Dec::default(), - max_commission_rate_change: namada::core::dec::Dec::default(), - email: "null@null.net".to_string(), - description: None, - website: None, - discord_handle: None, - avatar: None, - }; - let tx = shell.generate_tx( - TX_BECOME_VALIDATOR_WASM, - data, - None, - None, - vec![ - &defaults::albert_keypair(), - &consensus_key_sk, - eth_cold_key_sk, - eth_hot_key_sk, - &protocol_key_sk, - ], - ); - - c.bench_function("become_validator", |b| { - b.iter_batched_ref( - || { - let mut shell = BenchShell::default(); - // Initialize the account to be able to use it - shell - .state - .write( - &namada::core::storage::Key::validity_predicate( - &address, - ), - Vec::::new(), - ) - .unwrap(); - shell - }, - |shell| shell.execute_tx(&tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn change_validator_commission(c: &mut Criterion) { - let shell = BenchShell::default(); - let signed_tx = shell.generate_tx( - TX_CHANGE_VALIDATOR_COMMISSION_WASM, - CommissionChange { - validator: defaults::validator_address(), - new_rate: namada::core::dec::Dec::new(6, 2).unwrap(), - }, - None, - None, - vec![&defaults::albert_keypair()], - ); - - c.bench_function("change_validator_commission", |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(&signed_tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn change_consensus_key(c: &mut Criterion) { - let mut csprng = rand::rngs::OsRng {}; - let consensus_sk = ed25519::SigScheme::generate(&mut csprng) - .try_to_sk::() - .unwrap(); - let consensus_pk = consensus_sk.to_public(); - - let shell = BenchShell::default(); - let signed_tx = shell.generate_tx( - TX_CHANGE_CONSENSUS_KEY_WASM, - ConsensusKeyChange { - validator: defaults::validator_address(), - consensus_key: consensus_pk, - }, - None, - None, - vec![&defaults::validator_keypair(), &consensus_sk], - ); - - c.bench_function("change_consensus_key", |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(&signed_tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn change_validator_metadata(c: &mut Criterion) { - // Choose just one piece of data arbitrarily to change - let metadata_change = MetaDataChange { - validator: defaults::validator_address(), - email: None, - description: Some("I will change this piece of data".to_string()), - website: None, - discord_handle: None, - avatar: None, - commission_rate: None, - }; - - let shell = BenchShell::default(); - let signed_tx = shell.generate_tx( - TX_CHANGE_VALIDATOR_METADATA_WASM, - metadata_change, - None, - None, - vec![&defaults::albert_keypair()], - ); - - c.bench_function("change_validator_metadata", |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(&signed_tx), - criterion::BatchSize::SmallInput, - ) - }); -} fn ibc(c: &mut Criterion) { let mut group = c.benchmark_group("tx_ibc"); @@ -842,292 +125,5 @@ fn ibc(c: &mut Criterion) { group.finish(); } -fn unjail_validator(c: &mut Criterion) { - let shell = BenchShell::default(); - let signed_tx = shell.generate_tx( - TX_UNJAIL_VALIDATOR_WASM, - defaults::validator_address(), - None, - None, - vec![&defaults::albert_keypair()], - ); - - c.bench_function("unjail_validator", |b| { - b.iter_batched_ref( - || { - let mut shell = BenchShell::default(); - - // Jail the validator - let pos_params = read_pos_params(&shell.state).unwrap(); - let current_epoch = shell.state.in_mem().block.epoch; - let evidence_epoch = current_epoch.prev(); - proof_of_stake::slashing::slash( - &mut shell.state, - &pos_params, - current_epoch, - evidence_epoch, - 0u64, - SlashType::DuplicateVote, - &defaults::validator_address(), - current_epoch.next(), - ) - .unwrap(); - - shell.state.commit_tx(); - shell.commit_block(); - // Advance by slash epoch offset epochs - for _ in 0..=pos_params.slash_processing_epoch_offset() { - shell.advance_epoch(); - } - - shell - }, - |shell| shell.execute_tx(&signed_tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn tx_bridge_pool(c: &mut Criterion) { - if !is_bridge_comptime_enabled() { - return; - } - - let shell = BenchShell::default(); - - let data = PendingTransfer { - transfer: namada::core::eth_bridge_pool::TransferToEthereum { - kind: namada::core::eth_bridge_pool::TransferToEthereumKind::Erc20, - asset: read_native_erc20_address(&shell.state).unwrap(), - recipient: namada::core::ethereum_events::EthAddress([1u8; 20]), - sender: defaults::albert_address(), - amount: Amount::from(1), - }, - gas_fee: GasFee { - amount: Amount::from(100), - payer: defaults::albert_address(), - token: shell.state.in_mem().native_token.clone(), - }, - }; - let tx = shell.generate_tx( - TX_BRIDGE_POOL_WASM, - data, - None, - None, - vec![&defaults::albert_keypair()], - ); - c.bench_function("bridge pool", |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(&tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn resign_steward(c: &mut Criterion) { - c.bench_function("resign steward", |b| { - b.iter_batched_ref( - || { - let mut shell = BenchShell::default(); - namada::governance::pgf::storage::keys::stewards_handle() - .insert( - &mut shell.state, - defaults::albert_address(), - StewardDetail::base(defaults::albert_address()), - ) - .unwrap(); - - let tx = shell.generate_tx( - TX_RESIGN_STEWARD, - defaults::albert_address(), - None, - None, - vec![&defaults::albert_keypair()], - ); - - (shell, tx) - }, - |(shell, tx)| shell.execute_tx(tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn update_steward_commission(c: &mut Criterion) { - c.bench_function("update steward commission", |b| { - b.iter_batched_ref( - || { - let mut shell = BenchShell::default(); - namada::governance::pgf::storage::keys::stewards_handle() - .insert( - &mut shell.state, - defaults::albert_address(), - StewardDetail::base(defaults::albert_address()), - ) - .unwrap(); - - let data = namada::tx::data::pgf::UpdateStewardCommission { - steward: defaults::albert_address(), - commission: HashMap::from([( - defaults::albert_address(), - namada::core::dec::Dec::zero(), - )]), - }; - let tx = shell.generate_tx( - TX_UPDATE_STEWARD_COMMISSION, - data, - None, - None, - vec![&defaults::albert_keypair()], - ); - - (shell, tx) - }, - |(shell, tx)| shell.execute_tx(tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn deactivate_validator(c: &mut Criterion) { - let shell = BenchShell::default(); - let signed_tx = shell.generate_tx( - TX_DEACTIVATE_VALIDATOR_WASM, - defaults::validator_address(), - None, - None, - vec![&defaults::albert_keypair()], - ); - - c.bench_function("deactivate_validator", |b| { - b.iter_batched_ref( - BenchShell::default, - |shell| shell.execute_tx(&signed_tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn reactivate_validator(c: &mut Criterion) { - let shell = BenchShell::default(); - let signed_tx = shell.generate_tx( - TX_REACTIVATE_VALIDATOR_WASM, - defaults::validator_address(), - None, - None, - vec![&defaults::albert_keypair()], - ); - - c.bench_function("reactivate_validator", |b| { - b.iter_batched_ref( - || { - let mut shell = BenchShell::default(); - - // Deactivate the validator - let pos_params = read_pos_params(&shell.state).unwrap(); - let current_epoch = shell.state.in_mem().block.epoch; - proof_of_stake::deactivate_validator( - &mut shell.state, - &defaults::validator_address(), - current_epoch, - ) - .unwrap(); - - shell.state.commit_tx(); - shell.commit_block(); - // Advance by slash epoch offset epochs - for _ in 0..=pos_params.pipeline_len { - shell.advance_epoch(); - } - - shell - }, - |shell| shell.execute_tx(&signed_tx), - criterion::BatchSize::SmallInput, - ) - }); -} - -fn claim_rewards(c: &mut Criterion) { - let mut group = c.benchmark_group("claim_rewards"); - let shell = BenchShell::default(); - - let claim = shell.generate_tx( - TX_CLAIM_REWARDS_WASM, - Withdraw { - validator: defaults::validator_address(), - source: Some(defaults::albert_address()), - }, - None, - None, - vec![&defaults::albert_keypair()], - ); - - let self_claim = shell.generate_tx( - TX_CLAIM_REWARDS_WASM, - Withdraw { - validator: defaults::validator_address(), - source: None, - }, - None, - None, - vec![&defaults::albert_keypair()], - ); - - for (signed_tx, bench_name) in - [claim, self_claim].iter().zip(["claim", "self_claim"]) - { - group.bench_function(bench_name, |b| { - b.iter_batched_ref( - || { - let mut shell = BenchShell::default(); - - // Advance Epoch for pipeline and unbonding length - let params = - proof_of_stake::storage::read_pos_params(&shell.state) - .unwrap(); - let advance_epochs = - params.pipeline_len + params.unbonding_len; - - for _ in 0..=advance_epochs { - shell.advance_epoch(); - } - - shell - }, - |shell| shell.execute_tx(signed_tx), - criterion::BatchSize::SmallInput, - ) - }); - } - - group.finish(); -} - -criterion_group!( - allowed_txs, - transfer, - bond, - unbond, - withdraw, - redelegate, - reveal_pk, - update_account, - init_account, - init_proposal, - vote_proposal, - become_validator, - change_validator_commission, - ibc, - unjail_validator, - tx_bridge_pool, - resign_steward, - update_steward_commission, - deactivate_validator, - reactivate_validator, - change_validator_metadata, - claim_rewards, - change_consensus_key -); +criterion_group!(allowed_txs, ibc); criterion_main!(allowed_txs); diff --git a/crates/benches/vps.rs b/crates/benches/vps.rs deleted file mode 100644 index 127e056903..0000000000 --- a/crates/benches/vps.rs +++ /dev/null @@ -1,322 +0,0 @@ -use std::cell::RefCell; - -use criterion::{criterion_group, criterion_main, Criterion}; -use namada::account::UpdateAccount; -use namada::core::address::{self, Address}; -use namada::core::hash::Hash; -use namada::core::key::{ - common, ed25519, SecretKey as SecretKeyInterface, SigScheme, -}; -use namada::core::storage::{Key, TxIndex}; -use namada::governance::storage::vote::ProposalVote; -use namada::governance::VoteProposalData; -use namada::ledger::gas::{TxGasMeter, VpGasMeter}; -use namada::token::{Amount, Transfer}; -use namada::tx::data::pos::{Bond, CommissionChange}; -use namada::tx::{Code, Section}; -use namada::vm::wasm::run; -use namada_apps::bench_utils::{ - generate_foreign_key_tx, BenchShell, TX_BOND_WASM, - TX_CHANGE_VALIDATOR_COMMISSION_WASM, TX_REVEAL_PK_WASM, TX_TRANSFER_WASM, - TX_UNBOND_WASM, TX_UPDATE_ACCOUNT_WASM, TX_VOTE_PROPOSAL_WASM, - VP_USER_WASM, -}; -use namada_apps::wallet::defaults; -use sha2::Digest; - -const VP_IMPLICIT_WASM: &str = "vp_implicit.wasm"; - -fn vp_implicit(c: &mut Criterion) { - let mut group = c.benchmark_group("vp_implicit"); - - let mut csprng = rand::rngs::OsRng {}; - let implicit_account: common::SecretKey = - ed25519::SigScheme::generate(&mut csprng) - .try_to_sk() - .unwrap(); - - let foreign_key_write = generate_foreign_key_tx(&implicit_account); - - let shell = BenchShell::default(); - let transfer = shell.generate_tx( - TX_TRANSFER_WASM, - Transfer { - source: Address::from(&implicit_account.to_public()), - target: defaults::bertha_address(), - token: address::testing::nam(), - amount: Amount::native_whole(500).native_denominated(), - shielded: None, - }, - None, - None, - vec![&implicit_account], - ); - - let received_transfer = shell.generate_tx( - TX_TRANSFER_WASM, - Transfer { - source: defaults::bertha_address(), - target: Address::from(&implicit_account.to_public()), - token: address::testing::nam(), - amount: Amount::native_whole(1000).native_denominated(), - shielded: None, - }, - None, - None, - vec![&defaults::bertha_keypair()], - ); - - let reveal_pk = shell.generate_tx( - TX_REVEAL_PK_WASM, - implicit_account.to_public(), - None, - None, - vec![], - ); - - let pos = shell.generate_tx( - TX_BOND_WASM, - Bond { - validator: defaults::validator_address(), - amount: Amount::native_whole(1000), - source: Some(Address::from(&implicit_account.to_public())), - }, - None, - None, - vec![&implicit_account], - ); - - let vote = shell.generate_tx( - TX_VOTE_PROPOSAL_WASM, - VoteProposalData { - id: 0, - vote: ProposalVote::Yay, - voter: Address::from(&implicit_account.to_public()), - }, - None, - None, - vec![&implicit_account], - ); - - for (tx, bench_name) in [ - &foreign_key_write, - &reveal_pk, - &transfer, - &received_transfer, - &pos, - &vote, - ] - .into_iter() - .zip([ - "foreign_key_write", - "reveal_pk", - "transfer", - "received_transfer", - "pos", - "governance_vote", - ]) { - let mut shell = BenchShell::default(); - let vp_code_hash: Hash = shell - .read_storage_key(&Key::wasm_hash(VP_IMPLICIT_WASM)) - .unwrap(); - - if bench_name != "reveal_pk" { - // Reveal public key - shell.execute_tx(&reveal_pk); - shell.state.commit_tx(); - shell.commit_block(); - } - - if bench_name == "transfer" || bench_name == "pos" { - // Transfer some tokens to the implicit address - shell.execute_tx(&received_transfer); - shell.state.commit_tx(); - shell.commit_block(); - } - - // Run the tx to validate - let verifiers_from_tx = shell.execute_tx(tx); - let (verifiers, keys_changed) = shell - .state - .write_log() - .verifiers_and_changed_keys(&verifiers_from_tx); - - let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( - &TxGasMeter::new_from_sub_limit(u64::MAX.into()), - )); - group.bench_function(bench_name, |b| { - b.iter(|| { - assert!( - run::vp( - vp_code_hash, - tx, - &TxIndex(0), - &Address::from(&implicit_account.to_public()), - &shell.state, - &gas_meter, - &keys_changed, - &verifiers, - shell.vp_wasm_cache.clone(), - ) - .is_ok() - ) - }) - }); - } - - group.finish(); -} - -fn vp_user(c: &mut Criterion) { - let shell = BenchShell::default(); - let vp_code_hash: Hash = shell - .read_storage_key(&Key::wasm_hash(VP_USER_WASM)) - .unwrap(); - let mut group = c.benchmark_group("vp_user"); - - let foreign_key_write = - generate_foreign_key_tx(&defaults::validator_account_keypair()); - - let transfer = shell.generate_tx( - TX_TRANSFER_WASM, - Transfer { - source: defaults::validator_address(), - target: defaults::bertha_address(), - token: address::testing::nam(), - amount: Amount::native_whole(1000).native_denominated(), - shielded: None, - }, - None, - None, - vec![&defaults::validator_account_keypair()], - ); - - let received_transfer = shell.generate_tx( - TX_TRANSFER_WASM, - Transfer { - source: defaults::bertha_address(), - target: defaults::validator_address(), - token: address::testing::nam(), - amount: Amount::native_whole(1000).native_denominated(), - shielded: None, - }, - None, - None, - vec![&defaults::bertha_keypair()], - ); - - let extra_section = Section::ExtraData(Code::from_hash( - vp_code_hash, - Some(VP_USER_WASM.to_string()), - )); - let data = UpdateAccount { - addr: defaults::validator_address(), - vp_code_hash: Some(Hash( - extra_section - .hash(&mut sha2::Sha256::new()) - .finalize_reset() - .into(), - )), - public_keys: vec![defaults::validator_account_keypair().to_public()], - threshold: None, - }; - let vp = shell.generate_tx( - TX_UPDATE_ACCOUNT_WASM, - data, - None, - Some(vec![extra_section]), - vec![&defaults::validator_account_keypair()], - ); - - let commission_rate = shell.generate_tx( - TX_CHANGE_VALIDATOR_COMMISSION_WASM, - CommissionChange { - validator: defaults::validator_address(), - new_rate: namada::core::dec::Dec::new(6, 2).unwrap(), - }, - None, - None, - vec![&defaults::validator_account_keypair()], - ); - - let vote = shell.generate_tx( - TX_VOTE_PROPOSAL_WASM, - VoteProposalData { - id: 0, - vote: ProposalVote::Yay, - voter: defaults::validator_address(), - }, - None, - None, - vec![&defaults::validator_account_keypair()], - ); - - let pos = shell.generate_tx( - TX_UNBOND_WASM, - Bond { - validator: defaults::validator_address(), - amount: Amount::native_whole(1000), - source: None, - }, - None, - None, - vec![&defaults::validator_account_keypair()], - ); - - for (signed_tx, bench_name) in [ - foreign_key_write, - transfer, - received_transfer, - vote, - pos, - commission_rate, - vp, - ] - .iter() - .zip([ - "foreign_key_write", - "transfer", - "received_transfer", - "governance_vote", - "pos", - "commission_rate", - "vp", - ]) { - let mut shell = BenchShell::default(); - - let verifiers_from_tx = shell.execute_tx(signed_tx); - let (verifiers, keys_changed) = shell - .state - .write_log() - .verifiers_and_changed_keys(&verifiers_from_tx); - - let gas_meter = RefCell::new(VpGasMeter::new_from_tx_meter( - &TxGasMeter::new_from_sub_limit(u64::MAX.into()), - )); - group.bench_function(bench_name, |b| { - b.iter(|| { - // NOTE: the wasm code is always in cache so we don't - // include here the cost to read and compile the vp code - assert!( - run::vp( - vp_code_hash, - signed_tx, - &TxIndex(0), - &defaults::validator_address(), - &shell.state, - &gas_meter, - &keys_changed, - &verifiers, - shell.vp_wasm_cache.clone(), - ) - .is_ok() - ); - }) - }); - } - - group.finish(); -} - -criterion_group!(allowed_vps, vp_user, vp_implicit); -criterion_main!(allowed_vps);