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

Fix up contracts pallet tests #163

Merged
merged 3 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
116 changes: 79 additions & 37 deletions src/frame/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,43 +117,85 @@ pub struct InstantiatedEvent<T: Contracts> {

#[cfg(test)]
mod tests {
use sp_keyring::AccountKeyring;

use super::*;
use crate::{
ClientBuilder,
ContractsTemplateRuntime,
PairSigner,
};

fn contract_wasm() -> Vec<u8> {
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::<ContractsTemplateRuntime>::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::<ContractsTemplateRuntime>::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)
);
}
}
33 changes: 33 additions & 0 deletions src/runtimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <NodeTemplateRuntime as Runtime>::Signature;
type Extra = DefaultExtra<Self>;
}

impl System for ContractsTemplateRuntime {
type Index = <NodeTemplateRuntime as System>::Index;
type BlockNumber = <NodeTemplateRuntime as System>::BlockNumber;
type Hash = <NodeTemplateRuntime as System>::Hash;
type Hashing = <NodeTemplateRuntime as System>::Hashing;
type AccountId = <NodeTemplateRuntime as System>::AccountId;
type Address = <NodeTemplateRuntime as System>::Address;
type Header = <NodeTemplateRuntime as System>::Header;
type Extrinsic = <NodeTemplateRuntime as System>::Extrinsic;
type AccountData = <NodeTemplateRuntime as System>::AccountData;
}

impl Balances for ContractsTemplateRuntime {
type Balance = <NodeTemplateRuntime as Balances>::Balance;
}

impl Contracts for ContractsTemplateRuntime {}

impl Sudo for ContractsTemplateRuntime {}

/// Concrete type definitions compatible with those for kusama, v0.7
///
/// # Note
Expand Down