Skip to content

Commit

Permalink
Merge branch 'manta' into farming_rpc_test
Browse files Browse the repository at this point in the history
  • Loading branch information
zqhxuyuan authored Jul 20, 2023
2 parents 9ed3742 + e82ca14 commit 1e83a12
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 38 deletions.
78 changes: 75 additions & 3 deletions primitives/manta/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,69 @@ where
}
}

use xcm::latest::{Instruction::*, Weight};

/// Allows execution from `origin` if it is contained in `T` (i.e. `T::Contains(origin)`) taking
/// payments into account.
///
/// Only allows for `TeleportAsset`, `WithdrawAsset`, `ClaimAsset` and `ReserveAssetDeposit` XCMs
/// because they are the only ones that place assets in the Holding Register to pay for execution.
pub struct AllowTopLevelPaidExecutionFrom<T>(PhantomData<T>);
impl<T: Contains<MultiLocation>> ShouldExecute for AllowTopLevelPaidExecutionFrom<T> {
fn should_execute<RuntimeCall>(
origin: &MultiLocation,
message: &mut Xcm<RuntimeCall>,
max_weight: Weight,
_weight_credit: &mut Weight,
) -> Result<(), ()> {
log::trace!(
target: "xcm::barriers",
"AllowTopLevelPaidExecutionFrom origin: {:?}, message: {:?}, max_weight: {:?}, weight_credit: {:?}",
origin, message, max_weight, _weight_credit,
);
ensure!(T::contains(origin), ());
let mut iter = message.0.iter_mut();
let i = iter.next().ok_or(())?;
match i {
ReceiveTeleportedAsset(..)
| WithdrawAsset(..)
| ReserveAssetDeposited(..)
| ClaimAsset { .. } => (),
_ => return Err(()),
}
let mut i = iter.next().ok_or(())?;
while let ClearOrigin = i {
i = iter.next().ok_or(())?;
}
match i {
BuyExecution {
weight_limit: Limited(ref mut weight),
..
} if *weight >= max_weight => {
*weight = max_weight;
}
BuyExecution {
ref mut weight_limit,
..
} if weight_limit == &Unlimited => {
*weight_limit = Limited(max_weight);
}
_ => {
return Err(());
}
}

for next in iter {
if let TransferReserveAsset { .. } = next {
// We've currently blocked transfers of MANTA on the instruction level
return Err(());
}
}

Ok(())
}
}

/// Barrier allowing a top level paid message with DescendOrigin instruction first
pub struct AllowTopLevelPaidExecutionDescendOriginFirst<T>(PhantomData<T>);
impl<T: Contains<MultiLocation>> ShouldExecute for AllowTopLevelPaidExecutionDescendOriginFirst<T> {
Expand Down Expand Up @@ -452,16 +515,25 @@ impl<T: Contains<MultiLocation>> ShouldExecute for AllowTopLevelPaidExecutionDes
..
} if *weight >= max_weight => {
*weight = max_weight;
Ok(())
}
BuyExecution {
ref mut weight_limit,
..
} if weight_limit == &Unlimited => {
*weight_limit = Limited(max_weight);
Ok(())
}
_ => Err(()),
_ => {
return Err(());
}
}

for next in iter {
if let TransferReserveAsset { .. } = next {
// We've currently blocked transfers of MANTA on the instruction level
return Err(());
}
}

Ok(())
}
}
19 changes: 10 additions & 9 deletions runtime/calamari/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ use manta_primitives::{
assets::AssetIdLocationConvert,
types::{AccountId, Balance, CalamariAssetId},
xcm::{
AccountIdToMultiLocation, AllowTopLevelPaidExecutionDescendOriginFirst, FirstAssetTrader,
IsNativeConcrete, MultiAssetAdapter, MultiNativeAsset,
AccountIdToMultiLocation, AllowTopLevelPaidExecutionDescendOriginFirst,
AllowTopLevelPaidExecutionFrom, FirstAssetTrader, IsNativeConcrete, MultiAssetAdapter,
MultiNativeAsset, XcmFeesToAccount,
},
};
use orml_traits::location::AbsoluteReserveProvider;
Expand All @@ -45,11 +46,11 @@ use sp_std::prelude::*;
use xcm::latest::prelude::*;
use xcm_builder::{
Account32Hash, AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, ConvertedConcreteAssetId,
EnsureXcmOrigin, FixedRateOfFungible, LocationInverter, ParentAsSuperuser, ParentIsPreset,
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeRevenue,
TakeWeightCredit, WeightInfoBounds,
AllowUnpaidExecutionFrom, ConvertedConcreteAssetId, EnsureXcmOrigin, FixedRateOfFungible,
LocationInverter, ParentAsSuperuser, ParentIsPreset, RelayChainAsNative,
SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeRevenue, TakeWeightCredit,
WeightInfoBounds,
};
use xcm_executor::{traits::JustTry, Config, XcmExecutor};

Expand Down Expand Up @@ -205,7 +206,7 @@ impl TakeRevenue for XcmNativeFeeToTreasury {
}
}

pub type XcmFeesToAccount = manta_primitives::xcm::XcmFeesToAccount<
pub type CalamariXcmFeesToAccount = XcmFeesToAccount<
AccountId,
Assets,
ConvertedConcreteAssetId<
Expand Down Expand Up @@ -242,7 +243,7 @@ impl Config for XcmExecutorConfig {
// i.e. units_per_second in `AssetManager`
type Trader = (
FixedRateOfFungible<ParaTokenPerSecond, XcmNativeFeeToTreasury>,
FirstAssetTrader<AssetManager, XcmFeesToAccount>,
FirstAssetTrader<AssetManager, CalamariXcmFeesToAccount>,
);
type ResponseHandler = PolkadotXcm;
type AssetTrap = PolkadotXcm;
Expand Down
20 changes: 10 additions & 10 deletions runtime/integration-tests/src/xcm_mock/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ use manta_primitives::{
constants::{ASSET_MANAGER_PALLET_ID, CALAMARI_DECIMAL, WEIGHT_PER_SECOND},
types::{BlockNumber, CalamariAssetId, Header},
xcm::{
AllowTopLevelPaidExecutionDescendOriginFirst, FirstAssetTrader, IsNativeConcrete,
MultiAssetAdapter, MultiNativeAsset,
AccountIdToMultiLocation, AllowTopLevelPaidExecutionDescendOriginFirst,
AllowTopLevelPaidExecutionFrom, FirstAssetTrader, IsNativeConcrete, MultiAssetAdapter,
MultiNativeAsset, XcmFeesToAccount,
},
};
use pallet_xcm::XcmPassthrough;
Expand All @@ -57,11 +58,10 @@ use polkadot_parachain::primitives::{
use xcm::{latest::prelude::*, Version as XcmVersion, VersionedMultiLocation, VersionedXcm};
use xcm_builder::{
Account32Hash, AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, ConvertedConcreteAssetId,
EnsureXcmOrigin, FixedRateOfFungible, LocationInverter, ParentIsPreset,
SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
SignedToAccountId32, SovereignSignedViaLocation, TakeRevenue, TakeWeightCredit,
WeightInfoBounds,
AllowUnpaidExecutionFrom, ConvertedConcreteAssetId, EnsureXcmOrigin, FixedRateOfFungible,
LocationInverter, ParentIsPreset, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeRevenue,
TakeWeightCredit, WeightInfoBounds,
};
use xcm_executor::{traits::JustTry, Config, XcmExecutor};
use xcm_simulator::{DmpMessageHandlerT, Get, TestExt, XcmpMessageHandlerT};
Expand Down Expand Up @@ -288,7 +288,7 @@ impl TakeRevenue for XcmNativeFeeToTreasury {
}

/// Xcm fee of non native token
pub type XcmFeesToAccount = manta_primitives::xcm::XcmFeesToAccount<
pub type CalamariXcmFeesToAccount = XcmFeesToAccount<
AccountId,
Assets,
ConvertedConcreteAssetId<
Expand Down Expand Up @@ -321,7 +321,7 @@ impl Config for XcmExecutorConfig {
// i.e. units_per_second in `AssetManager`
type Trader = (
FixedRateOfFungible<ParaTokenPerSecond, XcmNativeFeeToTreasury>,
FirstAssetTrader<AssetManager, XcmFeesToAccount>,
FirstAssetTrader<AssetManager, CalamariXcmFeesToAccount>,
);
type ResponseHandler = PolkadotXcm;
type AssetTrap = PolkadotXcm;
Expand Down Expand Up @@ -687,7 +687,7 @@ impl orml_xtokens::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Balance = Balance;
type CurrencyId = CurrencyId;
type AccountIdToMultiLocation = manta_primitives::xcm::AccountIdToMultiLocation;
type AccountIdToMultiLocation = AccountIdToMultiLocation;
type CurrencyIdConvert = CurrencyIdtoMultiLocation<AssetIdLocationConvert<AssetManager>>;
type XcmExecutor = XcmExecutor<XcmExecutorConfig>;
type SelfLocation = SelfReserve;
Expand Down
10 changes: 6 additions & 4 deletions runtime/integration-tests/src/xcm_mock/relay_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ use sp_runtime::{
AccountId32,
};

use manta_primitives::types::{BlockNumber, Header};
use manta_primitives::{
types::{BlockNumber, Header},
xcm::AllowTopLevelPaidExecutionFrom,
};
use polkadot_parachain::primitives::Id as ParaId;
use polkadot_runtime_parachains::{configuration, origin, shared, ump};
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, ChildParachainAsNative,
ChildParachainConvertsVia, ChildSystemParachainAsSuperuser,
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowUnpaidExecutionFrom,
ChildParachainAsNative, ChildParachainConvertsVia, ChildSystemParachainAsSuperuser,
CurrencyAdapter as XcmCurrencyAdapter, FixedRateOfFungible, FixedWeightBounds, IsConcrete,
LocationInverter, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,
TakeWeightCredit,
Expand Down
47 changes: 47 additions & 0 deletions runtime/integration-tests/src/xcm_mock/xcm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3605,3 +3605,50 @@ fn send_disabled_asset_should_fail() {
),)
});
}

#[test]
fn transfer_reserve_asset_instruction_from_parachains_is_blocked() {
MockNet::reset();

ParaA::execute_with(|| {
let dummy_asset = MultiAsset {
id: Concrete(MultiLocation {
parents: 1,
interior: X1(Parachain(PARA_B_ID)),
}),
fun: Fungible(1000000000),
};
let dummy_assets = MultiAssets::from(vec![dummy_asset.clone()]);
// Send withdraw and deposit
assert_ok!(ParachainPalletXcm::send_xcm(
Here,
(Parent, Parachain(PARA_B_ID)),
Xcm(vec![
WithdrawAsset(dummy_assets),
BuyExecution {
fees: dummy_asset.clone(),
weight_limit: Unlimited,
},
TransferReserveAsset {
assets: dummy_asset.into(),
dest: Here.into(),
xcm: Xcm(vec![]),
}
]),
));
});

ParaB::execute_with(|| {
use parachain::{RuntimeEvent, System};

// AllowTopLevelPaidExecutionFrom<Everything> should fail because TransferReserverAsset instruction is not allowed
assert!(System::events().iter().any(|r| matches!(
r.event,
RuntimeEvent::XcmpQueue(cumulus_pallet_xcmp_queue::Event::Fail {
message_hash: _,
error: XcmError::Barrier,
weight: _
})
)));
});
}
22 changes: 10 additions & 12 deletions runtime/manta/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ use manta_primitives::{
assets::AssetIdLocationConvert,
types::{AccountId, MantaAssetId},
xcm::{
AccountIdToMultiLocation, FirstAssetTrader, IsNativeConcrete, MultiAssetAdapter,
MultiNativeAsset,
AccountIdToMultiLocation, AllowTopLevelPaidExecutionDescendOriginFirst,
AllowTopLevelPaidExecutionFrom, FirstAssetTrader, IsNativeConcrete, MultiAssetAdapter,
MultiNativeAsset, XcmFeesToAccount,
},
};
use orml_traits::location::AbsoluteReserveProvider;
Expand All @@ -44,17 +45,14 @@ use sp_runtime::traits::Convert;
use sp_std::marker::PhantomData;
use xcm::latest::prelude::*;
use xcm_builder::{
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom,
AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, ConvertedConcreteAssetId,
EnsureXcmOrigin, FixedRateOfFungible, LocationInverter, ParentAsSuperuser, ParentIsPreset,
RelayChainAsNative, SiblingParachainAsNative, SiblingParachainConvertsVia,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeRevenue,
TakeWeightCredit, WeightInfoBounds,
AccountId32Aliases, AllowKnownQueryResponses, AllowSubscriptionsFrom, AllowUnpaidExecutionFrom,
ConvertedConcreteAssetId, EnsureXcmOrigin, FixedRateOfFungible, LocationInverter,
ParentAsSuperuser, ParentIsPreset, RelayChainAsNative, SiblingParachainAsNative,
SiblingParachainConvertsVia, SignedAccountId32AsNative, SignedToAccountId32,
SovereignSignedViaLocation, TakeRevenue, TakeWeightCredit, WeightInfoBounds,
};
use xcm_executor::{traits::JustTry, Config, XcmExecutor};

use manta_primitives::xcm::AllowTopLevelPaidExecutionDescendOriginFirst;

parameter_types! {
pub const ReservedDmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
pub const ReservedXcmpWeight: Weight = MAXIMUM_BLOCK_WEIGHT.saturating_div(4);
Expand Down Expand Up @@ -193,7 +191,7 @@ impl TakeRevenue for XcmNativeFeeToTreasury {
}
}
}
pub type XcmFeesToAccount = manta_primitives::xcm::XcmFeesToAccount<
pub type MantaXcmFeesToAccount = XcmFeesToAccount<
AccountId,
Assets,
ConvertedConcreteAssetId<MantaAssetId, Balance, AssetIdLocationConvert<AssetManager>, JustTry>,
Expand Down Expand Up @@ -225,7 +223,7 @@ impl Config for XcmExecutorConfig {
// i.e. units_per_second in `AssetManager`
type Trader = (
FixedRateOfFungible<ParaTokenPerSecond, XcmNativeFeeToTreasury>,
FirstAssetTrader<AssetManager, XcmFeesToAccount>,
FirstAssetTrader<AssetManager, MantaXcmFeesToAccount>,
);
type ResponseHandler = PolkadotXcm;
type AssetTrap = PolkadotXcm;
Expand Down

0 comments on commit 1e83a12

Please sign in to comment.