File size: 2,507 Bytes
6c858ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from datetime import datetime, timedelta
from typing import TypeAlias

import bittensor as bt

from wandb_data import Uid, TIMEZONE

Weight: TypeAlias = float
Incentive: TypeAlias = float

NET_UID = 39
WEIGHTS_BY_MINER: list[list[tuple[Uid, Weight]]] = []
INCENTIVES: dict[Uid, Incentive] = {}
VALIDATOR_IDENTITIES: dict[Uid, str] = {}
VTRUST: dict[Uid, float] = {}
UPDATED: dict[Uid, int] = {}

subtensor = bt.subtensor()
metagraph = bt.metagraph(netuid=NET_UID)


def fetch_weights():
    WEIGHTS_BY_MINER.clear()
    weights = subtensor.weights(netuid=NET_UID)

    for miner_uid in range(metagraph.n.item()):
        WEIGHTS_BY_MINER.append([])
        for validator_uid, validator_weights in weights:
            if not metagraph.validator_permit[validator_uid]:
                continue

            weight = 0.0
            for miner_weight in validator_weights:
                if miner_weight[0] == miner_uid:
                    weight = miner_weight[1] / 2 ** 16
                    break
            WEIGHTS_BY_MINER[miner_uid].append((validator_uid, weight))


def fetch_incentives():
    INCENTIVES.clear()
    for i in range(len(metagraph.incentive)):
        INCENTIVES[i] = metagraph.incentive[i]

def fetch_vtrust():
    VTRUST.clear()
    for i in range(len(metagraph.validator_trust)):
        VTRUST[i] = metagraph.validator_trust[i]

def fetch_updated():
    UPDATED.clear()
    block = subtensor.get_current_block()
    for i in range(len(metagraph.last_update)):
        UPDATED[i] = block - metagraph.last_update[i]


def fetch_identities():
    VALIDATOR_IDENTITIES.clear()
    for uid in range(metagraph.n.item()):
        if not metagraph.validator_permit[uid]:
            continue
        identity = subtensor.substrate.query('SubtensorModule', 'Identities', [metagraph.coldkeys[uid]])
        if identity != None:
            VALIDATOR_IDENTITIES[uid] = identity.value["name"]


last_sync: datetime = datetime.fromtimestamp(0, TIMEZONE)
last_identity_sync: datetime = datetime.fromtimestamp(0, TIMEZONE)


def sync_metagraph():
    global last_sync
    now = datetime.now(TIMEZONE)
    if now - last_sync < timedelta(minutes=5):
        return

    print("Syncing metagraph...")
    last_sync = now
    metagraph.sync(subtensor=subtensor)

    fetch_weights()
    fetch_incentives()
    fetch_vtrust()
    fetch_updated()

    global last_identity_sync
    if now - last_identity_sync < timedelta(hours=12):
        return

    last_identity_sync = now
    fetch_identities()