-
-
Notifications
You must be signed in to change notification settings - Fork 784
/
test_chainid.py
99 lines (86 loc) · 1.75 KB
/
test_chainid.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
import pytest
from pytest import (
raises,
)
from vyper import (
compiler,
)
from vyper.exceptions import (
TypeMismatchException,
)
fail_list = [
"""
@public
def foo() -> int128[2]:
return [3,chain.id]
""",
"""
@public
def foo() -> decimal(wei / sec):
x: int128(wei) = as_wei_value(5, "finney")
y: int128 = chain.id + 50
return x / y
""",
"""
@public
def foo():
x: bytes[10] = slice("cow", start=0, len=chain.id)
""",
"""
@public
def foo():
x: int128 = 7
y: int128 = min(x, chain.id)
""",
"""
a: map(timestamp, int128)
@public
def add_record():
self.a[chain.id] = chain.id + 20
""",
"""
a: map(int128, timestamp)
@public
def add_record():
self.a[chain.id] = chain.id + 20
""",
"""
@public
def foo(inp: bytes[10]) -> bytes[3]:
return slice(inp, start=chain.id, len=3)
""",
]
@pytest.mark.parametrize('bad_code', fail_list)
def test_chain_fail(bad_code):
if isinstance(bad_code, tuple):
with raises(bad_code[1]):
compiler.compile_code(bad_code[0])
else:
with raises(TypeMismatchException):
compiler.compile_code(bad_code)
valid_list = [
"""
@public
@constant
def get_chain_id() -> uint256:
return chain.id
""",
"""
@public
@constant
def check_chain_id(c: uint256) -> bool:
return chain.id == c
""",
]
@pytest.mark.parametrize('good_code', valid_list)
def test_chain_success(good_code):
assert compiler.compile_code(good_code) is not None
def test_chainid_operation(get_contract_with_gas_estimation):
code = """
@public
@constant
def get_chain_id() -> uint256:
return chain.id
"""
c = get_contract_with_gas_estimation(code)
assert c.get_chain_id() == 0 # Default value of py-evm