-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rework contract billing storage (#477)
- Loading branch information
1 parent
8656ec2
commit 8212b84
Showing
5 changed files
with
142 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
substrate-node/pallets/pallet-smart-contract/src/migration.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use super::*; | ||
use frame_support::weights::Weight; | ||
|
||
pub mod v5 { | ||
use super::*; | ||
use crate::Config; | ||
|
||
use frame_support::{pallet_prelude::Weight, traits::OnRuntimeUpgrade}; | ||
use sp_std::marker::PhantomData; | ||
pub struct ContractMigrationV5<T: Config>(PhantomData<T>); | ||
|
||
impl<T: Config> OnRuntimeUpgrade for ContractMigrationV5<T> { | ||
#[cfg(feature = "try-runtime")] | ||
fn pre_upgrade() -> Result<(), &'static str> { | ||
info!("current pallet version: {:?}", PalletVersion::<T>::get()); | ||
assert!(PalletVersion::<T>::get() == types::StorageVersion::V5); | ||
|
||
info!("👥 Smart Contract pallet to v6 passes PRE migrate checks ✅",); | ||
Ok(()) | ||
} | ||
|
||
fn on_runtime_upgrade() -> Weight { | ||
migrate_to_version_6::<T>() | ||
} | ||
|
||
#[cfg(feature = "try-runtime")] | ||
fn post_upgrade() -> Result<(), &'static str> { | ||
info!("current pallet version: {:?}", PalletVersion::<T>::get()); | ||
assert!(PalletVersion::<T>::get() == types::StorageVersion::V6); | ||
|
||
info!( | ||
"👥 Smart Contract pallet to {:?} passes POST migrate checks ✅", | ||
PalletVersion::<T>::get() | ||
); | ||
|
||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
pub fn migrate_to_version_6<T: Config>() -> frame_support::weights::Weight { | ||
if PalletVersion::<T>::get() == types::StorageVersion::V5 { | ||
info!( | ||
" >>> Starting contract pallet migration, pallet version: {:?}", | ||
PalletVersion::<T>::get() | ||
); | ||
|
||
let mut migrated_count = 0; | ||
|
||
// Collect ContractsToBillAt storage in memory | ||
let contracts_to_bill_at = ContractsToBillAt::<T>::iter().collect::<Vec<_>>(); | ||
|
||
// Remove all items under ContractsToBillAt | ||
frame_support::migration::remove_storage_prefix( | ||
b"SmartContractModule", | ||
b"ContractsToBillAt", | ||
b"", | ||
); | ||
|
||
let billing_freq = 600; | ||
BillingFrequency::<T>::put(billing_freq); | ||
|
||
for (block_number, contract_ids) in contracts_to_bill_at { | ||
migrated_count += 1; | ||
// Construct new index | ||
let index = (block_number - 1) % billing_freq; | ||
// Reinsert items under the new key | ||
info!( | ||
"inserted contracts:{:?} at index: {:?}", | ||
contract_ids.clone(), | ||
index | ||
); | ||
ContractsToBillAt::<T>::insert(index, contract_ids); | ||
} | ||
|
||
info!( | ||
" <<< Contracts storage updated! Migrated {} Contracts ✅", | ||
migrated_count | ||
); | ||
|
||
// Update pallet storage version | ||
PalletVersion::<T>::set(types::StorageVersion::V6); | ||
info!(" <<< Storage version upgraded"); | ||
|
||
// Return the weight consumed by the migration. | ||
T::DbWeight::get().reads_writes(migrated_count as Weight + 1, migrated_count as Weight + 1) | ||
} else { | ||
info!(" >>> Unused migration"); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
substrate-node/pallets/pallet-smart-contract/src/test_utils.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.