Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend logic of managing storage version at humanode-runtime #1332

Merged
merged 8 commits into from
Nov 19, 2024
8 changes: 8 additions & 0 deletions crates/humanode-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub mod eth_sig;
mod find_author;
mod fixed_supply;
pub mod robonode;
pub mod storage_version_initializer;
#[cfg(test)]
mod tests;
mod weights;
Expand Down Expand Up @@ -926,6 +927,13 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
(
storage_version_initializer::StorageVersionInitializer<DummyPrecompilesCode, Runtime>,
storage_version_initializer::StorageVersionInitializer<
BalancedCurrencySwapBridgesInitializer,
Runtime,
>,
),
>;

impl frame_system::offchain::CreateSignedTransaction<RuntimeCall> for Runtime {
Expand Down
62 changes: 62 additions & 0 deletions crates/humanode-runtime/src/storage_version_initializer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use core::marker::PhantomData;

use frame_support::{
log::info,
traits::{Get, GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess},
weights::Weight,
};
#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;

/// Pallet storage version initializer.
pub struct StorageVersionInitializer<P, R>(PhantomData<(P, R)>);

impl<P, R> OnRuntimeUpgrade for StorageVersionInitializer<P, R>
where
P: GetStorageVersion + PalletInfoAccess,
R: frame_system::Config,
{
fn on_runtime_upgrade() -> Weight {
// Properly manage default on chain storage version as the pallet was added after genesis
// with initial storage version != 0.
//
// <https://github.com/paritytech/substrate/pull/14641>
let current_storage_version = P::current_storage_version();
let onchain_storage_version = P::on_chain_storage_version();

let mut weight = R::DbWeight::get().reads(1);

if onchain_storage_version == 0 && current_storage_version != 0 {
info!(
"{}: Initializing an unset on-chain storage version to {:?}, assuming the effective state version is the latest pallet version",
P::name(),
current_storage_version,
);

// Set new storage version.
current_storage_version.put::<P>();

// Write the onchain storage version.
weight = weight.saturating_add(R::DbWeight::get().writes(1));
} else {
info!(
"{}: Nothing to do. This runtime upgrade probably should be removed.",
P::name(),
);
}

weight
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
// Do nothing.
Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), &'static str> {
assert_eq!(P::on_chain_storage_version(), P::current_storage_version());
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,6 @@ pub fn on_runtime_upgrade<T: Config>() -> Weight {
weight.saturating_accrue(T::DbWeight::get().writes(2));
}

// Properly manage default on chain storage version as the pallet was added after genesis
// with initial storage version != 0.
//
// <https://github.com/paritytech/substrate/pull/14641>
let current_storage_version = <Pallet<T>>::current_storage_version();
let onchain_storage_version = <Pallet<T>>::on_chain_storage_version();

weight.saturating_accrue(T::DbWeight::get().reads(1));

if onchain_storage_version == 0 && current_storage_version != 0 {
// Set new storage version.
current_storage_version.put::<Pallet<T>>();

// Write the onchain storage version.
weight = weight.saturating_add(T::DbWeight::get().writes(1));
}

weight
}

Expand Down Expand Up @@ -75,10 +58,5 @@ pub fn post_upgrade<T: Config>(_state: Vec<u8>) -> Result<(), &'static str> {

assert_eq!(storage_root_before, storage_root(StateVersion::V1));

assert_eq!(
<Pallet<T>>::on_chain_storage_version(),
<Pallet<T>>::current_storage_version()
);

Ok(())
}
22 changes: 0 additions & 22 deletions crates/pallet-dummy-precompiles-code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,6 @@ pub mod pallet {
weight.saturating_accrue(T::DbWeight::get().writes(2));
}

// Properly manage default on chain storage version as the pallet was added after genesis
// with initial storage version != 0.
//
// <https://github.com/paritytech/substrate/pull/14641>
let current_storage_version = <Pallet<T>>::current_storage_version();
let onchain_storage_version = <Pallet<T>>::on_chain_storage_version();

weight.saturating_accrue(T::DbWeight::get().reads(1));

if onchain_storage_version == 0 && current_storage_version != 0 {
// Set new storage version.
current_storage_version.put::<Pallet<T>>();

// Write the onchain storage version.
weight = weight.saturating_add(T::DbWeight::get().writes(1));
}

weight
}

Expand All @@ -140,11 +123,6 @@ pub mod pallet {
return Err("precompiles not created properly: {:not_created_precompiles}");
}

assert_eq!(
<Pallet<T>>::on_chain_storage_version(),
<Pallet<T>>::current_storage_version()
);

Ok(())
}
}
Expand Down