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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#![warn(rust_2018_idioms)]
|
|
|
|
#![deny(
|
|
|
|
dead_code,
|
|
|
|
nonstandard_style,
|
|
|
|
unused_imports,
|
|
|
|
unused_mut,
|
|
|
|
unused_variables,
|
|
|
|
unused_unsafe,
|
|
|
|
unreachable_patterns
|
|
|
|
)]
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
mod call_services;
|
2021-05-16 22:52:22 +03:00
|
|
|
pub mod executed_state;
|
|
|
|
|
2021-05-10 14:25:34 +03:00
|
|
|
pub use avm_server::ne_vec::NEVec;
|
|
|
|
pub use avm_server::AVMConfig;
|
|
|
|
pub use avm_server::AVMError;
|
|
|
|
pub use avm_server::CallServiceClosure;
|
|
|
|
pub use avm_server::IType;
|
|
|
|
pub use avm_server::IValue;
|
|
|
|
pub use avm_server::InterpreterOutcome;
|
|
|
|
pub use avm_server::ParticleParameters;
|
|
|
|
pub use avm_server::AVM;
|
2021-08-24 16:14:15 +03:00
|
|
|
pub use call_services::*;
|
2020-10-08 12:43:23 +03:00
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
pub use air::interpreter_data::*;
|
2021-05-16 22:52:22 +03:00
|
|
|
|
2020-10-08 12:43:23 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2021-05-16 22:52:22 +03:00
|
|
|
pub(self) type JValue = serde_json::Value;
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2021-05-10 14:25:34 +03:00
|
|
|
pub fn create_avm(call_service: CallServiceClosure, current_peer_id: impl Into<String>) -> AVM {
|
2020-10-30 20:29:05 +03:00
|
|
|
let tmp_dir = std::env::temp_dir();
|
|
|
|
|
2021-05-10 14:25:34 +03:00
|
|
|
let config = AVMConfig {
|
|
|
|
air_wasm_path: PathBuf::from("../target/wasm32-wasi/debug/air_interpreter_server.wasm"),
|
2020-12-28 00:12:11 +03:00
|
|
|
call_service,
|
2020-10-15 17:31:56 +03:00
|
|
|
current_peer_id: current_peer_id.into(),
|
2021-06-30 18:58:54 +03:00
|
|
|
vault_dir: tmp_dir.join("vault"),
|
2020-10-30 20:29:05 +03:00
|
|
|
particle_data_store: tmp_dir,
|
2021-06-30 18:58:54 +03:00
|
|
|
logging_mask: i32::MAX,
|
2020-10-08 12:43:23 +03:00
|
|
|
};
|
|
|
|
|
2021-05-10 14:25:34 +03:00
|
|
|
AVM::new(config).expect("vm should be created")
|
2020-10-08 12:43:23 +03:00
|
|
|
}
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! checked_call_vm {
|
|
|
|
($vm:expr, $init_peer_id:expr, $script:expr, $prev_data:expr, $data:expr) => {{
|
|
|
|
match $vm.call_with_prev_data($init_peer_id, $script, $prev_data, $data) {
|
|
|
|
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 {
|
2020-11-11 14:31:53 +03:00
|
|
|
($vm:expr, $init_peer_id:expr, $script:expr, $prev_data:expr, $data:expr) => {
|
|
|
|
match $vm.call_with_prev_data($init_peer_id, $script, $prev_data, $data) {
|
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
|
|
|
|
|
|
|
pub fn trace_from_result(result: &InterpreterOutcome) -> ExecutionTrace {
|
|
|
|
let data = data_from_result(result);
|
|
|
|
data.trace
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn data_from_result(result: &InterpreterOutcome) -> InterpreterData {
|
|
|
|
serde_json::from_slice(&result.data).expect("default serializer shouldn't fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn raw_data_from_trace(trace: ExecutionTrace) -> Vec<u8> {
|
|
|
|
let data = InterpreterData::from_execution_result(trace, <_>::default());
|
|
|
|
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: &InterpreterOutcome, 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!("]");
|
|
|
|
}
|