|
| 1 | +import shutil |
| 2 | +import signal |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import time |
| 6 | +from argparse import ArgumentParser |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +import requests |
| 10 | + |
| 11 | +from systemtests import constants |
| 12 | +from systemtests.config import CONFIGURATIONS, Configuration |
| 13 | + |
| 14 | + |
| 15 | +def main() -> int: |
| 16 | + parser = ArgumentParser() |
| 17 | + parser.add_argument("--mode", choices=["data", "construction"], required=True) |
| 18 | + parser.add_argument("--network", choices=CONFIGURATIONS.keys(), required=True) |
| 19 | + args = parser.parse_args() |
| 20 | + |
| 21 | + mode = args.mode |
| 22 | + configuration = CONFIGURATIONS[args.network] |
| 23 | + |
| 24 | + process_rosetta = run_rosetta(configuration) |
| 25 | + process_adapter = run_proxy_to_observer_adapter(configuration) |
| 26 | + process_checker = run_mesh_cli(mode, configuration) |
| 27 | + |
| 28 | + # Handle termination signals |
| 29 | + def signal_handler(sig: Any, frame: Any): |
| 30 | + process_rosetta.kill() |
| 31 | + process_adapter.kill() |
| 32 | + process_checker.kill() |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | + signal.signal(signal.SIGINT, signal_handler) |
| 36 | + signal.signal(signal.SIGTERM, signal_handler) |
| 37 | + |
| 38 | + # Wait for checker to finish |
| 39 | + exit_code = process_checker.wait() |
| 40 | + |
| 41 | + process_rosetta.kill() |
| 42 | + process_adapter.kill() |
| 43 | + |
| 44 | + time.sleep(1) |
| 45 | + |
| 46 | + print(f"Checker finished with exit code: {exit_code}.") |
| 47 | + return exit_code |
| 48 | + |
| 49 | + |
| 50 | +def run_rosetta(configuration: Configuration): |
| 51 | + """ |
| 52 | + E.g. |
| 53 | +
|
| 54 | + rosetta --port=7091 --observer-http-url=http://localhost:8080 \ |
| 55 | + --observer-actual-shard=0 --network-id=D --network-name=devnet --native-currency=EGLD \ |
| 56 | + --first-historical-epoch=42 --num-historical-epochs=1 |
| 57 | + """ |
| 58 | + |
| 59 | + current_epoch = get_current_epoch(configuration) |
| 60 | + |
| 61 | + command = [ |
| 62 | + str(constants.PATH_ROSETTA), |
| 63 | + f"--port={constants.PORT_ROSETTA}", |
| 64 | + f"--observer-http-url=http://localhost:{constants.PORT_OBSERVER_SURROGATE}", |
| 65 | + f"--observer-actual-shard={configuration.network_shard}", |
| 66 | + f"--network-id={configuration.network_id}", |
| 67 | + f"--network-name={configuration.network_name}", |
| 68 | + f"--native-currency={configuration.native_currency}", |
| 69 | + f"--first-historical-epoch={current_epoch}", |
| 70 | + f"--num-historical-epochs={configuration.num_historical_epochs}", |
| 71 | + ] |
| 72 | + |
| 73 | + return subprocess.Popen(command) |
| 74 | + |
| 75 | + |
| 76 | +def run_proxy_to_observer_adapter(configuration: Configuration): |
| 77 | + command = [ |
| 78 | + str(constants.PATH_PROXY_TO_OBSERVER_ADAPTER), |
| 79 | + f"--proxy={configuration.proxy_url}", |
| 80 | + f"--shard={configuration.network_shard}", |
| 81 | + f"--sleep={constants.ADAPTER_DELAY_IN_MILLISECONDS}" |
| 82 | + ] |
| 83 | + |
| 84 | + return subprocess.Popen(command) |
| 85 | + |
| 86 | + |
| 87 | +def run_mesh_cli(mode: str, configuration: Configuration): |
| 88 | + if mode == "data": |
| 89 | + return run_mesh_cli_with_check_data(configuration) |
| 90 | + elif mode == "construction": |
| 91 | + return run_mesh_cli_with_check_construction(configuration) |
| 92 | + else: |
| 93 | + raise ValueError(f"Unknown mode: {mode}") |
| 94 | + |
| 95 | + |
| 96 | +def run_mesh_cli_with_check_construction(configuration: Configuration): |
| 97 | + """ |
| 98 | + E.g. |
| 99 | +
|
| 100 | + rosetta-cli check:construction --configuration-file devnet-construction.json \ |
| 101 | + --online-url=http://localhost:7091 --offline-url=http://localhost:7091 |
| 102 | + """ |
| 103 | + |
| 104 | + command = [ |
| 105 | + "rosetta-cli", |
| 106 | + "check:construction", |
| 107 | + f"--configuration-file={configuration.check_construction_configuration_file}", |
| 108 | + f"--online-url=http://localhost:{constants.PORT_ROSETTA}", |
| 109 | + f"--offline-url=http://localhost:{constants.PORT_ROSETTA}", |
| 110 | + ] |
| 111 | + |
| 112 | + return subprocess.Popen(command) |
| 113 | + |
| 114 | + |
| 115 | +def run_mesh_cli_with_check_data(configuration: Configuration): |
| 116 | + """ |
| 117 | + E.g. |
| 118 | +
|
| 119 | + rosetta-cli check:data --configuration-file devnet-data.json \ |
| 120 | + --online-url=http://localhost:7091 --data-dir=devnet-data |
| 121 | + """ |
| 122 | + |
| 123 | + shutil.rmtree(configuration.check_data_directory, ignore_errors=True) |
| 124 | + |
| 125 | + current_epoch = get_current_epoch(configuration) |
| 126 | + first_historical_epoch = current_epoch - configuration.num_historical_epochs + 1 |
| 127 | + start_block = get_start_of_epoch(configuration, first_historical_epoch) |
| 128 | + |
| 129 | + command = [ |
| 130 | + "rosetta-cli", |
| 131 | + "check:data", |
| 132 | + f"--configuration-file={configuration.check_data_configuration_file}", |
| 133 | + f"--online-url=http://localhost:{constants.PORT_ROSETTA}", |
| 134 | + f"--data-dir={configuration.check_data_directory}", |
| 135 | + f"--start-block={start_block}" |
| 136 | + ] |
| 137 | + |
| 138 | + return subprocess.Popen(command) |
| 139 | + |
| 140 | + |
| 141 | +def get_current_epoch(configuration: Configuration) -> int: |
| 142 | + response = requests.get(f"{configuration.proxy_url}/network/status/{configuration.network_shard}") |
| 143 | + response.raise_for_status() |
| 144 | + return response.json()["data"]["status"]["erd_epoch_number"] |
| 145 | + |
| 146 | + |
| 147 | +def get_start_of_epoch(configuration: Configuration, epoch: int) -> int: |
| 148 | + response = requests.get(f"{configuration.proxy_url}/network/epoch-start/{configuration.network_shard}/by-epoch/{epoch}") |
| 149 | + response.raise_for_status() |
| 150 | + return response.json()["data"]["epochStart"]["nonce"] |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + exit_code = main() |
| 155 | + sys.exit(exit_code) |
0 commit comments