Skip to content

Commit 9d819a0

Browse files
committed
code duplication reduced
1 parent e993deb commit 9d819a0

File tree

1 file changed

+96
-158
lines changed

1 file changed

+96
-158
lines changed

tests/cancun/eip6780_selfdestruct/test_selfdestruct_revert.py

Lines changed: 96 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -68,51 +68,58 @@ def recursive_revert_contract_code(
6868
Then, recurse into a new call which transfers value to A,
6969
call A.selfdestruct, and reverts.
7070
"""
71+
# Common prefix for all three cases:
72+
# case 1: selfdestruct_on_outer_call=1
73+
# case 2: selfdestruct_on_outer_call=2
74+
# case 3: selfdestruct_on_outer_call has a different value
75+
common_prefix = (
76+
Op.PUSH0
77+
+ Op.CALLDATALOAD
78+
+ Op.PUSH1(0x1)
79+
+ Op.PUSH20(selfdestruct_with_transfer_contract_address)
80+
+ Op.SWAP2
81+
+ Op.SWAP1
82+
+ Op.DUP2
83+
+ Op.PUSH0
84+
+ Op.EQ
85+
+ Op.PUSH1(0x3A)
86+
+ Op.JUMPI
87+
+ Op.POP
88+
+ Op.PUSH1(0x1)
89+
+ Op.EQ
90+
+ Op.PUSH1(0x29)
91+
+ Op.JUMPI
92+
+ Op.STOP
93+
+ Op.JUMPDEST
94+
+ Op.PUSH0
95+
+ Op.PUSH1(0x20)
96+
+ Op.DUP2
97+
+ Op.PUSH1(0x1)
98+
+ Op.DUP2
99+
+ Op.SWAP5
100+
+ Op.DUP2
101+
+ Op.DUP4
102+
+ Op.MSTORE
103+
+ Op.GASLIMIT
104+
+ Op.CALL
105+
+ Op.PUSH0
106+
+ Op.DUP1
107+
+ Op.REVERT
108+
+ Op.JUMPDEST
109+
)
110+
71111
if selfdestruct_on_outer_call == 1:
72-
return (
73-
Op.PUSH0
74-
+ Op.CALLDATALOAD
75-
+ Op.PUSH1[0x1]
76-
+ Op.PUSH20[selfdestruct_with_transfer_contract_address]
77-
+ Op.SWAP2
78-
+ Op.SWAP1
79-
+ Op.DUP2
80-
+ Op.PUSH0
81-
+ Op.EQ
82-
+ Op.PUSH1[0x3A]
83-
+ Op.JUMPI
112+
suffix = (
113+
Op.SWAP1
84114
+ Op.POP
85-
+ Op.PUSH1[0x1]
86-
+ Op.EQ
87-
+ Op.PUSH1[0x29]
88-
+ Op.JUMPI
89-
+ Op.STOP
90-
+ Op.JUMPDEST
115+
+ Op.PUSH1(0x1)
91116
+ Op.PUSH0
92-
+ Op.PUSH1[0x20]
93-
+ Op.DUP2
94-
+ Op.PUSH1[0x1]
95-
+ Op.DUP2
96-
+ Op.SWAP5
97-
+ Op.DUP2
98-
+ Op.DUP4
99117
+ Op.MSTORE
100-
+ Op.GASLIMIT
101-
+ Op.CALL
102118
+ Op.PUSH0
103119
+ Op.DUP1
104-
+ Op.REVERT
105-
+ Op.JUMPDEST
106-
+ Op.SWAP1
107-
+ Op.POP
108-
+ Op.PUSH1[0x1]
109-
+ Op.PUSH0
110-
+ Op.MSTORE
111-
+ Op.PUSH0
112-
+ Op.DUP1
113-
+ Op.PUSH1[0x20]
120+
+ Op.PUSH1(0x20)
114121
+ Op.DUP2
115-
+ Op.PUSH1[0x1]
122+
+ Op.PUSH1(0x1)
116123
+ Op.DUP7
117124
+ Op.GASLIMIT
118125
+ Op.CALL
@@ -122,9 +129,9 @@ def recursive_revert_contract_code(
122129
+ Op.MSTORE
123130
+ Op.PUSH0
124131
+ Op.DUP1
125-
+ Op.PUSH1[0x20]
132+
+ Op.PUSH1(0x20)
126133
+ Op.DUP2
127-
+ Op.PUSH1[0x1]
134+
+ Op.PUSH1(0x1)
128135
+ Op.DUP7
129136
+ Op.GASLIMIT
130137
+ Op.CALL
@@ -133,60 +140,26 @@ def recursive_revert_contract_code(
133140
+ Op.MSTORE
134141
+ Op.PUSH0
135142
+ Op.DUP1
136-
+ Op.PUSH1[0x20]
143+
+ Op.PUSH1(0x20)
137144
+ Op.DUP2
138145
+ Op.DUP1
139146
+ Op.ADDRESS
140147
+ Op.GASLIMIT
141148
+ Op.CALL
142149
+ Op.POP
143150
+ Op.BALANCE
144-
+ Op.PUSH1[0x1]
151+
+ Op.PUSH1(0x1)
145152
+ Op.SSTORE
146153
+ Op.PUSH0
147154
+ Op.DUP1
148155
+ Op.RETURN
149156
)
150-
151-
if selfdestruct_on_outer_call == 2:
152-
return (
157+
elif selfdestruct_on_outer_call == 2:
158+
suffix = (
153159
Op.PUSH0
154-
+ Op.CALLDATALOAD
155-
+ Op.PUSH1[0x1]
156-
+ Op.PUSH20[selfdestruct_with_transfer_contract_address]
157-
+ Op.SWAP2
158-
+ Op.SWAP1
159-
+ Op.DUP2
160-
+ Op.PUSH0
161-
+ Op.EQ
162-
+ Op.PUSH1[0x3A]
163-
+ Op.JUMPI
164-
+ Op.POP
165-
+ Op.PUSH1[0x1]
166-
+ Op.EQ
167-
+ Op.PUSH1[0x29]
168-
+ Op.JUMPI
169-
+ Op.STOP
170-
+ Op.JUMPDEST
171-
+ Op.PUSH0
172-
+ Op.PUSH1[0x20]
173-
+ Op.DUP2
174-
+ Op.PUSH1[0x1]
175-
+ Op.DUP2
176-
+ Op.SWAP5
177-
+ Op.DUP2
178-
+ Op.DUP4
179-
+ Op.MSTORE
180-
+ Op.GASLIMIT
181-
+ Op.CALL
182-
+ Op.PUSH0
183-
+ Op.DUP1
184-
+ Op.REVERT
185-
+ Op.JUMPDEST
186-
+ Op.PUSH0
187-
+ Op.PUSH1[0x20]
160+
+ Op.PUSH1(0x20)
188161
+ Op.DUP2
189-
+ Op.PUSH1[0x1]
162+
+ Op.PUSH1(0x1)
190163
+ Op.DUP7
191164
+ Op.DUP3
192165
+ Op.SWAP6
@@ -226,75 +199,42 @@ def recursive_revert_contract_code(
226199
+ Op.DUP1
227200
+ Op.RETURN
228201
)
202+
else: # selfdestruct_on_outer_call is neither 1 nor 2
203+
suffix = (
204+
Op.SWAP1
205+
+ Op.POP
206+
+ Op.PUSH0
207+
+ Op.DUP1
208+
+ Op.MSTORE
209+
+ Op.PUSH0
210+
+ Op.DUP1
211+
+ Op.PUSH1(0x20)
212+
+ Op.DUP2
213+
+ Op.PUSH1(0x1)
214+
+ Op.DUP7
215+
+ Op.GASLIMIT
216+
+ Op.CALL
217+
+ Op.POP
218+
+ Op.PUSH0
219+
+ Op.MSTORE
220+
+ Op.PUSH0
221+
+ Op.DUP1
222+
+ Op.PUSH1(0x20)
223+
+ Op.DUP2
224+
+ Op.DUP1
225+
+ Op.ADDRESS
226+
+ Op.GASLIMIT
227+
+ Op.CALL
228+
+ Op.POP
229+
+ Op.BALANCE
230+
+ Op.PUSH1(0x1)
231+
+ Op.SSTORE
232+
+ Op.PUSH0
233+
+ Op.DUP1
234+
+ Op.RETURN
235+
)
229236

230-
# if not 1 and not 2 return this:
231-
return (
232-
Op.PUSH0
233-
+ Op.CALLDATALOAD
234-
+ Op.PUSH1[0x1]
235-
+ Op.PUSH20[selfdestruct_with_transfer_contract_address]
236-
+ Op.SWAP2
237-
+ Op.SWAP1
238-
+ Op.DUP2
239-
+ Op.PUSH0
240-
+ Op.EQ
241-
+ Op.PUSH1[0x3A]
242-
+ Op.JUMPI
243-
+ Op.POP
244-
+ Op.PUSH1[0x1]
245-
+ Op.EQ
246-
+ Op.PUSH1[0x29]
247-
+ Op.JUMPI
248-
+ Op.STOP
249-
+ Op.JUMPDEST
250-
+ Op.PUSH0
251-
+ Op.PUSH1[0x20]
252-
+ Op.DUP2
253-
+ Op.PUSH1[0x1]
254-
+ Op.DUP2
255-
+ Op.SWAP5
256-
+ Op.DUP2
257-
+ Op.DUP4
258-
+ Op.MSTORE
259-
+ Op.GASLIMIT
260-
+ Op.CALL
261-
+ Op.PUSH0
262-
+ Op.DUP1
263-
+ Op.REVERT
264-
+ Op.JUMPDEST
265-
+ Op.SWAP1
266-
+ Op.POP
267-
+ Op.PUSH0
268-
+ Op.DUP1
269-
+ Op.MSTORE
270-
+ Op.PUSH0
271-
+ Op.DUP1
272-
+ Op.PUSH1[0x20]
273-
+ Op.DUP2
274-
+ Op.PUSH1[0x1]
275-
+ Op.DUP7
276-
+ Op.GASLIMIT
277-
+ Op.CALL
278-
+ Op.POP
279-
+ Op.PUSH0
280-
+ Op.MSTORE
281-
+ Op.PUSH0
282-
+ Op.DUP1
283-
+ Op.PUSH1[0x20]
284-
+ Op.DUP2
285-
+ Op.DUP1
286-
+ Op.ADDRESS
287-
+ Op.GASLIMIT
288-
+ Op.CALL
289-
+ Op.POP
290-
+ Op.BALANCE
291-
+ Op.PUSH1[0x1]
292-
+ Op.SSTORE
293-
+ Op.PUSH0
294-
+ Op.DUP1
295-
+ Op.RETURN
296-
)
297-
# TODO: above is wip, this does not fill yet
237+
return common_prefix + suffix
298238

299239

300240
@pytest.fixture
@@ -317,28 +257,26 @@ def selfdestruct_with_transfer_contract_code(selfdestruct_recipient_address: Add
317257
code: Bytecode = (
318258
Op.PUSH0
319259
+ Op.CALLDATALOAD
320-
+ Op.PUSH20[selfdestruct_recipient_address]
260+
+ Op.PUSH20(selfdestruct_recipient_address)
321261
+ Op.SWAP1
322-
+ Op.DUP1
323-
+ Op.PUSH0
324-
+ Op.EQ
325-
+ Op.PUSH1[0x2F]
262+
+ Op.EQ(Op.PUSH0, Op.DUP1)
263+
+ Op.PUSH1(0x2F)
326264
+ Op.JUMPI
327-
+ Op.PUSH1[0x1]
265+
+ Op.PUSH1(0x1)
328266
+ Op.EQ
329-
+ Op.PUSH1[0x25]
267+
+ Op.PUSH1(0x25)
330268
+ Op.JUMPI
331269
+ Op.STOP
332270
+ Op.JUMPDEST
333-
+ Op.PUSH1[0x1]
271+
+ Op.PUSH1(0x1)
334272
+ Op.DUP1
335273
+ Op.SLOAD
336274
+ Op.ADD
337-
+ Op.PUSH1[0x1]
275+
+ Op.PUSH1(0x1)
338276
+ Op.SSTORE
339277
+ Op.SELFDESTRUCT
340278
+ Op.JUMPDEST
341-
+ Op.PUSH1[0x1]
279+
+ Op.PUSH1(0x1)
342280
+ Op.PUSH0
343281
+ Op.SLOAD
344282
+ Op.ADD

0 commit comments

Comments
 (0)