From 9a8680246ea87bb8f3f9a9e2f252dffa574a2720 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 14 Sep 2020 13:44:11 +0100 Subject: [PATCH 1/3] Revert contracts put_code test to pure code (not using the macro) --- Cargo.toml | 3 ++ src/frame/contracts.rs | 108 ++++++++++++++++++++++++++--------------- src/runtimes.rs | 33 +++++++++++++ 3 files changed, 106 insertions(+), 38 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 16b02b4932..210a5c760e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,9 @@ include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"] [features] client = ["substrate-subxt-client"] +# enable this feature to run tests which require a local dev chain node +integration-tests = [] + [dependencies] log = "0.4.11" thiserror = "1.0.20" diff --git a/src/frame/contracts.rs b/src/frame/contracts.rs index 0a0bc1da34..c36311f80f 100644 --- a/src/frame/contracts.rs +++ b/src/frame/contracts.rs @@ -117,43 +117,75 @@ pub struct InstantiatedEvent { #[cfg(test)] mod tests { - use super::*; + use sp_keyring::AccountKeyring; - subxt_test!({ - name: test_put_code_and_instantiate, - prelude: { - const CONTRACT: &str = r#" -(module - (func (export "call")) - (func (export "deploy")) -) -"#; - let wasm = wabt::wat2wasm(CONTRACT).expect("invalid wabt"); - let code_hash; - }, - step: { - call: PutCodeCall { - _runtime: PhantomData, - code: &wasm, - }, - event: CodeStoredEvent { - code_hash: { - code_hash = event.code_hash.clone(); - event.code_hash.clone() - }, - }, - }, - step: { - call: InstantiateCall { - endowment: 100_000_000_000_000, - gas_limit: 500_000_000, - code_hash: &code_hash, - data: &[], - }, - event: InstantiatedEvent { - caller: alice.clone(), - contract: event.contract.clone(), - }, - }, - }); + use super::*; + use crate::{ClientBuilder, PairSigner, ContractsTemplateRuntime}; + + fn contract_wasm() -> Vec { + const CONTRACT: &str = r#" + (module + (func (export "call")) + (func (export "deploy")) + ) + "#; + wabt::wat2wasm(CONTRACT).expect("invalid wabt") + } + + #[async_std::test] + #[cfg(feature = "integration-tests")] + async fn tx_put_code() { + env_logger::try_init().ok(); + + let signer = PairSigner::new(AccountKeyring::Alice.pair()); + let client = ClientBuilder::::new().build().await.unwrap(); + + let code = contract_wasm(); + let result = client.put_code_and_watch(&signer, &code).await.unwrap(); + let code_stored = result.code_stored().unwrap(); + + assert!( + code_stored.is_some(), + format!( + "Error calling put_code and receiving CodeStored Event: {:?}", + code_stored + ) + ); + } + + // #[test] + // #[cfg(feature = "integration-tests")] + // fn tx_instantiate() { + // env_logger::try_init().ok(); + // let result: Result<_, Error> = async_std::task::block_on(async move { + // let signer = AccountKeyring::Bob.pair(); + // let client = test_client().await; + // + // let code_hash = put_code(&client, signer.clone()).await?; + // + // log::info!("Code hash: {:?}", code_hash); + // + // let xt = client.xt(signer, None).await?; + // let result = xt + // .watch() + // .submit(InstantiateCall { + // endowment: 100_000_000_000_000, + // gas_limit: 500_000_000, + // code_hash: &code_hash, + // data: &[], + // }) + // .await?; + // let event = result + // .find_event::>()? + // .ok_or(Error::Other("Failed to find Instantiated event".into()))?; + // Ok(event) + // }); + // + // log::info!("Instantiate result: {:?}", result); + // + // assert!( + // result.is_ok(), + // format!("Error instantiating contract: {:?}", result) + // ); + // } } diff --git a/src/runtimes.rs b/src/runtimes.rs index 13779d71b8..5e1a55ce42 100644 --- a/src/runtimes.rs +++ b/src/runtimes.rs @@ -116,6 +116,39 @@ impl Balances for NodeTemplateRuntime { impl Sudo for NodeTemplateRuntime {} +/// Concrete type definitions compatible with the node template, with the +/// contracts pallet enabled. +/// +/// Inherits types from [`NodeTemplateRuntime`], but adds an implementation for +/// the contracts pallet trait. +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct ContractsTemplateRuntime; + +impl Runtime for ContractsTemplateRuntime { + type Signature = ::Signature; + type Extra = DefaultExtra; +} + +impl System for ContractsTemplateRuntime { + type Index = ::Index; + type BlockNumber = ::BlockNumber; + type Hash = ::Hash; + type Hashing = ::Hashing; + type AccountId = ::AccountId; + type Address = ::Address; + type Header = ::Header; + type Extrinsic = ::Extrinsic; + type AccountData = ::AccountData; +} + +impl Balances for ContractsTemplateRuntime { + type Balance = ::Balance; +} + +impl Contracts for ContractsTemplateRuntime {} + +impl Sudo for ContractsTemplateRuntime {} + /// Concrete type definitions compatible with those for kusama, v0.7 /// /// # Note From 0e7bc2d2e57389580922f1c3ca236d61335aa75b Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 14 Sep 2020 16:12:48 +0100 Subject: [PATCH 2/3] Test contract instantiate --- src/frame/contracts.rs | 70 +++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/frame/contracts.rs b/src/frame/contracts.rs index c36311f80f..2f0498a96c 100644 --- a/src/frame/contracts.rs +++ b/src/frame/contracts.rs @@ -153,39 +153,39 @@ mod tests { ); } - // #[test] - // #[cfg(feature = "integration-tests")] - // fn tx_instantiate() { - // env_logger::try_init().ok(); - // let result: Result<_, Error> = async_std::task::block_on(async move { - // let signer = AccountKeyring::Bob.pair(); - // let client = test_client().await; - // - // let code_hash = put_code(&client, signer.clone()).await?; - // - // log::info!("Code hash: {:?}", code_hash); - // - // let xt = client.xt(signer, None).await?; - // let result = xt - // .watch() - // .submit(InstantiateCall { - // endowment: 100_000_000_000_000, - // gas_limit: 500_000_000, - // code_hash: &code_hash, - // data: &[], - // }) - // .await?; - // let event = result - // .find_event::>()? - // .ok_or(Error::Other("Failed to find Instantiated event".into()))?; - // Ok(event) - // }); - // - // log::info!("Instantiate result: {:?}", result); - // - // assert!( - // result.is_ok(), - // format!("Error instantiating contract: {:?}", result) - // ); - // } + #[async_std::test] + #[cfg(feature = "integration-tests")] + async fn tx_instantiate() { + env_logger::try_init().ok(); + let signer = PairSigner::new(AccountKeyring::Bob.pair()); + let client = ClientBuilder::::new().build().await.unwrap(); + + // call put_code extrinsic + let code = contract_wasm(); + let result = client.put_code_and_watch(&signer, &code).await.unwrap(); + let code_stored = result.code_stored().unwrap(); + let code_hash = code_stored.unwrap().code_hash; + + log::info!("Code hash: {:?}", code_hash); + + // call instantiate extrinsic + let result = client + .instantiate_and_watch( + &signer, + 100_000_000_000_000, // endowment + 500_000_000, // gas_limit + &code_hash, + &[], // data + ) + .await + .unwrap(); + + log::info!("Instantiate result: {:?}", result); + let event = result.instantiated().unwrap(); + + assert!( + event.is_some(), + format!("Error instantiating contract: {:?}", result) + ); + } } From 92bcd26c810522b1e4766a9a521b2b1d297f26ed Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Mon, 14 Sep 2020 16:13:17 +0100 Subject: [PATCH 3/3] Fmt --- src/frame/contracts.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/frame/contracts.rs b/src/frame/contracts.rs index 2f0498a96c..a3cfd149ed 100644 --- a/src/frame/contracts.rs +++ b/src/frame/contracts.rs @@ -120,7 +120,11 @@ mod tests { use sp_keyring::AccountKeyring; use super::*; - use crate::{ClientBuilder, PairSigner, ContractsTemplateRuntime}; + use crate::{ + ClientBuilder, + ContractsTemplateRuntime, + PairSigner, + }; fn contract_wasm() -> Vec { const CONTRACT: &str = r#" @@ -138,7 +142,10 @@ mod tests { env_logger::try_init().ok(); let signer = PairSigner::new(AccountKeyring::Alice.pair()); - let client = ClientBuilder::::new().build().await.unwrap(); + let client = ClientBuilder::::new() + .build() + .await + .unwrap(); let code = contract_wasm(); let result = client.put_code_and_watch(&signer, &code).await.unwrap(); @@ -158,7 +165,10 @@ mod tests { async fn tx_instantiate() { env_logger::try_init().ok(); let signer = PairSigner::new(AccountKeyring::Bob.pair()); - let client = ClientBuilder::::new().build().await.unwrap(); + let client = ClientBuilder::::new() + .build() + .await + .unwrap(); // call put_code extrinsic let code = contract_wasm(); @@ -172,10 +182,10 @@ mod tests { let result = client .instantiate_and_watch( &signer, - 100_000_000_000_000, // endowment - 500_000_000, // gas_limit + 100_000_000_000_000, // endowment + 500_000_000, // gas_limit &code_hash, - &[], // data + &[], // data ) .await .unwrap();