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,7 +23,9 @@ use crate::{
CodeHash, Config, Determinism, Pallet, Weight, LOG_TARGET,
};
use codec::{Decode, Encode};
use frame_support::{pallet_prelude::*, storage_alias, DefaultNoBound, Identity};
use frame_support::{
pallet_prelude::*, storage_alias, weights::WeightMeter, DefaultNoBound, Identity,
};
#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;
use sp_std::prelude::*;
@@ -87,7 +89,7 @@ impl<T: Config> MigrationStep for Migration<T> {
T::WeightInfo::v9_migration_step(T::MaxCodeLen::get())
}
fn step(&mut self) -> (IsFinished, Weight) {
fn step(&mut self, meter: &mut WeightMeter) -> IsFinished {
let mut iter = if let Some(last_key) = self.last_code_hash.take() {
v8::CodeStorage::<T>::iter_from(v8::CodeStorage::<T>::hashed_key_for(last_key))
} else {
@@ -106,10 +108,12 @@ impl<T: Config> MigrationStep for Migration<T> {
};
CodeStorage::<T>::insert(key, module);
self.last_code_hash = Some(key);
(IsFinished::No, T::WeightInfo::v9_migration_step(len))
meter.consume(T::WeightInfo::v9_migration_step(len));
IsFinished::No
} else {
log::debug!(target: LOG_TARGET, "No more contracts code to migrate");
(IsFinished::Yes, T::WeightInfo::v9_migration_step(0))
meter.consume(T::WeightInfo::v9_migration_step(0));
IsFinished::Yes
}
}
@@ -36,6 +36,7 @@ use frame_support::{
tokens::{fungible::Inspect, Fortitude::Polite, Preservation::Preserve},
ExistenceRequirement, ReservableCurrency,
},
weights::WeightMeter,
DefaultNoBound,
};
use sp_core::hexdisplay::HexDisplay;
@@ -160,7 +161,7 @@ where
T::WeightInfo::v10_migration_step()
}
fn step(&mut self) -> (IsFinished, Weight) {
fn step(&mut self, meter: &mut WeightMeter) -> IsFinished {
let mut iter = if let Some(last_account) = self.last_account.take() {
v9::ContractInfoOf::<T, OldCurrency>::iter_from(
v9::ContractInfoOf::<T, OldCurrency>::hashed_key_for(last_account),
@@ -267,10 +268,12 @@ where
// Store last key for next migration step
self.last_account = Some(account);
(IsFinished::No, T::WeightInfo::v10_migration_step())
meter.consume(T::WeightInfo::v10_migration_step());
IsFinished::No
} else {
log::debug!(target: LOG_TARGET, "Done Migrating contract info");
(IsFinished::Yes, T::WeightInfo::v10_migration_step())
meter.consume(T::WeightInfo::v10_migration_step());
IsFinished::Yes
}
}
@@ -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")]
@@ -25,7 +25,8 @@ use crate::{
};
use codec::{Decode, Encode};
use frame_support::{
pallet_prelude::*, storage_alias, traits::ReservableCurrency, DefaultNoBound, Identity,
pallet_prelude::*, storage_alias, traits::ReservableCurrency, weights::WeightMeter,
DefaultNoBound, Identity,
};
use scale_info::prelude::format;
use sp_core::hexdisplay::HexDisplay;
@@ -146,7 +147,7 @@ where
T::WeightInfo::v12_migration_step(T::MaxCodeLen::get())
}
fn step(&mut self) -> (IsFinished, Weight) {
fn step(&mut self, meter: &mut WeightMeter) -> IsFinished {
let mut iter = if let Some(last_key) = self.last_code_hash.take() {
v11::OwnerInfoOf::<T, OldCurrency>::iter_from(
v11::OwnerInfoOf::<T, OldCurrency>::hashed_key_for(last_key),
@@ -230,10 +231,12 @@ where
self.last_code_hash = Some(hash);
(IsFinished::No, T::WeightInfo::v12_migration_step(code_len as u32))
meter.consume(T::WeightInfo::v12_migration_step(code_len as u32));
IsFinished::No
} else {
log::debug!(target: LOG_TARGET, "No more OwnerInfo to migrate");
(IsFinished::Yes, T::WeightInfo::v12_migration_step(0))
meter.consume(T::WeightInfo::v12_migration_step(0));
IsFinished::Yes
}
}
@@ -24,7 +24,7 @@ use crate::{
AccountIdOf, BalanceOf, CodeHash, Config, Pallet, TrieId, Weight, LOG_TARGET,
};
use codec::{Decode, Encode};
use frame_support::{pallet_prelude::*, storage_alias, DefaultNoBound};
use frame_support::{pallet_prelude::*, storage_alias, weights::WeightMeter, DefaultNoBound};
use sp_runtime::BoundedBTreeMap;
use sp_std::prelude::*;
@@ -102,7 +102,7 @@ impl<T: Config> MigrationStep for Migration<T> {
T::WeightInfo::v13_migration_step()
}
fn step(&mut self) -> (IsFinished, Weight) {
fn step(&mut self, meter: &mut WeightMeter) -> IsFinished {
let mut iter = if let Some(last_account) = self.last_account.take() {
v12::ContractInfoOf::<T>::iter_from(v12::ContractInfoOf::<T>::hashed_key_for(
last_account,
@@ -126,10 +126,12 @@ impl<T: Config> MigrationStep for Migration<T> {
};
ContractInfoOf::<T>::insert(key.clone(), info);
self.last_account = Some(key);
(IsFinished::No, T::WeightInfo::v13_migration_step())
meter.consume(T::WeightInfo::v13_migration_step());
IsFinished::No
} else {
log::debug!(target: LOG_TARGET, "No more contracts to migrate");
(IsFinished::Yes, T::WeightInfo::v13_migration_step())
meter.consume(T::WeightInfo::v13_migration_step());
IsFinished::Yes
}
}
}
@@ -35,6 +35,7 @@ use frame_support::{
pallet_prelude::*,
storage_alias,
traits::{fungible::MutateHold, ReservableCurrency},
weights::WeightMeter,
DefaultNoBound,
};
use sp_core::hexdisplay::HexDisplay;
@@ -132,7 +133,7 @@ where
T::WeightInfo::v14_migration_step()
}
fn step(&mut self) -> (IsFinished, Weight) {
fn step(&mut self, meter: &mut WeightMeter) -> IsFinished {
let mut iter = if let Some(last_hash) = self.last_code_hash.take() {
v13::CodeInfoOf::<T, OldCurrency>::iter_from(
v13::CodeInfoOf::<T, OldCurrency>::hashed_key_for(last_hash),
@@ -185,10 +186,12 @@ where
});
self.last_code_hash = Some(hash);
(IsFinished::No, T::WeightInfo::v14_migration_step())
meter.consume(T::WeightInfo::v14_migration_step());
IsFinished::No
} else {
log::debug!(target: LOG_TARGET, "No more code upload deposit to migrate");
(IsFinished::Yes, T::WeightInfo::v14_migration_step())
meter.consume(T::WeightInfo::v14_migration_step());
IsFinished::Yes
}
}
@@ -36,6 +36,7 @@ use frame_support::{
fungible::{Mutate, MutateHold},
tokens::{fungible::Inspect, Fortitude, Preservation},
},
weights::WeightMeter,
BoundedBTreeMap, DefaultNoBound,
};
use frame_system::Pallet as System;
@@ -125,7 +126,7 @@ impl<T: Config> MigrationStep for Migration<T> {
T::WeightInfo::v15_migration_step()
}
fn step(&mut self) -> (IsFinished, Weight) {
fn step(&mut self, meter: &mut WeightMeter) -> IsFinished {
let mut iter = if let Some(last_account) = self.last_account.take() {
v14::ContractInfoOf::<T>::iter_from(v14::ContractInfoOf::<T>::hashed_key_for(
last_account,
@@ -234,10 +235,12 @@ impl<T: Config> MigrationStep for Migration<T> {
// Store last key for next migration step
self.last_account = Some(account);
(IsFinished::No, T::WeightInfo::v15_migration_step())
meter.consume(T::WeightInfo::v15_migration_step());
IsFinished::No
} else {
log::info!(target: LOG_TARGET, "Done Migrating Storage Deposits.");
(IsFinished::Yes, T::WeightInfo::v15_migration_step())
meter.consume(T::WeightInfo::v15_migration_step());
IsFinished::Yes
}
}