From 18182921f8e1c4a95079a4f609259ee78f319217 Mon Sep 17 00:00:00 2001 From: songtianyi Date: Tue, 23 Mar 2021 14:51:51 +0800 Subject: [PATCH 1/4] feat: migrate crab->backing from declarative macro to procedural macro --- bin/node/cli/src/chain_spec.rs | 4 +- bin/node/runtime/pangolin/src/lib.rs | 2 +- frame/bridge/crab/backing/src/lib.rs | 90 ++++++++++++++++------------ 3 files changed, 54 insertions(+), 42 deletions(-) diff --git a/bin/node/cli/src/chain_spec.rs b/bin/node/cli/src/chain_spec.rs index 28b5c33307..944df51356 100644 --- a/bin/node/cli/src/chain_spec.rs +++ b/bin/node/cli/src/chain_spec.rs @@ -219,7 +219,7 @@ fn pangolin_build_spec_genesis() -> pangolin_runtime::GenesisConfig { darwinia_crab_issuing: pangolin_runtime::CrabIssuingConfig { total_mapped_ring: 1 << 56 }, - darwinia_crab_backing: pangolin_runtime::CrabBackingConfig { + darwinia_crab_backing: pangolin_runtime::DarwiniaCrabBackingConfig { backed_ring: 1 << 56 }, darwinia_ethereum_relay: pangolin_runtime::EthereumRelayConfig { @@ -387,7 +387,7 @@ fn pangolin_development_genesis( darwinia_crab_issuing: pangolin_runtime::CrabIssuingConfig { total_mapped_ring: 1 << 56 }, - darwinia_crab_backing: pangolin_runtime::CrabBackingConfig { + darwinia_crab_backing: pangolin_runtime::DarwiniaCrabBackingConfig { backed_ring: 1 << 56 }, darwinia_ethereum_relay: pangolin_runtime::EthereumRelayConfig { diff --git a/bin/node/runtime/pangolin/src/lib.rs b/bin/node/runtime/pangolin/src/lib.rs index c4491e9e53..35f48e6738 100644 --- a/bin/node/runtime/pangolin/src/lib.rs +++ b/bin/node/runtime/pangolin/src/lib.rs @@ -481,7 +481,7 @@ frame_support::construct_runtime! { Multisig: pallet_multisig::{Module, Call, Storage, Event} = 32, CrabIssuing: darwinia_crab_issuing::{Module, Call, Storage, Config, Event} = 33, - CrabBacking: darwinia_crab_backing::{Module, Storage, Config} = 34, + DarwiniaCrabBacking: darwinia_crab_backing::{Module, Storage, Config} = 34, EthereumRelay: darwinia_ethereum_relay::{Module, Call, Storage, Config, Event} = 35, EthereumBacking: darwinia_ethereum_backing::{Module, Call, Storage, Config, Event} = 36, diff --git a/frame/bridge/crab/backing/src/lib.rs b/frame/bridge/crab/backing/src/lib.rs index ad0a951d6c..b7d07d8f33 100644 --- a/frame/bridge/crab/backing/src/lib.rs +++ b/frame/bridge/crab/backing/src/lib.rs @@ -20,66 +20,78 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub use pallet::*; + pub mod weights; // --- darwinia --- pub use weights::WeightInfo; +use frame_support::traits::Currency; + mod types { // --- darwinia --- - #[cfg(feature = "std")] use crate::*; pub type AccountId = ::AccountId; - #[cfg(feature = "std")] - pub type RingBalance = as Currency>>::Balance; - - #[cfg(feature = "std")] type RingCurrency = ::RingCurrency; + + pub type RingBalance = as Currency>>::Balance; } -// --- substrate --- -use frame_support::{ - decl_module, decl_storage, - traits::{Currency, Get}, -}; -use sp_runtime::{traits::AccountIdConversion, ModuleId}; -// --- darwinia --- -use types::*; +#[frame_support::pallet] +pub mod pallet { + use super::*; + use frame_support::pallet_prelude::*; + use frame_support::traits::{Currency, Get}; + use frame_system::pallet_prelude::*; + use sp_runtime::{traits::AccountIdConversion, ModuleId}; + use types::*; -pub trait Config: frame_system::Config { - type ModuleId: Get; + /// Configure the pallet by specifying the parameters and types on which it depends. + #[pallet::config] + pub trait Config: frame_system::Config { + #[pallet::constant] + type ModuleId: Get; - type RingCurrency: Currency>; + type RingCurrency: Currency>; - type WeightInfo: WeightInfo; -} + type WeightInfo: WeightInfo; -decl_storage! { - trait Store for Module as DarwiniaCrabBacking {} + // no event for this pallet + } - add_extra_genesis { - config(backed_ring): RingBalance; - build(|config| { - let _ = T::RingCurrency::make_free_balance_be( - &>::account_id(), - T::RingCurrency::minimum_balance() + config.backed_ring - ); - }); + // Define the pallet struct placeholder, various pallet function are implemented on it. + #[pallet::pallet] + pub struct Pallet(_); + + #[pallet::hooks] + impl Hooks> for Pallet {} + + #[pallet::call] + impl Pallet {} + + #[pallet::genesis_config] + pub struct GenesisConfig { + pub backed_ring: RingBalance, } -} -decl_module! { - pub struct Module for enum Call - where - origin: T::Origin - { - const ModuleId: ModuleId = T::ModuleId::get(); + #[cfg(feature = "std")] + impl Default for GenesisConfig { + fn default() -> Self { + Self { + backed_ring: Default::default(), + } + } } -} -impl Module { - pub fn account_id() -> T::AccountId { - T::ModuleId::get().into_account() + #[pallet::genesis_build] + impl GenesisBuild for GenesisConfig { + fn build(&self) { + let _ = T::RingCurrency::make_free_balance_be( + &T::ModuleId::get().into_account(), + T::RingCurrency::minimum_balance() + self.backed_ring, + ); + } } } From e9532b54be3f2a1770804262d8b379fd4b9b055e Mon Sep 17 00:00:00 2001 From: songtianyi Date: Mon, 29 Mar 2021 15:28:37 +0800 Subject: [PATCH 2/4] refact: follow pallet style --- frame/bridge/crab/backing/src/lib.rs | 79 +++++++++++++++------------- 1 file changed, 42 insertions(+), 37 deletions(-) diff --git a/frame/bridge/crab/backing/src/lib.rs b/frame/bridge/crab/backing/src/lib.rs index b7d07d8f33..0f016d351d 100644 --- a/frame/bridge/crab/backing/src/lib.rs +++ b/frame/bridge/crab/backing/src/lib.rs @@ -20,62 +20,53 @@ #![cfg_attr(not(feature = "std"), no_std)] -pub use pallet::*; - pub mod weights; -// --- darwinia --- -pub use weights::WeightInfo; - -use frame_support::traits::Currency; - -mod types { - // --- darwinia --- - use crate::*; - pub type AccountId = ::AccountId; - - type RingCurrency = ::RingCurrency; - - pub type RingBalance = as Currency>>::Balance; -} +pub use pallet::*; +pub use weights::WeightInfo; #[frame_support::pallet] pub mod pallet { - use super::*; - use frame_support::pallet_prelude::*; - use frame_support::traits::{Currency, Get}; + pub mod types { + // --- darwinia --- + use super::*; + + // Generic types + pub type AccountId = ::AccountId; + pub type RingBalance = as Currency>>::Balance; + type RingCurrency = ::RingCurrency; + } + pub use types::*; + + // --- substrate --- + use frame_support::{ + pallet_prelude::*, + traits::{Currency, Get}, + }; use frame_system::pallet_prelude::*; use sp_runtime::{traits::AccountIdConversion, ModuleId}; - use types::*; + // --- darwinia --- + use crate::weights::WeightInfo; - /// Configure the pallet by specifying the parameters and types on which it depends. #[pallet::config] pub trait Config: frame_system::Config { + // --- substrate --- + type WeightInfo: WeightInfo; + // --- darwinia --- #[pallet::constant] type ModuleId: Get; - type RingCurrency: Currency>; - - type WeightInfo: WeightInfo; - - // no event for this pallet } - // Define the pallet struct placeholder, various pallet function are implemented on it. - #[pallet::pallet] - pub struct Pallet(_); - - #[pallet::hooks] - impl Hooks> for Pallet {} - - #[pallet::call] - impl Pallet {} + // No event + // No error + // No storage + #[cfg(feature = "std")] #[pallet::genesis_config] pub struct GenesisConfig { pub backed_ring: RingBalance, } - #[cfg(feature = "std")] impl Default for GenesisConfig { fn default() -> Self { @@ -84,7 +75,6 @@ pub mod pallet { } } } - #[pallet::genesis_build] impl GenesisBuild for GenesisConfig { fn build(&self) { @@ -94,4 +84,19 @@ pub mod pallet { ); } } + + #[pallet::pallet] + pub struct Pallet(_); + #[pallet::hooks] + impl Hooks> for Pallet {} + #[pallet::call] + impl Pallet {} +} + +pub mod migration { + const OLD_PALLET_NAME: &[u8] = b"DarwiniaCrabBacking"; + + pub fn migrate(new_pallet_name: &[u8]) { + frame_support::migration::move_pallet(OLD_PALLET_NAME, new_pallet_name); + } } From f60708b638a877e94bdb1b2e8a36cf57963dae77 Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Wed, 31 Mar 2021 16:42:05 +0800 Subject: [PATCH 3/4] fix indent --- bin/node/cli/src/chain_spec.rs | 4 ++-- bin/node/runtime/pangolin/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/node/cli/src/chain_spec.rs b/bin/node/cli/src/chain_spec.rs index 74344fe118..50d5d0da90 100644 --- a/bin/node/cli/src/chain_spec.rs +++ b/bin/node/cli/src/chain_spec.rs @@ -325,7 +325,7 @@ fn pangolin_build_spec_genesis() -> pangolin_runtime::GenesisConfig { darwinia_vesting: Default::default(), pallet_sudo: pangolin_runtime::SudoConfig { key: root.clone() }, darwinia_crab_issuing: pangolin_runtime::DarwiniaCrabIssuingConfig { total_mapped_ring: BUNCH_OF_COINS }, - darwinia_crab_backing: pangolin_runtime::DarwiniaCrabBackingConfig { backed_ring: BUNCH_OF_COINS }, + darwinia_crab_backing: pangolin_runtime::DarwiniaCrabBackingConfig { backed_ring: BUNCH_OF_COINS }, darwinia_ethereum_relay: pangolin_runtime::EthereumRelayConfig { genesis_header_info: ( vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, 29, 204, 77, 232, 222, 199, 93, 122, 171, 133, 181, 103, 182, 204, 212, 26, 211, 18, 69, 27, 148, 138, 116, 19, 240, 161, 66, 253, 64, 212, 147, 71, 128, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 33, 123, 11, 188, 251, 114, 226, 213, 126, 40, 243, 60, 179, 97, 185, 152, 53, 19, 23, 119, 85, 220, 63, 51, 206, 62, 112, 34, 237, 98, 183, 123, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 136, 0, 0, 0, 0, 0, 0, 0, 66, 1, 65, 148, 16, 35, 104, 9, 35, 224, 254, 77, 116, 163, 75, 218, 200, 20, 31, 37, 64, 227, 174, 144, 98, 55, 24, 228, 125, 102, 209, 202, 74, 45], @@ -478,7 +478,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::DarwiniaCrabIssuingConfig { total_mapped_ring: BUNCH_OF_COINS }, - darwinia_crab_backing: pangolin_runtime::DarwiniaCrabBackingConfig { backed_ring: BUNCH_OF_COINS }, + darwinia_crab_backing: pangolin_runtime::DarwiniaCrabBackingConfig { backed_ring: BUNCH_OF_COINS }, darwinia_ethereum_relay: pangolin_runtime::EthereumRelayConfig { genesis_header_info: ( vec![0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 86, 232, 31, 23, 27, 204, 85, 166, 255, 131, 69, 230, 146, 192, 248, 110, 91, 72, 224, 27, 153, 108, 173, 192, 1, 98, 47, 181, 227, 99, 180, 33, 29, 204, 77, 232, 222, 199, 93, 122, 171, 133, 181, 103, 182, 204, 212, 26, 211, 18, 69, 27, 148, 138, 116, 19, 240, 161, 66, 253, 64, 212, 147, 71, 128, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 33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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 132, 160, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 136, 0, 0, 0, 0, 0, 0, 0, 66, 1, 65, 148, 16, 35, 104, 9, 35, 224, 254, 77, 116, 163, 75, 218, 200, 20, 31, 37, 64, 227, 174, 144, 98, 55, 24, 228, 125, 102, 209, 202, 74, 45], diff --git a/bin/node/runtime/pangolin/src/lib.rs b/bin/node/runtime/pangolin/src/lib.rs index 659c8297f5..99ba231171 100644 --- a/bin/node/runtime/pangolin/src/lib.rs +++ b/bin/node/runtime/pangolin/src/lib.rs @@ -488,7 +488,7 @@ frame_support::construct_runtime! { Multisig: pallet_multisig::{Pallet, Call, Storage, Event} = 32, DarwiniaCrabIssuing: darwinia_crab_issuing::{Pallet, Call, Storage, Config, Event} = 33, - DarwiniaCrabBacking: darwinia_crab_backing::{Pallet, Storage, Config} = 34, + DarwiniaCrabBacking: darwinia_crab_backing::{Pallet, Storage, Config} = 34, EthereumRelay: darwinia_ethereum_relay::{Pallet, Call, Storage, Config, Event} = 35, EthereumBacking: darwinia_ethereum_backing::{Pallet, Call, Storage, Config, Event} = 36, From 0184f7bb546c05bf4c7fd0a22ea12b465776861e Mon Sep 17 00:00:00 2001 From: Xavier Lau Date: Wed, 31 Mar 2021 17:10:51 +0800 Subject: [PATCH 4/4] remove unused code --- frame/bridge/crab/backing/src/lib.rs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/frame/bridge/crab/backing/src/lib.rs b/frame/bridge/crab/backing/src/lib.rs index 0f016d351d..743230b7e7 100644 --- a/frame/bridge/crab/backing/src/lib.rs +++ b/frame/bridge/crab/backing/src/lib.rs @@ -58,11 +58,6 @@ pub mod pallet { type RingCurrency: Currency>; } - // No event - // No error - // No storage - - #[cfg(feature = "std")] #[pallet::genesis_config] pub struct GenesisConfig { pub backed_ring: RingBalance, @@ -92,11 +87,3 @@ pub mod pallet { #[pallet::call] impl Pallet {} } - -pub mod migration { - const OLD_PALLET_NAME: &[u8] = b"DarwiniaCrabBacking"; - - pub fn migrate(new_pallet_name: &[u8]) { - frame_support::migration::move_pallet(OLD_PALLET_NAME, new_pallet_name); - } -}