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
12
13namespace bb::avm2::tracegen {
14
15// T is expected to be a std::variant that includes simulation::CheckPointEventType
16template <typename EventVariant>
18{
19 // Maps the index of the checkpoint to the index it was reverted in.
21
22 std::stack<size_t> checkpoint_stack;
23
24 // We need to reconstruct discard in this trace using checkpointing events.
25 // https://hackmd.io/luYtD3XVTpGCDFeeCYS_Uw?view#Discard-reconstruction
26 // Find all the reverts and record which checkpoint is being reverted.
27 for (size_t i = 0; i < events.size(); i++) {
28 const auto& event = events.at(i);
29
33 checkpoint_stack.push(i);
34 break;
36 assert(!checkpoint_stack.empty());
37 checkpoint_stack.pop();
38 break;
40 assert(!checkpoint_stack.empty());
41 reverted_in[checkpoint_stack.top()] = i;
42 checkpoint_stack.pop();
43 break;
44 }
45 }
46 }
47
48 return reverted_in;
49}
50
51template <typename EventType, typename ProcessEventFn>
53 ProcessEventFn&& process_event)
54{
55 auto reverted_in = compute_reverted_in_map(events);
56 bool discard = false;
57 std::optional<size_t> waiting_for_revert = std::nullopt;
58
59 for (size_t i = 0; i < events.size(); i++) {
60 const auto& event = events.at(i);
62 auto check_point_event = std::get<simulation::CheckPointEventType>(event);
63 if (check_point_event == simulation::CheckPointEventType::CREATE_CHECKPOINT && reverted_in.contains(i) &&
64 !waiting_for_revert.has_value()) {
65 // This checkpoint will revert in the future: discard all events until the revert.
66 waiting_for_revert = reverted_in.at(i);
67 discard = true;
68 } else if (check_point_event == simulation::CheckPointEventType::REVERT_CHECKPOINT &&
69 waiting_for_revert.has_value() && waiting_for_revert.value() == i) {
70 // We found the revert we were waiting for: stop discarding events.
71 // Note that we ensure that we find exactly the revert we were waiting for and ignore any nested
72 // reverts.
73 waiting_for_revert = std::nullopt;
74 discard = false;
75 }
76 } else {
77 const auto& payload_event = std::get<EventType>(event);
78 process_event(payload_event, discard);
79 }
80 }
81}
82
83} // namespace bb::avm2::tracegen
void process_with_discard(const std::vector< std::variant< EventType, simulation::CheckPointEventType > > &events, ProcessEventFn &&process_event)
std::unordered_map< size_t, size_t > compute_reverted_in_map(const std::vector< EventVariant > &events)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
simulation::PublicDataTreeReadWriteEvent event