2021-08-24 16:14:15 +03:00
|
|
|
/*
|
|
|
|
* 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 super::ExecutionTrace;
|
2021-09-20 14:49:20 +03:00
|
|
|
use super::KeeperError;
|
|
|
|
use super::KeeperResult;
|
2021-08-24 16:14:15 +03:00
|
|
|
use super::TraceSlider;
|
2022-06-10 08:29:56 +03:00
|
|
|
use crate::TracePos;
|
2021-08-24 16:14:15 +03:00
|
|
|
|
2021-11-24 17:57:14 +03:00
|
|
|
use air_interpreter_data::GlobalStreamGens;
|
2021-08-24 16:14:15 +03:00
|
|
|
use air_interpreter_data::InterpreterData;
|
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
/// Contains all necessary information about data.
|
|
|
|
#[derive(Debug, Default, PartialEq)]
|
2021-10-05 14:07:38 +03:00
|
|
|
pub struct MergeCtx {
|
|
|
|
pub slider: TraceSlider,
|
2021-11-24 17:57:14 +03:00
|
|
|
pub streams: GlobalStreamGens,
|
2021-08-24 16:14:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl MergeCtx {
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub(crate) fn from_trace(trace: ExecutionTrace) -> Self {
|
|
|
|
let slider = TraceSlider::new(trace);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
slider,
|
|
|
|
streams: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_data(data: InterpreterData) -> Self {
|
|
|
|
let slider = TraceSlider::new(data.trace);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
slider,
|
2021-11-24 17:57:14 +03:00
|
|
|
streams: data.global_streams,
|
2021-08-24 16:14:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-10 08:29:56 +03:00
|
|
|
pub(crate) fn try_get_generation(&self, position: TracePos) -> KeeperResult<u32> {
|
2021-09-20 14:49:20 +03:00
|
|
|
use air_interpreter_data::*;
|
2021-08-24 16:14:15 +03:00
|
|
|
|
2021-09-20 14:49:20 +03:00
|
|
|
let state = self
|
|
|
|
.slider
|
|
|
|
.state_at_position(position)
|
|
|
|
.ok_or_else(|| KeeperError::NoElementAtPosition {
|
|
|
|
position,
|
|
|
|
trace_len: self.slider.trace_len(),
|
|
|
|
})?;
|
2021-08-24 16:14:15 +03:00
|
|
|
|
2021-09-20 14:49:20 +03:00
|
|
|
match state {
|
|
|
|
ExecutedState::Call(CallResult::Executed(Value::Stream { generation, .. })) => Ok(*generation),
|
|
|
|
// such Aps are always preceded by Fold where corresponding stream could be used,
|
|
|
|
// so it's been already checked that res_generation is well-formed
|
|
|
|
// and accessing 0th element is safe here
|
|
|
|
ExecutedState::Ap(ap_result) => Ok(ap_result.res_generations[0]),
|
|
|
|
state => Err(KeeperError::NoStreamState { state: state.clone() }),
|
|
|
|
}
|
2021-08-24 16:14:15 +03:00
|
|
|
}
|
|
|
|
|
2021-09-20 14:49:20 +03:00
|
|
|
pub(crate) fn stream_generation(&self, stream_name: &str) -> Option<u32> {
|
|
|
|
self.streams.get(stream_name).copied()
|
2021-08-24 16:14:15 +03:00
|
|
|
}
|
|
|
|
}
|