Skip to content

Commit 7415bd3

Browse files
committed
feat: remake db getters const, add temp ref structs, cleanup
1 parent 9370939 commit 7415bd3

File tree

12 files changed

+142
-107
lines changed

12 files changed

+142
-107
lines changed

barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/concrete_dbs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace bb::avm2::simulation {
88

99
// Contracts DB starts.
10-
std::optional<ContractInstance> ContractDB::get_contract_instance(const AztecAddress& address)
10+
std::optional<ContractInstance> ContractDB::get_contract_instance(const AztecAddress& address) const
1111
{
1212
std::optional<ContractInstance> instance = raw_contract_db.get_contract_instance(address);
1313
// If we didn't get a contract instance, we don't prove anything.
@@ -26,7 +26,7 @@ std::optional<ContractInstance> ContractDB::get_contract_instance(const AztecAdd
2626
return instance;
2727
}
2828

29-
std::optional<ContractClass> ContractDB::get_contract_class(const ContractClassId& class_id)
29+
std::optional<ContractClass> ContractDB::get_contract_class(const ContractClassId& class_id) const
3030
{
3131
std::optional<ContractClass> klass = raw_contract_db.get_contract_class(class_id);
3232
// If we didn't get a contract class, we don't prove anything.

barretenberg/cpp/src/barretenberg/vm2/simulation/gadgets/concrete_dbs.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ class ContractDB final : public ContractDBInterface {
3030
// Gets an instance from the DB and proves address derivation from the result.
3131
// This does NOT prove that the address is in the nullifier tree.
3232
// Silo the address and use the MerkleDB to prove that.
33-
std::optional<ContractInstance> get_contract_instance(const AztecAddress& address) override;
33+
std::optional<ContractInstance> get_contract_instance(const AztecAddress& address) const override;
3434
// Gets a class from the DB and proves class id derivation from the result.
3535
// This does NOT prove that the class id is in the nullifier tree.
3636
// Silo the class id and use the MerkleDB to prove that.
37-
std::optional<ContractClass> get_contract_class(const ContractClassId& class_id) override;
37+
std::optional<ContractClass> get_contract_class(const ContractClassId& class_id) const override;
3838

3939
private:
4040
ContractDBInterface& raw_contract_db;

barretenberg/cpp/src/barretenberg/vm2/simulation/interfaces/db.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class ContractDBInterface {
1818
public:
1919
virtual ~ContractDBInterface() = default;
2020

21-
virtual std::optional<ContractInstance> get_contract_instance(const AztecAddress& address) = 0;
22-
virtual std::optional<ContractClass> get_contract_class(const ContractClassId& class_id) = 0;
21+
virtual std::optional<ContractInstance> get_contract_instance(const AztecAddress& address) const = 0;
22+
virtual std::optional<ContractClass> get_contract_class(const ContractClassId& class_id) const = 0;
2323
};
2424

2525
// Aliases.
@@ -45,13 +45,13 @@ class LowLevelMerkleDBInterface {
4545

4646
virtual TreeSnapshots get_tree_roots() const = 0;
4747

48-
virtual SiblingPath get_sibling_path(MerkleTreeId tree_id, index_t leaf_index) = 0;
49-
virtual GetLowIndexedLeafResponse get_low_indexed_leaf(MerkleTreeId tree_id, const FF& value) = 0;
48+
virtual SiblingPath get_sibling_path(MerkleTreeId tree_id, index_t leaf_index) const = 0;
49+
virtual GetLowIndexedLeafResponse get_low_indexed_leaf(MerkleTreeId tree_id, const FF& value) const = 0;
5050
// Returns the value if it exists, 0 otherwise.
51-
virtual FF get_leaf_value(MerkleTreeId tree_id, index_t leaf_index) = 0;
51+
virtual FF get_leaf_value(MerkleTreeId tree_id, index_t leaf_index) const = 0;
5252
// We don't template the preimage methods because templated methods cannot be virtual.
53-
virtual IndexedLeaf<PublicDataLeafValue> get_leaf_preimage_public_data_tree(index_t leaf_index) = 0;
54-
virtual IndexedLeaf<NullifierLeafValue> get_leaf_preimage_nullifier_tree(index_t leaf_index) = 0;
53+
virtual IndexedLeaf<PublicDataLeafValue> get_leaf_preimage_public_data_tree(index_t leaf_index) const = 0;
54+
virtual IndexedLeaf<NullifierLeafValue> get_leaf_preimage_nullifier_tree(index_t leaf_index) const = 0;
5555

5656
virtual SequentialInsertionResult<PublicDataLeafValue> insert_indexed_leaves_public_data_tree(
5757
const PublicDataLeafValue& leaf_value) = 0;

barretenberg/cpp/src/barretenberg/vm2/simulation/lib/db_types.hpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,39 @@
33
#include "barretenberg/crypto/merkle_tree/indexed_tree/indexed_leaf.hpp"
44
#include "barretenberg/crypto/merkle_tree/types.hpp"
55
#include "barretenberg/vm2/common/aztec_types.hpp"
6+
#include "barretenberg/vm2/common/map.hpp"
7+
#include "barretenberg/vm2/simulation/interfaces/db.hpp"
68

79
namespace bb::avm2::simulation {
810

911
using NullifierTreeLeafPreimage = crypto::merkle_tree::IndexedLeaf<crypto::merkle_tree::NullifierLeafValue>;
1012
using PublicDataTreeLeafPreimage = crypto::merkle_tree::IndexedLeaf<crypto::merkle_tree::PublicDataLeafValue>;
1113

14+
// Keys for hints stored in unordered_flat_maps, used in raw_data_dbs and hinting_dbs.
15+
using GetSiblingPathKey = std::tuple<AppendOnlyTreeSnapshot, MerkleTreeId, index_t>;
16+
using GetPreviousValueIndexKey = std::tuple<AppendOnlyTreeSnapshot, MerkleTreeId, FF>;
17+
using GetLeafPreimageKey = std::tuple<AppendOnlyTreeSnapshot, index_t>;
18+
using GetLeafValueKey = std::tuple<AppendOnlyTreeSnapshot, MerkleTreeId, index_t>;
19+
using SequentialInsertHintPublicDataTreeKey = std::tuple<AppendOnlyTreeSnapshot, MerkleTreeId, PublicDataLeafValue>;
20+
using SequentialInsertHintNullifierTreeKey = std::tuple<AppendOnlyTreeSnapshot, MerkleTreeId, NullifierLeafValue>;
21+
using AppendLeavesHintKey = std::tuple<AppendOnlyTreeSnapshot, MerkleTreeId, std::vector<FF>>;
22+
23+
// TODO(MW): Temp struct for query hints to allow using a ref in the HintingContractsDB class constructor
24+
struct MappedContractHints {
25+
unordered_flat_map<AztecAddress, ContractInstanceHint> contract_instances;
26+
unordered_flat_map<ContractClassId, ContractClassHint> contract_classes;
27+
unordered_flat_map<ContractClassId, BytecodeCommitmentHint> bytecode_commitments;
28+
};
29+
30+
// TODO(MW): Temp struct for query hints to allow using a ref in the HintingRawDB class constructor
31+
struct MappedQueryHints {
32+
unordered_flat_map<GetSiblingPathKey, SiblingPath> get_sibling_path_hints;
33+
unordered_flat_map<GetPreviousValueIndexKey, GetLowIndexedLeafResponse> get_previous_value_index_hints;
34+
unordered_flat_map<GetLeafPreimageKey, IndexedLeaf<PublicDataLeafValue>> get_leaf_preimage_hints_public_data_tree;
35+
unordered_flat_map<GetLeafPreimageKey, IndexedLeaf<NullifierLeafValue>> get_leaf_preimage_hints_nullifier_tree;
36+
unordered_flat_map<GetLeafValueKey, FF> get_leaf_value_hints;
37+
};
38+
1239
struct TreeCounters {
1340
uint32_t note_hash_counter;
1441
uint32_t nullifier_counter;

barretenberg/cpp/src/barretenberg/vm2/simulation/lib/hinting_dbs.cpp

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "barretenberg/vm2/simulation/lib/hinting_dbs.hpp"
22

3-
#include <cassert>
43
#include <cstdint>
54
#include <optional>
65
#include <span>
@@ -10,10 +9,8 @@
109

1110
#include "barretenberg/common/bb_bench.hpp"
1211
#include "barretenberg/common/log.hpp"
13-
#include "barretenberg/crypto/merkle_tree/indexed_tree/indexed_leaf.hpp"
1412
#include "barretenberg/vm2/common/avm_inputs.hpp"
1513
#include "barretenberg/vm2/common/aztec_types.hpp"
16-
#include "barretenberg/vm2/generated/relations/lookups_contract_instance_retrieval.hpp"
1714
#include "barretenberg/vm2/simulation/interfaces/db.hpp"
1815
#include "barretenberg/vm2/simulation/lib/contract_crypto.hpp"
1916

@@ -41,15 +38,15 @@ auto& get_tree_info_helper(world_state::MerkleTreeId tree_id, const auto& tree_r
4138
} // namespace
4239

4340
// HintingContractsDB starts.
44-
std::optional<ContractInstance> HintingContractsDB::get_contract_instance(const AztecAddress& address)
41+
std::optional<ContractInstance> HintingContractsDB::get_contract_instance(const AztecAddress& address) const
4542
{
4643
info("HintingContractsDB get_contract_instance");
4744
auto instance = db.get_contract_instance(address);
4845
// If we don't find the instance hint, this is not a catastrophic failure. The inner db should handle it, and
4946
// here we simply don't store any hint:
5047
if (instance.has_value()) {
5148
// TODO(MW): Use/write instance to hint methods for ContractInstance, PublicKeys, ContractClass, etc.
52-
contract_instances[address] = ContractInstanceHint{
49+
mapped_hints.contract_instances[address] = ContractInstanceHint{
5350
.address = address,
5451
.salt = instance->salt,
5552
.deployer = instance->deployer_addr,
@@ -67,23 +64,23 @@ std::optional<ContractInstance> HintingContractsDB::get_contract_instance(const
6764
return instance;
6865
}
6966

70-
std::optional<ContractClass> HintingContractsDB::get_contract_class(const ContractClassId& class_id)
67+
std::optional<ContractClass> HintingContractsDB::get_contract_class(const ContractClassId& class_id) const
7168
{
7269
info("HintingContractsDB get_contract_class");
7370
auto klass = db.get_contract_class(class_id);
7471
// If we don't find the instance hint, this is not a catastrophic failure. The inner db should handle it, and
7572
// here we simply don't store any hint:
7673
if (klass.has_value()) {
7774
// TODO(MW): Use/write instance to hint methods for ContractInstance, PublicKeys, ContractClass, etc.
78-
contract_classes[class_id] = ContractClassHint{
75+
mapped_hints.contract_classes[class_id] = ContractClassHint{
7976
.classId = class_id,
8077
.artifactHash = klass->artifact_hash,
8178
.privateFunctionsRoot = klass->private_function_root,
8279
.packedBytecode = klass->packed_bytecode,
8380
};
8481
// Note: HintedRawContractDB accesses the bytecode commitment 'hint' during get_contract_class, so following
8582
// same logic here:
86-
bytecode_commitments[class_id] =
83+
mapped_hints.bytecode_commitments[class_id] =
8784
BytecodeCommitmentHint{ .classId = class_id, .commitment = klass->public_bytecode_commitment };
8885
}
8986

@@ -93,13 +90,13 @@ std::optional<ContractClass> HintingContractsDB::get_contract_class(const Contra
9390
void HintingContractsDB::dump_hints(ExecutionHints& hints)
9491
{
9592
// TODO(MW): better way than to iterate? do we want push_back?
96-
for (const auto& contract_instance : contract_instances) {
93+
for (const auto& contract_instance : mapped_hints.contract_instances) {
9794
hints.contractInstances.push_back(contract_instance.second);
9895
}
99-
for (const auto& contract_class : contract_classes) {
96+
for (const auto& contract_class : mapped_hints.contract_classes) {
10097
hints.contractClasses.push_back(contract_class.second);
10198
}
102-
for (const auto& bytecode_commitment : bytecode_commitments) {
99+
for (const auto& bytecode_commitment : mapped_hints.bytecode_commitments) {
103100
hints.bytecodeCommitments.push_back(bytecode_commitment.second);
104101
}
105102
}
@@ -110,61 +107,61 @@ const AppendOnlyTreeSnapshot& HintingRawDB::get_tree_info(world_state::MerkleTre
110107
return get_tree_info_helper(tree_id, db.get_tree_roots());
111108
}
112109

113-
SiblingPath HintingRawDB::get_sibling_path(world_state::MerkleTreeId tree_id, index_t leaf_index)
110+
SiblingPath HintingRawDB::get_sibling_path(world_state::MerkleTreeId tree_id, index_t leaf_index) const
114111
{
115112
info("HintingRawDB get_sib: ", leaf_index);
116113
auto tree_info = get_tree_info(tree_id);
117114
auto path = db.get_sibling_path(tree_id, leaf_index);
118115
GetSiblingPathKey key = { tree_info, tree_id, leaf_index };
119-
get_sibling_path_hints[key] = path;
116+
query_hints.get_sibling_path_hints[key] = path;
120117

121118
return path;
122119
}
123120

124-
GetLowIndexedLeafResponse HintingRawDB::get_low_indexed_leaf(world_state::MerkleTreeId tree_id, const FF& value)
121+
GetLowIndexedLeafResponse HintingRawDB::get_low_indexed_leaf(world_state::MerkleTreeId tree_id, const FF& value) const
125122
{
126123
info("HintingRawDB get_low_indexed_leaf");
127124
auto tree_info = get_tree_info(tree_id);
128125
auto resp = db.get_low_indexed_leaf(tree_id, value);
129126
GetPreviousValueIndexKey key = { tree_info, tree_id, value };
130-
get_previous_value_index_hints[key] = { resp.is_already_present, resp.index };
127+
query_hints.get_previous_value_index_hints[key] = { resp.is_already_present, resp.index };
131128
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
132129
get_sibling_path(tree_id, resp.index);
133130
return resp;
134131
}
135132

136-
FF HintingRawDB::get_leaf_value(world_state::MerkleTreeId tree_id, index_t leaf_index)
133+
FF HintingRawDB::get_leaf_value(world_state::MerkleTreeId tree_id, index_t leaf_index) const
137134
{
138135
info("HintingRawDB get_leaf_value");
139136
auto tree_info = get_tree_info(tree_id);
140137
auto value = db.get_leaf_value(tree_id, leaf_index);
141138
GetLeafValueKey key = { tree_info, tree_id, leaf_index };
142-
get_leaf_value_hints[key] = value;
139+
query_hints.get_leaf_value_hints[key] = value;
143140
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
144141
get_sibling_path(tree_id, leaf_index);
145142
return value;
146143
}
147144

148-
IndexedLeaf<PublicDataLeafValue> HintingRawDB::get_leaf_preimage_public_data_tree(index_t leaf_index)
145+
IndexedLeaf<PublicDataLeafValue> HintingRawDB::get_leaf_preimage_public_data_tree(index_t leaf_index) const
149146
{
150147
info("HintingRawDB get_leaf_preimage_public_data_tree");
151148
auto tree_info = get_tree_info(world_state::MerkleTreeId::PUBLIC_DATA_TREE);
152149
auto preimage = db.get_leaf_preimage_public_data_tree(leaf_index);
153150

154151
GetLeafPreimageKey key = { tree_info, leaf_index };
155-
get_leaf_preimage_hints_public_data_tree[key] = preimage;
152+
query_hints.get_leaf_preimage_hints_public_data_tree[key] = preimage;
156153
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
157154
get_sibling_path(world_state::MerkleTreeId::PUBLIC_DATA_TREE, leaf_index);
158155
return preimage;
159156
}
160157

161-
IndexedLeaf<NullifierLeafValue> HintingRawDB::get_leaf_preimage_nullifier_tree(index_t leaf_index)
158+
IndexedLeaf<NullifierLeafValue> HintingRawDB::get_leaf_preimage_nullifier_tree(index_t leaf_index) const
162159
{
163160
info("HintingRawDB get_leaf_preimage_nullifier_tree");
164161
auto tree_info = get_tree_info(world_state::MerkleTreeId::NULLIFIER_TREE);
165162
auto preimage = db.get_leaf_preimage_nullifier_tree(leaf_index);
166163
GetLeafPreimageKey key = { tree_info, leaf_index };
167-
get_leaf_preimage_hints_nullifier_tree[key] = preimage;
164+
query_hints.get_leaf_preimage_hints_nullifier_tree[key] = preimage;
168165
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
169166
get_sibling_path(world_state::MerkleTreeId::NULLIFIER_TREE, leaf_index);
170167
return preimage;
@@ -320,12 +317,12 @@ AppendOnlyTreeSnapshot HintingRawDB::appendLeafInternal(AppendOnlyTreeSnapshot s
320317
void HintingRawDB::dump_hints(ExecutionHints& hints)
321318
{
322319
// TODO(MW): better way than to iterate? do we want push_back?
323-
for (const auto& get_sibling_path_hint : get_sibling_path_hints) {
320+
for (const auto& get_sibling_path_hint : query_hints.get_sibling_path_hints) {
324321
auto [hint_key, tree_id, index] = get_sibling_path_hint.first;
325322
hints.getSiblingPathHints.push_back(GetSiblingPathHint{
326323
.hintKey = hint_key, .treeId = tree_id, .index = index, .path = get_sibling_path_hint.second });
327324
}
328-
for (const auto& get_previous_value_index_hint : get_previous_value_index_hints) {
325+
for (const auto& get_previous_value_index_hint : query_hints.get_previous_value_index_hints) {
329326
auto [hint_key, tree_id, value] = get_previous_value_index_hint.first;
330327
hints.getPreviousValueIndexHints.push_back(GetPreviousValueIndexHint{
331328
.hintKey = hint_key,
@@ -335,17 +332,17 @@ void HintingRawDB::dump_hints(ExecutionHints& hints)
335332
.alreadyPresent = get_previous_value_index_hint.second.is_already_present,
336333
});
337334
}
338-
for (const auto& get_leaf_preimage_hint : get_leaf_preimage_hints_public_data_tree) {
335+
for (const auto& get_leaf_preimage_hint : query_hints.get_leaf_preimage_hints_public_data_tree) {
339336
auto [hint_key, index] = get_leaf_preimage_hint.first;
340337
hints.getLeafPreimageHintsPublicDataTree.push_back(
341338
{ .hintKey = hint_key, .index = index, .leafPreimage = get_leaf_preimage_hint.second });
342339
}
343-
for (const auto& get_leaf_preimage_hint : get_leaf_preimage_hints_nullifier_tree) {
340+
for (const auto& get_leaf_preimage_hint : query_hints.get_leaf_preimage_hints_nullifier_tree) {
344341
auto [hint_key, index] = get_leaf_preimage_hint.first;
345342
hints.getLeafPreimageHintsNullifierTree.push_back(
346343
{ .hintKey = hint_key, .index = index, .leafPreimage = get_leaf_preimage_hint.second });
347344
}
348-
for (const auto& get_leaf_value_hint : get_leaf_value_hints) {
345+
for (const auto& get_leaf_value_hint : query_hints.get_leaf_value_hints) {
349346
auto [hint_key, tree_id, index] = get_leaf_value_hint.first;
350347
hints.getLeafValueHints.push_back(GetLeafValueHint{
351348
.hintKey = hint_key, .treeId = tree_id, .index = index, .value = get_leaf_value_hint.second });

0 commit comments

Comments
 (0)