Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
fix: clear also JobRegistration since requirements format changed
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsimon committed Jun 29, 2023
1 parent ffec54e commit 79ccc26
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 97 deletions.
7 changes: 3 additions & 4 deletions pallets/acurast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ pub use traits::*;
pub type JobRegistrationFor<T> =
JobRegistration<<T as frame_system::Config>::AccountId, <T as Config>::RegistrationExtra>;

pub(crate) use pallet::STORAGE_VERSION;

#[frame_support::pallet]
pub mod pallet {
use acurast_common::*;
Expand Down Expand Up @@ -188,7 +186,8 @@ pub mod pallet {
}
}

pub(crate) const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
pub(crate) const STORAGE_VERSION_: usize = 3;
pub(crate) const STORAGE_VERSION: StorageVersion = StorageVersion::new(STORAGE_VERSION_ as u16);

#[pallet::pallet]
#[pallet::without_storage_info]
Expand Down Expand Up @@ -299,7 +298,7 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
crate::migration::migrate_to_v2::<T>()
crate::migration::migrate::<T>()
}
}

Expand Down
69 changes: 45 additions & 24 deletions pallets/acurast/src/migration.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use frame_support::{traits::GetStorageVersion, weights::Weight};
use frame_support::{
traits::{GetStorageVersion, StorageVersion},
weights::Weight,
};
use sp_core::Get;

use super::*;
Expand Down Expand Up @@ -29,29 +32,47 @@ pub mod v1 {
}
}

pub fn migrate_to_v2<T: Config>() -> Weight {
pub fn migrate<T: Config>() -> Weight {
let migrations: [&dyn Fn() -> Weight; STORAGE_VERSION_ - 1] =
[&migrate_to_v2::<T>, &migrate_to_v3::<T>];

let onchain_version = Pallet::<T>::on_chain_storage_version();
if onchain_version < crate::STORAGE_VERSION {
StoredJobRegistration::<T>::translate::<
v1::JobRegistration<T::AccountId, T::RegistrationExtra>,
_,
>(|_k1, _k2, job| {
Some(JobRegistration {
script: job.script,
allowed_sources: job.allowed_sources,
allow_only_verified_sources: job.allow_only_verified_sources,
schedule: job.schedule,
memory: job.memory,
network_requests: job.network_requests,
storage: job.storage,
required_modules: JobModules::default(),
extra: job.extra,
})
});
STORAGE_VERSION.put::<Pallet<T>>();
let count = StoredJobRegistration::<T>::iter().count() as u64;
T::DbWeight::get().reads_writes(count + 1, count + 1)
} else {
Weight::zero()
let mut weight: Weight = Default::default();
for (i, f) in migrations.iter().enumerate() {
// correct index since we start with version 2 but i is 0-based
if onchain_version < StorageVersion::new((i + 2).try_into().unwrap()) {
weight += f();
}
}

STORAGE_VERSION.put::<Pallet<T>>();
weight + T::DbWeight::get().writes(1)
}

fn migrate_to_v2<T: Config>() -> Weight {
StoredJobRegistration::<T>::translate::<
v1::JobRegistration<T::AccountId, T::RegistrationExtra>,
_,
>(|_k1, _k2, job| {
Some(JobRegistration {
script: job.script,
allowed_sources: job.allowed_sources,
allow_only_verified_sources: job.allow_only_verified_sources,
schedule: job.schedule,
memory: job.memory,
network_requests: job.network_requests,
storage: job.storage,
required_modules: JobModules::default(),
extra: job.extra,
})
});
let count = StoredJobRegistration::<T>::iter().count() as u64;
T::DbWeight::get().reads_writes(count + 1, count + 1)
}

fn migrate_to_v3<T: Config>() -> Weight {
let mut count = 0u32;
count += StoredJobRegistration::<T>::clear(10_000, None).loops;

T::DbWeight::get().reads_writes((count + 1).into(), (count + 1).into())
}
5 changes: 3 additions & 2 deletions pallets/assets-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ pub mod pallet {
use xcm::prelude::{Abstract, AssetId, Concrete, GeneralIndex, PalletInstance, Parachain, X3};
use xcm_executor::traits::Convert;

pub(crate) const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
pub(crate) const STORAGE_VERSION_: usize = 2;
pub(crate) const STORAGE_VERSION: StorageVersion = StorageVersion::new(STORAGE_VERSION_ as u16);

#[pallet::pallet]
#[pallet::without_storage_info]
Expand Down Expand Up @@ -120,7 +121,7 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config<I>, I: 'static> Hooks<BlockNumberFor<T>> for Pallet<T, I> {
fn on_runtime_upgrade() -> Weight {
crate::migration::migrate_to_v2::<T, I>()
crate::migration::migrate::<T, I>()
}
}

Expand Down
35 changes: 24 additions & 11 deletions pallets/assets-manager/src/migration.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
use frame_support::{traits::GetStorageVersion, weights::Weight};
use frame_support::{
traits::{GetStorageVersion, StorageVersion},
weights::Weight,
};
use sp_core::Get;

use super::*;

pub fn migrate_to_v2<T: Config<I>, I: 'static>() -> Weight {
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
if onchain_version < crate::STORAGE_VERSION {
let mut count = 0u32;
count += AssetIndex::<T, I>::clear(100, None).loops;
count += ReverseAssetIndex::<T, I>::clear(100, None).loops;
pub fn migrate<T: Config<I>, I: 'static>() -> Weight {
let migrations: [&dyn Fn() -> Weight; STORAGE_VERSION_ - 1] = [&migrate_to_v2::<T, I>];

STORAGE_VERSION.put::<Pallet<T, I>>();
T::DbWeight::get().reads_writes((count + 1).into(), (count + 1).into())
} else {
Weight::zero()
let onchain_version = Pallet::<T, I>::on_chain_storage_version();
let mut weight: Weight = Default::default();
for (i, f) in migrations.iter().enumerate() {
// correct index since we start with version 2 but i is 0-based
if onchain_version < StorageVersion::new((i + 2).try_into().unwrap()) {
weight += f();
}
}

STORAGE_VERSION.put::<Pallet<T, I>>();
weight + T::DbWeight::get().writes(1)
}

fn migrate_to_v2<T: Config<I>, I: 'static>() -> Weight {
let mut count = 0u32;
count += AssetIndex::<T, I>::clear(100, None).loops;
count += ReverseAssetIndex::<T, I>::clear(100, None).loops;

T::DbWeight::get().reads_writes((count + 1).into(), (count + 1).into())
}
21 changes: 3 additions & 18 deletions pallets/marketplace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ pub mod pallet {
use sp_runtime::{FixedPointOperand, FixedU128, Permill, SaturatedConversion};
use sp_std::iter::once;
use sp_std::prelude::*;
use xcm::v3::AssetId;

use pallet_acurast::utils::ensure_source_verified;
use pallet_acurast::{
Expand Down Expand Up @@ -95,7 +94,8 @@ pub mod pallet {
type BenchmarkHelper: crate::benchmarking::BenchmarkHelper<Self>;
}

pub(crate) const STORAGE_VERSION: StorageVersion = StorageVersion::new(3);
pub(crate) const STORAGE_VERSION_: usize = 4;
pub(crate) const STORAGE_VERSION: StorageVersion = StorageVersion::new(STORAGE_VERSION_ as u16);

#[pallet::pallet]
#[pallet::without_storage_info]
Expand Down Expand Up @@ -139,21 +139,11 @@ pub mod pallet {
pub type StoredReputation<T: Config> =
StorageMap<_, Blake2_128Concat, T::AccountId, BetaParameters<FixedU128>>;

/// Deprecated: Number of total jobs assigned as a map [`AssetId`] -> `Balance`
#[pallet::storage]
#[deprecated(since = "V2", note = "please use `StoredTotalAssignedV3` instead")]
pub type StoredTotalAssignedV2<T: Config> = StorageMap<_, Blake2_128Concat, AssetId, u128>;

/// Number of total jobs assigned.
#[pallet::storage]
#[pallet::getter(fn total_assigned)]
pub type StoredTotalAssignedV3<T: Config> = StorageValue<_, u128>;

/// Deprecated: Average job reward as a map [`AssetId`] -> `Balance`
#[pallet::storage]
#[deprecated(since = "V2", note = "please use `StoredAverageRewardV3` instead")]
pub type StoredAverageRewardV2<T> = StorageMap<_, Blake2_128Concat, AssetId, u128>;

/// Average job reward.
#[pallet::storage]
#[pallet::getter(fn average_reward)]
Expand All @@ -171,11 +161,6 @@ pub mod pallet {
AssignmentFor<T>,
>;

#[pallet::storage]
#[deprecated(since = "V2", note = "please use `AssignedProcessors` instead")]
pub type StoredMatchesReverseIndex<T: Config> =
StorageMap<_, Blake2_128, JobId<T::AccountId>, T::AccountId>;

/// Job matches as a map [`JobId`] -> [`AccountId`] `(source)` -> `()`.
///
/// This map can serve as a reverse index into `StoredMatches` to achieve a mapping [`JobId`] -> [[`AssignmentFor<T>`]] with one assignment per slot that is not yet finalized.
Expand Down Expand Up @@ -379,7 +364,7 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
crate::migration::migrate_to_v2::<T>() + crate::migration::migrate_to_v3::<T>()
crate::migration::migrate::<T>()
}
}

Expand Down
89 changes: 51 additions & 38 deletions pallets/marketplace/src/migration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![allow(deprecated)]

use frame_support::{traits::GetStorageVersion, weights::Weight};
use frame_support::{
traits::{GetStorageVersion, StorageVersion},
weights::Weight,
};
use pallet_acurast::JobModules;
use sp_core::Get;

Expand All @@ -25,46 +28,56 @@ pub mod v1 {
}
}

pub fn migrate_to_v2<T: Config>() -> Weight {
pub fn migrate<T: Config>() -> Weight {
let migrations: [&dyn Fn() -> Weight; STORAGE_VERSION_ - 1] = [
&migrate_to_v2::<T>,
&migrate_to_v3::<T>,
&migrate_to_v4::<T>,
];

let onchain_version = Pallet::<T>::on_chain_storage_version();
if onchain_version < crate::STORAGE_VERSION {
StoredAdvertisementRestriction::<T>::translate_values::<
v1::AdvertisementRestriction<T::AccountId>,
_,
>(|ad| {
Some(AdvertisementRestriction {
max_memory: ad.max_memory,
network_request_quota: ad.network_request_quota,
storage_capacity: ad.storage_capacity,
allowed_consumers: ad.allowed_consumers,
available_modules: JobModules::default(),
})
});
STORAGE_VERSION.put::<Pallet<T>>();
let count = StoredAdvertisementRestriction::<T>::iter_values().count() as u64;
T::DbWeight::get().reads_writes(count + 1, count + 1)
} else {
Weight::zero()
let mut weight: Weight = Default::default();
for (i, f) in migrations.iter().enumerate() {
// correct index since we start with version 2 but i is 0-based
if onchain_version < StorageVersion::new((i + 2).try_into().unwrap()) {
weight += f();
}
}

STORAGE_VERSION.put::<Pallet<T>>();
weight + T::DbWeight::get().writes(1)
}

pub fn migrate_to_v3<T: Config>() -> Weight {
let onchain_version = Pallet::<T>::on_chain_storage_version();
if onchain_version < crate::STORAGE_VERSION {
let mut count = 0u32;
count += StoredJobStatus::<T>::clear(10_000, None).loops;
count += StoredAdvertisementRestriction::<T>::clear(10_000, None).loops;
count += StoredAdvertisementPricing::<T>::clear(10_000, None).loops;
count += StoredStorageCapacity::<T>::clear(10_000, None).loops;
count += StoredReputation::<T>::clear(10_000, None).loops;
count += StoredTotalAssignedV2::<T>::clear(10_000, None).loops;
count += StoredAverageRewardV2::<T>::clear(10_000, None).loops;
count += StoredMatches::<T>::clear(10_000, None).loops;
count += StoredMatchesReverseIndex::<T>::clear(10_000, None).loops;
fn migrate_to_v2<T: Config>() -> Weight {
StoredAdvertisementRestriction::<T>::translate_values::<
v1::AdvertisementRestriction<T::AccountId>,
_,
>(|ad| {
Some(AdvertisementRestriction {
max_memory: ad.max_memory,
network_request_quota: ad.network_request_quota,
storage_capacity: ad.storage_capacity,
allowed_consumers: ad.allowed_consumers,
available_modules: JobModules::default(),
})
});
let count = StoredAdvertisementRestriction::<T>::iter_values().count() as u64;
T::DbWeight::get().reads_writes(count + 1, count + 1)
}

STORAGE_VERSION.put::<Pallet<T>>();
T::DbWeight::get().reads_writes((count + 1).into(), (count + 1).into())
} else {
Weight::zero()
}
fn migrate_to_v3<T: Config>() -> Weight {
let mut count = 0u32;
count += StoredJobStatus::<T>::clear(10_000, None).loops;
count += StoredAdvertisementRestriction::<T>::clear(10_000, None).loops;
count += StoredAdvertisementPricing::<T>::clear(10_000, None).loops;
count += StoredStorageCapacity::<T>::clear(10_000, None).loops;
count += StoredReputation::<T>::clear(10_000, None).loops;
count += StoredMatches::<T>::clear(10_000, None).loops;

T::DbWeight::get().reads_writes((count + 1).into(), (count + 1).into())
}

fn migrate_to_v4<T: Config>() -> Weight {
// clear again all storages since we want to clear at the same time as pallet acurast for consistent state
migrate_to_v3::<T>()
}

0 comments on commit 79ccc26

Please sign in to comment.