Spaces:
Sleeping

File size: 5,679 Bytes
07ba06f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
from kademlia.network import Server
import socket
from typing import Optional, Tuple

def get_local_ip() -> str:
    """Gets the local IP address."""
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        ip = s.getsockname()[0]
        s.close()
        return ip
    except Exception:
        return "127.0.0.1"

class SwarmDHTNode:
    """
    Wraps Kademlia DHT for Swarm Node Discovery.
    Provides methods to announce the current miner's HTTP port and query for peers.
    """
    def __init__(self, dht_port: int, bootstrap_host: Optional[str] = None, bootstrap_port: Optional[int] = None):
        self.dht_port = dht_port
        self.bootstrap_host = bootstrap_host
        self.bootstrap_port = bootstrap_port
        self.node = Server()
        self.local_ip = get_local_ip()

    async def start(self):
        """Starts the DHT UDP server and bootstraps if a host is provided."""
        await self.node.listen(self.dht_port)
        if self.bootstrap_host and self.bootstrap_port:
            try:
                print(f"📡 [DHT] Bootstrapping from {self.bootstrap_host}:{self.bootstrap_port}...")
                await self.node.bootstrap([(self.bootstrap_host, self.bootstrap_port)])
                print("✅ [DHT] Bootstrapped successfully.")
            except Exception as e:
                print(f"⚠️ [DHT] Bootstrap failed: {e}")
        else:
            print("🌟 [DHT] Running as Genesis Bootstrap Node.")

    async def announce_miner(self, layer_start: int, layer_end: int, http_port: int, model: str = "SmolLM-135M"):
        """
        Publishes this node's HTTP endpoint for a specific layer shard.
        Key: <model>:layers:<start>-<end>
        Value: http://<local_ip>:<http_port>
        """
        import json
        key = f"{model}:layers:{layer_start}-{layer_end}"
        value = f"http://{self.local_ip}:{http_port}"
        print(f"📣 [DHT] Announcing {key} -> {value}")
        await self.node.set(key, value)

        # Update mesh_topology list
        topology_key = f"{model}:mesh_topology"
        current_state = await self.node.get(topology_key)
        claimed_ranges = []
        if current_state:
            try:
                claimed_ranges = json.loads(current_state)
            except Exception:
                claimed_ranges = []
        
        # Avoid duplicate entries
        if [layer_start, layer_end] not in claimed_ranges:
            claimed_ranges.append([layer_start, layer_end])
            await self.node.set(topology_key, json.dumps(claimed_ranges))

    async def find_peer(self, layer_index: int, model: str = "SmolLM-135M") -> Optional[str]:
        """
        Queries the DHT for the HTTP endpoint of a peer hosting the specific layer_index.
        Dynamically discovers the exact layer range using the mesh_topology.
        """
        import json
        topology_key = f"{model}:mesh_topology"
        
        # 1. Fetch current topology to find the exact range holding layer_index
        current_state = await self.node.get(topology_key)
        target_range = None
        if current_state:
            try:
                claimed_ranges = json.loads(current_state)
                for r in claimed_ranges:
                    if isinstance(r, list) and len(r) == 2:
                        if r[0] <= layer_index <= r[1]:
                            target_range = r
                            break
            except Exception:
                pass
                
        if not target_range:
            if layer_index < 15:
                target_range = [0, 14]
            else:
                target_range = [15, 29]
            
        # 2. Query the exact key for that range
        key = f"{model}:layers:{target_range[0]}-{target_range[1]}"
        print(f"🔍 [DHT] Searching for exact key: {key}...")
        result = await self.node.get(key)
        return result

    async def negotiate_layers(self, capacity: int, total_layers: int = 30, model: str = "SmolLM-135M") -> Tuple[int, int]:
        """
        Dynamically finds an unassigned block of layers up to 'capacity' size.
        Uses a shared DHT key 'mesh_topology' to track claims.
        """
        import json
        topology_key = f"{model}:mesh_topology"
        
        # 1. Fetch current topology
        current_state = await self.node.get(topology_key)
        if current_state:
            try:
                claimed_ranges = json.loads(current_state)
            except Exception:
                claimed_ranges = []
        else:
            claimed_ranges = []
            
        # 2. Find the first gap or append to the end
        # A simple algorithm: just take the highest claimed layer + 1
        highest_layer = -1
        for r in claimed_ranges:
            if isinstance(r, list) and len(r) == 2:
                highest_layer = max(highest_layer, r[1])
                
        start_layer = highest_layer + 1
        
        # If we exceeded total layers, just help out on the first block (overlap)
        if start_layer >= total_layers:
            print("⚠️ [DHT] Mesh is fully covered. Joining as redundant replica for layers 0 to capacity.")
            start_layer = 0
            
        end_layer = min(start_layer + capacity - 1, total_layers - 1)
        
        # 3. Claim the range
        claimed_ranges.append([start_layer, end_layer])
        await self.node.set(topology_key, json.dumps(claimed_ranges))
        
        print(f"🤝 [DHT] Auto-negotiated layers: {start_layer} to {end_layer} (Capacity: {capacity})")
        return start_layer, end_layer