Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
discard_reconstruction.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cassert>
4#include <functional>
5#include <optional>
6#include <stack>
7#include <unordered_map>
8#include <variant>
9#include <vector>
10
13
14namespace bb::avm2::tracegen {
15
33template <typename EventVariant>
35{
36 // Maps the index of the checkpoint to the index it was reverted in.
38
39 std::stack<size_t> checkpoint_stack;
40
41 // We need to reconstruct discard in this trace using checkpointing events.
42 // https://hackmd.io/luYtD3XVTpGCDFeeCYS_Uw?view#Discard-reconstruction
43 // Find all the reverts and record which checkpoint is being reverted.
44 for (size_t i = 0; i < events.size(); i++) {
45 const auto& event = events.at(i);
46
50 checkpoint_stack.push(i);
51 break;
53 BB_ASSERT(!checkpoint_stack.empty());
54 checkpoint_stack.pop();
55 break;
57 BB_ASSERT(!checkpoint_stack.empty());
58 reverted_in[checkpoint_stack.top()] = i;
59 checkpoint_stack.pop();
60 break;
61 }
62 }
63 }
64
65 return reverted_in;
66}
67
85template <typename EventType, typename ProcessEventFn>
87 ProcessEventFn&& process_event)
88{
89 auto reverted_in = compute_reverted_in_map(events);
90 bool discard = false;
91 std::optional<size_t> waiting_for_revert = std::nullopt;
92
93 for (size_t i = 0; i < events.size(); i++) {
94 const auto& event = events.at(i);
96 auto check_point_event = std::get<simulation::CheckPointEventType>(event);
97 if (check_point_event == simulation::CheckPointEventType::CREATE_CHECKPOINT && reverted_in.contains(i) &&
98 !waiting_for_revert.has_value()) {
99 // This checkpoint will revert in the future: discard all events until the revert.
100 waiting_for_revert = reverted_in.at(i);
101 discard = true;
102 } else if (check_point_event == simulation::CheckPointEventType::REVERT_CHECKPOINT &&
103 waiting_for_revert.has_value() && waiting_for_revert.value() == i) {
104 // We found the revert we were waiting for: stop discarding events.
105 // Note that we ensure that we find exactly the revert we were waiting for and ignore any nested
106 // reverts.
107 waiting_for_revert = std::nullopt;
108 discard = false;
109 }
110 } else {
111 const auto& payload_event = std::get<EventType>(event);
112 process_event(payload_event, discard);
113 }
114 }
115}
116
117} // namespace bb::avm2::tracegen
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
void process_with_discard(const std::vector< std::variant< EventType, simulation::CheckPointEventType > > &events, ProcessEventFn &&process_event)
Processes events from a stream, attaching a discard flag to indicate reverted checkpoints.
std::unordered_map< size_t, size_t > compute_reverted_in_map(const std::vector< EventVariant > &events)
Computes a mapping of checkpoint creation indices to the indices where they are reverted.
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
simulation::PublicDataTreeReadWriteEvent event