Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cstdint>
#include <memory>
#include <optional>
#include <span>
#include <sys/types.h>
#include <utility>
Expand Down Expand Up @@ -74,25 +75,33 @@ class BytecodeManagerInterface {
public:
virtual ~BytecodeManagerInterface() = default;

virtual Instruction read_instruction(uint32_t pc) const = 0;
virtual BytecodeId get_bytecode_id() const = 0;
virtual Instruction read_instruction(uint32_t pc) = 0;
// Returns the id of the current bytecode. Tries to fetch it if not already done.
virtual BytecodeId get_bytecode_id() = 0;
};

class BytecodeManager : public BytecodeManagerInterface {
public:
BytecodeManager(BytecodeId bytecode_id, TxBytecodeManagerInterface& tx_bytecode_manager)
: bytecode_id(bytecode_id)
BytecodeManager(AztecAddress address, TxBytecodeManagerInterface& tx_bytecode_manager)
: address(address)
, tx_bytecode_manager(tx_bytecode_manager)
{}

Instruction read_instruction(uint32_t pc) const override
Instruction read_instruction(uint32_t pc) override
{
return tx_bytecode_manager.read_instruction(bytecode_id, pc);
return tx_bytecode_manager.read_instruction(get_bytecode_id(), pc);
}
Comment on lines -88 to +93
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this need to become non-const?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it calls a non-const, get_bytecode_id()

BytecodeId get_bytecode_id() override
{
if (!bytecode_id.has_value()) {
bytecode_id = tx_bytecode_manager.get_bytecode(address);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At some point we'll have to make some changes here, because if fetching fails, we will still want the bytecode_id (which we wouldn't -in principle- get if it just throws).

}
return bytecode_id.value();
}
BytecodeId get_bytecode_id() const override { return bytecode_id; }

private:
BytecodeId bytecode_id;
AztecAddress address;
std::optional<BytecodeId> bytecode_id;
TxBytecodeManagerInterface& tx_bytecode_manager;
};

Expand Down
7 changes: 2 additions & 5 deletions barretenberg/cpp/src/barretenberg/vm2/simulation/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,11 @@ class ContextProvider : public ContextProviderInterface {
{
uint32_t space_id = static_cast<uint32_t>(address); // FIXME: space id.

// FIXME: doing too much in a "constructor"!
BytecodeId bytecode_id = tx_bytecode_manager.get_bytecode(address);

return std::make_unique<Context>(address,
msg_sender,
calldata,
is_static,
std::make_unique<BytecodeManager>(bytecode_id, tx_bytecode_manager),
std::make_unique<BytecodeManager>(address, tx_bytecode_manager),
std::make_unique<Memory>(space_id, memory_events));
}

Expand All @@ -121,4 +118,4 @@ class ContextProvider : public ContextProviderInterface {
EventEmitterInterface<MemoryEvent>& memory_events;
};

} // namespace bb::avm2::simulation
} // namespace bb::avm2::simulation
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class MockBytecodeManager : public BytecodeManagerInterface {
MockBytecodeManager();
~MockBytecodeManager() override;

MOCK_METHOD(Instruction, read_instruction, (uint32_t pc), (const, override));
MOCK_METHOD(BytecodeId, get_bytecode_id, (), (const, override));
MOCK_METHOD(Instruction, read_instruction, (uint32_t pc), (override));
MOCK_METHOD(BytecodeId, get_bytecode_id, (), (override));
};

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