Skip to content

Commit

Permalink
fix treasury benchmarks when no SpendOrigin
Browse files Browse the repository at this point in the history
  • Loading branch information
bgallois committed Jan 24, 2024
1 parent a78ff7d commit 20c2e85
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
17 changes: 14 additions & 3 deletions substrate/frame/treasury/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ mod benchmarks {
Ok(())
}

// This benchmark is short-circuited if `SpendOrigin` cannot provide
// a successful origin, in which case `spend` is un-callable and can use weight=0.
#[benchmark]
fn spend() -> Result<(), BenchmarkError> {
let origin =
Expand Down Expand Up @@ -248,9 +250,12 @@ mod benchmarks {
Ok(())
}

// This benchmark is short-circuited if `SpendOrigin` cannot provide
// a successful origin, in which case there is no spend to payout.
#[benchmark]
fn payout() -> Result<(), BenchmarkError> {
let origin = T::SpendOrigin::try_successful_origin().map_err(|_| "No origin")?;
let origin =
T::SpendOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let (asset_kind, amount, beneficiary, beneficiary_lookup) =
create_spend_arguments::<T, _>(SEED);
T::BalanceConverter::ensure_successful(asset_kind.clone());
Expand Down Expand Up @@ -279,9 +284,12 @@ mod benchmarks {
Ok(())
}

// This benchmark is short-circuited if `SpendOrigin` cannot provide
// a successful origin, in which case there is no spend to check.
#[benchmark]
fn check_status() -> Result<(), BenchmarkError> {
let origin = T::SpendOrigin::try_successful_origin().map_err(|_| "No origin")?;
let origin =
T::SpendOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let (asset_kind, amount, beneficiary, beneficiary_lookup) =
create_spend_arguments::<T, _>(SEED);
T::BalanceConverter::ensure_successful(asset_kind.clone());
Expand Down Expand Up @@ -311,9 +319,12 @@ mod benchmarks {
Ok(())
}

// This benchmark is short-circuited if `SpendOrigin` cannot provide
// a successful origin, in which case there is no spend to void.
#[benchmark]
fn void_spend() -> Result<(), BenchmarkError> {
let origin = T::SpendOrigin::try_successful_origin().map_err(|_| "No origin")?;
let origin =
T::SpendOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
let (asset_kind, amount, _, beneficiary_lookup) = create_spend_arguments::<T, _>(SEED);
T::BalanceConverter::ensure_successful(asset_kind.clone());
Treasury::<T, _>::spend(
Expand Down
3 changes: 3 additions & 0 deletions substrate/frame/treasury/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,7 @@ pub mod pallet {
#[pallet::call_index(6)]
#[pallet::weight(T::WeightInfo::payout())]
pub fn payout(origin: OriginFor<T>, index: SpendIndex) -> DispatchResult {
T::SpendOrigin::ensure_origin(origin.clone())?;
ensure_signed(origin)?;
let mut spend = Spends::<T, I>::get(index).ok_or(Error::<T, I>::InvalidIndex)?;
let now = frame_system::Pallet::<T>::block_number();
Expand Down Expand Up @@ -856,6 +857,7 @@ pub mod pallet {
use PaymentState as State;
use PaymentStatus as Status;

T::SpendOrigin::ensure_origin(origin.clone())?;
ensure_signed(origin)?;
let mut spend = Spends::<T, I>::get(index).ok_or(Error::<T, I>::InvalidIndex)?;
let now = frame_system::Pallet::<T>::block_number();
Expand Down Expand Up @@ -907,6 +909,7 @@ pub mod pallet {
#[pallet::call_index(8)]
#[pallet::weight(T::WeightInfo::void_spend())]
pub fn void_spend(origin: OriginFor<T>, index: SpendIndex) -> DispatchResult {
T::SpendOrigin::ensure_origin(origin.clone())?;
T::RejectOrigin::ensure_origin(origin)?;
let spend = Spends::<T, I>::get(index).ok_or(Error::<T, I>::InvalidIndex)?;
ensure!(
Expand Down

0 comments on commit 20c2e85

Please sign in to comment.