2021-10-04 10:58:00 +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.
|
|
|
|
*/
|
|
|
|
|
2023-06-23 03:12:37 +07:00
|
|
|
use crate::key_utils::derive_dummy_keypair;
|
2022-06-10 08:28:40 +03:00
|
|
|
#[cfg(feature = "test_with_native_code")]
|
2023-06-23 19:28:28 +07:00
|
|
|
pub use crate::native_test_runner::NativeAirRunner as DefaultAirRunner;
|
2022-06-10 08:28:40 +03:00
|
|
|
#[cfg(not(feature = "test_with_native_code"))]
|
2023-06-23 19:28:28 +07:00
|
|
|
pub use crate::wasm_test_runner::WasmAirRunner as DefaultAirRunner;
|
|
|
|
|
|
|
|
pub use crate::native_test_runner::NativeAirRunner;
|
|
|
|
pub use crate::wasm_test_runner::ReleaseWasmAirRunner;
|
|
|
|
pub use crate::wasm_test_runner::WasmAirRunner;
|
2022-06-10 08:28:40 +03:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
use super::CallServiceClosure;
|
2023-06-23 03:12:37 +07:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
use avm_server::avm_runner::*;
|
2023-06-23 03:12:37 +07:00
|
|
|
use fluence_keypair::KeyPair;
|
2021-10-04 10:58:00 +03:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::collections::HashSet;
|
|
|
|
|
2022-06-10 08:28:40 +03:00
|
|
|
pub trait AirRunner {
|
|
|
|
fn new(current_call_id: impl Into<String>) -> Self;
|
2021-12-14 14:01:57 +03:00
|
|
|
|
2022-08-10 12:27:06 +03:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2022-06-10 08:28:40 +03:00
|
|
|
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,
|
2022-11-25 10:59:09 +03:00
|
|
|
override_current_peer_id: Option<String>,
|
2022-06-10 08:28:40 +03:00
|
|
|
call_results: avm_server::CallResults,
|
2023-06-23 03:12:37 +07:00
|
|
|
key_pair: &KeyPair,
|
|
|
|
particle_id: String,
|
2022-06-10 08:28:40 +03:00
|
|
|
) -> Result<RawAVMOutcome, Box<dyn std::error::Error>>;
|
2023-10-13 23:19:02 +04:00
|
|
|
|
|
|
|
fn get_current_peer_id(&self) -> &str;
|
2021-10-04 10:58:00 +03:00
|
|
|
}
|
|
|
|
|
2023-06-23 19:28:28 +07:00
|
|
|
pub struct TestRunner<R = DefaultAirRunner> {
|
2022-06-10 08:28:40 +03:00
|
|
|
pub runner: R,
|
2023-06-23 03:12:37 +07:00
|
|
|
call_service: CallServiceClosure,
|
2023-10-13 23:19:02 +04:00
|
|
|
pub keypair: KeyPair,
|
2022-05-12 19:29:08 +07:00
|
|
|
}
|
|
|
|
|
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-11-25 10:59:09 +03:00
|
|
|
pub override_current_peer_id: Option<String>,
|
2023-06-23 03:12:37 +07:00
|
|
|
pub particle_id: String,
|
2022-04-20 23:05:37 +03:00
|
|
|
}
|
|
|
|
|
2022-06-10 08:28:40 +03:00
|
|
|
impl<R: AirRunner> TestRunner<R> {
|
2021-10-04 10:58:00 +03:00
|
|
|
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,
|
2021-10-04 10:58:00 +03:00
|
|
|
) -> 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-11-25 10:59:09 +03:00
|
|
|
override_current_peer_id,
|
2023-06-23 03:12:37 +07:00
|
|
|
particle_id,
|
2022-04-20 23:05:37 +03:00
|
|
|
} = test_run_params;
|
2021-10-04 10:58:00 +03:00
|
|
|
|
2022-02-25 23:55:40 +03:00
|
|
|
let mut call_results = HashMap::new();
|
2021-10-04 10:58:00 +03:00
|
|
|
let mut next_peer_pks = HashSet::new();
|
|
|
|
|
|
|
|
loop {
|
2022-06-10 08:28:40 +03:00
|
|
|
let mut outcome: RawAVMOutcome = self
|
2021-10-04 10:58:00 +03:00
|
|
|
.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,
|
2022-11-25 10:59:09 +03:00
|
|
|
override_current_peer_id.clone(),
|
2021-10-04 10:58:00 +03:00
|
|
|
call_results,
|
2023-06-23 03:12:37 +07:00
|
|
|
&self.keypair,
|
|
|
|
particle_id.clone(),
|
2021-10-04 10:58:00 +03:00
|
|
|
)
|
|
|
|
.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![];
|
|
|
|
}
|
|
|
|
}
|
2023-06-23 03:12:37 +07:00
|
|
|
|
|
|
|
#[allow(clippy::too_many_arguments)]
|
|
|
|
pub fn call_single(
|
|
|
|
&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,
|
|
|
|
particle_id: impl Into<String>,
|
|
|
|
) -> Result<RawAVMOutcome, Box<dyn std::error::Error>> {
|
|
|
|
self.runner.call(
|
|
|
|
air,
|
|
|
|
prev_data,
|
|
|
|
data,
|
|
|
|
init_peer_id,
|
|
|
|
timestamp,
|
|
|
|
ttl,
|
|
|
|
override_current_peer_id,
|
|
|
|
call_results,
|
|
|
|
&self.keypair,
|
|
|
|
particle_id.into(),
|
|
|
|
)
|
|
|
|
}
|
2021-10-04 10:58:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_avm(
|
|
|
|
call_service: CallServiceClosure,
|
|
|
|
current_peer_id: impl Into<String>,
|
|
|
|
) -> TestRunner {
|
2023-06-23 19:28:28 +07:00
|
|
|
create_custom_avm(call_service, current_peer_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_custom_avm<R: AirRunner>(
|
|
|
|
call_service: CallServiceClosure,
|
|
|
|
current_peer_id: impl Into<String>,
|
|
|
|
) -> TestRunner<R> {
|
2023-06-23 03:12:37 +07:00
|
|
|
let current_peer_id = current_peer_id.into();
|
|
|
|
|
|
|
|
let (keypair, _) = derive_dummy_keypair(¤t_peer_id);
|
|
|
|
|
2023-06-23 19:28:28 +07:00
|
|
|
let runner = R::new(current_peer_id);
|
2021-10-04 10:58:00 +03:00
|
|
|
|
|
|
|
TestRunner {
|
|
|
|
runner,
|
|
|
|
call_service,
|
2023-11-02 18:43:35 +04:00
|
|
|
keypair: keypair.into_inner(),
|
2021-10-04 10:58:00 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-20 23:05:37 +03:00
|
|
|
|
2023-10-13 23:19:02 +04:00
|
|
|
pub fn create_avm_with_key<R: AirRunner>(
|
2023-11-02 18:43:35 +04:00
|
|
|
keypair: impl Into<KeyPair>,
|
2023-10-13 23:19:02 +04:00
|
|
|
call_service: CallServiceClosure,
|
|
|
|
) -> TestRunner<R> {
|
2023-11-02 18:43:35 +04:00
|
|
|
let keypair = keypair.into();
|
2023-10-13 23:19:02 +04:00
|
|
|
let current_peer_id = keypair.public().to_peer_id().to_string();
|
|
|
|
let runner = R::new(current_peer_id);
|
|
|
|
|
|
|
|
TestRunner {
|
|
|
|
runner,
|
|
|
|
call_service,
|
|
|
|
keypair,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 23:05:37 +03:00
|
|
|
impl TestRunParameters {
|
2023-06-23 03:12:37 +07:00
|
|
|
pub fn new(
|
|
|
|
init_peer_id: impl Into<String>,
|
|
|
|
timestamp: u64,
|
|
|
|
ttl: u32,
|
|
|
|
particle_id: impl Into<String>,
|
|
|
|
) -> 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-11-25 10:59:09 +03:00
|
|
|
override_current_peer_id: None,
|
2023-06-23 03:12:37 +07:00
|
|
|
particle_id: particle_id.into(),
|
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(),
|
2023-06-23 03:12:37 +07:00
|
|
|
..<_>::default()
|
2022-04-20 23:05:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_timestamp(timestamp: u64) -> Self {
|
|
|
|
Self {
|
|
|
|
timestamp,
|
2023-06-23 03:12:37 +07:00
|
|
|
..<_>::default()
|
2022-04-21 11:44:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_ttl(ttl: u32) -> Self {
|
|
|
|
Self {
|
|
|
|
ttl,
|
2023-06-23 03:12:37 +07:00
|
|
|
..<_>::default()
|
2022-04-20 23:05:37 +03:00
|
|
|
}
|
|
|
|
}
|
2023-10-13 23:19:02 +04:00
|
|
|
|
|
|
|
pub fn with_particle_id(mut self, particle_id: impl Into<String>) -> Self {
|
|
|
|
self.particle_id = particle_id.into();
|
|
|
|
self
|
|
|
|
}
|
2022-04-20 23:05:37 +03:00
|
|
|
}
|
2022-11-25 10:59:09 +03:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::call_services::{set_variables_call_service, VariableOptionSource};
|
|
|
|
|
|
|
|
use avm_interface::CallRequestParams;
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_override_current_peer_id() {
|
|
|
|
let spell_id = "spell_id";
|
|
|
|
let host_peer_id = "host_peer_id";
|
2023-06-23 19:28:28 +07:00
|
|
|
let script = format!(r#"(call "{spell_id}" ("service" "func") [])"#);
|
2022-11-25 10:59:09 +03:00
|
|
|
|
|
|
|
let variables = maplit::hashmap! {
|
|
|
|
"func".to_owned() => json!("success"),
|
|
|
|
};
|
|
|
|
|
2023-06-23 03:12:37 +07:00
|
|
|
let key_format = fluence_keypair::KeyFormat::Ed25519;
|
|
|
|
let keypair = KeyPair::generate(key_format);
|
|
|
|
let keypair2 = KeyPair::generate(key_format);
|
|
|
|
|
2023-06-23 19:28:28 +07:00
|
|
|
let mut client = create_custom_avm::<NativeAirRunner>(
|
2022-11-25 10:59:09 +03:00
|
|
|
set_variables_call_service(variables, VariableOptionSource::FunctionName),
|
|
|
|
host_peer_id,
|
|
|
|
);
|
|
|
|
|
|
|
|
let current_result_1 = client
|
|
|
|
.runner
|
2023-06-23 03:12:37 +07:00
|
|
|
.call(
|
|
|
|
&script,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
spell_id,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
None,
|
|
|
|
HashMap::new(),
|
|
|
|
&keypair,
|
|
|
|
"".to_owned(),
|
|
|
|
)
|
2022-11-25 10:59:09 +03:00
|
|
|
.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(),
|
2023-06-23 03:12:37 +07:00
|
|
|
&keypair2,
|
|
|
|
"".to_owned(),
|
2022-11-25 10:59:09 +03:00
|
|
|
)
|
|
|
|
.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);
|
|
|
|
}
|
|
|
|
}
|