Skip to content
Merged
1 change: 1 addition & 0 deletions crates/humanode-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,7 @@ impl pallet_balanced_currency_swap_bridges_initializer::Config for Runtime {
type NativeEvmBridgePot = NativeToEvmSwapBridgePotAccountId;
type NativeTreasuryPot = TreasuryPotAccountId;
type EvmNativeBridgePot = EvmToNativeSwapBridgePotAccountId;
type ForceRebalanceAskCounter = ConstU16<0>;
type WeightInfo = ();
}

Expand Down
2 changes: 1 addition & 1 deletion crates/humanode-runtime/src/tests/currency_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn currencies_are_balanced() {
// Build the state from the config.
new_test_ext_with().execute_with(move || {
assert_eq!(
BalancedCurrencySwapBridgesInitializer::initializer_version(),
BalancedCurrencySwapBridgesInitializer::last_initializer_version(),
pallet_balanced_currency_swap_bridges_initializer::CURRENT_BRIDGES_INITIALIZER_VERSION
);
assert!(BalancedCurrencySwapBridgesInitializer::is_balanced().unwrap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,22 @@ pub mod pallet {
/// The evm-native bridge pot account.
type EvmNativeBridgePot: Get<Self::EvmAccountId>;

/// The force rebalance ask counter.
type ForceRebalanceAskCounter: Get<u16>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}

/// The initializer version.
/// The last initializer version.
#[pallet::storage]
#[pallet::getter(fn last_initializer_version)]
pub type LastInitializerVersion<T: Config> = StorageValue<_, u16, ValueQuery>;

/// The last force rebalance ask counter.
#[pallet::storage]
#[pallet::getter(fn initializer_version)]
pub type InitializerVersion<T: Config> = StorageValue<_, u16, ValueQuery>;
#[pallet::getter(fn last_force_rebalance_ask_counter)]
pub type LastForceRebalanceAskCounter<T: Config> = StorageValue<_, u16, ValueQuery>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config>(PhantomData<T>);
Expand All @@ -129,7 +137,8 @@ pub mod pallet {
}
}

<InitializerVersion<T>>::put(CURRENT_BRIDGES_INITIALIZER_VERSION);
<LastInitializerVersion<T>>::put(CURRENT_BRIDGES_INITIALIZER_VERSION);
<LastForceRebalanceAskCounter<T>>::put(T::ForceRebalanceAskCounter::get());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ 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: u64 = 10;
pub(crate) const EXISTENTIAL_DEPOSIT_EVM_NEW: u64 = 1;
pub(crate) const CURRENT_BRIDGES_INITIALIZER_VERSION: u16 = 1;

pub(crate) type AccountId = u64;
pub(crate) type EvmAccountId = u64;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ use frame_support::{
traits::{ConstU32, ConstU64, StorageMapShim},
PalletId,
};
use sp_core::H256;
use sp_core::{ConstU16, H256};

use super::*;

pub(crate) const FORCE_REBALANCE_ASK_COUNTER: u16 = 0;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

Expand Down Expand Up @@ -131,5 +133,6 @@ impl pallet_balanced_currency_swap_bridges_initializer::Config for Test {
type NativeEvmBridgePot = SwapBridgeNativeToEvmPotAccountId;
type NativeTreasuryPot = NativeTreasury;
type EvmNativeBridgePot = SwapBridgeEvmToNativePotAccountId;
type ForceRebalanceAskCounter = ConstU16<FORCE_REBALANCE_ASK_COUNTER>;
type WeightInfo = ();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
//! 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::{ConstU16, H256};

pub(crate) const FORCE_REBALANCE_ASK_COUNTER: u16 = 1;

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 ForceRebalanceAskCounter = ConstU16<FORCE_REBALANCE_ASK_COUNTER>;
type WeightInfo = ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use frame_support::{
};

use crate::{
mock::{new_test_ext_with, v0, v1, with_runtime_lock, *},
swappable_balance, InitializerVersion, CURRENT_BRIDGES_INITIALIZER_VERSION,
mock::{new_test_ext_with, v0, v1, v2, with_runtime_lock, *},
swappable_balance, LastForceRebalanceAskCounter, LastInitializerVersion,
};

/// This test verifies that balanced bridges initialization works in case bridge pot accounts
Expand Down Expand Up @@ -55,9 +55,13 @@ fn initialization_bridges_ed_works() {
};
new_test_ext_with(config).execute_with(move || {
assert_eq!(
<InitializerVersion<v1::Test>>::get(),
<LastInitializerVersion<v1::Test>>::get(),
CURRENT_BRIDGES_INITIALIZER_VERSION
);
assert_eq!(
<LastForceRebalanceAskCounter<v1::Test>>::get(),
v1::FORCE_REBALANCE_ASK_COUNTER
);
assert_eq!(
v1::Balances::total_balance(&v1::SwapBridgeNativeToEvmPot::account_id()),
LION.balance
Expand Down Expand Up @@ -133,9 +137,13 @@ fn initialization_bridges_ed_delta_works() {
};
new_test_ext_with(config).execute_with(move || {
assert_eq!(
<InitializerVersion<v1::Test>>::get(),
<LastInitializerVersion<v1::Test>>::get(),
CURRENT_BRIDGES_INITIALIZER_VERSION
);
assert_eq!(
<LastForceRebalanceAskCounter<v1::Test>>::get(),
v1::FORCE_REBALANCE_ASK_COUNTER
);
assert_eq!(
v1::Balances::total_balance(&v1::SwapBridgeNativeToEvmPot::account_id()),
LION.balance
Expand Down Expand Up @@ -210,9 +218,13 @@ fn initialization_idempotency() {
new_test_ext_with(config).execute_with(move || {
// Verify that bridges initialization has been applied at genesis.
assert_eq!(
<InitializerVersion<v1::Test>>::get(),
<LastInitializerVersion<v1::Test>>::get(),
CURRENT_BRIDGES_INITIALIZER_VERSION
);
assert_eq!(
<LastForceRebalanceAskCounter<v1::Test>>::get(),
v1::FORCE_REBALANCE_ASK_COUNTER
);
assert!(v1::EvmNativeBridgesInitializer::is_balanced().unwrap());
assert_eq!(
v1::Balances::total_balance(&v1::SwapBridgeNativeToEvmPot::account_id()),
Expand Down Expand Up @@ -395,16 +407,21 @@ fn runtime_upgrade() {
v1::Balances::total_balance(&v1::SwapBridgeEvmToNativePot::account_id()),
0
);
assert_eq!(<InitializerVersion<v1::Test>>::get(), 0);
assert_eq!(<LastInitializerVersion<v1::Test>>::get(), 0);
assert_eq!(<LastForceRebalanceAskCounter<v1::Test>>::get(), 0);

// Do runtime upgrade hook.
v1::AllPalletsWithoutSystem::on_runtime_upgrade();

// Verify bridges initialization result.
assert_eq!(
<InitializerVersion<v1::Test>>::get(),
<LastInitializerVersion<v1::Test>>::get(),
CURRENT_BRIDGES_INITIALIZER_VERSION
);
assert_eq!(
<LastForceRebalanceAskCounter<v1::Test>>::get(),
v1::FORCE_REBALANCE_ASK_COUNTER
);
assert!(v1::EvmNativeBridgesInitializer::is_balanced().unwrap());
assert_eq!(
v1::Balances::total_balance(&v1::SwapBridgeNativeToEvmPot::account_id()),
Expand All @@ -426,12 +443,40 @@ 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
);

// Get bridges balances before runtime upgrade.
let native_evm_bridge_balance_before =
v2::Balances::total_balance(&v2::SwapBridgeNativeToEvmPot::account_id());
let evm_native_bridge_balance_before =
v2::EvmBalances::total_balance(&v2::SwapBridgeEvmToNativePot::account_id());

// Do runtime upgrade hook.
v2::AllPalletsWithoutSystem::on_runtime_upgrade();

// Verify result.
assert_eq!(
<LastInitializerVersion<v2::Test>>::get(),
CURRENT_BRIDGES_INITIALIZER_VERSION
);
assert_eq!(
<LastForceRebalanceAskCounter<v2::Test>>::get(),
v2::FORCE_REBALANCE_ASK_COUNTER
);
assert!(v2::EvmNativeBridgesInitializer::is_balanced().unwrap());
assert_eq!(
v2::Balances::total_balance(&v2::SwapBridgeNativeToEvmPot::account_id()),
native_evm_bridge_balance_before
);
assert_eq!(
v2::EvmBalances::total_balance(&v2::SwapBridgeEvmToNativePot::account_id()),
evm_native_bridge_balance_before
);
});
})
}
Loading