Skip to content

Commit

Permalink
introduce farewell step; reafactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevoronov committed Dec 17, 2021
1 parent 8dbae91 commit 54e383c
Show file tree
Hide file tree
Showing 26 changed files with 233 additions and 111 deletions.
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions air/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ serde = { version = "1.0.118", features = [ "derive", "rc" ] }
serde_json = "1.0.61"

boolinator = "2.4.0"
concat-idents = "1.1.3"
maplit = "1.0.2"
log = "0.4.11"
thiserror = "1.0.23"
Expand Down
2 changes: 1 addition & 1 deletion air/src/execution_step/air/ap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use super::ExecutionResult;
use super::TraceHandler;
use crate::execution_step::air::ValueAggregate;
use crate::execution_step::boxed_value::Variable;
use crate::execution_step::utils::apply_lambda;
use crate::execution_step::resolver::apply_lambda;
use crate::trace_to_exec_err;
use crate::JValue;
use crate::SecurityTetraplet;
Expand Down
2 changes: 1 addition & 1 deletion air/src/execution_step/air/ap/apply_to_arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn apply_last_error(
exec_ctx: &ExecutionCtx<'_>,
trace_ctx: &TraceHandler,
) -> ExecutionResult<ValueAggregate> {
let (value, mut tetraplets) = crate::execution_step::utils::prepare_last_error(error_path, exec_ctx)?;
let (value, mut tetraplets) = crate::execution_step::resolver::prepare_last_error(error_path, exec_ctx)?;
let value = Rc::new(value);
// removing is safe because prepare_last_error always returns a vec with one element.
let tetraplet = tetraplets.remove(0);
Expand Down
2 changes: 1 addition & 1 deletion air/src/execution_step/air/call/resolved_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<'i> ResolvedCall<'i> {

/// Prepare arguments of this call instruction by resolving and preparing their security tetraplets.
fn resolve_args(&self, exec_ctx: &ExecutionCtx<'i>) -> ExecutionResult<ResolvedArguments> {
use crate::execution_step::utils::resolve_to_args;
use crate::execution_step::resolver::resolve_to_args;

let function_args = self.function_arg_paths.iter();
let mut call_arguments = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion air/src/execution_step/air/call/triplet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) fn resolve<'i>(triplet: &ast::Triplet<'i>, ctx: &ExecutionCtx<'i>) ->
/// Resolve value to string by either resolving variable from `ExecutionCtx`, taking literal value, or etc.
// TODO: return Rc<String> to avoid excess cloning
fn resolve_to_string<'i>(value: &ast::CallInstrValue<'i>, ctx: &ExecutionCtx<'i>) -> ExecutionResult<String> {
use crate::execution_step::utils::resolve_ast_variable_wl;
use crate::execution_step::resolver::resolve_ast_variable_wl;
use ast::CallInstrValue::*;

let resolved = match value {
Expand Down
4 changes: 2 additions & 2 deletions air/src/execution_step/air/compare_matchable/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

use crate::execution_step::air::ExecutionResult;
use crate::execution_step::execution_context::ExecutionCtx;
use crate::execution_step::utils::prepare_last_error;
use crate::execution_step::utils::resolve_ast_variable_wl;
use crate::execution_step::resolver::prepare_last_error;
use crate::execution_step::resolver::resolve_ast_variable_wl;
use crate::JValue;

use air_parser::ast;
Expand Down
7 changes: 4 additions & 3 deletions air/src/execution_step/boxed_value/jvaluable/empty_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* limitations under the License.
*/

use super::ExecutionError;
use super::ExecutionError::LambdaApplierError;
use super::ExecutionResult;
use super::JValuable;
use super::LambdaAST;
use super::LambdaError::EmptyStream;
use crate::exec_err;
use crate::execution_step::ExecutionCtx;
use crate::execution_step::RSecurityTetraplet;
Expand All @@ -29,7 +30,7 @@ use std::borrow::Cow;
impl JValuable for () {
fn apply_lambda<'i>(&self, _lambda: &LambdaAST<'_>, _exec_ctx: &ExecutionCtx<'i>) -> ExecutionResult<&JValue> {
// applying lambda to an empty stream will produce a join behaviour
exec_err!(ExecutionError::EmptyStreamLambdaError)
exec_err!(LambdaApplierError(EmptyStream))
}

fn apply_lambda_with_tetraplets<'i>(
Expand All @@ -38,7 +39,7 @@ impl JValuable for () {
_exec_ctx: &ExecutionCtx<'i>,
) -> ExecutionResult<(&JValue, RSecurityTetraplet)> {
// applying lambda to an empty stream will produce a join behaviour
exec_err!(ExecutionError::EmptyStreamLambdaError)
exec_err!(LambdaApplierError(EmptyStream))
}

fn as_jvalue(&self) -> Cow<'_, JValue> {
Expand Down
38 changes: 22 additions & 16 deletions air/src/execution_step/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub(crate) use joinable::Joinable;
use super::Stream;
use crate::execution_step::lambda_applier::LambdaError;
use crate::JValue;
use crate::ToErrorCode;

use air_interpreter_interface::CallResults;
use air_trace_handler::MergerApResult;
use air_trace_handler::TraceHandlerError;
use strum::IntoEnumIterator;
Expand Down Expand Up @@ -54,10 +54,6 @@ pub(crate) enum ExecutionError {
#[error(transparent)]
LambdaApplierError(#[from] LambdaError),

/// An error occurred while trying to apply lambda to an empty stream.
#[error("lambda is applied to an empty stream")]
EmptyStreamLambdaError,

/// Provided JValue has incompatible type with a requested one.
#[error(
"expected JValue type '{expected_value_type}' for the variable `{variable_name}`, but got '{actual_value}'"
Expand Down Expand Up @@ -108,12 +104,6 @@ pub(crate) enum ExecutionError {
/// could be applied to a stream, but result doesn't contain generation in a source position.
#[error("ap result {0:?} doesn't match corresponding instruction")]
ApResultNotCorrespondToInstr(MergerApResult),

/// Call results should be empty at the end of execution thanks to a execution invariant.
#[error(
"after finishing execution of supplied AIR, call results aren't empty: `{0:?}`, probably wrong call_id used"
)]
CallResultsNotEmpty(CallResults),
}

impl From<LambdaError> for Rc<ExecutionError> {
Expand All @@ -131,15 +121,31 @@ macro_rules! trace_to_exec_err {
};
}

impl ExecutionError {
pub(crate) fn to_error_code(&self) -> u32 {
const EXECUTION_ERRORS_START_ID: u32 = 1000;
/*
impl ToErrorCode for ExecutionError {
fn to_error_code(&self) -> i64 {
const EXECUTION_ERRORS_START_ID: i64 = 1000;
let mut errors = ExecutionErrorDiscriminants::iter();
let actual_error_type = ExecutionErrorDiscriminants::from(self);
// unwrap is safe here because errors are guaranteed to contain all errors variants
let enum_variant_position = errors.position(|et| et == actual_error_type).unwrap() as u32;
let enum_variant_position = errors.position(|et| et == actual_error_type).unwrap() as i64;
EXECUTION_ERRORS_START_ID + enum_variant_position
}
}
*/

impl ToErrorCode for Rc<ExecutionError> {
fn to_error_code(&self) -> i64 {
const EXECUTION_ERRORS_START_ID: i64 = 1000;

let mut errors = ExecutionErrorDiscriminants::iter();
let actual_error_type = ExecutionErrorDiscriminants::from(self.as_ref());

// unwrap is safe here because errors are guaranteed to contain all errors variants
let enum_variant_position = errors.position(|et| et == actual_error_type).unwrap() as i64;
EXECUTION_ERRORS_START_ID + enum_variant_position
}
}
Expand All @@ -166,7 +172,7 @@ impl Joinable for ExecutionError {
log_join!(" waiting for an argument with idx '{}' on stream with size '{}'", idx, stream_size);
true
}
EmptyStreamLambdaError => {
LambdaApplierError(LambdaError::EmptyStream) => {
log_join!(" waiting on empty stream for path ");
true
}
Expand Down
4 changes: 4 additions & 0 deletions air/src/execution_step/lambda_applier/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ pub(crate) enum LambdaError {
#[error("lambda is applied to a stream that have only '{stream_size}' elements, but '{idx}' requested")]
StreamNotHaveEnoughValues { stream_size: usize, idx: u32 },

/// An error occurred while trying to apply lambda to an empty stream.
#[error("lambda is applied to an empty stream")]
EmptyStream,

#[error("field accessor (with field name = '{field_name}') can't be applied to a stream")]
FieldAccessorAppliedToStream { field_name: String },

Expand Down
2 changes: 1 addition & 1 deletion air/src/execution_step/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod boxed_value;
mod errors;
pub(crate) mod execution_context;
mod lambda_applier;
mod utils;
mod resolver;

pub(super) use self::air::ExecutableInstruction;
pub(super) use self::air::FoldState;
Expand Down
File renamed without changes.
File renamed without changes.
47 changes: 47 additions & 0 deletions air/src/farewell_step/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2021 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 crate::ToErrorCode;
use air_interpreter_interface::CallResults;

use strum::IntoEnumIterator;
use strum_macros::EnumDiscriminants;
use strum_macros::EnumIter;
use thiserror::Error as ThisError;

/// Errors happened during the interpreter farewell step.
#[derive(Debug, EnumDiscriminants, ThisError)]
#[strum_discriminants(derive(EnumIter))]
pub enum FarewellError {
/// Call results should be empty at the end of execution thanks to a execution invariant.
#[error(
"after finishing execution of supplied AIR, call results aren't empty: `{0:?}`, probably wrong call_id used"
)]
CallResultsNotEmpty(CallResults),
}

impl ToErrorCode for FarewellError {
fn to_error_code(&self) -> i64 {
const FAREWELL_ERRORS_START_ID: i64 = 20000;

let mut errors = FarewellErrorDiscriminants::iter();
let actual_error_type = FarewellErrorDiscriminants::from(self);

// unwrap is safe here because errors are guaranteed to contain all errors variants
let enum_variant_position = errors.position(|et| et == actual_error_type).unwrap() as i64;
FAREWELL_ERRORS_START_ID + enum_variant_position
}
}
23 changes: 23 additions & 0 deletions air/src/farewell_step/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2021 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.
*/

mod errors;
mod outcome;

pub(crate) use errors::FarewellError;
pub(crate) use outcome::from_execution_error;
pub(crate) use outcome::from_success_result;
pub(crate) use outcome::from_uncatchable_error;
Loading

0 comments on commit 54e383c

Please sign in to comment.