-
Notifications
You must be signed in to change notification settings - Fork 45
/
test_token_network.py
261 lines (235 loc) · 9.4 KB
/
test_token_network.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
from typing import Callable
import pytest
from eth_tester.exceptions import TransactionFailed
from web3.contract import Contract
from raiden_contracts.constants import EMPTY_ADDRESS, TEST_SETTLE_TIMEOUT
from raiden_contracts.tests.utils.constants import DEPLOYER_ADDRESS, NOT_ADDRESS
from raiden_contracts.tests.utils.contracts import call_and_transact
def test_constructor_call(
get_token_network: Callable,
custom_token: Contract,
secret_registry_contract: Contract,
get_accounts: Callable,
channel_participant_deposit_limit: int,
token_network_deposit_limit: int,
) -> None:
"""Try to deploy TokenNetwork with various wrong arguments"""
(A, controller) = get_accounts(2)
# failure with no arguments
with pytest.raises(TypeError):
get_token_network([])
# failures with integers instead of a Token address
with pytest.raises(TypeError):
get_token_network(
[
3,
secret_registry_contract.address,
controller,
channel_participant_deposit_limit,
token_network_deposit_limit,
]
)
with pytest.raises(TypeError):
get_token_network(
[
0,
secret_registry_contract.address,
controller,
channel_participant_deposit_limit,
token_network_deposit_limit,
]
)
# failures with non-address strings instead of a Token address
with pytest.raises(TypeError):
get_token_network(
[
"",
secret_registry_contract.address,
controller,
channel_participant_deposit_limit,
token_network_deposit_limit,
]
)
with pytest.raises(TypeError):
get_token_network(
[
NOT_ADDRESS,
secret_registry_contract.address,
controller,
channel_participant_deposit_limit,
token_network_deposit_limit,
]
)
# failures with integers instead of a SecretRegistry address
with pytest.raises(TypeError):
get_token_network(
[
custom_token.address,
3,
controller,
channel_participant_deposit_limit,
token_network_deposit_limit,
]
)
with pytest.raises(TypeError):
get_token_network(
[
custom_token.address,
0,
controller,
channel_participant_deposit_limit,
token_network_deposit_limit,
]
)
# failures with non-address strings instead of a SecretRegistry address
with pytest.raises(TypeError):
get_token_network(
[
custom_token.address,
"",
controller,
channel_participant_deposit_limit,
token_network_deposit_limit,
]
)
with pytest.raises(TypeError):
get_token_network(
[
custom_token.address,
NOT_ADDRESS,
controller,
channel_participant_deposit_limit,
token_network_deposit_limit,
]
)
# failures with Ethereum addresses that don't contain a Token contract
with pytest.raises(TransactionFailed, match="TN: invalid token address"):
get_token_network(
_token_address=EMPTY_ADDRESS,
_secret_registry=secret_registry_contract.address,
_controller=controller,
_channel_participant_deposit_limit=channel_participant_deposit_limit,
_token_network_deposit_limit=token_network_deposit_limit,
_settle_timeout=TEST_SETTLE_TIMEOUT,
)
with pytest.raises(TransactionFailed, match="TN: invalid token"):
get_token_network(
_token_address=A,
_secret_registry=secret_registry_contract.address,
_controller=controller,
_channel_participant_deposit_limit=channel_participant_deposit_limit,
_token_network_deposit_limit=token_network_deposit_limit,
_settle_timeout=TEST_SETTLE_TIMEOUT,
)
with pytest.raises(TransactionFailed):
get_token_network(
_token_address=secret_registry_contract.address,
_secret_registry=secret_registry_contract.address,
_controller=controller,
_channel_participant_deposit_limit=channel_participant_deposit_limit,
_token_network_deposit_limit=token_network_deposit_limit,
_settle_timeout=TEST_SETTLE_TIMEOUT,
)
# failures with Ethereum addresses that don't contain the SecretRegistry contract
with pytest.raises(TransactionFailed, match="TN: invalid SR address"):
get_token_network(
_token_address=custom_token.address,
_secret_registry=EMPTY_ADDRESS,
_controller=controller,
_channel_participant_deposit_limit=channel_participant_deposit_limit,
_token_network_deposit_limit=token_network_deposit_limit,
_settle_timeout=TEST_SETTLE_TIMEOUT,
)
with pytest.raises(TransactionFailed, match="TN: invalid SR contract"):
get_token_network(
_token_address=custom_token.address,
_secret_registry=A,
_controller=controller,
_channel_participant_deposit_limit=channel_participant_deposit_limit,
_token_network_deposit_limit=token_network_deposit_limit,
_settle_timeout=TEST_SETTLE_TIMEOUT,
)
# failure with channel_participant_deposit_limit being zero
with pytest.raises(TransactionFailed, match="TN: invalid participant limit"):
get_token_network(
_token_address=custom_token.address,
_secret_registry=secret_registry_contract.address,
_controller=controller,
_channel_participant_deposit_limit=0,
_token_network_deposit_limit=token_network_deposit_limit,
_settle_timeout=TEST_SETTLE_TIMEOUT,
)
# failure with both limits being zero
with pytest.raises(TransactionFailed, match="TN: invalid participant limit"):
get_token_network(
_token_address=custom_token.address,
_secret_registry=secret_registry_contract.address,
_controller=controller,
_channel_participant_deposit_limit=0,
_token_network_deposit_limit=0,
_settle_timeout=TEST_SETTLE_TIMEOUT,
)
# failure with channel_participant_deposit_limit being bigger than
# token_network_deposit_limit.
with pytest.raises(TransactionFailed, match="TN: invalid deposit limits"):
get_token_network(
_token_address=custom_token.address,
_secret_registry=secret_registry_contract.address,
_controller=controller,
_channel_participant_deposit_limit=token_network_deposit_limit,
_token_network_deposit_limit=channel_participant_deposit_limit,
_settle_timeout=TEST_SETTLE_TIMEOUT,
)
# see a success to make sure that the above failures are meaningful
get_token_network(
_token_address=custom_token.address,
_secret_registry=secret_registry_contract.address,
_controller=controller,
_channel_participant_deposit_limit=channel_participant_deposit_limit,
_token_network_deposit_limit=token_network_deposit_limit,
_settle_timeout=TEST_SETTLE_TIMEOUT,
)
def test_token_network_variables(token_network: Contract) -> None:
"""Check values of storage variables of the TokenNetwork contract"""
assert token_network.functions.channel_counter().call() == 0
assert token_network.functions.signature_prefix().call() == "\x19Ethereum Signed Message:\n"
@pytest.mark.usefixtures("no_token_network")
def test_constructor_not_registered(
custom_token: Contract,
secret_registry_contract: Contract,
token_network_registry_contract: Contract,
token_network_external: Contract,
) -> None:
"""Check that the TokenNetwork refers to the right Token address and chain_id"""
token_network = token_network_external
assert token_network.functions.token().call() == custom_token.address
assert token_network.functions.secret_registry().call() == secret_registry_contract.address
# The TokenNetworkRegistry doesn't know about the TokenNetwork
assert (
token_network_registry_contract.functions.token_to_token_networks(
custom_token.address
).call()
== EMPTY_ADDRESS
)
def test_change_owner(
token_network_external: Contract,
get_accounts: Callable,
) -> None:
"""Address must be allowed to remove limits after if became owner"""
token_network = token_network_external
new_controller = get_accounts(1)[0]
# Must fail when controller is still DEPLOYER_ADDRESS
with pytest.raises(TransactionFailed, match="Can only be called by controller"):
call_and_transact(
token_network.functions.removeLimits(),
{"from": new_controller},
)
# Must succeed after change of controller
call_and_transact(
token_network.functions.changeController(new_controller),
{"from": DEPLOYER_ADDRESS},
)
call_and_transact(
token_network.functions.removeLimits(),
{"from": new_controller},
)