1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! fvm-mock is a tool for developer to check contract logical is correct or not locally, which is suit for hyperchain.
//!
//! ### Demo contract
//!
//! ```ignore
//! 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 {}
//! }
//!
//! 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
//!
//! + Add dependency in `cargo.toml`
//!
//! ```toml
//! [dev-dependencies]
//! # ... other dependencies
//! fvm-mock = "xxx"
//! ```
//!
//! + Call `build_runtime()` in unit test
//!
//! ```ignore
//! #[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 chain.
//! 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***
use cfg_if;
cfg_if!