|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Cross-platform setup script for dashd and test blockchain data. |
| 3 | +
|
| 4 | +Downloads the Dash Core binary and regtest test data for integration tests. |
| 5 | +Outputs DASHD_PATH and DASHD_DATADIR lines suitable for appending to GITHUB_ENV |
| 6 | +or evaluating in a shell. |
| 7 | +
|
| 8 | +Environment variables: |
| 9 | + DASHVERSION - Dash Core version (default: 23.1.0) |
| 10 | + TEST_DATA_VERSION - Test data release version (default: v0.0.2) |
| 11 | + TEST_DATA_REPO - GitHub repo for test data (default: dashpay/regtest-blockchain) |
| 12 | + CACHE_DIR - Cache directory (default: ~/.rust-dashcore-test) |
| 13 | +""" |
| 14 | + |
| 15 | +import os |
| 16 | +import platform |
| 17 | +import sys |
| 18 | +import tarfile |
| 19 | +import time |
| 20 | +import urllib.request |
| 21 | +import zipfile |
| 22 | + |
| 23 | +# Keep these defaults in sync with .github/workflows/build-and-test.yml |
| 24 | +DASHVERSION = os.environ.get("DASHVERSION", "23.1.0") |
| 25 | +TEST_DATA_VERSION = os.environ.get("TEST_DATA_VERSION", "v0.0.2") |
| 26 | +TEST_DATA_REPO = os.environ.get("TEST_DATA_REPO", "dashpay/regtest-blockchain") |
| 27 | + |
| 28 | + |
| 29 | +def get_cache_dir(): |
| 30 | + if "CACHE_DIR" in os.environ: |
| 31 | + return os.environ["CACHE_DIR"] |
| 32 | + home = os.environ.get("HOME") or os.environ.get("USERPROFILE") |
| 33 | + if not home: |
| 34 | + sys.exit("Cannot determine home directory: neither HOME nor USERPROFILE is set") |
| 35 | + return os.path.join(home, ".rust-dashcore-test") |
| 36 | + |
| 37 | + |
| 38 | +def get_asset_info(): |
| 39 | + """Return the asset filename for the current platform.""" |
| 40 | + system = platform.system() |
| 41 | + machine = platform.machine() |
| 42 | + |
| 43 | + if system == "Linux": |
| 44 | + linux_archs = {"aarch64": "aarch64", "arm64": "aarch64", "x86_64": "x86_64", "amd64": "x86_64"} |
| 45 | + arch = linux_archs.get(machine) |
| 46 | + if not arch: |
| 47 | + sys.exit(f"Unsupported Linux architecture: {machine}") |
| 48 | + asset = f"dashcore-{DASHVERSION}-{arch}-linux-gnu.tar.gz" |
| 49 | + elif system == "Darwin": |
| 50 | + darwin_archs = {"arm64": "arm64", "x86_64": "x86_64"} |
| 51 | + arch = darwin_archs.get(machine) |
| 52 | + if not arch: |
| 53 | + sys.exit(f"Unsupported macOS architecture: {machine}") |
| 54 | + asset = f"dashcore-{DASHVERSION}-{arch}-apple-darwin.tar.gz" |
| 55 | + elif system == "Windows": |
| 56 | + asset = f"dashcore-{DASHVERSION}-win64.zip" |
| 57 | + else: |
| 58 | + sys.exit(f"Unsupported platform: {system}") |
| 59 | + |
| 60 | + return asset |
| 61 | + |
| 62 | + |
| 63 | +def log(msg): |
| 64 | + print(msg, file=sys.stderr) |
| 65 | + |
| 66 | + |
| 67 | +def download(url, dest, timeout=300, retries=3): |
| 68 | + for attempt in range(1, retries + 1): |
| 69 | + try: |
| 70 | + log(f"Downloading {url} (attempt {attempt}/{retries})...") |
| 71 | + with urllib.request.urlopen(url, timeout=timeout) as response: |
| 72 | + with open(dest, "wb") as f: |
| 73 | + while chunk := response.read(8192): |
| 74 | + f.write(chunk) |
| 75 | + return |
| 76 | + except Exception as e: |
| 77 | + log(f"Download failed: {e}") |
| 78 | + if attempt == retries: |
| 79 | + sys.exit(f"Failed to download {url} after {retries} attempts") |
| 80 | + time.sleep(5 * attempt) |
| 81 | + |
| 82 | + |
| 83 | +def extract(archive_path, dest_dir): |
| 84 | + if archive_path.endswith(".zip"): |
| 85 | + with zipfile.ZipFile(archive_path, "r") as zf: |
| 86 | + zf.extractall(dest_dir) |
| 87 | + else: |
| 88 | + with tarfile.open(archive_path, "r:gz") as tf: |
| 89 | + tf.extractall(dest_dir, filter="data") |
| 90 | + |
| 91 | + |
| 92 | +def setup_dashd(cache_dir): |
| 93 | + """Download and extract dashd binary. Returns the path to the dashd binary.""" |
| 94 | + asset = get_asset_info() |
| 95 | + dashd_dir = os.path.join(cache_dir, f"dashcore-{DASHVERSION}") |
| 96 | + |
| 97 | + ext = ".exe" if platform.system() == "Windows" else "" |
| 98 | + dashd_bin = os.path.join(dashd_dir, "bin", f"dashd{ext}") |
| 99 | + |
| 100 | + if os.path.isfile(dashd_bin): |
| 101 | + log(f"dashd {DASHVERSION} already available") |
| 102 | + return dashd_bin |
| 103 | + |
| 104 | + log(f"Downloading dashd {DASHVERSION}...") |
| 105 | + archive_path = os.path.join(cache_dir, asset) |
| 106 | + url = f"https://github.com/dashpay/dash/releases/download/v{DASHVERSION}/{asset}" |
| 107 | + download(url, archive_path) |
| 108 | + extract(archive_path, cache_dir) |
| 109 | + os.remove(archive_path) |
| 110 | + log(f"Downloaded dashd to {dashd_dir}") |
| 111 | + |
| 112 | + if not os.path.isfile(dashd_bin): |
| 113 | + sys.exit(f"Expected binary not found after extraction: {dashd_bin}") |
| 114 | + |
| 115 | + return dashd_bin |
| 116 | + |
| 117 | + |
| 118 | +def setup_test_data(cache_dir): |
| 119 | + """Download and extract test blockchain data. Returns the datadir path.""" |
| 120 | + test_data_dir = os.path.join( |
| 121 | + cache_dir, f"regtest-blockchain-{TEST_DATA_VERSION}", "regtest-40000" |
| 122 | + ) |
| 123 | + blocks_dir = os.path.join(test_data_dir, "regtest", "blocks") |
| 124 | + |
| 125 | + if os.path.isdir(blocks_dir): |
| 126 | + log(f"Test blockchain data {TEST_DATA_VERSION} already available") |
| 127 | + return test_data_dir |
| 128 | + |
| 129 | + log(f"Downloading test blockchain data {TEST_DATA_VERSION}...") |
| 130 | + parent_dir = os.path.join(cache_dir, f"regtest-blockchain-{TEST_DATA_VERSION}") |
| 131 | + os.makedirs(parent_dir, exist_ok=True) |
| 132 | + |
| 133 | + archive_path = os.path.join(cache_dir, "regtest-40000.tar.gz") |
| 134 | + url = f"https://github.com/{TEST_DATA_REPO}/releases/download/{TEST_DATA_VERSION}/regtest-40000.tar.gz" |
| 135 | + download(url, archive_path) |
| 136 | + extract(archive_path, parent_dir) |
| 137 | + os.remove(archive_path) |
| 138 | + |
| 139 | + if not os.path.isdir(blocks_dir): |
| 140 | + sys.exit(f"Expected blocks directory not found after extraction: {blocks_dir}") |
| 141 | + |
| 142 | + log(f"Downloaded test data to {test_data_dir}") |
| 143 | + |
| 144 | + return test_data_dir |
| 145 | + |
| 146 | + |
| 147 | +def main(): |
| 148 | + cache_dir = get_cache_dir() |
| 149 | + os.makedirs(cache_dir, exist_ok=True) |
| 150 | + |
| 151 | + dashd_path = setup_dashd(cache_dir) |
| 152 | + datadir = setup_test_data(cache_dir) |
| 153 | + |
| 154 | + # Output lines for GITHUB_ENV or shell eval |
| 155 | + print(f"DASHD_PATH={dashd_path}") |
| 156 | + print(f"DASHD_DATADIR={datadir}") |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + main() |
0 commit comments