Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contracts: Add XCM traits to interface with contracts #2086

Merged
merged 45 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
582467d
Contracts: Add XCM traits to interface with contracts
pgherveou Oct 30, 2023
79535cb
Update polkadot/xcm/xcm-executor/src/traits/controller.rs
pgherveou Oct 30, 2023
0a02451
Update doc
pgherveou Oct 30, 2023
18c6bb4
Use outcome in controller
pgherveou Oct 31, 2023
198d692
add docstring
pgherveou Oct 31, 2023
e3ce2e0
Event consistency
pgherveou Oct 31, 2023
6342962
Add more docs
pgherveou Oct 31, 2023
a2fde63
Update polkadot/xcm/xcm-executor/src/traits/controller.rs
pgherveou Oct 31, 2023
0ae8763
fix doc
pgherveou Oct 31, 2023
034150c
fix CI
pgherveou Oct 31, 2023
7848e2f
move to xcm-builder
pgherveou Nov 1, 2023
b7155b6
move to pallet-xcm
pgherveou Nov 1, 2023
34329e5
Update doc links
pgherveou Nov 1, 2023
07439dd
Merge branch 'master' into pg/xcm_contracts_update
pgherveou Nov 1, 2023
b37d169
wip
pgherveou Nov 1, 2023
8ac21da
Bench compile fix
pgherveou Nov 1, 2023
7ee5d08
add missing use
pgherveou Nov 2, 2023
f3cbdc4
fix test
pgherveou Nov 2, 2023
f3bd197
fix lint
pgherveou Nov 2, 2023
de8500f
Add prdoc
pgherveou Nov 2, 2023
9536755
Merge branch 'master' into pg/xcm_contracts_update
pgherveou Nov 2, 2023
abb196b
Move to xcm_builder
pgherveou Nov 6, 2023
df789aa
missing trait doc
pgherveou Nov 6, 2023
2c01af3
Merge branch 'master' into pg/xcm_contracts_update
pgherveou Nov 6, 2023
2fd1928
Merge branch 'master' into pg/xcm_contracts_update
pgherveou Nov 6, 2023
fd958af
Update polkadot/xcm/xcm-builder/src/controller.rs
pgherveou Nov 7, 2023
2517c73
Merge branch 'master' into pg/xcm_contracts_update
pgherveou Nov 7, 2023
65d3bf9
fix build
pgherveou Nov 7, 2023
9a29e87
zepter fix
pgherveou Nov 7, 2023
94055be
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Nov 7, 2023
18aacb3
".git/.scripts/commands/bench/bench.sh" --subcommand=runtime --runtim…
Nov 7, 2023
af5dd47
remove temp weightinfo impl
pgherveou Nov 7, 2023
56a5449
Merge branch 'master' into pg/xcm_contracts_update
pgherveou Nov 7, 2023
49e45d8
Revert "remove temp weightinfo impl"
pgherveou Nov 7, 2023
7ee9145
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Nov 7, 2023
02a3f4c
Add benchmark
pgherveou Nov 8, 2023
9f48c00
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Nov 9, 2023
de42120
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Nov 9, 2023
eb4a904
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Nov 10, 2023
fc09597
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Nov 10, 2023
443aff6
update weights
pgherveou Nov 10, 2023
0c308db
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Nov 10, 2023
2029223
".git/.scripts/commands/bench/bench.sh" --subcommand=pallet --runtime…
Nov 10, 2023
639c64f
westend weights
pgherveou Nov 10, 2023
105d9f5
remove default weights
pgherveou Nov 10, 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
27 changes: 27 additions & 0 deletions polkadot/xcm/pallet-xcm/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,33 @@ benchmarks! {
Pallet::<T>::check_xcm_version_change(VersionMigrationStage::MigrateAndNotifyOldTargets, Weight::zero());
}

new_query {
let responder = MultiLocation::from(Parent);
let timeout = 1u32.into();
let match_querier = MultiLocation::from(Here);
}: {
Pallet::<T>::new_query(responder, timeout, match_querier);
}

take_response {
let responder = MultiLocation::from(Parent);
let timeout = 1u32.into();
let match_querier = MultiLocation::from(Here);
let query_id = Pallet::<T>::new_query(responder, timeout, match_querier);
let infos = (0 .. xcm::v3::MaxPalletsInfo::get()).map(|_| PalletInfo::new(
u32::MAX,
(0..xcm::v3::MaxPalletNameLen::get()).map(|_| 97u8).collect::<Vec<_>>().try_into().unwrap(),
(0..xcm::v3::MaxPalletNameLen::get()).map(|_| 97u8).collect::<Vec<_>>().try_into().unwrap(),
u32::MAX,
u32::MAX,
u32::MAX,
).unwrap()).collect::<Vec<_>>();
Pallet::<T>::expect_response(query_id, Response::PalletsInfo(infos.try_into().unwrap()));

}: {
Pallet::<T>::take_response(query_id);
}

impl_benchmark_test_suite!(
Pallet,
crate::mock::new_test_ext_with_balances(Vec::new()),
Expand Down
153 changes: 118 additions & 35 deletions polkadot/xcm/pallet-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ mod tests;
pub mod migration;

use codec::{Decode, Encode, EncodeLike, MaxEncodedLen};
use frame_support::traits::{
Contains, ContainsPair, Currency, Defensive, EnsureOrigin, Get, LockableCurrency, OriginTrait,
use frame_support::{
dispatch::GetDispatchInfo,
pallet_prelude::*,
traits::{
Contains, ContainsPair, Currency, Defensive, EnsureOrigin, Get, LockableCurrency,
OriginTrait, WithdrawReasons,
},
PalletId,
};
use frame_system::pallet_prelude::{BlockNumberFor, *};
pub use pallet::*;
use scale_info::TypeInfo;
use sp_runtime::{
traits::{
Expand All @@ -41,17 +49,13 @@ use sp_runtime::{
};
use sp_std::{boxed::Box, marker::PhantomData, prelude::*, result::Result, vec};
use xcm::{latest::QueryResponseInfo, prelude::*};
use xcm_executor::traits::{ConvertOrigin, Properties};

use frame_support::{
dispatch::GetDispatchInfo, pallet_prelude::*, traits::WithdrawReasons, PalletId,
};
use frame_system::pallet_prelude::*;
pub use pallet::*;
use xcm_executor::{
traits::{
CheckSuspension, ClaimAssets, ConvertLocation, DropAssets, MatchesFungible, OnResponse,
QueryHandler, QueryResponseStatus, VersionChangeNotifier, WeightBounds,
CheckSuspension, ClaimAssets, ConvertLocation, ConvertOrigin, DropAssets,
ExecuteController, ExecuteControllerWeightInfo, MatchesFungible, OnResponse, Properties,
QueryController, QueryControllerWeightInfo, QueryHandler,
QueryResponseStatus, SendController, SendControllerWeightInfo, VersionChangeNotifier,
WeightBounds,
},
Assets,
};
Expand All @@ -73,6 +77,13 @@ pub trait WeightInfo {
fn notify_target_migration_fail() -> Weight;
fn migrate_version_notify_targets() -> Weight;
fn migrate_and_notify_old_targets() -> Weight;
// TODO remove after benchmark are regenerated
fn new_query() -> Weight {
Weight::zero()
}
fn take_response() -> Weight {
Weight::zero()
}
Copy link
Member

Choose a reason for hiding this comment

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

Still need to run benchmark in this PR before merging.

}

/// fallback implementation
Expand Down Expand Up @@ -141,6 +152,14 @@ impl WeightInfo for TestWeightInfo {
fn migrate_and_notify_old_targets() -> Weight {
Weight::from_parts(100_000_000, 0)
}

fn new_query() -> Weight {
Weight::from_parts(100_000_000, 0)
}

fn take_response() -> Weight {
Weight::from_parts(100_000_000, 0)
}
}

#[frame_support::pallet]
Expand Down Expand Up @@ -267,6 +286,91 @@ pub mod pallet {
type ReachableDest: Get<Option<MultiLocation>>;
}

impl<T: Config> ExecuteControllerWeightInfo for Pallet<T> {
fn execute() -> Weight {
T::WeightInfo::execute()
}
}

impl<T: Config> ExecuteController<OriginFor<T>, <T as Config>::RuntimeCall> for Pallet<T> {
type WeightInfo = Self;
fn execute(
origin: OriginFor<T>,
message: Box<VersionedXcm<<T as Config>::RuntimeCall>>,
max_weight: Weight,
) -> Result<Outcome, DispatchError> {
let origin_location = T::ExecuteXcmOrigin::ensure_origin(origin)?;
let hash = message.using_encoded(sp_io::hashing::blake2_256);
let message = (*message).try_into().map_err(|()| Error::<T>::BadVersion)?;
let value = (origin_location, message);
ensure!(T::XcmExecuteFilter::contains(&value), Error::<T>::Filtered);
let (origin_location, message) = value;
Ok(T::XcmExecutor::execute_xcm_in_credit(
origin_location,
message,
hash,
max_weight,
max_weight,
))
}
}

impl<T: Config> SendControllerWeightInfo for Pallet<T> {
fn send() -> Weight {
T::WeightInfo::send()
}
}

impl<T: Config> SendController<OriginFor<T>> for Pallet<T> {
type WeightInfo = Self;
fn send(
origin: OriginFor<T>,
dest: Box<VersionedMultiLocation>,
message: Box<VersionedXcm<()>>,
) -> Result<XcmHash, DispatchError> {
let origin_location = T::SendXcmOrigin::ensure_origin(origin)?;
let interior: Junctions =
origin_location.try_into().map_err(|_| Error::<T>::InvalidOrigin)?;
let dest = MultiLocation::try_from(*dest).map_err(|()| Error::<T>::BadVersion)?;
let message: Xcm<()> = (*message).try_into().map_err(|()| Error::<T>::BadVersion)?;

let message_id =
Self::send_xcm(interior, dest, message.clone()).map_err(Error::<T>::from)?;
let e = Event::Sent { origin: origin_location, destination: dest, message, message_id };
Self::deposit_event(e);
Ok(message_id)
}
}

impl<T: Config> QueryControllerWeightInfo for Pallet<T> {
fn query() -> Weight {
T::WeightInfo::new_query()
}
fn take_response() -> Weight {
T::WeightInfo::take_response()
}
}

impl<T: Config> QueryController<OriginFor<T>, BlockNumberFor<T>> for Pallet<T> {
type WeightInfo = Self;

fn query(
origin: OriginFor<T>,
timeout: BlockNumberFor<T>,
match_querier: VersionedMultiLocation,
) -> Result<Self::QueryId, DispatchError> {
let responder = <T as Config>::ExecuteXcmOrigin::ensure_origin(origin)?;
let query_id = <Self as QueryHandler>::new_query(
responder,
timeout.into(),
MultiLocation::try_from(match_querier)
.map_err(|_| Into::<DispatchError>::into(Error::<T>::BadVersion))?,
);

Ok(query_id)
}
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down Expand Up @@ -771,16 +875,7 @@ pub mod pallet {
dest: Box<VersionedMultiLocation>,
message: Box<VersionedXcm<()>>,
) -> DispatchResult {
let origin_location = T::SendXcmOrigin::ensure_origin(origin)?;
let interior: Junctions =
origin_location.try_into().map_err(|_| Error::<T>::InvalidOrigin)?;
let dest = MultiLocation::try_from(*dest).map_err(|()| Error::<T>::BadVersion)?;
let message: Xcm<()> = (*message).try_into().map_err(|()| Error::<T>::BadVersion)?;

let message_id =
Self::send_xcm(interior, dest, message.clone()).map_err(Error::<T>::from)?;
let e = Event::Sent { origin: origin_location, destination: dest, message, message_id };
Self::deposit_event(e);
<Self as SendController<_>>::send(origin, dest, message)?;
Ok(())
}

Expand Down Expand Up @@ -896,27 +991,15 @@ pub mod pallet {
/// execution attempt will be made.
///
/// NOTE: A successful return to this does *not* imply that the `msg` was executed
/// successfully to completion; only that *some* of it was executed.
/// successfully to completion; only that it was attempted.
#[pallet::call_index(3)]
#[pallet::weight(max_weight.saturating_add(T::WeightInfo::execute()))]
pub fn execute(
origin: OriginFor<T>,
message: Box<VersionedXcm<<T as Config>::RuntimeCall>>,
max_weight: Weight,
) -> DispatchResultWithPostInfo {
let origin_location = T::ExecuteXcmOrigin::ensure_origin(origin)?;
let hash = message.using_encoded(sp_io::hashing::blake2_256);
let message = (*message).try_into().map_err(|()| Error::<T>::BadVersion)?;
let value = (origin_location, message);
ensure!(T::XcmExecuteFilter::contains(&value), Error::<T>::Filtered);
let (origin_location, message) = value;
let outcome = T::XcmExecutor::execute_xcm_in_credit(
origin_location,
message,
hash,
max_weight,
max_weight,
);
let outcome = <Self as ExecuteController<_, _>>::execute(origin, message, max_weight)?;
let result =
Ok(Some(outcome.weight_used().saturating_add(T::WeightInfo::execute())).into());
Self::deposit_event(Event::Attempted { outcome });
Expand Down
Loading
Loading