Browse Source

Merge pull request #284 from mnaamani/migration-constantinople

Runtime: Migration module - migrations for constantinople
Bedeho Mender 4 years ago
parent
commit
82b6109a76

+ 4 - 2
node/src/chain_spec.rs

@@ -23,8 +23,9 @@ use node_runtime::{
     AuthorityDiscoveryConfig, BabeConfig, Balance, BalancesConfig, ContentWorkingGroupConfig,
     CouncilConfig, CouncilElectionConfig, DataObjectStorageRegistryConfig,
     DataObjectTypeRegistryConfig, ElectionParameters, GrandpaConfig, ImOnlineConfig, IndicesConfig,
-    MembersConfig, Perbill, ProposalsCodexConfig, SessionConfig, SessionKeys, Signature,
-    StakerStatus, StakingConfig, SudoConfig, SystemConfig, VersionedStoreConfig, DAYS, WASM_BINARY,
+    MembersConfig, MigrationConfig, Perbill, ProposalsCodexConfig, SessionConfig, SessionKeys,
+    Signature, StakerStatus, StakingConfig, SudoConfig, SystemConfig, VersionedStoreConfig, DAYS,
+    WASM_BINARY,
 };
 pub use node_runtime::{AccountId, GenesisConfig};
 use primitives::{sr25519, Pair, Public};
@@ -301,6 +302,7 @@ pub fn testnet_genesis(
             channel_banner_constraint: crate::forum_config::new_validation(5, 1024),
             channel_title_constraint: crate::forum_config::new_validation(5, 1024),
         }),
+        migration: Some(MigrationConfig {}),
         proposals_codex: Some(ProposalsCodexConfig {
             set_validator_count_proposal_voting_period: cpcp
                 .set_validator_count_proposal_voting_period,

+ 1 - 1
runtime-modules/content-working-group/src/lib.rs

@@ -1894,7 +1894,7 @@ decl_module! {
             origin,
             curator_id: CuratorId<T>,
             rationale_text: Vec<u8>
-            ) {
+        ) {
 
             // Ensure lead is set and is origin signer
             Self::ensure_origin_is_set_lead(origin)?;

+ 1 - 1
runtime/src/lib.rs

@@ -907,12 +907,12 @@ construct_runtime!(
         RandomnessCollectiveFlip: randomness_collective_flip::{Module, Call, Storage},
         Sudo: sudo,
         // Joystream
+        Migration: migration::{Module, Call, Storage, Event<T>, Config},
         CouncilElection: election::{Module, Call, Storage, Event<T>, Config<T>},
         Council: council::{Module, Call, Storage, Event<T>, Config<T>},
         Memo: memo::{Module, Call, Storage, Event<T>},
         Members: members::{Module, Call, Storage, Event<T>, Config<T>},
         Forum: forum::{Module, Call, Storage, Event<T>, Config<T>},
-        Migration: migration::{Module, Call, Storage, Event<T>},
         Actors: actors::{Module, Call, Storage, Event<T>, Config},
         DataObjectTypeRegistry: data_object_type_registry::{Module, Call, Storage, Event<T>, Config<T>},
         DataDirectory: data_directory::{Module, Call, Storage, Event<T>},

+ 37 - 18
runtime/src/migration.rs

@@ -2,24 +2,36 @@
 #![allow(clippy::redundant_closure_call)] // disable it because of the substrate lib design
 
 use crate::VERSION;
+use rstd::prelude::*;
 use sr_primitives::{print, traits::Zero};
-use srml_support::{decl_event, decl_module, decl_storage};
+use srml_support::{debug, decl_event, decl_module, decl_storage};
 
 impl<T: Trait> Module<T> {
+    /// This method is called from on_initialize() when a runtime upgrade is detected. This
+    /// happens when the runtime spec version is found to be higher than the stored value.
+    /// Important to note this method should be carefully maintained, because it runs on every runtime
+    /// upgrade.
     fn runtime_upgraded() {
-        print("running runtime initializers...");
+        print("Running runtime upgraded handler");
 
-        // ...
-        // add initialization of modules introduced in new runtime release. This
+        // Add initialization of modules introduced in new runtime release. Typically this
         // would be any new storage values that need an initial value which would not
-        // have been initialized with config() or build() mechanism.
-        // ...
+        // have been initialized with config() or build() chainspec construction mechanism.
+        // Other tasks like resetting values, migrating values etc.
+
+        // Runtime Upgrade Code for going from Rome to Constantinople
 
         // Create the Council mint. If it fails, we can't do anything about it here.
-        let _ = governance::council::Module::<T>::create_new_council_mint(
+        if let Err(err) = governance::council::Module::<T>::create_new_council_mint(
             minting::BalanceOf::<T>::zero(),
-        );
+        ) {
+            debug::warn!(
+                "Failed to create a mint for council during migration: {:?}",
+                err
+            );
+        }
 
+        // Initialise the proposal system various periods
         proposals_codex::Module::<T>::set_default_config_values();
 
         Self::deposit_event(RawEvent::Migrated(
@@ -31,11 +43,9 @@ impl<T: Trait> Module<T> {
 
 pub trait Trait:
     system::Trait
-    + storage::data_directory::Trait
-    + storage::data_object_storage_registry::Trait
-    + forum::Trait
-    + sudo::Trait
-    + governance::council::Trait
+    + governance::election::Trait
+    + content_working_group::Trait
+    + roles::actors::Trait
     + proposals_codex::Trait
 {
     type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
@@ -43,9 +53,13 @@ pub trait Trait:
 
 decl_storage! {
     trait Store for Module<T: Trait> as Migration {
-        /// Records at what runtime spec version the store was initialized. This allows the runtime
-        /// to know when to run initialize code if it was installed as an update.
-        pub SpecVersion get(spec_version) build(|_| VERSION.spec_version) : Option<u32>;
+        /// Records at what runtime spec version the store was initialized. At genesis this will be
+        /// initialized to Some(VERSION.spec_version). It is an Option because the first time the module
+        /// was introduced was as a runtime upgrade and type was never changed.
+        /// When the runtime is upgraded the spec version be updated.
+        pub SpecVersion get(spec_version) build(|_config: &GenesisConfig| {
+            VERSION.spec_version
+        }) : Option<u32>;
     }
 }
 
@@ -61,11 +75,16 @@ decl_module! {
 
         fn on_initialize(_now: T::BlockNumber) {
             if Self::spec_version().map_or(true, |spec_version| VERSION.spec_version > spec_version) {
-                // mark store version with current version of the runtime
+                // Mark store version with current version of the runtime
                 SpecVersion::put(VERSION.spec_version);
 
-                // run migrations and store initializers
+                // Run migrations and store initializers
                 Self::runtime_upgraded();
+
+                Self::deposit_event(RawEvent::Migrated(
+                    <system::Module<T>>::block_number(),
+                    VERSION.spec_version,
+                ));
             }
         }
     }