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: set code #262

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions eth_tester/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ def get_balance(self, account, block_number=None):
def get_code(self, account, block_number=None):
raise NotImplementedError("Must be implemented by subclasses")

@abstractmethod
def set_code(self, account, code):
raise NotImplementedError("Must be implemented by subclasses")

#
# Transactions
#
Expand Down
6 changes: 6 additions & 0 deletions eth_tester/backends/mock/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,12 @@ def get_code(self, account, block_number=None):
except KeyError:
return b""

def set_code(self, account, code):
if account in self.account_state_lookup:
self.account_state_lookup[account]["code"] = code
else:
self.account_state_lookup[account] = {"code": code}

#
# Transactions
#
Expand Down
4 changes: 4 additions & 0 deletions eth_tester/backends/pyevm/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,10 @@ def get_code(self, account, block_number="latest"):
vm = _get_vm_for_block_number(self.chain, block_number)
return vm.state.get_code(account)

def set_code(self, account, code):
vm = _get_vm_for_block_number(self.chain, block_number)
return vm.state.set_code(account, code)

def get_base_fee(self, block_number="latest"):
vm = _get_vm_for_block_number(self.chain, block_number)
return vm.state.base_fee
Expand Down
5 changes: 5 additions & 0 deletions eth_tester/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ def get_code(self, account, block_number="latest"):
code = self.normalizer.normalize_outbound_code(raw_code)
return code

def set_code(self, account, code):
self.validator.validate_inbound_account(account)
raw_account = self.normalizer.normalize_inbound_account(account)
self.backend.set_code(raw_account, code)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think this might be too irregular of a state to work, i have tried .persist() but cant get things to really persist


def get_nonce(self, account, block_number="latest"):
self.validator.validate_inbound_account(account)
self.validator.validate_inbound_block_number(block_number)
Expand Down
5 changes: 5 additions & 0 deletions eth_tester/utils/backend_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ def test_get_code_account_without_code(self, eth_tester):
code = eth_tester.get_code(BURN_ADDRESS)
assert code == "0x"

def test_set_code(self, eth_tester):
eth_tester.set_code(BURN_ADDRESS, "0x123")
code = eth_tester.get_code(BURN_ADDRESS)
assert code == "0x123"

def test_get_nonce(self, eth_tester):
for account in eth_tester.get_accounts():
nonce = eth_tester.get_nonce(account)
Expand Down