aquavm/interpreter-lib/src/contexts/execution_trace_context.rs

65 lines
2.2 KiB
Rust
Raw Normal View History

2020-10-15 17:31:56 +03:00
/*
* Copyright 2020 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.
*/
2021-01-15 00:38:58 +03:00
mod executed_state;
pub use executed_state::CallResult;
pub use executed_state::ExecutedState;
2020-10-15 17:31:56 +03:00
use serde::Deserialize;
use serde::Serialize;
2020-10-30 20:29:05 +03:00
use std::fmt::Display;
use std::fmt::Formatter;
2020-10-15 17:31:56 +03:00
2021-01-15 00:38:58 +03:00
pub type ExecutionTrace = std::collections::VecDeque<ExecutedState>;
/// Encapsulates all necessary state regarding to the call pathes1.
2020-10-15 17:31:56 +03:00
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
2021-01-15 00:38:58 +03:00
pub(crate) struct ExecutionTraceCtx {
/// Contains trace (serialized tree of states) after merging current and previous data,
2021-02-17 23:36:36 +03:00
/// interpreter used it to realize which instructions've been already executed.
2021-01-15 00:38:58 +03:00
pub(crate) current_trace: ExecutionTrace,
2020-12-22 21:05:04 +03:00
/// Size of a current considered subtree inside current path.
pub(crate) current_subtree_size: usize,
2020-12-22 21:05:04 +03:00
2020-10-16 12:47:46 +03:00
// TODO: consider change it to Vec for optimization
2021-02-17 23:36:36 +03:00
/// Accumulator for resulted path produced by the interpreter after execution.
2021-01-15 00:38:58 +03:00
pub(crate) new_trace: ExecutionTrace,
2020-10-15 17:31:56 +03:00
}
2021-01-15 00:38:58 +03:00
impl ExecutionTraceCtx {
pub fn new(current_trace: ExecutionTrace) -> Self {
let current_subtree_size = current_trace.len();
// a new execution trace will contain at least current_path.len() elements
let new_trace = ExecutionTrace::with_capacity(current_subtree_size);
2020-10-15 17:31:56 +03:00
Self {
2021-01-15 00:38:58 +03:00
current_trace,
current_subtree_size,
2021-01-15 00:38:58 +03:00
new_trace,
2020-10-15 17:31:56 +03:00
}
}
}
2020-10-30 20:29:05 +03:00
2021-01-15 00:38:58 +03:00
impl Display for ExecutionTraceCtx {
2020-10-30 20:29:05 +03:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
2021-01-15 00:38:58 +03:00
writeln!(f, "current trace:\n{:?}", self.current_trace)?;
writeln!(f, "current subtree elements count:\n{:?}", self.current_subtree_size)?;
2021-01-15 00:38:58 +03:00
writeln!(f, "new trace:\n{:?}", self.new_trace)
2020-10-30 20:29:05 +03:00
}
}