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
3#include <memory>
4#include <stack>
5
10
11namespace bb::avm2::fuzzer {
12
14 public:
15 FuzzerContractDB() = default;
16
21 const FunctionSelector& selector) const override;
22
23 void add_contracts(const ContractDeploymentData& contract_deployment_data) override;
24
25 // Direct methods to add contract class and instance
26 void add_contract_class(const ContractClassId& class_id, const ContractClass& contract_class);
27 void add_contract_instance(const AztecAddress& address, const ContractInstance& contract_instance);
28
29 void create_checkpoint() override;
30 void commit_checkpoint() override;
31 void revert_checkpoint() override;
32
33 // Getters for serialization
39
40 private:
42 ContractInstance from_logs(const PrivateLog& log) const;
43
46
47 // Used for serialization keeping track of the order of the contracts and instances
50
56};
57
58// Set up and manage a world state for the fuzzer, the plan is to use this to set up different world states
59// This is a bit of hack since we need to access the world state in both cpp and ts. Normally, ws is instantiated
60// 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
61// 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
62// only one thing will be writing to it at a time.
63// FIXME(ilyas): This won't work with multiple concurrent fuzzing processes, but that's ok for now.
65 public:
66 // Shared constants for C++ and TypeScript to use the same database
67 // Note: TypeScript expects trees in {DATA_DIR}/world_state/, so we include that subdirectory
68 static constexpr const char* DATA_DIR = "/tmp/avm_fuzzer_ws/world_state";
69 static constexpr uint64_t MAP_SIZE_KB = 10240; // 10 MB
70
71 // Static instance management (similar to JsSimulator pattern)
72 static void initialize()
73 {
74 if (instance == nullptr) {
77 }
78 }
79
81 {
82 if (instance == nullptr) {
83 throw std::runtime_error("FuzzerWorldStateManager not initialized. Call initialize() first.");
84 }
85 return instance;
86 }
87
88 void reset_world_state();
90 void write_fee_payer_balance(const AztecAddress& fee_payer, const FF& balance);
91
95
96 void checkpoint() { ws->checkpoint(fork_ids.top()); }
97
98 void commit() { ws->commit_checkpoint(fork_ids.top()); }
99
100 void revert() { ws->revert_checkpoint(fork_ids.top()); }
101
102 static const char* get_data_dir() { return DATA_DIR; }
103
104 static uint64_t get_map_size_kb() { return MAP_SIZE_KB; }
105
106 private:
108
110
112 std::stack<uint64_t> fork_ids;
113};
114
115} // 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:32
const std::vector< ContractClass > & get_contract_classes() const
Definition dbs.hpp:34
void revert_checkpoint() override
Definition dbs.cpp:163
std::unordered_map< ContractClassId, ContractClass > contract_classes
Definition dbs.hpp:44
ContractClass from_logs(const ContractClassLog &log) const
Definition dbs.cpp:93
std::vector< std::pair< AztecAddress, ContractInstance > > contract_instances_vector
Definition dbs.hpp:49
std::vector< ContractClass > contract_classes_vector
Definition dbs.hpp:48
void commit_checkpoint() override
Definition dbs.cpp:156
const std::vector< std::pair< AztecAddress, ContractInstance > > & get_contract_instances() const
Definition dbs.hpp:35
void create_checkpoint() override
Definition dbs.cpp:148
std::optional< FF > get_bytecode_commitment(const ContractClassId &class_id) const override
Definition dbs.cpp:40
std::unordered_map< AztecAddress, ContractInstance > contract_instances
Definition dbs.hpp:45
void add_contract_class(const ContractClassId &class_id, const ContractClass &contract_class)
Definition dbs.cpp:71
void add_contract_instance(const AztecAddress &address, const ContractInstance &contract_instance)
Definition dbs.cpp:82
std::optional< std::string > get_debug_function_name(const AztecAddress &address, const FunctionSelector &selector) const override
Definition dbs.cpp:49
std::stack< Checkpoint > checkpoints
Definition dbs.hpp:55
world_state::WorldState & get_world_state()
Definition dbs.hpp:94
static constexpr const char * DATA_DIR
Definition dbs.hpp:68
std::unique_ptr< world_state::WorldState > ws
Definition dbs.hpp:111
static FuzzerWorldStateManager * getInstance()
Definition dbs.hpp:80
world_state::WorldStateRevision fork()
Definition dbs.cpp:204
void register_contract_address(const AztecAddress &contract_address)
Definition dbs.cpp:218
static constexpr uint64_t MAP_SIZE_KB
Definition dbs.hpp:69
static const char * get_data_dir()
Definition dbs.hpp:102
std::stack< uint64_t > fork_ids
Definition dbs.hpp:112
void write_fee_payer_balance(const AztecAddress &fee_payer, const FF &balance)
Definition dbs.cpp:227
static FuzzerWorldStateManager * instance
FuzzerWorldStateManager methods.
Definition dbs.hpp:107
world_state::WorldStateRevision get_current_revision() const
Definition dbs.cpp:199
Holds the Merkle trees responsible for storing the state of the Aztec protocol.
FF ContractClassId
FF FunctionSelector
AvmFlavorSettings::FF FF
Definition field.hpp:10
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::unordered_map< ContractClassId, ContractClass > contract_classes
Definition dbs.hpp:52
std::unordered_map< AztecAddress, ContractInstance > contract_instances
Definition dbs.hpp:53