Skip to content

Commit

Permalink
Add function to clear unapproved proposals treasury from pallet
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidK committed Oct 1, 2024
1 parent 8279d10 commit 547fc0d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 6 deletions.
15 changes: 15 additions & 0 deletions substrate/frame/treasury/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,21 @@ mod benchmarks {
Ok(())
}

#[benchmark]
fn release_proposal_bonds() -> Result<(), BenchmarkError> {
let (_, _value, _beneficiary_lookup) = setup_proposal::<T, _>(SEED);
let origin =
T::RejectOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;

#[block]
{
let _ = Treasury::<T, _>::release_proposal_bonds(origin as T::RuntimeOrigin);
}

assert!(Spends::<T, I>::get(0).is_none());
Ok(())
}

impl_benchmark_test_suite!(
Treasury,
crate::tests::ExtBuilder::default().build(),
Expand Down
60 changes: 54 additions & 6 deletions substrate/frame/treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,10 @@ extern crate alloc;
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

use alloc::{boxed::Box, collections::btree_map::BTreeMap};
use alloc::{
boxed::Box,
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
};
use sp_runtime::{
traits::{AccountIdConversion, CheckedAdd, Saturating, StaticLookup, Zero},
Permill, RuntimeDebug,
Expand Down Expand Up @@ -379,6 +382,12 @@ pub mod pallet {
/// A spend was processed and removed from the storage. It might have been successfully
/// paid or it may have expired.
SpendProcessed { index: SpendIndex },
/// An approved spend was voided.
ProposalBondReleased {
proposal_index: ProposalIndex,
amount: BalanceOf<T, I>,
account: T::AccountId,
},
}

/// Error for the treasury pallet.
Expand Down Expand Up @@ -407,6 +416,8 @@ pub mod pallet {
NotAttempted,
/// The payment has neither failed nor succeeded yet.
Inconclusive,
/// No proposals were released
NoProposalsReleased,
}

#[pallet::hooks]
Expand Down Expand Up @@ -777,6 +788,48 @@ pub mod pallet {
Self::deposit_event(Event::<T, I>::AssetSpendVoided { index });
Ok(())
}

/// ## Dispatch Origin
///
/// Any user with account
///
/// ## Details
///
/// Releases any proposals that didn't make it into Approval yet
///
/// ## Events
///
/// Emits [`Event::ProposalBondReleased`] if successful.
#[pallet::call_index(9)]
#[pallet::weight(T::WeightInfo::release_proposal_bonds())]
pub fn release_proposal_bonds(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let _who = ensure_signed(origin)?;

let mut approval_index = BTreeSet::new();
for approval in Approvals::<T, I>::get().iter() {
approval_index.insert(*approval);
}

let mut proposals_released = 0;
for (proposal_index, p) in Proposals::<T, I>::iter() {
if !approval_index.contains(&proposal_index) {
let err_amount = T::Currency::unreserve(&p.proposer, p.bond);
debug_assert!(err_amount.is_zero());
Proposals::<T, I>::remove(proposal_index);
proposals_released += 1;
Self::deposit_event(Event::<T, I>::ProposalBondReleased {
proposal_index,
amount: p.bond,
account: p.proposer,
});
}
}

// we don't want people spamming this function if it doesn't do any work
ensure!(proposals_released > 0, Error::<T, I>::NoProposalsReleased);

Ok(frame_support::dispatch::Pays::No.into())
}
}
}

Expand All @@ -796,11 +849,6 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
ProposalCount::<T, I>::get()
}

/// Public function to proposals storage.
pub fn proposals(index: ProposalIndex) -> Option<Proposal<T::AccountId, BalanceOf<T, I>>> {
Proposals::<T, I>::get(index)
}

/// Public function to approvals storage.
pub fn approvals() -> BoundedVec<ProposalIndex, T::MaxApprovals> {
Approvals::<T, I>::get()
Expand Down
15 changes: 15 additions & 0 deletions substrate/frame/treasury/src/weights.rs

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

0 comments on commit 547fc0d

Please sign in to comment.