Skip to content

Commit

Permalink
Do not print warning in xor for match/mismatch (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikevoronov authored Apr 15, 2021
1 parent 3d59e6f commit ecd97a2
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
45 changes: 44 additions & 1 deletion interpreter-lib/src/execution/air/match_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ mod tests {
use crate::contexts::execution_trace::ExecutionTrace;
use crate::JValue;

use aqua_test_utils::call_vm;
use aqua_test_utils::create_aqua_vm;
use aqua_test_utils::echo_string_call_service;
use aqua_test_utils::{call_vm, set_variable_call_service};

use std::rc::Rc;

Expand Down Expand Up @@ -194,4 +194,47 @@ mod tests {

assert_eq!(res.ret_code, 1015);
}

#[test]
fn match_with_two_xors() {
use crate::contexts::execution_trace::CallResult::*;
use crate::contexts::execution_trace::ExecutedState::*;

let local_peer_id = "local_peer_id";
let mut vm = create_aqua_vm(
set_variable_call_service(serde_json::json!(false).to_string()),
local_peer_id,
);

let local_peer_id_2 = "local_peer_id_2";

let script = format!(
r#"
(xor
(seq
(seq
(call "{0}" ("getDataSrv" "condition") [] condition)
(call "{0}" ("getDataSrv" "relay") [] relay)
)
(xor
(match condition true
(call "{0}" ("println" "print") ["it is true"])
)
(call "{1}" ("println" "print") ["it is false"])
)
)
(call "{0}" ("errorHandlingSrv" "error") [%last_error%])
)
"#,
local_peer_id, local_peer_id_2
);

let res = call_vm!(vm, "", script, "", "");
let mut trace: ExecutionTrace =
serde_json::from_slice(&res.data).expect("the interpreter should provide correct trace");

let expected_executed_call_result = Call(RequestSentBy(local_peer_id.to_string()));
assert_eq!(res.ret_code, 0);
assert_eq!(trace.pop_back().unwrap(), expected_executed_call_result);
}
}
44 changes: 44 additions & 0 deletions interpreter-lib/src/execution/air/mismatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ mod tests {
use aqua_test_utils::call_vm;
use aqua_test_utils::create_aqua_vm;
use aqua_test_utils::echo_string_call_service;
use aqua_test_utils::set_variable_call_service;

use std::rc::Rc;

Expand Down Expand Up @@ -194,4 +195,47 @@ mod tests {

assert_eq!(res.ret_code, 1016);
}

#[test]
fn mismatch_with_two_xors() {
use crate::contexts::execution_trace::CallResult::*;
use crate::contexts::execution_trace::ExecutedState::*;

let local_peer_id = "local_peer_id";
let mut vm = create_aqua_vm(
set_variable_call_service(serde_json::json!(false).to_string()),
local_peer_id,
);

let local_peer_id_2 = "local_peer_id_2";

let script = format!(
r#"
(xor
(seq
(seq
(call "{0}" ("getDataSrv" "condition") [] condition)
(call "{0}" ("getDataSrv" "relay") [] relay)
)
(xor
(mismatch condition true
(call "{1}" ("println" "print") ["it is false"])
)
(call "{0}" ("println" "print") ["it is true"])
)
)
(call "{0}" ("errorHandlingSrv" "error") [%last_error%])
)
"#,
local_peer_id, local_peer_id_2
);

let res = call_vm!(vm, "", script, "", "");
let mut trace: ExecutionTrace =
serde_json::from_slice(&res.data).expect("the interpreter should provide correct trace");

let expected_executed_call_result = Call(RequestSentBy(local_peer_id.to_string()));
assert_eq!(res.ret_code, 0);
assert_eq!(trace.pop_back().unwrap(), expected_executed_call_result);
}
}
13 changes: 12 additions & 1 deletion interpreter-lib/src/execution/air/xor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'i> super::ExecutableInstruction<'i> for Xor<'i> {
Err(e) if is_catchable_by_xor(&e) => {
exec_ctx.subtree_complete = true;
exec_ctx.last_error_could_be_set = true;
log::warn!("xor caught an error: {}", e);
print_xor_log(&e);

self.1.execute(exec_ctx, trace_ctx)
}
Expand All @@ -46,6 +46,17 @@ fn is_catchable_by_xor(exec_error: &ExecutionError) -> bool {
!matches!(exec_error, ExecutionError::InvalidExecutedState(..))
}

fn print_xor_log(e: &ExecutionError) {
match e {
// These errors actually aren't real errors, but a way to bubble execution up from match
// to a corresponding xor. They'll become errors iff there is no such xor and execution is
// bubble up until the very beginning of current subtree. So the error message shouldn't
// be print out in order not to confuse users.
ExecutionError::MatchWithoutXorError | ExecutionError::MismatchWithoutXorError => {}
e => log::warn!("xor caught an error: {}", e),
}
}

#[cfg(test)]
mod tests {
use crate::contexts::execution_trace::ExecutionTrace;
Expand Down

0 comments on commit ecd97a2

Please sign in to comment.