Skip to content

Commit

Permalink
Elevate patch to evm instead of vm
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Apr 17, 2024
1 parent 49c769a commit d50f0bc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 35 deletions.
2 changes: 1 addition & 1 deletion boa/contracts/vyper/decoder_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class ByteAddressableStorage:
def __init__(self, evm, address: Address, key: int):
self.evm = evm
self.address = address.canonical_address
self.address = address
self.key = key

def __getitem__(self, subscript):
Expand Down
16 changes: 11 additions & 5 deletions boa/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,12 @@ def reset_gas_used(self):
# to the snapshot on exiting the with statement
@contextlib.contextmanager
def anchor(self):
with self.evm.anchor():
yield
snapshot_id = self.evm.snapshot()
try:
with self.evm.patch.anchor():
yield
finally:
self.evm.revert(snapshot_id)

@contextlib.contextmanager
def sender(self, address):
Expand Down Expand Up @@ -312,11 +316,11 @@ def get_code(self, address):

@property
def block_number(self):
return self.evm.block_number
return self.evm.patch.block_number

@property
def timestamp(self):
return self.evm.timestamp
return self.evm.patch.timestamp

# function to time travel
def time_travel(
Expand All @@ -332,4 +336,6 @@ def time_travel(
else:
assert blocks is not None # mypy hint
seconds = blocks * block_delta
self.evm.time_travel(seconds, blocks)

self.evm.patch.timestamp += seconds
self.evm.patch.block_number += blocks
2 changes: 1 addition & 1 deletion boa/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def anchor(self):
if not self._rpc_has_snapshot:
raise RuntimeError("RPC does not have `evm_snapshot` capability!")
try:
block_id = self.evm.block_id
block_id = self.evm.patch.block_id
snapshot_id = self._rpc.fetch("evm_snapshot", [])
yield
# note we cannot call super.anchor() because vm/accountdb fork
Expand Down
31 changes: 3 additions & 28 deletions boa/vm/py_evm.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def _init_vm(self, account_db_class=AccountDB):
self.vm = self.chain.get_vm()
self.vm.__class__._state_class.account_db_class = account_db_class

self.vm.patch = VMPatcher(self.vm)
self.patch = VMPatcher(self.vm)

c: Type[titanoboa_computation] = type(
"TitanoboaComputation",
Expand Down Expand Up @@ -392,8 +392,8 @@ def fork_rpc(self, rpc: RPC, block_identifier: str, **kwargs):
self._init_vm(account_db_class)
block_info = self.vm.state._account_db._block_info

self.vm.patch.timestamp = int(block_info["timestamp"], 16)
self.vm.patch.block_number = int(block_info["number"], 16)
self.patch.timestamp = int(block_info["timestamp"], 16)
self.patch.block_number = int(block_info["number"], 16)
# TODO patch the other stuff

self.vm.state._account_db._rpc._init_mem_db()
Expand Down Expand Up @@ -430,15 +430,6 @@ def reset_access_counters(self):
def snapshot(self) -> Any:
return self.vm.state.snapshot()

@contextlib.contextmanager
def anchor(self):
snapshot_id = self.snapshot()
try:
with self.vm.patch.anchor():
yield
finally:
self.revert(snapshot_id)

def revert(self, snapshot_id: Any) -> None:
self.vm.state.revert(snapshot_id)

Expand Down Expand Up @@ -516,26 +507,10 @@ def execute_code(
tx_ctx = BaseTransactionContext(origin=origin, gas_price=gas_price)
return self.vm.state.computation_class.apply_message(self.vm.state, msg, tx_ctx)

@property
def block_id(self):
return self.vm.state._account_db._block_id

@property
def block_number(self):
return self.vm.state.block_number

@property
def timestamp(self):
return self.vm.state.timestamp

def get_storage_slot(self, address: Address, slot: int) -> bytes:
data = self.vm.state._account_db.get_storage(address.canonical_address, slot)
return data.to_bytes(32, "big")

def time_travel(self, add_seconds: int, add_blocks: int):
self.vm.patch.timestamp += add_seconds
self.vm.patch.block_number += add_blocks


GENESIS_PARAMS = {"difficulty": constants.GENESIS_DIFFICULTY, "gas_limit": int(1e8)}

Expand Down

0 comments on commit d50f0bc

Please sign in to comment.