diff --git a/.github/workflows/010_build_and_test.yaml b/.github/workflows/010_build_and_test.yaml index 5db61fcd2..d6701e42e 100644 --- a/.github/workflows/010_build_and_test.yaml +++ b/.github/workflows/010_build_and_test.yaml @@ -16,7 +16,9 @@ jobs: CARGO_HOME: /root/.cargo steps: - - uses: actions/checkout@v3 + - name: Checkout + uses: actions/checkout@v3 + timeout-minutes: 4 - name: Chown user run: | diff --git a/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs b/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs index 7a4dde373..60f8d6aeb 100644 --- a/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs +++ b/substrate-node/pallets/pallet-tfgrid/src/migrations/mod.rs @@ -6,3 +6,4 @@ pub mod v13; pub mod v14; pub mod v15; pub mod v16; +pub mod v17; diff --git a/substrate-node/pallets/pallet-tfgrid/src/migrations/v17.rs b/substrate-node/pallets/pallet-tfgrid/src/migrations/v17.rs new file mode 100644 index 000000000..6f7457d61 --- /dev/null +++ b/substrate-node/pallets/pallet-tfgrid/src/migrations/v17.rs @@ -0,0 +1,189 @@ +use crate::*; +use frame_support::{traits::OnRuntimeUpgrade, weights::Weight}; +use log::{debug, info}; +use sp_core::Get; +use sp_runtime::Saturating; +use sp_std::marker::PhantomData; + +#[cfg(feature = "try-runtime")] +use sp_std::{vec, vec::Vec}; + +pub struct CleanStorageState(PhantomData); + +impl OnRuntimeUpgrade for CleanStorageState { + fn on_runtime_upgrade() -> Weight { + if PalletVersion::::get() == types::StorageVersion::V16Struct { + clean_pallet_tfgrid::() + } else { + info!("⛔ Unused TFGrid pallet V16 storage cleaning"); + Weight::zero() + } + } +} + +pub fn clean_pallet_tfgrid() -> frame_support::weights::Weight { + info!("🧼 Cleaning TFGrid pallet storagem"); + let mut weight = clean_farm_id_by_name::(); + weight.saturating_accrue(clean_farm_payout_v2_address_by_farm_id::()); + weight.saturating_accrue(clean_node_id_by_twin_id::()); + + PalletVersion::::put(types::StorageVersion::V17Struct); + info!("🔔 Ending TFGrid pallet storage cleaning"); + weight.saturating_add(T::DbWeight::get().writes(1)) +} + +// Farms +pub fn clean_farms() -> frame_support::weights::Weight { + debug!( + "🧼 TFGrid pallet {:?} cleaning Farms storage map START", + PalletVersion::::get() + ); + + let mut r = 0u64; + let mut w = 0u64; + + let to_remove: Vec = Farms::::iter() + .filter(|(_, farm)| { + r.saturating_accrue(2); + Twins::::get(farm.twin_id).is_none() + }) + .map(|(id, _)| id) + .collect(); + + for farm_id in to_remove { + Farms::::remove(farm_id); + w.saturating_inc(); + } + + debug!( + "✨ TFGrid pallet {:?} cleaning Farms storage map END", + PalletVersion::::get() + ); + + T::DbWeight::get().reads_writes(r.saturating_add(2), w) +} + +// FarmIdByName +pub fn clean_farm_id_by_name() -> frame_support::weights::Weight { + debug!( + "🧼 TFGrid pallet {:?} cleaning FarmIdByName storage map START", + PalletVersion::::get() + ); + + let mut r = 0u64; + let mut w = 0u64; + + let mut to_remove = vec![]; + let mut to_reinsert = vec![]; + + for (farm_name, farm_id) in FarmIdByName::::iter() { + r.saturating_inc(); + if let Some(f) = Farms::::get(farm_id) { + if f.name.clone().into() != farm_name { + to_remove.push(farm_name); + to_reinsert.push((f.name, farm_id)); + } + } else { + to_remove.push(farm_name); + } + } + + // 1. Remove obsolete entries + for farm_name in to_remove { + FarmIdByName::::remove(farm_name); + w.saturating_inc(); + } + + // 2. Re-insert entries with same name as set in farm + for (farm_name, farm_id) in to_reinsert { + FarmIdByName::::insert(farm_name.into(), farm_id); + w.saturating_inc(); + } + + debug!( + "✨ TFGrid pallet {:?} cleaning FarmIdByName storage map END", + PalletVersion::::get() + ); + + T::DbWeight::get().reads_writes(r.saturating_add(2), w) +} + +// FarmPayoutV2AddressByFarmID +pub fn clean_farm_payout_v2_address_by_farm_id() -> frame_support::weights::Weight { + debug!( + "🧼 TFGrid pallet {:?} cleaning FarmPayoutV2AddressByFarmID storage map START", + PalletVersion::::get() + ); + + let mut r = 0u64; + let mut w = 0u64; + + let to_remove: Vec = FarmPayoutV2AddressByFarmID::::iter() + .filter(|(farm_id, _)| { + r.saturating_accrue(2); + Farms::::get(farm_id).is_none() + }) + .map(|(id, _)| id) + .collect(); + + for farm_id in to_remove { + FarmPayoutV2AddressByFarmID::::remove(farm_id); + w.saturating_inc(); + } + + debug!( + "✨ TFGrid pallet {:?} cleaning FarmPayoutV2AddressByFarmID storage map END", + PalletVersion::::get() + ); + + T::DbWeight::get().reads_writes(r.saturating_add(2), w) +} + +// NodeIdByTwinID +pub fn clean_node_id_by_twin_id() -> frame_support::weights::Weight { + debug!( + "🧼 TFGrid pallet {:?} cleaning NodeIdByTwinID storage map START", + PalletVersion::::get() + ); + + let mut r = 0u64; + let mut w = 0u64; + + let to_remove: Vec = NodeIdByTwinID::::iter() + .filter(|(_, node_id)| { + r.saturating_accrue(2); + Nodes::::get(node_id).is_none() + }) + .map(|(id, _)| id) + .collect(); + + for twin_id in to_remove { + NodeIdByTwinID::::remove(twin_id); + w.saturating_inc(); + } + + debug!( + "✨ TFGrid pallet {:?} cleaning NodeIdByTwinID storage map END", + PalletVersion::::get() + ); + + T::DbWeight::get().reads_writes(r.saturating_add(2), w) +} + +pub struct CheckStorageState(PhantomData); + +impl OnRuntimeUpgrade for CheckStorageState { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result, &'static str> { + info!("current pallet version: {:?}", PalletVersion::::get()); + assert!(PalletVersion::::get() == types::StorageVersion::V17Struct); + + check_pallet_tfgrid::(); + + Ok(vec![]) + } +} + +pub fn check_pallet_tfgrid() { + migrations::v16::check_pallet_tfgrid::(); +} diff --git a/substrate-node/pallets/pallet-tfgrid/src/types.rs b/substrate-node/pallets/pallet-tfgrid/src/types.rs index 37e34f65d..a43ce5791 100644 --- a/substrate-node/pallets/pallet-tfgrid/src/types.rs +++ b/substrate-node/pallets/pallet-tfgrid/src/types.rs @@ -24,11 +24,12 @@ pub enum StorageVersion { V14Struct, V15Struct, V16Struct, + V17Struct, } impl Default for StorageVersion { fn default() -> StorageVersion { - StorageVersion::V16Struct + StorageVersion::V17Struct } } diff --git a/substrate-node/runtime/src/lib.rs b/substrate-node/runtime/src/lib.rs index 374e28c9d..16f8cc30a 100644 --- a/substrate-node/runtime/src/lib.rs +++ b/substrate-node/runtime/src/lib.rs @@ -772,7 +772,8 @@ pub type Executive = frame_executive::Executive< // `OnRuntimeUpgrade`. type Migrations = ( pallet_tfgrid::migrations::v16::KillNodeGpuStatus, - pallet_tfgrid::migrations::v16::CheckStorageState, + pallet_tfgrid::migrations::v17::CleanStorageState, + pallet_tfgrid::migrations::v17::CheckStorageState, pallet_smart_contract::migrations::v10::ReworkBillingLoopInsertion, pallet_smart_contract::migrations::v11::ExtendContractLock, pallet_smart_contract::migrations::v11::CheckStorageState,