Skip to content

Commit 2aa8e12

Browse files
authored
feat(fw): also test system contract deployments with balance (#1132)
1 parent 5f84ce4 commit 2aa8e12

File tree

1 file changed

+30
-5
lines changed

1 file changed

+30
-5
lines changed

src/ethereum_test_tools/utility/generators.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ class DeploymentTestType(Enum):
2121
DEPLOY_AFTER_FORK = "deploy_after_fork"
2222

2323

24+
class ContractAddressHasBalance(Enum):
25+
"""Represents whether the target deployment test has a balance before deployment."""
26+
27+
ZERO_BALANCE = "zero_balance"
28+
NONZERO_BALANCE = "nonzero_balance"
29+
30+
2431
class SystemContractDeployTestFunction(Protocol):
2532
"""
2633
Represents a function to be decorated with the `generate_system_contract_deploy_test`
@@ -61,9 +68,17 @@ def generate_system_contract_deploy_test(
6168
"""
6269
Generate a test that verifies the correct deployment of a system contract.
6370
64-
Generates two tests:
65-
- One that deploys the contract before the fork.
66-
- One that deploys the contract after the fork.
71+
Generates four test cases:
72+
73+
| before/after fork | has balance |
74+
------------------------------------|-------------------|-------------|
75+
`deploy_before_fork-nonzero_balance`| before | True |
76+
`deploy_before_fork-zero_balance` | before | False |
77+
`deploy_after_fork-nonzero_balance` | after | True |
78+
`deploy_after_fork-zero_balance` | after | False |
79+
80+
where `has balance` refers to whether the contract address has a non-zero balance before
81+
deployment, or not.
6782
6883
Args:
6984
fork (Fork): The fork to test.
@@ -89,6 +104,14 @@ def generate_system_contract_deploy_test(
89104
deployer_address = deploy_tx.sender
90105

91106
def decorator(func: SystemContractDeployTestFunction):
107+
@pytest.mark.parametrize(
108+
"has_balance",
109+
[
110+
pytest.param(ContractAddressHasBalance.NONZERO_BALANCE),
111+
pytest.param(ContractAddressHasBalance.ZERO_BALANCE),
112+
],
113+
ids=lambda x: x.name.lower(),
114+
)
92115
@pytest.mark.parametrize(
93116
"test_type",
94117
[
@@ -101,6 +124,7 @@ def decorator(func: SystemContractDeployTestFunction):
101124
@pytest.mark.valid_at_transition_to(fork.name())
102125
def wrapper(
103126
blockchain_test: BlockchainTestFiller,
127+
has_balance: ContractAddressHasBalance,
104128
pre: Alloc,
105129
test_type: DeploymentTestType,
106130
fork: Fork,
@@ -131,11 +155,11 @@ def wrapper(
131155
timestamp=15_001,
132156
),
133157
]
134-
158+
balance = 1 if has_balance == ContractAddressHasBalance.NONZERO_BALANCE else 0
135159
pre[expected_deploy_address] = Account(
136160
code=b"", # Remove the code that is automatically allocated on the fork
137161
nonce=0,
138-
balance=0,
162+
balance=balance,
139163
)
140164
pre[deployer_address] = Account(
141165
balance=deployer_required_balance,
@@ -147,6 +171,7 @@ def wrapper(
147171
fork_pre_allocation = fork.pre_allocation_blockchain()
148172
assert expected_deploy_address_int in fork_pre_allocation
149173
expected_code = fork_pre_allocation[expected_deploy_address_int]["code"]
174+
# Note: balance check is omitted; it may be modified by the underlying, decorated test
150175
if expected_system_contract_storage is None:
151176
post[expected_deploy_address] = Account(
152177
code=expected_code,

0 commit comments

Comments
 (0)