Skip to content

Commit

Permalink
Rework on runtime upgrade logic for pallet-dummy-precompiles-code
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrylavrenov committed Nov 1, 2024
1 parent e1e669a commit 38cd651
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 76 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions crates/pallet-dummy-precompiles-code/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ frame-system = { workspace = true }
pallet-evm = { workspace = true }
scale-info = { workspace = true, features = ["derive"] }
sp-core = { workspace = true }
sp-std = { workspace = true }

[dev-dependencies]
fp-evm = { workspace = true }
Expand All @@ -35,7 +34,6 @@ std = [
"pallet-timestamp/std",
"scale-info/std",
"sp-core/std",
"sp-std/std",
]
try-runtime = [
"frame-support/try-runtime",
Expand Down
76 changes: 3 additions & 73 deletions crates/pallet-dummy-precompiles-code/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ use frame_support::{
pub use pallet::*;
use sp_core::H160;

mod upgrade_init;
pub use upgrade_init::UpgradeInit;

#[cfg(test)]
mod mock;

Expand All @@ -33,7 +36,6 @@ pub const DUMMY_CODE: &[u8] = &[0x5F, 0x5F, 0xFD];
#[frame_support::pallet]
pub mod pallet {
use frame_support::{pallet_prelude::*, sp_std::vec::Vec};
use frame_system::pallet_prelude::*;

use super::*;

Expand Down Expand Up @@ -76,78 +78,6 @@ pub mod pallet {
<LastForceExecuteAskCounter<T>>::put(T::ForceExecuteAskCounter::get());
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
let last_execution_version = Self::last_execution_version();
let last_force_execute_ask_counter = Self::last_force_execute_ask_counter();

let current_force_execute_ask_counter = T::ForceExecuteAskCounter::get();
let mut weight = T::DbWeight::get().reads(2);

let is_version_mismatch = last_execution_version != CURRENT_EXECUTION_VERSION;
let is_forced = last_force_execute_ask_counter < current_force_execute_ask_counter;

if is_version_mismatch || is_forced {
weight.saturating_accrue(Self::precompiles_addresses_add_dummy_code());

<LastExecutionVersion<T>>::put(CURRENT_EXECUTION_VERSION);
<LastForceExecuteAskCounter<T>>::put(current_force_execute_ask_counter);
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
}

#[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> {
use sp_std::vec::Vec;

let mut not_created_precompiles = Vec::new();

for precompile_address in &T::PrecompilesAddresses::get() {
let code = pallet_evm::AccountCodes::<T>::get(*precompile_address);
if code != DUMMY_CODE {
not_created_precompiles.push(*precompile_address);
}
}

if !not_created_precompiles.is_empty() {
return Err("precompiles not created properly: {:not_created_precompiles}");
}

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

Ok(())
}
}
}

impl<T: Config> Pallet<T> {
Expand Down
75 changes: 75 additions & 0 deletions crates/pallet-dummy-precompiles-code/src/upgrade_init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//! Upgrade init implementation.

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

use crate::{
Config, LastExecutionVersion, LastForceExecuteAskCounter, Pallet, CURRENT_EXECUTION_VERSION,
};

/// Execute upgrade init.
pub struct UpgradeInit<T>(sp_std::marker::PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for UpgradeInit<T> {
fn on_runtime_upgrade() -> Weight {
let last_execution_version = Pallet::<T>::last_execution_version();
let last_force_execute_ask_counter = Pallet::<T>::last_force_execute_ask_counter();

let current_force_execute_ask_counter = T::ForceExecuteAskCounter::get();
let mut weight = T::DbWeight::get().reads(2);

let is_version_mismatch = last_execution_version != CURRENT_EXECUTION_VERSION;
let is_forced = last_force_execute_ask_counter < current_force_execute_ask_counter;

if is_version_mismatch || is_forced {
weight.saturating_accrue(Pallet::<T>::precompiles_addresses_add_dummy_code());

<LastExecutionVersion<T>>::put(CURRENT_EXECUTION_VERSION);
<LastForceExecuteAskCounter<T>>::put(current_force_execute_ask_counter);
weight.saturating_accrue(T::DbWeight::get().writes(2));
} else {
info!(message = "Nothing to do. This runtime upgrade probably should be removed");
}

weight
}

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

#[cfg(feature = "try-runtime")]
fn post_upgrade(_state: Vec<u8>) -> Result<(), TryRuntimeError> {
use crate::DUMMY_CODE;

let mut not_created_precompiles = Vec::new();

for precompile_address in &T::PrecompilesAddresses::get() {
let code = pallet_evm::AccountCodes::<T>::get(*precompile_address);
if code != DUMMY_CODE {
not_created_precompiles.push(*precompile_address);
}
}

if !not_created_precompiles.is_empty() {
return Err(TryRuntimeError::Other(
"precompiles not created properly: {:not_created_precompiles}",
));
}

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

Ok(())
}
}

0 comments on commit 38cd651

Please sign in to comment.