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
#[cfg(feature = "test_with_native_code")]
use crate::native_test_runner::NativeAirRunner as AirRunnerImpl;
#[cfg(not(feature = "test_with_native_code"))]
use crate::wasm_test_runner::WasmAirRunner as AirRunnerImpl;
use super::CallServiceClosure;
use avm_server::avm_runner::*;
use std::collections::HashMap;
use std::collections::HashSet;
pub trait AirRunner {
fn new(current_call_id: impl Into<String>) -> Self;
#[allow(clippy::too_many_arguments)]
fn call(
&mut self,
air: impl Into<String>,
prev_data: impl Into<Vec<u8>>,
data: impl Into<Vec<u8>>,
init_peer_id: impl Into<String>,
timestamp: u64,
ttl: u32,
override_current_peer_id: Option<String>,
call_results: avm_server::CallResults,
) -> Result<RawAVMOutcome, Box<dyn std::error::Error>>;
}
pub struct TestRunner<R = AirRunnerImpl> {
pub runner: R,
pub call_service: CallServiceClosure,
}
#[derive(Debug, Default, Clone)]
pub struct TestRunParameters {
pub init_peer_id: String,
pub timestamp: u64,
pub ttl: u32,
pub override_current_peer_id: Option<String>,
}
impl<R: AirRunner> TestRunner<R> {
pub fn call(
&mut self,
air: impl Into<String>,
prev_data: impl Into<Vec<u8>>,
data: impl Into<Vec<u8>>,
test_run_params: TestRunParameters,
) -> Result<RawAVMOutcome, String> {
let air = air.into();
let mut prev_data = prev_data.into();
let mut data = data.into();
let TestRunParameters {
init_peer_id,
timestamp,
ttl,
override_current_peer_id,
} = test_run_params;
let mut call_results = HashMap::new();
let mut next_peer_pks = HashSet::new();
loop {
let mut outcome: RawAVMOutcome = self
.runner
.call(
air.clone(),
prev_data,
data,
init_peer_id.clone(),
timestamp,
ttl,
override_current_peer_id.clone(),
call_results,
)
.map_err(|e| e.to_string())?;
next_peer_pks.extend(outcome.next_peer_pks);
if outcome.call_requests.is_empty() {
outcome.next_peer_pks = next_peer_pks.into_iter().collect::<Vec<_>>();
return Ok(outcome);
}
call_results = outcome
.call_requests
.into_iter()
.map(|(id, call_parameters)| {
let service_result = (self.call_service)(call_parameters);
(id, service_result)
})
.collect::<HashMap<_, _>>();
prev_data = outcome.data;
data = vec![];
}
}
}
pub fn create_avm(
call_service: CallServiceClosure,
current_peer_id: impl Into<String>,
) -> TestRunner {
let runner = AirRunnerImpl::new(current_peer_id);
TestRunner {
runner,
call_service,
}
}
impl TestRunParameters {
pub fn new(init_peer_id: impl Into<String>, timestamp: u64, ttl: u32) -> Self {
Self {
init_peer_id: init_peer_id.into(),
timestamp,
ttl,
override_current_peer_id: None,
}
}
pub fn from_init_peer_id(init_peer_id: impl Into<String>) -> Self {
Self {
init_peer_id: init_peer_id.into(),
timestamp: 0,
ttl: 0,
override_current_peer_id: None,
}
}
pub fn from_timestamp(timestamp: u64) -> Self {
Self {
init_peer_id: String::new(),
timestamp,
ttl: 0,
override_current_peer_id: None,
}
}
pub fn from_ttl(ttl: u32) -> Self {
Self {
init_peer_id: String::new(),
timestamp: 0,
ttl,
override_current_peer_id: None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::call_services::{set_variables_call_service, VariableOptionSource};
use avm_interface::CallRequestParams;
use fstrings::f;
use fstrings::format_args_f;
use serde_json::json;
#[test]
fn test_override_current_peer_id() {
let spell_id = "spell_id";
let host_peer_id = "host_peer_id";
let script = f!(r#"(call "{}" ("service" "func") [])"#, spell_id);
let variables = maplit::hashmap! {
"func".to_owned() => json!("success"),
};
let mut client = create_avm(
set_variables_call_service(variables, VariableOptionSource::FunctionName),
host_peer_id,
);
let current_result_1 = client
.runner
.call(&script, "", "", spell_id, 0, 0, None, HashMap::new())
.expect("call should be success");
let expected_current_call_requests = HashMap::new();
let expected_current_next_peers_pks = vec![spell_id.to_owned()];
assert_eq!(
current_result_1.call_requests,
expected_current_call_requests
);
assert_eq!(
current_result_1.next_peer_pks,
expected_current_next_peers_pks
);
let spell_result_1 = client
.runner
.call(
script,
"",
"",
spell_id,
0,
0,
Some(spell_id.to_owned()),
HashMap::new(),
)
.expect("call should be success");
let expected_spell_call_requests = maplit::hashmap! {
1 => CallRequestParams::new("service", "func", vec![], vec![]),
};
let expected_spell_next_peers_pks = Vec::<String>::new();
assert_eq!(spell_result_1.call_requests, expected_spell_call_requests);
assert_eq!(spell_result_1.next_peer_pks, expected_spell_next_peers_pks);
}
}