|
| 1 | +""" |
| 2 | +Integration helpers for InfiniteHash Proxy pool routing. |
| 3 | +
|
| 4 | +This module updates subnet pool target hashrate in pools.toml so winning bids |
| 5 | +receive an absolute allocation while private pools can consume the remainder. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import os |
| 11 | +from collections.abc import Iterable |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | +import structlog |
| 15 | +import tomlkit |
| 16 | + |
| 17 | +from .models import BidResult |
| 18 | + |
| 19 | +logger = structlog.get_logger(__name__) |
| 20 | + |
| 21 | +DEFAULT_POOLS_CONFIG_PATH = "/root/src/proxy/pools.toml" |
| 22 | +DEFAULT_SUBNET_POOL_NAME = "central-proxy" |
| 23 | +DEFAULT_RELOAD_SENTINEL_PATH = "/root/src/proxy/.reload-ihp" |
| 24 | + |
| 25 | + |
| 26 | +def pools_config_path() -> Path: |
| 27 | + configured_path = os.environ.get("APS_MINER_POOLS_CONFIG_PATH", DEFAULT_POOLS_CONFIG_PATH) |
| 28 | + return Path(configured_path) |
| 29 | + |
| 30 | + |
| 31 | +def reload_sentinel_path() -> Path: |
| 32 | + configured_path = os.environ.get("APS_MINER_IHP_RELOAD_SENTINEL", DEFAULT_RELOAD_SENTINEL_PATH) |
| 33 | + return Path(configured_path) |
| 34 | + |
| 35 | + |
| 36 | +def _sum_hashrates_ph(bids: Iterable[BidResult]) -> float: |
| 37 | + total = 0.0 |
| 38 | + for bid in bids: |
| 39 | + try: |
| 40 | + total += float(bid.hashrate) |
| 41 | + except (TypeError, ValueError): |
| 42 | + logger.warning("Failed to parse bid hashrate for proxy routing", hashrate=bid.hashrate) |
| 43 | + return total |
| 44 | + |
| 45 | + |
| 46 | +def _format_target_hashrate_from_ph(total_ph: float) -> str: |
| 47 | + target_th = max(total_ph, 0.0) * 1000.0 |
| 48 | + target_str = f"{target_th:.3f}".rstrip("0").rstrip(".") |
| 49 | + if not target_str: |
| 50 | + target_str = "0" |
| 51 | + return f"{target_str}TH/s" |
| 52 | + |
| 53 | + |
| 54 | +def _is_subnet_pool(pool: dict, subnet_pool_name: str) -> bool: |
| 55 | + pool_name = str(pool.get("name", "")).strip().lower() |
| 56 | + return pool_name == subnet_pool_name |
| 57 | + |
| 58 | + |
| 59 | +def update_subnet_target_hashrate(won_bids: Iterable[BidResult], _lost_bids: Iterable[BidResult]) -> None: |
| 60 | + """ |
| 61 | + Update the subnet pool target hashrate in pools.toml. |
| 62 | +
|
| 63 | + Won hashrate is assigned to subnet pools as absolute `target_hashrate`. |
| 64 | + Remaining hashrate continues to flow to private pools through their weights. |
| 65 | + """ |
| 66 | + config_path = pools_config_path() |
| 67 | + if not config_path.exists(): |
| 68 | + logger.info("InfiniteHash proxy pools config not present - skipping update", path=str(config_path)) |
| 69 | + return |
| 70 | + |
| 71 | + subnet_pool_name = os.environ.get("APS_MINER_SUBNET_POOL_NAME", DEFAULT_SUBNET_POOL_NAME).strip().lower() |
| 72 | + target_hashrate = _format_target_hashrate_from_ph(_sum_hashrates_ph(won_bids)) |
| 73 | + |
| 74 | + try: |
| 75 | + with config_path.open("r", encoding="utf-8") as f: |
| 76 | + doc = tomlkit.load(f) |
| 77 | + except Exception: |
| 78 | + logger.exception("Unable to load InfiniteHash proxy pools config", path=str(config_path)) |
| 79 | + return |
| 80 | + |
| 81 | + pools_section = doc.get("pools") |
| 82 | + if not isinstance(pools_section, dict): |
| 83 | + logger.warning("Missing [pools] section in proxy config", path=str(config_path)) |
| 84 | + return |
| 85 | + |
| 86 | + main_pools = pools_section.get("main") |
| 87 | + if not isinstance(main_pools, list): |
| 88 | + logger.warning("Missing [[pools.main]] entries in proxy config", path=str(config_path)) |
| 89 | + return |
| 90 | + |
| 91 | + updated = False |
| 92 | + matched = 0 |
| 93 | + for pool in main_pools: |
| 94 | + if not isinstance(pool, dict): |
| 95 | + continue |
| 96 | + if not _is_subnet_pool(pool, subnet_pool_name): |
| 97 | + continue |
| 98 | + |
| 99 | + matched += 1 |
| 100 | + old_target_hashrate = pool.get("target_hashrate") |
| 101 | + if old_target_hashrate != target_hashrate: |
| 102 | + pool["target_hashrate"] = target_hashrate |
| 103 | + updated = True |
| 104 | + |
| 105 | + if "weight" in pool: |
| 106 | + del pool["weight"] |
| 107 | + updated = True |
| 108 | + |
| 109 | + if matched == 0: |
| 110 | + logger.warning( |
| 111 | + "No subnet pool entries matched for target hashrate update", |
| 112 | + name=subnet_pool_name, |
| 113 | + path=str(config_path), |
| 114 | + ) |
| 115 | + return |
| 116 | + |
| 117 | + if not updated: |
| 118 | + logger.debug("Subnet target hashrate already up to date", target_hashrate=target_hashrate) |
| 119 | + return |
| 120 | + |
| 121 | + try: |
| 122 | + with config_path.open("w", encoding="utf-8") as f: |
| 123 | + tomlkit.dump(doc, f) |
| 124 | + except Exception: |
| 125 | + logger.exception("Failed to persist proxy pools config updates", path=str(config_path)) |
| 126 | + return |
| 127 | + |
| 128 | + sentinel_path = reload_sentinel_path() |
| 129 | + try: |
| 130 | + sentinel_path.parent.mkdir(parents=True, exist_ok=True) |
| 131 | + sentinel_path.touch() |
| 132 | + except Exception: |
| 133 | + logger.exception("Failed to signal IHP proxy reload", sentinel=str(sentinel_path)) |
| 134 | + |
| 135 | + logger.info( |
| 136 | + "Updated subnet target hashrate", |
| 137 | + name=subnet_pool_name, |
| 138 | + target_hashrate=target_hashrate, |
| 139 | + matched_pools=matched, |
| 140 | + ) |
0 commit comments