167 lines
4.3 KiB
Rust
Raw Normal View History

/*
* 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.
*/
use super::CallServiceClosure;
use avm_server::avm_runner::*;
use once_cell::sync::OnceCell;
use std::collections::HashMap;
use std::collections::HashSet;
use std::path::PathBuf;
2021-12-14 14:01:57 +03:00
// 10 Mb
const AVM_MAX_HEAP_SIZE: u64 = 10 * 1024 * 1024;
const AIR_WASM_PATH: &str = "../target/wasm32-wasi/debug/air_interpreter_server.wasm";
2021-12-14 14:01:57 +03:00
pub struct TestRunner {
pub runner: object_pool::Reusable<'static, AVMRunner>,
2021-11-29 18:35:11 +03:00
pub call_service: CallServiceClosure,
}
fn make_pooled_avm_runner() -> AVMRunner {
let fake_current_peer_id = "";
let logging_mask = i32::MAX;
AVMRunner::new(
PathBuf::from(AIR_WASM_PATH),
fake_current_peer_id,
Some(AVM_MAX_HEAP_SIZE),
logging_mask,
)
.expect("vm should be created")
}
2022-04-20 23:05:37 +03:00
#[derive(Debug, Default, Clone)]
pub struct TestRunParameters {
pub init_peer_id: String,
pub timestamp: u64,
2022-04-21 11:44:18 +03:00
pub ttl: u32,
2022-04-20 23:05:37 +03:00
}
impl TestRunner {
pub fn call(
&mut self,
air: impl Into<String>,
prev_data: impl Into<Vec<u8>>,
data: impl Into<Vec<u8>>,
2022-04-20 23:05:37 +03:00
test_run_params: TestRunParameters,
) -> Result<RawAVMOutcome, String> {
let air = air.into();
let mut prev_data = prev_data.into();
let mut data = data.into();
2022-04-20 23:05:37 +03:00
let TestRunParameters {
init_peer_id,
timestamp,
2022-04-21 11:44:18 +03:00
ttl,
2022-04-20 23:05:37 +03:00
} = test_run_params;
2022-02-25 23:55:40 +03:00
let mut call_results = HashMap::new();
let mut next_peer_pks = HashSet::new();
loop {
let mut outcome = self
.runner
.call(
air.clone(),
prev_data,
data,
2022-04-20 23:05:37 +03:00
init_peer_id.clone(),
timestamp,
2022-04-21 11:44:18 +03:00
ttl,
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 {
static POOL_CELL: OnceCell<object_pool::Pool<AVMRunner>> = OnceCell::new();
let pool = POOL_CELL.get_or_init(|| {
object_pool::Pool::new(
// we create an empty pool and let it fill on demand
0,
|| unreachable!(),
)
});
let mut runner = pool.pull(make_pooled_avm_runner);
runner.set_peer_id(current_peer_id);
TestRunner {
runner,
call_service,
}
}
2022-04-20 23:05:37 +03:00
impl TestRunParameters {
2022-04-21 11:44:18 +03:00
pub fn new(init_peer_id: impl Into<String>, timestamp: u64, ttl: u32) -> Self {
2022-04-20 23:05:37 +03:00
Self {
init_peer_id: init_peer_id.into(),
timestamp,
2022-04-21 11:44:18 +03:00
ttl,
2022-04-20 23:05:37 +03:00
}
}
pub fn from_init_peer_id(init_peer_id: impl Into<String>) -> Self {
Self {
init_peer_id: init_peer_id.into(),
timestamp: 0,
2022-04-21 11:44:18 +03:00
ttl: 0,
2022-04-20 23:05:37 +03:00
}
}
pub fn from_timestamp(timestamp: u64) -> Self {
Self {
init_peer_id: String::new(),
timestamp,
2022-04-21 11:44:18 +03:00
ttl: 0,
}
}
pub fn from_ttl(ttl: u32) -> Self {
Self {
init_peer_id: String::new(),
timestamp: 0,
ttl,
2022-04-20 23:05:37 +03:00
}
}
}