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