Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
126 commits
Select commit Hold shift + click to select a range
251e8cd
boilerplate type changes
pgherveou Aug 29, 2023
3b1cc37
Add pallet_xcm::execute
pgherveou Aug 29, 2023
22430a3
wip
pgherveou Aug 30, 2023
c6e1f49
add other xcm_methods
pgherveou Aug 30, 2023
bf66839
add benchmarks
pgherveou Aug 31, 2023
a348bd9
build fixes
pgherveou Aug 31, 2023
b2a9f4e
fix
pgherveou Aug 31, 2023
d20d6ca
fix post-rebase missing imports
pgherveou Sep 1, 2023
8388585
Revert "boilerplate type changes"
pgherveou Sep 1, 2023
4bae246
Use trait instead of tight coupling
pgherveou Sep 1, 2023
0675899
add mock_network setup
pgherveou Sep 1, 2023
2049b57
fixes
pgherveou Sep 4, 2023
be04741
fix clippy
pgherveou Sep 4, 2023
b7875ad
quick fixes
pgherveou Sep 4, 2023
c312b98
simplify
pgherveou Sep 4, 2023
5983dbd
move stuff to pallet_xcm_extension
pgherveou Sep 4, 2023
7484d32
fmt
pgherveou Sep 4, 2023
b40f92c
headr
pgherveou Sep 4, 2023
c3f7d40
missing headers
pgherveou Sep 4, 2023
a08e41b
fix missing comment
pgherveou Sep 4, 2023
376a6b3
fixes
pgherveou Sep 4, 2023
5b883d6
Merge branch 'master' into pg/xcm
pgherveou Sep 4, 2023
967616c
Merge branch 'master' into pg/xcm
pgherveou Sep 4, 2023
d2b5ba3
refactoring
pgherveou Sep 4, 2023
d7fe1bc
fix build
pgherveou Sep 4, 2023
c15adab
missing type in node/runtime
pgherveou Sep 5, 2023
e15c7eb
format cargo.toml
pgherveou Sep 5, 2023
971b167
more zepter fixes
pgherveou Sep 5, 2023
3dba8d1
fixes
pgherveou Sep 5, 2023
be1a46d
missing trait implementation
pgherveou Sep 5, 2023
a4d706c
fix tests
pgherveou Sep 5, 2023
b15ea88
fixes
pgherveou Sep 5, 2023
b938df4
fmt fix
pgherveou Sep 5, 2023
fcc37f5
zepter format
pgherveou Sep 5, 2023
9cc990f
fix doc links
pgherveou Sep 5, 2023
7ff220b
rm println!
pgherveou Sep 5, 2023
bdf0ae7
add tests comments
pgherveou Sep 5, 2023
14e1514
worst case
pgherveou Sep 5, 2023
e9d2cdf
simplify XCM
pgherveou Sep 14, 2023
c9f8f35
add docs
pgherveou Sep 14, 2023
151175b
Filter out transact calls
pgherveou Sep 21, 2023
43f38d2
fmt
pgherveou Sep 21, 2023
4583e03
move ensure to it's own function for benchmark
pgherveou Sep 21, 2023
ceb3fc8
fix error handling
pgherveou Sep 21, 2023
db7c479
doc fixes
pgherveou Sep 22, 2023
cd8281e
fix weight
pgherveou Sep 22, 2023
c004909
improve weight
pgherveou Sep 22, 2023
526abbd
XCMAdapter -> XcmAdapter
pgherveou Sep 25, 2023
0a98a89
XCM<T> -> Xcm<T>
pgherveou Sep 25, 2023
1d1f40a
Update substrate/frame/contracts/src/xcm.rs
pgherveou Sep 25, 2023
e01cb2c
Update substrate/frame/contracts/src/xcm.rs
pgherveou Sep 25, 2023
2c7735b
Update substrate/frame/contracts/src/xcm.rs
pgherveou Sep 25, 2023
4e3de86
Update substrate/frame/contracts/src/xcm.rs
pgherveou Sep 25, 2023
e52f85f
s/xcm message/message
pgherveou Sep 25, 2023
d78ec47
typo
pgherveou Sep 25, 2023
435de45
fix build issues
pgherveou Sep 27, 2023
c4d87ff
fix fmt
pgherveou Sep 27, 2023
4363e6d
More doc fix changes
pgherveou Sep 27, 2023
ac39a50
Apply suggestions from code review
pgherveou Sep 29, 2023
0bf6e1a
address PR comments
pgherveou Sep 29, 2023
ddf485a
Add reentrency test
pgherveou Oct 9, 2023
975ef56
Simplify tests
pgherveou Oct 9, 2023
448c9d4
Apply suggestions from code review
pgherveou Oct 24, 2023
132e203
PR review comment
pgherveou Oct 24, 2023
7bf716d
Fix lint
pgherveou Oct 24, 2023
6a7a8bd
return proper error code
pgherveou Oct 24, 2023
8c1c118
Merge branch 'master' into pg/xcm
pgherveou Oct 24, 2023
06a5c9a
fix compilation issues after rebase
pgherveou Oct 24, 2023
b47f660
lint
pgherveou Oct 24, 2023
06cf121
Merge branch 'master' of https://github.com/paritytech/polkadot-sdk i…
Oct 24, 2023
0949455
".git/.scripts/commands/bench/bench.sh" --subcommand=runtime --runtim…
Oct 24, 2023
14fed0e
Update substrate/frame/contracts/src/tests/test_xcm.rs
pgherveou Oct 25, 2023
e694941
simplify mock_network
pgherveou Oct 25, 2023
72f15c1
fix return type in comment
pgherveou Oct 26, 2023
c8f39e7
Contracts expose pallet-xcm - tweaks (#2062)
pgherveou Oct 27, 2023
54e2803
Add copyright
pgherveou Oct 27, 2023
ce637dc
reuse QueryHandler
pgherveou Oct 29, 2023
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
262e17c
Merge branch 'pg/xcm_contracts_update' into pg/xcm
pgherveou Oct 31, 2023
9413874
use Outcome return value for xcm_execute
pgherveou Oct 31, 2023
18c6bb4
Use outcome in controller
pgherveou Oct 31, 2023
b9b2ccc
Merge branch 'pg/xcm_contracts_update' into pg/xcm
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
de8f1a9
Merge branch 'pg/xcm_contracts_update' into pg/xcm
pgherveou Nov 1, 2023
7cd84a2
update xcm stuff
pgherveou Nov 1, 2023
8ac21da
Bench compile fix
pgherveou Nov 1, 2023
7ee5d08
add missing use
pgherveou Nov 2, 2023
e13b51b
Merge branch 'pg/xcm_contracts_update' into pg/xcm
pgherveou Nov 2, 2023
2359fa0
fix test
pgherveou Nov 2, 2023
41215d3
mock-network crate
pgherveou Nov 10, 2023
e515180
Merge branch 'master' into pg/xcm
pgherveou Nov 10, 2023
8b0f8ac
revert bad merge
pgherveou Nov 10, 2023
3526658
move fixtures files
pgherveou Nov 10, 2023
784f8df
rm pallet-xcm dep
pgherveou Nov 10, 2023
0b5517a
fix xcm deps
pgherveou Nov 10, 2023
7b3ad3d
fix tests
pgherveou Nov 10, 2023
0b4c840
fix lint
pgherveou Nov 10, 2023
80e0c53
fixes
pgherveou Nov 10, 2023
59d67a0
fix
pgherveou Nov 10, 2023
9a9d572
fix
pgherveou Nov 10, 2023
3adde14
fixed deps moved to pallet-contracts-mock-network
pgherveou Nov 14, 2023
c70aca5
Review https://github.com/paritytech/polkadot-sdk/pull/1248#discussio…
pgherveou Nov 14, 2023
5d63636
Review https://github.com/paritytech/polkadot-sdk/pull/1248#discussio…
pgherveou Nov 14, 2023
57b7ca5
PR review https://github.com/paritytech/polkadot-sdk/pull/1248#discus…
pgherveou Nov 14, 2023
10f22fb
PR review https://github.com/paritytech/polkadot-sdk/pull/1248#discus…
pgherveou Nov 14, 2023
162ad54
PR review https://github.com/paritytech/polkadot-sdk/pull/1248#discus…
pgherveou Nov 14, 2023
3a4a251
rm macro_export
pgherveou Nov 14, 2023
dab8314
Temporary remove xcm_query and xcm_take_response fns
pgherveou Nov 14, 2023
e3161c9
misc removed stuff from query
pgherveou Nov 14, 2023
4128aa6
fix doc
pgherveou Nov 14, 2023
f0df85b
rm more stuff from query & take_response
pgherveou Nov 14, 2023
b48a8f9
fmt
pgherveou Nov 14, 2023
7c72e24
Merge branch 'master' into pg/xcm
pgherveou Nov 14, 2023
b34e245
doc test
pgherveou Nov 14, 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
Prev Previous commit
Next Next commit
fix error handling
  • Loading branch information
pgherveou committed Sep 21, 2023
commit ceb3fc8ec8939843d957f970d7925e2ea1ba9fd4
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use frame_support::{
use pallet_contracts::{
migration::{v12, v13, v14, v15},
weights::SubstrateWeight,
xcm::PalletXCMAdapter,
xcm::XCMAdapter,
Config, DebugInfo, DefaultAddressGenerator, Frame, Schedule,
};
use sp_runtime::Perbill;
Expand Down Expand Up @@ -80,5 +80,5 @@ impl Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type Debug = ();
type Environment = ();
type Xcm = PalletXCMAdapter<Self>;
type Xcm = XCMAdapter<Self>;
}
2 changes: 1 addition & 1 deletion substrate/frame/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ pub mod pallet {
/// A type that exposes XCM APIs, allowing contracts to interact with other parachains, and
/// execute XCM messages.
///
/// Use [`xcm::PalletXCMAdapter`] to enable or `()` to disable.
/// Use [`xcm::XCMAdapter`] to enable or `()` to disable.
type Xcm: XCM<Self>;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
parachain::RuntimeHoldReason,
primitives::{Balance, CENTS},
},
xcm::PalletXCMAdapter,
xcm::XCMAdapter,
Config,
};
use frame_support::{
Expand Down Expand Up @@ -85,5 +85,5 @@ impl Config for Runtime {
type WeightPrice = Self;
type Debug = ();
type Environment = ();
type Xcm = PalletXCMAdapter<Self>;
type Xcm = XCMAdapter<Self>;
}
15 changes: 11 additions & 4 deletions substrate/frame/contracts/src/tests/test_xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::{
};
use codec::{Decode, Encode};
use frame_support::{
assert_err,
pallet_prelude::Weight,
traits::{fungibles::Mutate, Currency},
};
Expand Down Expand Up @@ -153,11 +154,17 @@ fn test_xcm_execute_filtered_call() {
DebugInfo::UnsafeDebug,
CollectEvents::UnsafeCollect,
Determinism::Enforced,
)
.result
.unwrap();
);

// The debug message should say that the call was filtered.
assert_eq!(
std::str::from_utf8(&result.debug_message).unwrap(),
"call failed with: CallFiltered"
);

assert_return_code!(result, RuntimeReturnCode::CallRuntimeFailed);
// The call should fail, with an `OutOfGas` error, as the failed xcm::execute will not
// refund the max_weight gas passed to it.
assert_err!(result.result, crate::Error::<parachain::Runtime>::OutOfGas);
});
}

Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/contracts/src/xcm.rs
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, what's the purpose of the Xcm trait in this file? I'm not sure I'm following just by reading the code here...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rather than tight coupling pallet_contracts::Config with pallet_xcm::Config, we are exposing a subset of the function we want to expose to contracts through this trait.

This is exposed in the config here:

/// A type that exposes XCM APIs, allowing contracts to interact with other parachains, and
/// execute XCM programs.
///
/// Use [`xcm::XCMAdapter`] to enable or `()` to disable.
type Xcm: Xcm<Self>;

XcmAdapter<Self> implement this trait and forward the calls to pallet-xcm.

This is exposed to SC through the new host fn (xcm_*) methods:

/// Execute an XCM program locally, using the contract's address as the origin.
/// This is equivalent to dispatching `pallet_xcm::execute` through call_runtime, except that
/// the function is called directly instead of being dispatched.
///
/// # Parameters
///
/// - `msg_ptr`: the pointer into the linear memory where the message is placed.
/// - `msg_len`: the length of the message in bytes.
///
/// # Return Value
///
/// Returns `ReturnCode::Success` when the XCM was successfully executed. When the XCM
/// execution fails, `ReturnCode::CallRuntimeFailed` is returned.
#[unstable]
fn xcm_execute(
ctx: _,
memory: _,
msg_ptr: u32,
msg_len: u32,
) -> Result<ReturnCode, TrapReason> {
use crate::xcm::{CallOf, WeightInfo, Xcm};
use frame_support::dispatch::DispatchInfo;
use xcm::VersionedXcm;
ctx.charge_gas(RuntimeCosts::CopyFromContract(msg_len))?;
let message: VersionedXcm<CallOf<E::T>> =
ctx.read_sandbox_memory_as_unbounded(memory, msg_ptr, msg_len)?;
let execute_weight = <<E::T as Config>::Xcm as Xcm<E::T>>::WeightInfo::execute();
let weight = ctx.ext.gas_meter().gas_left().max(execute_weight);
let dispatch_info = DispatchInfo { weight, ..Default::default() };
ctx.call_dispatchable(dispatch_info, |ext| {
<<E::T as Config>::Xcm as Xcm<E::T>>::execute(
ext.address(),
message,
weight.saturating_sub(execute_weight),
)
})
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with loosely coupling vs tightly coupling. We already do this all over the place when we need the pallet-xcm for something, so we have a bunch of traits that represent certain subsets of functionality. I'm in favor of reusing those and if no one suits your needs then modify it at the source because that should be the way to do this.

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ use sp_runtime::{DispatchError, DispatchResult};
use xcm::{VersionedMultiLocation, VersionedXcm};
use xcm_executor::traits::QueryResponseStatus;

pub mod pallet_xcm_adapter;
pub mod adapter;

pub use pallet_xcm_adapter::PalletXCMAdapter;
pub use adapter::XCMAdapter;
pub type CallOf<T> = <T as frame_system::Config>::RuntimeCall;

pub trait XCM<T: Config> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ use crate::{
xcm::{CallOf, WeightInfo, XCM},
AccountIdOf, Box, Config, RawOrigin,
};
use frame_support::{
dispatch::PostDispatchInfo, pallet_prelude::DispatchResultWithPostInfo, weights::Weight,
};
use frame_support::{pallet_prelude::DispatchResultWithPostInfo, weights::Weight};
use frame_system::pallet_prelude::BlockNumberFor;
use pallet_xcm::WeightInfo as XcmWeightInfo;
use sp_runtime::{DispatchError, DispatchErrorWithPostInfo, DispatchResult};
use sp_runtime::{DispatchError, DispatchResult};
use xcm::{v3::MultiLocation, VersionedMultiLocation, VersionedXcm};
use xcm_executor::traits::{QueryHandler, QueryResponseStatus};

/// A pallet-xcm adapter for the XCM trait.
pub struct PalletXCMAdapter<T: pallet_xcm::Config>(sp_std::marker::PhantomData<T>);
pub struct XCMAdapter<T: pallet_xcm::Config>(sp_std::marker::PhantomData<T>);

impl<T> WeightInfo for PalletXCMAdapter<T>
impl<T> WeightInfo for XCMAdapter<T>
where
T: pallet_xcm::Config,
{
Expand All @@ -49,8 +47,8 @@ where
}
}

impl<T: Config + pallet_xcm::Config> PalletXCMAdapter<T> {
/// Ensure that the message is executable, by checking that it does not contain any Transact
impl<T: Config + pallet_xcm::Config> XCMAdapter<T> {
/// Ensure that the message is executable, by checking that it does not contain any [`Transact`]
/// instruction with a call that is not allowed by the CallFilter.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this CallFilter a contracts thing? I'm not that familiar with contracts 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes it is. A contract can call any dispatchable as long as they are allowed by the CallFilter config trait.
So this piece just ensure that you can't execute a transact that is filtered.

fn ensure_executable(
message: VersionedXcm<CallOf<T>>,
Expand All @@ -76,7 +74,7 @@ impl<T: Config + pallet_xcm::Config> PalletXCMAdapter<T> {
}
}

impl<T: Config + pallet_xcm::Config> XCM<T> for PalletXCMAdapter<T> {
impl<T: Config + pallet_xcm::Config> XCM<T> for XCMAdapter<T> {
type QueryId = <pallet_xcm::Pallet<T> as QueryHandler>::QueryId;
type WeightInfo = Self;

Expand All @@ -85,16 +83,7 @@ impl<T: Config + pallet_xcm::Config> XCM<T> for PalletXCMAdapter<T> {
message: VersionedXcm<CallOf<T>>,
max_weight: Weight,
) -> DispatchResultWithPostInfo {
// TODO since we are doing more than just calling pallet_xcm::Pallet::<T>::execute, we
// should benchmark this ensure_executable
let msg = Self::ensure_executable(message).map_err(|err| DispatchErrorWithPostInfo {
post_info: PostDispatchInfo {
actual_weight: Some(Weight::zero()),
pays_fee: Default::default(),
},
error: err.into(),
})?;

let msg = Self::ensure_executable(message)?;
let origin = RawOrigin::Signed(origin.clone()).into();
pallet_xcm::Pallet::<T>::execute(origin, msg, max_weight)
}
Expand Down