Skip to content

Commit e7a0603

Browse files
authored
Merge pull request #300 from spencer-tb/assert-no-empty-accounts
improvement: Deprecate empty accounts within framework.
2 parents d03af0b + 99a5919 commit e7a0603

File tree

4 files changed

+26
-21
lines changed

4 files changed

+26
-21
lines changed

src/ethereum_test_tools/common/types.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,17 @@ def check_alloc(self: "Account", address: str, alloc: dict):
684684
actual_storage = Storage(alloc["storage"]) if "storage" in alloc else Storage({})
685685
expected_storage.must_be_equal(address=address, other=actual_storage)
686686

687+
def is_empty(self: "Account") -> bool:
688+
"""
689+
Returns true if an account deemed empty.
690+
"""
691+
return (
692+
(self.nonce == 0 or self.nonce is None)
693+
and (self.balance == 0 or self.balance is None)
694+
and (not self.code and self.code is None)
695+
and (not self.storage or self.storage == {} or self.storage is None)
696+
)
697+
687698
@classmethod
688699
def from_dict(cls: Type, data: "Dict | Account") -> "Account":
689700
"""
@@ -734,7 +745,9 @@ def __init__(self, d: Mapping[FixedSizeBytesConvertible, Account | Dict] = {}):
734745
for address, account in d.items():
735746
address = Address(address)
736747
assert address not in self, f"Duplicate address in alloc: {address}"
737-
self[address] = Account.from_dict(account)
748+
account = Account.from_dict(account)
749+
assert not account.is_empty(), f"Empty account: {account} for address: {address}"
750+
self[address] = account
738751

739752
@classmethod
740753
def merge(cls, alloc_1: "Alloc", alloc_2: "Alloc") -> "Alloc":

tests/cancun/eip4788_beacon_root/conftest.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,17 +189,19 @@ def pre(
189189
Prepares the pre state of all test cases, by setting the balance of the
190190
source account of all test transactions, and the contract caller account.
191191
"""
192-
return {
192+
pre_alloc = {
193193
TestAddress: Account(
194194
nonce=0,
195195
balance=0x10**10,
196196
),
197197
caller_address: contract_call_account,
198-
SYSTEM_ADDRESS: Account(
198+
}
199+
if system_address_balance > 0:
200+
pre_alloc[to_address(SYSTEM_ADDRESS)] = Account(
199201
nonce=0,
200202
balance=system_address_balance,
201-
),
202-
}
203+
)
204+
return pre_alloc
203205

204206

205207
@pytest.fixture

tests/cancun/eip4788_beacon_root/test_blocks_beacon_root_contract.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ def test_multi_block_beacon_root_timestamp_calls(
104104
tx: Transaction,
105105
call_gas: int,
106106
call_value: int,
107-
system_address_balance: int,
108107
):
109108
"""
110109
Tests multiple blocks where each block writes a timestamp to storage and contains one
@@ -125,10 +124,6 @@ def test_multi_block_beacon_root_timestamp_calls(
125124
nonce=0,
126125
balance=0x10**10,
127126
),
128-
SYSTEM_ADDRESS: Account(
129-
nonce=0,
130-
balance=system_address_balance,
131-
),
132127
}
133128
post = {}
134129

@@ -235,7 +230,6 @@ def test_beacon_root_transition(
235230
tx: Transaction,
236231
call_gas: int,
237232
call_value: int,
238-
system_address_balance: int,
239233
fork: Fork,
240234
):
241235
"""
@@ -248,10 +242,6 @@ def test_beacon_root_transition(
248242
nonce=0,
249243
balance=0x10**10,
250244
),
251-
SYSTEM_ADDRESS: Account(
252-
nonce=0,
253-
balance=system_address_balance,
254-
),
255245
}
256246
post = {}
257247

tests/shanghai/eip4895_withdrawals/test_withdrawals.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def post(self, test_case: str) -> Dict: # noqa: D102
109109
if test_case == "tx_in_withdrawals_block":
110110
return {}
111111
if test_case == "tx_after_withdrawals_block":
112-
return {TestAddress: Account(balance=ONE_GWEI)}
112+
return {TestAddress: Account(balance=ONE_GWEI + 1)}
113113
raise Exception("Invalid test case.")
114114

115115
def test_use_value_in_tx(
@@ -121,7 +121,7 @@ def test_use_value_in_tx(
121121
"""
122122
Test sending withdrawal value in a transaction.
123123
"""
124-
pre = {TestAddress: Account(balance=0)}
124+
pre = {TestAddress: Account(balance=1)}
125125
blockchain_test(pre=pre, post=post, blocks=blocks)
126126

127127

@@ -137,7 +137,7 @@ def test_use_value_in_contract(blockchain_test: BlockchainTestFiller):
137137
pre = {
138138
TestAddress: Account(balance=1000000000000000000000, nonce=0),
139139
to_address(0x100): Account(balance=0, code=SEND_ONE_GWEI),
140-
to_address(0x200): Account(balance=0),
140+
to_address(0x200): Account(balance=1),
141141
}
142142
tx = Transaction(
143143
# Transaction sent from the `TestAddress`, which has 0 balance at start
@@ -172,7 +172,7 @@ def test_use_value_in_contract(blockchain_test: BlockchainTestFiller):
172172
}
173173
),
174174
to_address(0x200): Account(
175-
balance=ONE_GWEI,
175+
balance=ONE_GWEI + 1,
176176
),
177177
}
178178

@@ -384,7 +384,7 @@ def test_self_destructing_account(blockchain_test: BlockchainTestFiller):
384384
balance=(100 * ONE_GWEI),
385385
),
386386
to_address(0x200): Account(
387-
balance=0,
387+
balance=1,
388388
),
389389
}
390390

@@ -417,7 +417,7 @@ def test_self_destructing_account(blockchain_test: BlockchainTestFiller):
417417
),
418418
to_address(0x200): Account(
419419
code=None,
420-
balance=(100 * ONE_GWEI),
420+
balance=(100 * ONE_GWEI) + 1,
421421
),
422422
}
423423

0 commit comments

Comments
 (0)