Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[fork]: add set_code, get_storage, set_storage to boa.env #210

Merged
merged 7 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 0 additions & 2 deletions boa/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,14 +315,12 @@ def get_code(self, address: _AddressType) -> bytes:
return self.evm.get_code(Address(address))

def set_code(self, address: _AddressType, code: bytes) -> None:
assert self.evm.is_forked, "The EVM is not forked, cannot set code"
self.evm.set_code(Address(address), code)

def get_storage(self, address: _AddressType, slot: int) -> int:
return self.evm.get_storage(Address(address), slot)

def set_storage(self, address: _AddressType, slot: int, value: int) -> None:
assert self.evm.is_forked, "The EVM is not forked, cannot set storage"
self.evm.set_storage(Address(address), slot, value)

# function to time travel
Expand Down
10 changes: 9 additions & 1 deletion boa/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from eth_account import Account
from requests.exceptions import HTTPError

from boa.environment import Env
from boa.environment import Env, _AddressType
from boa.rpc import (
RPC,
EthereumRPC,
Expand Down Expand Up @@ -495,3 +495,11 @@ def _send_txn(self, from_, to=None, gas=None, value=None, data=None):

t_obj = TraceObject(trace) if trace is not None else None
return receipt, t_obj

def set_code(self, address: _AddressType, code: bytes) -> None:
assert self.evm.is_forked, "The EVM is not forked, cannot set code"
return super().set_code(address, code)

def set_storage(self, address: _AddressType, slot: int, value: int) -> None:
assert self.evm.is_forked, "The EVM is not forked, cannot set storage"
super().set_storage(address, slot, value)
5 changes: 4 additions & 1 deletion boa/vm/fork.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ def fetch_multi(self, payload):
# fetch_multi is called only with the missing payloads
# map the results back to the original indices
for result_ix, rpc_result in enumerate(self._rpc.fetch_multi(batch)):
key, item_ix = keys[result_ix]
try:
key, item_ix = keys[result_ix]
except IndexError:
charles-cooper marked this conversation as resolved.
Show resolved Hide resolved
continue
ret[item_ix] = rpc_result
self._db[key] = json.dumps(rpc_result).encode("utf-8")

Expand Down
8 changes: 3 additions & 5 deletions tests/unitary/test_fork_utils.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import pytest

import boa
from boa.vm.fork import AccountDBFork
from boa import NetworkEnv

address = "0x0000000000000000000000000000000000000065"


def test_code_non_fork():
with pytest.raises(AssertionError):
boa.env.set_code(address, b"")
NetworkEnv.set_code(boa.env, address, b"")


def test_storage_non_fork():
with pytest.raises(AssertionError):
boa.env.set_storage(address, 0, 0)
NetworkEnv.set_storage(boa.env, address, 0, 0)


def test_code():
boa.env.evm._set_account_db_class(AccountDBFork)
assert boa.env.get_code(address) == b""
code = b"some test code"
boa.env.set_code(address, code)
Expand All @@ -26,7 +25,6 @@ def test_code():

def test_storage(monkeypatch):
DanielSchiavini marked this conversation as resolved.
Show resolved Hide resolved
storage = 12381920371289
boa.env.evm._set_account_db_class(AccountDBFork)
assert boa.env.get_storage(address, 0) == 0
boa.env.set_storage(address, 0, storage)
assert boa.env.get_storage(address, 0) == storage
Loading