Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
update trait ProposalProvider, mock & test for pallet alliance
Browse files Browse the repository at this point in the history
Signed-off-by: muraca <[email protected]>
  • Loading branch information
muraca committed Jun 15, 2023
1 parent d17bf70 commit abbb9ee
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 162 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions frame/alliance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ array-bytes = "4.1"
sp-core-hashing = { version = "9.0.0", default-features = false, path = "../../primitives/core/hashing" }
pallet-balances = { version = "4.0.0-dev", path = "../balances" }
pallet-collective = { version = "4.0.0-dev", path = "../collective" }
pallet-preimage = { version = "4.0.0-dev", path = "../preimage" }

[features]
default = ["std"]
Expand All @@ -54,6 +55,7 @@ std = [
"frame-support/std",
"frame-system/std",
"pallet-identity/std",
"pallet-preimage/std",
]
runtime-benchmarks = [
"array-bytes",
Expand Down
91 changes: 45 additions & 46 deletions frame/alliance/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

//! Alliance pallet benchmarking.

use sp_runtime::traits::{Bounded, Hash, StaticLookup};
use sp_runtime::traits::{Bounded, StaticLookup};
use sp_std::{
cmp,
convert::{TryFrom, TryInto},
Expand All @@ -26,7 +26,10 @@ use sp_std::{
};

use frame_benchmarking::v1::{account, benchmarks_instance_pallet, BenchmarkError};
use frame_support::traits::{EnsureOrigin, Get, UnfilteredDispatchable};
use frame_support::{
bounded_vec,
traits::{EnsureOrigin, Get, UnfilteredDispatchable},
};
use frame_system::{Pallet as System, RawOrigin as SystemOrigin};

use super::{Call as AllianceCall, Pallet as Alliance, *};
Expand Down Expand Up @@ -116,7 +119,7 @@ benchmarks_instance_pallet! {
let threshold = m;
// Add previous proposals.
for i in 0 .. p - 1 {
// Proposals should be different so that different proposal hashes are generated
// Proposals should be different
let proposal: T::Proposal = AllianceCall::<T, I>::set_rule {
rule: rule(vec![i as u8; b as usize])
}.into();
Expand All @@ -133,8 +136,8 @@ benchmarks_instance_pallet! {
}: propose(SystemOrigin::Signed(proposer.clone()), threshold, Box::new(proposal.clone()), bytes_in_storage)
verify {
// New proposal is recorded
let proposal_hash = T::Hashing::hash_of(&proposal);
assert_eq!(T::ProposalProvider::proposal_of(proposal_hash), Some(proposal));
let proposal_bounded = T::ProposalProvider::bound_proposal(proposal.clone()).unwrap();
assert_eq!(T::ProposalProvider::proposal_of(proposal_bounded), Some(proposal));
}

vote {
Expand All @@ -161,9 +164,9 @@ benchmarks_instance_pallet! {
let threshold = m - 1;

// Add previous proposals
let mut last_hash = T::Hash::default();
let mut last_bound = frame_support::traits::Bounded::Inline(bounded_vec![]);
for i in 0 .. p {
// Proposals should be different so that different proposal hashes are generated
// Proposals should be different
let proposal: T::Proposal = AllianceCall::<T, I>::set_rule {
rule: rule(vec![i as u8; b as usize])
}.into();
Expand All @@ -173,7 +176,7 @@ benchmarks_instance_pallet! {
Box::new(proposal.clone()),
b,
)?;
last_hash = T::Hashing::hash_of(&proposal);
last_bound = T::ProposalProvider::bound_proposal(proposal).unwrap();
}

let index = p - 1;
Expand All @@ -182,7 +185,7 @@ benchmarks_instance_pallet! {
let voter = &members[j as usize];
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
true,
)?;
Expand All @@ -192,7 +195,7 @@ benchmarks_instance_pallet! {
// Voter votes aye without resolving the vote.
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
true,
)?;
Expand All @@ -203,7 +206,7 @@ benchmarks_instance_pallet! {
// Whitelist voter account from further DB operations.
let voter_key = frame_system::Account::<T>::hashed_key_for(&voter);
frame_benchmarking::benchmarking::add_to_whitelist(voter_key.into());
}: _(SystemOrigin::Signed(voter), last_hash.clone(), index, approve)
}: _(SystemOrigin::Signed(voter), last_bound.clone(), index, approve)
verify {
}

Expand Down Expand Up @@ -233,9 +236,9 @@ benchmarks_instance_pallet! {
let threshold = m;

// Add previous proposals
let mut last_hash = T::Hash::default();
let mut last_bound = frame_support::traits::Bounded::Inline(bounded_vec![]);
for i in 0 .. p {
// Proposals should be different so that different proposal hashes are generated
// Proposals should be different
let proposal: T::Proposal = AllianceCall::<T, I>::set_rule {
rule: rule(vec![i as u8; bytes as usize])
}.into();
Expand All @@ -245,8 +248,7 @@ benchmarks_instance_pallet! {
Box::new(proposal.clone()),
bytes_in_storage,
)?;
last_hash = T::Hashing::hash_of(&proposal);
assert_eq!(T::ProposalProvider::proposal_of(last_hash), Some(proposal));
last_bound = T::ProposalProvider::bound_proposal(proposal).unwrap();
}

let index = p - 1;
Expand All @@ -255,7 +257,7 @@ benchmarks_instance_pallet! {
let voter = &members[j as usize];
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
true,
)?;
Expand All @@ -264,26 +266,26 @@ benchmarks_instance_pallet! {
// Voter votes aye without resolving the vote.
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
true,
)?;

// Voter switches vote to nay, which kills the vote
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
false,
)?;

// Whitelist voter account from further DB operations.
let voter_key = frame_system::Account::<T>::hashed_key_for(&voter);
frame_benchmarking::benchmarking::add_to_whitelist(voter_key.into());
}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::MAX, bytes_in_storage)
}: close(SystemOrigin::Signed(voter), last_bound.clone(), index, Weight::MAX, bytes_in_storage)
verify {
// The last proposal is removed.
assert_eq!(T::ProposalProvider::proposal_of(last_hash), None);
assert_eq!(T::ProposalProvider::proposal_of(last_bound), None);
}

close_early_approved {
Expand Down Expand Up @@ -312,9 +314,9 @@ benchmarks_instance_pallet! {
let threshold = 2;

// Add previous proposals
let mut last_hash = T::Hash::default();
let mut last_bound = frame_support::traits::Bounded::Inline(bounded_vec![]);
for i in 0 .. p {
// Proposals should be different so that different proposal hashes are generated
// Proposals should be different
let proposal: T::Proposal = AllianceCall::<T, I>::set_rule {
rule: rule(vec![i as u8; b as usize])
}.into();
Expand All @@ -324,15 +326,14 @@ benchmarks_instance_pallet! {
Box::new(proposal.clone()),
bytes_in_storage,
)?;
last_hash = T::Hashing::hash_of(&proposal);
assert_eq!(T::ProposalProvider::proposal_of(last_hash), Some(proposal));
last_bound = T::ProposalProvider::bound_proposal(proposal).unwrap();
}

let index = p - 1;
// Caller switches vote to nay on their own proposal, allowing them to be the deciding approval vote
Alliance::<T, I>::vote(
SystemOrigin::Signed(proposer.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
false,
)?;
Expand All @@ -342,7 +343,7 @@ benchmarks_instance_pallet! {
let voter = &members[j as usize];
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
false,
)?;
Expand All @@ -351,7 +352,7 @@ benchmarks_instance_pallet! {
// Member zero is the first aye
Alliance::<T, I>::vote(
SystemOrigin::Signed(members[0].clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
true,
)?;
Expand All @@ -360,14 +361,14 @@ benchmarks_instance_pallet! {
// Caller switches vote to aye, which passes the vote
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
true,
)?;
}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::MAX, bytes_in_storage)
}: close(SystemOrigin::Signed(voter), last_bound.clone(), index, Weight::MAX, bytes_in_storage)
verify {
// The last proposal is removed.
assert_eq!(T::ProposalProvider::proposal_of(last_hash), None);
assert_eq!(T::ProposalProvider::proposal_of(last_bound), None);
}

close_disapproved {
Expand Down Expand Up @@ -396,9 +397,9 @@ benchmarks_instance_pallet! {
let threshold = m - 1;

// Add proposals
let mut last_hash = T::Hash::default();
let mut last_bound = frame_support::traits::Bounded::Inline(bounded_vec![]);
for i in 0 .. p {
// Proposals should be different so that different proposal hashes are generated
// Proposals should be different
let proposal: T::Proposal = AllianceCall::<T, I>::set_rule {
rule: rule(vec![i as u8; bytes as usize])
}.into();
Expand All @@ -408,8 +409,7 @@ benchmarks_instance_pallet! {
Box::new(proposal.clone()),
bytes_in_storage,
)?;
last_hash = T::Hashing::hash_of(&proposal);
assert_eq!(T::ProposalProvider::proposal_of(last_hash), Some(proposal));
last_bound = T::ProposalProvider::bound_proposal(proposal).unwrap();
}

let index = p - 1;
Expand All @@ -419,25 +419,25 @@ benchmarks_instance_pallet! {
let voter = &members[j as usize];
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
true,
)?;
}

Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
false,
)?;

System::<T>::set_block_number(T::BlockNumber::max_value());

}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::MAX, bytes_in_storage)
}: close(SystemOrigin::Signed(voter), last_bound.clone(), index, Weight::MAX, bytes_in_storage)
verify {
// The last proposal is removed.
assert_eq!(T::ProposalProvider::proposal_of(last_hash), None);
assert_eq!(T::ProposalProvider::proposal_of(last_bound), None);
}

close_approved {
Expand Down Expand Up @@ -466,9 +466,9 @@ benchmarks_instance_pallet! {
let threshold = 2;

// Add proposals
let mut last_hash = T::Hash::default();
let mut last_bound = frame_support::traits::Bounded::Inline(bounded_vec![]);
for i in 0 .. p {
// Proposals should be different so that different proposal hashes are generated
// Proposals should be different
let proposal: T::Proposal = AllianceCall::<T, I>::set_rule {
rule: rule(vec![i as u8; b as usize])
}.into();
Expand All @@ -478,14 +478,13 @@ benchmarks_instance_pallet! {
Box::new(proposal.clone()),
bytes_in_storage,
)?;
last_hash = T::Hashing::hash_of(&proposal);
assert_eq!(T::ProposalProvider::proposal_of(last_hash), Some(proposal));
last_bound = T::ProposalProvider::bound_proposal(proposal).unwrap();
}

// The prime member votes aye, so abstentions default to aye.
Alliance::<T, I>::vote(
SystemOrigin::Signed(proposer.clone()).into(),
last_hash.clone(),
last_bound.clone(),
p - 1,
true // Vote aye.
)?;
Expand All @@ -497,7 +496,7 @@ benchmarks_instance_pallet! {
let voter = &members[j as usize];
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
last_bound.clone(),
index,
false
)?;
Expand All @@ -506,10 +505,10 @@ benchmarks_instance_pallet! {
// caller is prime, prime already votes aye by creating the proposal
System::<T>::set_block_number(T::BlockNumber::max_value());

}: close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::MAX, bytes_in_storage)
}: close(SystemOrigin::Signed(voter), last_bound.clone(), index, Weight::MAX, bytes_in_storage)
verify {
// The last proposal is removed.
assert_eq!(T::ProposalProvider::proposal_of(last_hash), None);
assert_eq!(T::ProposalProvider::proposal_of(last_bound), None);
}

init_members {
Expand Down
Loading

0 comments on commit abbb9ee

Please sign in to comment.