2021-05-10 14:25:34 +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.
|
|
|
|
*/
|
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
use super::avm_runner::AVMRunner;
|
|
|
|
use super::AVMDataStore;
|
|
|
|
use super::AVMError;
|
2022-04-20 20:21:07 +03:00
|
|
|
use super::AVMMemoryStats;
|
2021-10-04 10:58:00 +03:00
|
|
|
use super::AVMOutcome;
|
|
|
|
use super::CallResults;
|
2021-05-10 14:25:34 +03:00
|
|
|
use crate::config::AVMConfig;
|
2022-04-20 23:05:37 +03:00
|
|
|
use crate::interface::ParticleParameters;
|
2021-10-04 10:58:00 +03:00
|
|
|
use crate::AVMResult;
|
2021-05-10 14:25:34 +03:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2021-05-10 14:25:34 +03:00
|
|
|
|
|
|
|
/// A newtype needed to mark it as `unsafe impl Send`
|
2021-10-04 10:58:00 +03:00
|
|
|
struct SendSafeRunner(AVMRunner);
|
2021-05-10 14:25:34 +03:00
|
|
|
|
|
|
|
/// Mark runtime as Send, so libp2p on the node (use-site) is happy
|
2021-10-04 10:58:00 +03:00
|
|
|
unsafe impl Send for SendSafeRunner {}
|
2021-05-10 14:25:34 +03:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
impl Deref for SendSafeRunner {
|
|
|
|
type Target = AVMRunner;
|
2021-05-10 14:25:34 +03:00
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 10:58:00 +03:00
|
|
|
impl DerefMut for SendSafeRunner {
|
2021-05-10 14:25:34 +03:00
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
pub struct AVM<E> {
|
|
|
|
runner: SendSafeRunner,
|
|
|
|
data_store: AVMDataStore<E>,
|
2021-05-10 14:25:34 +03:00
|
|
|
}
|
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
impl<E> AVM<E> {
|
2021-05-10 14:25:34 +03:00
|
|
|
/// Create AVM with provided config.
|
2021-10-04 10:58:00 +03:00
|
|
|
pub fn new(config: AVMConfig<E>) -> AVMResult<Self, E> {
|
|
|
|
let AVMConfig {
|
|
|
|
air_wasm_path,
|
|
|
|
current_peer_id,
|
2021-12-14 14:01:57 +03:00
|
|
|
max_heap_size,
|
2021-10-04 10:58:00 +03:00
|
|
|
logging_mask,
|
|
|
|
mut data_store,
|
|
|
|
} = config;
|
2021-05-10 14:25:34 +03:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
data_store.initialize()?;
|
2021-05-10 14:25:34 +03:00
|
|
|
|
2021-12-14 14:01:57 +03:00
|
|
|
let runner = AVMRunner::new(air_wasm_path, current_peer_id, max_heap_size, logging_mask)
|
2021-10-04 10:58:00 +03:00
|
|
|
.map_err(AVMError::RunnerError)?;
|
|
|
|
let runner = SendSafeRunner(runner);
|
|
|
|
let avm = Self { runner, data_store };
|
2021-05-10 14:25:34 +03:00
|
|
|
|
|
|
|
Ok(avm)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn call(
|
|
|
|
&mut self,
|
|
|
|
air: impl Into<String>,
|
|
|
|
data: impl Into<Vec<u8>>,
|
2022-04-20 23:05:37 +03:00
|
|
|
particle_parameters: ParticleParameters<'_, '_>,
|
2021-10-04 10:58:00 +03:00
|
|
|
call_results: CallResults,
|
|
|
|
) -> AVMResult<AVMOutcome, E> {
|
2022-04-20 23:05:37 +03:00
|
|
|
let particle_id = particle_parameters.particle_id.as_str();
|
2021-10-04 10:58:00 +03:00
|
|
|
let prev_data = self.data_store.read_data(particle_id)?;
|
2021-05-10 14:25:34 +03:00
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
let outcome = self
|
|
|
|
.runner
|
2022-04-20 23:05:37 +03:00
|
|
|
.call(
|
|
|
|
air,
|
|
|
|
prev_data,
|
|
|
|
data,
|
|
|
|
particle_parameters.init_peer_id.into_owned(),
|
|
|
|
particle_parameters.timestamp,
|
2022-04-21 11:44:18 +03:00
|
|
|
particle_parameters.ttl,
|
2022-04-20 23:05:37 +03:00
|
|
|
call_results,
|
|
|
|
)
|
2021-10-04 10:58:00 +03:00
|
|
|
.map_err(AVMError::RunnerError)?;
|
2021-05-10 14:25:34 +03:00
|
|
|
|
|
|
|
// persist resulted data
|
2021-10-04 10:58:00 +03:00
|
|
|
self.data_store.store_data(&outcome.data, particle_id)?;
|
|
|
|
let outcome = AVMOutcome::from_raw_outcome(outcome)?;
|
2021-05-10 14:25:34 +03:00
|
|
|
|
|
|
|
Ok(outcome)
|
|
|
|
}
|
|
|
|
|
2021-10-04 10:58:00 +03:00
|
|
|
/// Cleanup data that become obsolete.
|
|
|
|
pub fn cleanup_data(&mut self, particle_id: &str) -> AVMResult<(), E> {
|
|
|
|
self.data_store.cleanup_data(particle_id)?;
|
2021-06-30 18:58:54 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-01-26 13:37:22 +03:00
|
|
|
|
2022-04-20 20:21:07 +03:00
|
|
|
/// Return memory stat of an interpreter heap.
|
|
|
|
pub fn memory_stats(&self) -> AVMMemoryStats {
|
|
|
|
self.runner.memory_stats()
|
2022-01-26 13:37:22 +03:00
|
|
|
}
|
2021-05-10 14:25:34 +03:00
|
|
|
}
|