Skip to content

Commit

Permalink
XCM wip
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Feb 1, 2024
1 parent 4088fa5 commit adef8b3
Show file tree
Hide file tree
Showing 13 changed files with 653 additions and 78 deletions.
386 changes: 308 additions & 78 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ sp-keyring = { version = "26.0.0", default-features = false }
sp-runtime = { version = "26.0.0", default-features = false }
sp-weights = { version = "22.0.0", default-features = false }

xcm = { package = "staging-xcm", version = "1.0.0", default-features = false}

# Local dependencies
ink = { version = "=5.0.0-rc", path = "crates/ink", default-features = false }
ink_allocator = { version = "=5.0.0-rc", path = "crates/allocator", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions crates/env/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ cfg-if = { workspace = true }
paste = { workspace = true }
static_assertions = { workspace = true }
const_env = { workspace = true }
xcm = {workspace = true}

[target.'cfg(target_arch = "wasm32")'.dependencies]
rlibc = "1"
Expand Down Expand Up @@ -73,6 +74,7 @@ std = [
"secp256k1",
"schnorrkel",
"num-traits/std",
"xcm/std",
# Enables hashing crates for off-chain environment.
"sha2",
"sha3",
Expand Down
26 changes: 26 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,3 +787,29 @@ where
TypedEnvBackend::call_runtime::<E, _>(instance, call)
})
}

/// Execute an XCM message locally, using the contract's address as the origin.
///
/// `call` (after SCALE encoding) should be decodable to a valid instance of `RuntimeCall`
/// enum.
///
/// For more details consult
/// [host function documentation](https://paritytech.github.io/substrate/master/pallet_contracts/api_doc/trait.Current.html#tymethod.xcm_execute).
///
/// # Errors
///
/// - If the call cannot be properly decoded on the pallet contracts side.
/// - If the runtime doesn't allow for the contract unstable feature.
///
/// # Panics
///
/// Panics in the off-chain environment.
pub fn xcm_execute<E, Call>(msg: &xcm::VersionedXcm<Call>) -> Result<()>
where
E: Environment,
Call: scale::Encode,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::xcm_execute::<E, _>(instance, msg)
})
}
10 changes: 10 additions & 0 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,14 @@ pub trait TypedEnvBackend: EnvBackend {
where
E: Environment,
Call: scale::Encode;

/// Execute an XCM message locally, using the contract's address as the origin.
///
/// # Note
///
/// For more details visit: [`xcm_execute`][`crate::xcm_execute`]
fn xcm_execute<E, Call>(&mut self, msg: &xcm::VersionedXcm<Call>) -> Result<()>
where
E: Environment,
Call: scale::Encode;
}
7 changes: 7 additions & 0 deletions crates/env/src/engine/off_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,11 @@ impl TypedEnvBackend for EnvInstance {
{
unimplemented!("off-chain environment does not support `call_runtime`")
}

fn xcm_execute<E, Call>(&mut self, _msg: &xcm::VersionedXcm<Call>) -> Result<()>
where
E: Environment,
{
unimplemented!("off-chain environment does not support `xcm_execute`")
}
}
10 changes: 10 additions & 0 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,4 +612,14 @@ impl TypedEnvBackend for EnvInstance {
let enc_call = scope.take_encoded(call);
ext::call_runtime(enc_call).map_err(Into::into)
}

fn xcm_execute<E, Call>(&mut self, msg: &VersionedXcm<Call>) -> Result<()>
where
E: Environment,
Call: scale::Encode,
{
let mut scope = self.scoped_buffer();
let enc_msg = scope.take_encoded(msg);
ext::xcm_execute(enc_msg).map_err(Into::into)
}
}
3 changes: 3 additions & 0 deletions crates/ink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ scale = { workspace = true }
scale-info = { workspace = true, default-features = false, features = ["derive"], optional = true }
derive_more = { workspace = true, features = ["from"] }

xcm = { workspace = true}

[dev-dependencies]
ink_ir = { workspace = true, default-features = true }
ink_metadata = { workspace = true }
Expand All @@ -45,6 +47,7 @@ std = [
"ink_macro/std",
"scale/std",
"scale-info/std",
"xcm/std",
]
# Enable contract debug messages via `debug_print!` and `debug_println!`.
ink-debug = [
Expand Down
7 changes: 7 additions & 0 deletions crates/ink/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,4 +1112,11 @@ where
pub fn call_runtime<Call: scale::Encode>(self, call: &Call) -> Result<()> {
ink_env::call_runtime::<E, _>(call)
}

pub fn xcm_execute<Call: scale::Encode>(
self,
msg: &xcm::VersionedXcm<Call>,
) -> Result<()> {
ink_env::xcm_execute::<E, _>(msg)
}
}
9 changes: 9 additions & 0 deletions integration-tests/xcm-execute/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Ignore build artifacts from the local tests sub-crate.
/target/

# Ignore backup files creates by cargo fmt.
**/*.rs.bk

# Remove Cargo.lock when creating an executable, leave it for libraries
# More information here http://doc.crates.io/guide.html#cargotoml-vs-cargolock
Cargo.lock
35 changes: 35 additions & 0 deletions integration-tests/xcm-execute/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
[package]
name = "xcm-execute"
version = "4.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
publish = false

[dependencies]
ink = { path = "../../crates/ink", default-features = false }
xcm = { package = "staging-xcm", version = "1.0.0", default-features = false}

# Substrate
#
# We need to explicitly turn off some of the `sp-io` features, to avoid conflicts
# (especially for global allocator).
#
# See also: https://substrate.stackexchange.com/questions/4733/error-when-compiling-a-contract-using-the-xcm-chain-extension.
sp-io = { version = "23.0.0", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] }
sp-runtime = { version = "24.0.0", default-features = false }

[dev-dependencies]
ink_e2e = { path = "../../crates/e2e" }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"sp-runtime/std",
"sp-io/std",
]
ink-as-dependency = []
e2e-tests = []
22 changes: 22 additions & 0 deletions integration-tests/xcm-execute/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# `xcm-execute` example

## What is this example about?

It demonstrates how to use XCM from an ink! contract.

## Chain-side configuration

To integrate this example into Substrate you need to implement `pallet-xcm` and configure the `Xcm` trait of `pallet-contracts`

```rust
// In your node's runtime configuration file (runtime.rs)
impl pallet_xcm::Config for Runtime {
// ...
}

impl pallet_contracts::Config for Runtime {
//
type Xcm = PalletXCMAdapter<Self>;
//
}
```
Loading

0 comments on commit adef8b3

Please sign in to comment.