|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib |
| 4 | +import sys |
| 5 | +import types |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | + |
| 10 | +def _install_epoch_validator_test_stubs(): |
| 11 | + if "bittensor" not in sys.modules: |
| 12 | + bittensor_mod = types.ModuleType("bittensor") |
| 13 | + |
| 14 | + class _Logging: |
| 15 | + def info(self, *_args, **_kwargs): |
| 16 | + return None |
| 17 | + |
| 18 | + def warning(self, *_args, **_kwargs): |
| 19 | + return None |
| 20 | + |
| 21 | + def success(self, *_args, **_kwargs): |
| 22 | + return None |
| 23 | + |
| 24 | + def error(self, *_args, **_kwargs): |
| 25 | + return None |
| 26 | + |
| 27 | + def debug(self, *_args, **_kwargs): |
| 28 | + return None |
| 29 | + |
| 30 | + bittensor_mod.logging = _Logging() |
| 31 | + bittensor_mod.BLOCKTIME = 12 |
| 32 | + sys.modules["bittensor"] = bittensor_mod |
| 33 | + |
| 34 | + if "metahash.base.validator" not in sys.modules: |
| 35 | + base_validator_mod = types.ModuleType("metahash.base.validator") |
| 36 | + |
| 37 | + class _BaseValidatorNeuron: |
| 38 | + pass |
| 39 | + |
| 40 | + base_validator_mod.BaseValidatorNeuron = _BaseValidatorNeuron |
| 41 | + sys.modules["metahash.base.validator"] = base_validator_mod |
| 42 | + |
| 43 | + if "metahash.validator.strategy" not in sys.modules: |
| 44 | + strategy_mod = types.ModuleType("metahash.validator.strategy") |
| 45 | + |
| 46 | + class _Strategy: |
| 47 | + def __init__(self, *args, **kwargs): |
| 48 | + return None |
| 49 | + |
| 50 | + def compute_weights_bps(self, **_kwargs): |
| 51 | + return {} |
| 52 | + |
| 53 | + strategy_mod.Strategy = _Strategy |
| 54 | + sys.modules["metahash.validator.strategy"] = strategy_mod |
| 55 | + |
| 56 | + |
| 57 | +_install_epoch_validator_test_stubs() |
| 58 | +sys.modules.pop("metahash.validator.epoch_validator", None) |
| 59 | +epoch_validator_mod = importlib.import_module("metahash.validator.epoch_validator") |
| 60 | + |
| 61 | + |
| 62 | +def _make_neuron(): |
| 63 | + neuron = object.__new__(epoch_validator_mod.EpochValidatorNeuron) |
| 64 | + neuron.should_exit = False |
| 65 | + neuron.config = types.SimpleNamespace(netuid=73, no_epoch=True, force_epoch=False) |
| 66 | + neuron._epoch_len = None |
| 67 | + neuron._override_active = False |
| 68 | + neuron._bootstrapped = False |
| 69 | + neuron._running = False |
| 70 | + neuron.block = 0 |
| 71 | + neuron.step = 0 |
| 72 | + neuron.sync = lambda: None |
| 73 | + neuron.concurrent_forward = _noop_forward |
| 74 | + neuron.axon = None |
| 75 | + return neuron |
| 76 | + |
| 77 | + |
| 78 | +async def _noop_forward(): |
| 79 | + return None |
| 80 | + |
| 81 | + |
| 82 | +def test_discover_epoch_length_retries_tempo_until_recovery(monkeypatch): |
| 83 | + neuron = _make_neuron() |
| 84 | + monkeypatch.setattr(epoch_validator_mod, "EPOCH_LENGTH_OVERRIDE", 0) |
| 85 | + monkeypatch.setattr(epoch_validator_mod.random, "uniform", lambda _a, _b: 0.0) |
| 86 | + monkeypatch.setattr(epoch_validator_mod.time, "sleep", lambda _s: None) |
| 87 | + |
| 88 | + class _Subtensor: |
| 89 | + def __init__(self): |
| 90 | + self.tempo_calls = 0 |
| 91 | + |
| 92 | + def tempo(self, _netuid): |
| 93 | + self.tempo_calls += 1 |
| 94 | + if self.tempo_calls < 3: |
| 95 | + raise RuntimeError("Header was not found in the database") |
| 96 | + return 360 |
| 97 | + |
| 98 | + def get_current_block(self): |
| 99 | + return 721 |
| 100 | + |
| 101 | + def get_next_epoch_start_block(self, _netuid): |
| 102 | + return 1081 |
| 103 | + |
| 104 | + st = _Subtensor() |
| 105 | + neuron.subtensor = st |
| 106 | + |
| 107 | + epoch_len = neuron._discover_epoch_length() |
| 108 | + assert epoch_len == 361 |
| 109 | + assert st.tempo_calls == 3 |
| 110 | + |
| 111 | + |
| 112 | +def test_retry_rpc_call_uses_exponential_backoff_capped_at_30s(monkeypatch): |
| 113 | + neuron = _make_neuron() |
| 114 | + sleep_calls: list[float] = [] |
| 115 | + |
| 116 | + monkeypatch.setattr(epoch_validator_mod.random, "uniform", lambda _a, _b: 0.0) |
| 117 | + |
| 118 | + def _fake_sleep(seconds: float): |
| 119 | + sleep_calls.append(seconds) |
| 120 | + if len(sleep_calls) >= 6: |
| 121 | + neuron.should_exit = True |
| 122 | + |
| 123 | + monkeypatch.setattr(epoch_validator_mod.time, "sleep", _fake_sleep) |
| 124 | + |
| 125 | + def _always_fail(): |
| 126 | + raise RuntimeError("rpc down") |
| 127 | + |
| 128 | + with pytest.raises(RuntimeError, match="Stopping RPC retries"): |
| 129 | + neuron._retry_rpc_call(_always_fail, op_name="tempo") |
| 130 | + |
| 131 | + assert sleep_calls == [1.0, 2.0, 4.0, 8.0, 16.0, 30.0] |
| 132 | + |
| 133 | + |
| 134 | +def test_discover_epoch_length_falls_back_when_next_head_is_none(monkeypatch): |
| 135 | + neuron = _make_neuron() |
| 136 | + monkeypatch.setattr(epoch_validator_mod, "EPOCH_LENGTH_OVERRIDE", 0) |
| 137 | + monkeypatch.setattr(epoch_validator_mod.random, "uniform", lambda _a, _b: 0.0) |
| 138 | + monkeypatch.setattr(epoch_validator_mod.time, "sleep", lambda _s: None) |
| 139 | + |
| 140 | + class _Subtensor: |
| 141 | + def tempo(self, _netuid): |
| 142 | + return 360 |
| 143 | + |
| 144 | + def get_current_block(self): |
| 145 | + return 720 |
| 146 | + |
| 147 | + def get_next_epoch_start_block(self, _netuid): |
| 148 | + return None |
| 149 | + |
| 150 | + neuron.subtensor = _Subtensor() |
| 151 | + epoch_len = neuron._discover_epoch_length() |
| 152 | + assert epoch_len == 361 |
| 153 | + |
| 154 | + |
| 155 | +def test_discover_epoch_length_uses_override_without_rpc(monkeypatch): |
| 156 | + neuron = _make_neuron() |
| 157 | + monkeypatch.setattr(epoch_validator_mod, "EPOCH_LENGTH_OVERRIDE", 7) |
| 158 | + |
| 159 | + class _Subtensor: |
| 160 | + def tempo(self, _netuid): # pragma: no cover - should not be called |
| 161 | + raise AssertionError("tempo() should not be called when override is set") |
| 162 | + |
| 163 | + def get_current_block(self): # pragma: no cover - should not be called |
| 164 | + raise AssertionError("get_current_block() should not be called when override is set") |
| 165 | + |
| 166 | + def get_next_epoch_start_block(self, _netuid): # pragma: no cover - should not be called |
| 167 | + raise AssertionError("get_next_epoch_start_block() should not be called when override is set") |
| 168 | + |
| 169 | + neuron.subtensor = _Subtensor() |
| 170 | + assert neuron._discover_epoch_length() == 7 |
| 171 | + |
| 172 | + |
| 173 | +def test_run_continues_after_snapshot_error(monkeypatch): |
| 174 | + neuron = _make_neuron() |
| 175 | + calls = {"snap": 0, "sleep": 0} |
| 176 | + |
| 177 | + def _snapshot(): |
| 178 | + calls["snap"] += 1 |
| 179 | + neuron.should_exit = True |
| 180 | + raise RuntimeError("boom") |
| 181 | + |
| 182 | + async def _fake_async_sleep(_seconds: float): |
| 183 | + calls["sleep"] += 1 |
| 184 | + return None |
| 185 | + |
| 186 | + monkeypatch.setattr(epoch_validator_mod.asyncio, "sleep", _fake_async_sleep) |
| 187 | + neuron._epoch_snapshot = _snapshot |
| 188 | + |
| 189 | + neuron.run() |
| 190 | + |
| 191 | + assert calls["snap"] == 1 |
| 192 | + assert calls["sleep"] == 1 |
| 193 | + assert neuron._running is False |
0 commit comments