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
use crate::{contract::GasMeter, transfer::TransferEntry, Sandbox};
use ceres_executor::{Error, Executor, Result, ReturnData};
use ceres_std::Vec;
#[derive(Default)]
pub struct InstantiateEntry {
pub code_hash: [u8; 32],
pub endowment: u64,
pub data: Vec<u8>,
pub gas_left: u64,
pub salt: Vec<u8>,
}
impl Sandbox {
pub fn invoke(
&mut self,
code_hash: [u8; 32],
method: &str,
data: Vec<u8>,
) -> Result<([u8; 32], ReturnData)> {
self.input = Some(data);
self.prepare(code_hash)?;
let mut executor = Executor::new(code_hash, self)?;
let ret = executor.invoke(method, &[], self)?;
let mut cache_mut = self.cache.borrow_mut();
cache_mut.top().ok_or(Error::StateNotFound)?;
cache_mut.flush().ok_or(Error::FlushDataFailed)?;
drop(cache_mut);
self.cache
.borrow_mut()
.flush()
.ok_or(Error::FlushDataFailed)?;
Ok((code_hash, ret.data))
}
pub fn instantiate(
&mut self,
code_hash: [u8; 32],
gas_meter: &mut GasMeter,
data: Vec<u8>,
salt: &[u8],
) -> Result<([u8; 32], ReturnData)> {
self.ext.instantiates.push(InstantiateEntry {
code_hash,
endowment: 3,
data: data.to_vec(),
gas_left: gas_meter.gas_left,
salt: salt.to_vec(),
});
self.invoke(code_hash, "deploy", data)
}
pub fn call(
&mut self,
code_hash: [u8; 32],
data: Vec<u8>,
) -> Result<ReturnData> {
self.ext.transfers.push(TransferEntry {
to: code_hash,
value: 0,
data: data.clone(),
});
self.invoke(code_hash, "call", data).map(|v| v.1)
}
}