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

Adds remove_approval feature to treasury pallet #11243

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions frame/treasury/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,17 @@ benchmarks_instance_pallet! {
let proposal_id = Treasury::<T, _>::proposal_count() - 1;
}: _(RawOrigin::Root, proposal_id)

remove_approval {
let (caller, value, beneficiary_lookup) = setup_proposal::<T, _>(SEED);
Treasury::<T, _>::propose_spend(
RawOrigin::Signed(caller).into(),
value,
beneficiary_lookup
)?;
let proposal_id = Treasury::<T, _>::proposal_count() - 1;
Treasury::<T, I>::approve_proposal(RawOrigin::Root.into(), proposal_id)?;
}: _(RawOrigin::Root, proposal_id)

on_initialize_proposals {
let p in 0 .. T::MaxApprovals::get();
setup_pot_account::<T, _>();
Expand Down
29 changes: 29 additions & 0 deletions frame/treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ pub mod pallet {
InvalidIndex,
/// Too many approvals in the queue.
TooManyApprovals,
/// Proposal not in approval queue.
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
ProposalNotApproved,
}

#[pallet::hooks]
Expand Down Expand Up @@ -393,6 +395,33 @@ pub mod pallet {
.map_err(|_| Error::<T, I>::TooManyApprovals)?;
Ok(())
}

/// Force a previously approved proposal to be removed from the approval queue.
///
/// May only be called from `T::RejectOrigin`.
///
KiChjang marked this conversation as resolved.
Show resolved Hide resolved
/// # <weight>
/// - Complexity: O(A) where `A` is the number of approvals
/// - Db reads and writes: `Approvals`
/// # </weight>
#[pallet::weight((T::WeightInfo::remove_approval(), DispatchClass::Operational))]
pub fn remove_approval(
origin: OriginFor<T>,
#[pallet::compact] proposal_id: ProposalIndex,
) -> DispatchResult {
T::RejectOrigin::ensure_origin(origin)?;

Approvals::<T, I>::try_mutate(|v| -> DispatchResult {
if let Some(index) = v.iter().position(|x| x == &proposal_id) {
v.remove(index);
Ok(())
} else {
Err(Error::<T, I>::ProposalNotApproved.into())
}
})?;

Ok(())
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions frame/treasury/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,18 @@ fn max_approvals_limited() {
);
});
}

#[test]
fn remove_already_removed_approval_fails() {
new_test_ext().execute_with(|| {
Balances::make_free_balance_be(&Treasury::account_id(), 101);

assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
assert_ok!(Treasury::approve_proposal(Origin::root(), 0));
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
assert_ok!(Treasury::remove_approval(Origin::root(), 0));
assert_noop!(
Treasury::remove_approval(Origin::root(), 0),
Error::<Test, _>::ProposalNotApproved
);
});
}
50 changes: 31 additions & 19 deletions frame/treasury/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
//! Autogenerated weights for pallet_treasury
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-01-31, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2022-03-07, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

// Executed Command:
// ./target/production/substrate
// ./target/release/substrate
// benchmark
// --chain=dev
// --steps=50
Expand All @@ -35,7 +35,6 @@
// --output=./frame/treasury/src/weights.rs
// --template=.maintain/frame-weight-template.hbs
// --header=HEADER-APACHE2
// --raw

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
Expand All @@ -49,6 +48,7 @@ pub trait WeightInfo {
fn propose_spend() -> Weight;
fn reject_proposal() -> Weight;
fn approve_proposal(p: u32, ) -> Weight;
fn remove_approval() -> Weight;
fn on_initialize_proposals(p: u32, ) -> Weight;
}

Expand All @@ -58,34 +58,40 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: Treasury ProposalCount (r:1 w:1)
// Storage: Treasury Proposals (r:0 w:1)
fn propose_spend() -> Weight {
(21_673_000 as Weight)
(36_900_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Treasury Proposals (r:1 w:1)
// Storage: System Account (r:1 w:1)
fn reject_proposal() -> Weight {
(25_353_000 as Weight)
(44_600_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
}
// Storage: Treasury Proposals (r:1 w:0)
// Storage: Treasury Approvals (r:1 w:1)
fn approve_proposal(p: u32, ) -> Weight {
(8_164_000 as Weight)
// Standard Error: 1_000
.saturating_add((57_000 as Weight).saturating_mul(p as Weight))
(14_213_000 as Weight)
// Standard Error: 2_000
.saturating_add((137_000 as Weight).saturating_mul(p as Weight))
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Treasury Approvals (r:1 w:1)
fn remove_approval() -> Weight {
(7_600_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
// Storage: Treasury Approvals (r:1 w:1)
// Storage: Bounties BountyApprovals (r:1 w:1)
// Storage: Treasury Proposals (r:2 w:2)
// Storage: System Account (r:4 w:4)
fn on_initialize_proposals(p: u32, ) -> Weight {
(20_762_000 as Weight)
// Standard Error: 21_000
.saturating_add((26_835_000 as Weight).saturating_mul(p as Weight))
(0 as Weight)
// Standard Error: 245_000
.saturating_add((57_786_000 as Weight).saturating_mul(p as Weight))
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().reads((3 as Weight).saturating_mul(p as Weight)))
.saturating_add(T::DbWeight::get().writes(2 as Weight))
Expand All @@ -98,34 +104,40 @@ impl WeightInfo for () {
// Storage: Treasury ProposalCount (r:1 w:1)
// Storage: Treasury Proposals (r:0 w:1)
fn propose_spend() -> Weight {
(21_673_000 as Weight)
(36_900_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Treasury Proposals (r:1 w:1)
// Storage: System Account (r:1 w:1)
fn reject_proposal() -> Weight {
(25_353_000 as Weight)
(44_600_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
}
// Storage: Treasury Proposals (r:1 w:0)
// Storage: Treasury Approvals (r:1 w:1)
fn approve_proposal(p: u32, ) -> Weight {
(8_164_000 as Weight)
// Standard Error: 1_000
.saturating_add((57_000 as Weight).saturating_mul(p as Weight))
(14_213_000 as Weight)
// Standard Error: 2_000
.saturating_add((137_000 as Weight).saturating_mul(p as Weight))
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Treasury Approvals (r:1 w:1)
fn remove_approval() -> Weight {
(7_600_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(1 as Weight))
.saturating_add(RocksDbWeight::get().writes(1 as Weight))
}
// Storage: Treasury Approvals (r:1 w:1)
// Storage: Bounties BountyApprovals (r:1 w:1)
// Storage: Treasury Proposals (r:2 w:2)
// Storage: System Account (r:4 w:4)
fn on_initialize_proposals(p: u32, ) -> Weight {
(20_762_000 as Weight)
// Standard Error: 21_000
.saturating_add((26_835_000 as Weight).saturating_mul(p as Weight))
(0 as Weight)
// Standard Error: 245_000
.saturating_add((57_786_000 as Weight).saturating_mul(p as Weight))
.saturating_add(RocksDbWeight::get().reads(2 as Weight))
.saturating_add(RocksDbWeight::get().reads((3 as Weight).saturating_mul(p as Weight)))
.saturating_add(RocksDbWeight::get().writes(2 as Weight))
Expand Down