2020-10-15 17:31:56 +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-08-24 16:14:15 +03:00
|
|
|
use super::LastErrorDescriptor;
|
|
|
|
use super::LastErrorWithTetraplet;
|
2021-10-20 23:35:46 +03:00
|
|
|
use super::Scalars;
|
2021-11-24 17:57:14 +03:00
|
|
|
use super::Streams;
|
2021-02-11 15:39:37 +03:00
|
|
|
|
2021-10-15 14:26:46 +03:00
|
|
|
use air_execution_info_collector::InstructionTracker;
|
2021-10-04 10:58:00 +03:00
|
|
|
use air_interpreter_interface::*;
|
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
use std::rc::Rc;
|
2020-10-15 17:31:56 +03:00
|
|
|
|
2021-05-10 14:25:34 +03:00
|
|
|
/// Contains all necessary state needed to execute AIR script.
|
2021-02-11 15:39:37 +03:00
|
|
|
#[derive(Default)]
|
2020-11-03 17:43:58 +03:00
|
|
|
pub(crate) struct ExecutionCtx<'i> {
|
2021-08-24 16:14:15 +03:00
|
|
|
/// Contains all scalars.
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) scalars: Scalars<'i>,
|
2021-08-24 16:14:15 +03:00
|
|
|
|
|
|
|
/// Contains all streams.
|
2021-11-24 17:57:14 +03:00
|
|
|
pub(crate) streams: Streams,
|
2020-10-23 12:41:58 +03:00
|
|
|
|
|
|
|
/// Set of peer public keys that should receive resulted data.
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) next_peer_pks: Vec<String>,
|
2020-10-23 12:41:58 +03:00
|
|
|
|
2021-05-10 14:25:34 +03:00
|
|
|
/// PeerId of a peer executing this AIR script at the moment.
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) current_peer_id: Rc<String>,
|
2020-10-23 12:41:58 +03:00
|
|
|
|
2021-05-10 14:25:34 +03:00
|
|
|
/// PeerId of a peer send this AIR script.
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) init_peer_id: String,
|
2020-11-11 14:31:53 +03:00
|
|
|
|
2021-02-11 15:39:37 +03:00
|
|
|
/// Last error produced by local service.
|
|
|
|
/// None means that there weren't any error.
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) last_error: Option<LastErrorDescriptor>,
|
2021-02-11 15:39:37 +03:00
|
|
|
|
2021-02-16 20:04:00 +03:00
|
|
|
/// True, if last error could be set. This flag is used to distinguish
|
|
|
|
/// whether an error is being bubbled up from the bottom or just encountered.
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) last_error_could_be_set: bool,
|
2021-02-16 20:04:00 +03:00
|
|
|
|
2020-10-23 12:41:58 +03:00
|
|
|
/// Indicates that previous executed subtree is complete.
|
|
|
|
/// A subtree treats as a complete if all subtree elements satisfy the following rules:
|
2021-01-15 00:38:58 +03:00
|
|
|
/// - at least one of par subtrees is completed
|
2021-08-24 16:14:15 +03:00
|
|
|
/// - at least one of xor subtrees is completed without an error
|
2021-01-15 00:38:58 +03:00
|
|
|
/// - all of seq subtrees are completed
|
|
|
|
/// - call executed successfully (executed state is Executed)
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) subtree_complete: bool,
|
2021-08-24 16:14:15 +03:00
|
|
|
|
|
|
|
/// Tracker of all met instructions.
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) tracker: InstructionTracker,
|
2021-10-04 10:58:00 +03:00
|
|
|
|
|
|
|
/// Last call request id that was used as an id for call request in outcome.
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) last_call_request_id: u32,
|
2021-10-04 10:58:00 +03:00
|
|
|
|
|
|
|
/// Contains all executed results from a host side.
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) call_results: CallResults,
|
2021-10-04 10:58:00 +03:00
|
|
|
|
|
|
|
/// Tracks all functions that should be called from services.
|
2021-10-20 23:35:46 +03:00
|
|
|
pub(crate) call_requests: CallRequests,
|
2020-10-15 17:31:56 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 17:43:58 +03:00
|
|
|
impl<'i> ExecutionCtx<'i> {
|
2021-10-04 10:58:00 +03:00
|
|
|
pub(crate) fn new(
|
|
|
|
current_peer_id: String,
|
|
|
|
init_peer_id: String,
|
|
|
|
call_results: CallResults,
|
|
|
|
last_call_request_id: u32,
|
|
|
|
) -> Self {
|
2021-08-24 16:14:15 +03:00
|
|
|
let current_peer_id = Rc::new(current_peer_id);
|
|
|
|
|
2020-10-15 17:31:56 +03:00
|
|
|
Self {
|
|
|
|
current_peer_id,
|
2020-11-11 14:31:53 +03:00
|
|
|
init_peer_id,
|
2020-10-23 12:41:58 +03:00
|
|
|
subtree_complete: true,
|
2021-02-16 20:04:00 +03:00
|
|
|
last_error_could_be_set: true,
|
2021-10-04 10:58:00 +03:00
|
|
|
last_call_request_id,
|
|
|
|
call_results,
|
2021-02-11 15:39:37 +03:00
|
|
|
..<_>::default()
|
2020-10-15 17:31:56 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-02 14:33:21 +03:00
|
|
|
|
2021-08-24 16:14:15 +03:00
|
|
|
pub(crate) fn last_error(&self) -> LastErrorWithTetraplet {
|
2021-06-02 14:33:21 +03:00
|
|
|
match &self.last_error {
|
2021-08-24 16:14:15 +03:00
|
|
|
Some(error_descriptor) => LastErrorWithTetraplet::from_error_descriptor(error_descriptor, self),
|
2021-06-02 14:33:21 +03:00
|
|
|
None => <_>::default(),
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 10:58:00 +03:00
|
|
|
|
|
|
|
pub(crate) fn next_call_request_id(&mut self) -> u32 {
|
|
|
|
self.last_call_request_id += 1;
|
|
|
|
self.last_call_request_id
|
|
|
|
}
|
2020-10-15 17:31:56 +03:00
|
|
|
}
|
2020-10-30 20:29:05 +03:00
|
|
|
|
2021-01-15 00:38:58 +03:00
|
|
|
use std::fmt::Display;
|
|
|
|
use std::fmt::Formatter;
|
|
|
|
|
2020-11-03 17:43:58 +03:00
|
|
|
impl<'i> Display for ExecutionCtx<'i> {
|
2020-10-30 20:29:05 +03:00
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
2021-10-20 23:35:46 +03:00
|
|
|
writeln!(f, "scalars:")?;
|
|
|
|
writeln!(f, " {}", self.scalars)?;
|
|
|
|
|
|
|
|
writeln!(f, "streams:")?;
|
2021-11-24 17:57:14 +03:00
|
|
|
writeln!(f, " {}", self.streams)?;
|
2021-10-20 23:35:46 +03:00
|
|
|
|
2020-10-30 20:29:05 +03:00
|
|
|
writeln!(f, "current peer id: {}", self.current_peer_id)?;
|
|
|
|
writeln!(f, "subtree complete: {}", self.subtree_complete)?;
|
|
|
|
writeln!(f, "next peer public keys: {:?}", self.next_peer_pks)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|