mirror of
https://github.com/fluencelabs/aquavm
synced 2025-07-31 22:12:00 +00:00
fix(aquavm): temporary fix entire value in canon (#358)
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
use super::*;
|
||||
use crate::merger::errors::CanonResultError;
|
||||
|
||||
use bimap::BiHashMap;
|
||||
use serde_json::Value as JValue;
|
||||
|
||||
const EXPECTED_STATE_NAME: &str = "canon";
|
||||
|
||||
@@ -28,8 +28,7 @@ pub enum MergerCanonResult {
|
||||
|
||||
/// There was a state in at least one of the contexts. If there were two states in
|
||||
/// both contexts, they were successfully merged.
|
||||
/// Positions correspond to a new data trace.
|
||||
CanonResult { stream_elements_pos: Vec<TracePos> },
|
||||
CanonResult { canonicalized_element: JValue },
|
||||
}
|
||||
|
||||
pub(crate) fn try_merge_next_state_as_canon(data_keeper: &mut DataKeeper) -> MergeResult<MergerCanonResult> {
|
||||
@@ -39,13 +38,9 @@ pub(crate) fn try_merge_next_state_as_canon(data_keeper: &mut DataKeeper) -> Mer
|
||||
let current_state = data_keeper.current_slider_mut().next_state();
|
||||
|
||||
match (prev_state, current_state) {
|
||||
(Some(Canon(prev_canon)), Some(Canon(current_canon))) => {
|
||||
prepare_both_canon_result(&prev_canon, ¤t_canon, data_keeper)
|
||||
}
|
||||
(Some(Canon(prev_canon)), None) => prepare_single_canon_result(&prev_canon, &data_keeper.new_to_prev_pos),
|
||||
(None, Some(Canon(current_canon))) => {
|
||||
prepare_single_canon_result(¤t_canon, &data_keeper.new_to_current_pos)
|
||||
}
|
||||
(Some(Canon(prev_canon)), Some(Canon(current_canon))) => prepare_both_canon_result(prev_canon, ¤t_canon),
|
||||
(Some(Canon(prev_canon)), None) => prepare_single_canon_result(prev_canon),
|
||||
(None, Some(Canon(current_canon))) => prepare_single_canon_result(current_canon),
|
||||
(None, None) => Ok(MergerCanonResult::Empty),
|
||||
(prev_state, current_state) => Err(MergeError::incompatible_states(
|
||||
prev_state,
|
||||
@@ -56,78 +51,29 @@ pub(crate) fn try_merge_next_state_as_canon(data_keeper: &mut DataKeeper) -> Mer
|
||||
}
|
||||
|
||||
fn prepare_both_canon_result(
|
||||
prev_canon_result: &CanonResult,
|
||||
prev_canon_result: CanonResult,
|
||||
current_canon_result: &CanonResult,
|
||||
data_keeper: &DataKeeper,
|
||||
) -> MergeResult<MergerCanonResult> {
|
||||
check_canon_results(prev_canon_result, current_canon_result, data_keeper)
|
||||
.map_err(MergeError::IncorrectCanonResult)?;
|
||||
prepare_single_canon_result(prev_canon_result, &data_keeper.new_to_prev_pos)
|
||||
check_canon_results(&prev_canon_result, current_canon_result).map_err(MergeError::IncorrectCanonResult)?;
|
||||
prepare_single_canon_result(prev_canon_result)
|
||||
}
|
||||
|
||||
fn prepare_single_canon_result(
|
||||
canon_result: &CanonResult,
|
||||
new_to_other_pos: &BiHashMap<TracePos, TracePos>,
|
||||
) -> MergeResult<MergerCanonResult> {
|
||||
let new_positions = canon_result
|
||||
.stream_elements_pos
|
||||
.iter()
|
||||
.map(|pos| {
|
||||
new_to_other_pos
|
||||
.get_by_right(pos)
|
||||
.cloned()
|
||||
.ok_or_else(|| CanonResultError::not_met_position(canon_result.clone(), *pos))
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
fn prepare_single_canon_result(canon_result: CanonResult) -> MergeResult<MergerCanonResult> {
|
||||
Ok(MergerCanonResult::CanonResult {
|
||||
stream_elements_pos: new_positions,
|
||||
canonicalized_element: canon_result.canonicalized_element,
|
||||
})
|
||||
}
|
||||
|
||||
fn check_canon_results(
|
||||
prev_canon_result: &CanonResult,
|
||||
current_canon_result: &CanonResult,
|
||||
data_keeper: &DataKeeper,
|
||||
) -> Result<(), CanonResultError> {
|
||||
if prev_canon_result.stream_elements_pos.len() != current_canon_result.stream_elements_pos.len() {
|
||||
return Err(CanonResultError::different_lens(
|
||||
if prev_canon_result.canonicalized_element != current_canon_result.canonicalized_element {
|
||||
return Err(CanonResultError::incompatible_state(
|
||||
prev_canon_result.clone(),
|
||||
current_canon_result.clone(),
|
||||
));
|
||||
}
|
||||
|
||||
let prev_slider = data_keeper.prev_slider();
|
||||
let current_slider = data_keeper.current_slider();
|
||||
for (position, (prev_idx, current_idx)) in prev_canon_result
|
||||
.stream_elements_pos
|
||||
.iter()
|
||||
.zip(current_canon_result.stream_elements_pos.iter())
|
||||
.enumerate()
|
||||
{
|
||||
let prev_state = prev_slider.state_at_position(*prev_idx);
|
||||
let current_state = current_slider.state_at_position(*current_idx);
|
||||
|
||||
match (prev_state, current_state) {
|
||||
(Some(ExecutedState::Call(prev_call_result)), Some(ExecutedState::Call(current_call_result)))
|
||||
if prev_call_result == current_call_result =>
|
||||
{
|
||||
continue;
|
||||
}
|
||||
(Some(ExecutedState::Ap(_)), Some(ExecutedState::Ap(_))) => {
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
return Err(CanonResultError::incompatible_state(
|
||||
prev_canon_result.clone(),
|
||||
current_canon_result.clone(),
|
||||
prev_state.cloned(),
|
||||
current_state.cloned(),
|
||||
position,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@@ -75,25 +75,10 @@ pub enum CallResultError {
|
||||
|
||||
#[derive(ThisError, Debug)]
|
||||
pub enum CanonResultError {
|
||||
#[error("canon results have different length: {prev_canon_result:?} != {current_canon_result:?}")]
|
||||
LensNotEqual {
|
||||
prev_canon_result: CanonResult,
|
||||
current_canon_result: CanonResult,
|
||||
},
|
||||
|
||||
#[error("canon results {prev_canon_result:?} {current_canon_result:?} at position {position} points to incompatible execution states: {prev_state:?} {current_state:?}")]
|
||||
#[error("canon results {prev_canon_result:?} {current_canon_result:?} points to incompatible execution states")]
|
||||
IncompatibleState {
|
||||
prev_canon_result: CanonResult,
|
||||
current_canon_result: CanonResult,
|
||||
prev_state: Option<ExecutedState>,
|
||||
current_state: Option<ExecutedState>,
|
||||
position: usize,
|
||||
},
|
||||
|
||||
#[error("position {position} from canon result {canon_result:?} hasn't been met yet")]
|
||||
NotMetPosition {
|
||||
canon_result: CanonResult,
|
||||
position: TracePos,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -155,32 +140,12 @@ impl CallResultError {
|
||||
}
|
||||
|
||||
impl CanonResultError {
|
||||
pub(crate) fn different_lens(prev_canon_result: CanonResult, current_canon_result: CanonResult) -> Self {
|
||||
Self::LensNotEqual {
|
||||
prev_canon_result,
|
||||
current_canon_result,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn incompatible_state(
|
||||
prev_canon_result: CanonResult,
|
||||
current_canon_result: CanonResult,
|
||||
prev_state: Option<ExecutedState>,
|
||||
current_state: Option<ExecutedState>,
|
||||
position: usize,
|
||||
) -> Self {
|
||||
pub(crate) fn incompatible_state(prev_canon_result: CanonResult, current_canon_result: CanonResult) -> Self {
|
||||
Self::IncompatibleState {
|
||||
prev_canon_result,
|
||||
current_canon_result,
|
||||
prev_state,
|
||||
current_state,
|
||||
position,
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn not_met_position(canon_result: CanonResult, position: TracePos) -> Self {
|
||||
Self::NotMetPosition { canon_result, position }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
|
Reference in New Issue
Block a user