2020-10-08 12:43:23 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Fluence Labs Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-09-05 20:03:30 +03:00
|
|
|
#![forbid(unsafe_code)]
|
2020-10-08 12:43:23 +03:00
|
|
|
#![warn(rust_2018_idioms)]
|
|
|
|
#![deny(
|
|
|
|
dead_code,
|
|
|
|
nonstandard_style,
|
|
|
|
unused_imports,
|
|
|
|
unused_mut,
|
|
|
|
unused_variables,
|
|
|
|
unused_unsafe,
|
|
|
|
unreachable_patterns
|
|
|
|
)]
|
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
pub mod call_services;
|
2021-05-16 22:52:22 +03:00
|
|
|
pub mod executed_state;
|
2021-10-04 10:58:00 +03:00
|
|
|
pub mod test_runner;
|
2020-10-08 12:43:23 +03:00
|
|
|
|
2022-06-10 08:28:40 +03:00
|
|
|
#[cfg(feature = "test_with_native_code")]
|
|
|
|
mod native_test_runner;
|
|
|
|
#[cfg(not(feature = "test_with_native_code"))]
|
|
|
|
mod wasm_test_runner;
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
pub use air::interpreter_data::*;
|
2022-08-19 12:29:21 +03:00
|
|
|
pub use avm_interface::raw_outcome::*;
|
2021-10-04 10:58:00 +03:00
|
|
|
pub use avm_server::*;
|
2021-05-16 22:52:22 +03:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
pub mod prelude {
|
|
|
|
pub use super::*;
|
|
|
|
pub use call_services::*;
|
|
|
|
pub use executed_state::*;
|
|
|
|
pub use test_runner::*;
|
2020-10-08 12:43:23 +03:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
pub use air::interpreter_data::*;
|
|
|
|
pub use avm_server::*;
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2022-03-10 16:06:43 +03:00
|
|
|
pub use fstrings::f;
|
|
|
|
pub use fstrings::format_args_f;
|
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
pub use serde_json::json;
|
|
|
|
}
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
pub type CallServiceClosure = Box<dyn Fn(CallRequestParams) -> CallServiceResult + 'static>;
|
2020-10-08 12:43:23 +03:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
pub type JValue = serde_json::Value;
|
2020-10-08 12:43:23 +03:00
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! checked_call_vm {
|
2022-04-20 23:05:37 +03:00
|
|
|
($vm:expr, $test_run_parameters:expr, $script:expr, $prev_data:expr, $data:expr) => {{
|
|
|
|
match $vm.call($script, $prev_data, $data, $test_run_parameters) {
|
2021-08-24 16:14:15 +03:00
|
|
|
Ok(v) if v.ret_code != 0 => {
|
|
|
|
panic!("VM returns a error: {} {}", v.ret_code, v.error_message)
|
2020-10-30 20:29:05 +03:00
|
|
|
}
|
2021-08-24 16:14:15 +03:00
|
|
|
Ok(v) => v,
|
|
|
|
Err(err) => panic!("VM call failed: {}", err),
|
2021-02-16 20:04:00 +03:00
|
|
|
}
|
2021-08-24 16:14:15 +03:00
|
|
|
}};
|
2021-02-16 20:04:00 +03:00
|
|
|
}
|
|
|
|
|
2020-10-30 20:29:05 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! call_vm {
|
2022-04-20 23:05:37 +03:00
|
|
|
($vm:expr, $test_run_parameters:expr, $script:expr, $prev_data:expr, $data:expr) => {
|
|
|
|
match $vm.call($script, $prev_data, $data, $test_run_parameters) {
|
2020-11-03 17:43:58 +03:00
|
|
|
Ok(v) => v,
|
|
|
|
Err(err) => panic!("VM call failed: {}", err),
|
|
|
|
}
|
2020-10-30 20:29:05 +03:00
|
|
|
};
|
|
|
|
}
|
2021-08-24 16:14:15 +03:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
pub fn trace_from_result(result: &RawAVMOutcome) -> ExecutionTrace {
|
2021-08-24 16:14:15 +03:00
|
|
|
let data = data_from_result(result);
|
|
|
|
data.trace
|
|
|
|
}
|
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
pub fn data_from_result(result: &RawAVMOutcome) -> InterpreterData {
|
2021-08-24 16:14:15 +03:00
|
|
|
serde_json::from_slice(&result.data).expect("default serializer shouldn't fail")
|
|
|
|
}
|
|
|
|
|
2022-06-10 08:29:56 +03:00
|
|
|
pub fn raw_data_from_trace(trace: impl Into<ExecutionTrace>) -> Vec<u8> {
|
|
|
|
let data =
|
|
|
|
InterpreterData::from_execution_result(trace.into(), <_>::default(), <_>::default(), 0);
|
2021-08-24 16:14:15 +03:00
|
|
|
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)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
pub fn print_trace(result: &RawAVMOutcome, trace_name: &str) {
|
2021-08-24 16:14:15 +03:00
|
|
|
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!("]");
|
|
|
|
}
|
2021-12-21 11:37:35 +03:00
|
|
|
|
|
|
|
#[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 {
|
2022-10-09 12:56:12 +03:00
|
|
|
println!(
|
|
|
|
"{} == {} && {} == {}",
|
|
|
|
result.ret_code,
|
|
|
|
error.to_error_code(),
|
|
|
|
result.error_message,
|
|
|
|
error.to_string()
|
|
|
|
);
|
2021-12-21 11:37:35 +03:00
|
|
|
result.ret_code == error.to_error_code() && result.error_message == error.to_string()
|
|
|
|
}
|