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
use super::CallResults;
use crate::raw_outcome::RawAVMOutcome;
use crate::RunnerError;
use crate::RunnerResult;
use air_interpreter_interface::InterpreterOutcome;
use fluence_faas::FaaSConfig;
use fluence_faas::FluenceFaaS;
use fluence_faas::IValue;
use fluence_faas::ModuleDescriptor;
use std::path::PathBuf;
pub struct AVMRunner {
faas: FluenceFaaS,
current_peer_id: String,
wasm_filename: String,
}
impl AVMRunner {
pub fn new(
air_wasm_path: PathBuf,
current_peer_id: impl Into<String>,
logging_mask: i32,
) -> RunnerResult<Self> {
let (wasm_dir, wasm_filename) = split_dirname(air_wasm_path)?;
let faas_config = make_faas_config(wasm_dir, &wasm_filename, logging_mask);
let faas = FluenceFaaS::with_raw_config(faas_config)?;
let current_peer_id = current_peer_id.into();
let avm = Self {
faas,
current_peer_id,
wasm_filename,
};
Ok(avm)
}
pub fn call(
&mut self,
air: impl Into<String>,
prev_data: impl Into<Vec<u8>>,
data: impl Into<Vec<u8>>,
init_user_id: impl Into<String>,
call_results: CallResults,
) -> RunnerResult<RawAVMOutcome> {
let init_user_id = init_user_id.into();
let args = prepare_args(
air,
prev_data,
data,
init_user_id,
self.current_peer_id.clone(),
call_results,
);
let result =
self.faas
.call_with_ivalues(&self.wasm_filename, "invoke", &args, <_>::default())?;
let result = try_as_one_value_vec(result)?;
let outcome = InterpreterOutcome::from_ivalue(result)
.map_err(RunnerError::InterpreterResultDeError)?;
let outcome = RawAVMOutcome::from_interpreter_outcome(outcome)?;
Ok(outcome)
}
}
fn prepare_args(
air: impl Into<String>,
prev_data: impl Into<Vec<u8>>,
data: impl Into<Vec<u8>>,
init_peer_id: impl Into<String>,
current_peer_id: String,
call_results: CallResults,
) -> Vec<IValue> {
use fluence_faas::ne_vec::NEVec;
let run_parameters = vec![
IValue::String(init_peer_id.into()),
IValue::String(current_peer_id),
];
let run_parameters = NEVec::new(run_parameters).unwrap();
let call_results = crate::interface::into_raw_result(call_results);
let call_results =
serde_json::to_vec(&call_results).expect("the default serializer shouldn't fail");
vec![
IValue::String(air.into()),
IValue::ByteArray(prev_data.into()),
IValue::ByteArray(data.into()),
IValue::Record(run_parameters),
IValue::ByteArray(call_results),
]
}
fn split_dirname(path: PathBuf) -> RunnerResult<(PathBuf, String)> {
use RunnerError::InvalidAIRPath;
let metadata = path.metadata().map_err(|err| InvalidAIRPath {
invalid_path: path.clone(),
reason: "failed to get file's metadata (doesn't exist or invalid permissions)",
io_error: Some(err),
})?;
if !metadata.is_file() {
return Err(InvalidAIRPath {
invalid_path: path,
reason: "is not a file",
io_error: None,
});
}
let file_name = path
.file_name()
.expect("checked to be a file, file name must be defined");
let file_name = file_name.to_string_lossy().into_owned();
let mut path = path;
path.pop();
Ok((path, file_name))
}
fn make_faas_config(air_wasm_dir: PathBuf, air_wasm_file: &str, logging_mask: i32) -> FaaSConfig {
let air_module_config = fluence_faas::FaaSModuleConfig {
mem_pages_count: None,
logger_enabled: true,
host_imports: <_>::default(),
wasi: None,
logging_mask,
};
FaaSConfig {
modules_dir: Some(air_wasm_dir),
modules_config: vec![ModuleDescriptor {
file_name: String::from(air_wasm_file),
import_name: String::from(air_wasm_file),
config: air_module_config,
}],
default_modules_config: None,
}
}
fn try_as_one_value_vec(mut ivalues: Vec<IValue>) -> RunnerResult<IValue> {
use RunnerError::IncorrectInterpreterResult;
if ivalues.len() != 1 {
return Err(IncorrectInterpreterResult(ivalues));
}
Ok(ivalues.remove(0))
}