#contracts #testing #smart-contracts #casper-labs #key #casperlabs #networking

已删除 casperlabs-engine-test-support

支持在CasperLabs网络上使用Wasm智能合约测试的库

0.8.1 2020年7月31日
0.7.1 2020年7月2日
0.6.0 2020年3月31日

#casper-labs中排名21

自定义许可

1.5MB
32K SLoC

casperlabs-engine-test-support

LOGO

Build Status Crates.io Documentation License

一个库,用于支持在CasperLabs网络上测试Wasm智能合约。

许可

根据CasperLabs开源许可(COSL)授权。


lib.rs:

一个库,用于支持在CasperLabs平台上测试Wasm智能合约。

示例

假设有一个名为"contract.wasm"的合约,它在名为"special_value"的密钥下存储任意String

use casperlabs_contract::contract_api::{runtime, storage};
use casperlabs_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);
}

测试可以编写如下:

use casperlabs_engine_test_support::{Code, Error, SessionBuilder, TestContextBuilder, Value};
use casperlabs_types::{account::AccountHash, U512, RuntimeArgs, runtime_args};

const MY_ACCOUNT: AccountHash = AccountHash::new([7u8; 32]);
const KEY: &str = "special_value";
const VALUE: &str = "hello world";
const ARG_MESSAGE: &str = "message";

let mut context = TestContextBuilder::new()
    .with_account(MY_ACCOUNT, U512::from(128_000_000))
    .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(MY_ACCOUNT)
    .with_authorization_keys(&[MY_ACCOUNT])
    .build();

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

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);

依赖项

~44MB
~712K SLoC