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
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms)]
#![deny(
dead_code,
nonstandard_style,
unused_imports,
unused_mut,
unused_variables,
unused_unsafe,
unreachable_patterns
)]
pub mod call_services;
pub mod executed_state;
pub mod test_runner;
#[cfg(feature = "test_with_native_code")]
mod native_test_runner;
#[cfg(not(feature = "test_with_native_code"))]
mod wasm_test_runner;
pub use air::interpreter_data::*;
use air::ExecutionCidState;
pub use avm_interface::raw_outcome::*;
pub use avm_server::*;
pub mod prelude {
pub use super::*;
pub use call_services::*;
pub use executed_state::*;
pub use test_runner::*;
pub use air::interpreter_data::*;
pub use avm_server::*;
pub use fstrings::f;
pub use fstrings::format_args_f;
pub use serde_json::json;
}
pub type CallServiceClosure = Box<dyn Fn(CallRequestParams) -> CallServiceResult + 'static>;
pub type JValue = serde_json::Value;
#[macro_export]
macro_rules! checked_call_vm {
($vm:expr, $test_run_parameters:expr, $script:expr, $prev_data:expr, $data:expr) => {{
match $vm.call($script, $prev_data, $data, $test_run_parameters) {
Ok(v) if v.ret_code != 0 => {
panic!("VM returns a error: {} {}", v.ret_code, v.error_message)
}
Ok(v) => v,
Err(err) => panic!("VM call failed: {}", err),
}
}};
}
#[macro_export]
macro_rules! call_vm {
($vm:expr, $test_run_parameters:expr, $script:expr, $prev_data:expr, $data:expr) => {
match $vm.call($script, $prev_data, $data, $test_run_parameters) {
Ok(v) => v,
Err(err) => panic!("VM call failed: {}", err),
}
};
}
pub fn trace_from_result(result: &RawAVMOutcome) -> ExecutionTrace {
let data = data_from_result(result);
data.trace
}
pub fn data_from_result(result: &RawAVMOutcome) -> InterpreterData {
serde_json::from_slice(&result.data).expect("default serializer shouldn't fail")
}
pub fn raw_data_from_trace(
trace: impl Into<ExecutionTrace>,
cid_state: ExecutionCidState,
) -> Vec<u8> {
let data = InterpreterData::from_execution_result(
trace.into(),
<_>::default(),
<_>::default(),
cid_state.into(),
0,
semver::Version::new(1, 1, 1),
);
serde_json::to_vec(&data).expect("default serializer shouldn't fail")
}
pub fn raw_data_from_trace_with_canon(
trace: impl Into<ExecutionTrace>,
cid_state: ExecutionCidState,
) -> Vec<u8> {
let data = InterpreterData::from_execution_result(
trace.into(),
<_>::default(),
<_>::default(),
CidInfo {
value_store: cid_state.value_tracker.into(),
tetraplet_store: cid_state.tetraplet_tracker.into(),
canon_store: cid_state.canon_tracker.into(),
service_result_store: cid_state.service_result_agg_tracker.into(),
},
0,
semver::Version::new(1, 1, 1),
);
serde_json::to_vec(&data).expect("default serializer shouldn't fail")
}
#[macro_export]
macro_rules! assert_next_pks {
($expected:expr, $actual:expr) => {
let expected: std::collections::HashSet<_> =
$expected.into_iter().map(|s| s.as_str()).collect();
let actual: std::collections::HashSet<_> = $actual.into_iter().map(|s| *s).collect();
assert_eq!(expected, actual)
};
}
pub fn print_trace(result: &RawAVMOutcome, trace_name: &str) {
let trace = trace_from_result(result);
println!("trace {} (states_count: {}): [", trace_name, trace.len());
for (id, state) in trace.iter().enumerate() {
println!(" {id}: {state}");
}
println!("]");
}
#[macro_export]
macro_rules! rc {
($expr:expr) => {
std::rc::Rc::new($expr)
};
}
use air::ToErrorCode;
use air_interpreter_interface::INTERPRETER_SUCCESS;
pub fn is_interpreter_succeded(result: &RawAVMOutcome) -> bool {
result.ret_code == INTERPRETER_SUCCESS
}
pub fn check_error(result: &RawAVMOutcome, error: impl ToErrorCode + ToString) -> bool {
result.ret_code == error.to_error_code() && result.error_message == error.to_string()
}