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

Rework migration and on runtime upgrade logic execution #1315

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rework on runtime upgrade logic for pallet-dummy-precompiles-code
  • Loading branch information
dmitrylavrenov committed Nov 25, 2024
commit 39214bca1c5bd50fa68b3f078069f7d3d3226848
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
54 changes: 3 additions & 51 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,56 +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));
}

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}");
}

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(())
}
}