-
Notifications
You must be signed in to change notification settings - Fork 27
/
helpers.py
475 lines (395 loc) · 16.3 KB
/
helpers.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
import argparse
import contextlib
import json
import logging
import os
import subprocess
import time
from pathlib import Path
from typing import Any
from typing import Dict
from typing import Generator
from typing import List
from typing import Optional
import pytest
from _pytest.fixtures import FixtureRequest
from cardano_node_tests.utils import clusterlib
from cardano_node_tests.utils.types import FileType
LOGGER = logging.getLogger(__name__)
def wait_for(func, delay=5, num_sec=180, message=None):
"""Wait for success of `func` for `num_sec`."""
end_time = time.time() + num_sec
while time.time() < end_time:
response = func()
if response:
return response
time.sleep(delay)
pytest.fail(f"Failed to {message or 'finish'} in time.")
@contextlib.contextmanager
def change_cwd(dir_path: FileType) -> Generator[FileType, None, None]:
"""Change and restore CWD - context manager."""
orig_cwd = os.getcwd()
os.chdir(dir_path)
LOGGER.debug(f"Changed CWD to '{dir_path}'.")
try:
yield dir_path
finally:
os.chdir(orig_cwd)
LOGGER.debug(f"Restored CWD to '{orig_cwd}'.")
def read_address_from_file(location: FileType) -> str:
"""Read address stored in file."""
with open(Path(location).expanduser()) as in_file:
return in_file.read().strip()
def write_json(location: FileType, content: dict) -> FileType:
"""Write dictionary content to JSON file."""
with open(Path(location).expanduser(), "w") as out_file:
out_file.write(json.dumps(content, indent=4))
return location
def run_shell_command(command: str, workdir: FileType = ""):
"""Run command in shell."""
cmd = f"bash -c '{command}'"
cmd = cmd if not workdir else f"cd {workdir}; {cmd}"
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = p.communicate()
if p.returncode != 0:
raise AssertionError(f"An error occurred while running `{cmd}`: {stderr.decode()}")
return stdout
def get_cardano_version():
out = run_shell_command("cardano-node --version").decode().strip()
env_info, git_info, *__ = out.splitlines()
node, platform, ghc, *__ = env_info.split(" - ")
version = {
"cardano-node": node.split(" ")[-1],
"platform": platform,
"ghc": ghc,
"git_rev": git_info.split(" ")[-1],
}
return version
def fund_from_genesis(
*dst_addrs: str,
cluster_obj: clusterlib.ClusterLib,
amount: int = 2_000_000,
tx_name: Optional[str] = None,
destination_dir: FileType = ".",
):
"""Send `amount` from genesis addr to all `dst_addrs`."""
fund_dst = [
clusterlib.TxOut(address=d, amount=amount)
for d in dst_addrs
if cluster_obj.get_address_balance(d) < amount
]
if not fund_dst:
return
tx_name = tx_name or clusterlib.get_timestamped_rand_str()
tx_name = f"{tx_name}_genesis_funding"
fund_tx_files = clusterlib.TxFiles(
signing_key_files=[*cluster_obj.delegate_skeys, cluster_obj.genesis_utxo_skey]
)
cluster_obj.send_funds(
src_address=cluster_obj.genesis_utxo_addr,
destinations=fund_dst,
tx_name=tx_name,
tx_files=fund_tx_files,
destination_dir=destination_dir,
)
cluster_obj.wait_for_new_block(new_blocks=2)
def return_funds_to_faucet(
*src_addrs: clusterlib.AddressRecord,
cluster_obj: clusterlib.ClusterLib,
faucet_addr: str,
amount: int = -1,
tx_name: Optional[str] = None,
destination_dir: FileType = ".",
):
"""Send `amount` from all `src_addrs` to `faucet_addr`.
The amount of "-1" means all available funds.
"""
tx_name = tx_name or clusterlib.get_timestamped_rand_str()
tx_name = f"{tx_name}_return_funds"
try:
logging.disable(logging.ERROR)
for src in src_addrs:
fund_dst = [clusterlib.TxOut(address=faucet_addr, amount=amount)]
fund_tx_files = clusterlib.TxFiles(signing_key_files=[src.skey_file])
# try to return funds; don't mind if there's not enough funds for fees etc.
try:
cluster_obj.send_funds(
src_address=src.address,
destinations=fund_dst,
tx_name=tx_name,
tx_files=fund_tx_files,
destination_dir=destination_dir,
)
except clusterlib.CLIError:
pass
finally:
logging.disable(logging.NOTSET)
cluster_obj.wait_for_new_block(new_blocks=2)
def fund_from_faucet(
*dst_addrs: clusterlib.AddressRecord,
cluster_obj: clusterlib.ClusterLib,
faucet_data: dict,
amount: int = 3_000_000,
tx_name: Optional[str] = None,
request: Optional[FixtureRequest] = None,
destination_dir: FileType = ".",
):
"""Send `amount` from faucet addr to all `dst_addrs`."""
fund_dst = [
clusterlib.TxOut(address=d.address, amount=amount)
for d in dst_addrs
if cluster_obj.get_address_balance(d.address) < amount
]
if not fund_dst:
return
if request:
request.addfinalizer(
lambda: return_funds_to_faucet(
*dst_addrs,
cluster_obj=cluster_obj,
faucet_addr=faucet_data["payment_addr"],
tx_name=tx_name,
destination_dir=destination_dir,
)
)
tx_name = tx_name or clusterlib.get_timestamped_rand_str()
tx_name = f"{tx_name}_funding"
fund_tx_files = clusterlib.TxFiles(
signing_key_files=[faucet_data["payment_key_pair"].skey_file]
)
cluster_obj.send_funds(
src_address=faucet_data["payment_addr"],
destinations=fund_dst,
tx_name=tx_name,
tx_files=fund_tx_files,
destination_dir=destination_dir,
)
cluster_obj.wait_for_new_block(new_blocks=2)
def create_payment_addr_records(
*names: str,
cluster_obj: clusterlib.ClusterLib,
stake_vkey_file: Optional[FileType] = None,
destination_dir: FileType = ".",
) -> List[clusterlib.AddressRecord]:
"""Create new payment address(es)."""
addrs = [
cluster_obj.gen_payment_addr_and_keys(
name=name, stake_vkey_file=stake_vkey_file, destination_dir=destination_dir,
)
for name in names
]
LOGGER.debug(f"Created {len(addrs)} payment address(es)")
return addrs
def create_stake_addr_records(
*names: str, cluster_obj: clusterlib.ClusterLib, destination_dir: FileType = ".",
) -> List[clusterlib.AddressRecord]:
"""Create new stake address(es)."""
addrs = [
cluster_obj.gen_stake_addr_and_keys(name=name, destination_dir=destination_dir,)
for name in names
]
LOGGER.debug(f"Created {len(addrs)} stake address(es)")
return addrs
def load_devops_pools_data():
"""Load data for pools existing in the devops environment."""
data_dir = get_cluster_env()["state_dir"] / "nodes"
pools = ("node-pool1", "node-pool2")
addrs_data = {}
for addr_name in pools:
addr_data_dir = data_dir / addr_name
addrs_data[addr_name] = {
"payment_key_pair": clusterlib.KeyPair(
vkey_file=addr_data_dir / "owner-utxo.vkey",
skey_file=addr_data_dir / "owner-utxo.skey",
),
"stake_key_pair": clusterlib.KeyPair(
vkey_file=addr_data_dir / "owner-stake.vkey",
skey_file=addr_data_dir / "owner-stake.skey",
),
"payment_addr": read_address_from_file(addr_data_dir / "owner.addr"),
"stake_addr": read_address_from_file(addr_data_dir / "owner-stake.addr"),
"stake_addr_registration_cert": read_address_from_file(
addr_data_dir / "stake.reg.cert"
),
"cold_key_pair": clusterlib.ColdKeyPair(
vkey_file=addr_data_dir / "cold.vkey",
skey_file=addr_data_dir / "cold.skey",
counter_file=addr_data_dir / "cold.counter",
),
}
return addrs_data
def get_cluster_env() -> dict:
"""Get cardano cluster environment."""
socket_path = Path(os.environ["CARDANO_NODE_SOCKET_PATH"]).expanduser().resolve()
state_dir = socket_path.parent
work_dir = state_dir.parent
repo_dir = Path(os.environ.get("CARDANO_NODE_REPO_PATH") or work_dir)
cluster_env = {
"socket_path": socket_path,
"state_dir": state_dir,
"repo_dir": repo_dir,
"work_dir": work_dir,
}
return cluster_env
def wait_for_stake_distribution(cluster_obj: clusterlib.ClusterLib):
"""Wait to 3rd epoch (if necessary) and return stake distribution info."""
last_block_epoch = cluster_obj.get_last_block_epoch()
if last_block_epoch < 3:
new_epochs = 3 - last_block_epoch
LOGGER.info(f"Waiting {new_epochs} epoch(s) to get stake distribution.")
cluster_obj.wait_for_new_epoch(new_epochs)
return cluster_obj.get_stake_distribution()
def setup_test_addrs(cluster_obj: clusterlib.ClusterLib, destination_dir: FileType = ".",) -> dict:
"""Create addresses and their keys for usage in tests."""
destination_dir = Path(destination_dir).expanduser()
destination_dir.mkdir(parents=True, exist_ok=True)
addrs = ("user1", "pool-owner1")
LOGGER.debug("Creating addresses and keys for tests.")
addrs_data: Dict[str, Dict[str, Any]] = {}
for addr_name in addrs:
payment_key_pair = cluster_obj.gen_payment_key_pair(
key_name=addr_name, destination_dir=destination_dir,
)
stake_key_pair = cluster_obj.gen_stake_key_pair(
key_name=addr_name, destination_dir=destination_dir,
)
payment_addr = cluster_obj.gen_payment_addr(
payment_vkey_file=payment_key_pair.vkey_file, stake_vkey_file=stake_key_pair.vkey_file,
)
stake_addr = cluster_obj.gen_stake_addr(stake_vkey_file=stake_key_pair.vkey_file)
stake_addr_registration_cert = cluster_obj.gen_stake_addr_registration_cert(
addr_name=addr_name,
stake_vkey_file=stake_key_pair.vkey_file,
destination_dir=destination_dir,
)
addrs_data[addr_name] = {
"payment_key_pair": payment_key_pair,
"stake_key_pair": stake_key_pair,
"payment_addr": payment_addr,
"stake_addr": stake_addr,
"stake_addr_registration_cert": stake_addr_registration_cert,
}
LOGGER.debug("Funding created addresses." "")
fund_from_genesis(
*[d["payment_addr"] for d in addrs_data.values()],
cluster_obj=cluster_obj,
amount=20_000_000_000,
destination_dir=destination_dir,
)
pools_data = load_devops_pools_data()
return {**addrs_data, **pools_data}
def check_dir_arg(dir_path: str) -> Path:
"""Check that the dir passed as argparse parameter is a valid existing dir."""
abs_path = Path(dir_path).expanduser().resolve()
if not (abs_path.exists() and abs_path.is_dir()):
raise argparse.ArgumentTypeError(f"check_dir_arg: directory '{dir_path}' doesn't exist")
return abs_path
def check_file_arg(file_path: str) -> Path:
"""Check that the file passed as argparse parameter is a valid existing file."""
abs_path = Path(file_path).expanduser().resolve()
if not abs_path.exists():
raise argparse.ArgumentTypeError(f"check_dir_arg: directory '{file_path}' doesn't exist")
return abs_path
def save_cli_coverage(cluster_obj: clusterlib.ClusterLib, request: FixtureRequest):
"""Save CLI coverage info."""
cli_coverage_dir = request.config.getoption("--cli-coverage-dir")
if not (cli_coverage_dir and cluster_obj.cli_coverage):
return None
json_file = f"cli_coverage_{clusterlib.get_timestamped_rand_str(0)}.json"
with open(cli_coverage_dir / json_file, "w") as out_json:
json.dump(cluster_obj.cli_coverage, out_json, indent=4)
LOGGER.info(f"Coverage files saved to '{cli_coverage_dir}'.")
return None
def start_cluster() -> clusterlib.ClusterLib:
"""Start cluster."""
LOGGER.info("Starting cluster.")
cluster_env = get_cluster_env()
run_shell_command("start-cluster", workdir=cluster_env["work_dir"])
LOGGER.info("Cluster started.")
return clusterlib.ClusterLib(cluster_env["state_dir"])
def stop_cluster():
"""Stop cluster."""
LOGGER.info("Stopping cluster.")
cluster_env = get_cluster_env()
try:
run_shell_command("stop-cluster", workdir=cluster_env["work_dir"])
except Exception as exc:
LOGGER.debug(f"Failed to stop cluster: {exc}")
def start_stop_cluster(request: FixtureRequest) -> clusterlib.ClusterLib:
"""Stop cluster if needed, start fresh cluster, stop cluster at scope end."""
stop_cluster()
request.addfinalizer(stop_cluster)
cluster_obj = start_cluster()
request.addfinalizer(lambda: save_cli_coverage(cluster_obj=cluster_obj, request=request))
return cluster_obj
def check_pool_data( # noqa: C901
pool_ledger_state: dict, pool_creation_data: clusterlib.PoolData
) -> str:
"""Check that actual pool state corresponds with pool creation data."""
errors_list = []
if pool_ledger_state["cost"] != pool_creation_data.pool_cost:
errors_list.append(
"'cost' value is different than expected; "
f"Expected: {pool_creation_data.pool_cost} vs Returned: {pool_ledger_state['cost']}"
)
if pool_ledger_state["margin"] != pool_creation_data.pool_margin:
errors_list.append(
"'margin' value is different than expected; "
f"Expected: {pool_creation_data.pool_margin} vs Returned: {pool_ledger_state['margin']}"
)
if pool_ledger_state["pledge"] != pool_creation_data.pool_pledge:
errors_list.append(
"'pledge' value is different than expected; "
f"Expected: {pool_creation_data.pool_pledge} vs Returned: {pool_ledger_state['pledge']}"
)
if pool_ledger_state["relays"] != (pool_creation_data.pool_relay_dns or []):
errors_list.append(
"'relays' value is different than expected; "
f"Expected: {pool_creation_data.pool_relay_dns} vs "
f"Returned: {pool_ledger_state['relays']}"
)
if pool_creation_data.pool_metadata_url and pool_creation_data.pool_metadata_hash:
metadata = pool_ledger_state.get("metadata") or {}
metadata_hash = metadata.get("hash")
if metadata_hash != pool_creation_data.pool_metadata_hash:
errors_list.append(
"'metadata hash' value is different than expected; "
f"Expected: {pool_creation_data.pool_metadata_hash} vs "
f"Returned: {metadata_hash}"
)
metadata_url = metadata.get("url")
if metadata_url != pool_creation_data.pool_metadata_url:
errors_list.append(
"'metadata url' value is different than expected; "
f"Expected: {pool_creation_data.pool_metadata_url} vs "
f"Returned: {metadata_url}"
)
elif pool_ledger_state["metadata"] is not None:
errors_list.append(
"'metadata' value is different than expected; "
f"Expected: None vs Returned: {pool_ledger_state['metadata']}"
)
if errors_list:
for err in errors_list:
LOGGER.error(err)
LOGGER.error(f"Stake Pool Details: \n{pool_ledger_state}")
return "\n\n".join(errors_list)
def update_params(cluster_obj: clusterlib.ClusterLib, cli_arg: str, param_name: str, param_value):
"""Update params using update proposal."""
if str(cluster_obj.get_protocol_params()[param_name]) == str(param_value):
LOGGER.info(f"Value for '{param_name}' is already {param_value}. Nothing to do.")
return
LOGGER.info("Waiting for new epoch to submit proposal.")
cluster_obj.wait_for_new_epoch()
cluster_obj.submit_update_proposal(cli_args=[cli_arg, str(param_value)])
LOGGER.info(
f"Update Proposal submitted (cli_arg={cli_arg}, param_value={param_value}). "
"Sleeping until next epoch."
)
cluster_obj.wait_for_new_epoch()
updated_value = cluster_obj.get_protocol_params()[param_name]
if str(updated_value) != str(param_value):
raise AssertionError(
f"Cluster update proposal failed! Param value: {updated_value}.\n"
f"Tip:{cluster_obj.get_tip()}"
)