diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs index f3c61895e..d8b31fe2a 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/lib.rs @@ -40,7 +40,7 @@ type EvmBalanceOf = const STORAGE_VERSION: StorageVersion = StorageVersion::new(1); /// The current bridges initializer version. -pub const CURRENT_BRIDGES_INITIALIZER_VERSION: u16 = 1; +pub const CURRENT_BRIDGES_INITIALIZER_VERSION: u16 = 2; // We have to temporarily allow some clippy lints. Later on we'll send patches to substrate to // fix them at their end. diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/mod.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/mod.rs index aa3853449..f030c7806 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/mod.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/mod.rs @@ -6,9 +6,11 @@ use crate::{self as pallet_balanced_currency_swap_bridges_initializer}; pub mod v0; pub mod v1; +pub mod v2; pub(crate) const EXISTENTIAL_DEPOSIT_NATIVE: u64 = 10; pub(crate) const EXISTENTIAL_DEPOSIT_EVM: u64 = 20; +pub(crate) const EXISTENTIAL_DEPOSIT_EVM_NEW: u64 = 1; pub(crate) type AccountId = u64; pub(crate) type EvmAccountId = u64; diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v2.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v2.rs new file mode 100644 index 000000000..5855ecfbf --- /dev/null +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/mock/v2.rs @@ -0,0 +1,135 @@ +//! The v2 mock. + +// Allow simple integer arithmetic in tests. +#![allow(clippy::integer_arithmetic)] + +use frame_support::{ + parameter_types, + sp_runtime::{ + testing::Header, + traits::{BlakeTwo256, Identity, IdentityLookup}, + }, + traits::{ConstU32, ConstU64, StorageMapShim}, + PalletId, +}; +use sp_core::H256; + +use super::*; + +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; + +frame_support::construct_runtime!( + pub enum Test where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system, + Balances: pallet_balances::, + EvmBalances: pallet_balances::, + SwapBridgeNativeToEvmPot: pallet_pot::, + SwapBridgeEvmToNativePot: pallet_pot::, + EvmNativeBridgesInitializer: pallet_balanced_currency_swap_bridges_initializer, + } +); + +impl frame_system::Config for Test { + type BaseCallFilter = frame_support::traits::Everything; + type BlockWeights = (); + type BlockLength = (); + type DbWeight = (); + type RuntimeOrigin = RuntimeOrigin; + type RuntimeCall = RuntimeCall; + type Index = u64; + type BlockNumber = u64; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = AccountId; + type Lookup = IdentityLookup; + type Header = Header; + type RuntimeEvent = RuntimeEvent; + type BlockHashCount = ConstU64<250>; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = pallet_balances::AccountData; + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = (); + type OnSetCode = (); + type MaxConsumers = ConstU32<16>; +} + +impl pallet_balances::Config for Test { + type Balance = u64; + type RuntimeEvent = RuntimeEvent; + type DustRemoval = (); + type ExistentialDeposit = ConstU64; + type AccountStore = System; + type MaxLocks = (); + type MaxReserves = (); + type ReserveIdentifier = [u8; 8]; + type WeightInfo = (); +} + +impl pallet_balances::Config for Test { + type Balance = u64; + type RuntimeEvent = RuntimeEvent; + type DustRemoval = (); + type ExistentialDeposit = ConstU64; + type AccountStore = StorageMapShim< + pallet_balances::Account, + frame_system::Provider, + EvmAccountId, + pallet_balances::AccountData, + >; + type MaxLocks = (); + type MaxReserves = (); + type ReserveIdentifier = [u8; 8]; + type WeightInfo = (); +} + +parameter_types! { + pub const SwapBridgeNativeToEvmPotPalletId: PalletId = PalletId(*b"humanoNE"); + pub const SwapBridgeEvmToNativePotPalletId: PalletId = PalletId(*b"humanoEN"); +} + +type PotInstanceSwapBridgeNativeToEvm = pallet_pot::Instance1; +type PotInstanceSwapBridgeEvmToNative = pallet_pot::Instance2; + +impl pallet_pot::Config for Test { + type RuntimeEvent = RuntimeEvent; + type AccountId = AccountId; + type PalletId = SwapBridgeNativeToEvmPotPalletId; + type Currency = Balances; +} + +impl pallet_pot::Config for Test { + type RuntimeEvent = RuntimeEvent; + type AccountId = EvmAccountId; + type PalletId = SwapBridgeEvmToNativePotPalletId; + type Currency = EvmBalances; +} + +parameter_types! { + pub const SwapBridgeNativeToEvmPalletId: PalletId = PalletId(*b"hmsb/ne1"); + pub const SwapBridgeEvmToNativePalletId: PalletId = PalletId(*b"hmsb/en1"); +} + +parameter_types! { + pub SwapBridgeNativeToEvmPotAccountId: AccountId = SwapBridgeNativeToEvmPot::account_id(); + pub SwapBridgeEvmToNativePotAccountId: AccountId = SwapBridgeEvmToNativePot::account_id(); +} + +impl pallet_balanced_currency_swap_bridges_initializer::Config for Test { + type EvmAccountId = EvmAccountId; + type NativeCurrency = Balances; + type EvmCurrency = EvmBalances; + type BalanceConverterEvmToNative = Identity; + type BalanceConverterNativeToEvm = Identity; + type NativeEvmBridgePot = SwapBridgeNativeToEvmPotAccountId; + type NativeTreasuryPot = NativeTreasury; + type EvmNativeBridgePot = SwapBridgeEvmToNativePotAccountId; + type WeightInfo = (); +} diff --git a/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs b/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs index 36f972d44..7122ff2c1 100644 --- a/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs +++ b/crates/pallet-balanced-currency-swap-bridges-initializer/src/tests.rs @@ -4,7 +4,7 @@ use frame_support::{ }; use crate::{ - mock::{new_test_ext_with, v0, v1, with_runtime_lock, *}, + mock::{new_test_ext_with, v0, v1, v2, with_runtime_lock, *}, swappable_balance, InitializerVersion, CURRENT_BRIDGES_INITIALIZER_VERSION, }; @@ -399,12 +399,9 @@ fn runtime_upgrade() { // Do runtime upgrade hook. v1::AllPalletsWithoutSystem::on_runtime_upgrade(); + >::put(1); // Verify bridges initialization result. - assert_eq!( - >::get(), - CURRENT_BRIDGES_INITIALIZER_VERSION - ); assert!(v1::EvmNativeBridgesInitializer::is_balanced().unwrap()); assert_eq!( v1::Balances::total_balance(&v1::SwapBridgeNativeToEvmPot::account_id()), @@ -426,12 +423,49 @@ fn runtime_upgrade() { - (EXISTENTIAL_DEPOSIT_EVM - EXISTENTIAL_DEPOSIT_NATIVE) ); assert_eq!( - v1::EvmBalances::total_balance(&v1::SwapBridgeEvmToNativePot::account_id(),), + v1::EvmBalances::total_balance(&v1::SwapBridgeEvmToNativePot::account_id()), v1::Balances::total_balance(&NativeTreasury::get()) + ALICE.balance + BOB.balance + EXISTENTIAL_DEPOSIT_EVM ); + + // Do runtime upgrade hook. + v2::AllPalletsWithoutSystem::on_runtime_upgrade(); + + let existential_deposit_balance = + EXISTENTIAL_DEPOSIT_EVM_NEW.max(EXISTENTIAL_DEPOSIT_NATIVE); + + // Verify bridges initialization result. + assert_eq!( + >::get(), + CURRENT_BRIDGES_INITIALIZER_VERSION + ); + assert!(v2::EvmNativeBridgesInitializer::is_balanced().unwrap()); + assert_eq!( + v2::Balances::total_balance(&v2::SwapBridgeNativeToEvmPot::account_id()), + LION.balance + + DOG.balance + + CAT.balance + + FISH.balance + + existential_deposit_balance + ); + assert_eq!( + v1::Balances::total_balance(&NativeTreasury::get()), + treasury.balance + - (LION.balance + + DOG.balance + + CAT.balance + + FISH.balance + + existential_deposit_balance) + ); + assert_eq!( + v2::EvmBalances::total_balance(&v2::SwapBridgeEvmToNativePot::account_id(),), + v2::Balances::total_balance(&NativeTreasury::get()) + + ALICE.balance + + BOB.balance + + existential_deposit_balance + ); }); }) }