File size: 9,274 Bytes
f60a6c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os, time, socket, asyncio, logging, signal, gc, threading
from typing import Tuple

from aiohttp import web
import bittensor as bt

from scorevision.utils.settings import get_settings

logger = logging.getLogger("sv-signer")

NETUID = int(os.getenv("SCOREVISION_NETUID", "44"))
MECHID = 1

# Global shutdown event
shutdown_event = asyncio.Event()

_ASYNC_SUBTENSOR: bt.AsyncSubtensor | None = None
_ASYNC_SUBTENSOR_LOCK = asyncio.Lock()
_SYNC_SUBTENSOR: bt.Subtensor | None = None
_SYNC_SUBTENSOR_LOCK = threading.Lock()


async def get_subtensor():
    global _ASYNC_SUBTENSOR
    async with _ASYNC_SUBTENSOR_LOCK:
        if _ASYNC_SUBTENSOR is not None:
            return _ASYNC_SUBTENSOR
        settings = get_settings()
        ep = settings.BITTENSOR_SUBTENSOR_ENDPOINT
        fb = settings.BITTENSOR_SUBTENSOR_FALLBACK
        for endpoint in (ep, fb):
            try:
                st = bt.async_subtensor(endpoint)
                await st.initialize()
                _ASYNC_SUBTENSOR = st
                if endpoint != ep:
                    logger.warning("Subtensor init fell back to %s", endpoint)
                break
            except Exception as e:
                logger.warning("Subtensor init failed for %s: %s", endpoint, e)
                continue
        if _ASYNC_SUBTENSOR is None:
            raise RuntimeError("Unable to initialize async subtensor")
        return _ASYNC_SUBTENSOR


def _get_sync_subtensor() -> bt.Subtensor:
    global _SYNC_SUBTENSOR
    with _SYNC_SUBTENSOR_LOCK:
        if _SYNC_SUBTENSOR is not None:
            return _SYNC_SUBTENSOR
        settings = get_settings()
        ep = settings.BITTENSOR_SUBTENSOR_ENDPOINT
        fb = settings.BITTENSOR_SUBTENSOR_FALLBACK
        for endpoint in (ep, fb):
            try:
                st = bt.subtensor(endpoint)
                _SYNC_SUBTENSOR = st
                if endpoint != ep:
                    logger.warning("Sync subtensor init fell back to %s", endpoint)
                break
            except Exception as e:
                logger.warning("Sync subtensor init failed for %s: %s", endpoint, e)
                continue
        if _SYNC_SUBTENSOR is None:
            raise RuntimeError("Unable to initialize sync subtensor")
        return _SYNC_SUBTENSOR


async def _reset_async_subtensor():
    global _ASYNC_SUBTENSOR
    async with _ASYNC_SUBTENSOR_LOCK:
        if _ASYNC_SUBTENSOR is not None:
            try:
                await _ASYNC_SUBTENSOR.close()
            except Exception:
                pass
            _ASYNC_SUBTENSOR = None


def _reset_sync_subtensor():
    global _SYNC_SUBTENSOR
    with _SYNC_SUBTENSOR_LOCK:
        if _SYNC_SUBTENSOR is not None:
            try:
                _SYNC_SUBTENSOR.close()
            except Exception:
                pass
            _SYNC_SUBTENSOR = None


def _set_weights(
    *,
    wallet: "bt.wallet",
    netuid: int,
    mechid: int,
    uids: list[int],
    weights: list[float],
    wait_for_inclusion: bool,
    wait_for_finalization: bool,
    log_prefix: str = "[signer]",
) -> bool:
    """ """
    try:
        st = _get_sync_subtensor()
    except Exception:
        _reset_sync_subtensor()
        st = _get_sync_subtensor()

    try:
        success, message = st.set_weights(
            wallet=wallet,
            netuid=netuid,
            mechid=mechid,
            uids=uids,
            weights=weights,
            wait_for_inclusion=wait_for_inclusion,
            wait_for_finalization=wait_for_finalization,
        )
        msg_str = str(message or "")
    except Exception as e:
        msg_str = f"{type(e).__name__}: {e}"
        if "SettingWeightsTooFast" in msg_str:
            logger.error(
                f"{log_prefix} SettingWeightsTooFast (exception) → treating as success."
            )
            return True
        logger.warning(f"{log_prefix} set_weights exception: {msg_str}")
        return False

    if success:
        logger.info(f"{log_prefix} set_weights success.")
        return True

    if "SettingWeightsTooFast" in msg_str:
        logger.error(
            f"{log_prefix} SettingWeightsTooFast (return) → treating as success."
        )
        return True

    logger.warning(f"{log_prefix} set_weights failed: {msg_str or 'unknown error'}")
    return False


async def run_signer() -> None:
    settings = get_settings()
    host = settings.SIGNER_HOST
    port = settings.SIGNER_PORT

    def signal_handler():
        logger.info("Received shutdown signal, stopping signer...")
        shutdown_event.set()

    for sig in (signal.SIGTERM, signal.SIGINT):
        signal.signal(sig, lambda s, f: signal_handler())

    cold = settings.BITTENSOR_WALLET_COLD
    hot = settings.BITTENSOR_WALLET_HOT
    wallet = bt.wallet(name=cold, hotkey=hot)

    @web.middleware
    async def access_log(request: web.Request, handler):
        t0 = time.monotonic()
        try:
            resp = await handler(request)
            return resp
        finally:
            dt = (time.monotonic() - t0) * 1000
            logger.info(
                "[signer] %s %s -> %s %.1fms",
                request.method,
                request.path,
                getattr(getattr(request, "response", None), "status", "?"),
                dt,
            )

    async def health(_req: web.Request):
        return web.json_response({"ok": True})

    async def sign_handler(req: web.Request):
        try:
            payload = await req.json()
            data = payload.get("payloads") or payload.get("data") or []
            if isinstance(data, str):
                data = [data]
            sigs = [(wallet.hotkey.sign(data=d.encode("utf-8"))).hex() for d in data]
            return web.json_response(
                {
                    "success": True,
                    "signatures": sigs,
                    "hotkey": wallet.hotkey.ss58_address,
                }
            )
        except Exception as e:
            logger.error("[sign] error: %s", e)
            return web.json_response({"success": False, "error": str(e)}, status=500)

    async def set_weights_handler(req: web.Request):
        try:
            payload = await req.json()
            netuid = int(payload.get("netuid", NETUID))
            default_mechid = getattr(settings, "SCOREVISION_MECHID", MECHID)
            mechid = int(payload.get("mechid", default_mechid))
            uids = payload.get("uids") or []
            wgts = payload.get("weights") or []
            wfi = bool(payload.get("wait_for_inclusion", False))
            wff = bool(payload.get("wait_for_finalization", False))

            if isinstance(uids, int):
                uids = [uids]
            if isinstance(wgts, (int, float, str)):
                try:
                    wgts = [float(wgts)]
                except Exception:
                    wgts = [0.0]
            if not isinstance(uids, list):
                uids = list(uids)
            if not isinstance(wgts, list):
                wgts = list(wgts)
            try:
                uids = [int(u) for u in uids]
            except Exception:
                uids = []
            try:
                wgts = [float(w) for w in wgts]
            except Exception:
                wgts = []

            if len(uids) != len(wgts) or not uids:
                return web.json_response(
                    {"success": False, "error": "uids/weights mismatch or empty"},
                    status=400,
                )

            ok = _set_weights(
                wallet=wallet,
                netuid=netuid,
                mechid=mechid,
                uids=uids,
                weights=wgts,
                wait_for_inclusion=wfi,
                wait_for_finalization=wff,
                log_prefix="[signer]",
            )
            return web.json_response(
                (
                    {"success": True}
                    if ok
                    else {"success": False, "error": "set_weights failed"}
                ),
                status=200 if ok else 500,
            )
        except Exception as e:
            logger.error("[set_weights] error: %s", e)
            return web.json_response({"success": False, "error": str(e)}, status=500)
        finally:
            gc.collect()

    app = web.Application(middlewares=[access_log])
    app.add_routes(
        [
            web.get("/healthz", health),
            web.post("/sign", sign_handler),
            web.post("/set_weights", set_weights_handler),
        ]
    )
    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, host=host, port=port)
    await site.start()

    try:
        hn = socket.gethostname()
        ip = socket.gethostbyname(hn)
    except Exception:
        hn, ip = ("?", "?")
    logger.info(
        "Signer listening on http://%s:%s hostname=%s ip=%s", host, port, hn, ip
    )

    # Wait for shutdown signal instead of infinite loop
    try:
        await shutdown_event.wait()
    except asyncio.CancelledError:
        pass
    finally:
        logger.info("Shutting down signer...")
        await runner.cleanup()
        await _reset_async_subtensor()
        _reset_sync_subtensor()
        gc.collect()