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

Commit

Permalink
Migration updates
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed May 2, 2023
1 parent 7ab5774 commit c224350
Show file tree
Hide file tree
Showing 6 changed files with 372 additions and 112 deletions.
10 changes: 10 additions & 0 deletions frame/contracts/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ benchmarks! {
Contracts::<T>::reinstrument_module(&mut module, &schedule)?;
}

// This benchmarks the v9 migration cost
#[pov_mode = Measured]
v9_translate_wasm_module {
let c in 0 .. Perbill::from_percent(49).mul_ceil(T::MaxCodeLen::get());
migration::v9::store_old_dummy_code::<T>(c as usize);
let mut migration = migration::v9::Migration::<T>::default();
}: {
migration.step();
}

// This benchmarks the overhead of loading a code of size `c` byte from storage and into
// the sandbox. This does **not** include the actual execution for which the gas meter
// is responsible. This is achieved by generating all code to the `deploy` function
Expand Down
40 changes: 29 additions & 11 deletions frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ use sp_std::{fmt::Debug, marker::PhantomData, prelude::*};
pub use crate::{
address::{AddressGenerator, DefaultAddressGenerator},
exec::Frame,
migration::Migration,
migration::*,
pallet::*,
schedule::{HostFnWeights, InstructionWeights, Limits, Schedule},
wasm::Determinism,
Expand Down Expand Up @@ -177,8 +177,12 @@ pub mod pallet {
use frame_system::pallet_prelude::*;

/// The current storage version.
#[cfg(not(test))]
const STORAGE_VERSION: StorageVersion = StorageVersion::new(9);

#[cfg(test)]
const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(PhantomData<T>);
Expand Down Expand Up @@ -314,10 +318,12 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_idle(_block: T::BlockNumber, remaining_weight: Weight) -> Weight {
// TODO: run migration here
if Migration::<T>::ensure_migrated().is_err() {
return T::DbWeight::get().reads(1)
}
let remaining_weight = match Migration::<T>::migrate(remaining_weight) {
MigrateResult::InProgress(weight) => return remaining_weight.saturating_sub(weight),
MigrateResult::NoMigrationPerformed(weight) | MigrateResult::Completed(weight) =>
remaining_weight.saturating_sub(weight),
};

ContractInfo::<T>::process_deletion_queue_batch(remaining_weight)
.saturating_add(T::WeightInfo::on_process_deletion_queue_batch())
}
Expand Down Expand Up @@ -724,15 +730,23 @@ pub mod pallet {
)
}

/// TODO: add benchmark for base weight
/// TODO: add a minimum weight limit so that people don't spam small free extrinsics
#[pallet::call_index(9)]
#[pallet::weight(*weight_limit)]
#[pallet::weight(T::WeightInfo::migrate().saturating_add(*weight_limit))]
pub fn migrate(origin: OriginFor<T>, weight_limit: Weight) -> DispatchResultWithPostInfo {
ensure_signed(origin)?;
Migration::<T>::migrate(weight_limit)
.map(|weight| PostDispatchInfo { actual_weight: Some(weight), pays_fee: Pays::No })
.map_err(|(weight, error)| error.with_weight(weight))
ensure!(
weight_limit.any_gt(T::WeightInfo::migrate().saturating_mul(10)),
Error::<T>::MigrateWeightLimitTooLow
);

match Migration::<T>::migrate(weight_limit) {
MigrateResult::InProgress(weight) | MigrateResult::Completed(weight) =>
Ok(PostDispatchInfo { actual_weight: Some(weight), pays_fee: Pays::No }),
MigrateResult::NoMigrationPerformed(weight) => {
let err: DispatchError = <Error<T>>::NoMigrationPerformed.into();
Err(err.with_weight(weight))
},
}
}
}

Expand Down Expand Up @@ -886,8 +900,12 @@ pub mod pallet {
CodeRejected,
/// An indetermistic code was used in a context where this is not permitted.
Indeterministic,
/// A pending migration needs to complete before the extrinsic can be called.
MigrationInProgress,
/// Migrate dispatch call was attempted but no migration was performed.
NoMigrationPerformed,
/// The weight limit for a migration dispatch call was too low.
MigrateWeightLimitTooLow,
}

/// A mapping from an original code hash to the original code, untouched by instrumentation.
Expand Down
Loading

0 comments on commit c224350

Please sign in to comment.