-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework on runtime upgrade logic for pallet-dummy-precompiles-code
- Loading branch information
1 parent
e1e669a
commit 38cd651
Showing
4 changed files
with
78 additions
and
76 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,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(()) | ||
} | ||
} |