From 4ed8e37ffe7d9d6cd6c340bcf3b0f9b4a94de74e Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Tue, 15 Sep 2020 08:32:33 +0100 Subject: [PATCH] Fix up contracts pallet tests (#163) * Revert contracts put_code test to pure code (not using the macro) * Test contract instantiate * Fmt --- Cargo.toml | 3 ++ src/frame/contracts.rs | 116 ++++++++++++++++++++++++++++------------- src/runtimes.rs | 33 ++++++++++++ 3 files changed, 115 insertions(+), 37 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..a3cfd149ed 100644 --- a/src/frame/contracts.rs +++ b/src/frame/contracts.rs @@ -117,43 +117,85 @@ pub struct InstantiatedEvent { #[cfg(test)] mod tests { + use sp_keyring::AccountKeyring; + use super::*; + use crate::{ + ClientBuilder, + ContractsTemplateRuntime, + PairSigner, + }; + + 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 + ) + ); + } + + #[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(); - 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(), - }, - }, - }); + assert!( + event.is_some(), + 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