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

feat: add async XCM contract call scenario #941

Merged
merged 21 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
51ce1c1
feat: remove fixtures dir
ashutoshvarma May 18, 2023
a0f5cd0
feat: add utilty method to call contract method
ashutoshvarma May 18, 2023
60bcb33
feat: build.rs for compiling ink! contracts
ashutoshvarma May 18, 2023
92fa3ad
fix: ci build issues
ashutoshvarma May 19, 2023
965f42a
feat: add async xcm ink! contract fixture
ashutoshvarma May 20, 2023
cc192cd
feat: add async xcm experimental scenario
ashutoshvarma May 20, 2023
45ffe6a
feat: add call filter to allow `pallet_xcm:send` only
ashutoshvarma May 21, 2023
d4cd410
docs: add comments
ashutoshvarma May 21, 2023
b5b228c
fix: make clippy happy
ashutoshvarma May 21, 2023
e148ddf
fix: make clippy happy again
ashutoshvarma May 21, 2023
90b6ab7
feat: apply review suggestions
ashutoshvarma May 24, 2023
3133613
lint: make clippy happy
ashutoshvarma May 25, 2023
985d5bf
feat: update package meta for fixture contracts
ashutoshvarma Jun 1, 2023
b78d52e
feat: move `contract-build` dep to workspace
ashutoshvarma Jun 1, 2023
8b6f9b1
feat: automatically pick contracts from contarcts_dir
ashutoshvarma Jun 1, 2023
657cccb
refactor: move build contracts method to `BuildConfig`
ashutoshvarma Jun 1, 2023
af03b05
docs: update readme for contract fixtures
ashutoshvarma Jun 1, 2023
f9eeae0
feat: remove build script for contract compilation
ashutoshvarma Jun 12, 2023
b7dd9cb
feat: revert build script changes
ashutoshvarma Jun 12, 2023
099d441
Merge branch 'master' into feat/async-contract-call-xcm-scenario
ashutoshvarma Jul 4, 2023
ac5c028
Merge branch 'master' into feat/async-contract-call-xcm-scenario
ashutoshvarma Jul 4, 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
98 changes: 96 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[toolchain]
channel = "nightly-2023-01-10"
components = ["rustfmt", "clippy"]
components = ["rustfmt", "clippy", "rust-src"]
targets = ["wasm32-unknown-unknown"]
profile = "minimal"
1 change: 1 addition & 0 deletions tests/xcm-simulator/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fixtures
10 changes: 10 additions & 0 deletions tests/xcm-simulator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ orml-traits = { workspace = true }
orml-xcm-support = { workspace = true }
orml-xtokens = { workspace = true }

[build-dependencies]
# Currently using patched version due to issue with `RUSTFLAGS` inside build script.
# cargo-build sets the `RUSTFLAGS` for adding linker flags which are not applied when
# invoking it inside a build script, thus contract compilation fails.
# Fix - use `CARGO_ENCODED_RUSTFLAGS` instead of `RUSTFLAGS`
# https://github.com/rust-lang/cargo/issues/10111
# TODO: remove this once it is merged in upstream
contract-build = { git = "https://github.com/ashutoshvarma/cargo-contract", branch = "patch/fix-rustflags" }

[features]
default = ["std"]
std = [
Expand Down Expand Up @@ -93,4 +102,5 @@ runtime-benchmarks = [
"xcm-executor/runtime-benchmarks",
"polkadot-runtime-parachains/runtime-benchmarks",
"polkadot-parachain/runtime-benchmarks",
"orml-xtokens/runtime-benchmarks",
]
140 changes: 140 additions & 0 deletions tests/xcm-simulator/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

/// build.rs
/// Compile and copy the contract artifacts to be used as fixture
/// in tests
use std::{
fs,
path::{Path, PathBuf},
};

use contract_build::{
BuildArtifacts, BuildMode, Features, ManifestPath, Network, OptimizationPasses, OutputType,
Target, UnstableFlags, Verbosity,
};

const DEFAULT_FIXTURES_DIR: &'static str = "./fixtures";
const DEFAULT_CONTRACTS_DIR: &'static str = "./contracts";

/// Execute the clousre with given directory as current dir
fn with_directory<T, F: FnOnce() -> T>(dir: &Path, f: F) -> T {
let curr_dir = std::env::current_dir().unwrap();

std::env::set_current_dir(dir).unwrap();
let res = f();
std::env::set_current_dir(curr_dir).unwrap();

res
}

/// Build config for adjusting the ink! contract compilation
struct BuildConfig {
/// Directory where artifacts will be copied to after compilation
fixtures_dir: PathBuf,
/// Directory where individual contract are present, each on it's own sub-directory
contracts_dir: PathBuf,
is_verbose: bool,
/// Whether to build the metadata json along with WASM blob
build_metadata: bool,
/// Skip Wasm post build validation
skip_wasm_validation: bool,
}

impl BuildConfig {
fn from_env() -> Self {
Self {
fixtures_dir: PathBuf::from(
std::env::var("CB_FIXTURES_DIR").unwrap_or(DEFAULT_FIXTURES_DIR.to_string()),
),
contracts_dir: PathBuf::from(
std::env::var("CB_CONTRACTS_DIR").unwrap_or(DEFAULT_CONTRACTS_DIR.to_string()),
),
is_verbose: std::env::var("CB_BUILD_VERBOSE").is_ok(),
build_metadata: std::env::var("CB_BUILD_METADATA").is_ok(),
skip_wasm_validation: std::env::var("CB_SKIP_WASM_VALIDATION").is_ok(),
}
}
}

/// Build the contracts and copy the artifacts to fixtures dir
fn build_contracts(config: &BuildConfig, contacts: Vec<&str>) {
for contract in contacts {
let dir = &config.contracts_dir.join(contract);
println!("[build.rs] Building Contract - {contract}");
let build = with_directory(dir, || {
let manifest_path = ManifestPath::new("Cargo.toml").unwrap();
let verbosity = if config.is_verbose {
Verbosity::Verbose
} else {
Verbosity::Default
};
let build_artifact = if config.build_metadata {
BuildArtifacts::All
} else {
BuildArtifacts::CodeOnly
};
let args = contract_build::ExecuteArgs {
manifest_path,
verbosity,
build_artifact,
skip_wasm_validation: config.skip_wasm_validation,
build_mode: BuildMode::Debug,
features: Features::default(),
network: Network::Online,
unstable_flags: UnstableFlags::default(),
optimization_passes: Some(OptimizationPasses::default()),
keep_debug_symbols: true,
lint: false,
output_type: OutputType::HumanReadable,
target: Target::Wasm,
};
contract_build::execute(args).expect(&format!("Failed to build contract at - {dir:?}"))
});

// copy wasm artifact
fs::copy(
build.dest_wasm.unwrap(),
config.fixtures_dir.join(format!("{contract}.wasm")),
)
.unwrap();

// copy metadata
if let Some(res) = build.metadata_result {
fs::copy(
res.dest_metadata,
config.fixtures_dir.join(format!("{contract}.json")),
)
.unwrap();
}
}
}

fn main() {
let config = BuildConfig::from_env();
// create fixtures dir if not exists
fs::create_dir_all(&config.fixtures_dir).unwrap();

// build all the contracts
build_contracts(&config, ["flipper", "async-xcm-call-no-ce"].to_vec());
ashutoshvarma marked this conversation as resolved.
Show resolved Hide resolved

println!(
"cargo:rerun-if-changed={}",
config.contracts_dir.to_str().unwrap()
);
}
9 changes: 9 additions & 0 deletions tests/xcm-simulator/contracts/async-xcm-call-no-ce/.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
29 changes: 29 additions & 0 deletions tests/xcm-simulator/contracts/async-xcm-call-no-ce/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "async_xcm_call_contract"
version = "0.1.0"
authors = ["Ashutosh Varma <ashutoshvarma11@live.com>"]
ashutoshvarma marked this conversation as resolved.
Show resolved Hide resolved
edition = "2021"

[dependencies]
ink = { version = "~4.2.0", default-features = false, features = ["call-runtime"] }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] }
scale-info = { version = "2.6", default-features = false, features = ["derive"], optional = true }
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.39", default-features = false }

[lib]
path = "lib.rs"

[features]
default = ["std"]
std = [
"ink/std",
"scale/std",
"scale-info/std",
"xcm/std",
]
ink-as-dependency = []

[profile.release]
overflow-checks = false

[workspace]
Loading