Contracts: Refactor API to use WeightMeter (#2943)

Update the Contracts API to use `WeightMeter`, as it simplifies the code
and makes it easier to reason about, rather than taking a mutable weight
or returning a tuple with the weight consumed

---------

Co-authored-by: Alexander Theißen <alex.theissen@me.com>
This commit is contained in:
PG Herveou
2024-04-09 12:22:54 +02:00
committed by GitHub
parent 10ed76437f
commit b6231c79ca
12 changed files with 177 additions and 152 deletions
@@ -23,11 +23,10 @@ use crate::{
weights::WeightInfo,
Config, Pallet, TrieId, Weight, LOG_TARGET,
};
use codec::{Decode, Encode};
use frame_support::{pallet_prelude::*, storage_alias, weights::WeightMeter, DefaultNoBound};
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;
use codec::{Decode, Encode};
use frame_support::{pallet_prelude::*, storage_alias, DefaultNoBound};
use sp_std::{marker::PhantomData, prelude::*};
mod v10 {
use super::*;
@@ -79,9 +78,10 @@ impl<T: Config> MigrationStep for Migration<T> {
T::WeightInfo::v11_migration_step(128)
}
fn step(&mut self) -> (IsFinished, Weight) {
fn step(&mut self, meter: &mut WeightMeter) -> IsFinished {
let Some(old_queue) = v10::DeletionQueue::<T>::take() else {
return (IsFinished::Yes, Weight::zero())
meter.consume(T::WeightInfo::v11_migration_step(0));
return IsFinished::Yes
};
let len = old_queue.len();
@@ -101,7 +101,8 @@ impl<T: Config> MigrationStep for Migration<T> {
<DeletionQueueCounter<T>>::set(queue);
}
(IsFinished::Yes, T::WeightInfo::v11_migration_step(len as u32))
meter.consume(T::WeightInfo::v11_migration_step(len as u32));
IsFinished::Yes
}
#[cfg(feature = "try-runtime")]