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
11
#include "
barretenberg/common/assert.hpp
"
12
#include "
barretenberg/vm2/simulation/events/checkpoint_event_type.hpp
"
13
14
namespace
bb::avm2::tracegen
{
15
33
template
<
typename
EventVariant>
34
std::unordered_map<size_t, size_t>
compute_reverted_in_map
(
const
std::vector<EventVariant>
& events)
35
{
36
// Maps the index of the checkpoint to the index it was reverted in.
37
std::unordered_map<size_t, size_t>
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
47
if
(
std::holds_alternative<simulation::CheckPointEventType>
(
event
)) {
48
switch
(
std::get<simulation::CheckPointEventType>
(
event
)) {
49
case
simulation::CheckPointEventType::CREATE_CHECKPOINT
:
50
checkpoint_stack.push(i);
51
break
;
52
case
simulation::CheckPointEventType::COMMIT_CHECKPOINT
:
53
BB_ASSERT
(!checkpoint_stack.empty());
54
checkpoint_stack.pop();
55
break
;
56
case
simulation::CheckPointEventType::REVERT_CHECKPOINT
:
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
85
template
<
typename
EventType,
typename
ProcessEventFn>
86
void
process_with_discard
(
const
std::vector<
std::variant<EventType, simulation::CheckPointEventType>
>& events,
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);
95
if
(
std::holds_alternative<simulation::CheckPointEventType>
(
event
)) {
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
assert.hpp
BB_ASSERT
#define BB_ASSERT(expression,...)
Definition
assert.hpp:70
checkpoint_event_type.hpp
bb::avm2::discard
Definition
discard.hpp:33
bb::avm2::simulation::COMMIT_CHECKPOINT
@ COMMIT_CHECKPOINT
Definition
checkpoint_event_type.hpp:9
bb::avm2::simulation::REVERT_CHECKPOINT
@ REVERT_CHECKPOINT
Definition
checkpoint_event_type.hpp:10
bb::avm2::simulation::CREATE_CHECKPOINT
@ CREATE_CHECKPOINT
Definition
checkpoint_event_type.hpp:8
bb::avm2::tracegen
Definition
full_row.hpp:9
bb::avm2::tracegen::process_with_discard
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.
Definition
discard_reconstruction.hpp:86
bb::avm2::tracegen::compute_reverted_in_map
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.
Definition
discard_reconstruction.hpp:34
std::get
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition
tuple.hpp:13
event
simulation::PublicDataTreeReadWriteEvent event
Definition
public_data_tree_trace.cpp:24
src
barretenberg
vm2
tracegen
lib
discard_reconstruction.hpp
Generated by
1.9.8