1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
 * AquaVM Workflow Engine
 *
 * Copyright (C) 2024 Fluence DAO
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use air_interpreter_data::GenerationIdx;

use super::ExecutionTrace;
use super::KeeperError;
use super::KeeperResult;
use super::TraceSlider;
use crate::TracePos;

/// Contains all necessary information about data.
#[derive(Debug, Default, PartialEq, Eq)]
pub struct MergeCtx {
    pub slider: TraceSlider,
}

impl MergeCtx {
    pub(crate) fn from_trace(trace: ExecutionTrace) -> Self {
        let slider = TraceSlider::new(trace);

        Self { slider }
    }

    pub(crate) fn try_get_generation(&self, position: TracePos) -> KeeperResult<GenerationIdx> {
        use air_interpreter_data::*;

        let state = self
            .slider
            .state_at_position(position)
            .ok_or_else(|| KeeperError::NoElementAtPosition {
                position,
                trace_len: self.slider.trace_len(),
            })?;

        match state {
            ExecutedState::Call(CallResult::Executed(ValueRef::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() }),
        }
    }
}