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

Add xcm integration #1912

Merged
merged 57 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
3896cdf
Release `5.0.0`
SkymanOne Mar 4, 2024
4a778e8
update lock file
SkymanOne Mar 4, 2024
b296b4d
add release summary
SkymanOne Mar 4, 2024
6aae647
fix typos
SkymanOne Mar 4, 2024
6a932a3
update changelog
SkymanOne Mar 5, 2024
08f1087
update changelog
SkymanOne Mar 6, 2024
79b1b00
Merge branch 'master' into gn/5.0.0
SkymanOne Mar 6, 2024
e633876
update release notes
SkymanOne Mar 6, 2024
d786de3
Merge branch 'master' into gn/5.0.0
SkymanOne Mar 6, 2024
c283a6a
typo
SkymanOne Mar 6, 2024
00e0553
add migration guide notice
SkymanOne Mar 6, 2024
ffec19e
fix typo
SkymanOne Mar 7, 2024
87f5cc7
Apply suggestions from code review
Mar 8, 2024
47ac7ad
Merge branch 'master' into gn/5.0.0
SkymanOne Mar 11, 2024
3f24a9a
finish merge
SkymanOne Mar 11, 2024
d1aa5ac
Merge branch 'master' into gn/5.0.0
SkymanOne Mar 11, 2024
011b775
update changelog
SkymanOne Mar 11, 2024
e346592
Bump drink to 14.0.0 and fix drink_client
pgherveou Mar 12, 2024
a6a0fae
replace the export
SkymanOne Mar 12, 2024
8625f32
Reexport `subxt_signer`
cmichi Mar 12, 2024
3bd6899
Add xcm methods
pgherveou Jan 31, 2024
39dfd97
Proper fixup
pgherveou Mar 12, 2024
9b00d5b
Merge branch 'master' into pg/xcm
pgherveou Mar 13, 2024
f3a19d6
Merge branch 'master' into pg/xcm
pgherveou Apr 8, 2024
3445b55
Update latest master
pgherveou Apr 9, 2024
dc26cc6
nighltly fmt
pgherveou Apr 9, 2024
f1dbd38
rm changes
pgherveou Apr 9, 2024
0b189f8
fix lint
pgherveou Apr 9, 2024
20be373
wip
pgherveou Apr 9, 2024
ab1f53f
Update crates
pgherveou Apr 9, 2024
e1e4a4c
update Cargo.tomls
pgherveou Apr 9, 2024
cd3bc7e
rm lint
pgherveou Apr 9, 2024
c47d711
rm space
pgherveou Apr 9, 2024
17504a7
fix lints
pgherveou Apr 9, 2024
fede0a5
fix lint
pgherveou Apr 9, 2024
3d9be73
Merge branch 'master' into pg/xcm
pgherveou Apr 9, 2024
cfb1421
fix version
pgherveou Apr 9, 2024
78e649b
fix versions
pgherveou Apr 9, 2024
7402b0d
Fix lock
pgherveou Apr 9, 2024
c84a04d
Apply suggestions from code review
pgherveou Apr 10, 2024
fca97da
Apply suggestions from code review
pgherveou Apr 10, 2024
1044421
Merge branch 'master' into pg/xcm
pgherveou Apr 10, 2024
2d66e41
Move integration-tests
pgherveou Apr 10, 2024
b51c96c
fix
pgherveou Apr 10, 2024
4ee01a8
fix comment
pgherveou Apr 10, 2024
f4234c1
update changelog
pgherveou Apr 10, 2024
37ece22
fix fmt
pgherveou Apr 10, 2024
61016bb
Add comment
pgherveou Apr 12, 2024
ad7b06f
RM xcm-builder
pgherveou Apr 15, 2024
4bd4f04
ignore contract-xcm
pgherveou Apr 15, 2024
25a36d6
Use xcm builder
pgherveou Apr 15, 2024
1dbad34
fix lint
pgherveou Apr 15, 2024
172959a
Simplify XCM test
pgherveou Apr 17, 2024
891b548
nit cleanup
pgherveou Apr 17, 2024
dc8730d
nit cleanup
pgherveou Apr 17, 2024
70769af
fix
pgherveou Apr 18, 2024
b83a535
Merge branch 'master' into pg/xcm
pgherveou Apr 26, 2024
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
1 change: 1 addition & 0 deletions .config/cargo_spellcheck.dic
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,4 @@ DRink
^
externalities
sandbox_client
xcm
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ jobs:
# - custom_allocator
# Pulls in sp-std which needlessly requires atomic pointers (TODO: Fix sp-std and enable this example)
# - call-runtime
scripts/for_all_contracts_exec.sh --path integration-tests --ignore public/custom-allocator --ignore public/call-runtime \
scripts/for_all_contracts_exec.sh --path integration-tests --ignore public/custom-allocator --ignore public/call-runtime --ignore public/contract-xcm \
Copy link
Collaborator

Choose a reason for hiding this comment

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

The tests look like they pass (on my machine anyway) so we can enable them now?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is for building the risc-v example, I did you try that as well?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah no sorry I did not see it was part of the riscv step

-- cargo build --manifest-path {} --no-default-features --target $RISCV_TARGET -Zbuild-std="core,alloc"

examples-docs:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- [Linter] Add links to detailed lint description ‒ [#2170](https://github.com/use-ink/ink/pull/2170)
- Add `xcm_execute` and `xcm_send` support - [#1912](https://github.com/paritytech/ink/pull/1912)
- Environment agnostic contract invocation API ‒ [#219](https://github.com/use-ink/ink/pull/2219)

### Changed
Expand Down
2 changes: 2 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ sp-core = { version = "32.0.0", default-features = false }
sp-keyring = { version = "35.0.0", default-features = false }
sp-runtime = { version = "35.0.0", default-features = false }
sp-weights = { version = "31.0.0", default-features = false }
xcm = { package = "staging-xcm", version = "11.0.0", default-features = false }

# Local dependencies
ink = { version = "=5.0.0", path = "crates/ink", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/e2e/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ std = [
"ink_e2e_macro/std",
"ink_sandbox/std",
"frame-support/std",
"pallet-contracts-mock-network?/std"
"pallet-contracts-mock-network?/std",
ascjones marked this conversation as resolved.
Show resolved Hide resolved
]

sandbox = [
Expand Down
5 changes: 4 additions & 1 deletion crates/e2e/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ pub use node_proc::{
TestNodeProcessBuilder,
};
#[cfg(feature = "sandbox")]
pub use sandbox_client::Client as SandboxClient;
pub use sandbox_client::{
preset,
Client as SandboxClient,
};
pub use sp_core::H256;
pub use sp_keyring::AccountKeyring;
pub use subxt::{
Expand Down
13 changes: 11 additions & 2 deletions crates/e2e/src/sandbox_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,18 +483,27 @@ pub mod preset {
/// }
/// ```
#[derive(Default)]
pub struct MockNetworkSandbox;
pub struct MockNetworkSandbox {
dry_run: bool,
}

impl Sandbox for MockNetworkSandbox {
type Runtime = parachain::Runtime;

fn execute_with<T>(&mut self, execute: impl FnOnce() -> T) -> T {
ParaA::execute_with(execute)
if self.dry_run {
ParaA::execute_with(execute)
} else {
ParaA::execute_without_dispatch(execute)
}
}

fn dry_run<T>(&mut self, action: impl FnOnce(&mut Self) -> T) -> T {
EXT_PARAA.with(|v| {
let backend_backup = v.borrow_mut().as_backend();
self.dry_run = true;
let result = action(self);
self.dry_run = false;

let mut v = v.borrow_mut();
v.commit_all().expect("Failed to commit changes");
Expand Down
14 changes: 8 additions & 6 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 @@ -63,22 +64,23 @@ default = [ "std" ]
std = [
"blake2",
"ink_allocator/std",
"ink_engine/std",
"ink_prelude/std",
"ink_primitives/std",
"ink_storage_traits/std",
"num-traits/std",
"ink_engine/std",
"scale/std",
"scale-decode",
"scale-encode",
"scale-info/std",
"scale/std",
"schnorrkel",
"secp256k1",
# Enables hashing crates for off-chain environment.
"schnorrkel",
"num-traits/std",
# Enables hashing crates for off-chain environment.
"sha2",
"sha3",
"scale-decode?/std",
"scale-encode?/std"
"scale-encode?/std",
"xcm/std"
]

# Enable contract debug messages via `debug_print!` and `debug_println!`.
Expand Down
51 changes: 51 additions & 0 deletions crates/env/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,3 +903,54 @@ where
instance.unlock_delegate_dependency::<E>(code_hash)
})
}

/// Execute an XCM message locally, using the contract's address as the origin.
///
/// For more details consult the
/// [host function documentation](https://paritytech.github.io/substrate/master/pallet_contracts/api_doc/trait.Current.html#tymethod.xcm_execute).
///
/// # Errors
///
/// - If the message cannot be properly decoded on the `pallet-contracts` side.
/// - If the XCM execution fails because of the runtime's XCM configuration.
///
/// # 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)
})
}

/// Send an XCM message, using the contract's address as the origin.
///
/// The `msg` argument has to be SCALE encoded, it needs to be decodable to a valid
/// instance of the `RuntimeCall` enum.
///
/// For more details consult
/// [host function documentation](https://paritytech.github.io/substrate/master/pallet_contracts/api_doc/trait.Current.html#tymethod.xcm_send).
///
/// # Errors
///
/// - If the message cannot be properly decoded on the `pallet-contracts` side.
///
/// # Panics
///
/// Panics in the off-chain environment.
pub fn xcm_send<E, Call>(
dest: &xcm::VersionedLocation,
msg: &xcm::VersionedXcm<Call>,
) -> Result<xcm::v4::XcmHash>
where
E: Environment,
Call: scale::Encode,
{
<EnvInstance as OnInstance>::on_instance(|instance| {
TypedEnvBackend::xcm_send::<E, _>(instance, dest, msg)
})
}
24 changes: 24 additions & 0 deletions crates/env/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,28 @@ pub trait TypedEnvBackend: EnvBackend {
fn unlock_delegate_dependency<E>(&mut self, code_hash: &E::Hash)
where
E: Environment;

/// Execute an XCM message locally, using the contract's address as the origin.
///
/// # Note
///
/// For more details visit: [`xcm`][`crate::xcm_execute`].
fn xcm_execute<E, Call>(&mut self, msg: &xcm::VersionedXcm<Call>) -> Result<()>
where
E: Environment,
Call: scale::Encode;

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

fn xcm_execute<E, Call>(&mut self, _msg: &xcm::VersionedXcm<Call>) -> Result<()>
where
E: Environment,
{
unimplemented!("off-chain environment does not support `xcm_execute`")
}

fn xcm_send<E, Call>(
&mut self,
_dest: &xcm::VersionedLocation,
_msg: &xcm::VersionedXcm<Call>,
) -> Result<xcm::v4::XcmHash>
where
E: Environment,
{
unimplemented!("off-chain environment does not support `xcm_send`")
}

fn unlock_delegate_dependency<E>(&mut self, _code_hash: &E::Hash)
where
E: Environment,
Expand Down
37 changes: 37 additions & 0 deletions crates/env/src/engine/on_chain/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ use pallet_contracts_uapi::{
ReturnErrorCode,
ReturnFlags,
};
use xcm::VersionedXcm;

impl CryptoHash for Blake2x128 {
fn hash(input: &[u8], output: &mut <Self as HashOutput>::Type) {
Expand Down Expand Up @@ -726,4 +727,40 @@ impl TypedEnvBackend for EnvInstance {
let enc_code_hash = scope.take_encoded(code_hash);
ext::unlock_delegate_dependency(enc_code_hash)
}

fn xcm_execute<E, Call>(&mut self, msg: &VersionedXcm<Call>) -> Result<()>
where
E: Environment,
Call: scale::Encode,
{
let mut scope = self.scoped_buffer();

// Double encoding the message as the host fn expects an encoded message.
let enc_msg = scope.take_encoded(&scale::Encode::encode(msg));
ascjones marked this conversation as resolved.
Show resolved Hide resolved
#[allow(deprecated)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is the function already marked deprecated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unstable host api (which is still the case for this one as of this release of polkadot-sdk) are marked as deprecated so that they can trigger a warning if you try to use them without specifying allow(deprecated)

ext::xcm_execute(enc_msg).map_err(Into::into)
}

fn xcm_send<E, Call>(
&mut self,
dest: &xcm::VersionedLocation,
msg: &VersionedXcm<Call>,
) -> Result<xcm::v4::XcmHash>
where
E: Environment,
Call: scale::Encode,
{
let mut scope = self.scoped_buffer();
let output = scope.take(32);
scope.append_encoded(dest);
let enc_dest = scope.take_appended();

// Double encoding the message as the host fn expects an encoded message.
scope.append_encoded(&scale::Encode::encode(msg));
let enc_msg = scope.take_appended();
#[allow(deprecated)]
ext::xcm_send(enc_dest, enc_msg, output.try_into().unwrap())?;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could possibly use array_mut_ref! for output here?

let hash: xcm::v4::XcmHash = scale::Decode::decode(&mut &output[..])?;
Ok(hash)
}
}
3 changes: 2 additions & 1 deletion crates/ink/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ pallet-contracts-uapi = { workspace = true }
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 }

trybuild = { workspace = true, features = ["diff"] }


Expand All @@ -45,6 +45,7 @@ std = [
"ink_storage/std",
"scale-info/std",
"scale/std",
"xcm/std"
]
# Enable contract debug messages via `debug_print!` and `debug_println!`.
ink-debug = [ "ink_env/ink-debug" ]
Expand Down
15 changes: 15 additions & 0 deletions crates/ink/src/env_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,4 +1341,19 @@ where
pub fn unlock_delegate_dependency(self, code_hash: &E::Hash) {
ink_env::unlock_delegate_dependency::<E>(code_hash)
}

pub fn xcm_execute<Call: scale::Encode>(
self,
msg: &xcm::VersionedXcm<Call>,
) -> Result<()> {
ink_env::xcm_execute::<E, _>(msg)
}

pub fn xcm_send<Call: scale::Encode>(
self,
dest: &xcm::VersionedLocation,
msg: &xcm::VersionedXcm<Call>,
) -> Result<xcm::v4::XcmHash> {
ink_env::xcm_send::<E, _>(dest, msg)
}
}
1 change: 1 addition & 0 deletions crates/ink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub use ink_primitives as primitives;
pub use scale;
#[cfg(feature = "std")]
pub use scale_info;
pub use xcm;

pub mod storage {
pub mod traits {
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use {
From,
)]
#[cfg_attr(feature = "std", derive(TypeInfo, DecodeAsType, EncodeAsType))]
pub struct AccountId([u8; 32]);
pub struct AccountId(pub [u8; 32]);

impl AsRef<[u8; 32]> for AccountId {
#[inline]
Expand Down
27 changes: 27 additions & 0 deletions integration-tests/public/contract-xcm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "contract-xcm"
version = "4.0.0"
authors = ["Parity Technologies <admin@parity.io>"]
edition = "2021"
publish = false

[dependencies]
ink = { path = "../../../crates/ink", default-features = false }
frame-support = { version = "32.0.0", default-features = false }
pallet-balances = { version = "33.0.0", default-features = false }

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

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"pallet-balances/std",
"frame-support/std",
]
ink-as-dependency = []
e2e-tests = []
Loading