From a9c1b75d6ee6a8c3400c20b3ce8c8696bd322be4 Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Fri, 24 Apr 2020 11:40:28 +0200 Subject: [PATCH] Repair and extend some benchmarks (#5648) --- bin/node/runtime/src/lib.rs | 2 +- frame/benchmarking/src/lib.rs | 10 +- frame/collective/src/benchmarking.rs | 424 ++++++++--- frame/collective/src/lib.rs | 2 +- frame/democracy/src/benchmarking.rs | 800 ++++++++++++++++---- frame/democracy/src/tests.rs | 24 +- frame/support/src/traits.rs | 2 +- frame/timestamp/src/benchmarking.rs | 24 +- frame/vesting/src/benchmarking.rs | 203 +++-- utils/frame/benchmarking-cli/src/command.rs | 3 + 10 files changed, 1174 insertions(+), 320 deletions(-) diff --git a/bin/node/runtime/src/lib.rs b/bin/node/runtime/src/lib.rs index 7c39c72bd4a65..29defaaecf767 100644 --- a/bin/node/runtime/src/lib.rs +++ b/bin/node/runtime/src/lib.rs @@ -903,13 +903,13 @@ impl_runtime_apis! { add_benchmark!(params, batches, b"democracy", Democracy); add_benchmark!(params, batches, b"identity", Identity); add_benchmark!(params, batches, b"im-online", ImOnline); + add_benchmark!(params, batches, b"offences", OffencesBench::); add_benchmark!(params, batches, b"session", SessionBench::); add_benchmark!(params, batches, b"staking", Staking); add_benchmark!(params, batches, b"timestamp", Timestamp); add_benchmark!(params, batches, b"treasury", Treasury); add_benchmark!(params, batches, b"utility", Utility); add_benchmark!(params, batches, b"vesting", Vesting); - add_benchmark!(params, batches, b"offences", OffencesBench::); if batches.is_empty() { return Err("Benchmark not found for this pallet.".into()) } Ok(batches) diff --git a/frame/benchmarking/src/lib.rs b/frame/benchmarking/src/lib.rs index 6bb10f3d97216..8045408d53040 100644 --- a/frame/benchmarking/src/lib.rs +++ b/frame/benchmarking/src/lib.rs @@ -723,6 +723,9 @@ macro_rules! impl_benchmark { let steps = steps.get(idx).cloned().unwrap_or(prev_steps); prev_steps = steps; + // Skip this loop if steps is zero + if steps == 0 { continue } + let lowest = lowest_range_values.get(idx).cloned().unwrap_or(*low); let highest = highest_range_values.get(idx).cloned().unwrap_or(*high); @@ -828,6 +831,9 @@ macro_rules! impl_benchmark { let steps = steps.get(idx).cloned().unwrap_or(prev_steps); prev_steps = steps; + // Skip this loop if steps is zero + if steps == 0 { continue } + let lowest = lowest_range_values.get(idx).cloned().unwrap_or(*low); let highest = highest_range_values.get(idx).cloned().unwrap_or(*high); @@ -1036,7 +1042,7 @@ macro_rules! add_benchmark { &steps[..], repeat, )?, - pallet: pallet.to_vec(), + pallet: $name.to_vec(), benchmark: benchmark.to_vec(), }); } @@ -1049,7 +1055,7 @@ macro_rules! add_benchmark { &steps[..], repeat, )?, - pallet: pallet.to_vec(), + pallet: $name.to_vec(), benchmark: benchmark.clone(), }); } diff --git a/frame/collective/src/benchmarking.rs b/frame/collective/src/benchmarking.rs index edef5e2e24e05..5c25051fd0837 100644 --- a/frame/collective/src/benchmarking.rs +++ b/frame/collective/src/benchmarking.rs @@ -19,173 +19,398 @@ use super::*; use frame_system::RawOrigin as SystemOrigin; +use frame_system::EventRecord; use frame_benchmarking::{benchmarks_instance, account}; +use sp_runtime::traits::Bounded; use frame_system::Module as System; use crate::Module as Collective; const SEED: u32 = 0; +const MAX_MEMBERS: u32 = 1000; +const MAX_PROPOSALS: u32 = 100; +const MAX_BYTES: u32 = 1_024; + +fn assert_last_event, I: Instance>(generic_event: >::Event) { + let events = System::::events(); + let system_event: ::Event = generic_event.into(); + // compare to the last event record + let EventRecord { event, .. } = &events[events.len() - 1]; + assert_eq!(event, &system_event); +} + benchmarks_instance! { - _{ - // User account seed. - let u in 1 .. 1000 => (); - // Old members. - let n in 1 .. 1000 => (); - // New members. - let m in 1 .. 1000 => (); - // Existing proposals. - let p in 1 .. 100 => (); - } + _{ } set_members { - let m in ...; - let n in ...; - - // Construct `new_members`. - // It should influence timing since it will sort this vector. - let mut new_members = vec![]; - for i in 0 .. m { - let member = account("member", i, SEED); - new_members.push(member); - } + let m in 1 .. MAX_MEMBERS; + let n in 1 .. MAX_MEMBERS; // Set old members. // We compute the difference of old and new members, so it should influence timing. let mut old_members = vec![]; + let mut last_old_member = T::AccountId::default(); for i in 0 .. n { - let old_member = account("old member", i, SEED); - old_members.push(old_member); + last_old_member = account("old member", i, SEED); + old_members.push(last_old_member.clone()); } - let prime = Some(account("prime", 0, SEED)); + Collective::::set_members(SystemOrigin::Root.into(), old_members, Some(last_old_member))?; - Collective::::set_members(SystemOrigin::Root.into(), old_members, prime.clone())?; + // Construct `new_members`. + // It should influence timing since it will sort this vector. + let mut new_members = vec![]; + let mut last_member = T::AccountId::default(); + for i in 0 .. m { + last_member = account("member", i, SEED); + new_members.push(last_member.clone()); + } - }: _(SystemOrigin::Root, new_members.clone(), prime) + }: _(SystemOrigin::Root, new_members.clone(), Some(last_member)) verify { new_members.sort(); assert_eq!(Collective::::members(), new_members); } execute { - let u in ...; + let m in 1 .. MAX_MEMBERS; + let b in 1 .. MAX_BYTES; - let caller: T::AccountId = account("caller", u, SEED); - let proposal: T::Proposal = Call::::close(Default::default(), Default::default()).into(); + // Construct `members`. + let mut members = vec![]; + for i in 0 .. m { + let member = account("member", i, SEED); + members.push(member); + } - Collective::::set_members(SystemOrigin::Root.into(), vec![caller.clone()], None)?; + let caller: T::AccountId = account("caller", 0, SEED); + members.push(caller.clone()); - }: _(SystemOrigin::Signed(caller), Box::new(proposal)) + Collective::::set_members(SystemOrigin::Root.into(), members, None)?; - propose { - let u in ...; + let proposal: T::Proposal = frame_system::Call::::remark(vec![1; b as usize]).into(); - let caller: T::AccountId = account("caller", u, SEED); - let proposal: T::Proposal = Call::::close(Default::default(), Default::default()).into(); + }: _(SystemOrigin::Signed(caller), Box::new(proposal.clone())) + verify { + let proposal_hash = T::Hashing::hash_of(&proposal); + // Note that execution fails due to mis-matched origin + assert_last_event::(RawEvent::MemberExecuted(proposal_hash, false).into()); + } - Collective::::set_members(SystemOrigin::Root.into(), vec![caller.clone()], None)?; + // This tests when execution would happen immediately after proposal + propose_execute { + let m in 1 .. MAX_MEMBERS; + let b in 1 .. MAX_BYTES; - let member_count = 0; + // Construct `members`. + let mut members = vec![]; + for i in 0 .. m { + let member = account("member", i, SEED); + members.push(member); + } - }: _(SystemOrigin::Signed(caller), member_count, Box::new(proposal.into())) + let caller: T::AccountId = account("caller", 0, SEED); + members.push(caller.clone()); - propose_else_branch { - let u in ...; - let p in ...; + Collective::::set_members(SystemOrigin::Root.into(), members, None)?; - let caller: T::AccountId = account("caller", u, SEED); - let proposal: T::Proposal = Call::::close(Default::default(), Default::default()).into(); + let proposal: T::Proposal = frame_system::Call::::remark(vec![1; b as usize]).into(); + let threshold = 1; - Collective::::set_members(SystemOrigin::Root.into(), vec![caller.clone()], None)?; + }: propose(SystemOrigin::Signed(caller), threshold, Box::new(proposal.clone())) + verify { + let proposal_hash = T::Hashing::hash_of(&proposal); + // Note that execution fails due to mis-matched origin + assert_last_event::(RawEvent::Executed(proposal_hash, false).into()); + } + + // This tests when proposal is created and queued as "proposed" + propose_proposed { + let m in 1 .. MAX_MEMBERS; + let p in 0 .. MAX_PROPOSALS; + let b in 1 .. MAX_BYTES; + + // Construct `members`. + let mut members = vec![]; + for i in 0 .. m { + let member = account("member", i, SEED); + members.push(member); + } + + let caller: T::AccountId = account("caller", 0, SEED); + members.push(caller.clone()); + Collective::::set_members(SystemOrigin::Root.into(), members, None)?; - let member_count = 3; + let threshold = m.max(2); // Add previous proposals. for i in 0 .. p { - let proposal: T::Proposal = Call::::close(Default::default(), (i + 1).into()).into(); - Collective::::propose(SystemOrigin::Signed(caller.clone()).into(), member_count.clone(), Box::new(proposal.into()))?; + // Proposals should be different so that different proposal hashes are generated + let proposal: T::Proposal = frame_system::Call::::remark(vec![i as u8; b as usize]).into(); + Collective::::propose(SystemOrigin::Signed(caller.clone()).into(), threshold, Box::new(proposal))?; } - }: propose(SystemOrigin::Signed(caller), member_count, Box::new(proposal.into())) + assert_eq!(Collective::::proposals().len(), p as usize); - vote { - let u in ...; + let proposal: T::Proposal = frame_system::Call::::remark(vec![p as u8; b as usize]).into(); - let caller1: T::AccountId = account("caller1", u, SEED); - let caller2: T::AccountId = account("caller2", u, SEED); - - let proposal: Box = Box::new(Call::::close(Default::default(), Default::default()).into()); + }: propose(SystemOrigin::Signed(caller.clone()), threshold, Box::new(proposal.clone())) + verify { + // New proposal is recorded + assert_eq!(Collective::::proposals().len(), (p + 1) as usize); let proposal_hash = T::Hashing::hash_of(&proposal); + assert_last_event::(RawEvent::Proposed(caller, p, proposal_hash, threshold).into()); + } + + vote_insert { + let m in 2 .. MAX_MEMBERS; + let p in 1 .. MAX_PROPOSALS; + let b in 1 .. MAX_BYTES; + + // Construct `members`. + let mut members = vec![]; + for i in 0 .. m { + let member = account("member", i, SEED); + members.push(member); + } - Collective::::set_members(SystemOrigin::Root.into(), vec![caller1.clone(), caller2.clone()], None)?; + let caller: T::AccountId = account("caller", 0, SEED); + members.push(caller.clone()); + Collective::::set_members(SystemOrigin::Root.into(), members.clone(), None)?; - let member_count = 3; - Collective::::propose(SystemOrigin::Signed(caller1.clone()).into(), member_count, proposal)?; + // Threshold is 1 less than the number of members so that one person can vote nay + let threshold = m; - let index = 0; - let approve = true; + // Add previous proposals + let mut last_hash = T::Hash::default(); + for i in 0 .. p { + // Proposals should be different so that different proposal hashes are generated + let proposal: T::Proposal = frame_system::Call::::remark(vec![i as u8; b as usize]).into(); + Collective::::propose(SystemOrigin::Signed(caller.clone()).into(), threshold, Box::new(proposal.clone()))?; + last_hash = T::Hashing::hash_of(&proposal); + } - }: _(SystemOrigin::Signed(caller2), proposal_hash, index, approve) + // Have everyone vote aye on last proposal, while keeping it from passing + for j in 2 .. m { + let voter = &members[j as usize]; + let approve = true; + Collective::::vote(SystemOrigin::Signed(voter.clone()).into(), last_hash.clone(), p - 1, approve)?; + } - vote_not_approve { - let u in ...; + assert_eq!(Collective::::proposals().len(), p as usize); - let caller1: T::AccountId = account("caller1", u, SEED); - let caller2: T::AccountId = account("caller2", u, SEED); + // Caller switches vote to nay, but does not kill the vote, just updates + inserts + let index = p - 1; + let approve = false; - let proposal: Box = Box::new(Call::::close(Default::default(), Default::default()).into()); - let proposal_hash = T::Hashing::hash_of(&proposal); + }: vote(SystemOrigin::Signed(caller), last_hash.clone(), index, approve) + verify { + // All proposals exist and the last proposal has just been updated. + assert_eq!(Collective::::proposals().len(), p as usize); + let voting = Collective::::voting(&last_hash).ok_or(Error::::ProposalMissing)?; + assert_eq!(voting.ayes.len(), (m - 2) as usize); + assert_eq!(voting.nays.len(), 1); + } - Collective::::set_members(SystemOrigin::Root.into(), vec![caller1.clone(), caller2.clone()], None)?; + vote_disapproved { + let m in 2 .. MAX_MEMBERS; + let p in 1 .. MAX_PROPOSALS; + let b in 1 .. MAX_BYTES; - let member_count = 3; - Collective::::propose(SystemOrigin::Signed(caller1.clone()).into(), member_count, proposal)?; + // Construct `members`. + let mut members = vec![]; + for i in 0 .. m { + let member = account("member", i, SEED); + members.push(member); + } - let index = 0; + let caller: T::AccountId = account("caller", 0, SEED); + members.push(caller.clone()); + Collective::::set_members(SystemOrigin::Root.into(), members.clone(), None)?; + + // Threshold is total members so that one nay will disapprove the vote + let threshold = m + 1; + + // Add previous proposals + let mut last_hash = T::Hash::default(); + for i in 0 .. p { + // Proposals should be different so that different proposal hashes are generated + let proposal: T::Proposal = frame_system::Call::::remark(vec![i as u8; b as usize]).into(); + Collective::::propose(SystemOrigin::Signed(caller.clone()).into(), threshold, Box::new(proposal.clone()))?; + last_hash = T::Hashing::hash_of(&proposal); + } + + // Have everyone vote aye on last proposal, while keeping it from passing + for j in 1 .. m { + let voter = &members[j as usize]; + let approve = true; + Collective::::vote(SystemOrigin::Signed(voter.clone()).into(), last_hash.clone(), p - 1, approve)?; + } + + assert_eq!(Collective::::proposals().len(), p as usize); + + // Caller switches vote to nay, which kills the vote + let index = p - 1; let approve = false; - }: vote(SystemOrigin::Signed(caller2), proposal_hash, index, approve) + }: vote(SystemOrigin::Signed(caller), last_hash.clone(), index, approve) + verify { + // The last proposal is removed. + assert_eq!(Collective::::proposals().len(), (p - 1) as usize); + assert_last_event::(RawEvent::Disapproved(last_hash).into()); + } vote_approved { - let u in ...; + let m in 2 .. MAX_MEMBERS; + let p in 1 .. MAX_PROPOSALS; + let b in 1 .. MAX_BYTES; + + // Construct `members`. + let mut members = vec![]; + for i in 0 .. m { + let member = account("member", i, SEED); + members.push(member); + } - let caller1: T::AccountId = account("caller1", u, SEED); - let caller2: T::AccountId = account("caller2", u, SEED); + let caller: T::AccountId = account("caller", 0, SEED); + members.push(caller.clone()); + Collective::::set_members(SystemOrigin::Root.into(), members.clone(), None)?; - let proposal: Box = Box::new(Call::::close(Default::default(), Default::default()).into()); - let proposal_hash = T::Hashing::hash_of(&proposal); + // Threshold is 2 so any two ayes will approve the vote + let threshold = 2; - Collective::::set_members(SystemOrigin::Root.into(), vec![caller1.clone(), caller2.clone()], None)?; + // Add previous proposals + let mut last_hash = T::Hash::default(); + for i in 0 .. p { + // Proposals should be different so that different proposal hashes are generated + let proposal: T::Proposal = frame_system::Call::::remark(vec![i as u8; b as usize]).into(); + Collective::::propose(SystemOrigin::Signed(caller.clone()).into(), threshold, Box::new(proposal.clone()))?; + last_hash = T::Hashing::hash_of(&proposal); + } + + // Caller switches vote to nay on their own proposal, allowing them to be the deciding approval vote + Collective::::vote(SystemOrigin::Signed(caller.clone()).into(), last_hash.clone(), p - 1, false)?; + + // Have everyone vote nay on last proposal, while keeping it from failing + for j in 2 .. m { + let voter = &members[j as usize]; + let approve = false; + Collective::::vote(SystemOrigin::Signed(voter.clone()).into(), last_hash.clone(), p - 1, approve)?; + } - let member_count = 2; - Collective::::propose(SystemOrigin::Signed(caller1.clone()).into(), member_count, proposal)?; + // Member zero is the first aye + Collective::::vote(SystemOrigin::Signed(members[0].clone()).into(), last_hash.clone(), p - 1, true)?; - let index = 0; + assert_eq!(Collective::::proposals().len(), p as usize); + + // Caller switches vote to aye, which passes the vote + let index = p - 1; let approve = true; - }: vote(SystemOrigin::Signed(caller2), proposal_hash, index, approve) + }: vote(SystemOrigin::Signed(caller), last_hash.clone(), index, approve) + verify { + // The last proposal is removed. + assert_eq!(Collective::::proposals().len(), (p - 1) as usize); + assert_last_event::(RawEvent::Executed(last_hash, false).into()); + } + + close_disapproved { + let m in 2 .. MAX_MEMBERS; + let p in 1 .. MAX_PROPOSALS; + let b in 1 .. MAX_BYTES; + + // Construct `members`. + let mut members = vec![]; + for i in 0 .. m { + let member = account("member", i, SEED); + members.push(member); + } + let caller: T::AccountId = account("caller", 0, SEED); + members.push(caller.clone()); + + Collective::::set_members(SystemOrigin::Root.into(), members.clone(), Some(caller.clone()))?; - close { - let u in ...; + // Threshold is one less than total members so that two nays will disapprove the vote + let threshold = m; - let caller1: T::AccountId = account("caller1", u, SEED); - let caller2: T::AccountId = account("caller2", u, SEED); + // Add proposals + let mut last_hash = T::Hash::default(); + for i in 0 .. p { + // Proposals should be different so that different proposal hashes are generated + let proposal: T::Proposal = frame_system::Call::::remark(vec![i as u8; b as usize]).into(); + Collective::::propose(SystemOrigin::Signed(caller.clone()).into(), threshold, Box::new(proposal.clone()))?; + last_hash = T::Hashing::hash_of(&proposal); + } - let proposal: Box = Box::new(Call::::close(Default::default(), Default::default()).into()); - let proposal_hash = T::Hashing::hash_of(&proposal); + // Have everyone vote aye on last proposal, while keeping it from passing + // A few abstainers will be the nay votes needed to fail the vote + for j in 2 .. m { + let voter = &members[j as usize]; + let approve = true; + Collective::::vote(SystemOrigin::Signed(voter.clone()).into(), last_hash.clone(), p - 1, approve)?; + } - Collective::::set_members(SystemOrigin::Root.into(), vec![caller1.clone(), caller2.clone()], None)?; - let member_count = 2; - Collective::::propose(SystemOrigin::Signed(caller1.clone()).into(), member_count, proposal)?; + // caller is prime, prime votes nay + Collective::::vote(SystemOrigin::Signed(caller.clone()).into(), last_hash.clone(), p - 1, false)?; - let index = 0; - let approve = true; + System::::set_block_number(T::BlockNumber::max_value()); + assert_eq!(Collective::::proposals().len(), p as usize); + + // Prime nay will close it as disapproved + }: close(SystemOrigin::Signed(caller), last_hash, p - 1) + verify { + assert_eq!(Collective::::proposals().len(), (p - 1) as usize); + assert_last_event::(RawEvent::Disapproved(last_hash).into()); + } + + + close_approved { + let m in 2 .. MAX_MEMBERS; + let p in 1 .. MAX_PROPOSALS; + let b in 1 .. MAX_BYTES; - let vote_end = T::MotionDuration::get() + 1u32.into(); - System::::set_block_number(vote_end); + // Construct `members`. + let mut members = vec![]; + for i in 0 .. m { + let member = account("member", i, SEED); + members.push(member); + } + let caller: T::AccountId = account("caller", 0, SEED); + members.push(caller.clone()); + + Collective::::set_members(SystemOrigin::Root.into(), members.clone(), Some(caller.clone()))?; - }: _(SystemOrigin::Signed(caller2), proposal_hash, index) + // Threshold is two, so any two ayes will pass the vote + let threshold = 2; + + // Add proposals + let mut last_hash = T::Hash::default(); + for i in 0 .. p { + // Proposals should be different so that different proposal hashes are generated + let proposal: T::Proposal = frame_system::Call::::remark(vec![i as u8; b as usize]).into(); + Collective::::propose(SystemOrigin::Signed(caller.clone()).into(), threshold, Box::new(proposal.clone()))?; + last_hash = T::Hashing::hash_of(&proposal); + } + + // Have everyone vote nay on last proposal, while keeping it from failing + // A few abstainers will be the aye votes needed to pass the vote + for j in 2 .. m { + let voter = &members[j as usize]; + let approve = false; + Collective::::vote(SystemOrigin::Signed(voter.clone()).into(), last_hash.clone(), p - 1, approve)?; + } + + // caller is prime, prime already votes aye by creating the proposal + System::::set_block_number(T::BlockNumber::max_value()); + assert_eq!(Collective::::proposals().len(), p as usize); + + // Prime aye will close it as approved + }: close(SystemOrigin::Signed(caller), last_hash, p - 1) + verify { + assert_eq!(Collective::::proposals().len(), (p - 1) as usize); + assert_last_event::(RawEvent::Executed(last_hash, false).into()); + } } #[cfg(test)] @@ -199,12 +424,13 @@ mod tests { new_test_ext().execute_with(|| { assert_ok!(test_benchmark_set_members::()); assert_ok!(test_benchmark_execute::()); - assert_ok!(test_benchmark_propose::()); - assert_ok!(test_benchmark_propose_else_branch::()); - assert_ok!(test_benchmark_vote::()); - assert_ok!(test_benchmark_vote_not_approve::()); + assert_ok!(test_benchmark_propose_execute::()); + assert_ok!(test_benchmark_propose_proposed::()); + assert_ok!(test_benchmark_vote_insert::()); + assert_ok!(test_benchmark_vote_disapproved::()); assert_ok!(test_benchmark_vote_approved::()); - assert_ok!(test_benchmark_close::()); + assert_ok!(test_benchmark_close_disapproved::()); + assert_ok!(test_benchmark_close_approved::()); }); } } diff --git a/frame/collective/src/lib.rs b/frame/collective/src/lib.rs index 19a9a12b34bff..cf6247cf00351 100644 --- a/frame/collective/src/lib.rs +++ b/frame/collective/src/lib.rs @@ -65,7 +65,7 @@ pub trait Trait: frame_system::Trait { type Origin: From>; /// The outer call dispatch type. - type Proposal: Parameter + Dispatchable>::Origin> + From>; + type Proposal: Parameter + Dispatchable>::Origin> + From>; /// The outer event type. type Event: From> + Into<::Event>; diff --git a/frame/democracy/src/benchmarking.rs b/frame/democracy/src/benchmarking.rs index 6165a4f8977ed..4459a1aaa5410 100644 --- a/frame/democracy/src/benchmarking.rs +++ b/frame/democracy/src/benchmarking.rs @@ -19,8 +19,8 @@ use super::*; use frame_benchmarking::{benchmarks, account}; -use frame_support::traits::{Currency, Get, EnsureOrigin}; -use frame_system::{RawOrigin, Module as System, self}; +use frame_support::traits::{Currency, Get, EnsureOrigin, OnInitialize}; +use frame_system::{RawOrigin, Module as System, self, EventRecord}; use sp_runtime::traits::{Bounded, One}; use crate::Module as Democracy; @@ -33,6 +33,14 @@ const MAX_SECONDERS: u32 = 100; const MAX_VETOERS: u32 = 100; const MAX_BYTES: u32 = 16_384; +fn assert_last_event(generic_event: ::Event) { + let events = System::::events(); + let system_event: ::Event = generic_event.into(); + // compare to the last event record + let EventRecord { event, .. } = &events[events.len() - 1]; + assert_eq!(event, &system_event); +} + fn funded_account(name: &'static str, index: u32) -> T::AccountId { let caller: T::AccountId = account(name, index, SEED); T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); @@ -54,7 +62,7 @@ fn add_referendum(n: u32) -> Result { let vote_threshold = VoteThreshold::SimpleMajority; Democracy::::inject_referendum( - 0.into(), + T::LaunchPeriod::get(), proposal_hash, vote_threshold, 0.into(), @@ -70,7 +78,7 @@ fn add_referendum(n: u32) -> Result { Ok(referendum_index) } -fn account_vote() -> AccountVote> { +fn account_vote(b: BalanceOf) -> AccountVote> { let v = Vote { aye: true, conviction: Conviction::Locked1x, @@ -78,18 +86,18 @@ fn account_vote() -> AccountVote> { AccountVote::Standard { vote: v, - balance: BalanceOf::::one(), + balance: b, } } -fn open_activate_proxy(u: u32) -> Result { +fn open_activate_proxy(u: u32) -> Result<(T::AccountId, T::AccountId), &'static str> { let caller = funded_account::("caller", u); - let proxy = funded_account::("proxy", u); + let voter = funded_account::("voter", u); - Democracy::::open_proxy(RawOrigin::Signed(proxy.clone()).into(), caller.clone())?; - Democracy::::activate_proxy(RawOrigin::Signed(caller).into(), proxy.clone())?; + Democracy::::open_proxy(RawOrigin::Signed(caller.clone()).into(), voter.clone())?; + Democracy::::activate_proxy(RawOrigin::Signed(voter.clone()).into(), caller.clone())?; - Ok(proxy) + Ok((caller, voter)) } benchmarks! { @@ -103,10 +111,15 @@ benchmarks! { add_proposal::(i)?; } + assert_eq!(Democracy::::public_props().len(), p as usize, "Proposals not created."); + let caller = funded_account::("caller", 0); let proposal_hash: T::Hash = T::Hashing::hash_of(&p); let value = T::MinimumDeposit::get(); }: _(RawOrigin::Signed(caller), proposal_hash, value.into()) + verify { + assert_eq!(Democracy::::public_props().len(), (p + 1) as usize, "Proposals not created."); + } second { let s in 0 .. MAX_SECONDERS; @@ -120,95 +133,225 @@ benchmarks! { Democracy::::second(RawOrigin::Signed(seconder).into(), 0)?; } + let deposits = Democracy::::deposit_of(0).ok_or("Proposal not created")?; + assert_eq!(deposits.1.len(), (s + 1) as usize, "Seconds not recorded"); }: _(RawOrigin::Signed(caller), 0) + verify { + let deposits = Democracy::::deposit_of(0).ok_or("Proposal not created")?; + assert_eq!(deposits.1.len(), (s + 2) as usize, "`second` benchmark did not work"); + } - vote { + vote_new { let r in 1 .. MAX_REFERENDUMS; let caller = funded_account::("caller", 0); - let account_vote = account_vote::(); + let account_vote = account_vote::(100.into()); + // We need to create existing direct votes for i in 0 .. r { let ref_idx = add_referendum::(i)?; Democracy::::vote(RawOrigin::Signed(caller.clone()).into(), ref_idx, account_vote.clone())?; } + let votes = match VotingOf::::get(&caller) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), r as usize, "Votes were not recorded."); + + let referendum_index = add_referendum::(r)?; + + }: vote(RawOrigin::Signed(caller.clone()), referendum_index, account_vote) + verify { + let votes = match VotingOf::::get(&caller) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), (r + 1) as usize, "Vote was not recorded."); + } - let referendum_index = r - 1; + vote_existing { + let r in 1 .. MAX_REFERENDUMS; - }: _(RawOrigin::Signed(caller), referendum_index, account_vote) + let caller = funded_account::("caller", 0); + let account_vote = account_vote::(100.into()); + + // We need to create existing direct votes + for i in 0 ..=r { + let ref_idx = add_referendum::(i)?; + Democracy::::vote(RawOrigin::Signed(caller.clone()).into(), ref_idx, account_vote.clone())?; + } + let votes = match VotingOf::::get(&caller) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), (r + 1) as usize, "Votes were not recorded."); + + // Change vote from aye to nay + let nay = Vote { aye: false, conviction: Conviction::Locked1x }; + let new_vote = AccountVote::Standard { vote: nay, balance: 1000.into() }; + let referendum_index = Democracy::::referendum_count() - 1; + + // This tests when a user changes a vote + }: vote(RawOrigin::Signed(caller.clone()), referendum_index, new_vote) + verify { + let votes = match VotingOf::::get(&caller) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), (r + 1) as usize, "Vote was incorrectly added"); + let referendum_info = Democracy::::referendum_info(referendum_index) + .ok_or("referendum doesn't exist")?; + let tally = match referendum_info { + ReferendumInfo::Ongoing(r) => r.tally, + _ => return Err("referendum not ongoing"), + }; + assert_eq!(tally.nays, 1000.into(), "changed vote was not recorded"); + } - proxy_vote { + // Basically copy paste of `vote_new` + proxy_vote_new { let r in 1 .. MAX_REFERENDUMS; - let caller = funded_account::("caller", r); - let proxy = open_activate_proxy::(r)?; - let account_vote = account_vote::(); + let (caller, voter) = open_activate_proxy::(0)?; + let account_vote = account_vote::(100.into()); + // Populate existing direct votes for the voter, they can vote on their own behalf for i in 0 .. r { let ref_idx = add_referendum::(i)?; - Democracy::::vote(RawOrigin::Signed(caller.clone()).into(), ref_idx, account_vote.clone())?; + Democracy::::vote(RawOrigin::Signed(voter.clone()).into(), ref_idx, account_vote.clone())?; } - let referendum_index = r - 1; + let referendum_index = add_referendum::(r)?; + + }: proxy_vote(RawOrigin::Signed(caller), referendum_index, account_vote) + verify { + let votes = match VotingOf::::get(&voter) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), (r + 1) as usize, "Vote was not recorded."); + } - }: _(RawOrigin::Signed(proxy), referendum_index, account_vote) + // Basically copy paste of `vote_existing` + proxy_vote_existing { + let r in 1 .. MAX_REFERENDUMS; - emergency_cancel { - let u in 1 .. MAX_USERS; + let (caller, voter) = open_activate_proxy::(0)?; + let account_vote = account_vote::(100.into()); - let referendum_index = add_referendum::(u)?; + // We need to create existing direct votes + for i in 0 ..=r { + let ref_idx = add_referendum::(i)?; + Democracy::::vote(RawOrigin::Signed(voter.clone()).into(), ref_idx, account_vote.clone())?; + } + let votes = match VotingOf::::get(&voter) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), (r + 1) as usize, "Votes were not recorded."); + + // Change vote from aye to nay + let nay = Vote { aye: false, conviction: Conviction::Locked1x }; + let new_vote = AccountVote::Standard { vote: nay, balance: 1000.into() }; + let referendum_index = Democracy::::referendum_count() - 1; + + // This tests when a user changes a vote + }: proxy_vote(RawOrigin::Signed(caller.clone()), referendum_index, new_vote) + verify { + let votes = match VotingOf::::get(&voter) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), (r + 1) as usize, "Vote was incorrectly added"); + let referendum_info = Democracy::::referendum_info(referendum_index) + .ok_or("referendum doesn't exist")?; + let tally = match referendum_info { + ReferendumInfo::Ongoing(r) => r.tally, + _ => return Err("referendum not ongoing"), + }; + assert_eq!(tally.nays, 1000.into(), "changed vote was not recorded"); + } + + emergency_cancel { + let r in 1 .. MAX_REFERENDUMS; let origin = T::CancellationOrigin::successful_origin(); + + // Create and cancel a bunch of referendums + for i in 0 .. r { + let ref_idx = add_referendum::(i)?; + let call = Call::::emergency_cancel(ref_idx); + call.dispatch(origin.clone())?; + } + + // Lets now measure one more + let referendum_index = add_referendum::(r)?; let call = Call::::emergency_cancel(referendum_index); - }: { - let _ = call.dispatch(origin)?; + assert!(Democracy::::referendum_status(referendum_index).is_ok()); + }: { call.dispatch(origin)? } + verify { + // Referendum has been canceled + assert!(Democracy::::referendum_status(referendum_index).is_err()); } + // Worst case scenario, we external propose a previously blacklisted proposal external_propose { - let u in 1 .. MAX_USERS; + let p in 1 .. MAX_PROPOSALS; let origin = T::ExternalOrigin::successful_origin(); - let proposal_hash = T::Hashing::hash_of(&u); + let proposal_hash = T::Hashing::hash_of(&p); + // Add proposal to blacklist with block number 0 + Blacklist::::insert( + proposal_hash, + (T::BlockNumber::zero(), vec![T::AccountId::default()]) + ); + let call = Call::::external_propose(proposal_hash); - }: { - let _ = call.dispatch(origin)?; + }: { call.dispatch(origin)? } + verify { + // External proposal created + ensure!(>::exists(), "External proposal didn't work"); } external_propose_majority { - let u in 1 .. MAX_USERS; + let p in 1 .. MAX_PROPOSALS; let origin = T::ExternalMajorityOrigin::successful_origin(); - let proposal_hash = T::Hashing::hash_of(&u); + let proposal_hash = T::Hashing::hash_of(&p); let call = Call::::external_propose_majority(proposal_hash); - - }: { - let _ = call.dispatch(origin)?; + }: { call.dispatch(origin)? } + verify { + // External proposal created + ensure!(>::exists(), "External proposal didn't work"); } external_propose_default { - let u in 1 .. MAX_USERS; + let p in 1 .. MAX_PROPOSALS; let origin = T::ExternalDefaultOrigin::successful_origin(); - let proposal_hash = T::Hashing::hash_of(&u); + let proposal_hash = T::Hashing::hash_of(&p); let call = Call::::external_propose_default(proposal_hash); - - }: { - let _ = call.dispatch(origin)?; + }: { call.dispatch(origin)? } + verify { + // External proposal created + ensure!(>::exists(), "External proposal didn't work"); } fast_track { - let u in 1 .. MAX_USERS; + let p in 1 .. MAX_PROPOSALS; let origin_propose = T::ExternalDefaultOrigin::successful_origin(); - let proposal_hash: T::Hash = T::Hashing::hash_of(&u); + let proposal_hash: T::Hash = T::Hashing::hash_of(&p); Democracy::::external_propose_default(origin_propose, proposal_hash.clone())?; + // NOTE: Instant origin may invoke a little bit more logic, but may not always succeed. let origin_fast_track = T::FastTrackOrigin::successful_origin(); let voting_period = T::FastTrackVotingPeriod::get(); let delay = 0; let call = Call::::fast_track(proposal_hash, voting_period.into(), delay.into()); - }: { - let _ = call.dispatch(origin_fast_track)?; + }: { call.dispatch(origin_fast_track)? } + verify { + assert_eq!(Democracy::::referendum_count(), 1, "referendum not created") } veto_external { @@ -224,33 +367,105 @@ benchmarks! { for i in 0 .. v { vetoers.push(account("vetoer", i, SEED)); } + vetoers.sort(); Blacklist::::insert(proposal_hash, (T::BlockNumber::zero(), vetoers)); let call = Call::::veto_external(proposal_hash); let origin = T::VetoOrigin::successful_origin(); - }: { - let _ = call.dispatch(origin)?; + ensure!(NextExternal::::get().is_some(), "no external proposal"); + }: { call.dispatch(origin)? } + verify { + assert!(NextExternal::::get().is_none()); + let (_, new_vetoers) = >::get(&proposal_hash).ok_or("no blacklist")?; + assert_eq!(new_vetoers.len(), (v + 1) as usize, "vetoers not added"); } cancel_referendum { - let u in 1 .. MAX_USERS; - - let referendum_index = add_referendum::(u)?; + let r in 0 .. MAX_REFERENDUMS; + // Should have no effect on the execution time. + for i in 0..r { + add_referendum::(i)?; + } + let referendum_index = add_referendum::(r)?; }: _(RawOrigin::Root, referendum_index) cancel_queued { - let u in 1 .. MAX_USERS; - - let referendum_index = add_referendum::(u)?; + let r in 1 .. MAX_REFERENDUMS; + // Should have no effect on the execution time. + for i in 0..r { + add_referendum::(i)?; + } + let referendum_index = add_referendum::(r)?; }: _(RawOrigin::Root, referendum_index) - open_proxy { - let u in 1 .. MAX_USERS; + // Note that we have a separate benchmark for `launch_next` + on_initialize_external { + let r in 0 .. MAX_REFERENDUMS; - let caller: T::AccountId = funded_account::("caller", u); - let proxy: T::AccountId = funded_account::("proxy", u); + for i in 0..r { + add_referendum::(i)?; + } - }: _(RawOrigin::Signed(proxy), caller) + assert_eq!(Democracy::::referendum_count(), r, "referenda not created"); + + // Launch external + LastTabledWasExternal::put(false); + + let origin = T::ExternalMajorityOrigin::successful_origin(); + let proposal_hash = T::Hashing::hash_of(&r); + let call = Call::::external_propose_majority(proposal_hash); + call.dispatch(origin)?; + // External proposal created + ensure!(>::exists(), "External proposal didn't work"); + + let block_number = T::LaunchPeriod::get(); + + }: { Democracy::::on_initialize(block_number) } + verify { + // One extra because of next external + assert_eq!(Democracy::::referendum_count(), r + 1, "referenda not created"); + ensure!(!>::exists(), "External wasn't taken"); + + // All but the new next external should be finished + for i in 0 .. r { + if let Some(value) = ReferendumInfoOf::::get(i) { + match value { + ReferendumInfo::Finished { .. } => (), + ReferendumInfo::Ongoing(_) => return Err("Referendum was not finished"), + } + } + } + } + + on_initialize_public { + let r in 1 .. MAX_REFERENDUMS; + + for i in 0..r { + add_referendum::(i)?; + } + + assert_eq!(Democracy::::referendum_count(), r, "referenda not created"); + + // Launch public + LastTabledWasExternal::put(true); + + let block_number = T::LaunchPeriod::get(); + + }: { Democracy::::on_initialize(block_number) } + verify { + // One extra because of next public + assert_eq!(Democracy::::referendum_count(), r + 1, "referenda not created"); + + // All should be finished + for i in 0 .. r { + if let Some(value) = ReferendumInfoOf::::get(i) { + match value { + ReferendumInfo::Finished { .. } => (), + ReferendumInfo::Ongoing(_) => return Err("Referendum was not finished"), + } + } + } + } activate_proxy { let u in 1 .. MAX_USERS; @@ -258,51 +473,120 @@ benchmarks! { let caller: T::AccountId = funded_account::("caller", u); let proxy: T::AccountId = funded_account::("proxy", u); Democracy::::open_proxy(RawOrigin::Signed(proxy.clone()).into(), caller.clone())?; - - }: _(RawOrigin::Signed(caller), proxy) + }: _(RawOrigin::Signed(caller.clone()), proxy.clone()) + verify { + assert_eq!(Democracy::::proxy(proxy), Some(ProxyState::Active(caller))); + } close_proxy { let u in 1 .. MAX_USERS; - - let proxy = open_activate_proxy::(u)?; - - }: _(RawOrigin::Signed(proxy)) + let (caller, _) = open_activate_proxy::(u)?; + }: _(RawOrigin::Signed(caller.clone())) + verify { + assert_eq!(Democracy::::proxy(caller), None); + } deactivate_proxy { let u in 1 .. MAX_USERS; - - let caller = funded_account::("caller", u); - let proxy = open_activate_proxy::(u)?; - - }: _(RawOrigin::Signed(caller), proxy) + let (caller, voter) = open_activate_proxy::(u)?; + }: _(RawOrigin::Signed(voter.clone()), caller.clone()) + verify { + assert_eq!(Democracy::::proxy(caller), Some(ProxyState::Open(voter))); + } delegate { - let u in 1 .. MAX_USERS; + let r in 1 .. MAX_REFERENDUMS; - let caller = funded_account::("caller", u); - let d: T::AccountId = funded_account::("delegate", u); - let balance = 1u32; + let initial_balance: BalanceOf = 100.into(); + let delegated_balance: BalanceOf = 1000.into(); - }: _(RawOrigin::Signed(caller), d.into(), Conviction::Locked1x, balance.into()) + let caller = funded_account::("caller", 0); + // Caller will initially delegate to `old_delegate` + let old_delegate: T::AccountId = funded_account::("old_delegate", r); + Democracy::::delegate( + RawOrigin::Signed(caller.clone()).into(), + old_delegate.clone(), + Conviction::Locked1x, + delegated_balance, + )?; + let (target, balance) = match VotingOf::::get(&caller) { + Voting::Delegating { target, balance, .. } => (target, balance), + _ => return Err("Votes are not direct"), + }; + assert_eq!(target, old_delegate, "delegation target didn't work"); + assert_eq!(balance, delegated_balance, "delegation balance didn't work"); + // Caller will now switch to `new_delegate` + let new_delegate: T::AccountId = funded_account::("new_delegate", r); + let account_vote = account_vote::(initial_balance); + // We need to create existing direct votes for the `new_delegate` + for i in 0..r { + let ref_idx = add_referendum::(i)?; + Democracy::::vote(RawOrigin::Signed(new_delegate.clone()).into(), ref_idx, account_vote.clone())?; + } + let votes = match VotingOf::::get(&new_delegate) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), r as usize, "Votes were not recorded."); + }: _(RawOrigin::Signed(caller.clone()), new_delegate.clone(), Conviction::Locked1x, delegated_balance) + verify { + let (target, balance) = match VotingOf::::get(&caller) { + Voting::Delegating { target, balance, .. } => (target, balance), + _ => return Err("Votes are not direct"), + }; + assert_eq!(target, new_delegate, "delegation target didn't work"); + assert_eq!(balance, delegated_balance, "delegation balance didn't work"); + let delegations = match VotingOf::::get(&new_delegate) { + Voting::Direct { delegations, .. } => delegations, + _ => return Err("Votes are not direct"), + }; + assert_eq!(delegations.capital, delegated_balance, "delegation was not recorded."); + } undelegate { let r in 1 .. MAX_REFERENDUMS; - let other = funded_account::("other", 0); - let account_vote = account_vote::(); + let initial_balance: BalanceOf = 100.into(); + let delegated_balance: BalanceOf = 1000.into(); - for i in 0 .. r { + let caller = funded_account::("caller", 0); + // Caller will delegate + let the_delegate: T::AccountId = funded_account::("delegate", r); + Democracy::::delegate( + RawOrigin::Signed(caller.clone()).into(), + the_delegate.clone(), + Conviction::Locked1x, + delegated_balance, + )?; + let (target, balance) = match VotingOf::::get(&caller) { + Voting::Delegating { target, balance, .. } => (target, balance), + _ => return Err("Votes are not direct"), + }; + assert_eq!(target, the_delegate, "delegation target didn't work"); + assert_eq!(balance, delegated_balance, "delegation balance didn't work"); + // We need to create votes direct votes for the `delegate` + let account_vote = account_vote::(initial_balance); + for i in 0..r { let ref_idx = add_referendum::(i)?; - Democracy::::vote(RawOrigin::Signed(other.clone()).into(), ref_idx, account_vote.clone())?; + Democracy::::vote( + RawOrigin::Signed(the_delegate.clone()).into(), + ref_idx, + account_vote.clone() + )?; } - - let delegator = funded_account::("delegator", r); - let conviction = Conviction::Locked1x; - let balance = 1u32; - - Democracy::::delegate(RawOrigin::Signed(delegator.clone()).into(), other.clone().into(), conviction, balance.into())?; - - }: _(RawOrigin::Signed(delegator)) + let votes = match VotingOf::::get(&the_delegate) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), r as usize, "Votes were not recorded."); + }: _(RawOrigin::Signed(caller.clone())) + verify { + // Voting should now be direct + match VotingOf::::get(&caller) { + Voting::Direct { .. } => (), + _ => return Err("undelegation failed"), + } + } clear_public_proposals { let p in 0 .. MAX_PROPOSALS; @@ -317,133 +601,364 @@ benchmarks! { // Num of bytes in encoded proposal let b in 0 .. MAX_BYTES; - let caller = funded_account::("caller", b); - let encoded_proposal = vec![0; b as usize]; - }: _(RawOrigin::Signed(caller), encoded_proposal) + let caller = funded_account::("caller", 0); + let encoded_proposal = vec![1; b as usize]; + }: _(RawOrigin::Signed(caller), encoded_proposal.clone()) + verify { + let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); + match Preimages::::get(proposal_hash) { + Some(PreimageStatus::Available { .. }) => (), + _ => return Err("preimage not available") + } + } note_imminent_preimage { // Num of bytes in encoded proposal let b in 0 .. MAX_BYTES; // d + 1 to include the one we are testing - let encoded_proposal = vec![0; b as usize]; + let encoded_proposal = vec![1; b as usize]; let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); let block_number = T::BlockNumber::one(); Preimages::::insert(&proposal_hash, PreimageStatus::Missing(block_number)); - let caller = funded_account::("caller", b); - let encoded_proposal = vec![0; b as usize]; - }: _(RawOrigin::Signed(caller), encoded_proposal) + let caller = funded_account::("caller", 0); + let encoded_proposal = vec![1; b as usize]; + }: _(RawOrigin::Signed(caller), encoded_proposal.clone()) + verify { + let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); + match Preimages::::get(proposal_hash) { + Some(PreimageStatus::Available { .. }) => (), + _ => return Err("preimage not available") + } + } reap_preimage { // Num of bytes in encoded proposal let b in 0 .. MAX_BYTES; - let encoded_proposal = vec![0; b as usize]; + let encoded_proposal = vec![1; b as usize]; let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); - let caller = funded_account::("caller", b); - Democracy::::note_preimage(RawOrigin::Signed(caller.clone()).into(), encoded_proposal.clone())?; + let submitter = funded_account::("submitter", b); + Democracy::::note_preimage(RawOrigin::Signed(submitter.clone()).into(), encoded_proposal.clone())?; // We need to set this otherwise we get `Early` error. let block_number = T::VotingPeriod::get() + T::EnactmentPeriod::get() + T::BlockNumber::one(); System::::set_block_number(block_number.into()); - }: _(RawOrigin::Signed(caller), proposal_hash) + assert!(Preimages::::contains_key(proposal_hash)); - unlock { - let u in 1 .. MAX_USERS; + let caller = funded_account::("caller", 0); + }: _(RawOrigin::Signed(caller), proposal_hash.clone()) + verify { + let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); + assert!(!Preimages::::contains_key(proposal_hash)); + } - let caller = funded_account::("caller", u); - let locked_until = T::BlockNumber::zero(); - Locks::::insert(&caller, locked_until); + // Test when unlock will remove locks + unlock_remove { + let r in 1 .. MAX_REFERENDUMS; - T::Currency::extend_lock( - DEMOCRACY_ID, - &caller, - Bounded::max_value(), - WithdrawReason::Transfer.into() - ); + let locker = funded_account::("locker", 0); + // Populate votes so things are locked + let base_balance: BalanceOf = 100.into(); + let small_vote = account_vote::(base_balance); + // Vote and immediately unvote + for i in 0 .. r { + let ref_idx = add_referendum::(i)?; + Democracy::::vote(RawOrigin::Signed(locker.clone()).into(), ref_idx, small_vote.clone())?; + Democracy::::remove_vote(RawOrigin::Signed(locker.clone()).into(), ref_idx)?; + } - let other = caller.clone(); + let caller = funded_account::("caller", 0); + }: unlock(RawOrigin::Signed(caller), locker.clone()) + verify { + // Note that we may want to add a `get_lock` api to actually verify + let voting = VotingOf::::get(&locker); + assert_eq!(voting.locked_balance(), BalanceOf::::zero()); + } - }: _(RawOrigin::Signed(caller), other) + // Test when unlock will set a new value + unlock_set { + let r in 1 .. MAX_REFERENDUMS; + + let locker = funded_account::("locker", 0); + // Populate votes so things are locked + let base_balance: BalanceOf = 100.into(); + let small_vote = account_vote::(base_balance); + for i in 0 .. r { + let ref_idx = add_referendum::(i)?; + Democracy::::vote(RawOrigin::Signed(locker.clone()).into(), ref_idx, small_vote.clone())?; + } + + // Create a big vote so lock increases + let big_vote = account_vote::(base_balance * 10.into()); + let referendum_index = add_referendum::(r)?; + Democracy::::vote(RawOrigin::Signed(locker.clone()).into(), referendum_index, big_vote)?; + + let votes = match VotingOf::::get(&locker) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), (r + 1) as usize, "Votes were not recorded."); + + let voting = VotingOf::::get(&locker); + assert_eq!(voting.locked_balance(), base_balance * 10.into()); + + Democracy::::remove_vote(RawOrigin::Signed(locker.clone()).into(), referendum_index)?; + + let caller = funded_account::("caller", 0); + }: unlock(RawOrigin::Signed(caller), locker.clone()) + verify { + let votes = match VotingOf::::get(&locker) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), r as usize, "Vote was not removed"); + + let voting = VotingOf::::get(&locker); + // Note that we may want to add a `get_lock` api to actually verify + assert_eq!(voting.locked_balance(), base_balance); + } + + open_proxy { + let u in 1 .. MAX_USERS; + + let caller: T::AccountId = funded_account::("caller", u); + let proxy: T::AccountId = funded_account::("proxy", u); + + }: _(RawOrigin::Signed(proxy), caller) remove_vote { let r in 1 .. MAX_REFERENDUMS; let caller = funded_account::("caller", 0); - let account_vote = account_vote::(); + let account_vote = account_vote::(100.into()); for i in 0 .. r { let ref_idx = add_referendum::(i)?; Democracy::::vote(RawOrigin::Signed(caller.clone()).into(), ref_idx, account_vote.clone())?; } + let votes = match VotingOf::::get(&caller) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), r as usize, "Votes not created"); + let referendum_index = r - 1; - }: _(RawOrigin::Signed(caller), referendum_index) + }: _(RawOrigin::Signed(caller.clone()), referendum_index) + verify { + let votes = match VotingOf::::get(&caller) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), (r - 1) as usize, "Vote was not removed"); + } remove_other_vote { let r in 1 .. MAX_REFERENDUMS; let other = funded_account::("other", r); - let account_vote = account_vote::(); + let account_vote = account_vote::(100.into()); for i in 0 .. r { let ref_idx = add_referendum::(i)?; Democracy::::vote(RawOrigin::Signed(other.clone()).into(), ref_idx, account_vote.clone())?; } + let votes = match VotingOf::::get(&other) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), r as usize, "Votes not created"); + let referendum_index = r - 1; ReferendumInfoOf::::insert( referendum_index, ReferendumInfo::Finished { end: T::BlockNumber::zero(), approved: true } ); - let caller = funded_account::("caller", r); + let caller = funded_account::("caller", 0); System::::set_block_number(T::EnactmentPeriod::get() * 10u32.into()); - }: _(RawOrigin::Signed(caller), other, referendum_index) + }: _(RawOrigin::Signed(caller), other.clone(), referendum_index) + verify { + let votes = match VotingOf::::get(&other) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), (r - 1) as usize, "Vote was not removed"); + } + // This is a copy of delegate benchmark, but with `open_activate_proxy` proxy_delegate { - let u in 1 .. MAX_USERS; - - let other: T::AccountId = account("other", u, SEED); - let proxy = open_activate_proxy::(u)?; - let conviction = Conviction::Locked1x; - let balance = 1u32; + let r in 1 .. MAX_REFERENDUMS; - }: _(RawOrigin::Signed(proxy), other, conviction, balance.into()) + let initial_balance: BalanceOf = 100.into(); + let delegated_balance: BalanceOf = 1000.into(); + + let (caller, voter) = open_activate_proxy::(0)?; + + // Voter will initially delegate to `old_delegate` + let old_delegate: T::AccountId = funded_account::("old_delegate", r); + Democracy::::delegate( + RawOrigin::Signed(voter.clone()).into(), + old_delegate.clone(), + Conviction::Locked1x, + delegated_balance, + )?; + let (target, balance) = match VotingOf::::get(&voter) { + Voting::Delegating { target, balance, .. } => (target, balance), + _ => return Err("Votes are not direct"), + }; + assert_eq!(target, old_delegate, "delegation target didn't work"); + assert_eq!(balance, delegated_balance, "delegation balance didn't work"); + // Voter will now switch to `new_delegate` + let new_delegate: T::AccountId = funded_account::("new_delegate", r); + let account_vote = account_vote::(initial_balance); + // We need to create existing direct votes for the `new_delegate` + for i in 0..r { + let ref_idx = add_referendum::(i)?; + Democracy::::vote(RawOrigin::Signed(new_delegate.clone()).into(), ref_idx, account_vote.clone())?; + } + let votes = match VotingOf::::get(&new_delegate) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), r as usize, "Votes were not recorded."); + }: _(RawOrigin::Signed(caller.clone()), new_delegate.clone(), Conviction::Locked1x, delegated_balance) + verify { + let (target, balance) = match VotingOf::::get(&voter) { + Voting::Delegating { target, balance, .. } => (target, balance), + _ => return Err("Votes are not direct"), + }; + assert_eq!(target, new_delegate, "delegation target didn't work"); + assert_eq!(balance, delegated_balance, "delegation balance didn't work"); + let delegations = match VotingOf::::get(&new_delegate) { + Voting::Direct { delegations, .. } => delegations, + _ => return Err("Votes are not direct"), + }; + assert_eq!(delegations.capital, delegated_balance, "delegation was not recorded."); + } + // This is a copy of undelegate benchmark, but with `open_activate_proxy` proxy_undelegate { let r in 1 .. MAX_REFERENDUMS; - let other = funded_account::("other", 0); - let account_vote = account_vote::(); + let initial_balance: BalanceOf = 100.into(); + let delegated_balance: BalanceOf = 1000.into(); + + let (caller, voter) = open_activate_proxy::(0)?; + // Caller will delegate + let the_delegate: T::AccountId = funded_account::("delegate", r); + Democracy::::delegate( + RawOrigin::Signed(voter.clone()).into(), + the_delegate.clone(), + Conviction::Locked1x, + delegated_balance, + )?; + let (target, balance) = match VotingOf::::get(&voter) { + Voting::Delegating { target, balance, .. } => (target, balance), + _ => return Err("Votes are not direct"), + }; + assert_eq!(target, the_delegate, "delegation target didn't work"); + assert_eq!(balance, delegated_balance, "delegation balance didn't work"); + // We need to create votes direct votes for the `delegate` + let account_vote = account_vote::(initial_balance); + for i in 0..r { + let ref_idx = add_referendum::(i)?; + Democracy::::vote( + RawOrigin::Signed(the_delegate.clone()).into(), + ref_idx, + account_vote.clone() + )?; + } + let votes = match VotingOf::::get(&the_delegate) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), r as usize, "Votes were not recorded."); + }: _(RawOrigin::Signed(caller.clone())) + verify { + // Voting should now be direct + match VotingOf::::get(&voter) { + Voting::Direct { .. } => (), + _ => return Err("undelegation failed"), + } + } + + proxy_remove_vote { + let r in 1 .. MAX_REFERENDUMS; + + let (caller, voter) = open_activate_proxy::(0)?; + let account_vote = account_vote::(100.into()); for i in 0 .. r { let ref_idx = add_referendum::(i)?; - Democracy::::vote(RawOrigin::Signed(other.clone()).into(), ref_idx, account_vote.clone())?; + Democracy::::vote(RawOrigin::Signed(voter.clone()).into(), ref_idx, account_vote.clone())?; } - let proxy = open_activate_proxy::(r)?; - let conviction = Conviction::Locked1x; - let balance = 1u32; - Democracy::::proxy_delegate(RawOrigin::Signed(proxy.clone()).into(), other, conviction, balance.into())?; + let votes = match VotingOf::::get(&voter) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), r as usize, "Votes not created"); - }: _(RawOrigin::Signed(proxy)) + let referendum_index = r - 1; - proxy_remove_vote { - let u in 1 .. MAX_USERS; + }: _(RawOrigin::Signed(caller.clone()), referendum_index) + verify { + let votes = match VotingOf::::get(&voter) { + Voting::Direct { votes, .. } => votes, + _ => return Err("Votes are not direct"), + }; + assert_eq!(votes.len(), (r - 1) as usize, "Vote was not removed"); + } - let referendum_index = add_referendum::(u)?; - let account_vote = account_vote::(); - let proxy = open_activate_proxy::(u)?; + enact_proposal_execute { + // Num of bytes in encoded proposal + let b in 0 .. MAX_BYTES; - Democracy::::proxy_vote(RawOrigin::Signed(proxy.clone()).into(), referendum_index, account_vote)?; + let proposer = funded_account::("proposer", 0); + let raw_call = Call::note_preimage(vec![1; b as usize]); + let generic_call: T::Proposal = raw_call.into(); + let encoded_proposal = generic_call.encode(); + let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); + Democracy::::note_preimage(RawOrigin::Signed(proposer).into(), encoded_proposal)?; - }: _(RawOrigin::Signed(proxy), referendum_index) + match Preimages::::get(proposal_hash) { + Some(PreimageStatus::Available { .. }) => (), + _ => return Err("preimage not available") + } + }: enact_proposal(RawOrigin::Root, proposal_hash, 0) + verify { + // Fails due to mismatched origin + assert_last_event::(RawEvent::Executed(0, false).into()); + } + + enact_proposal_slash { + // Num of bytes in encoded proposal + let b in 0 .. MAX_BYTES; + + let proposer = funded_account::("proposer", 0); + // Random invalid bytes + let encoded_proposal = vec![200; b as usize]; + let proposal_hash = T::Hashing::hash(&encoded_proposal[..]); + Democracy::::note_preimage(RawOrigin::Signed(proposer).into(), encoded_proposal)?; + + match Preimages::::get(proposal_hash) { + Some(PreimageStatus::Available { .. }) => (), + _ => return Err("preimage not available") + } + }: { + assert_eq!( + Democracy::::enact_proposal(RawOrigin::Root.into(), proposal_hash, 0), + Err(Error::::PreimageInvalid.into()) + ); + } } #[cfg(test)] @@ -457,8 +972,10 @@ mod tests { new_test_ext().execute_with(|| { assert_ok!(test_benchmark_propose::()); assert_ok!(test_benchmark_second::()); - assert_ok!(test_benchmark_vote::()); - assert_ok!(test_benchmark_proxy_vote::()); + assert_ok!(test_benchmark_vote_new::()); + assert_ok!(test_benchmark_vote_existing::()); + assert_ok!(test_benchmark_proxy_vote_new::()); + assert_ok!(test_benchmark_proxy_vote_existing::()); assert_ok!(test_benchmark_emergency_cancel::()); assert_ok!(test_benchmark_external_propose::()); assert_ok!(test_benchmark_external_propose_majority::()); @@ -467,6 +984,8 @@ mod tests { assert_ok!(test_benchmark_veto_external::()); assert_ok!(test_benchmark_cancel_referendum::()); assert_ok!(test_benchmark_cancel_queued::()); + assert_ok!(test_benchmark_on_initialize_external::()); + assert_ok!(test_benchmark_on_initialize_public::()); assert_ok!(test_benchmark_open_proxy::()); assert_ok!(test_benchmark_activate_proxy::()); assert_ok!(test_benchmark_close_proxy::()); @@ -477,12 +996,15 @@ mod tests { assert_ok!(test_benchmark_note_preimage::()); assert_ok!(test_benchmark_note_imminent_preimage::()); assert_ok!(test_benchmark_reap_preimage::()); - assert_ok!(test_benchmark_unlock::()); + assert_ok!(test_benchmark_unlock_remove::()); + assert_ok!(test_benchmark_unlock_set::()); assert_ok!(test_benchmark_remove_vote::()); assert_ok!(test_benchmark_remove_other_vote::()); assert_ok!(test_benchmark_proxy_delegate::()); assert_ok!(test_benchmark_proxy_undelegate::()); assert_ok!(test_benchmark_proxy_remove_vote::()); + assert_ok!(test_benchmark_enact_proposal_execute::()); + assert_ok!(test_benchmark_enact_proposal_slash::()); }); } } diff --git a/frame/democracy/src/tests.rs b/frame/democracy/src/tests.rs index 4d540f63d5080..31213919e4a78 100644 --- a/frame/democracy/src/tests.rs +++ b/frame/democracy/src/tests.rs @@ -21,7 +21,7 @@ use std::cell::RefCell; use codec::Encode; use frame_support::{ impl_outer_origin, impl_outer_dispatch, assert_noop, assert_ok, parameter_types, - ord_parameter_types, traits::{Contains, OnInitialize}, weights::Weight, + impl_outer_event, ord_parameter_types, traits::{Contains, OnInitialize}, weights::Weight, }; use sp_core::H256; use sp_runtime::{ @@ -53,11 +53,25 @@ impl_outer_origin! { impl_outer_dispatch! { pub enum Call for Test where origin: Origin { + frame_system::System, pallet_balances::Balances, democracy::Democracy, } } +mod democracy { + pub use crate::Event; +} + +impl_outer_event! { + pub enum Event for Test { + system, + pallet_balances, + pallet_scheduler, + democracy, + } +} + // Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted. #[derive(Clone, Eq, PartialEq, Debug)] pub struct Test; @@ -77,7 +91,7 @@ impl frame_system::Trait for Test { type AccountId = u64; type Lookup = IdentityLookup; type Header = Header; - type Event = (); + type Event = Event; type BlockHashCount = BlockHashCount; type MaximumBlockWeight = MaximumBlockWeight; type DbWeight = (); @@ -94,14 +108,14 @@ parameter_types! { pub const MaximumWeight: u32 = 1000000; } impl pallet_scheduler::Trait for Test { - type Event = (); + type Event = Event; type Origin = Origin; type Call = Call; type MaximumWeight = MaximumWeight; } impl pallet_balances::Trait for Test { type Balance = u64; - type Event = (); + type Event = Event; type DustRemoval = (); type ExistentialDeposit = ExistentialDeposit; type AccountStore = System; @@ -144,7 +158,7 @@ impl Get for InstantAllowed { } impl super::Trait for Test { type Proposal = Call; - type Event = (); + type Event = Event; type Currency = pallet_balances::Module; type EnactmentPeriod = EnactmentPeriod; type LaunchPeriod = LaunchPeriod; diff --git a/frame/support/src/traits.rs b/frame/support/src/traits.rs index 35e1231698a46..dfc2ddf4de129 100644 --- a/frame/support/src/traits.rs +++ b/frame/support/src/traits.rs @@ -208,7 +208,7 @@ pub trait Contains { /// /// **Should be used for benchmarking only!!!** #[cfg(feature = "runtime-benchmarks")] - fn add(t: &T) { unimplemented!() } + fn add(_t: &T) { unimplemented!() } } /// Determiner to say whether a given account is unused. diff --git a/frame/timestamp/src/benchmarking.rs b/frame/timestamp/src/benchmarking.rs index 01a3d502a818e..c468bf82fba6f 100644 --- a/frame/timestamp/src/benchmarking.rs +++ b/frame/timestamp/src/benchmarking.rs @@ -21,18 +21,31 @@ use super::*; use sp_std::prelude::*; use frame_system::RawOrigin; +use frame_support::{ensure, traits::OnFinalize}; use frame_benchmarking::benchmarks; +use crate::Module as Timestamp; + const MAX_TIME: u32 = 100; benchmarks! { - _ { - let n in 1 .. MAX_TIME => (); - } + _ { } set { - let n in ...; - }: _(RawOrigin::None, n.into()) + let t in 1 .. MAX_TIME; + }: _(RawOrigin::None, t.into()) + verify { + ensure!(Timestamp::::now() == t.into(), "Time was not set."); + } + + on_finalize { + let t in 1 .. MAX_TIME; + Timestamp::::set(RawOrigin::None.into(), t.into())?; + ensure!(DidUpdate::exists(), "Time was not set."); + }: { Timestamp::::on_finalize(t.into()); } + verify { + ensure!(!DidUpdate::exists(), "Time was not removed."); + } } #[cfg(test)] @@ -45,6 +58,7 @@ mod tests { fn test_benchmarks() { new_test_ext().execute_with(|| { assert_ok!(test_benchmark_set::()); + assert_ok!(test_benchmark_on_finalize::()); }); } } diff --git a/frame/vesting/src/benchmarking.rs b/frame/vesting/src/benchmarking.rs index be2cb4cb2b0aa..10f19af65ee18 100644 --- a/frame/vesting/src/benchmarking.rs +++ b/frame/vesting/src/benchmarking.rs @@ -21,108 +21,177 @@ use super::*; use frame_system::{RawOrigin, Module as System}; -use sp_io::hashing::blake2_256; use frame_benchmarking::{benchmarks, account}; +use sp_runtime::traits::Bounded; use crate::Module as Vesting; const SEED: u32 = 0; const MAX_LOCKS: u32 = 20; -fn add_locks(l: u32) { - for id in 0..l { - let lock_id = <[u8; 8]>::decode(&mut &id.using_encoded(blake2_256)[..]) - .unwrap_or_default(); - let locker = account("locker", 0, SEED); - let locked = 1; +type BalanceOf = <::Currency as Currency<::AccountId>>::Balance; + +fn add_locks(who: &T::AccountId, n: u8) { + for id in 0..n { + let lock_id = [id; 8]; + let locked = 100; let reasons = WithdrawReason::Transfer | WithdrawReason::Reserve; - T::Currency::set_lock(lock_id, &locker, locked.into(), reasons); + T::Currency::set_lock(lock_id, who, locked.into(), reasons); } } -fn setup(b: u32) -> T::AccountId { - let locked = 1; - let per_block = 1; - let starting_block = 0; - - let caller = account("caller", 0, SEED); - System::::set_block_number(0.into()); - - // Add schedule to avoid `NotVesting` error. - let _ = Vesting::::add_vesting_schedule( - &caller, - locked.into(), - per_block.into(), - starting_block.into(), - ); - - // Set lock and block number to take different code paths. - let reasons = WithdrawReason::Transfer | WithdrawReason::Reserve; - T::Currency::set_lock(VESTING_ID, &caller, locked.into(), reasons); - System::::set_block_number(b.into()); - - caller +fn add_vesting_schedule(who: &T::AccountId) -> Result<(), &'static str> { + let locked = 100; + let per_block = 10; + let starting_block = 1; + + System::::set_block_number(0.into()); + + // Add schedule to avoid `NotVesting` error. + Vesting::::add_vesting_schedule( + &who, + locked.into(), + per_block.into(), + starting_block.into(), + )?; + Ok(()) } benchmarks! { - _ { - // Number of previous locks. - // It doesn't seems to influence the timings for lower values. - let l in 0 .. MAX_LOCKS => add_locks::(l); - } + _ { } vest_locked { - let l in ...; - - let caller = setup::(0u32); + let l in 0 .. MAX_LOCKS; - }: vest(RawOrigin::Signed(caller)) - - vest_not_locked { - let l in ...; + let caller = account("caller", 0, SEED); + T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); + add_locks::(&caller, l as u8); + add_vesting_schedule::(&caller)?; + // At block zero, everything is vested. + System::::set_block_number(T::BlockNumber::zero()); + assert_eq!( + Vesting::::vesting_balance(&caller), + Some(100.into()), + "Vesting schedule not added", + ); + }: vest(RawOrigin::Signed(caller.clone())) + verify { + // Nothing happened since everything is still vested. + assert_eq!( + Vesting::::vesting_balance(&caller), + Some(100.into()), + "Vesting schedule was removed", + ); + } - let caller = setup::(1u32); + vest_unlocked { + let l in 0 .. MAX_LOCKS; - }: vest(RawOrigin::Signed(caller)) + let caller = account("caller", 0, SEED); + T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); + add_locks::(&caller, l as u8); + add_vesting_schedule::(&caller)?; + // At block 20, everything is unvested. + System::::set_block_number(20.into()); + assert_eq!( + Vesting::::vesting_balance(&caller), + Some(BalanceOf::::zero()), + "Vesting schedule still active", + ); + }: vest(RawOrigin::Signed(caller.clone())) + verify { + // Vesting schedule is removed! + assert_eq!( + Vesting::::vesting_balance(&caller), + None, + "Vesting schedule was not removed", + ); + } vest_other_locked { - let l in ...; + let l in 0 .. MAX_LOCKS; - let other: T::AccountId = setup::(0u32); + let other: T::AccountId = account("other", 0, SEED); let other_lookup: ::Source = T::Lookup::unlookup(other.clone()); + T::Currency::make_free_balance_be(&other, BalanceOf::::max_value()); + add_locks::(&other, l as u8); + add_vesting_schedule::(&other)?; + // At block zero, everything is vested. + System::::set_block_number(T::BlockNumber::zero()); + assert_eq!( + Vesting::::vesting_balance(&other), + Some(100.into()), + "Vesting schedule not added", + ); - let caller = account("caller", 0, SEED); - - }: vest_other(RawOrigin::Signed(caller), other_lookup) + let caller: T::AccountId = account("caller", 0, SEED); + }: vest_other(RawOrigin::Signed(caller.clone()), other_lookup) + verify { + // Nothing happened since everything is still vested. + assert_eq!( + Vesting::::vesting_balance(&other), + Some(100.into()), + "Vesting schedule was removed", + ); + } - vest_other_not_locked { - let l in ...; + vest_other_unlocked { + let l in 0 .. MAX_LOCKS; - let other: T::AccountId = setup::(1u32); + let other: T::AccountId = account("other", 0, SEED); let other_lookup: ::Source = T::Lookup::unlookup(other.clone()); + T::Currency::make_free_balance_be(&other, BalanceOf::::max_value()); + add_locks::(&other, l as u8); + add_vesting_schedule::(&other)?; + // At block 20, everything is unvested. + System::::set_block_number(20.into()); + assert_eq!( + Vesting::::vesting_balance(&other), + Some(BalanceOf::::zero()), + "Vesting schedule still active", + ); - let caller = account("caller", 0, SEED); - - }: vest_other(RawOrigin::Signed(caller), other_lookup) + let caller: T::AccountId = account("caller", 0, SEED); + }: vest_other(RawOrigin::Signed(caller.clone()), other_lookup) + verify { + // Vesting schedule is removed! + assert_eq!( + Vesting::::vesting_balance(&other), + None, + "Vesting schedule was not removed", + ); + } vested_transfer { - let u in 0 .. 1000; + let l in 0 .. MAX_LOCKS; - let from = account("from", u, SEED); - let to = account("to", u, SEED); - let to_lookup: ::Source = T::Lookup::unlookup(to); + let caller: T::AccountId = account("caller", 0, SEED); + T::Currency::make_free_balance_be(&caller, BalanceOf::::max_value()); + let target: T::AccountId = account("target", 0, SEED); + let target_lookup: ::Source = T::Lookup::unlookup(target.clone()); + // Give target existing locks + add_locks::(&target, l as u8); let transfer_amount = T::MinVestedTransfer::get(); let vesting_schedule = VestingInfo { locked: transfer_amount, - per_block: 1.into(), - starting_block: 0.into(), + per_block: 10.into(), + starting_block: 1.into(), }; - - let _ = T::Currency::make_free_balance_be(&from, transfer_amount * 10.into()); - - }: _(RawOrigin::Signed(from), to_lookup, vesting_schedule) + }: _(RawOrigin::Signed(caller), target_lookup, vesting_schedule) + verify { + assert_eq!( + T::MinVestedTransfer::get(), + T::Currency::free_balance(&target), + "Transfer didn't happen", + ); + assert_eq!( + Vesting::::vesting_balance(&target), + Some(T::MinVestedTransfer::get()), + "Lock not created", + ); + } } #[cfg(test)] @@ -135,9 +204,9 @@ mod tests { fn test_benchmarks() { ExtBuilder::default().existential_deposit(256).build().execute_with(|| { assert_ok!(test_benchmark_vest_locked::()); - assert_ok!(test_benchmark_vest_not_locked::()); + assert_ok!(test_benchmark_vest_unlocked::()); assert_ok!(test_benchmark_vest_other_locked::()); - assert_ok!(test_benchmark_vest_other_not_locked::()); + assert_ok!(test_benchmark_vest_other_unlocked::()); assert_ok!(test_benchmark_vested_transfer::()); }); } diff --git a/utils/frame/benchmarking-cli/src/command.rs b/utils/frame/benchmarking-cli/src/command.rs index 5e35d57cdaee9..e71a4d950702c 100644 --- a/utils/frame/benchmarking-cli/src/command.rs +++ b/utils/frame/benchmarking-cli/src/command.rs @@ -92,6 +92,9 @@ impl BenchmarkCmd { self.repeat, ); + // Skip raw data + analysis if there are no results + if batch.results.len() == 0 { continue } + if self.raw_data { // Print the table header batch.results[0].0.iter().for_each(|param| print!("{:?},", param.0));