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

[E2E] Allow testing with live-chain state #1949

Merged
merged 17 commits into from
Nov 16, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- [E2E] Allow testing with live-chain state - [#1949](https://github.com/paritytech/ink/pull/1949)
- [E2E] Call builders and extra gas margin option - [#1917](https://github.com/paritytech/ink/pull/1917)
- Make `set_code_hash` generic - [#1906](https://github.com/paritytech/ink/pull/1906)
- Clean E2E configuration parsing - [#1922](https://github.com/paritytech/ink/pull/1922)
Expand Down
60 changes: 42 additions & 18 deletions crates/e2e/macro/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl InkE2ETest {
pub fn generate_code(&self) -> TokenStream2 {
#[cfg(clippy)]
if true {
return quote! {}
return quote! {};
}

let item_fn = &self.test.item_fn.item_fn;
Expand Down Expand Up @@ -64,8 +64,12 @@ impl InkE2ETest {
}
};

let node_url = self.test.config.node_url();

let client_building = match self.test.config.backend() {
Backend::Full => build_full_client(&environment, exec_build_contracts),
Backend::Full => {
build_full_client(&environment, exec_build_contracts, node_url)
}
#[cfg(any(test, feature = "drink"))]
Backend::RuntimeOnly { runtime } => {
build_runtime_client(exec_build_contracts, runtime)
Expand Down Expand Up @@ -110,22 +114,42 @@ impl InkE2ETest {
}
}

fn build_full_client(environment: &syn::Path, contracts: TokenStream2) -> TokenStream2 {
quote! {
// Spawn a contracts node process just for this test.
let node_proc = ::ink_e2e::TestNodeProcess::<::ink_e2e::PolkadotConfig>
::build_with_env_or_default()
.spawn()
.await
.unwrap_or_else(|err|
::core::panic!("Error spawning substrate-contracts-node: {err:?}")
);

let contracts = #contracts;
let mut client = ::ink_e2e::Client::<
::ink_e2e::PolkadotConfig,
#environment
>::new(node_proc.rpc(), contracts).await?;
fn build_full_client(
environment: &syn::Path,
contracts: TokenStream2,
node_url: Option<String>,
) -> TokenStream2 {
match node_url {
Some(url) => {
quote! {
let rpc = ::ink_e2e::RpcClient::from_url(#url)
.await
.unwrap_or_else(|err|
::core::panic!("Error connecting to Chopsticks node: {err:?}")
);
let contracts = #contracts;
let mut client = ::ink_e2e::Client::<
::ink_e2e::PolkadotConfig,
#environment
>::new(rpc, contracts).await?;
}
}
None => {
quote! {
let node_rpc = ::ink_e2e::TestNodeProcess::<::ink_e2e::PolkadotConfig>
::build_with_env_or_default()
.spawn()
.await
.unwrap_or_else(|err|
::core::panic!("Error spawning substrate-contracts-node: {err:?}")
);
let contracts = #contracts;
let mut client = ::ink_e2e::Client::<
::ink_e2e::PolkadotConfig,
#environment
>::new(node_rpc.rpc(), contracts).await?;
}
}
}
}

Expand Down
12 changes: 12 additions & 0 deletions crates/e2e/macro/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ pub struct E2EConfig {
/// The type of the architecture that should be used to run test.
#[darling(default)]
backend: Backend,
/// The URL to the running node. If not set then a default node instance will be
/// spawned per test.
node_url: Option<String>,
}

impl E2EConfig {
Expand Down Expand Up @@ -73,6 +76,12 @@ impl E2EConfig {
pub fn backend(&self) -> Backend {
self.backend.clone()
}

/// The URL to the running node. If not set then a default node instance will be
/// spawned per test.
pub fn node_url(&self) -> Option<String> {
self.node_url.clone()
}
}

#[cfg(test)]
Expand All @@ -90,6 +99,7 @@ mod tests {
additional_contracts = "adder/Cargo.toml flipper/Cargo.toml",
environment = crate::CustomEnvironment,
backend(runtime_only()),
node_url = "ws://127.0.0.1:8000"
};
let config =
E2EConfig::from_list(&NestedMeta::parse_meta_list(input).unwrap()).unwrap();
Expand All @@ -104,6 +114,7 @@ mod tests {
);

assert_eq!(config.backend(), Backend::RuntimeOnly { runtime: None });
assert_eq!(config.node_url(), Some(String::from("ws://127.0.0.1:8000")))
}

#[test]
Expand All @@ -120,5 +131,6 @@ mod tests {
runtime: Some(syn::parse_quote! { ::ink_e2e::MinimalRuntime })
}
);
assert_eq!(config.node_url(), None)
}
}
5 changes: 4 additions & 1 deletion crates/e2e/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ pub use node_proc::{
};
pub use sp_core::H256;
pub use sp_keyring::AccountKeyring;
pub use subxt;
pub use subxt::{
self,
backend::rpc::RpcClient,
};
pub use subxt_client::{
CallBuilderFinal,
Client,
Expand Down
1 change: 0 additions & 1 deletion integration-tests/e2e-call-runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ ink = { path = "../../crates/ink", default-features = false }

[dev-dependencies]
ink_e2e = { path = "../../crates/e2e" }
subxt = { version = "0.32.1", default-features = false }

[lib]
path = "lib.rs"
Expand Down