-
Notifications
You must be signed in to change notification settings - Fork 25
/
test.py
executable file
·529 lines (457 loc) · 25.9 KB
/
test.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
#!/usr/bin/env python3
import argparse
import json
import os
import random
import re
import subprocess
import shutil
import sys
import time
import pyhmy
import requests
ACC_NAMES_ADDED = []
ACC_NAME_PREFIX = "_Test_key_"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description='Wrapper python script to test API using newman.')
parser.add_argument("--test_dir", dest="test_dir", default="./tests/default",
help="Path to test directory. Default is './tests/default'", type=str)
parser.add_argument("--iterations", dest="iterations", default=5,
help="Number of attempts for a successful test. Default is 5.", type=int)
parser.add_argument("--start_epoch", dest="start_epoch", default=1,
help="The minimum epoch before starting tests. Default is 1.", type=int)
parser.add_argument("--rpc_endpoint_src", dest="hmy_endpoint_src", default="https://api.s0.b.hmny.io/",
help="Source endpoint for Cx. Default is https://api.s0.b.hmny.io/", type=str)
parser.add_argument("--rpc_endpoint_dst", dest="hmy_endpoint_dst", default="https://api.s1.b.hmny.io/",
help="Destination endpoint for Cx. Default is https://api.s1.b.hmny.io/", type=str)
parser.add_argument("--src_shard", dest="src_shard", default=None, type=str,
help=f"The source shard of the Cx. Default assumes associated shard from src endpoint.")
parser.add_argument("--dst_shard", dest="dst_shard", default=None, type=str,
help=f"The destination shard of the Cx. Default assumes associated shard from dst endpoint.")
parser.add_argument("--exp_endpoint", dest="hmy_exp_endpoint", default="http://e0.b.hmny.io:5000/",
help="Default is http://e0.b.hmny.io:5000/", type=str)
parser.add_argument("--delay", dest="txn_delay", default=45,
help="The time to wait before checking if a Cx/Tx is on the blockchain. "
"Default is 45 seconds. (Input is in seconds)", type=int)
parser.add_argument("--chain_id", dest="chain_id", default="testnet",
help="Chain ID for the CLI. Default is 'testnet'", type=str)
parser.add_argument("--cli_path", dest="hmy_binary_path", default=None,
help=f"ABSOLUTE PATH of CLI binary. "
f"Default uses the CLI included in pyhmy module", type=str)
parser.add_argument("--cli_passphrase", dest="passphrase", default='',
help=f"Passphrase used to unlock the keystore. "
f"Default is ''", type=str)
parser.add_argument("--keystore", dest="keys_dir", default="TestnetValidatorKeys",
help=f"Directory of keystore to import. Must follow the format of CLI's keystore. "
f"Default is ./TestnetValidatorKeys", type=str)
parser.add_argument("--ignore_regression_test", dest="ignore_regression_test", action='store_true', default=False,
help="Disable the regression tests.")
parser.add_argument("--ignore_staking_test", dest="ignore_staking_test", action='store_true', default=False,
help="Disable the staking tests.")
return parser.parse_args()
def get_balance(name, node) -> dict:
address = CLI.get_address(name)
if not address:
return {}
response = CLI.single_call(f"hmy balances {address} --node={node}").replace("\r", "").replace("\n", "")
return json.loads(response)
def load_keys() -> None:
print("Loading keys...")
random_num = random.randint(-1e9, 1e9)
key_paths = os.listdir(args.keys_dir)
for i, key in enumerate(key_paths):
if not os.path.isdir(f"{args.keys_dir}/{key}"):
continue
key_content = os.listdir(f"{args.keys_dir}/{key}")
account_name = f"{ACC_NAME_PREFIX}{random_num}_{i}"
CLI.remove_account(account_name)
for file_name in key_content:
if not file_name.endswith(".key"): # Strong assumption about key file, some valid files may be ignored.
continue
from_key_file_path = f"{os.path.abspath(args.keys_dir)}/{key}/{file_name}"
to_key_file_path = f"{CLI.keystore_path}/{account_name}"
if not os.path.isdir(to_key_file_path):
os.mkdir(to_key_file_path)
shutil.copy(from_key_file_path, to_key_file_path)
try:
address = CLI.get_address(account_name)
names_with_address = CLI.get_accounts(address)
if len(names_with_address) > 1: # Remove duplicate accounts as passphrase may fail.
for name in names_with_address:
if not name.startswith(ACC_NAME_PREFIX):
print(f"[!] Removing {name} ({address}) from keystore as it conflicts with imported key")
CLI.remove_account(name)
except AttributeError:
print("[!] pyhmy is out of date. Upgrade with:\n\t python3 -m pip install pyhmy --upgrade\n")
ACC_NAMES_ADDED.append(account_name)
assert len(ACC_NAMES_ADDED) > 1, "Must load at least 2 keys and must match CLI's keystore format"
def is_after_epoch(n):
url = args.hmy_endpoint_src
payload = """{
"jsonrpc": "2.0",
"method": "hmy_latestHeader",
"params": [ ],
"id": 1
}"""
headers = {
'Content-Type': 'application/json'
}
try:
response = requests.request('POST', url, headers=headers, data=payload, allow_redirects=False, timeout=3)
body = json.loads(response.content)
return int(body["result"]["epoch"]) > n
except (requests.ConnectionError, KeyError):
return False
def create_validator() -> list:
print("== Creating validators ==")
bls_keys_for_new_val = [
"1480fca328daaddd3487195c5500969ecccbb806b6bf464734e0e3ad18c64badfae8578d76e2e9281b6a3645d056960a",
"9d657b1854d6477dba8bca9606f6ac884df308558f2b3b545fc76a9ef02abc87d3518cf1134d21acf03036cea2820f02",
"e2c13c84c8f2396cf7180d5026e048e59ec770a2851003f1f2ab764a79c07463681ed7ddfe62bc4d440526a270891c86",
]
# Sourced from harmony/test/config/local-resharding.txt (Keys must be in provided keystore).
foundational_node_data = [
("one1ghkz3frhske7emk79p7v2afmj4a5t0kmjyt4s5",
"eca09c1808b729ca56f1b5a6a287c6e1c3ae09e29ccf7efa35453471fcab07d9f73cee249e2b91f5ee44eb9618be3904"),
("one1d7jfnr6yraxnrycgaemyktkmhmajhp8kl0yahv",
"f47238daef97d60deedbde5302d05dea5de67608f11f406576e363661f7dcbc4a1385948549b31a6c70f6fde8a391486"),
("one1r4zyyjqrulf935a479sgqlpa78kz7zlcg2jfen",
"fc4b9c535ee91f015efff3f32fbb9d32cdd9bfc8a837bb3eee89b8fff653c7af2050a4e147ebe5c7233dc2d5df06ee0a"),
("one1p7ht2d4kl8ve7a8jxw746yfnx4wnfxtp8jqxwe",
"ca86e551ee42adaaa6477322d7db869d3e203c00d7b86c82ebee629ad79cb6d57b8f3db28336778ec2180e56a8e07296"),
("one1z05g55zamqzfw9qs432n33gycdmyvs38xjemyl",
"95117937cd8c09acd2dfae847d74041a67834ea88662a7cbed1e170350bc329e53db151e5a0ef3e712e35287ae954818"),
("one1ljznytjyn269azvszjlcqvpcj6hjm822yrcp2e",
"68ae289d73332872ec8d04ac256ca0f5453c88ad392730c5741b6055bc3ec3d086ab03637713a29f459177aaa8340615"),
("one1uyshu2jgv8w465yc8kkny36thlt2wvel89tcmg",
"a547a9bf6fdde4f4934cde21473748861a3cc0fe8bbb5e57225a29f483b05b72531f002f8187675743d819c955a86100"),
("one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7",
"678ec9670899bf6af85b877058bea4fc1301a5a3a376987e826e3ca150b80e3eaadffedad0fedfa111576fa76ded980c"),
("one1658znfwf40epvy7e46cqrmzyy54h4n0qa73nep",
"576d3c48294e00d6be4a22b07b66a870ddee03052fe48a5abbd180222e5d5a1f8946a78d55b025de21635fd743bbad90"),
("one1d2rngmem4x2c6zxsjjz29dlah0jzkr0k2n88wc",
"16513c487a6bb76f37219f3c2927a4f281f9dd3fd6ed2e3a64e500de6545cf391dd973cc228d24f9bd01efe94912e714")
]
added_validators = []
while not is_after_epoch(0):
print("Waiting for epoch 1...")
time.sleep(5)
for key in bls_keys_for_new_val:
account_name = f"{ACC_NAME_PREFIX}{random.randint(-1e6, 1e6)}"
proc = CLI.expect_call(f"hmy keys add {account_name} --passphrase")
proc.expect("Enter passphrase\r\n")
proc.sendline(f"{args.passphrase}")
proc.expect("Repeat the passphrase:\r\n")
proc.sendline(f"{args.passphrase}")
proc.wait()
address = CLI.get_address(account_name)
added_validators.append(address)
staking_command = f"hmy staking create-validator --amount 1 " \
f"--validator-addr {address} " \
f"--bls-pubkeys {key} --identity foo --details bar --name baz " \
f"--max-change-rate 0.1 --max-rate 0.2 --max-total-delegation 10 " \
f"--min-self-delegation 1 --rate 0.1 --security-contact Leo " \
f"--website harmony.one --passphrase={args.passphrase}"
ACC_NAMES_ADDED.append(account_name)
print(f"Staking command response for {address}: ", CLI.single_call(staking_command))
for address, key in foundational_node_data:
added_validators.append(address)
staking_command = f"hmy staking create-validator --amount 1 " \
f"--validator-addr {address} " \
f"--bls-pubkeys {key} --identity foo --details bar --name baz " \
f"--max-change-rate 0.1 --max-rate 0.2 --max-total-delegation 10 " \
f"--min-self-delegation 1 --rate 0.1 --security-contact Leo " \
f"--website harmony.one --passphrase={args.passphrase}"
print(f"Staking command response for {address}: ", CLI.single_call(staking_command))
print("Validators added: ", added_validators)
return added_validators
def bls_generator(count):
for _ in range(count):
proc = CLI.expect_call("hmy keys generate-bls-key --bls-file-path /tmp/file.key")
proc.expect("Enter passphrase\r\n")
proc.sendline("")
proc.expect("Repeat the passphrase:\r\n")
proc.sendline("")
response = proc.read().decode().strip().replace('\r', '').replace('\n', '')
yield json.loads(response)
def create_validator_many_keys():
print("== Running CLI staking tests ==")
bls_keys = [d for d in bls_generator(10)]
for acc in ACC_NAMES_ADDED:
balance = get_balance(acc, args.hmy_endpoint_src)
if balance[0]["amount"] < 1:
continue
address = CLI.get_address(acc)
key_counts = [1, 10]
for i in key_counts:
bls_key_string = ','.join(el["public-key"] for el in bls_keys[:i])
staking_command = f"hmy staking create-validator --amount 1 --validator-addr {address} " \
f"--bls-pubkeys {bls_key_string} --identity foo --details bar --name baz " \
f"--max-change-rate 0.1 --max-rate 0.2 --max-total-delegation 10 " \
f"--min-self-delegation 1 --rate 0.1 --security-contact Leo " \
f"--website harmony.one --node={args.hmy_endpoint_src} " \
f"--chain-id={args.chain_id} --passphrase={args.passphrase}"
response = CLI.single_call(staking_command)
print(f"\nPassed creating a validator with {i} bls key(s)")
print(f"\tCLI command: {staking_command}")
print(f"\tStaking transaction response: {response}")
if i == key_counts[-1]:
return
print(f"Sleeping {args.txn_delay} seconds for finality...\n")
time.sleep(args.txn_delay)
print("Failed CLI staking test.")
sys.exit(-1)
def edit_validator(address):
print("== Editing Validator ==")
old_bls_key = "1480fca328daaddd3487195c5500969ecccbb806b6bf464734e0e3ad18c64badfae8578d76e2e9281b6a3645d056960a"
new_bls_key = "249976f984f30306f800ef42fb45272b391cfdd17f966e093a9f711e30f66f77ecda6c367bf79afc9fa31a1789e9ee8e"
staking_command = f"hmy staking edit-validator --validator-addr {address} " \
f"--identity foo --details bar --name baz " \
f"--max-total-delegation 10 " \
f"--min-self-delegation 1 --rate 0.1 --security-contact Leo " \
f"--website harmony.one --node={args.hmy_endpoint_src} " \
f"--remove-bls-key {old_bls_key} --add-bls-key {new_bls_key} " \
f"--chain-id={args.chain_id} --passphrase={args.passphrase}"
response = CLI.single_call(staking_command)
print(f"\tStaking transaction response: {response}")
print(f"Sleeping {args.txn_delay} seconds for finality...\n")
time.sleep(args.txn_delay)
def create_delegator(address) -> str:
print("== Creating Delegator ==")
account_name = f"{ACC_NAME_PREFIX}delegator"
proc = CLI.expect_call(f"hmy keys add {account_name} --passphrase")
proc.expect("Enter passphrase\r\n")
proc.sendline(f"{args.passphrase}")
proc.expect("Repeat the passphrase:\r\n")
proc.sendline(f"{args.passphrase}")
proc.wait()
ACC_NAMES_ADDED.append(account_name)
delegator_address = CLI.get_address(account_name)
staking_command = f"hmy staking delegate --validator-addr {address} " \
f"--delegator-addr {delegator_address} --amount 1 " \
f"--node={args.hmy_endpoint_src} " \
f"--chain-id={args.chain_id} --passphrase={args.passphrase}"
response = CLI.single_call(staking_command)
print(f"\tDelegator transaction response: {response}")
print(f"Sleeping {args.txn_delay} seconds for finality...\n")
time.sleep(args.txn_delay)
return delegator_address
def undelegate(v_address, d_address):
print("== Undelegate ==")
staking_command = f"hmy staking undelegate --validator-addr {v_address} " \
f"--delegator-addr {d_address} --amount 1 " \
f"--node={args.hmy_endpoint_src} " \
f"--chain-id={args.chain_id} --passphrase={args.passphrase}"
response = CLI.single_call(staking_command)
print(f"\tUndelegate transaction response: {response}")
print(f"Sleeping {args.txn_delay} seconds for finality...\n")
time.sleep(args.txn_delay)
def collect_rewards(address):
print("== Collecting Rewards ==")
staking_command = f"hmy staking collect-rewards --delegator-addr {address} " \
f"--node={args.hmy_endpoint_src} " \
f"--chain-id={args.chain_id} --passphrase={args.passphrase}"
response = CLI.single_call(staking_command)
print(f"\tCollect rewards transaction response: {response}")
print(f"Sleeping {args.txn_delay} seconds for finality...\n")
time.sleep(args.txn_delay)
def get_validators():
print("== Listing All Active Validators ==")
staking_command = f"hmy blockchain validator all-active " \
f"--node={args.hmy_endpoint_src} "
response = CLI.single_call(staking_command)
print(f"\tValidator transaction response: {response}")
print("== Listing History of All Validators ==")
staking_command = f"hmy blockchain validator all " \
f"--node={args.hmy_endpoint_src} "
response = CLI.single_call(staking_command)
print(f"\tValidator transaction response: {response}")
def get_validator_info(v_addr):
print("== Getting Validator Info ==")
staking_command = f"hmy blockchain validator information {v_addr} " \
f"--node={args.hmy_endpoint_src}"
response = CLI.single_call(staking_command)
print(f"\tValidator info transaction response: {response}")
def get_delegator_info(v_addr, d_addr):
print("== Getting Delegator Info by Delegator ==")
staking_command = f"hmy blockchain delegation by-delegator {d_addr} " \
f"--node={args.hmy_endpoint_src}"
response = CLI.single_call(staking_command)
print(f"\tDelegator info transaction response: {response} ")
print("== Getting Delegator Info by Validator ==")
staking_command = f"hmy blockchain delegation by-validator {v_addr} " \
f"--node={args.hmy_endpoint_src}"
response = CLI.single_call(staking_command)
print(f"\tDelegator info transaction response: {response}")
def get_raw_txn(passphrase, chain_id, node, src_shard, dst_shard) -> str:
"""
Must be cross shard transaction for tests.
If importing keys using 'import-ks', no passphrase is needed.
"""
print("== Getting raw transaction ==")
assert len(ACC_NAMES_ADDED) > 1, "Must load at least 2 keys and must match CLI's keystore format"
for acc_name in ACC_NAMES_ADDED:
balances = get_balance(acc_name, node)
from_addr = CLI.get_address(acc_name)
to_addr_candidates = ACC_NAMES_ADDED.copy()
to_addr_candidates.remove(acc_name)
to_addr = CLI.get_address(random.choice(to_addr_candidates))
if balances[src_shard]["amount"] >= 5: # Ensure enough funds (even with high gas fees).
print(f"Raw transaction details:\n"
f"\tNode: {node}\n"
f"\tFrom: {from_addr}\n"
f"\tTo: {to_addr}\n"
f"\tFrom-shard: {src_shard}\n"
f"\tTo-shard: {dst_shard}")
response = CLI.single_call(f"hmy --node={node} transfer --from={from_addr} --to={to_addr} "
f"--from-shard={src_shard} --to-shard={dst_shard} --amount={1e-9} "
f"--chain-id={chain_id} --dry-run --passphrase={passphrase}")
print(f"\tTransaction for {chain_id}")
response_lines = response.split("\n")
assert len(response_lines) == 17, 'CLI output for transaction dry-run is not recognized, check CLI version.'
transaction = '\n\t\t'.join(response_lines[1:15])
print(f"\tTransaction:\n\t\t{transaction}")
return response_lines[-2].replace("RawTxn: ", "")
raise RuntimeError(f"None of the loaded accounts have funds on shard {src_shard}")
def get_shard_from_endpoint(endpoint):
"""
Currently assumes <= 10 shards
"""
re_match = re.search('\.s.\.', endpoint)
if re_match:
return int(re_match.group(0)[-2])
re_match = re.search(':950./', endpoint)
if re_match:
return int(re_match.group(0)[-2])
raise ValueError(f"Unknown endpoint format: {endpoint}")
def setup_newman_no_explorer(test_json, global_json, env_json):
source_shard = args.src_shard if args.src_shard else get_shard_from_endpoint(args.hmy_endpoint_src)
destination_shard = args.dst_shard if args.dst_shard else get_shard_from_endpoint(args.hmy_endpoint_dst)
raw_txn = get_raw_txn(passphrase=args.passphrase, chain_id=args.chain_id,
node=args.hmy_endpoint_src, src_shard=source_shard, dst_shard=destination_shard)
if str(source_shard) not in args.hmy_endpoint_src:
print(f"Source shard {source_shard} may not match source endpoint {args.hmy_endpoint_src}")
if str(destination_shard) not in args.hmy_endpoint_dst:
print(f"Destination shard {destination_shard} may not match destination endpoint {args.hmy_endpoint_dst}")
for i, var in enumerate(env_json["values"]):
if var["key"] == "rawTransaction":
env_json["values"][i]["value"] = raw_txn
if var["key"] == "txn_delay":
env_json["values"][i]["value"] = args.txn_delay
for i, var in enumerate(global_json["values"]):
if var["key"] == "hmy_endpoint_src":
global_json["values"][i]["value"] = args.hmy_endpoint_src
if var["key"] == "hmy_endpoint_dst":
global_json["values"][i]["value"] = args.hmy_endpoint_dst
def setup_newman_only_explorer(test_json, global_json, env_json):
if "localhost" in args.hmy_endpoint_src or "localhost" in args.hmy_exp_endpoint:
print("\n\t[WARNING] This test is for testnet or mainnet.\n")
source_shard = args.src_shard if args.src_shard else get_shard_from_endpoint(args.hmy_endpoint_src)
destination_shard = args.dst_shard if args.dst_shard else get_shard_from_endpoint(args.hmy_endpoint_dst)
raw_txn = get_raw_txn(passphrase=args.passphrase, chain_id=args.chain_id,
node=args.hmy_endpoint_src, src_shard=source_shard, dst_shard=destination_shard)
if str(source_shard) not in args.hmy_endpoint_src:
print(f"Source shard {source_shard} may not match source endpoint {args.hmy_endpoint_src}")
if str(destination_shard) not in args.hmy_endpoint_dst:
print(f"Destination shard {destination_shard} may not match destination endpoint {args.hmy_endpoint_dst}")
for i, var in enumerate(env_json["values"]):
if var["key"] == "rawTransaction":
env_json["values"][i]["value"] = raw_txn
if var["key"] == "tx_beta_endpoint":
env_json["values"][i]["value"] = args.hmy_exp_endpoint
if var["key"] == "txn_delay":
env_json["values"][i]["value"] = args.txn_delay
if var["key"] == "source_shard":
env_json["values"][i]["value"] = source_shard
for i, var in enumerate(global_json["values"]):
if var["key"] == "hmy_exp_endpoint":
global_json["values"][i]["value"] = args.hmy_exp_endpoint
if var["key"] == "hmy_endpoint_src":
global_json["values"][i]["value"] = args.hmy_endpoint_src
def setup_newman_default(test_json, global_json, env_json):
if "localhost" in args.hmy_endpoint_src or "localhost" in args.hmy_exp_endpoint:
print("\n\t[WARNING] This test is for testnet or mainnet.\n")
source_shard = args.src_shard if args.src_shard else get_shard_from_endpoint(args.hmy_endpoint_src)
destination_shard = args.dst_shard if args.dst_shard else get_shard_from_endpoint(args.hmy_endpoint_dst)
raw_txn = get_raw_txn(passphrase=args.passphrase, chain_id=args.chain_id,
node=args.hmy_endpoint_src, src_shard=source_shard, dst_shard=destination_shard)
if str(source_shard) not in args.hmy_endpoint_src:
print(f"Source shard {source_shard} may not match source endpoint {args.hmy_endpoint_src}")
if str(destination_shard) not in args.hmy_endpoint_dst:
print(f"Destination shard {destination_shard} may not match destination endpoint {args.hmy_endpoint_dst}")
for i, var in enumerate(env_json["values"]):
if var["key"] == "rawTransaction":
env_json["values"][i]["value"] = raw_txn
if var["key"] == "tx_beta_endpoint":
env_json["values"][i]["value"] = args.hmy_exp_endpoint
if var["key"] == "txn_delay":
env_json["values"][i]["value"] = args.txn_delay
if var["key"] == "source_shard":
env_json["values"][i]["value"] = source_shard
for i, var in enumerate(global_json["values"]):
if var["key"] == "hmy_endpoint_src":
global_json["values"][i]["value"] = args.hmy_endpoint_src
if var["key"] == "hmy_endpoint_dst":
global_json["values"][i]["value"] = args.hmy_endpoint_dst
if var["key"] == "hmy_exp_endpoint":
global_json["values"][i]["value"] = args.hmy_exp_endpoint
if __name__ == "__main__":
args = parse_args()
print("\n\t== Starting Tests ==\n")
if args.chain_id not in {"mainnet", "testnet", "pangaea"}:
args.chain_id = "testnet"
assert os.path.isdir(args.keys_dir), "Could not find keystore directory"
CLI = pyhmy.HmyCLI(environment=pyhmy.get_environment(), hmy_binary_path=args.hmy_binary_path)
exit_code = 0
print(f"CLI Version: {CLI.version}")
try:
load_keys()
print(f"Waiting for epoch {args.start_epoch} (or later)")
while not is_after_epoch(args.start_epoch-1):
time.sleep(5)
if not args.ignore_staking_test:
test_validators = create_validator()
create_validator_many_keys()
edit_validator(test_validators[0])
delegator = create_delegator(test_validators[0])
undelegate(test_validators[0], delegator)
collect_rewards(delegator)
get_validators()
get_validator_info(test_validators[0])
get_delegator_info(test_validators[0], delegator)
if not args.ignore_regression_test:
with open(f"{args.test_dir}/test.json", 'r') as f:
test_json = json.load(f)
with open(f"{args.test_dir}/global.json", 'r') as f:
global_json = json.load(f)
with open(f"{args.test_dir}/env.json", 'r') as f:
env_json = json.load(f)
if "Harmony API Tests - no-explorer" in test_json["info"]["name"]:
setup_newman_no_explorer(test_json, global_json, env_json)
elif "Harmony API Tests - only-explorer" in test_json["info"]["name"]:
setup_newman_only_explorer(test_json, global_json, env_json)
else:
setup_newman_default(test_json, global_json, env_json)
with open(f"{args.test_dir}/global.json", 'w') as f:
json.dump(global_json, f)
with open(f"{args.test_dir}/env.json", 'w') as f:
json.dump(env_json, f)
for i in range(args.iterations):
print(f"\n\tIteration {i+1} out of {args.iterations}\n")
proc = subprocess.Popen(["newman", "run", f"{args.test_dir}/test.json",
"-e", f"{args.test_dir}/env.json",
"-g", f"{args.test_dir}/global.json"])
proc.wait()
exit_code = proc.returncode
if proc.returncode == 0:
print(f"\n\tSucceeded in {i+1} attempt(s)\n")
break
except (RuntimeError, KeyboardInterrupt) as err:
print("Removing imported keys from CLI's keystore...")
for acc_name in ACC_NAMES_ADDED:
CLI.remove_account(acc_name)
raise err
print("Removing imported keys from CLI's keystore...")
for acc_name in ACC_NAMES_ADDED:
CLI.remove_account(acc_name)
sys.exit(exit_code)