-
Notifications
You must be signed in to change notification settings - Fork 45
/
test_deploy_script.py
1240 lines (1098 loc) · 46.6 KB
/
test_deploy_script.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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
from copy import deepcopy
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import IO, Generator, List, Optional
from unittest.mock import MagicMock, patch
import pytest
from click import BadParameter, NoSuchOption
from click.testing import CliRunner
from eth_typing import HexStr
from eth_typing.evm import ChecksumAddress, HexAddress
from eth_utils import ValidationError, to_checksum_address
from pyfakefs.fake_filesystem import FakeFilesystem
from pyfakefs.fake_filesystem_unittest import Patcher
from web3 import Web3
from web3.contract import Contract
from web3.eth import Eth
import raiden_contracts
from raiden_contracts.constants import (
ALDERAAN_VERSION,
CONTRACT_MONITORING_SERVICE,
CONTRACT_ONE_TO_N,
CONTRACT_SECRET_REGISTRY,
CONTRACT_SERVICE_REGISTRY,
CONTRACT_TOKEN_NETWORK_REGISTRY,
CONTRACT_USER_DEPOSIT,
EMPTY_ADDRESS,
)
from raiden_contracts.contract_manager import DeployedContracts, contracts_precompiled_path
from raiden_contracts.deploy.__main__ import (
ContractDeployer,
ContractVerifier,
contracts_version_with_max_token_networks,
error_removed_option,
raiden,
register,
services,
token,
validate_address,
verify,
)
from raiden_contracts.deploy.contract_deployer import (
contracts_version_monitoring_service_takes_token_network_registry,
)
from raiden_contracts.deploy.contract_verifier import (
_verify_monitoring_service_deployment,
_verify_user_deposit_deployment,
)
from raiden_contracts.tests.utils import FAKE_ADDRESS, get_random_privkey
from raiden_contracts.tests.utils.constants import (
DEPLOYER_ADDRESS,
FAUCET_PRIVATE_KEY,
SECONDS_PER_DAY,
SERVICE_DEPOSIT,
UINT256_MAX,
)
from raiden_contracts.utils.versions import contracts_version_has_initial_service_deposit
GAS_LIMIT = 5860000
@pytest.fixture(scope="session")
def deployer(web3: Web3) -> ContractDeployer:
return ContractDeployer(
web3=web3,
private_key=FAUCET_PRIVATE_KEY,
gas_limit=GAS_LIMIT,
gas_price=1,
wait=10,
contracts_version=None,
)
@pytest.fixture(scope="session")
def deployer_0_37_0(web3: Web3) -> ContractDeployer:
return ContractDeployer(
web3=web3,
private_key=FAUCET_PRIVATE_KEY,
gas_limit=GAS_LIMIT,
gas_price=1,
wait=10,
contracts_version="0.37.0",
)
@pytest.mark.slow
@pytest.fixture(scope="session")
def deployed_raiden_info(deployer: ContractDeployer) -> DeployedContracts:
return deployer.deploy_raiden_contracts(
max_num_of_token_networks=1, reuse_secret_registry_from_deploy_file=None
)
@pytest.mark.slow
@pytest.fixture(scope="session")
def deployed_raiden_info2(deployer: ContractDeployer) -> DeployedContracts:
return deployer.deploy_raiden_contracts(
max_num_of_token_networks=1, reuse_secret_registry_from_deploy_file=None
)
TOKEN_SUPPLY = 10000000
@pytest.fixture(scope="session")
def token_address(deployer: ContractDeployer) -> HexAddress:
token_type = "CustomToken"
deployed_token = deployer.deploy_token_contract(
token_supply=TOKEN_SUPPLY,
token_decimals=18,
token_name="TestToken",
token_symbol="TTT",
token_type=token_type,
)
return deployed_token[token_type]
DEPOSIT_LIMIT = TOKEN_SUPPLY // 2
WITHDRAW_TIMEOUT = 25 * 60
@pytest.mark.slow
@pytest.fixture(scope="session")
def deployed_service_info(
deployer: ContractDeployer,
token_address: HexAddress,
token_network_registry_contract: Contract,
) -> DeployedContracts:
return deployer.deploy_service_contracts(
token_address=token_address,
user_deposit_whole_balance_limit=DEPOSIT_LIMIT,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
service_registry_controller=DEPLOYER_ADDRESS,
initial_service_deposit_price=SERVICE_DEPOSIT // 2,
service_deposit_bump_numerator=6,
service_deposit_bump_denominator=5,
decay_constant=200 * SECONDS_PER_DAY,
min_price=1000,
registration_duration=180 * SECONDS_PER_DAY,
token_network_registry_address=token_network_registry_contract.address,
reuse_service_registry_from_deploy_file=None,
)
@pytest.mark.slow
@pytest.fixture(scope="session")
def test_deploy_service_0_37_0(
deployer_0_37_0: ContractDeployer,
token_address: HexAddress,
token_network_registry_contract: Contract,
) -> None:
with pytest.raises(RuntimeError, match="older service contracts is not supported"):
deployer_0_37_0.deploy_service_contracts(
token_address=token_address,
user_deposit_whole_balance_limit=DEPOSIT_LIMIT,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
service_registry_controller=DEPLOYER_ADDRESS,
initial_service_deposit_price=SERVICE_DEPOSIT // 2,
service_deposit_bump_numerator=6,
service_deposit_bump_denominator=5,
decay_constant=200 * SECONDS_PER_DAY,
min_price=1000,
registration_duration=180 * SECONDS_PER_DAY,
token_network_registry_address=token_network_registry_contract.address,
reuse_service_registry_from_deploy_file=None,
)
@pytest.mark.parametrize(
"version,expectation",
[(ALDERAAN_VERSION, True), (None, True)],
)
def test_contracts_version_with_max_token_networks(
version: Optional[str], expectation: bool
) -> None:
assert contracts_version_with_max_token_networks(version) == expectation
@pytest.mark.parametrize(
"version,expectation",
[(ALDERAAN_VERSION, True), (None, True)],
)
def test_contracts_version_monitoring_service_takes_token_network_registry(
version: Optional[str], expectation: bool
) -> None:
assert (
contracts_version_monitoring_service_takes_token_network_registry(version) == expectation
)
@pytest.mark.slow
def test_deploy_script_raiden(
web3: Web3,
deployer: ContractDeployer,
deployed_raiden_info: DeployedContracts,
deployed_raiden_info2: DeployedContracts,
) -> None:
"""Run raiden contracts deployment function and tamper with deployed_contracts_info
This checks if deploy_raiden_contracts() works correctly in the happy case,
to make sure no code dependencies have been changed, affecting the deployment script.
This does not check however that the cli command works correctly.
This also tampers with deployed_contracts_info to make sure an error is raised in
verify_deployed_contracts()
"""
deployed_contracts_info = deployed_raiden_info
deployer.verify_deployment_data(deployment_data=deployed_contracts_info)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts_version"] = "0.0.0"
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployment_data=deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_SECRET_REGISTRY]["address"] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_SECRET_REGISTRY]["address"] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_TOKEN_NETWORK_REGISTRY][
"address"
] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_SECRET_REGISTRY]["block_number"] = 0
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_SECRET_REGISTRY]["block_number"] = 0
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_TOKEN_NETWORK_REGISTRY]["block_number"] = 0
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_TOKEN_NETWORK_REGISTRY]["gas_cost"] = 0
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_TOKEN_NETWORK_REGISTRY][
"address"
] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts_version"] = "0.4.0"
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_SECRET_REGISTRY] = deployed_raiden_info2[
"contracts"
][CONTRACT_SECRET_REGISTRY]
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_TOKEN_NETWORK_REGISTRY][
"constructor_arguments"
][0] = DEPLOYER_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
deployed_contracts_info_fail = deepcopy(deployed_contracts_info)
deployed_contracts_info_fail["contracts"][CONTRACT_TOKEN_NETWORK_REGISTRY][
"constructor_arguments"
] = []
with pytest.raises(RuntimeError):
deployer.verify_deployment_data(deployed_contracts_info_fail)
# check that it fails if sender has no eth
deployer = ContractDeployer(
web3=web3,
private_key=get_random_privkey(),
gas_limit=GAS_LIMIT,
gas_price=1,
wait=10,
)
with pytest.raises(ValidationError):
deployer.deploy_raiden_contracts(1, reuse_secret_registry_from_deploy_file=None)
def test_deploy_raiden_reuse_secret_registry(
deployer: ContractDeployer, deployed_raiden_info: DeployedContracts
) -> None:
"""Run deploy_raiden_contracts with a previous SecretRegistry deployment data"""
with NamedTemporaryFile() as previous_deployment_file:
previous_deployment_file.write(bytearray(json.dumps(deployed_raiden_info), "ascii"))
previous_deployment_file.flush()
new_deployment = deployer.deploy_raiden_contracts(
1, reuse_secret_registry_from_deploy_file=Path(previous_deployment_file.name)
)
assert (
new_deployment["contracts"][CONTRACT_SECRET_REGISTRY]
== deployed_raiden_info["contracts"][CONTRACT_SECRET_REGISTRY]
)
assert (
new_deployment["contracts"][CONTRACT_TOKEN_NETWORK_REGISTRY]
!= deployed_raiden_info["contracts"][CONTRACT_TOKEN_NETWORK_REGISTRY]
)
def test_deploy_services_reuse_service_registry(
deployer: ContractDeployer,
deployed_service_info: DeployedContracts,
token_address: HexAddress,
token_network_registry_contract: Contract,
) -> None:
"""Run deploy_service_contracts with a previous ServiceRegistry deployment data"""
with NamedTemporaryFile() as previous_deployment_file:
previous_deployment_file.write(bytearray(json.dumps(deployed_service_info), "ascii"))
previous_deployment_file.flush()
new_deployment = deployer.deploy_service_contracts(
token_address=token_address,
user_deposit_whole_balance_limit=DEPOSIT_LIMIT,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
service_registry_controller=DEPLOYER_ADDRESS,
initial_service_deposit_price=SERVICE_DEPOSIT // 2,
service_deposit_bump_numerator=6,
service_deposit_bump_denominator=5,
decay_constant=200 * SECONDS_PER_DAY,
min_price=1000,
registration_duration=180 * SECONDS_PER_DAY,
token_network_registry_address=token_network_registry_contract.address,
reuse_service_registry_from_deploy_file=Path(previous_deployment_file.name),
)
assert (
new_deployment["contracts"][CONTRACT_SERVICE_REGISTRY]
== deployed_service_info["contracts"][CONTRACT_SERVICE_REGISTRY]
)
assert (
new_deployment["contracts"][CONTRACT_ONE_TO_N]
!= deployed_service_info["contracts"][CONTRACT_ONE_TO_N]
)
def test_deploy_script_token(web3: Web3) -> None:
"""Run test token deployment function used in the deployment script
This checks if deploy_token_contract() works correctly in the happy case,
to make sure no code dependencies have been changed, affecting the deployment script.
This does not check however that the cli command works correctly.
"""
# normal deployment
token_type = "CustomToken"
gas_limit = 5860000
deployer = ContractDeployer(
web3=web3,
private_key=FAUCET_PRIVATE_KEY,
gas_limit=gas_limit,
gas_price=1,
wait=10,
)
deployed_token = deployer.deploy_token_contract(
token_supply=10000000,
token_decimals=18,
token_name="TestToken",
token_symbol="TTT",
token_type=token_type,
)
assert deployed_token[token_type] is not None
assert isinstance(deployed_token[token_type], str)
# check that it fails if sender has no eth
deployer = ContractDeployer(
web3=web3,
private_key=get_random_privkey(),
gas_limit=gas_limit,
gas_price=1,
wait=10,
)
with pytest.raises(ValidationError):
deployer.deploy_token_contract(
token_supply=10000000,
token_decimals=18,
token_name="TestToken",
token_symbol="TTT",
token_type="CustomToken",
)
@pytest.mark.slow
def test_deploy_script_register(
web3: Web3,
channel_participant_deposit_limit: int,
token_network_deposit_limit: int,
deployed_raiden_info: DeployedContracts,
token_address: ChecksumAddress,
) -> None:
"""Run token register function used in the deployment script
This checks if register_token_network() works correctly in the happy case,
to make sure no code dependencies have been changed, affecting the deployment script.
This does not check however that the cli command works correctly.
"""
# normal deployment
gas_limit = 5860000
deployer = ContractDeployer(
web3=web3,
private_key=FAUCET_PRIVATE_KEY,
gas_limit=gas_limit,
gas_price=1,
wait=10,
)
token_registry_abi = deployer.contract_manager.get_contract_abi(
CONTRACT_TOKEN_NETWORK_REGISTRY
)
token_registry_address = deployed_raiden_info["contracts"][CONTRACT_TOKEN_NETWORK_REGISTRY][
"address"
]
with patch(
"raiden_contracts.deploy.contract_deployer.get_contracts_deployment_info"
) as get_deploy_info_mock:
get_deploy_info_mock.return_value = deployed_raiden_info
token_network_address = deployer.register_token_network(
token_registry_abi=token_registry_abi,
token_registry_address=token_registry_address,
token_address=token_address,
channel_participant_deposit_limit=channel_participant_deposit_limit,
token_network_deposit_limit=token_network_deposit_limit,
)["token_network_address"]
assert token_network_address is not None
assert isinstance(token_network_address, str)
@pytest.mark.slow
def test_deploy_script_service(
web3: Web3,
deployed_service_info: DeployedContracts,
token_address: HexAddress,
token_network_registry_contract: Contract,
) -> None:
"""Run deploy_service_contracts() used in the deployment script
This checks if deploy_service_contracts() works correctly in the happy case.
"""
gas_limit = 5860000
deployer = ContractDeployer(
web3=web3,
private_key=FAUCET_PRIVATE_KEY,
gas_limit=gas_limit,
gas_price=1,
wait=10,
)
token_supply = 10000000
assert isinstance(token_address, str)
deposit_limit = token_supply // 2
deployed_service_contracts = deployed_service_info
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_service_contracts,
token_network_registry_address=token_network_registry_contract.address,
)
with pytest.raises(RuntimeError):
assert EMPTY_ADDRESS != token_address
deployer.verify_service_contracts_deployment_data(
token_address=EMPTY_ADDRESS,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_service_contracts,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts_version"] = "0.0.0"
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["chain_id"] = deployed_service_contracts["chain_id"] + 1
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][CONTRACT_SERVICE_REGISTRY]["constructor_arguments"] = [
EMPTY_ADDRESS
]
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][CONTRACT_SERVICE_REGISTRY]["constructor_arguments"][
0
] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
original = deployed_info_fail["contracts"][CONTRACT_USER_DEPOSIT]["constructor_arguments"]
deployed_info_fail["contracts"][CONTRACT_USER_DEPOSIT]["constructor_arguments"] += original
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][CONTRACT_USER_DEPOSIT]["constructor_arguments"][
0
] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][CONTRACT_USER_DEPOSIT]["constructor_arguments"][1] = (
deposit_limit + 1
)
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
original = deployed_info_fail["contracts"][CONTRACT_MONITORING_SERVICE][
"constructor_arguments"
]
deployed_info_fail["contracts"][CONTRACT_MONITORING_SERVICE][
"constructor_arguments"
] += original
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][CONTRACT_MONITORING_SERVICE]["constructor_arguments"][
0
] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][CONTRACT_MONITORING_SERVICE]["constructor_arguments"][
2
] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][CONTRACT_MONITORING_SERVICE]["constructor_arguments"][
3
] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][CONTRACT_ONE_TO_N]["constructor_arguments"][0] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][CONTRACT_ONE_TO_N]["constructor_arguments"][1] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][CONTRACT_ONE_TO_N]["constructor_arguments"][2] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
deployed_info_fail = deepcopy(deployed_service_contracts)
original = deployed_info_fail["contracts"][CONTRACT_ONE_TO_N]["constructor_arguments"]
deployed_info_fail["contracts"][CONTRACT_ONE_TO_N]["constructor_arguments"] += original
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
def test_missing_deployment(contract_name: str) -> None:
deployed_info_fail = deepcopy(deployed_service_contracts)
deployed_info_fail["contracts"][contract_name]["address"] = EMPTY_ADDRESS
with pytest.raises(RuntimeError):
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
user_deposit_whole_balance_limit=deposit_limit,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
deployed_contracts_info=deployed_info_fail,
token_network_registry_address=token_network_registry_contract.address,
)
for contract_name in [
CONTRACT_SERVICE_REGISTRY,
CONTRACT_MONITORING_SERVICE,
CONTRACT_ONE_TO_N,
CONTRACT_USER_DEPOSIT,
]:
test_missing_deployment(contract_name)
def test_validate_address_on_none() -> None:
"""validate_address(x, y, None) should return None"""
mock_command = MagicMock()
mock_parameter = MagicMock()
assert validate_address(mock_command, mock_parameter, None) is None
def test_validate_address_empty_string() -> None:
"""validate_address(x, y, '') should return None"""
assert validate_address(MagicMock(), MagicMock(), "") is None
def test_validate_address_not_an_address() -> None:
"""validate_address(x, y, 'not an address') should raise click.BadParameter"""
with pytest.raises(BadParameter):
validate_address(MagicMock(), MagicMock(), "not an address")
def test_validate_address_happy_path() -> None:
"""validate_address(x, y, address) should return the same address checksumed"""
address = DEPLOYER_ADDRESS
assert validate_address(MagicMock(), MagicMock(), address) == to_checksum_address(address)
@pytest.fixture
def fs_reload_deployer() -> Generator[FakeFilesystem, None, None]:
patcher = Patcher(
modules_to_reload=[
raiden_contracts.contract_manager,
raiden_contracts.deploy.__main__,
]
)
patcher.setUp()
yield patcher.fs
patcher.tearDown()
@pytest.mark.slow
def test_store_and_verify_raiden(
fs_reload_deployer: FakeFilesystem,
deployed_raiden_info: DeployedContracts,
deployer: ContractDeployer,
) -> None:
"""Store some raiden contract deployment information and verify them"""
fs_reload_deployer.add_real_directory(
contracts_precompiled_path(version=None).parent, read_only=False
)
deployed_contracts_info = deployed_raiden_info
deployer.store_and_verify_deployment_info_raiden(
deployed_contracts_info=deployed_contracts_info
)
deployer.store_and_verify_deployment_info_raiden(
deployed_contracts_info=deployed_contracts_info
)
@pytest.mark.slow
def test_store_and_verify_services(
fs_reload_deployer: FakeFilesystem,
deployer: ContractDeployer,
deployed_service_info: DeployedContracts,
token_address: HexAddress,
token_network_registry_contract: Contract,
) -> None:
"""Store some service contract deployment information and verify them"""
fs_reload_deployer.add_real_directory(
contracts_precompiled_path(version=None).parent, read_only=False
)
deployed_contracts_info = deployed_service_info
deployer.verify_service_contracts_deployment_data(
token_address=token_address,
deployed_contracts_info=deployed_contracts_info,
user_deposit_whole_balance_limit=DEPOSIT_LIMIT,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
token_network_registry_address=token_network_registry_contract.address,
)
deployer.store_and_verify_deployment_info_services(
token_address=token_address,
deployed_contracts_info=deployed_contracts_info,
user_deposit_whole_balance_limit=DEPOSIT_LIMIT,
user_deposit_withdraw_timeout=WITHDRAW_TIMEOUT,
token_network_registry_address=token_network_registry_contract.address,
)
def test_error_removed_option_raises() -> None:
with pytest.raises(NoSuchOption):
mock = MagicMock()
error_removed_option("msg")(None, mock, "0xaabbcc")
def test_contracts_version_has_initial_service_deposit() -> None:
assert contracts_version_has_initial_service_deposit(ALDERAAN_VERSION)
assert contracts_version_has_initial_service_deposit(None)
with pytest.raises(ValueError):
contracts_version_has_initial_service_deposit("not a semver string")
def deploy_token_arguments(privkey: str) -> List[str]:
return [
"--rpc-provider",
"rpc_provider",
"--private-key",
privkey,
"--gas-price",
"12",
"--token-supply",
"20000000",
"--token-name",
"ServiceToken",
"--token-decimals",
"18",
"--token-symbol",
"SVT",
]
def test_deploy_token_invalid_privkey() -> None:
"""Call deploy token command with invalid private key"""
with patch.object(
ContractDeployer, "deploy_token_contract", spec=ContractDeployer
) as mock_deployer:
runner = CliRunner()
result = runner.invoke(token, deploy_token_arguments(privkey="wrong_priv_key"))
assert result.exit_code != 0
assert type(result.exception) == RuntimeError
assert result.exception
assert result.exception.args == ("Could not access the private key.",)
mock_deployer.assert_not_called()
def test_deploy_token_no_balance(privkey_file: IO) -> None:
"""Call deploy token command with a private key with no balance"""
with patch.object(
ContractDeployer, "deploy_token_contract", spec=ContractDeployer
) as mock_deployer:
with patch.object(Eth, "get_balance", return_value=0):
runner = CliRunner()
result = runner.invoke(token, deploy_token_arguments(privkey=privkey_file.name))
assert result.exit_code != 0
assert type(result.exception) == RuntimeError
assert result.exception
assert result.exception.args == ("Account with insufficient funds.",)
mock_deployer.assert_not_called()
@patch.object(ContractDeployer, "_adjust_chain_settings")
def test_deploy_token_with_balance(
mock_adjust_chain_settings: MagicMock, privkey_file: IO
) -> None:
"""Call deploy token command with a private key with some balance"""
with patch.object(
ContractDeployer,
"deploy_token_contract",
spec=ContractDeployer,
return_value={},
) as mock_deployer:
with patch.object(Eth, "get_balance", return_value=100):
runner = CliRunner()
result = runner.invoke(token, deploy_token_arguments(privkey=privkey_file.name))
assert result.exit_code == 0
mock_adjust_chain_settings.assert_called_once()
mock_deployer.assert_called_once()
def deploy_raiden_arguments(
privkey: str,
save_info: Optional[bool],
contracts_version: Optional[str],
reuse_secret_registry: bool,
) -> List:
arguments: List = ["--private-key", privkey, "--rpc-provider", "rpc_provider"]
if save_info is True:
arguments.append("--save-info")
elif save_info is False:
arguments.append("--no-save-info")
if contracts_version_with_max_token_networks(contracts_version):
arguments.extend(["--max-token-networks", 1])
if contracts_version:
arguments.extend(["--contracts-version", contracts_version])
if reuse_secret_registry:
arguments.extend(["--secret-registry-from-deployment-file", "."])
return arguments
@patch.object(ContractVerifier, "store_and_verify_deployment_info_raiden")
@patch.object(ContractDeployer, "deploy_raiden_contracts")
@patch.object(ContractDeployer, "_adjust_chain_settings")
@pytest.mark.parametrize("contracts_version", [None, ALDERAAN_VERSION])
@pytest.mark.parametrize("reuse_secret_registry", [False, True])
def test_deploy_raiden(
mock_adjust_chain_settings: MagicMock,
mock_deploy: MagicMock,
mock_verify: MagicMock,
contracts_version: Optional[str],
reuse_secret_registry: bool,
privkey_file: IO,
) -> None:
"""Calling deploy raiden command"""
with patch.object(Eth, "get_balance", return_value=1):
runner = CliRunner()
result = runner.invoke(
raiden,
deploy_raiden_arguments(
privkey=privkey_file.name,
save_info=None,
contracts_version=contracts_version,
reuse_secret_registry=reuse_secret_registry,
),
)
assert result.exception is None
assert result.exit_code == 0
mock_adjust_chain_settings.assert_called_once()
mock_deploy.assert_called_once()
mock_verify.assert_called_once()
@patch.object(ContractDeployer, "register_token_network")
@patch.object(ContractDeployer, "_adjust_chain_settings")
def test_register_script(
mock_adjust_chain_settings: MagicMock,
mock_register: MagicMock,
deployed_raiden_info: DeployedContracts,
privkey_file: IO,
) -> None:
"""Calling deploy raiden command"""
with patch(
"raiden_contracts.deploy.contract_deployer.get_contracts_deployment_info"
) as get_deploy_info_mock, patch(
"raiden_contracts.deploy.__main__._add_token_network_deploy_info"
) as add_tn_info:
get_deploy_info_mock.return_value = deployed_raiden_info
with patch.object(Eth, "get_balance", return_value=1), patch.object(Eth, "chainId", 61):
runner = CliRunner()
result = runner.invoke(
register,
[
"--rpc-provider",
"rpc_provider",
"--private-key",
privkey_file.name,
"--gas-price",
"12",
"--token-network-registry-address",
"0x90a16f6aEA062c429c85dc4124ee4b24A00bCc9a",
"--token-address",
"0x90a16f6aEA062c429c85dc4124ee4b24A00bCc9a",
"--channel-participant-deposit-limit",
"100",
"--token-network-deposit-limit",
"200",
],
catch_exceptions=False,
)
assert result.exit_code == 0
mock_adjust_chain_settings.assert_called_once()
mock_register.assert_called_once()
add_tn_info.assert_called_once()
@patch.object(ContractDeployer, "register_token_network")
@patch.object(ContractDeployer, "_adjust_chain_settings")
def test_register_script_without_token_network(
mock_adjust_chain_settings: MagicMock,
mock_register: MagicMock,
privkey_file: IO,
) -> None:
"""Calling deploy raiden command"""
with patch.object(Eth, "get_balance", return_value=1):
runner = CliRunner()
result = runner.invoke(
register,
[
"--rpc-provider",
"rpv_provider",
"--private-key",
privkey_file.name,
"--gas-price",
"12",
"--token-address",
"0x90a16f6aEA062c429c85dc4124ee4b24A00bCc9a",
"--channel-participant-deposit-limit",
"100",
"--token-network-deposit-limit",
"200",
],
)
assert result.exit_code != 0
assert type(result.exception) == RuntimeError
assert result.exception
assert result.exception.args == (
"No TokenNetworkRegistry was specified. "
"Add --token-network-registry-address <address>.",
)
mock_adjust_chain_settings.assert_called_once()
mock_register.assert_not_called()
@patch.object(ContractDeployer, "verify_deployment_data")
@patch.object(ContractDeployer, "deploy_raiden_contracts")
@patch.object(ContractDeployer, "_adjust_chain_settings")