aquavm/stepper-lib/src/air/execution_context.rs

68 lines
2.2 KiB
Rust
Raw Normal View History

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.
*/
2020-10-30 20:29:05 +03:00
use crate::AValue;
2020-10-15 17:31:56 +03:00
use std::collections::HashMap;
2020-10-30 20:29:05 +03:00
use std::fmt::Display;
use std::fmt::Formatter;
2020-10-15 17:31:56 +03:00
2020-10-23 12:41:58 +03:00
/// Execution context contains all necessary information needed to execute aqua script.
2020-10-15 17:31:56 +03:00
#[derive(Clone, Default, Debug)]
2020-11-03 17:43:58 +03:00
pub(crate) struct ExecutionCtx<'i> {
2020-10-23 12:41:58 +03:00
/// Contains all set variables.
2020-11-03 17:43:58 +03:00
pub data_cache: HashMap<String, AValue<'i>>,
2020-10-23 12:41:58 +03:00
/// Set of peer public keys that should receive resulted data.
2020-10-15 17:31:56 +03:00
pub next_peer_pks: Vec<String>,
2020-10-23 12:41:58 +03:00
/// PeerId of a peer executing this aqua script.
2020-10-15 17:31:56 +03:00
pub current_peer_id: String,
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:
/// - at least one of par subtrees is complete
2020-10-30 20:29:05 +03:00
/// - non-thrown subtree of xor is complete
2020-10-23 12:41:58 +03:00
/// - all of seq subtrees are complete
/// - call executes successfully (call evidence equals to Executed)
pub subtree_complete: bool,
2020-10-15 17:31:56 +03:00
}
2020-11-03 17:43:58 +03:00
impl<'i> ExecutionCtx<'i> {
2020-10-30 20:29:05 +03:00
pub(crate) fn new(current_peer_id: String) -> Self {
2020-10-15 17:31:56 +03:00
Self {
2020-10-30 20:29:05 +03:00
data_cache: HashMap::new(),
2020-10-15 17:31:56 +03:00
next_peer_pks: vec![],
current_peer_id,
2020-10-23 12:41:58 +03:00
subtree_complete: true,
2020-10-15 17:31:56 +03:00
}
}
}
2020-10-30 20:29:05 +03:00
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 {
writeln!(f, "data cache:")?;
for (key, value) in self.data_cache.iter() {
writeln!(f, " {} => {}", key, value)?;
}
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(())
}
}