@@ -14,79 +14,69 @@ namespace bb::avm2::simulation {
1414
1515BytecodeId TxBytecodeManager::get_bytecode (const AztecAddress& address)
1616{
17+
1718 // Use shared ContractInstanceManager for contract instance retrieval and validation
1819 // This handles nullifier checks, address derivation, and update validation
19- AppendOnlyTreeSnapshot snapshot_before = retrieved_bytecodes_tree_check.snapshot ();
2020 auto tree_states = merkle_db.get_tree_state ();
21+ AppendOnlyTreeSnapshot before_snapshot = retrieved_bytecodes_tree_check.get_snapshot ();
22+
23+ BytecodeRetrievalEvent retrieval_event = {
24+ .bytecode_id = FF (0 ), // Use default ID for error cases
25+ .address = address,
26+ .nullifier_root = tree_states.nullifierTree .tree .root ,
27+ .public_data_tree_root = tree_states.publicDataTree .tree .root ,
28+ .retrieved_bytecodes_snapshot_before = before_snapshot,
29+ .retrieved_bytecodes_snapshot_after = before_snapshot,
30+ };
2131
2232 auto maybe_instance = contract_instance_manager.get_contract_instance (address);
2333
2434 if (!maybe_instance.has_value ()) {
35+ retrieval_event.instance_not_found_error = true ;
2536 // Contract instance not found - emit error event and throw
26- retrieval_events.emit ({
27- .bytecode_id = FF (0 ), // Use default ID for error case
28- .address = address,
29- .nullifier_root = tree_states.nullifierTree .tree .root ,
30- .public_data_tree_root = tree_states.publicDataTree .tree .root ,
31- .retrieved_bytecodes_snapshot_before = snapshot_before,
32- .retrieved_bytecodes_snapshot_after = snapshot_before,
33- .instance_not_found_error = true ,
34- });
37+ retrieval_events.emit (std::move (retrieval_event));
3538 vinfo (" Contract " , field_to_string (address), " is not deployed!" );
36- throw BytecodeNotFoundError (" Contract " + field_to_string (address) + " is not deployed" );
39+ throw BytecodeRetrievalError (" Contract " + field_to_string (address) + " is not deployed" );
3740 }
3841
3942 ContractInstance instance = maybe_instance.value ();
4043 ContractClassId current_class_id = instance.current_class_id ;
44+ retrieval_event.current_class_id = current_class_id;
45+
46+ bool is_new_class = !retrieved_bytecodes_tree_check.contains (current_class_id);
47+ retrieval_event.is_new_class = is_new_class;
4148
42- bool new_bytecode = !retrieved_bytecodes_tree_check.contains (current_class_id);
4349 uint32_t retrieved_bytecodes_count = retrieved_bytecodes_tree_check.size ();
4450
45- if (new_bytecode && retrieved_bytecodes_count == MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) {
46- retrieval_events.emit ({
47- .bytecode_id = FF (0 ), // Use default ID for error case
48- .address = address,
49- .current_class_id = current_class_id,
50- .nullifier_root = tree_states.nullifierTree .tree .root ,
51- .public_data_tree_root = tree_states.publicDataTree .tree .root ,
52- .retrieved_bytecodes_snapshot_before = snapshot_before,
53- .retrieved_bytecodes_snapshot_after = snapshot_before,
54- .new_bytecode = new_bytecode,
55- .limit_error = true ,
56- });
57- throw BytecodeRetrievalLimitReachedError (" Can't retrieve more than " +
58- std::to_string (MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) +
59- " bytecodes per tx" );
51+ if (is_new_class && retrieved_bytecodes_count >= MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) {
52+ retrieval_event.limit_error = true ;
53+ retrieval_events.emit (std::move (retrieval_event));
54+ throw BytecodeRetrievalError (" Can't retrieve more than " +
55+ std::to_string (MAX_PUBLIC_CALLS_TO_UNIQUE_CONTRACT_CLASS_IDS) +
56+ " bytecodes per tx" );
6057 }
6158
6259 retrieved_bytecodes_tree_check.insert (current_class_id);
63- AppendOnlyTreeSnapshot snapshot_after = retrieved_bytecodes_tree_check.snapshot ();
60+ AppendOnlyTreeSnapshot snapshot_after = retrieved_bytecodes_tree_check.get_snapshot ();
61+ retrieval_event.retrieved_bytecodes_snapshot_after = snapshot_after;
6462
6563 // Contract class retrieval and class ID validation
6664 std::optional<ContractClass> maybe_klass = contract_db.get_contract_class (current_class_id);
6765 // Note: we don't need to silo and check the class id because the deployer contract guarrantees
6866 // that if a contract instance exists, the class has been registered.
6967 assert (maybe_klass.has_value ());
7068 auto & klass = maybe_klass.value ();
69+ retrieval_event.contract_class = klass; // WARNING: this class has the whole bytecode.
7170 info (" Bytecode for " , address, " successfully retrieved!" );
7271
7372 // Bytecode hashing and decomposition, deduplicated by bytecode_id (commitment)
7473 BytecodeId bytecode_id = klass.public_bytecode_commitment ;
74+ retrieval_event.bytecode_id = bytecode_id;
7575
7676 // Check if we've already processed this bytecode. If so, don't do hashing and decomposition again!
7777 if (bytecodes.contains (bytecode_id)) {
7878 // Already processed this bytecode - just emit retrieval event and return
79- retrieval_events.emit ({
80- .bytecode_id = bytecode_id,
81- .address = address,
82- .current_class_id = current_class_id,
83- .contract_class = klass,
84- .nullifier_root = tree_states.nullifierTree .tree .root ,
85- .public_data_tree_root = tree_states.publicDataTree .tree .root ,
86- .retrieved_bytecodes_snapshot_before = snapshot_before,
87- .retrieved_bytecodes_snapshot_after = snapshot_after,
88- .new_bytecode = new_bytecode,
89- });
79+ retrieval_events.emit (std::move (retrieval_event));
9080 return bytecode_id;
9181 }
9282
@@ -102,17 +92,7 @@ BytecodeId TxBytecodeManager::get_bytecode(const AztecAddress& address)
10292 // We now save the bytecode so that we don't repeat this process.
10393 bytecodes.emplace (bytecode_id, std::move (shared_bytecode));
10494
105- retrieval_events.emit ({
106- .bytecode_id = bytecode_id,
107- .address = address,
108- .current_class_id = current_class_id,
109- .contract_class = klass, // WARNING: this class has the whole bytecode.
110- .nullifier_root = tree_states.nullifierTree .tree .root ,
111- .public_data_tree_root = tree_states.publicDataTree .tree .root ,
112- .retrieved_bytecodes_snapshot_before = snapshot_before,
113- .retrieved_bytecodes_snapshot_after = snapshot_after,
114- .new_bytecode = new_bytecode,
115- });
95+ retrieval_events.emit (std::move (retrieval_event));
11696
11797 return bytecode_id;
11898}
0 commit comments