forked from vyperlang/vyper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_abi_encode.py
373 lines (313 loc) · 11.7 KB
/
test_abi_encode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
from decimal import Decimal
import pytest
from eth.codecs import abi
# @pytest.mark.parametrize("string", ["a", "abc", "abcde", "potato"])
def test_abi_encode(get_contract):
code = """
struct Animal:
name: String[5]
address_: address
id_: int128
is_furry: bool
price: decimal
data: uint256[3]
metadata: bytes32
struct Human:
name: String[64]
pet: Animal
@external
# TODO accept struct input once the functionality is available
def abi_encode(
name: String[64],
pet_name: String[5],
pet_address: address,
pet_id: int128,
pet_is_furry: bool,
pet_price: decimal,
pet_data: uint256[3],
pet_metadata: bytes32,
ensure_tuple: bool,
include_method_id: bool
) -> Bytes[548]:
human: Human = Human(
name=name,
pet=Animal(
name=pet_name,
address_=pet_address,
id_=pet_id,
is_furry=pet_is_furry,
price=pet_price,
data=pet_data,
metadata=pet_metadata
),
)
if ensure_tuple:
if not include_method_id:
return _abi_encode(human) # default ensure_tuple=True
return _abi_encode(human, method_id=0xdeadbeef)
else:
if not include_method_id:
return _abi_encode(human, ensure_tuple=False)
return _abi_encode(human, ensure_tuple=False, method_id=0xdeadbeef)
@external
def abi_encode2(name: String[32], ensure_tuple: bool, include_method_id: bool) -> Bytes[100]:
if ensure_tuple:
if not include_method_id:
return _abi_encode(name) # default ensure_tuple=True
return _abi_encode(name, method_id=0xdeadbeef)
else:
if not include_method_id:
return _abi_encode(name, ensure_tuple=False)
return _abi_encode(name, ensure_tuple=False, method_id=0xdeadbeef)
@external
def abi_encode3(x: uint256, ensure_tuple: bool, include_method_id: bool) -> Bytes[36]:
if ensure_tuple:
if not include_method_id:
return _abi_encode(x) # default ensure_tuple=True
return _abi_encode(x, method_id=0xdeadbeef)
else:
if not include_method_id:
return _abi_encode(x, ensure_tuple=False)
return _abi_encode(x, ensure_tuple=False, method_id=0xdeadbeef)
"""
c = get_contract(code)
method_id = 0xDEADBEEF.to_bytes(4, "big")
# test each method once each with ensure_tuple set to True and False
arg = 123
assert c.abi_encode3(arg, False, False).hex() == abi.encode("uint256", arg).hex()
assert c.abi_encode3(arg, True, False).hex() == abi.encode("(uint256)", (arg,)).hex()
assert c.abi_encode3(arg, False, True).hex() == (method_id + abi.encode("uint256", arg)).hex()
assert (
c.abi_encode3(arg, True, True).hex() == (method_id + abi.encode("(uint256)", (arg,))).hex()
)
arg = "some string"
assert c.abi_encode2(arg, False, False).hex() == abi.encode("string", arg).hex()
assert c.abi_encode2(arg, True, False).hex() == abi.encode("(string)", (arg,)).hex()
assert c.abi_encode2(arg, False, True).hex() == (method_id + abi.encode("string", arg)).hex()
assert (
c.abi_encode2(arg, True, True).hex() == (method_id + abi.encode("(string)", (arg,))).hex()
)
test_addr = "0x" + b"".join(chr(i).encode("utf-8") for i in range(20)).hex()
test_bytes32 = b"".join(chr(i).encode("utf-8") for i in range(32))
human_tuple = (
"foobar",
("vyper", test_addr, 123, True, Decimal("123.4"), [123, 456, 789], test_bytes32),
)
args = tuple([human_tuple[0]] + list(human_tuple[1]))
human_t = "(string,(string,address,int128,bool,fixed168x10,uint256[3],bytes32))"
human_encoded = abi.encode(human_t, human_tuple)
assert c.abi_encode(*args, False, False).hex() == human_encoded.hex()
assert c.abi_encode(*args, False, True).hex() == (method_id + human_encoded).hex()
human_encoded = abi.encode(f"({human_t})", (human_tuple,))
assert c.abi_encode(*args, True, False).hex() == human_encoded.hex()
assert c.abi_encode(*args, True, True).hex() == (method_id + human_encoded).hex()
@pytest.mark.parametrize("type,value", [("Bytes", b"hello"), ("String", "hello")])
def test_abi_encode_length_failing(get_contract, assert_compile_failed, type, value):
code = f"""
struct WrappedBytes:
bs: {type}[6]
@internal
def foo():
x: WrappedBytes = WrappedBytes(bs={value})
y: {type}[96] = _abi_encode(x, ensure_tuple=True) # should be Bytes[128]
"""
assert_compile_failed(lambda: get_contract(code))
def test_abi_encode_dynarray(get_contract):
code = """
@external
def abi_encode(d: DynArray[uint256, 3], ensure_tuple: bool, include_method_id: bool) -> Bytes[164]:
if ensure_tuple:
if not include_method_id:
return _abi_encode(d) # default ensure_tuple=True
return _abi_encode(d, method_id=0xdeadbeef)
else:
if not include_method_id:
return _abi_encode(d, ensure_tuple=False)
return _abi_encode(d, ensure_tuple=False, method_id=0xdeadbeef)
"""
c = get_contract(code)
method_id = 0xDEADBEEF.to_bytes(4, "big")
arg = [123, 456, 789]
assert c.abi_encode(arg, False, False).hex() == abi.encode("uint256[]", arg).hex()
assert c.abi_encode(arg, True, False).hex() == abi.encode("(uint256[])", (arg,)).hex()
assert c.abi_encode(arg, False, True).hex() == (method_id + abi.encode("uint256[]", arg)).hex()
assert (
c.abi_encode(arg, True, True).hex() == (method_id + abi.encode("(uint256[])", (arg,))).hex()
)
nested_2d_array_args = [
[[123, 456, 789], [234, 567, 891], [345, 678, 912]],
[[], [], []],
[[123, 456], [234, 567, 891]],
[[123, 456, 789], [234, 567], [345]],
[[123], [], [345, 678, 912]],
[[], [], [345, 678, 912]],
[[], [], [345]],
[[], [234], []],
[[], [234, 567, 891], []],
[[]],
[[123], [234]],
]
@pytest.mark.parametrize("args", nested_2d_array_args)
def test_abi_encode_nested_dynarray(get_contract, args):
code = """
@external
def abi_encode(
d: DynArray[DynArray[uint256, 3], 3], ensure_tuple: bool, include_method_id: bool
) -> Bytes[548]:
if ensure_tuple:
if not include_method_id:
return _abi_encode(d) # default ensure_tuple=True
return _abi_encode(d, method_id=0xdeadbeef)
else:
if not include_method_id:
return _abi_encode(d, ensure_tuple=False)
return _abi_encode(d, ensure_tuple=False, method_id=0xdeadbeef)
"""
c = get_contract(code)
method_id = 0xDEADBEEF.to_bytes(4, "big")
assert c.abi_encode(args, False, False).hex() == abi.encode("uint256[][]", args).hex()
assert c.abi_encode(args, True, False).hex() == abi.encode("(uint256[][])", (args,)).hex()
assert (
c.abi_encode(args, False, True).hex() == (method_id + abi.encode("uint256[][]", args)).hex()
)
assert (
c.abi_encode(args, True, True).hex()
== (method_id + abi.encode("(uint256[][])", (args,))).hex()
)
nested_3d_array_args = [
[
[[123, 456, 789], [234, 567, 891], [345, 678, 912]],
[[234, 567, 891], [345, 678, 912], [123, 456, 789]],
[[345, 678, 912], [123, 456, 789], [234, 567, 891]],
],
[[[123, 789], [234], [345, 678, 912]], [[234, 567], [345, 678]], [[345]]],
[[[123], [234, 567, 891]], [[234]]],
[[[], [], []], [[], [], []], [[], [], []]],
[[[123, 456, 789], [234, 567, 891], [345, 678, 912]], [[234, 567, 891], [345, 678, 912]], [[]]],
[[[]], [[]], [[234]]],
[[[123]], [[]], [[]]],
[[[]], [[123]], [[]]],
[[[123, 456, 789], [234, 567]], [[234]], [[567], [912], [345]]],
[[[]]],
]
@pytest.mark.parametrize("args", nested_3d_array_args)
def test_abi_encode_nested_dynarray_2(get_contract, args):
code = """
@external
def abi_encode(
d: DynArray[DynArray[DynArray[uint256, 3], 3], 3],
ensure_tuple: bool,
include_method_id: bool
) -> Bytes[1700]:
if ensure_tuple:
if not include_method_id:
return _abi_encode(d) # default ensure_tuple=True
return _abi_encode(d, method_id=0xdeadbeef)
else:
if not include_method_id:
return _abi_encode(d, ensure_tuple=False)
return _abi_encode(d, ensure_tuple=False, method_id=0xdeadbeef)
"""
c = get_contract(code)
method_id = 0xDEADBEEF.to_bytes(4, "big")
assert c.abi_encode(args, False, False).hex() == abi.encode("uint256[][][]", args).hex()
assert c.abi_encode(args, True, False).hex() == abi.encode("(uint256[][][])", (args,)).hex()
assert (
c.abi_encode(args, False, True).hex()
== (method_id + abi.encode("uint256[][][]", args)).hex()
)
assert (
c.abi_encode(args, True, True).hex()
== (method_id + abi.encode("(uint256[][][])", (args,))).hex()
)
def test_side_effects_evaluation(get_contract):
contract_1 = """
counter: uint256
@deploy
def __init__():
self.counter = 0
@external
def get_counter() -> (uint256, String[6]):
self.counter += 1
return (self.counter, "hello")
"""
c = get_contract(contract_1)
contract_2 = """
interface Foo:
def get_counter() -> (uint256, String[6]): nonpayable
@external
def foo(addr: address) -> Bytes[164]:
return _abi_encode(extcall Foo(addr).get_counter(), method_id=0xdeadbeef)
"""
c2 = get_contract(contract_2)
method_id = 0xDEADBEEF.to_bytes(4, "big")
# call to get_counter() should be evaluated only once
get_counter_encoded = abi.encode("((uint256,string))", ((1, "hello"),))
assert c2.foo(c.address).hex() == (method_id + get_counter_encoded).hex()
# test _abi_encode in private functions to check buffer overruns
def test_abi_encode_private(get_contract):
code = """
bytez: Bytes[96]
@internal
def _foo(bs: Bytes[32]):
self.bytez = _abi_encode(bs)
@external
def foo(bs: Bytes[32]) -> (uint256, Bytes[96]):
dont_clobber_me: uint256 = max_value(uint256)
self._foo(bs)
return dont_clobber_me, self.bytez
"""
c = get_contract(code)
bs = b"\x00" * 32
assert c.foo(bs) == [2**256 - 1, abi.encode("(bytes)", (bs,))]
def test_abi_encode_private_dynarray(get_contract):
code = """
bytez: Bytes[160]
@internal
def _foo(bs: DynArray[uint256, 3]):
self.bytez = _abi_encode(bs)
@external
def foo(bs: DynArray[uint256, 3]) -> (uint256, Bytes[160]):
dont_clobber_me: uint256 = max_value(uint256)
self._foo(bs)
return dont_clobber_me, self.bytez
"""
c = get_contract(code)
bs = [1, 2, 3]
assert c.foo(bs) == [2**256 - 1, abi.encode("(uint256[])", (bs,))]
def test_abi_encode_private_nested_dynarray(get_contract):
code = """
bytez: Bytes[1696]
@internal
def _foo(bs: DynArray[DynArray[DynArray[uint256, 3], 3], 3]):
self.bytez = _abi_encode(bs)
@external
def foo(bs: DynArray[DynArray[DynArray[uint256, 3], 3], 3]) -> (uint256, Bytes[1696]):
dont_clobber_me: uint256 = max_value(uint256)
self._foo(bs)
return dont_clobber_me, self.bytez
"""
c = get_contract(code)
bs = [
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[10, 11, 12], [13, 14, 15], [16, 17, 18]],
[[19, 20, 21], [22, 23, 24], [25, 26, 27]],
]
assert c.foo(bs) == [2**256 - 1, abi.encode("(uint256[][][])", (bs,))]
@pytest.mark.parametrize("empty_literal", ('b""', '""', "empty(Bytes[1])", "empty(String[1])"))
def test_abi_encode_empty_string(get_contract, empty_literal):
code = f"""
@external
def foo(ensure_tuple: bool) -> Bytes[96]:
if ensure_tuple:
return _abi_encode({empty_literal}) # default ensure_tuple=True
else:
return _abi_encode({empty_literal}, ensure_tuple=False)
"""
c = get_contract(code)
# eth-abi does not encode zero-length string correctly -
# see https://github.com/ethereum/eth-abi/issues/157
expected_output = b"\x00" * 32
assert c.foo(False) == expected_output
expected_output = b"\x00" * 31 + b"\x20" + b"\x00" * 32
assert c.foo(True) == expected_output