-
Notifications
You must be signed in to change notification settings - Fork 206
/
engine.rs
142 lines (124 loc) Β· 4.44 KB
/
engine.rs
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
use log::*;
use wasmi::{ModuleRef, RuntimeValue};
use super::contract::ContractInstance;
use crate::wasm::errors::{wasmi_error_to_enclave_error, WasmEngineError};
use enclave_ffi_types::EnclaveError;
pub struct Engine {
contract_instance: ContractInstance,
module: ModuleRef,
}
impl Engine {
pub fn new(contract_instance: ContractInstance, module: ModuleRef) -> Self {
Self {
contract_instance,
module,
}
}
pub fn gas_used(&self) -> u64 {
self.contract_instance.gas_used
}
pub fn write_to_memory(&mut self, buffer: &[u8]) -> Result<u32, WasmEngineError> {
self.contract_instance.write_to_memory(buffer)
}
pub fn extract_vector(&self, vec_ptr_ptr: u32) -> Result<Vec<u8>, WasmEngineError> {
self.contract_instance.extract_vector(vec_ptr_ptr)
}
pub fn init(&mut self, env_ptr: u32, msg_ptr: u32) -> Result<u32, EnclaveError> {
info!("Invoking init() in wasm");
match self
.module
.invoke_export(
"init",
&[
RuntimeValue::I32(env_ptr as i32),
RuntimeValue::I32(msg_ptr as i32),
],
&mut self.contract_instance,
)
.map_err(wasmi_error_to_enclave_error)?
{
Some(RuntimeValue::I32(offset)) => Ok(offset as u32),
other => {
warn!("init method returned value which wasn't u32: {:?}", other);
Err(EnclaveError::FailedFunctionCall)
}
}
// Itzik: leaving this here as an example in case we will want to do something like this in the future
// if result.is_ok() {
// write_encrypted_key(
// b"key",
// contract_key,
// &self.contract_instance.context,
// &self.contract_instance.contract_key,
// )
// .map_err(|_| {
// error!("Failed to write contract key to database");
// EnclaveError::InternalError
// })?;
// }
//result
}
pub fn handle(&mut self, env_ptr: u32, msg_ptr: u32) -> Result<u32, EnclaveError> {
info!("Invoking handle() in wasm");
// Itzik: leaving this here as an example in case we will want to do something like this in the future
// let stored_address = read_encrypted_key(
// b"key",
// &self.contract_instance.context,
// &self.contract_instance.contract_key,
// )
// .map_err(|_| {
// error!("WTF wrong contract key are you crazy???");
// EnclaveError::InternalError
// })?;
//
// match stored_address.0 {
// Some(addr) => {
// if addr != contract_key.to_vec() {
// error!("WTF wrong contract key are you crazy???");
// return Err(EnclaveError::FailedUnseal);
// }
// Ok(())
// }
// None => {
// error!("WTF no contract address found you must be trippin' dawg");
// Err(EnclaveError::InternalError)
// }
// }?;
match self
.module
.invoke_export(
"handle",
&[
RuntimeValue::I32(env_ptr as i32),
RuntimeValue::I32(msg_ptr as i32),
],
&mut self.contract_instance,
)
.map_err(wasmi_error_to_enclave_error)?
{
Some(RuntimeValue::I32(offset)) => Ok(offset as u32),
other => {
warn!("handle method returned value which wasn't u32: {:?}", other);
Err(EnclaveError::FailedFunctionCall)
}
}
}
pub fn query(&mut self, msg_ptr: u32) -> Result<u32, EnclaveError> {
info!("Invoking query() in wasm");
match self
.module
.invoke_export(
"query",
&[RuntimeValue::I32(msg_ptr as i32)],
&mut self.contract_instance,
)
.map_err(wasmi_error_to_enclave_error)?
{
Some(RuntimeValue::I32(offset)) => Ok(offset as u32),
other => {
warn!("query method returned value which wasn't u32: {:?}", other);
Err(EnclaveError::FailedFunctionCall)
}
}
}
}