Crates.io | fvm-mock |
lib.rs | fvm-mock |
version | 1.0.0 |
source | src |
created_at | 2023-02-07 03:39:07.909569 |
updated_at | 2023-02-07 03:39:07.909569 |
description | tool for user to test contract locally |
homepage | |
repository | https://github.com/hyperchain/hyperchain |
max_upload_size | |
id | 778470 |
size | 39,502 |
fvm-mock is a tool for developer to check contract logical is correct or not locally, which is suit for hyperchain.
use macros::contract;
use macros::storage;
use fvm_std::runtime;
use scale::{Decode, Encode};
#[storage]
pub struct TestHello {}
#[contract]
impl TestHello {
fn new() -> Self {
Self {}
}
// 将键值对的键名加上test前缀再存入
pub fn set(&mut self, key: String, value: String) {
runtime::storage_write(key.as_bytes(), "test".as_bytes(), value.as_bytes())
}
// 读取存入的数据
pub fn get(&mut self, key: String) -> Vec<u8> {
return if let Some(res) = runtime::storage_read(key.as_bytes(), "test".as_bytes()) {
res
} else {
vec![]
};
}
}
In order to check locally, we should do as below
cargo.toml
[dev-dependencies]
# ... other dependencies
fvm-mock = "xxx"
build_runtime()
in unit test#[cfg(test)]
mod tests {
use scale::{Decode, Encode};
use crate::TestHello;
use fvm_mock::build_runtime;
#[test]
fn test_set() {
let mut contract = TestHello::new();
let handle = build_runtime();
contract.set("hello".to_string(), "world".to_string());
assert_eq!("world".as_bytes(), contract.get("hello".to_string()).as_slice())
}
#[test]
fn test_get() {
let mut contract = TestHello::new();
let handle = build_runtime();
handle.storage_write("hello", "test", "world");
assert_eq!("world".as_bytes(), contract.get("hello".to_string()).as_slice())
}
}
Notice that if you could pass the unit test, it indicates the logical is correct, but can not represent it can
run well on the blockchain.
For there are specifications that WASM core not support well, so developer should ensure that you do not use them, such
as print to console
, file read
and so on