fvm-mock 1.0.0

tool for user to test contract locally
Documentation
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use sha3::Digest;
use fvm_std::prelude::{Vec, Address, H256};
use fvm_std::types::LogLevel;


/// Mock of contract execution runtime
#[derive(Default)]
pub struct Runtime {
    pub inner: Rc<RefCell<RuntimeInner>>,
}

#[derive(Default)]
pub struct RuntimeInner {
    pub storage: HashMap<Vec<u8>, Vec<u8>>,
    pub block_time: u64,
    pub block_height: u64,
    pub caller_addr: Address,
    pub origin_addr: Address,
    pub self_addr: Address,
    pub block_hash: H256,
    pub tx_hash: H256,
    pub log: Vec<Vec<u8>>,
    pub call_return: Vec<Vec<u8>>,
    pub debug: HashMap<u32, Vec<Vec<u8>>>,
    pub input: Vec<u8>,
    pub ret: Vec<u8>,
    pub hyper_counter: HashMap<Vec<u8>, i64>,
    pub hyper_list: HashMap<Vec<u8>, Vec<i64>>,
}

impl RuntimeInner {
    fn call_contract(&mut self) -> u32 {
        self.call_return.last().expect("cross call set is not enough.").len() as u32
    }
}

impl Runtime {
    fn storage_write(&self, key: &[u8], prefix: &[u8], val: &[u8]) {
        let mut p = prefix.to_vec();
        let mut s = key.to_vec();
        p.append(&mut s);
        self.inner.borrow_mut().storage.insert(p, val.to_vec());
    }

    fn storage_read(&self, key: &[u8], prefix: &[u8]) -> Option<Vec<u8>> {
        let mut p = prefix.to_vec();
        let mut s = key.to_vec();
        p.append(&mut s);
        self.inner.borrow().storage.get(&p).map(|val| val.to_vec())
    }

    fn storage_delete(&self, key: &[u8], prefix: &[u8]) {
        let mut p = prefix.to_vec();
        let mut s = key.to_vec();
        p.append(&mut s);
        self.inner.borrow_mut().storage.remove(&p);
    }

    fn block_time(&self) -> u64 {
        self.inner.borrow().block_time
    }

    fn block_height(&self) -> u64 {
        self.inner.borrow().block_height
    }

    fn self_address(&self) -> Address {
        self.inner.borrow().self_addr.clone()
    }

    fn caller_address(&self) -> Address {
        self.inner.borrow().caller_addr.clone()
    }

    fn origin_address(&self) -> Address {
        self.inner.borrow().origin_addr.clone()
    }

    fn tx_hash(&self) -> H256 {
        self.inner.borrow().tx_hash.clone()
    }

    fn event(&self, msg: &[u8]) {
        self.inner.borrow_mut().log.push(msg.to_vec());
    }

    fn sha256(&self, data: &[u8]) -> H256 {
        let mut hasher = sha3::Keccak256::new();
        hasher.update(data);
        let hash = hasher.finalize();
        H256::from_slice(hash.as_slice())
    }

    fn call_contract(&self, _: &Address, _: &[u8]) -> u32 {
        self.inner.borrow_mut().call_contract()
    }

    fn cns_call_contract(&self, _: &[u8], _: &[u8]) -> u32 {
        self.inner.borrow_mut().call_contract()
    }

    fn call_return(&self) -> Vec<u8> {
        self.inner.borrow_mut().call_return.pop().expect("cross call set is not enough.")
    }

    fn ret(&self, _data: &[u8]) {
        self.inner.borrow_mut().ret = _data.to_vec()
    }

    fn debug(&self, class_name: &[u8], data: &[u8], level: LogLevel) {
        let mut c = String::from("[");
        c.push_str(String::from_utf8(class_name.to_vec()).unwrap().as_str());
        c.push_str("]: ");
        c.push_str(String::from_utf8(data.to_vec()).unwrap().as_str());
        if self.inner.borrow_mut().debug.contains_key(&level.to_int()) {
            self.inner.borrow_mut().debug.get_mut(&level.to_int()).unwrap().push(c.into_bytes());
        } else {
            let mut temp = Vec::new();
            temp.push(c.into_bytes());
            self.inner.borrow_mut().debug.entry(level.to_int()).or_insert(temp);
        }
    }

    pub fn revert(&self, _msg: &str) -> ! {
        panic!("{}", _msg)
    }

    pub fn input(&self) -> Vec<u8> {
        self.inner.borrow_mut().input.to_vec()
    }

    pub fn input_length(&self) -> u32 {
        self.inner.borrow().input.len() as u32
    }

    pub fn add_list_key(&self, list_name: &[u8], index: u32) -> Option<i64> {
        let list_name = list_name.to_vec();
        // get inner
        let inner = &mut self.inner.borrow_mut();
        // get key
        let key =
            match inner.hyper_counter.get(&list_name) {
                None => {
                    inner.hyper_counter.insert(list_name.clone(), 0);
                    0
                }
                Some(v) => v.clone()
            };

        // add key
        match inner.hyper_list.get_mut(&list_name) {
            None => {
                if index == 0 {
                    let mut new_vec = Vec::<i64>::new();
                    new_vec.insert(index as usize, key);
                    inner.hyper_list.insert(list_name.clone(), new_vec);
                } else {
                    panic!("invalid index when add");
                }
                inner.hyper_counter.insert(list_name, key + 1);
                Some(key)
            }
            Some(v) => {
                // check index
                if index as usize > v.len() {
                    return None;
                }
                // add key
                v.insert(index as usize, key);
                // update counter
                inner.hyper_counter.insert(list_name, key + 1);
                Some(key)
            }
        }
    }

    pub fn get_list_key(&self, list_name: &[u8], index: u32) -> Option<i64> {
        let list_name = list_name.to_vec();
        // get &mut list
        let hyper_list = &self.inner.borrow().hyper_list;
        let list = match hyper_list.get(&list_name) {
            None => {
                panic!("no list according to list_name when try to get key");
            }
            Some(v) => v
        };
        // check index
        if index as usize >= list.len() {
            None
        } else {
            Some(list[index as usize])
        }
    }

    pub fn remove_list_key(&self, list_name: &[u8], index: u32) -> Option<i64> {
        let list_name = list_name.to_vec();
        // get &mut list
        let hyper_list = &mut self.inner.borrow_mut().hyper_list;
        let list = match hyper_list.get_mut(&list_name) {
            None => {
                panic!("no list according to list_name when try to remove key");
            }
            Some(v) => v
        };
        // check index
        if index as usize >= list.len() {
            None
        } else {
            Some(list.remove(index as usize))
        }
    }

    pub fn write_list_keys_back(&mut self, old_list_name: &[u8], list_name: &[u8], size: u32) {
        let old_list_name = old_list_name.to_vec();
        let new_list_name = list_name.to_vec();
        let list_name = if old_list_name.len() > 0 {
            old_list_name.clone()
        } else {
            new_list_name.clone()
        };
        // check size
        let inner = &mut self.inner.borrow_mut();
        let is_contain = inner.hyper_list.contains_key(&list_name);
        match is_contain {
            false => {
                if size == 0 {
                    // create this list
                    inner.hyper_counter.insert(new_list_name.clone(), 0);
                    let new_list = Vec::<i64>::new();
                    inner.hyper_list.insert(new_list_name.clone(), new_list);
                } else {
                    // panic 
                    panic!("size is not 0 but no list when writing keys back");
                }
            }
            true => {
                // replace
                if old_list_name.len() > 0 {
                    let counter = inner.hyper_counter.remove(&old_list_name).unwrap();
                    let list = inner.hyper_list.remove(&old_list_name).unwrap();
                    inner.hyper_counter.insert(new_list_name.clone(), counter);
                    inner.hyper_list.insert(new_list_name.clone(), list);
                }
                let list = inner.hyper_list.get(&list_name).unwrap();
                if list.len() != size as usize {
                    panic!("size not equal when writing keys back");
                }
            }
        };
        inner.storage.insert(["@HyperList".as_bytes(), new_list_name.as_slice()].concat().to_vec(), vec![0; 28]);
        // (&new_list_name, "@HyperList".as_bytes(), vec![0;28].as_slice());
    }
}

thread_local!(static RUNTIME: RefCell<Runtime> = RefCell::new(Runtime::default()));

pub fn setup_runtime(runtime: Runtime) {
    RUNTIME.with(|r| *r.borrow_mut() = runtime);
}

mod env {
    use super::*;
    use std::cmp;
    use std::ptr;
    use std::slice;
    use std::u32;
    use fvm_std::types::Address;
    use fvm_std::prelude::H256;

    #[no_mangle]
    pub unsafe extern "C" fn fvm_block_time() -> u64 {
        RUNTIME.with(|r| r.borrow().block_time())
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_block_height() -> u64 {
        RUNTIME.with(|r| r.borrow().block_height())
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_self_address(dest: *mut u8) {
        RUNTIME.with(|r| {
            let addr = r.borrow().self_address();
            ptr::copy(addr.as_ptr(), dest, Address::len_bytes());
        })
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_caller_address(dest: *mut u8) {
        RUNTIME.with(|r| {
            let caller = r.borrow().caller_address();
            ptr::copy(caller.as_ptr(), dest, Address::len_bytes());
        })
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_origin_address(dest: *mut u8) {
        RUNTIME.with(|r| {
            let origin = r.borrow().origin_address();
            ptr::copy(origin.as_ptr(), dest, Address::len_bytes());
        })
    }


    #[no_mangle]
    pub unsafe extern "C" fn fvm_tx_hash(dest: *const u8) {
        RUNTIME.with(|r| {
            let tx_hash = r.borrow().tx_hash();
            ptr::copy(tx_hash.as_ptr(), dest as *mut u8, H256::len_bytes());
        })
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_storage_read(
        key_ptr: *const u8, key_len: u32, prefix_ptr: *const u8, prefix_len: u32,
        val: *mut u8, vlen: u32, offset: u32,
    ) -> u32 {
        let key = core::slice::from_raw_parts(key_ptr, key_len as usize);
        let prefix = core::slice::from_raw_parts(prefix_ptr, prefix_len as usize);
        let offset = offset as usize;

        let v = RUNTIME.with(|r| r.borrow().storage_read(key, prefix));
        match v {
            None => 0,
            Some(v) => {
                ptr::copy(
                    v.as_slice()[offset..].as_ptr(),
                    val,
                    cmp::min(vlen as usize, v.len() - offset),
                );
                v.len() as u32
            }
        }
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_storage_write(
        key_ptr: *const u8, key_len: u32, prefix_ptr: *const u8, prefix_len: u32, val: *const u8, vlen: u32,
    ) {
        let key = core::slice::from_raw_parts(key_ptr, key_len as usize);
        let prefix = core::slice::from_raw_parts(prefix_ptr, prefix_len as usize);
        let val = core::slice::from_raw_parts(val, vlen as usize);
        RUNTIME.with(|r| r.borrow().storage_write(key, prefix, val));
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_storage_delete(key_ptr: *const u8, key_len: u32, prefix_ptr: *const u8, prefix_len: u32) {
        let key = core::slice::from_raw_parts(key_ptr, key_len as usize);
        let prefix = core::slice::from_raw_parts(prefix_ptr, prefix_len as usize);
        RUNTIME.with(|r| r.borrow().storage_delete(key, prefix));
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_log(ptr: *const u8, len: u32) {
        let data = core::slice::from_raw_parts(ptr, len as usize);
        RUNTIME.with(|r| r.borrow().event(data));
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_sha256(data_ptr: *const u8, len: u32, val: *mut u8) {
        let data = core::slice::from_raw_parts(data_ptr, len as usize);
        RUNTIME.with(|r| {
            let mut hash = r.borrow_mut().sha256(data);
            ptr::copy(hash.as_mut_ptr(), val, 32);
        });
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_call_contract(
        addr: *const u8, input_ptr: *const u8, input_len: u32,
    ) -> u32 {
        let input = core::slice::from_raw_parts(input_ptr, input_len as usize);
        let addr = Address::from_slice(slice::from_raw_parts(addr, 20));
        RUNTIME.with(|r| r.borrow().call_contract(&addr, input))
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_cns_call_contract(
        cns: *const u8, cns_len: u32, input_ptr: *const u8, input_len: u32,
    ) -> u32 {
        let input = core::slice::from_raw_parts(input_ptr, input_len as usize);
        let addr = core::slice::from_raw_parts(cns, cns_len as usize);
        RUNTIME.with(|r| r.borrow().cns_call_contract(&addr, input))
    }


    #[no_mangle]
    pub unsafe extern "C" fn fvm_revert(ptr: *const u8, len: u32) -> ! {
        let temp = core::slice::from_raw_parts(ptr, len as usize);
        RUNTIME.with(|r| r.borrow().revert(std::str::from_utf8(temp).unwrap()))
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_call_return(dst: *mut u8) {
        let output = RUNTIME.with(|r| r.borrow().call_return());
        std::ptr::copy(output.as_ptr(), dst, output.len());
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_input_length() -> u32 {
        RUNTIME.with(|r| r.borrow().input_length())
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_fetch_input(_dst: *mut u8) {
        let output = RUNTIME.with(|r| r.borrow().input());
        ptr::copy(output.as_ptr(), _dst, output.len())
    }

    #[no_mangle]
    pub unsafe extern "C" fn fvm_return(ptr: *const u8, len: u32) {
        let ret = core::slice::from_raw_parts(ptr, len as usize);
        RUNTIME.with(|r| r.borrow().ret(ret))
    }

    #[no_mangle]
    pub unsafe fn fvm_debug(class_prt: *const u8, class_len: u32, ptr: *const u8, len: u32, level: u32) {
        let class_name = core::slice::from_raw_parts(class_prt, class_len as usize);
        let data = core::slice::from_raw_parts(ptr, len as usize);
        let l = match level {
            0 => LogLevel::CRITICAL,
            1 => LogLevel::ERROR,
            2 => LogLevel::WARNING,
            3 => LogLevel::NOTICE,
            4 => LogLevel::INFO,
            5 => LogLevel::DEBUG,
            _ => panic!("invalid log level type")
        };
        RUNTIME.with(|r| r.borrow_mut().debug(class_name, data, l))
    }

    #[no_mangle]
    pub unsafe fn fvm_add_list_key(key_ptr: *const u8, key_len: u32, index: u32) -> i64 {
        let list_name = core::slice::from_raw_parts(key_ptr, key_len as usize);
        let v = RUNTIME.with(|r| r.borrow_mut().add_list_key(list_name, index));
        match v {
            None => {
                -1
            }
            Some(v) => v
        }
    }

    #[no_mangle]
    pub unsafe fn fvm_get_list_key(key_ptr: *const u8, key_len: u32, index: u32) -> i64 {
        let list_name = core::slice::from_raw_parts(key_ptr, key_len as usize);
        let v = RUNTIME.with(|r| r.borrow().get_list_key(list_name, index));
        match v {
            None => -1,
            Some(v) => v
        }
    }

    #[no_mangle]
    pub fn fvm_remove_list_key(key_ptr: *const u8, key_len: u32, index: u32) -> i64 {
        unsafe {
            let list_name = core::slice::from_raw_parts(key_ptr, key_len as usize);
            let v = RUNTIME.with(|r| r.borrow().remove_list_key(list_name, index));
            match v {
                None => -1,
                Some(v) => v
            }
        }
    }

    #[no_mangle]
    pub fn fvm_write_list_keys(old_list_name_ptr: *const u8, old_list_name_len: u32, new_list_name_ptr: *const u8, new_list_name_len: u32, size: u32) {
        unsafe {
            let old_list_name = core::slice::from_raw_parts(old_list_name_ptr, old_list_name_len as usize);
            let list_name = core::slice::from_raw_parts(new_list_name_ptr, new_list_name_len as usize);
            RUNTIME.with(|r| r.borrow_mut().write_list_keys_back(old_list_name, list_name, size))
        }
    }
}