Skip to content

Commit 790e441

Browse files
committed
remove cached bytecode id in context
1 parent 8664b51 commit 790e441

File tree

7 files changed

+17
-17
lines changed

7 files changed

+17
-17
lines changed

barretenberg/cpp/src/barretenberg/vm2/simulation/bytecode_manager.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ class BytecodeManagerInterface {
9999
// Returns the id of the current bytecode. Tries to fetch it if not already done.
100100
// Throws BytecodeNotFoundError if contract does not exist.
101101
virtual BytecodeId get_bytecode_id() = 0;
102+
103+
// Returns the id of the current bytecode if it has been retrieved, std::nullopt otherwise.
104+
// Won't try to retrieve the bytecode.
105+
virtual std::optional<BytecodeId> get_retrieved_bytecode_id() = 0;
102106
};
103107

104108
class BytecodeManager : public BytecodeManagerInterface {
@@ -121,6 +125,8 @@ class BytecodeManager : public BytecodeManagerInterface {
121125
return bytecode_id.value();
122126
}
123127

128+
std::optional<BytecodeId> get_retrieved_bytecode_id() override { return bytecode_id; }
129+
124130
private:
125131
AztecAddress address;
126132
std::optional<BytecodeId> bytecode_id;

barretenberg/cpp/src/barretenberg/vm2/simulation/context.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ ContextEvent EnqueuedCallContext::serialize_context_event()
7171
.pc = get_pc(),
7272
.msg_sender = get_msg_sender(),
7373
.contract_addr = get_address(),
74-
.bytecode_id = get_bytecode_id().value_or(FF(0)),
74+
.bytecode_id = get_bytecode_manager().get_retrieved_bytecode_id().value_or(FF(0)),
7575
.transaction_fee = get_transaction_fee(),
7676
.is_static = get_is_static(),
7777
.parent_cd_addr = 0,
@@ -131,7 +131,7 @@ ContextEvent NestedContext::serialize_context_event()
131131
.pc = get_pc(),
132132
.msg_sender = get_msg_sender(),
133133
.contract_addr = get_address(),
134-
.bytecode_id = get_bytecode_id().value_or(FF(0)),
134+
.bytecode_id = get_bytecode_manager().get_retrieved_bytecode_id().value_or(FF(0)),
135135
.transaction_fee = get_transaction_fee(),
136136
.is_static = get_is_static(),
137137
.parent_cd_addr = parent_cd_addr,

barretenberg/cpp/src/barretenberg/vm2/simulation/context.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ class ContextInterface {
8282

8383
virtual uint32_t get_checkpoint_id_at_creation() const = 0;
8484

85-
virtual std::optional<BytecodeId> get_bytecode_id() const = 0;
86-
virtual void set_bytecode_id(BytecodeId bytecode_id) = 0;
87-
8885
// Events
8986
virtual ContextEvent serialize_context_event() = 0;
9087
};
@@ -190,9 +187,6 @@ class BaseContext : public ContextInterface {
190187

191188
uint32_t get_checkpoint_id_at_creation() const override { return checkpoint_id_at_creation; }
192189

193-
std::optional<BytecodeId> get_bytecode_id() const override { return bytecode_id; }
194-
void set_bytecode_id(BytecodeId bytecode_id) override { this->bytecode_id = bytecode_id; }
195-
196190
// Input / Output
197191
std::vector<FF> get_returndata(uint32_t rd_offset, uint32_t rd_copy_size) override;
198192

@@ -220,7 +214,6 @@ class BaseContext : public ContextInterface {
220214
Gas gas_used;
221215
Gas gas_limit;
222216
std::unique_ptr<BytecodeManagerInterface> bytecode;
223-
std::optional<BytecodeId> bytecode_id = std::nullopt;
224217
std::unique_ptr<MemoryInterface> memory;
225218
std::unique_ptr<InternalCallStackManagerInterface> internal_call_stack_manager;
226219

barretenberg/cpp/src/barretenberg/vm2/simulation/execution.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,8 +1080,7 @@ ExecutionResult Execution::execute(std::unique_ptr<ContextInterface> enqueued_ca
10801080
// We try to get the bytecode id. This can throw if the contract is not deployed or if we have retrieved too
10811081
// many unique class ids. Note: bytecode_id is tracked in context events, not in the top-level execution
10821082
// event. It is already included in the before_context_event (defaulting to 0 on error/not-found).
1083-
BytecodeId id = context.get_bytecode_manager().get_bytecode_id();
1084-
context.set_bytecode_id(id);
1083+
context.get_bytecode_manager().get_bytecode_id();
10851084

10861085
//// Temporality group 2 starts ////
10871086

@@ -1167,7 +1166,9 @@ void Execution::handle_enter_call(ContextInterface& parent_context, std::unique_
11671166
.next_pc = parent_context.get_next_pc(),
11681167
.msg_sender = parent_context.get_msg_sender(),
11691168
.contract_addr = parent_context.get_address(),
1170-
.bytecode_id = parent_context.get_bytecode_id().value(), // Should never be nullopt when entering a call
1169+
.bytecode_id = parent_context.get_bytecode_manager()
1170+
.get_retrieved_bytecode_id()
1171+
.value(), // Bytecode should have been retrieved in the parent context if it issued a call.
11711172
.is_static = parent_context.get_is_static(),
11721173
.parent_cd_addr = parent_context.get_parent_cd_addr(),
11731174
.parent_cd_size = parent_context.get_parent_cd_size(),

barretenberg/cpp/src/barretenberg/vm2/simulation/execution.test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ TEST_F(ExecutionSimulationTest, Call)
270270
// Context snapshotting
271271
EXPECT_CALL(context, get_context_id);
272272
EXPECT_CALL(context, get_parent_id);
273-
EXPECT_CALL(context, get_bytecode_id).WillOnce(Return(FF(1)));
273+
EXPECT_CALL(context, get_bytecode_manager).WillOnce(ReturnRef(bytecode_manager));
274+
EXPECT_CALL(bytecode_manager, get_retrieved_bytecode_id).WillOnce(Return(FF(1)));
274275
EXPECT_CALL(context, get_next_pc);
275276
EXPECT_CALL(context, get_is_static).WillRepeatedly(Return(false));
276277
EXPECT_CALL(context, get_msg_sender).WillOnce(ReturnRef(parent_address));
@@ -346,7 +347,8 @@ TEST_F(ExecutionSimulationTest, ExternalCallStaticnessPropagation)
346347
EXPECT_CALL(gas_tracker, consume_gas(Gas{ 0, 0 }));
347348
EXPECT_CALL(context, get_context_id);
348349
EXPECT_CALL(context, get_parent_id);
349-
EXPECT_CALL(context, get_bytecode_id).WillOnce(Return(FF(1)));
350+
EXPECT_CALL(context, get_bytecode_manager).WillOnce(ReturnRef(bytecode_manager));
351+
EXPECT_CALL(bytecode_manager, get_retrieved_bytecode_id).WillOnce(Return(FF(1)));
350352
EXPECT_CALL(context, get_next_pc);
351353
EXPECT_CALL(context, get_is_static).WillRepeatedly(Return(parent_is_static));
352354
EXPECT_CALL(context, get_msg_sender).WillOnce(ReturnRef(parent_address));

barretenberg/cpp/src/barretenberg/vm2/simulation/testing/mock_bytecode_manager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class MockBytecodeManager : public BytecodeManagerInterface {
1919

2020
MOCK_METHOD(Instruction, read_instruction, (uint32_t pc), (override));
2121
MOCK_METHOD(BytecodeId, get_bytecode_id, (), (override));
22+
MOCK_METHOD(std::optional<BytecodeId>, get_retrieved_bytecode_id, (), (override));
2223
};
2324

2425
} // namespace bb::avm2::simulation

barretenberg/cpp/src/barretenberg/vm2/simulation/testing/mock_context.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ class MockContext : public ContextInterface {
7373

7474
MOCK_METHOD(uint32_t, get_checkpoint_id_at_creation, (), (const, override));
7575

76-
MOCK_METHOD(std::optional<BytecodeId>, get_bytecode_id, (), (const, override));
77-
MOCK_METHOD(void, set_bytecode_id, (BytecodeId bytecode_id), (override));
78-
7976
// Event Emitting
8077
MOCK_METHOD(ContextEvent, serialize_context_event, (), (override));
8178
};

0 commit comments

Comments
 (0)