Skip to content

Commit

Permalink
add missing benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmovses committed Oct 13, 2023
1 parent 9aa362d commit 47e2bd7
Showing 1 changed file with 129 additions and 1 deletion.
130 changes: 129 additions & 1 deletion substrate/frame/alliance/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,137 @@ mod benchmarks {
Ok(())
}

// We choose 5 fellows as a minimum so we always trigger a vote in the voting loop (`for j in ...`)
#[benchmark]
fn close_disapproved(
m: Linear<2, { T::MaxFellows::get() }>,
p: Linear<1, { T::MaxProposals::get() }>,
) -> Result<(), BenchmarkError> {
let bytes = 100;
let bytes_in_storage = bytes + size_of::<Cid>() as u32 + 32;

// Construct `members`.
let fellows = (0 .. m).map(fellow::<T, I>).collect::<Vec<_>>();

let members = fellows.clone();

Alliance::<T, I>::init_members(
SystemOrigin::Root.into(),
fellows,
vec![],
)?;

let proposer = members[0].clone();
let voter = members[1].clone();

// Threshold is one less than total members so that two nays will disapprove the vote
let threshold = m - 1;

// 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 = AllianceCall::<T, I>::set_rule {
rule: rule(vec![i as u8; bytes as usize])
}.into();
Alliance::<T, I>::propose(
SystemOrigin::Signed(proposer.clone()).into(),
threshold,
Box::new(proposal.clone()),
bytes_in_storage,
)?;
last_hash = T::Hashing::hash_of(&proposal);
assert_eq!(T::ProposalProvider::proposal_of(last_hash), Some(proposal));
}

let index = p - 1;
// Have almost 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 - 1 {
let voter = &members[j as usize];
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
index,
true,
)?;
}

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

System::<T>::set_block_number(BlockNumberFor::<T>::max_value());et bytes = 100;
let bytes_in_storage = bytes + size_of::<Cid>() as u32 + 32;

// Construct `members`.
let fellows = (0 .. m).map(fellow::<T, I>).collect::<Vec<_>>();

let members = fellows.clone();

Alliance::<T, I>::init_members(
SystemOrigin::Root.into(),
fellows,
vec![],
)?;

let proposer = members[0].clone();
let voter = members[1].clone();

// Threshold is one less than total members so that two nays will disapprove the vote
let threshold = m - 1;

// 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 = AllianceCall::<T, I>::set_rule {
rule: rule(vec![i as u8; bytes as usize])
}.into();
Alliance::<T, I>::propose(
SystemOrigin::Signed(proposer.clone()).into(),
threshold,
Box::new(proposal.clone()),
bytes_in_storage,
)?;
last_hash = T::Hashing::hash_of(&proposal);
assert_eq!(T::ProposalProvider::proposal_of(last_hash), Some(proposal));
}

let index = p - 1;
// Have almost 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 - 1 {
let voter = &members[j as usize];
Alliance::<T, I>::vote(
SystemOrigin::Signed(voter.clone()).into(),
last_hash.clone(),
index,
true,
)?;
}

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

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

#[extrinsic_call]
close(SystemOrigin::Signed(voter), last_hash.clone(), index, Weight::MAX, bytes_in_storage);

// The last proposal is removed.
assert_eq!(T::ProposalProvider::proposal_of(last_hash), None);
}

// We choose 5 fellows as a minimum so we always trigger a vote in the voting loop (`for j in ...`)
#[benchmark]
fn close_approved(
b: Linear<1, MAX_BYTES>,
m: Linear<5, { T::MaxFellows::get() }>,
p: Linear<1, { T::MaxProposals::get() }>,
Expand Down

0 comments on commit 47e2bd7

Please sign in to comment.