From 8e8d58859b0e8f44af395530e1d56f0f8e1bc76a Mon Sep 17 00:00:00 2001 From: Shawn Tabrizi Date: Tue, 3 Mar 2020 17:59:29 +0200 Subject: [PATCH] Gui shawntabrizi runtime upgrade (#5118) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * adding unleash to ci (#5020) * adding unleash to ci * fixing formatting * with a dot please * alpha.3 now * do not publish testing helpers * remove old test-helpers cruft * fix cargo.lock * with alpha 4 * do not publish runtime-interface-test either * disable more test crates from publishing * switch to alpha.5 * replace tempdir with tempfile * update lru * switch to bytes 0.5 * release script fixes * switch on and to latest alpha * BUT THE SPACES * Fix: CI failing for some CLI tests (#5043) * Initial commit Forked at: 41bb2193a267805e2093a081bc3e2aaccc64283a Parent branch: origin/master * Increase killing grace period of CLI tests and display more info * Use --dev everywhere possible * Put pruning mode to its own params struct * Add pruning params to export-blocks command * Added missing file * Removed not-dev mode in tests * Add pruning mode to the revert command * Decrease killing grace period again * Move back unsafe_pruning to import_params * Applied proposed changes * aura: remove unused tx pool (#5046) * aura: remove unused transaction pool parameter * node-template: remove transaction pool from aura * aura: fix tests * Extend rust nightly detection in `wasm-builder` (#5021) Instead of just testing `cargo` and `rustup run nightly`, we now test the `CARGO` env variable and also scan non default nightlies. The user is also now able to select the toolchain with `WASM_BUILD_TOOLCHAIN`. * Add steps setting to benchmarking CLI (#5033) * Add steps setting to CLI, use max value to hit worst case. * Bump impl_version. * Apply review suggestion. * Remove indices from node-template (#5025) * Remove indices from node-template * Use identity lookup instead * Bump impl * clean cargo.toml * Fix documentation for "BlockBuilder::push_trusted" (#5051) * fix doc * rephrase * do not check unleash on every PR, only master and tags (#5054) * do not check unleash on every PR, only master and tags * move scripts folder * add signed-tag check to CI * remove publish-to-crates-io dependencies Co-authored-by: s3krit * prepare version to alpha.1 (#5055) bump version to -alpha.1 * Sync: validate block responses for required data (#5052) * Less verbose state-db logging * Validate block responses for block bodies * Update client/network/src/protocol.rs Co-Authored-By: Bastian Köcher * Added validation test * Disconnect on missing header as well * Typo Co-Authored-By: André Silva Co-authored-by: Bastian Köcher Co-authored-by: André Silva * Make these chainspecs fields private (#5031) * Fix dockerfile (#5059) * Adds documentation for `wipe` and `commit` (#5053) * Adds documentation for `wipe` and `commit` This adds documentation to `wipe` and `commit` of `Externalities`. Besides that it removes the default implementation that would just panic and requires that all implementers of the trait implement the functions. * Update primitives/externalities/src/lib.rs Co-Authored-By: joe petrowski <25483142+joepetrowski@users.noreply.github.com> Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> * Fix the issue with `trybuild`'s `ui` tests (#4992) * repro ui bug * fix the tests * test with the new image * test without CARGO_HOME * test without fixes * test again * fix trybuild old versions * bump CArgo.lock * fix trybuild newest versions * bump Cargo.lock * trying on the latest image * bump Cargo.lock * run with the old image * ci will be green on the image from 2020-02-19 [skip ci] * bump Cargo.lock * Activate publishing of draft releases... (#5062) * Activate publishing of draft releases... ... And fix the message sending (missing parameter). * publish_draft_release.sh now checks latest... ... release on github rather than just a tag * Fix/div by zero (#5041) * Handle gas_price being zero separately * Bump spec_version * Add a unit & integration tests for gas price = 0 * set missing metadata fields, prepping alpha.2 (#5067) * setting first batch of descriptions * fix what I just broke * next batch * and pallets, too * last batch * set cargo.lock * keep'em dev-deps * bump version to alpha.2 * Fix revalidation not revalidating multiple times (#5065) * removes use of sc_client::Client from sc_finality_grandpa (#5030) * removes use of sc_client::Client from sc_finality_grandpa * code formatting * code formatting * removes use of sc_client::Client from sc_finality_grandpa * Remove deprecated host functions (#5038) Sadly we need to keep one function `ext_blake2_256`. This function is manually defined in `sp-core`. * removes use of sc_client::Client from sc_basic_authorship (#5050) * removes use of sc-client from sc-basic-authorship * refactor use of ProposerFactory * correct dep path * pallet-transaction-payment clean up (#5070) * Formatting clean up * Introduce separate setters for the fees. * *: Rename prometheus-exporter crate to substrate-prometheus-end… (#5076) This patch renames the crate for the following two reasons: 1. The prometheus-exporter crate introduces native in-process Prometheus style instrumentation to the Substrate project. Within the Prometheus ecosystem the term "exporter" is used for external processes exposing metrics for e.g. the Linux Kernel. In-process exposition would be described via the term "endpoint". 2. "prometheus-exporter" is generic and ignores the fact that it is only usable within the context of Substrate. In addition the name "prometheus-exporter" is already taken on crates.io. * rename `browser-utils` to `substrate-browser-utils` (#5079) * prepping for Alpha.3 (#5080) * Bump to alpha.3 * update gitlab-ci * Propagate DispatchError for benchmarks. (#5075) * Propagate DispatchError for benchmarks. * Apply review suggestions. * Use RuntimeString. * fix expect Co-Authored-By: Bastian Köcher Co-authored-by: Bastian Köcher * Add options to overwrite range bounds in benchmark command. (#5072) * Add --mins --maxs to benchmark command. * Apply review suggestions. * Update yamux to version 0.4.4. (#5086) * Remove more instances of futures01 (#4633) * Start removing last few instances of futures01 * Use to_poll on wasm * Revert "Use to_poll on wasm" This reverts commit 1c61728f10d520df5f9b28c415a0db68e478b9c7. * Fix fg test * Upgrade network test futures * Update offchain hyper version * Update service test * bump tokio to 0.2.10 * Removed some unneeded tokios * fixes * fix run_until_all_full * Make service test debuggable * Update client/offchain/src/api/http.rs Co-Authored-By: Demi Obenour <48690212+DemiMarie-parity@users.noreply.github.com> * Add service_test to test-int output * nitpicking * Finally fix test * Give up and revert client/serviec/test * Revert gitlab ci too Co-authored-by: Demi Obenour * Make export blocks default to json on stdout (#5090) * Make export blocks default to json on stdout * Multiline instead of single line to stay under 100 cols * Change --json flag to --binary, defaulting to json * Offence reporting returns a result (#5082) * Offence reporting returns a result * Bump spec_version * Use unwrap instead of assertions * Fix more review grumbles * Update to libp2p 0.16.2 (#5088) * Remove request ID from the new protocol (#5049) * Make sure we remove a peer on disconnect in gossip (#5104) * Make sure we remove peers on disconnect in gossip state machine * Clear up the code * Add a comment * Expose `state-db` memory info (#5110) This exposes memory statistics from the state-db. * Change extrinsic_count to extrinsic_index in pallet-utility (#5044) Co-authored-by: Benjamin Kampmann * Add more metrics to prometheus (#5034) * Add a few things * Add finality_grandpa_round * fix fg tests * Nitpicks * Nitpicks * Fix name of prometheus crate * Update to SCALE 1.2.0 (#5113) This updates `parity-scale-codec` to `1.2.0`, which includes multiple performance improvements and a fix that bounds the capacity of a vector at decoding. * Lazy payouts (#4474) * TODOs * Remove superfluous: * partial implementation * full implementation * fix preferences * update comments * upgrade test WIP * fix more tests * fix cutoff * fix saturation * comment * upgrade mock * upgrade test * WIP migration * WIP migration * remove slot stake stuff * fix merge * migration of ledger * remove equalize from test * add test * fix * update doc * fix compilation * improve test readibility * improve doc * fix most todo * fix migration and test * remove println * WIP * add test and spec * weight * update doc * safer end_era * fix exposure of conversion * Revert "safer end_era" This reverts commit 72ff737d27be67d87308514b13e2574bc5f09fce. * fix useless put * exposure clipped * doc * fix payout with clipped * fix node runtime * add doc * pluggable and generalized staking module * remove print * update doc * refactor * improve documentation and implementation * fix test * Fix test * fix test * fix test * fix remove lowest stake from exposure, not biggest. * nomination index arguments in nominator_payout * add test * try to fix offence * apply slashed and bond eras until active era * doc * update spec version * add test upgrade from previous test environment * Apply suggestions from code review Co-Authored-By: Shawn Tabrizi * nominators upgrade has been cleaned * dynamic history depth implementation * make current_era - history_depth included * Change equality check to start era to less than or equal * Use era specific validator prefs * Add print statement and comment about start era if < * fix next_reward overflow * make more check for bad era claim for zero cost * small refactor * code refactor + fix use of deprecated storage * fix wasm build * add comment * Fix tests * remove outdated comment * Apply suggestions from code review Co-Authored-By: Shawn Tabrizi * gather active era information into one storage Co-authored-by: thiolliere Co-authored-by: Shawn Tabrizi * impl on_runtime_upgrade Co-authored-by: Benjamin Kampmann Co-authored-by: Cecile Tonglet Co-authored-by: André Silva Co-authored-by: Bastian Köcher Co-authored-by: Marcio Diaz Co-authored-by: Nikolay Volf Co-authored-by: s3krit Co-authored-by: Arkadiy Paronyan Co-authored-by: Pierre Krieger Co-authored-by: Chevdor Co-authored-by: joe petrowski <25483142+joepetrowski@users.noreply.github.com> Co-authored-by: Denis Pisarev Co-authored-by: Eric Co-authored-by: Seun Lanlege Co-authored-by: Sergei Pepyakin Co-authored-by: Max Inden Co-authored-by: Ashley Co-authored-by: Toralf Wittner Co-authored-by: Demi Obenour Co-authored-by: pscott <30843220+pscott@users.noreply.github.com> Co-authored-by: Fedor Sakharov Co-authored-by: Gavin Wood Co-authored-by: thiolliere --- frame/executive/src/lib.rs | 7 ++ frame/staking/src/lib.rs | 5 +- frame/support/src/dispatch.rs | 168 ++++++++++++++++++++++++++++++++++ frame/support/src/weights.rs | 10 ++ frame/system/src/lib.rs | 5 + 5 files changed, 194 insertions(+), 1 deletion(-) diff --git a/frame/executive/src/lib.rs b/frame/executive/src/lib.rs index 3ea1606b6789d..c36ce36b83069 100644 --- a/frame/executive/src/lib.rs +++ b/frame/executive/src/lib.rs @@ -186,6 +186,13 @@ where digest, frame_system::InitKind::Full, ); + let last_runtime_upgrade = >::last_runtime_upgrade(); + if last_runtime_upgrade.map(|n| n == *block_number).unwrap_or(false) { + ::on_runtime_upgrade(); + >::register_extra_weight_unchecked( + >::on_runtime_upgrade() + ); + } >::on_initialize(*block_number); >::register_extra_weight_unchecked( >::on_initialize(*block_number) diff --git a/frame/staking/src/lib.rs b/frame/staking/src/lib.rs index 3609191f4cb2c..e6afdd421190a 100644 --- a/frame/staking/src/lib.rs +++ b/frame/staking/src/lib.rs @@ -927,10 +927,13 @@ decl_module! { fn deposit_event() = default; - fn on_initialize() { + fn on_runtime_upgrade() { Self::ensure_storage_upgraded(); } + fn on_initialize() { + } + fn on_finalize() { // Set the start of the first era. if let Some(mut active_era) = Self::active_era() { diff --git a/frame/support/src/dispatch.rs b/frame/support/src/dispatch.rs index 084ea285af464..3c13de11be4a3 100644 --- a/frame/support/src/dispatch.rs +++ b/frame/support/src/dispatch.rs @@ -196,6 +196,10 @@ impl Parameter for T where T: Codec + EncodeLike + Clone + Eq + fmt::Debug {} /// /// The following reserved functions also take the block number (with type `T::BlockNumber`) as an optional input: /// +/// * `on_runtime_upgrade`: Executes at the beginning of a block prior to on_initialize when there +/// is a runtime ugprade. This allow each module to upgrade their storage, calling other module +/// must be avoided. Using this function will implement the +/// [`OnInitialize`](../sp_runtime/traits/trait.OnInitialize.html) trait. /// * `on_initialize`: Executes at the beginning of a block. Using this function will /// implement the [`OnInitialize`](../sp_runtime/traits/trait.OnInitialize.html) trait. /// * `on_finalize`: Executes at the end of a block. Using this function will @@ -229,6 +233,7 @@ macro_rules! decl_module { {} {} {} + {} [] $($t)* ); @@ -261,6 +266,7 @@ macro_rules! decl_module { {} {} {} + {} [] $($t)* ); @@ -274,6 +280,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } {} { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -290,6 +297,7 @@ macro_rules! decl_module { { $( $other_where_bounds )* } { $vis fn deposit_event() = default; } { $( $on_initialize )* } + { $( $on_runtime_upgrade )* } { $( $on_finalize )* } { $( $offchain )* } { $( $constants )* } @@ -305,6 +313,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } {} { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -326,6 +335,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } {} { $( $offchain:tt )* } { $( $constants:tt )* } @@ -342,6 +352,7 @@ macro_rules! decl_module { { $( $other_where_bounds )* } { $( $deposit_event )* } { $( $on_initialize )* } + { $( $on_runtime_upgrade )* } { #[weight = $crate::dispatch::SimpleDispatchInfo::zero()] fn on_finalize( $( $param_name : $param ),* ) { $( $impl )* } @@ -361,6 +372,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } {} { $( $offchain:tt )* } { $( $constants:tt )* } @@ -378,6 +390,7 @@ macro_rules! decl_module { { $( $other_where_bounds )* } { $( $deposit_event )* } { $( $on_initialize )* } + { $( $on_runtime_upgrade )* } { #[weight = $weight] fn on_finalize( $( $param_name : $param ),* ) { $( $impl )* } @@ -389,6 +402,85 @@ macro_rules! decl_module { $($rest)* ); }; + // Add on_runtime_upgrade, without a given weight. + (@normalize + $(#[$attr:meta])* + pub struct $mod_type:ident< + $trait_instance:ident: $trait_name:ident$(, I: $instantiable:path $(= $module_default_instance:path)?)? + > + for enum $call_type:ident where origin: $origin_type:ty, system = $system:ident + { $( $other_where_bounds:tt )* } + { $( $deposit_event:tt )* } + { $( $on_initialize:tt )* } + {} + { $( $on_finalize:tt )* } + { $( $offchain:tt )* } + { $( $constants:tt )* } + { $( $error_type:tt )* } + [ $( $dispatchables:tt )* ] + $(#[doc = $doc_attr:tt])* + fn on_runtime_upgrade( $( $param_name:ident : $param:ty ),* $(,)? ) { $( $impl:tt )* } + $($rest:tt)* + ) => { + $crate::decl_module!(@normalize + $(#[$attr])* + pub struct $mod_type<$trait_instance: $trait_name$(, I: $instantiable $(= $module_default_instance)?)?> + for enum $call_type where origin: $origin_type, system = $system + { $( $other_where_bounds )* } + { $( $deposit_event )* } + { $( $on_initialize )* } + { + #[weight = $crate::dispatch::SimpleDispatchInfo::zero()] + fn on_runtime_upgrade( $( $param_name : $param ),* ) { $( $impl )* } + } + { $( $on_finalize )* } + { $( $offchain )* } + { $( $constants )* } + { $( $error_type )* } + [ $( $dispatchables )* ] + $($rest)* + ); + }; + // Add on_runtime_upgrade, given weight. + (@normalize + $(#[$attr:meta])* + pub struct $mod_type:ident< + $trait_instance:ident: $trait_name:ident$(, I: $instantiable:path $(= $module_default_instance:path)?)? + > + for enum $call_type:ident where origin: $origin_type:ty, system = $system:ident + { $( $other_where_bounds:tt )* } + { $( $deposit_event:tt )* } + { $( $on_initialize:tt )* } + {} + { $( $on_finalize:tt )* } + { $( $offchain:tt )* } + { $( $constants:tt )* } + { $( $error_type:tt )* } + [ $( $dispatchables:tt )* ] + $(#[doc = $doc_attr:tt])* + #[weight = $weight:expr] + fn on_runtime_upgrade( $( $param_name:ident : $param:ty ),* $(,)? ) { $( $impl:tt )* } + $($rest:tt)* + ) => { + $crate::decl_module!(@normalize + $(#[$attr])* + pub struct $mod_type<$trait_instance: $trait_name$(, I: $instantiable $(= $module_default_instance)?)?> + for enum $call_type where origin: $origin_type, system = $system + { $( $other_where_bounds )* } + { $( $deposit_event )* } + { $( $on_initialize )* } + { + #[weight = $weight] + fn on_runtime_upgrade( $( $param_name : $param ),* ) { $( $impl )* } + } + { $( $on_finalize )* } + { $( $offchain )* } + { $( $constants )* } + { $( $error_type )* } + [ $( $dispatchables )* ] + $($rest)* + ); + }; // Add on_initialize, without a given weight. (@normalize $(#[$attr:meta])* @@ -399,6 +491,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } {} + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -418,6 +511,7 @@ macro_rules! decl_module { #[weight = $crate::dispatch::SimpleDispatchInfo::zero()] fn on_initialize( $( $param_name : $param ),* ) { $( $impl )* } } + { $( $on_runtime_upgrade )* } { $( $on_finalize )* } { $( $offchain )* } { $( $constants )* } @@ -436,6 +530,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } {} + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -456,6 +551,7 @@ macro_rules! decl_module { #[weight = $weight] fn on_initialize( $( $param_name : $param ),* ) { $( $impl )* } } + { $( $on_runtime_upgrade )* } { $( $on_finalize )* } { $( $offchain )* } { $( $constants )* } @@ -474,6 +570,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { } { $( $constants:tt )* } @@ -492,6 +589,7 @@ macro_rules! decl_module { { $( $other_where_bounds )* } { $( $deposit_event )* } { $( $on_initialize )* } + { $( $on_runtime_upgrade )* } { $( $on_finalize )* } { fn offchain_worker( $( $param_name : $param ),* ) { $( $impl )* } } { $( $constants )* } @@ -512,6 +610,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -531,6 +630,7 @@ macro_rules! decl_module { { $( $other_where_bounds )* } { $( $deposit_event )* } { $( $on_initialize )* } + { $( $on_runtime_upgrade )* } { $( $on_finalize )* } { $( $offchain )* } { @@ -555,6 +655,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -573,6 +674,7 @@ macro_rules! decl_module { { $( $other_where_bounds )* } { $( $deposit_event )* } { $( $on_initialize )* } + { $( $on_runtime_upgrade )* } { $( $on_finalize )* } { $( $offchain )* } { $( $constants )* } @@ -592,6 +694,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -608,6 +711,7 @@ macro_rules! decl_module { { $( $other_where_bounds )* } { $( $deposit_event )* } { $( $on_initialize )* } + { $( $on_runtime_upgrade )* } { $( $on_finalize )* } { $( $offchain )* } { $( $constants )* } @@ -628,6 +732,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -649,6 +754,7 @@ macro_rules! decl_module { { $( $other_where_bounds )* } { $( $deposit_event )* } { $( $on_initialize )* } + { $( $on_runtime_upgrade )* } { $( $on_finalize )* } { $( $offchain )* } { $( $constants )* } @@ -676,6 +782,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -696,6 +803,7 @@ macro_rules! decl_module { { $( $other_where_bounds )* } { $( $deposit_event )* } { $( $on_initialize )* } + { $( $on_runtime_upgrade )* } { $( $on_finalize )* } { $( $offchain )* } { $( $constants )* } @@ -717,6 +825,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -742,6 +851,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -767,6 +877,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -793,6 +904,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -808,6 +920,7 @@ macro_rules! decl_module { { $( $other_where_bounds )* } { $( $deposit_event )* } { $( $on_initialize )* } + { $( $on_runtime_upgrade )* } { $( $on_finalize )* } { $( $offchain )* } { $( $constants )* } @@ -903,6 +1016,39 @@ macro_rules! decl_module { {} }; + (@impl_on_runtime_upgrade + $module:ident<$trait_instance:ident: $trait_name:ident$(, $instance:ident: $instantiable:path)?>; + { $( $other_where_bounds:tt )* } + #[weight = $weight:expr] + fn on_runtime_upgrade() { $( $impl:tt )* } + ) => { + impl<$trait_instance: $trait_name$(, $instance: $instantiable)?> + $crate::sp_runtime::traits::OnRuntimeUpgrade + for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )* + { + fn on_runtime_upgrade() { + use $crate::sp_std::if_std; + if_std! { + use $crate::tracing; + let span = tracing::span!(tracing::Level::DEBUG, "on_runtime_upgrade"); + let _enter = span.enter(); + } + { $( $impl )* } + } + } + }; + + (@impl_on_runtime_upgrade + $module:ident<$trait_instance:ident: $trait_name:ident$(, $instance:ident: $instantiable:path)?>; + { $( $other_where_bounds:tt )* } + ) => { + impl<$trait_instance: $trait_name$(, $instance: $instantiable)?> + $crate::sp_runtime::traits::OnRuntimeUpgrade + for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )* + {} + }; + + (@impl_on_finalize $module:ident<$trait_instance:ident: $trait_name:ident$(, $instance:ident: $instantiable:path)?>; { $( $other_where_bounds:tt )* } @@ -961,6 +1107,10 @@ macro_rules! decl_module { (@impl_block_hooks_weight $module:ident<$trait_instance:ident: $trait_name:ident$(, $instance:ident: $instantiable:path)?>; { $( $other_where_bounds:tt )* } + @runtime_upgrade $( + #[weight = $weight_runtime_update:expr] + fn on_runtime_upgrade($( $param_runtime_upgrade:ident : $param_ty_runtime_upgrade:ty )*) { $( $impl_runtime_upgrade:tt )* } + )? @init $( #[weight = $weight_initialize:expr] fn on_initialize($( $param_initialize:ident : $param_ty_initialize:ty )*) { $( $impl_initialize:tt )* } @@ -974,6 +1124,11 @@ macro_rules! decl_module { $crate::dispatch::WeighBlock<$trait_instance::BlockNumber> for $module<$trait_instance$(, $instance)?> where $( $other_where_bounds )* { + $( + fn on_runtime_upgrade() -> $crate::dispatch::Weight { + >::weigh_data(&$weight_initialize, ()) + } + )? $( fn on_initialize(n: $trait_instance::BlockNumber) -> $crate::dispatch::Weight { >::weigh_data(&$weight_initialize, n) @@ -1208,6 +1363,7 @@ macro_rules! decl_module { { $( $other_where_bounds:tt )* } { $( $deposit_event:tt )* } { $( $on_initialize:tt )* } + { $( $on_runtime_upgrade:tt )* } { $( $on_finalize:tt )* } { $( $offchain:tt )* } { $( $constants:tt )* } @@ -1231,6 +1387,14 @@ macro_rules! decl_module { $( $on_initialize )* } + $crate::decl_module! { + @impl_on_runtime_upgrade + $mod_type<$trait_instance: $trait_name $(, $instance: $instantiable)?>; + { $( $other_where_bounds )* } + $( $on_runtime_upgrade )* + } + + $crate::decl_module! { @impl_on_finalize $mod_type<$trait_instance: $trait_name $(, $instance: $instantiable)?>; @@ -1242,6 +1406,7 @@ macro_rules! decl_module { @impl_block_hooks_weight $mod_type<$trait_instance: $trait_name $(, $instance: $instantiable)?>; { $( $other_where_bounds )* } + @runtime_upgrade $( $on_runtime_upgrade )* @init $( $on_initialize )* @fin $( $on_finalize )* } @@ -1869,6 +2034,9 @@ macro_rules! __check_reserved_fn_name { (on_initialize $( $rest:ident )*) => { $crate::__check_reserved_fn_name!(@compile_error on_initialize); }; + (on_runtime_upgrade $( $rest:ident )*) => { + $crate::__check_reserved_fn_name!(@compile_error on_runtime_upgrade); + }; (on_initialise $( $rest:ident )*) => { $crate::__check_reserved_fn_name!(@compile_error_renamed on_initialise on_initialize); }; diff --git a/frame/support/src/weights.rs b/frame/support/src/weights.rs index c46cca683ba93..883aed693ddda 100644 --- a/frame/support/src/weights.rs +++ b/frame/support/src/weights.rs @@ -70,6 +70,8 @@ pub trait ClassifyDispatch { /// Means of determining the weight of a block's life cycle hooks: `on_initialize`, `on_finalize` and /// such. pub trait WeighBlock { + /// Return the weight of the block's on_runtime_upgrade hook. + fn on_runtime_upgrade() -> Weight { Zero::zero() } /// Return the weight of the block's on_initialize hook. fn on_initialize(_: BlockNumber) -> Weight { Zero::zero() } /// Return the weight of the block's on_finalize hook. @@ -87,6 +89,14 @@ pub trait PaysFee { /// Maybe I can do something to remove the duplicate code here. #[impl_for_tuples(30)] impl WeighBlock for SingleModule { + fn on_runtime_upgrade() -> Weight { + let mut accumulated_weight: Weight = Zero::zero(); + for_tuples!( + #( accumulated_weight = accumulated_weight.saturating_add(SingleModule::on_runtime_upgrade()); )* + ); + accumulated_weight + } + fn on_initialize(n: BlockNumber) -> Weight { let mut accumulated_weight: Weight = Zero::zero(); for_tuples!( diff --git a/frame/system/src/lib.rs b/frame/system/src/lib.rs index f89c9efbd2962..7968e11082131 100644 --- a/frame/system/src/lib.rs +++ b/frame/system/src/lib.rs @@ -367,6 +367,9 @@ decl_storage! { /// the `EventIndex` then in case if the topic has the same contents on the next block /// no notification will be triggered thus the event might be lost. EventTopics get(fn event_topics): map hasher(blake2_256) T::Hash => Vec<(T::BlockNumber, EventIndex)>; + + /// Store the block number of last runtime upgrade. + LastRuntimeUpgrade get(fn last_runtime_upgrade): Option; } add_extra_genesis { config(changes_trie_config): Option; @@ -486,6 +489,7 @@ decl_module! { } storage::unhashed::put_raw(well_known_keys::CODE, &code); + >::put(Self::block_number()); Self::deposit_event(RawEvent::CodeUpdated); } @@ -494,6 +498,7 @@ decl_module! { pub fn set_code_without_checks(origin, code: Vec) { ensure_root(origin)?; storage::unhashed::put_raw(well_known_keys::CODE, &code); + >::put(Self::block_number()); Self::deposit_event(RawEvent::CodeUpdated); }