Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
445ba54
correct rate limiter Cargo.toml
apopiak Jul 12, 2023
cb275b6
Merge branch 'feat/xcm-defer' into feat/xcm-rate-limiter
apopiak Jul 12, 2023
db44625
bump crate versions
apopiak Jul 12, 2023
79a64a1
Revert "remove xcm rate limiter from runtime"
apopiak Jul 24, 2023
144605e
update xcmp queue pallet
apopiak Jul 24, 2023
673f54e
Merge branch 'master' of github.com:galacticcouncil/HydraDX-node into…
apopiak Jul 24, 2023
33828ce
Merge branch 'master' into feat/xcm-rate-limiter
mrq1911 Jul 27, 2023
4d48737
Merge branch 'master' into feat/xcm-rate-limiter
apopiak Jul 28, 2023
353a6c6
Merge branch 'master' into feat/xcm-rate-limiter
mrq1911 Aug 8, 2023
a6ee5de
bump integration tests
dmoka Aug 8, 2023
8797e19
Merge remote-tracking branch 'origin/feat/xcm-rate-limiter' into feat…
dmoka Aug 8, 2023
a931eb0
bump runtime version
dmoka Aug 8, 2023
21f5866
change function order to follow execution orders
dmoka Aug 8, 2023
b743f77
bump patch versions of touched packages
dmoka Aug 8, 2023
c95d0e5
fix xcm rate limiter non deterministic tests as we use different rela…
dmoka Aug 8, 2023
ce57dbb
merge master to feat/xcm-rate-limiter
dmoka Aug 11, 2023
62ecd90
bump versions
dmoka Aug 11, 2023
b33182c
merge master to feat/xcm-rate-limiter
dmoka Aug 14, 2023
04440f3
bump runtime version
dmoka Aug 14, 2023
0671025
bump patch versions for touched packages
dmoka Aug 14, 2023
658b7be
update lock
dmoka Aug 14, 2023
fed4df2
Merge branch 'master' of github.com:galacticcouncil/HydraDX-node into…
apopiak Oct 13, 2023
54744fe
bump versions
apopiak Oct 20, 2023
c90e469
remove duplicate assert macro definitions
apopiak Oct 20, 2023
0a0682a
update xcmp queue pallet to new extended version
apopiak Oct 20, 2023
ff7c102
update integration tests
apopiak Oct 20, 2023
7a1eaed
formatting
apopiak Oct 20, 2023
d958f16
configure defer duration
apopiak Dec 8, 2023
6c88813
Merge branch 'feat/xcm-rate-limiter' into apopiak/rate-limiter-polkad…
apopiak Dec 22, 2023
c4f8c5c
fix merge issues and tests
apopiak Dec 22, 2023
c3e7610
formatting
apopiak Dec 22, 2023
f5de0ca
bump crate versions
apopiak Dec 22, 2023
bbbe2d4
make clippy happy
apopiak Dec 22, 2023
20380ce
Merge branch 'polkadot-v1.1.0' into apopiak/rate-limiter-polkadot-v1-1
Roznovjak Dec 24, 2023
c51a163
add previously removed fn
Roznovjak Dec 25, 2023
192a887
Merge branch 'polkadot-v1.1.0' into apopiak/rate-limiter-polkadot-v1-1
Roznovjak Dec 26, 2023
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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "runtime-integration-tests"
version = "1.17.0"
version = "1.17.1"
description = "Integration tests"
authors = ["GalacticCouncil"]
edition = "2021"
Expand Down
15 changes: 15 additions & 0 deletions integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,19 @@ mod staking;
mod transact_call_filter;
mod vesting;
mod xcm_defer;
mod xcm_rate_limiter;

#[macro_export]
macro_rules! assert_balance {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to have these macros here? I'm just curious. I wouldn't expect any code in this file.

( $who:expr, $asset:expr, $amount:expr) => {{
assert_eq!(Currencies::free_balance($asset, &$who), $amount);
}};
}

#[macro_export]
macro_rules! assert_reserved_balance {
( $who:expr, $asset:expr, $amount:expr) => {{
assert_eq!(Currencies::reserved_balance($asset, &$who), $amount);
}};
}
mod xyk;
14 changes: 0 additions & 14 deletions integration-tests/src/polkadot_test_net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,20 +685,6 @@ pub fn init_omnipool() {
set_zero_reward_for_referrals(HDX);
}

#[macro_export]
macro_rules! assert_balance {
( $who:expr, $asset:expr, $amount:expr) => {{
assert_eq!(Currencies::free_balance($asset, &$who), $amount);
}};
}

#[macro_export]
macro_rules! assert_reserved_balance {
( $who:expr, $asset:expr, $amount:expr) => {{
assert_eq!(Currencies::reserved_balance($asset, &$who), $amount);
}};
}

pub fn set_zero_reward_for_referrals(asset_id: AssetId) {
assert_ok!(Referrals::set_reward_percentage(
RawOrigin::Root.into(),
Expand Down
262 changes: 262 additions & 0 deletions integration-tests/src/xcm_rate_limiter.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
#![cfg(test)]

use crate::polkadot_test_net::*;

use frame_support::{assert_ok, pallet_prelude::Weight};
use orml_traits::currency::MultiCurrency;
use pallet_asset_registry::AssetType;
use polkadot_xcm::prelude::*;
use xcm_emulator::TestExt;

/// Returns the message hash in the `XcmpMessageSent` event at the `n`th last event (1-indexed, so if the second to last
/// event has the hash, pass `2`);
fn get_message_hash_from_event(n: usize) -> Option<[u8; 32]> {
use cumulus_pallet_xcmp_queue::Event;
use hydradx_runtime::RuntimeEvent;
let RuntimeEvent::XcmpQueue(Event::XcmpMessageSent { message_hash }) = &last_hydra_events(n)[0] else {
panic!("expecting to find message sent event");
};
Some(*message_hash)
}

#[test]
fn xcm_rate_limiter_should_limit_aca_when_limit_is_exceeded() {
// Arrange
TestNet::reset();

Hydra::execute_with(|| {
assert_ok!(hydradx_runtime::AssetRegistry::set_location(
hydradx_runtime::RuntimeOrigin::root(),
ACA,
hydradx_runtime::AssetLocation(MultiLocation::new(1, X2(Parachain(ACALA_PARA_ID), GeneralIndex(0))))
));

// set an xcm rate limit
assert_ok!(hydradx_runtime::AssetRegistry::update(
hydradx_runtime::RuntimeOrigin::root(),
ACA,
b"ACA".to_vec(),
AssetType::Token,
None,
Some(50 * UNITS),
));

assert_eq!(hydradx_runtime::Tokens::free_balance(ACA, &AccountId::from(BOB)), 0);

//Set it to same as the relay block number should be in XcmDeferFilter
//since we use different RelayChainBlockNumberProvider in runtime-benchmark feature
//where we return frame_system current time
frame_system::Pallet::<hydradx_runtime::Runtime>::set_block_number(4);
});

let amount = 100 * UNITS;
let mut message_hash = None;
Acala::execute_with(|| {
// Act
assert_ok!(hydradx_runtime::XTokens::transfer(
hydradx_runtime::RuntimeOrigin::signed(ALICE.into()),
0,
amount,
Box::new(
MultiLocation::new(
1,
X2(
Junction::Parachain(HYDRA_PARA_ID),
Junction::AccountId32 { id: BOB, network: None }
)
)
.into()
),
WeightLimit::Limited(Weight::from_parts(399_600_000_000, 0))
));

message_hash = get_message_hash_from_event(2);

// Assert
assert_eq!(
hydradx_runtime::Balances::free_balance(AccountId::from(ALICE)),
ALICE_INITIAL_NATIVE_BALANCE - amount
);
});

let relay_block = PolkadotRelay::execute_with(polkadot_runtime::System::block_number);

Hydra::execute_with(|| {
expect_hydra_events(vec![
cumulus_pallet_xcmp_queue::Event::XcmDeferred {
sender: ACALA_PARA_ID.into(),
sent_at: relay_block,
deferred_to: hydradx_runtime::DeferDuration::get() + relay_block - 1,
message_hash,
index: (hydradx_runtime::DeferDuration::get() + relay_block - 1, 0),
position: 0,
}
.into(),
pallet_relaychain_info::Event::CurrentBlockNumbers {
parachain_block_number: 5,
relaychain_block_number: relay_block + 1,
}
.into(),
]);
assert_eq!(hydradx_runtime::Tokens::free_balance(ACA, &AccountId::from(BOB)), 0);
});
}

#[test]
fn xcm_rate_limiter_should_not_limit_aca_when_limit_is_not_exceeded() {
// Arrange
TestNet::reset();

Hydra::execute_with(|| {
assert_ok!(hydradx_runtime::AssetRegistry::set_location(
hydradx_runtime::RuntimeOrigin::root(),
ACA,
hydradx_runtime::AssetLocation(MultiLocation::new(1, X2(Parachain(ACALA_PARA_ID), GeneralIndex(0))))
));

// set an xcm rate limit
assert_ok!(hydradx_runtime::AssetRegistry::update(
hydradx_runtime::RuntimeOrigin::root(),
ACA,
b"ACA".to_vec(),
AssetType::Token,
None,
Some(101 * UNITS),
));
});

let amount = 100 * UNITS;
Acala::execute_with(|| {
// Act
assert_ok!(hydradx_runtime::XTokens::transfer(
hydradx_runtime::RuntimeOrigin::signed(ALICE.into()),
0,
amount,
Box::new(
MultiLocation::new(
1,
X2(
Junction::Parachain(HYDRA_PARA_ID),
Junction::AccountId32 { id: BOB, network: None }
)
)
.into()
),
WeightLimit::Limited(Weight::from_parts(399_600_000_000, 0))
));

// Assert
assert_eq!(
hydradx_runtime::Balances::free_balance(AccountId::from(ALICE)),
ALICE_INITIAL_NATIVE_BALANCE - amount
);
});

Hydra::execute_with(|| {
let fee = hydradx_runtime::Tokens::free_balance(ACA, &hydradx_runtime::Treasury::account_id());
assert_eq!(
hydradx_runtime::Tokens::free_balance(ACA, &AccountId::from(BOB)),
amount - fee
);
});
}

#[test]
fn deferred_messages_should_be_executable_by_root() {
// Arrange
TestNet::reset();

Hydra::execute_with(|| {
assert_ok!(hydradx_runtime::AssetRegistry::set_location(
hydradx_runtime::RuntimeOrigin::root(),
ACA,
hydradx_runtime::AssetLocation(MultiLocation::new(1, X2(Parachain(ACALA_PARA_ID), GeneralIndex(0))))
));

// set an xcm rate limit
assert_ok!(hydradx_runtime::AssetRegistry::update(
hydradx_runtime::RuntimeOrigin::root(),
ACA,
b"ACA".to_vec(),
AssetType::Token,
None,
Some(50 * UNITS),
));

assert_eq!(hydradx_runtime::Tokens::free_balance(ACA, &AccountId::from(BOB)), 0);

//Set it to same as the relay block number should be in XcmDeferFilter
//since we use different RelayChainBlockNumberProvider in runtime-benchmark feature
//where we return frame_system current time
frame_system::Pallet::<hydradx_runtime::Runtime>::set_block_number(4);
});

let amount = 100 * UNITS;
let mut message_hash = None;
let max_weight = Weight::from_parts(399_600_000_000, 0);
Acala::execute_with(|| {
// Act
assert_ok!(hydradx_runtime::XTokens::transfer(
hydradx_runtime::RuntimeOrigin::signed(ALICE.into()),
0,
amount,
Box::new(
MultiLocation::new(
1,
X2(
Junction::Parachain(HYDRA_PARA_ID),
Junction::AccountId32 { id: BOB, network: None }
)
)
.into()
),
WeightLimit::Limited(max_weight),
));

message_hash = get_message_hash_from_event(2);

// Assert
assert_eq!(
hydradx_runtime::Balances::free_balance(AccountId::from(ALICE)),
ALICE_INITIAL_NATIVE_BALANCE - amount
);
});

let relay_block = PolkadotRelay::execute_with(polkadot_runtime::System::block_number);

Hydra::execute_with(|| {
expect_hydra_events(vec![
cumulus_pallet_xcmp_queue::Event::XcmDeferred {
sender: ACALA_PARA_ID.into(),
sent_at: relay_block,
deferred_to: hydradx_runtime::DeferDuration::get() + relay_block - 1,
message_hash,
index: (hydradx_runtime::DeferDuration::get() + relay_block - 1, 0),
position: 0,
}
.into(),
pallet_relaychain_info::Event::CurrentBlockNumbers {
parachain_block_number: 5,
relaychain_block_number: relay_block + 1,
}
.into(),
]);
assert_eq!(hydradx_runtime::Tokens::free_balance(ACA, &AccountId::from(BOB)), 0);

set_relaychain_block_number(hydradx_runtime::DeferDuration::get() + relay_block);

assert_eq!(hydradx_runtime::Tokens::free_balance(ACA, &AccountId::from(BOB)), 0);
assert_ok!(hydradx_runtime::XcmpQueue::service_deferred(
hydradx_runtime::RuntimeOrigin::root(),
hydradx_runtime::ReservedXcmpWeight::get(),
ACALA_PARA_ID.into(),
hydradx_runtime::MaxDeferredBuckets::get(),
));

let fee = hydradx_runtime::Tokens::free_balance(ACA, &hydradx_runtime::Treasury::account_id());
assert_eq!(
hydradx_runtime::Tokens::free_balance(ACA, &AccountId::from(BOB)),
amount - fee
);
});
}
2 changes: 1 addition & 1 deletion pallets/xcm-rate-limiter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pallet-xcm-rate-limiter"
version = "0.1.3"
version = "0.1.4"
authors = ["GalacticCouncil <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
Expand Down
20 changes: 10 additions & 10 deletions pallets/xcm-rate-limiter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,6 @@ pub mod pallet {
impl<T: Config> Pallet<T> {}
}

fn get_loc_and_amount(m: &MultiAsset) -> Option<(MultiLocation, u128)> {
match m.id {
AssetId::Concrete(location) => match m.fun {
Fungibility::Fungible(amount) => Some((location, amount)),
_ => None,
},
_ => None,
}
}

impl<T: Config> Pallet<T> {
fn get_locations_and_amounts(instruction: &Instruction<T::RuntimeCall>) -> Vec<(MultiLocation, u128)> {
use Instruction::*;
Expand All @@ -186,6 +176,16 @@ impl<T: Config> Pallet<T> {
}
}

fn get_loc_and_amount(m: &MultiAsset) -> Option<(MultiLocation, u128)> {
match m.id {
AssetId::Concrete(location) => match m.fun {
Fungibility::Fungible(amount) => Some((location, amount)),
_ => None,
},
_ => None,
}
}

impl<T: Config> XcmDeferFilter<T::RuntimeCall> for Pallet<T> {
fn deferred_by(
_para: polkadot_parachain::primitives::Id,
Expand Down
1 change: 1 addition & 0 deletions runtime/hydradx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ construct_runtime!(
LBP: pallet_lbp = 73,
XYK: pallet_xyk = 74,
Referrals: pallet_referrals = 75,
XcmRateLimiter: pallet_xcm_rate_limiter = 76,

// ORML related modules
Tokens: orml_tokens = 77,
Expand Down
Loading