Crate casper_engine_test_support[][src]

Expand description

A library to support testing of Wasm smart contracts for use on the Casper Platform.

Example

Consider a contract held in “contract.wasm” which stores an arbitrary String under a Key named “special_value”:

use casper_contract::contract_api::{runtime, storage};
use casper_types::Key;
const KEY: &str = "special_value";
const ARG_VALUE: &str = "value";

#[no_mangle]
pub extern "C" fn call() {
    let value: String = runtime::get_named_arg(ARG_VALUE);
    let value_ref = storage::new_uref(value);
    let value_key: Key = value_ref.into();
    runtime::put_key(KEY, value_key);
}

The test could be written as follows:

use casper_engine_test_support::{Code, Error, SessionBuilder, TestContextBuilder, Value};
use casper_types::{U512, RuntimeArgs, runtime_args, PublicKey, account::AccountHash, SecretKey};


const MY_ACCOUNT: [u8; 32] = [7u8; 32];
const MY_ADDR: [u8; 32] = [8u8; 32];
const KEY: &str = "special_value";
const VALUE: &str = "hello world";
const ARG_MESSAGE: &str = "message";

let secret_key = SecretKey::ed25519_from_bytes(MY_ACCOUNT).unwrap();
let public_key = PublicKey::from(&secret_key);
let account_addr = AccountHash::new(MY_ADDR);

let mut context = TestContextBuilder::new()
    .with_public_key(public_key, U512::from(128_000_000_000_000u64))
    .build();

// The test framework checks for compiled Wasm files in '<current working dir>/wasm'.  Paths
// relative to the current working dir (e.g. 'wasm/contract.wasm') can also be used, as can
// absolute paths.
let session_code = Code::from("contract.wasm");
let session_args = runtime_args! {
    ARG_MESSAGE => VALUE,
};
let session = SessionBuilder::new(session_code, session_args)
    .with_address(account_addr)
    .with_authorization_keys(&[account_addr])
    .build();

let result_of_query: Result<Value, Error> = context.run(session).query(account_addr, &[KEY.to_string()]);

let returned_value = result_of_query.expect("should be a value");

let expected_value = Value::from_t(VALUE.to_string()).expect("should construct Value");
assert_eq!(expected_value, returned_value);

Structs

An Account instance.

A newtype wrapping a AccountHashBytes which is the raw bytes of the AccountHash, a hash of Public Key and Algorithm

The error type returned by any casper-engine-test-support operation.

A single session, i.e. a single request to execute a single deploy within the test context.

Builder for a Session.

Transfer Information for validating a transfer including gas usage from source

Context in which to run a test of a Wasm smart contract.

A value stored under a given key on the network.

Enums

Represents the types of session or payment code.

Constants

Default initial balance of a test account in motes.

Minimal amount for a transfer that creates new accounts.

Statics

Type Definitions

The hash of a smart contract stored on the network, which can be used to reference the contract.

A specialized std::result::Result for this crate.

The address of a URef (unforgeable reference) on the network.