diff --git a/Cargo.lock b/Cargo.lock index d3db8a61fe..9b1cd8853c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1304,7 +1304,9 @@ dependencies = [ "darwinia-support", "frame-support", "frame-system", + "log", "parity-scale-codec", + "paste 1.0.5", "serde", "serde_json", "sp-io", diff --git a/bin/node/cli/Cargo.toml b/bin/node/cli/Cargo.toml index f8cb4a35ea..93b8e618f7 100644 --- a/bin/node/cli/Cargo.toml +++ b/bin/node/cli/Cargo.toml @@ -94,6 +94,7 @@ try-runtime-cli = { optional = true, git = "https://g [build-dependencies] # substrate substrate-build-script-utils = { git = "https://github.com/darwinia-network/substrate.git", tag = "rococo-v1.2" } +try-runtime-cli = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "rococo-v1.2" } [features] default = [ diff --git a/bin/node/cli/src/chain_spec.rs b/bin/node/cli/src/chain_spec.rs index eca0c81423..ed323cd52e 100644 --- a/bin/node/cli/src/chain_spec.rs +++ b/bin/node/cli/src/chain_spec.rs @@ -324,7 +324,7 @@ fn pangolin_build_spec_genesis() -> pangolin_runtime::GenesisConfig { darwinia_claims: Default::default(), darwinia_vesting: Default::default(), pallet_sudo: pangolin_runtime::SudoConfig { key: root.clone() }, - darwinia_crab_issuing: pangolin_runtime::CrabIssuingConfig { total_mapped_ring: BUNCH_OF_COINS }, + darwinia_crab_issuing: pangolin_runtime::DarwiniaCrabIssuingConfig { total_mapped_ring: BUNCH_OF_COINS }, darwinia_crab_backing: pangolin_runtime::CrabBackingConfig { backed_ring: BUNCH_OF_COINS }, darwinia_ethereum_relay: pangolin_runtime::EthereumRelayConfig { genesis_header_info: ( @@ -477,7 +477,7 @@ fn pangolin_development_genesis() -> pangolin_runtime::GenesisConfig { }, darwinia_vesting: Default::default(), pallet_sudo: pangolin_runtime::SudoConfig { key: root.clone() }, - darwinia_crab_issuing: pangolin_runtime::CrabIssuingConfig { total_mapped_ring: BUNCH_OF_COINS }, + darwinia_crab_issuing: pangolin_runtime::DarwiniaCrabIssuingConfig { total_mapped_ring: BUNCH_OF_COINS }, darwinia_crab_backing: pangolin_runtime::CrabBackingConfig { backed_ring: BUNCH_OF_COINS }, darwinia_ethereum_relay: pangolin_runtime::EthereumRelayConfig { genesis_header_info: ( diff --git a/bin/node/cli/src/command.rs b/bin/node/cli/src/command.rs index d8ee8ac77c..5935ad7225 100644 --- a/bin/node/cli/src/command.rs +++ b/bin/node/cli/src/command.rs @@ -202,7 +202,10 @@ pub fn run() -> sc_cli::Result<()> { sc_service::TaskManager::new(config.task_executor.clone(), registry) .map_err(|e| sc_cli::Error::Service(sc_service::Error::Prometheus(e)))?; - Ok((cmd.run::(config), task_manager)) + Ok(( + cmd.run::(config), + task_manager, + )) }) } } diff --git a/bin/node/runtime/pangolin/Cargo.toml b/bin/node/runtime/pangolin/Cargo.toml index 9587d3ec9c..fbaa3118c6 100644 --- a/bin/node/runtime/pangolin/Cargo.toml +++ b/bin/node/runtime/pangolin/Cargo.toml @@ -48,7 +48,7 @@ frame-executive = { default-features = false, git = " frame-support = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "rococo-v1.2" } frame-system = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "rococo-v1.2" } frame-system-rpc-runtime-api = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "rococo-v1.2" } -frame-try-runtime = { optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "rococo-v1.2" } +frame-try-runtime = { default-features = false, optional = true, git = "https://github.com/darwinia-network/substrate.git", tag = "rococo-v1.2" } pallet-authority-discovery = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "rococo-v1.2" } pallet-authorship = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "rococo-v1.2" } pallet-babe = { default-features = false, git = "https://github.com/darwinia-network/substrate.git", tag = "rococo-v1.2" } @@ -127,6 +127,7 @@ std = [ "frame-support/std", "frame-system/std", "frame-system-rpc-runtime-api/std", + "frame-try-runtime/std", "pallet-authority-discovery/std", "pallet-authorship/std", "pallet-babe/std", @@ -166,8 +167,11 @@ std = [ "sp-version/std", ] +with-tracing = ["frame-executive/with-tracing"] + try-runtime = [ "frame-executive/try-runtime", "frame-try-runtime", + "darwinia-crab-issuing/try-runtime", + "darwinia-staking/try-runtime", ] -with-tracing = ["frame-executive/with-tracing"] diff --git a/bin/node/runtime/pangolin/src/lib.rs b/bin/node/runtime/pangolin/src/lib.rs index f9cfdff850..04c5bda3b9 100644 --- a/bin/node/runtime/pangolin/src/lib.rs +++ b/bin/node/runtime/pangolin/src/lib.rs @@ -362,11 +362,11 @@ use dvm_rpc_runtime_api::TransactionStatus; use impls::*; /// The address format for describing accounts. -type Address = MultiAddress; +pub type Address = MultiAddress; /// Block type as expected by this runtime. -type Block = generic::Block; +pub type Block = generic::Block; /// The SignedExtension to the basic transaction logic. -type SignedExtra = ( +pub type SignedExtra = ( frame_system::CheckSpecVersion, frame_system::CheckTxVersion, frame_system::CheckGenesis, @@ -377,19 +377,18 @@ type SignedExtra = ( darwinia_ethereum_relay::CheckEthereumRelayHeaderParcel, ); /// Unchecked extrinsic type as expected by this runtime. -type UncheckedExtrinsic = generic::UncheckedExtrinsic; +pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; /// Executive: handles dispatch to the various modules. -type Executive = frame_executive::Executive< +pub type Executive = frame_executive::Executive< Runtime, Block, frame_system::ChainContext, Runtime, AllPallets, - // (), - // CustomOnRuntimeUpgrade, + CustomOnRuntimeUpgrade, >; /// The payload being signed in transactions. -type SignedPayload = generic::SignedPayload; +pub type SignedPayload = generic::SignedPayload; type Ring = Balances; @@ -488,7 +487,7 @@ frame_support::construct_runtime! { // Multisig module. Late addition. Multisig: pallet_multisig::{Pallet, Call, Storage, Event} = 32, - CrabIssuing: darwinia_crab_issuing::{Pallet, Call, Storage, Config, Event} = 33, + DarwiniaCrabIssuing: darwinia_crab_issuing::{Pallet, Call, Storage, Config, Event} = 33, CrabBacking: darwinia_crab_backing::{Pallet, Storage, Config} = 34, EthereumRelay: darwinia_ethereum_relay::{Pallet, Call, Storage, Config, Event} = 35, @@ -881,7 +880,10 @@ impl_runtime_apis! { #[cfg(feature = "try-runtime")] impl frame_try_runtime::TryRuntime for Runtime { - fn on_runtime_upgrade() -> Result<(Weight, Weight), sp_runtime::RuntimeString> { + fn on_runtime_upgrade() -> Result< + (frame_support::weights::Weight, frame_support::weights::Weight), + sp_runtime::RuntimeString + > { let weight = Executive::try_runtime_upgrade()?; Ok((weight, RuntimeBlockWeights::get().max_block)) } @@ -907,12 +909,15 @@ impl dvm_rpc_runtime_api::ConvertTransaction for TransactionCon } } -// pub struct CustomOnRuntimeUpgrade; -// impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { -// fn on_runtime_upgrade() -> frame_support::weights::Weight { -// // --- substrate --- -// use frame_support::migration::*; +pub struct CustomOnRuntimeUpgrade; +impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade { + #[cfg(feature = "try-runtime")] + fn pre_upgrade() -> Result<(), &'static str> { + darwinia_crab_issuing::migration::try_runtime::pre_migrate::()?; + darwinia_staking::migrations::v6::pre_migrate::() + } -// MAXIMUM_BLOCK_WEIGHT -// } -// } + fn on_runtime_upgrade() -> frame_support::weights::Weight { + 0 + } +} diff --git a/frame/bridge/crab/issuing/Cargo.toml b/frame/bridge/crab/issuing/Cargo.toml index b6bc52087f..15b5460eaa 100644 --- a/frame/bridge/crab/issuing/Cargo.toml +++ b/frame/bridge/crab/issuing/Cargo.toml @@ -12,6 +12,8 @@ version = "2.1.0" [dependencies] # crates codec = { package = "parity-scale-codec", version = "2.0.1", default-features = false } +log = { version = "0.4.14", optional = true } +paste = { version = "1.0.5", optional = true } serde = { version = "1.0.124", optional = true } serde_json = { version = "1.0.62", optional = true } # substrate @@ -50,3 +52,9 @@ genesis-loader = [ "serde_json", "darwinia-support", ] + +try-runtime = [ + "log", + "paste", + "frame-support/try-runtime", +] diff --git a/frame/bridge/crab/issuing/src/lib.rs b/frame/bridge/crab/issuing/src/lib.rs index 3001fb72db..c6163afeb8 100644 --- a/frame/bridge/crab/issuing/src/lib.rs +++ b/frame/bridge/crab/issuing/src/lib.rs @@ -21,7 +21,8 @@ #![cfg_attr(not(feature = "std"), no_std)] pub mod weights; -// --- darwinia --- + +pub use pallet::*; pub use weights::WeightInfo; #[cfg(test)] @@ -29,86 +30,141 @@ mod mock; #[cfg(test)] mod tests; -mod types { +#[frame_support::pallet] +pub mod pallet { + pub mod types { + // --- darwinia --- + use super::*; + + // Generic type + pub type AccountId = ::AccountId; + pub type RingBalance = as Currency>>::Balance; + type RingCurrency = ::RingCurrency; + // Simple type + pub type MappedRing = u128; + } + pub use types::*; + + // --- substrate --- + use frame_support::{ + pallet_prelude::*, + traits::{Currency, Get}, + }; + use frame_system::pallet_prelude::*; + use sp_runtime::{traits::AccountIdConversion, ModuleId}; // --- darwinia --- - use crate::*; - - pub type MappedRing = u128; - - pub type AccountId = ::AccountId; - - pub type RingBalance = as Currency>>::Balance; - - type RingCurrency = ::RingCurrency; -} - -// --- substrate --- -use frame_support::{ - decl_error, decl_event, decl_module, decl_storage, - traits::{Currency, Get}, -}; -use sp_runtime::{traits::AccountIdConversion, ModuleId}; -// --- darwinia --- -use types::*; - -pub trait Config: frame_system::Config { - type Event: From> + Into<::Event>; - - type ModuleId: Get; - - type RingCurrency: Currency>; - - type WeightInfo: WeightInfo; -} - -decl_event! { - pub enum Event - where - AccountId = AccountId, - RingBalance = RingBalance, - { - /// Dummy Event. [who, swapped *CRING*, burned Mapped *RING*] - DummyEvent(AccountId, RingBalance, MappedRing), + use crate::weights::WeightInfo; + + #[pallet::config] + pub trait Config: frame_system::Config { + // --- substrate --- + type Event: From> + IsType<::Event>; + type WeightInfo: WeightInfo; + // --- darwinia --- + #[pallet::constant] + type ModuleId: Get; + type RingCurrency: Currency>; } -} -decl_error! { - pub enum Error for Module { + #[pallet::event] + pub enum Event { + /// Dummy Event. \[who, swapped *CRING*, burned Mapped *RING*\] + DummyEvent(AccountId, RingBalance, MappedRing), } -} -decl_storage! { - trait Store for Module as DarwiniaCrabIssuing { - pub TotalMappedRing get(fn total_mapped_ring) config(): MappedRing; - } + #[pallet::error] + pub enum Error {} + + #[pallet::storage] + #[pallet::getter(fn total_mapped_ring)] + pub type TotalMappedRing = StorageValue<_, MappedRing>; - add_extra_genesis { - build(|config| { + #[cfg_attr(feature = "std", derive(Default))] + #[pallet::genesis_config] + pub struct GenesisConfig { + pub total_mapped_ring: MappedRing, + } + #[pallet::genesis_build] + impl GenesisBuild for GenesisConfig { + fn build(&self) { let _ = T::RingCurrency::make_free_balance_be( - &>::account_id(), + &T::ModuleId::get().into_account(), T::RingCurrency::minimum_balance(), ); - TotalMappedRing::put(config.total_mapped_ring); - }); + >::put(self.total_mapped_ring); + } } + + #[pallet::pallet] + pub struct Pallet(_); + #[pallet::hooks] + impl Hooks> for Pallet {} + #[pallet::call] + impl Pallet {} } -decl_module! { - pub struct Module for enum Call - where - origin: T::Origin - { - type Error = Error; +pub mod migration { + const OLD_PALLET_NAME: &[u8] = b"DarwiniaCrabIssuing"; + + #[cfg(feature = "try-runtime")] + pub mod try_runtime { + // --- substrate --- + use frame_support::{pallet_prelude::*, traits::StorageInstance}; + // --- darwinia --- + use crate::*; + + macro_rules! generate_storage_types { + ($prefix:expr, $name:ident => Value<$value:ty>) => { + paste::paste! { + type $name = StorageValue<[<$name Instance>], $value, ValueQuery>; + + struct [<$name Instance>]; + impl StorageInstance for [<$name Instance>] { + const STORAGE_PREFIX: &'static str = "TotalMappedRing"; + + fn pallet_prefix() -> &'static str { $prefix } + } + } + }; + } + + generate_storage_types!("DarwiniaCrabIssuing", OldTotalMappedRing => Value<()>); + generate_storage_types!("CrabIssuing", NewTotalMappedRing => Value<()>); + + pub fn pre_migrate() -> Result<(), &'static str> { + log::info!( + "OldTotalMappedRing.exits()? {:?}", + OldTotalMappedRing::exists() + ); + log::info!( + "NewTotalMappedRing.exits()? {:?}", + NewTotalMappedRing::exists() + ); + + assert!(OldTotalMappedRing::exists()); + assert!(!NewTotalMappedRing::exists()); - const ModuleId: ModuleId = T::ModuleId::get(); + log::info!("Migrating `DarwiniaCrabIssuing` to `CrabIssuing`..."); + migration::migrate(b"CrabIssuing"); - fn deposit_event() = default; + log::info!( + "OldTotalMappedRing.exits()? {:?}", + OldTotalMappedRing::exists() + ); + log::info!( + "NewTotalMappedRing.exits()? {:?}", + NewTotalMappedRing::exists() + ); + + assert!(!OldTotalMappedRing::exists()); + assert!(NewTotalMappedRing::exists()); + + Ok(()) + } } -} -impl Module { - pub fn account_id() -> T::AccountId { - T::ModuleId::get().into_account() + pub fn migrate(new_pallet_name: &[u8]) { + frame_support::migration::move_pallet(OLD_PALLET_NAME, new_pallet_name); } } diff --git a/frame/bridge/crab/issuing/src/mock.rs b/frame/bridge/crab/issuing/src/mock.rs index 1571a76f3e..5835703dae 100644 --- a/frame/bridge/crab/issuing/src/mock.rs +++ b/frame/bridge/crab/issuing/src/mock.rs @@ -21,8 +21,10 @@ // --- crates --- use codec::{Decode, Encode}; // --- substrate --- +use frame_support::traits::GenesisBuild; use frame_system::mocking::*; use sp_io::TestExternalities; +use sp_runtime::ModuleId; use sp_runtime::{ testing::{Header, H256}, traits::{BlakeTwo256, IdentityLookup}, @@ -31,12 +33,12 @@ use sp_runtime::{ // --- darwinia --- use crate::{self as darwinia_crab_issuing, *}; +// Global primitives pub type Block = MockBlock; pub type UncheckedExtrinsic = MockUncheckedExtrinsic; - pub type AccountId = u64; pub type Balance = u128; - +// Pallet primitives pub type CrabIssuingError = Error; darwinia_support::impl_test_account_data! {} @@ -100,7 +102,7 @@ frame_support::construct_runtime! { { System: frame_system::{Pallet, Call, Storage, Config, Event}, Ring: darwinia_balances::::{Pallet, Call, Storage, Config, Event}, - CrabIssuing: darwinia_crab_issuing::{Pallet, Call, Storage, Config, Event}, + DarwiniaCrabIssuing: darwinia_crab_issuing::{Pallet, Call, Storage, Config, Event}, } } @@ -117,10 +119,12 @@ pub fn new_test_ext() -> TestExternalities { } .assimilate_storage(&mut t) .unwrap(); - darwinia_crab_issuing::GenesisConfig { - total_mapped_ring: 4_000, - } - .assimilate_storage::(&mut t) + >::assimilate_storage( + &darwinia_crab_issuing::GenesisConfig { + total_mapped_ring: 4_000, + }, + &mut t, + ) .unwrap(); t.into() diff --git a/frame/staking/Cargo.toml b/frame/staking/Cargo.toml index 02bf1229fb..d9039fcc58 100644 --- a/frame/staking/Cargo.toml +++ b/frame/staking/Cargo.toml @@ -80,3 +80,5 @@ substrate-std = [ "sp-staking/std", "sp-std/std", ] + +try-runtime = ["frame-support/try-runtime"]