Files
aquavm/stepper-lib/src/execution/air/call/utils.rs

158 lines
5.8 KiB
Rust
Raw Normal View History

2020-10-30 20:29:05 +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.
*/
use super::ExecutionCtx;
2021-01-15 00:38:58 +03:00
use super::ExecutionError;
use super::ExecutionResult;
use crate::contexts::execution::ResolvedCallResult;
use crate::contexts::execution_trace::*;
use crate::log_targets::EXECUTED_STATE_CHANGING;
2020-10-30 20:29:05 +03:00
use crate::JValue;
2021-01-27 00:37:58 +03:00
use air_parser::ast::CallOutputValue;
2020-12-22 21:05:04 +03:00
use polyplets::ResolvedTriplet;
2020-10-30 20:29:05 +03:00
2020-12-22 21:05:04 +03:00
use std::rc::Rc;
2020-11-03 17:43:58 +03:00
2020-12-22 21:05:04 +03:00
/// Writes result of a local `Call` instruction to `ExecutionCtx` at `output`.
2020-11-03 17:43:58 +03:00
pub(super) fn set_local_call_result<'i>(
2020-10-30 20:29:05 +03:00
result: Rc<JValue>,
2020-12-22 21:05:04 +03:00
triplet: Rc<ResolvedTriplet>,
2021-01-27 00:37:58 +03:00
output: &CallOutputValue<'i>,
2020-12-22 21:05:04 +03:00
exec_ctx: &mut ExecutionCtx<'i>,
2021-01-15 00:38:58 +03:00
) -> ExecutionResult<()> {
use crate::contexts::execution::AValue;
2020-12-22 21:05:04 +03:00
use std::cell::RefCell;
2020-10-30 20:29:05 +03:00
use std::collections::hash_map::Entry::{Occupied, Vacant};
2021-01-15 00:38:58 +03:00
use ExecutionError::*;
2020-10-30 20:29:05 +03:00
2020-12-22 21:05:04 +03:00
let executed_result = ResolvedCallResult { result, triplet };
2020-11-03 17:43:58 +03:00
match output {
2021-01-27 00:37:58 +03:00
CallOutputValue::Scalar(name) => {
if let Some(fold_block_name) = exec_ctx.met_folds.back() {
let fold_state = match exec_ctx.data_cache.get_mut(*fold_block_name) {
Some(AValue::JValueFoldCursor(fold_state)) => fold_state,
_ => unreachable!("fold block data must be represented as fold cursor"),
};
2020-12-22 21:05:04 +03:00
fold_state.met_variables.insert(name, executed_result.clone());
}
2020-11-03 17:43:58 +03:00
match exec_ctx.data_cache.entry(name.to_string()) {
Vacant(entry) => {
2020-12-22 21:05:04 +03:00
entry.insert(AValue::JValueRef(executed_result));
}
Occupied(mut entry) => {
// check that current execution flow is inside a fold block
if exec_ctx.met_folds.is_empty() {
// shadowing is allowed only inside fold blocks
return Err(MultipleVariablesFound(entry.key().clone()));
}
match entry.get() {
AValue::JValueRef(_) => {}
// shadowing is allowed only for scalar values
_ => return Err(ShadowingError(entry.key().clone())),
};
2020-12-22 21:05:04 +03:00
entry.insert(AValue::JValueRef(executed_result));
}
2020-11-03 17:43:58 +03:00
};
}
2021-01-27 00:37:58 +03:00
CallOutputValue::Accumulator(name) => {
2020-11-03 17:43:58 +03:00
match exec_ctx.data_cache.entry(name.to_string()) {
Occupied(mut entry) => match entry.get_mut() {
// if result is an array, insert result to the end of the array
2020-12-22 21:05:04 +03:00
AValue::JValueAccumulatorRef(values) => values.borrow_mut().push(executed_result),
v => return Err(IncompatibleAValueType(format!("{}", v), String::from("Array"))),
2020-11-03 17:43:58 +03:00
},
Vacant(entry) => {
2020-12-22 21:05:04 +03:00
entry.insert(AValue::JValueAccumulatorRef(RefCell::new(vec![executed_result])));
2020-11-03 17:43:58 +03:00
}
};
2020-10-30 20:29:05 +03:00
}
2021-01-27 00:37:58 +03:00
CallOutputValue::None => {}
2020-10-30 20:29:05 +03:00
}
Ok(())
}
2021-01-15 00:38:58 +03:00
/// Writes an executed state of a particle being sent to remote node
2020-11-03 17:43:58 +03:00
pub(super) fn set_remote_call_result<'i>(
peer_pk: String,
exec_ctx: &mut ExecutionCtx<'i>,
2021-01-15 00:38:58 +03:00
trace_ctx: &mut ExecutionTraceCtx,
2020-11-03 17:43:58 +03:00
) {
2020-10-30 20:29:05 +03:00
exec_ctx.next_peer_pks.push(peer_pk);
exec_ctx.subtree_complete = false;
2021-01-15 00:38:58 +03:00
let new_executed_state = ExecutedState::Call(CallResult::RequestSentBy(exec_ctx.current_peer_id.clone()));
log::trace!(
2021-01-15 00:38:58 +03:00
target: EXECUTED_STATE_CHANGING,
" adding new call executed state {:?}",
new_executed_state
2020-10-30 20:29:05 +03:00
);
2021-01-15 00:38:58 +03:00
trace_ctx.new_trace.push_back(new_executed_state);
2020-10-30 20:29:05 +03:00
}
2020-12-22 21:05:04 +03:00
/// This function looks at the existing call state, validates it,
/// and returns Ok(true) if the call should be executed further.
pub(super) fn handle_prev_state<'i>(
triplet: &Rc<ResolvedTriplet>,
2021-01-27 00:37:58 +03:00
output: &CallOutputValue<'i>,
2021-01-15 00:38:58 +03:00
prev_state: ExecutedState,
2020-12-22 21:05:04 +03:00
exec_ctx: &mut ExecutionCtx<'i>,
2021-01-15 00:38:58 +03:00
trace_ctx: &mut ExecutionTraceCtx,
) -> ExecutionResult<bool> {
use CallResult::*;
use ExecutedState::*;
2020-12-22 21:05:04 +03:00
match &prev_state {
// this call was failed on one of the previous executions,
// here it's needed to bubble this special error up
Call(CallServiceFailed(err_msg)) => {
let err_msg = err_msg.clone();
2021-01-15 00:38:58 +03:00
trace_ctx.new_trace.push_back(prev_state);
2020-12-22 21:05:04 +03:00
exec_ctx.subtree_complete = false;
2021-01-15 00:38:58 +03:00
Err(ExecutionError::LocalServiceError(err_msg))
2020-12-22 21:05:04 +03:00
}
2021-01-15 00:38:58 +03:00
Call(RequestSentBy(..)) => {
2020-12-22 21:05:04 +03:00
let peer_pk = triplet.peer_pk.as_str();
// check whether current node can execute this call
let is_current_peer = peer_pk == exec_ctx.current_peer_id;
if is_current_peer {
Ok(true)
} else {
exec_ctx.subtree_complete = false;
2021-01-15 00:38:58 +03:00
trace_ctx.new_trace.push_back(prev_state);
2020-12-22 21:05:04 +03:00
Ok(false)
}
}
// this instruction's been already executed
Call(Executed(result)) => {
set_local_call_result(result.clone(), triplet.clone(), output, exec_ctx)?;
2021-01-15 00:38:58 +03:00
trace_ctx.new_trace.push_back(prev_state);
2020-12-22 21:05:04 +03:00
Ok(false)
}
// state has inconsistent order - return a error, call shouldn't be executed
2021-01-15 00:38:58 +03:00
par_state @ Par(..) => Err(ExecutionError::InvalidExecutedState(
2020-12-22 21:05:04 +03:00
String::from("call"),
2021-01-15 00:38:58 +03:00
par_state.clone(),
2020-12-22 21:05:04 +03:00
)),
}
}