Skip to content

Commit 3361dc7

Browse files
committed
feat: use transform in dump_hints, use mutable, cleanup checked todos
1 parent db22b6d commit 3361dc7

File tree

3 files changed

+79
-71
lines changed

3 files changed

+79
-71
lines changed

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

Lines changed: 73 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,17 @@ std::optional<ContractClass> HintingContractsDB::get_contract_class(const Contra
8686

8787
void HintingContractsDB::dump_hints(ExecutionHints& hints)
8888
{
89-
// TODO(MW): better way than to iterate? do we want push_back?
90-
for (const auto& contract_instance : contract_hints.contract_instances) {
91-
hints.contractInstances.push_back(contract_instance.second);
92-
}
93-
for (const auto& contract_class : contract_hints.contract_classes) {
94-
hints.contractClasses.push_back(contract_class.second);
95-
}
96-
for (const auto& bytecode_commitment : contract_hints.bytecode_commitments) {
97-
hints.bytecodeCommitments.push_back(bytecode_commitment.second);
98-
}
89+
std::ranges::transform(contract_hints.contract_instances,
90+
std::back_inserter(hints.contractInstances),
91+
[](const auto& mapped_contract_instance) { return mapped_contract_instance.second; });
92+
93+
std::ranges::transform(contract_hints.contract_classes,
94+
std::back_inserter(hints.contractClasses),
95+
[](const auto& mapped_contract_class) { return mapped_contract_class.second; });
96+
97+
std::ranges::transform(contract_hints.bytecode_commitments,
98+
std::back_inserter(hints.bytecodeCommitments),
99+
[](const auto& mapped_bytecode_commitment) { return mapped_bytecode_commitment.second; });
99100
}
100101

101102
// Hinting MerkleDB starts.
@@ -128,21 +129,18 @@ GetLowIndexedLeafResponse HintingRawDB::get_low_indexed_leaf(world_state::Merkle
128129
.alreadyPresent = resp.is_already_present,
129130
};
130131

131-
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
132+
// Note: We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
132133
get_sibling_path(tree_id, resp.index);
133134

134135
if (tree_id == world_state::MerkleTreeId::NULLIFIER_TREE) {
135-
// TODO(MW): We may need a GetLeafPreimageHint for the nullifier tree when calling nullifier_exists, so collect
136-
// it
137-
// in case.
138-
// NB: The PureMerkleDB does not perform this, but the nullifier check gadget requires a leaf preimage. Ts
139-
// gathers the hint: (state_manager -> checkNullifierExists() -> doMerkleOperations -> public_db_sources ->
136+
// Note: We may need a GetLeafPreimageHint for the nullifier tree when calling nullifier_exists, so collect it
137+
// in case. NB: The PureMerkleDB does not perform this, but the nullifier check gadget requires a leaf preimage.
138+
// Ts gathers the hint: (state_manager -> checkNullifierExists() -> doMerkleOperations -> public_db_sources ->
140139
// checkNullifierExists())
141140
get_leaf_preimage_nullifier_tree(resp.index);
142141
} else if ((tree_id == world_state::MerkleTreeId::PUBLIC_DATA_TREE) && (!resp.is_already_present)) {
143-
// TODO(MW): We may need a GetLeafPreimageHint for the public data tree when calling storage_read, so collect it
144-
// in case.
145-
// NB: The PureMerkleDB does not perform this if !is_already_present, but MerkleDB and ts perform it
142+
// Note: We may need a GetLeafPreimageHint for the public data tree when calling storage_read, so collect it in
143+
// case. NB: The PureMerkleDB does not perform this if !is_already_present, but MerkleDB and ts perform it
146144
// unconditionally. Ts gathers the hint: (public_db_sources -> storageRead())
147145
get_leaf_preimage_public_data_tree(resp.index);
148146
}
@@ -156,7 +154,7 @@ FF HintingRawDB::get_leaf_value(world_state::MerkleTreeId tree_id, index_t leaf_
156154
GetLeafValueKey key = { tree_info, tree_id, leaf_index };
157155
merkle_hints.get_leaf_value_hints[key] =
158156
GetLeafValueHint{ .hintKey = tree_info, .treeId = tree_id, .index = leaf_index, .value = value };
159-
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
157+
// Note: We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
160158
get_sibling_path(tree_id, leaf_index);
161159
return value;
162160
}
@@ -170,7 +168,7 @@ IndexedLeaf<PublicDataLeafValue> HintingRawDB::get_leaf_preimage_public_data_tre
170168
merkle_hints.get_leaf_preimage_hints_public_data_tree[key] = GetLeafPreimageHint<PublicDataTreeLeafPreimage>{
171169
.hintKey = tree_info, .index = leaf_index, .leafPreimage = preimage
172170
};
173-
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
171+
// Note: We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
174172
get_sibling_path(world_state::MerkleTreeId::PUBLIC_DATA_TREE, leaf_index);
175173
return preimage;
176174
}
@@ -183,7 +181,7 @@ IndexedLeaf<NullifierLeafValue> HintingRawDB::get_leaf_preimage_nullifier_tree(i
183181
merkle_hints.get_leaf_preimage_hints_nullifier_tree[key] = GetLeafPreimageHint<NullifierTreeLeafPreimage>{
184182
.hintKey = tree_info, .index = leaf_index, .leafPreimage = preimage
185183
};
186-
// TODO(MW): We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
184+
// Note: We may need a sibling path hint so must collect it in case - see comments in public_db_sources.ts
187185
get_sibling_path(world_state::MerkleTreeId::NULLIFIER_TREE, leaf_index);
188186
return preimage;
189187
}
@@ -335,40 +333,59 @@ AppendOnlyTreeSnapshot HintingRawDB::appendLeafInternal(AppendOnlyTreeSnapshot s
335333

336334
void HintingRawDB::dump_hints(ExecutionHints& hints)
337335
{
338-
// TODO(MW): better way than to iterate? do we want push_back?
339-
for (const auto& get_sibling_path_hint : merkle_hints.get_sibling_path_hints) {
340-
hints.getSiblingPathHints.push_back(get_sibling_path_hint.second);
341-
}
342-
for (const auto& get_previous_value_index_hint : merkle_hints.get_previous_value_index_hints) {
343-
hints.getPreviousValueIndexHints.push_back(get_previous_value_index_hint.second);
344-
}
345-
for (const auto& get_leaf_preimage_hint : merkle_hints.get_leaf_preimage_hints_public_data_tree) {
346-
hints.getLeafPreimageHintsPublicDataTree.push_back(get_leaf_preimage_hint.second);
347-
}
348-
for (const auto& get_leaf_preimage_hint : merkle_hints.get_leaf_preimage_hints_nullifier_tree) {
349-
hints.getLeafPreimageHintsNullifierTree.push_back(get_leaf_preimage_hint.second);
350-
}
351-
for (const auto& get_leaf_value_hint : merkle_hints.get_leaf_value_hints) {
352-
hints.getLeafValueHints.push_back(get_leaf_value_hint.second);
353-
}
354-
for (const auto& sequential_insert_hint : merkle_hints.sequential_insert_hints_public_data_tree) {
355-
hints.sequentialInsertHintsPublicDataTree.push_back(sequential_insert_hint.second);
356-
}
357-
for (const auto& sequential_insert_hint : merkle_hints.sequential_insert_hints_nullifier_tree) {
358-
hints.sequentialInsertHintsNullifierTree.push_back(sequential_insert_hint.second);
359-
}
360-
for (const auto& append_leaves_hint : merkle_hints.append_leaves_hints) {
361-
hints.appendLeavesHints.push_back(append_leaves_hint.second);
362-
}
363-
for (const auto& create_checkpoint_hint : create_checkpoint_hints) {
364-
hints.createCheckpointHints.push_back(create_checkpoint_hint.second);
365-
}
366-
for (const auto& commit_checkpoint_hint : commit_checkpoint_hints) {
367-
hints.commitCheckpointHints.push_back(commit_checkpoint_hint.second);
368-
}
369-
for (const auto& revert_checkpoint_hint : revert_checkpoint_hints) {
370-
hints.revertCheckpointHints.push_back(revert_checkpoint_hint.second);
371-
}
336+
337+
std::ranges::transform(
338+
merkle_hints.get_sibling_path_hints,
339+
std::back_inserter(hints.getSiblingPathHints),
340+
[](const auto& mapped_get_sibling_path_hint) { return mapped_get_sibling_path_hint.second; });
341+
342+
std::ranges::transform(
343+
merkle_hints.get_previous_value_index_hints,
344+
std::back_inserter(hints.getPreviousValueIndexHints),
345+
[](const auto& mapped_get_previous_value_index_hint) { return mapped_get_previous_value_index_hint.second; });
346+
347+
std::ranges::transform(
348+
merkle_hints.get_leaf_preimage_hints_public_data_tree,
349+
std::back_inserter(hints.getLeafPreimageHintsPublicDataTree),
350+
[](const auto& mapped_get_leaf_preimage_hint) { return mapped_get_leaf_preimage_hint.second; });
351+
352+
std::ranges::transform(
353+
merkle_hints.get_leaf_preimage_hints_nullifier_tree,
354+
std::back_inserter(hints.getLeafPreimageHintsNullifierTree),
355+
[](const auto& mapped_get_leaf_preimage_hint) { return mapped_get_leaf_preimage_hint.second; });
356+
357+
std::ranges::transform(merkle_hints.get_leaf_value_hints,
358+
std::back_inserter(hints.getLeafValueHints),
359+
[](const auto& mapped_get_leaf_value_hint) { return mapped_get_leaf_value_hint.second; });
360+
361+
std::ranges::transform(
362+
merkle_hints.sequential_insert_hints_public_data_tree,
363+
std::back_inserter(hints.sequentialInsertHintsPublicDataTree),
364+
[](const auto& mapped_sequential_insert_hint) { return mapped_sequential_insert_hint.second; });
365+
366+
std::ranges::transform(
367+
merkle_hints.sequential_insert_hints_nullifier_tree,
368+
std::back_inserter(hints.sequentialInsertHintsNullifierTree),
369+
[](const auto& mapped_sequential_insert_hint) { return mapped_sequential_insert_hint.second; });
370+
371+
std::ranges::transform(merkle_hints.append_leaves_hints,
372+
std::back_inserter(hints.appendLeavesHints),
373+
[](const auto& mapped_append_leaves_hint) { return mapped_append_leaves_hint.second; });
374+
375+
std::ranges::transform(
376+
create_checkpoint_hints,
377+
std::back_inserter(hints.createCheckpointHints),
378+
[](const auto& mapped_create_checkpoint_hint) { return mapped_create_checkpoint_hint.second; });
379+
380+
std::ranges::transform(
381+
commit_checkpoint_hints,
382+
std::back_inserter(hints.commitCheckpointHints),
383+
[](const auto& mapped_commit_checkpoint_hint) { return mapped_commit_checkpoint_hint.second; });
384+
385+
std::ranges::transform(
386+
revert_checkpoint_hints,
387+
std::back_inserter(hints.revertCheckpointHints),
388+
[](const auto& mapped_revert_checkpoint_hint) { return mapped_revert_checkpoint_hint.second; });
372389
}
373390

374391
} // namespace bb::avm2::simulation

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ namespace bb::avm2::simulation {
2121

2222
class HintingContractsDB final : public ContractDBInterface {
2323
public:
24-
HintingContractsDB(ContractDBInterface& db, MappedContractHints& contract_hints)
24+
HintingContractsDB(ContractDBInterface& db)
2525
: db(db)
26-
, contract_hints(contract_hints)
2726
{}
2827

2928
std::optional<ContractInstance> get_contract_instance(const AztecAddress& address) const override;
@@ -34,16 +33,13 @@ class HintingContractsDB final : public ContractDBInterface {
3433

3534
private:
3635
ContractDBInterface& db;
37-
// TODO(MW): Just here so we can use a ref to query hints and not remove const from getters
38-
MappedContractHints& contract_hints;
36+
mutable MappedContractHints contract_hints;
3937
};
4038

4139
class HintingRawDB final : public LowLevelMerkleDBInterface {
4240
public:
43-
HintingRawDB(LowLevelMerkleDBInterface& db, MappedMerkleHints& merkle_hints)
41+
HintingRawDB(LowLevelMerkleDBInterface& db)
4442
: db(db)
45-
, merkle_hints(
46-
merkle_hints) // TODO(MW): Just here so we can use a ref to query hints and not remove const from getters
4743
{}
4844

4945
TreeSnapshots get_tree_roots() const override { return db.get_tree_roots(); }
@@ -75,9 +71,7 @@ class HintingRawDB final : public LowLevelMerkleDBInterface {
7571
LowLevelMerkleDBInterface& db;
7672
uint32_t checkpoint_action_counter = 0;
7773

78-
// Query hints.
79-
// TODO(MW): Just here so we can use a ref to query hints and not remove const from getters:
80-
MappedMerkleHints& merkle_hints;
74+
mutable MappedMerkleHints merkle_hints;
8175
unordered_flat_map</*action_counter*/ uint32_t, CreateCheckpointHint> create_checkpoint_hints;
8276
unordered_flat_map</*action_counter*/ uint32_t, CommitCheckpointHint> commit_checkpoint_hints;
8377
unordered_flat_map</*action_counter*/ uint32_t, RevertCheckpointHint> revert_checkpoint_hints;

barretenberg/cpp/src/barretenberg/vm2/simulation_helper.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -472,12 +472,9 @@ TxSimulationResult AvmSimulationHelper::simulate_fast_without_hinted_dbs(const E
472472
// Note: we currently only have hinted raw dbs, TODO eventually replace with raw db:
473473
HintedRawContractDB base_contract_db(hints);
474474
HintedRawMerkleDB base_merkle_db(hints);
475-
// TODO(MW): Just here so we can use a ref to query hints and not remove const from getters:
476-
MappedContractHints contract_hints;
477-
MappedMerkleHints merkle_hints;
478475

479-
HintingContractsDB raw_contract_db(base_contract_db, contract_hints);
480-
HintingRawDB raw_merkle_db(base_merkle_db, merkle_hints);
476+
HintingContractsDB raw_contract_db(base_contract_db);
477+
HintingRawDB raw_merkle_db(base_merkle_db);
481478
auto result =
482479
simulate_fast(raw_contract_db, raw_merkle_db, hints.tx, hints.globalVariables, hints.protocolContracts);
483480

0 commit comments

Comments
 (0)