-
-
Notifications
You must be signed in to change notification settings - Fork 803
/
test_external_contract_calls.py
542 lines (406 loc) · 12.1 KB
/
test_external_contract_calls.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
from vyper.exceptions import StructureException, VariableDeclarationException, InvalidTypeException
def test_external_contract_calls(get_contract_with_gas_estimation):
contract_1 = """
@public
def foo(arg1: int128) -> int128:
return arg1
"""
c = get_contract_with_gas_estimation(contract_1)
contract_2 = """
class Foo():
def foo(arg1: int128) -> int128: pass
@public
def bar(arg1: address, arg2: int128) -> int128:
return Foo(arg1).foo(arg2)
"""
c2 = get_contract_with_gas_estimation(contract_2)
assert c2.bar(c.address, 1) == 1
print('Successfully executed an external contract call')
def test_complicated_external_contract_calls(get_contract_with_gas_estimation):
contract_1 = """
lucky: public(int128)
@public
def __init__(_lucky: int128):
self.lucky = _lucky
@public
def foo() -> int128:
return self.lucky
@public
def array() -> bytes <= 3:
return 'dog'
"""
lucky_number = 7
c = get_contract_with_gas_estimation(contract_1, args=[lucky_number])
contract_2 = """
class Foo():
def foo() -> int128: pass
def array() -> bytes <= 3: pass
@public
def bar(arg1: address) -> int128:
return Foo(arg1).foo()
"""
c2 = get_contract_with_gas_estimation(contract_2)
assert c2.bar(c.address) == lucky_number
print('Successfully executed a complicated external contract call')
def test_external_contract_calls_with_bytes(get_contract_with_gas_estimation):
contract_1 = """
@public
def array() -> bytes <= 3:
return 'dog'
"""
c = get_contract_with_gas_estimation(contract_1)
contract_2 = """
class Foo():
def array() -> bytes <= 3: pass
@public
def get_array(arg1: address) -> bytes <= 3:
return Foo(arg1).array()
"""
c2 = get_contract_with_gas_estimation(contract_2)
assert c2.get_array(c.address) == b'dog'
def test_external_contract_call_state_change(get_contract):
contract_1 = """
lucky: public(int128)
@public
def set_lucky(_lucky: int128):
self.lucky = _lucky
"""
lucky_number = 7
c = get_contract(contract_1)
contract_2 = """
class Foo():
def set_lucky(_lucky: int128): pass
@public
def set_lucky(arg1: address, arg2: int128):
Foo(arg1).set_lucky(arg2)
"""
c2 = get_contract(contract_2)
assert c.lucky() == 0
c2.set_lucky(c.address, lucky_number)
assert c.lucky() == lucky_number
print('Successfully executed an external contract call state change')
def test_constant_external_contract_call_cannot_change_state(assert_tx_failed, get_contract_with_gas_estimation):
contract_1 = """
lucky: public(int128)
@public
def set_lucky(_lucky: int128) -> int128:
self.lucky = _lucky
return _lucky
"""
lucky_number = 7
c = get_contract_with_gas_estimation(contract_1)
contract_2 = """
class Foo():
def set_lucky(_lucky: int128) -> int128: pass
@public
@constant
def set_lucky_expr(arg1: address, arg2: int128):
Foo(arg1).set_lucky(arg2)
@public
@constant
def set_lucky_stmt(arg1: address, arg2: int128) -> int128:
return Foo(arg1).set_lucky(arg2)
"""
c2 = get_contract_with_gas_estimation(contract_2)
assert_tx_failed(lambda: c2.set_lucky_expr(c.address, lucky_number))
assert_tx_failed(lambda: c2.set_lucky_stmt(c.address, lucky_number))
print('Successfully tested an constant external contract call attempted state change')
def test_external_contract_can_be_changed_based_on_address(get_contract):
contract_1 = """
lucky: public(int128)
@public
def set_lucky(_lucky: int128):
self.lucky = _lucky
"""
lucky_number_1 = 7
c = get_contract(contract_1)
contract_2 = """
lucky: public(int128)
@public
def set_lucky(_lucky: int128) -> int128:
self.lucky = _lucky
return self.lucky
"""
lucky_number_2 = 3
c2 = get_contract(contract_2)
contract_3 = """
class Foo():
def set_lucky(_lucky: int128): pass
@public
def set_lucky(arg1: address, arg2: int128):
Foo(arg1).set_lucky(arg2)
"""
c3 = get_contract(contract_3)
c3.set_lucky(c.address, lucky_number_1)
c3.set_lucky(c2.address, lucky_number_2)
assert c.lucky() == lucky_number_1
assert c2.lucky() == lucky_number_2
print('Successfully executed multiple external contract calls to different contracts based on address')
def test_external_contract_calls_with_public_globals(get_contract):
contract_1 = """
lucky: public(int128)
@public
def __init__(_lucky: int128):
self.lucky = _lucky
"""
lucky_number = 7
c = get_contract(contract_1, args=[lucky_number])
contract_2 = """
class Foo():
def lucky() -> int128: pass
@public
def bar(arg1: address) -> int128:
return Foo(arg1).lucky()
"""
c2 = get_contract(contract_2)
assert c2.bar(c.address) == lucky_number
print('Successfully executed an external contract call with public globals')
def test_external_contract_calls_with_multiple_contracts(get_contract):
contract_1 = """
lucky: public(int128)
@public
def __init__(_lucky: int128):
self.lucky = _lucky
"""
lucky_number = 7
c = get_contract(contract_1, args=[lucky_number])
contract_2 = """
class Foo():
def lucky() -> int128: pass
magic_number: public(int128)
@public
def __init__(arg1: address):
self.magic_number = Foo(arg1).lucky()
"""
c2 = get_contract(contract_2, args=[c.address])
contract_3 = """
class Bar():
def magic_number() -> int128: pass
best_number: public(int128)
@public
def __init__(arg1: address):
self.best_number = Bar(arg1).magic_number()
"""
c3 = get_contract(contract_3, args=[c2.address])
assert c3.best_number() == lucky_number
print('Successfully executed a multiple external contract calls')
def test_invalid_external_contract_call_to_the_same_contract(assert_tx_failed, get_contract):
contract_1 = """
@public
def bar() -> int128:
return 1
"""
contract_2 = """
class Bar():
def bar() -> int128: pass
@public
def bar() -> int128:
return 1
@public
def _stmt(x: address):
Bar(x).bar()
@public
def _expr(x: address) -> int128:
return Bar(x).bar()
"""
c1 = get_contract(contract_1)
c2 = get_contract(contract_2)
c2._stmt(c1.address)
c2._expr(c1.address)
assert_tx_failed(lambda: c2._stmt(c2.address))
assert_tx_failed(lambda: c2._expr(c2.address))
def test_invalid_nonexistent_contract_call(t, assert_tx_failed, get_contract):
contract_1 = """
@public
def bar() -> int128:
return 1
"""
contract_2 = """
class Bar():
def bar() -> int128: pass
@public
def foo(x: address) -> int128:
return Bar(x).bar()
"""
c1 = get_contract(contract_1)
c2 = get_contract(contract_2)
assert c2.foo(c1.address) == 1
assert_tx_failed(lambda: c2.foo(t.a1))
assert_tx_failed(lambda: c2.foo(t.a7))
def test_invalid_contract_reference_declaration(assert_tx_failed, get_contract):
contract = """
class Bar():
get_magic_number: 1
best_number: public(int128)
@public
def __init__():
pass
"""
assert_tx_failed(lambda: get_contract(contract), exception=StructureException)
def test_invalid_contract_reference_call(assert_tx_failed, get_contract):
contract = """
@public
def bar(arg1: address, arg2: int128) -> int128:
return Foo(arg1).foo(arg2)
"""
assert_tx_failed(lambda: get_contract(contract), exception=VariableDeclarationException)
def test_invalid_contract_reference_return_type(assert_tx_failed, get_contract):
contract = """
class Foo():
def foo(arg2: int128) -> invalid: pass
@public
def bar(arg1: address, arg2: int128) -> int128:
return Foo(arg1).foo(arg2)
"""
assert_tx_failed(lambda: get_contract(contract), exception=InvalidTypeException)
def test_external_contracts_must_be_declared_first_1(assert_tx_failed, get_contract):
contract = """
item: public(int128)
class Foo():
def foo(arg2: int128) -> int128: pass
"""
assert_tx_failed(lambda: get_contract(contract), exception=StructureException)
def test_external_contracts_must_be_declared_first_2(assert_tx_failed, get_contract):
contract = """
MyLog: event({})
class Foo():
def foo(arg2: int128) -> int128: pass
"""
assert_tx_failed(lambda: get_contract(contract), exception=StructureException)
def test_external_contracts_must_be_declared_first_3(assert_tx_failed, get_contract):
contract = """
@public
def foo() -> int128:
return 1
class Foo():
def foo(arg2: int128) -> int128: pass
"""
assert_tx_failed(lambda: get_contract(contract), exception=StructureException)
def test_external_contract_call_declaration_expr(get_contract):
contract_1 = """
@public
def bar() -> int128:
return 1
"""
contract_2 = """
class Bar():
def bar() -> int128: pass
bar_contract: Bar
@public
def foo(contract_address: contract(Bar)) -> int128:
self.bar_contract = contract_address
return self.bar_contract.bar()
"""
c1 = get_contract(contract_1)
c2 = get_contract(contract_2)
assert c2.foo(c1.address) == 1
def test_external_contract_call_declaration_stmt(get_contract):
contract_1 = """
lucky: int128
@public
def set_lucky(_lucky: int128):
self.lucky = _lucky
@public
def get_lucky() -> int128:
return self.lucky
"""
contract_2 = """
class Bar():
def set_lucky(arg1: int128): pass
def get_lucky() -> int128: pass
bar_contract: Bar
@public
def set_lucky(contract_address: contract(Bar)):
self.bar_contract = contract_address
self.bar_contract.set_lucky(1)
@public
def get_lucky(contract_address: contract(Bar)) -> int128:
self.bar_contract = contract_address
return self.bar_contract.get_lucky()
"""
c1 = get_contract(contract_1)
c2 = get_contract(contract_2)
assert c1.get_lucky() == 0
assert c2.get_lucky(c1.address) == 0
c1.set_lucky(6)
assert c1.get_lucky() == 6
assert c2.get_lucky(c1.address) == 6
c2.set_lucky(c1.address)
assert c1.get_lucky() == 1
assert c2.get_lucky(c1.address) == 1
def test_complex_external_contract_call_declaration(get_contract_with_gas_estimation):
contract_1 = """
@public
def get_lucky() -> int128:
return 1
"""
contract_2 = """
@public
def get_lucky() -> int128:
return 2
"""
contract_3 = """
class Bar():
def set_lucky(arg1: int128): pass
def get_lucky() -> int128: pass
bar_contract: Bar
@public
def set_contract(contract_address: contract(Bar)):
self.bar_contract = contract_address
@public
def get_lucky() -> int128:
return self.bar_contract.get_lucky()
"""
c1 = get_contract_with_gas_estimation(contract_1)
c2 = get_contract_with_gas_estimation(contract_2)
c3 = get_contract_with_gas_estimation(contract_3)
assert c1.get_lucky() == 1
assert c2.get_lucky() == 2
c3.set_contract(c1.address)
assert c3.get_lucky() == 1
c3.set_contract(c2.address)
assert c3.get_lucky() == 2
def test_address_can_returned_from_contract_type(get_contract, utils):
contract_1 = """
@public
def bar() -> int128:
return 1
"""
contract_2 = """
class Bar():
def bar() -> int128: pass
bar_contract: public(Bar)
@public
def foo(contract_address: contract(Bar)):
self.bar_contract = contract_address
@public
def get_bar() -> int128:
return self.bar_contract.bar()
"""
c1 = get_contract(contract_1)
c2 = get_contract(contract_2)
c2.foo(c1.address)
assert utils.remove_0x_head(c2.bar_contract()) == c1.address.hex()
assert c2.get_bar() == 1
def test_invalid_external_contract_call_declaration_1(assert_compile_failed, get_contract):
contract_1 = """
class Bar():
def bar() -> int128: pass
bar_contract: Bar
@public
def foo(contract_address: contract(Boo)) -> int128:
self.bar_contract = contract_address
return self.bar_contract.bar()
"""
assert_compile_failed(lambda: get_contract(contract_1), InvalidTypeException)
def test_invalid_external_contract_call_declaration_2(assert_compile_failed, get_contract):
contract_1 = """
class Bar():
def bar() -> int128: pass
bar_contract: Boo
@public
def foo(contract_address: contract(Bar)) -> int128:
self.bar_contract = contract_address
return self.bar_contract.bar()
"""
assert_compile_failed(lambda: get_contract(contract_1), InvalidTypeException)