Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

feat: migrate crab issuing to use pallet! #568

Merged
merged 9 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand Down Expand Up @@ -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: (
Expand Down
5 changes: 4 additions & 1 deletion bin/node/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Block, Executor>(config), task_manager))
Ok((
cmd.run::<service::pangolin_runtime::Block, service::PangolinExecutor>(config),
task_manager,
))
})
}
}
Expand Down
8 changes: 6 additions & 2 deletions bin/node/runtime/pangolin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"]
41 changes: 23 additions & 18 deletions bin/node/runtime/pangolin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,11 +362,11 @@ use dvm_rpc_runtime_api::TransactionStatus;
use impls::*;

/// The address format for describing accounts.
type Address = MultiAddress<AccountId, ()>;
pub type Address = MultiAddress<AccountId, ()>;
/// Block type as expected by this runtime.
type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
/// The SignedExtension to the basic transaction logic.
type SignedExtra = (
pub type SignedExtra = (
frame_system::CheckSpecVersion<Runtime>,
frame_system::CheckTxVersion<Runtime>,
frame_system::CheckGenesis<Runtime>,
Expand All @@ -377,19 +377,18 @@ type SignedExtra = (
darwinia_ethereum_relay::CheckEthereumRelayHeaderParcel<Runtime>,
);
/// Unchecked extrinsic type as expected by this runtime.
type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Call, Signature, SignedExtra>;
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Call, Signature, SignedExtra>;
/// Executive: handles dispatch to the various modules.
type Executive = frame_executive::Executive<
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPallets,
// (),
// CustomOnRuntimeUpgrade,
CustomOnRuntimeUpgrade,
>;
/// The payload being signed in transactions.
type SignedPayload = generic::SignedPayload<Call, SignedExtra>;
pub type SignedPayload = generic::SignedPayload<Call, SignedExtra>;

type Ring = Balances;

Expand Down Expand Up @@ -488,7 +487,7 @@ frame_support::construct_runtime! {
// Multisig module. Late addition.
Multisig: pallet_multisig::{Pallet, Call, Storage, Event<T>} = 32,

CrabIssuing: darwinia_crab_issuing::{Pallet, Call, Storage, Config, Event<T>} = 33,
DarwiniaCrabIssuing: darwinia_crab_issuing::{Pallet, Call, Storage, Config, Event<T>} = 33,
CrabBacking: darwinia_crab_backing::{Pallet, Storage, Config<T>} = 34,

EthereumRelay: darwinia_ethereum_relay::{Pallet, Call, Storage, Config<T>, Event<T>} = 35,
Expand Down Expand Up @@ -881,7 +880,10 @@ impl_runtime_apis! {

#[cfg(feature = "try-runtime")]
impl frame_try_runtime::TryRuntime<Block> 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))
}
Expand All @@ -907,12 +909,15 @@ impl dvm_rpc_runtime_api::ConvertTransaction<OpaqueExtrinsic> 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::<Runtime>()?;
darwinia_staking::migrations::v6::pre_migrate::<Runtime>()
}

// MAXIMUM_BLOCK_WEIGHT
// }
// }
fn on_runtime_upgrade() -> frame_support::weights::Weight {
0
}
}
8 changes: 8 additions & 0 deletions frame/bridge/crab/issuing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,3 +52,9 @@ genesis-loader = [
"serde_json",
"darwinia-support",
]

try-runtime = [
"log",
"paste",
"frame-support/try-runtime",
]
186 changes: 121 additions & 65 deletions frame/bridge/crab/issuing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,94 +21,150 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod weights;
// --- darwinia ---

pub use pallet::*;
pub use weights::WeightInfo;

#[cfg(test)]
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<T> = <T as frame_system::Config>::AccountId;
pub type RingBalance<T> = <RingCurrency<T> as Currency<AccountId<T>>>::Balance;
type RingCurrency<T> = <T as Config>::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<T> = <T as frame_system::Config>::AccountId;

pub type RingBalance<T> = <RingCurrency<T> as Currency<AccountId<T>>>::Balance;

type RingCurrency<T> = <T as Config>::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<Event<Self>> + Into<<Self as frame_system::Config>::Event>;

type ModuleId: Get<ModuleId>;

type RingCurrency: Currency<AccountId<Self>>;

type WeightInfo: WeightInfo;
}

decl_event! {
pub enum Event<T>
where
AccountId = AccountId<T>,
RingBalance = RingBalance<T>,
{
/// 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<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
type WeightInfo: WeightInfo;
// --- darwinia ---
#[pallet::constant]
type ModuleId: Get<ModuleId>;
type RingCurrency: Currency<AccountId<Self>>;
}
}

decl_error! {
pub enum Error for Module<T: Config> {
#[pallet::event]
pub enum Event<T: Config> {
/// Dummy Event. \[who, swapped *CRING*, burned Mapped *RING*\]
DummyEvent(AccountId<T>, RingBalance<T>, MappedRing),
}
}

decl_storage! {
trait Store for Module<T: Config> as DarwiniaCrabIssuing {
pub TotalMappedRing get(fn total_mapped_ring) config(): MappedRing;
}
#[pallet::error]
pub enum Error<T> {}

#[pallet::storage]
#[pallet::getter(fn total_mapped_ring)]
pub type TotalMappedRing<T: Config> = 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<T: Config> GenesisBuild<T> for GenesisConfig {
fn build(&self) {
let _ = T::RingCurrency::make_free_balance_be(
&<Module<T>>::account_id(),
&T::ModuleId::get().into_account(),
T::RingCurrency::minimum_balance(),
);

TotalMappedRing::put(config.total_mapped_ring);
});
<TotalMappedRing<T>>::put(self.total_mapped_ring);
}
}

#[pallet::pallet]
pub struct Pallet<T>(_);
#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
#[pallet::call]
impl<T: Config> Pallet<T> {}
}

decl_module! {
pub struct Module<T: Config> for enum Call
where
origin: T::Origin
{
type Error = Error<T>;
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<T: Config>() -> 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<T: Config> Module<T> {
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);
}
}
Loading