Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type EvmBalanceOf<T> =
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system,
Balances: pallet_balances::<Instance1>,
EvmBalances: pallet_balances::<Instance2>,
SwapBridgeNativeToEvmPot: pallet_pot::<Instance1>,
SwapBridgeEvmToNativePot: pallet_pot::<Instance2>,
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<AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = ConstU64<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = ConstU32<16>;
}

impl pallet_balances::Config<BalancesInstanceNative> for Test {
type Balance = u64;
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ConstU64<EXISTENTIAL_DEPOSIT_NATIVE>;
type AccountStore = System;
type MaxLocks = ();
type MaxReserves = ();
type ReserveIdentifier = [u8; 8];
type WeightInfo = ();
}

impl pallet_balances::Config<BalancesInstanceEvm> for Test {
type Balance = u64;
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ConstU64<EXISTENTIAL_DEPOSIT_EVM_NEW>;
type AccountStore = StorageMapShim<
pallet_balances::Account<Test, BalancesInstanceEvm>,
frame_system::Provider<Test>,
EvmAccountId,
pallet_balances::AccountData<Balance>,
>;
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<PotInstanceSwapBridgeNativeToEvm> for Test {
type RuntimeEvent = RuntimeEvent;
type AccountId = AccountId;
type PalletId = SwapBridgeNativeToEvmPotPalletId;
type Currency = Balances;
}

impl pallet_pot::Config<PotInstanceSwapBridgeEvmToNative> 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 = ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -399,12 +399,9 @@ fn runtime_upgrade() {

// Do runtime upgrade hook.
v1::AllPalletsWithoutSystem::on_runtime_upgrade();
<InitializerVersion<v1::Test>>::put(1);

// Verify bridges initialization result.
assert_eq!(
<InitializerVersion<v1::Test>>::get(),
CURRENT_BRIDGES_INITIALIZER_VERSION
);
assert!(v1::EvmNativeBridgesInitializer::is_balanced().unwrap());
assert_eq!(
v1::Balances::total_balance(&v1::SwapBridgeNativeToEvmPot::account_id()),
Expand All @@ -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!(
<InitializerVersion<v2::Test>>::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
);
});
})
}