Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
dbs.hpp
Go to the documentation of this file.
1#pragma once
2
8#include <memory>
9#include <stack>
10
11namespace bb::avm2::fuzzer {
12
13// FIXME(ilyas): This is now unused for the fuzzer, but I have a plan to experiment with this later.
15 public:
16 TreeSnapshots get_tree_roots() const override;
17
19
21
22 FF get_leaf_value(simulation::MerkleTreeId tree_id, index_t leaf_index) const override;
23
25
27
29 const PublicDataLeafValue& leaf_value) override;
30
32 const NullifierLeafValue& leaf_value) override;
33
35 std::span<const FF> leaves) override;
36
37 void pad_tree(simulation::MerkleTreeId tree_id, size_t num_leaves) override;
38 void create_checkpoint() override;
39 void commit_checkpoint() override;
40 void revert_checkpoint() override;
41 uint32_t get_checkpoint_id() const override;
42
43 // Some helper functions for the fuzzer to use
45
46 private:
48 const FF& value) const;
49
50 // Stored leaves sorted by value/slots for low-indexed leaf retrieval
53 // Stored leaves with their indices
56 std::vector<FF> note_hash_leaves;
57
58 // Indices
62};
63
65 public:
66 FuzzerContractDB(const std::vector<uint8_t>& bytecode)
68 {}
69
74 const FunctionSelector& selector) const override;
75
76 void add_contracts(const ContractDeploymentData& contract_deployment_data) override;
77
78 void create_checkpoint() override;
79 void commit_checkpoint() override;
80 void revert_checkpoint() override;
81
82 private:
83 std::vector<uint8_t> bytecode;
84};
85
86// Set up and manage a world state for the fuzzer, the plan is to use this to set up different world states
87// This is a bit of hack since we need to access the world state in both cpp and ts. Normally, ws is instantiated
88// inside ts and we use napi to access it from cpp, but for the fuzzer we want to instantiate it in cpp and access it
89// from ts. The simplest way is to use the same database files from both cpp and ts, this is fine for now since we know
90// only one thing will be writing to it at a time.
91// FIXME(ilyas): This won't work with multiple concurrent fuzzing processes, but that's ok for now.
93 public:
94 // Shared constants for C++ and TypeScript to use the same database
95 // Note: TypeScript expects trees in {DATA_DIR}/world_state/, so we include that subdirectory
96 static constexpr const char* DATA_DIR = "/tmp/avm_fuzzer_ws/world_state";
97 static constexpr uint64_t MAP_SIZE_KB = 10240; // 10 MB
98
99 // Static instance management (similar to JsSimulator pattern)
100 static void initialize()
101 {
102 if (instance == nullptr) {
105 }
106 }
107
109 {
110 if (instance == nullptr) {
111 throw std::runtime_error("FuzzerWorldStateManager not initialized. Call initialize() first.");
112 }
113 return instance;
114 }
115
116 void reset_world_state();
118
122
123 void checkpoint() { ws->checkpoint(fork_ids.top()); }
124
125 void commit() { ws->commit_checkpoint(fork_ids.top()); }
126
127 void revert() { ws->revert_checkpoint(fork_ids.top()); }
128
129 static const char* get_data_dir() { return DATA_DIR; }
130
131 static uint64_t get_map_size_kb() { return MAP_SIZE_KB; }
132
133 private:
135
137
139 std::stack<uint64_t> fork_ids;
140};
141
142} // namespace bb::avm2::fuzzer
std::shared_ptr< Napi::ThreadSafeFunction > add_contracts
std::optional< ContractClass > get_contract_class(const ContractClassId &class_id) const override
Definition dbs.cpp:208
void revert_checkpoint() override
Definition dbs.cpp:238
FuzzerContractDB(const std::vector< uint8_t > &bytecode)
Definition dbs.hpp:66
void commit_checkpoint() override
Definition dbs.cpp:237
void create_checkpoint() override
Definition dbs.cpp:236
std::optional< FF > get_bytecode_commitment(const ContractClassId &class_id) const override
Definition dbs.cpp:220
std::optional< std::string > get_debug_function_name(const AztecAddress &address, const FunctionSelector &selector) const override
Definition dbs.cpp:224
std::vector< uint8_t > bytecode
Definition dbs.hpp:83
simulation::SequentialInsertionResult< NullifierLeafValue > insert_indexed_leaves_nullifier_tree(const NullifierLeafValue &leaf_value) override
Definition dbs.cpp:142
std::vector< std::pair< FF, index_t > > public_data_slots
Definition dbs.hpp:52
uint32_t get_checkpoint_id() const override
Definition dbs.cpp:173
TreeSnapshots get_tree_roots() const override
Definition dbs.cpp:15
std::unordered_map< index_t, PublicDataLeafValue > public_data_leaves
Definition dbs.hpp:55
std::vector< FF > note_hash_leaves
Definition dbs.hpp:56
void create_checkpoint() override
Definition dbs.cpp:170
uint64_t next_available_public_data_index
Definition dbs.hpp:60
void pad_tree(simulation::MerkleTreeId tree_id, size_t num_leaves) override
Definition dbs.cpp:168
std::vector< simulation::AppendLeafResult > append_leaves(simulation::MerkleTreeId tree_id, std::span< const FF > leaves) override
Definition dbs.cpp:161
simulation::SiblingPath get_sibling_path(simulation::MerkleTreeId tree_id, index_t leaf_index) const override
Definition dbs.cpp:25
std::unordered_map< index_t, NullifierLeafValue > nullifier_leaves
Definition dbs.hpp:54
FF get_leaf_value(simulation::MerkleTreeId tree_id, index_t leaf_index) const override
Definition dbs.cpp:64
std::vector< std::pair< FF, index_t > > nullifier_values
Definition dbs.hpp:51
void revert_checkpoint() override
Definition dbs.cpp:172
void commit_checkpoint() override
Definition dbs.cpp:171
GetLowIndexedLeafResponse get_low_indexed_leaf(simulation::MerkleTreeId tree_id, const FF &value) const override
Definition dbs.cpp:46
simulation::IndexedLeaf< PublicDataLeafValue > get_leaf_preimage_public_data_tree(index_t leaf_index) const override
Definition dbs.cpp:78
simulation::SequentialInsertionResult< PublicDataLeafValue > insert_indexed_leaves_public_data_tree(const PublicDataLeafValue &leaf_value) override
Definition dbs.cpp:123
void insert_contract_address(const AztecAddress &contract_address)
Definition dbs.cpp:179
simulation::IndexedLeaf< NullifierLeafValue > get_leaf_preimage_nullifier_tree(index_t leaf_index) const override
Definition dbs.cpp:100
std::pair< FF, index_t > get_indexed_low_leaf_helper(const std::vector< std::pair< FF, index_t > > &value_sorted_leaves, const FF &value) const
Definition dbs.cpp:31
world_state::WorldState & get_world_state()
Definition dbs.hpp:121
static constexpr const char * DATA_DIR
Definition dbs.hpp:96
std::unique_ptr< world_state::WorldState > ws
Definition dbs.hpp:138
static FuzzerWorldStateManager * getInstance()
Definition dbs.hpp:108
world_state::WorldStateRevision fork()
Definition dbs.cpp:272
void register_contract_address(const AztecAddress &contract_address)
Definition dbs.cpp:286
static constexpr uint64_t MAP_SIZE_KB
Definition dbs.hpp:97
static const char * get_data_dir()
Definition dbs.hpp:129
std::stack< uint64_t > fork_ids
Definition dbs.hpp:139
static FuzzerWorldStateManager * instance
FuzzerWorldStateManager methods.
Definition dbs.hpp:134
world_state::WorldStateRevision get_current_revision() const
Definition dbs.cpp:267
Holds the Merkle trees responsible for storing the state of the Aztec protocol.
::bb::crypto::merkle_tree::fr_sibling_path SiblingPath
Definition db.hpp:36
::bb::crypto::merkle_tree::NullifierLeafValue NullifierLeafValue
Definition db.hpp:39
::bb::crypto::merkle_tree::PublicDataLeafValue PublicDataLeafValue
Definition db.hpp:38
::bb::crypto::merkle_tree::index_t index_t
Definition db.hpp:37
FF ContractClassId
FF FunctionSelector
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13