nexaroa 0.0.111__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. neuroshard/__init__.py +93 -0
  2. neuroshard/__main__.py +4 -0
  3. neuroshard/cli.py +466 -0
  4. neuroshard/core/__init__.py +92 -0
  5. neuroshard/core/consensus/verifier.py +252 -0
  6. neuroshard/core/crypto/__init__.py +20 -0
  7. neuroshard/core/crypto/ecdsa.py +392 -0
  8. neuroshard/core/economics/__init__.py +52 -0
  9. neuroshard/core/economics/constants.py +387 -0
  10. neuroshard/core/economics/ledger.py +2111 -0
  11. neuroshard/core/economics/market.py +975 -0
  12. neuroshard/core/economics/wallet.py +168 -0
  13. neuroshard/core/governance/__init__.py +74 -0
  14. neuroshard/core/governance/proposal.py +561 -0
  15. neuroshard/core/governance/registry.py +545 -0
  16. neuroshard/core/governance/versioning.py +332 -0
  17. neuroshard/core/governance/voting.py +453 -0
  18. neuroshard/core/model/__init__.py +30 -0
  19. neuroshard/core/model/dynamic.py +4186 -0
  20. neuroshard/core/model/llm.py +905 -0
  21. neuroshard/core/model/registry.py +164 -0
  22. neuroshard/core/model/scaler.py +387 -0
  23. neuroshard/core/model/tokenizer.py +568 -0
  24. neuroshard/core/network/__init__.py +56 -0
  25. neuroshard/core/network/connection_pool.py +72 -0
  26. neuroshard/core/network/dht.py +130 -0
  27. neuroshard/core/network/dht_plan.py +55 -0
  28. neuroshard/core/network/dht_proof_store.py +516 -0
  29. neuroshard/core/network/dht_protocol.py +261 -0
  30. neuroshard/core/network/dht_service.py +506 -0
  31. neuroshard/core/network/encrypted_channel.py +141 -0
  32. neuroshard/core/network/nat.py +201 -0
  33. neuroshard/core/network/nat_traversal.py +695 -0
  34. neuroshard/core/network/p2p.py +929 -0
  35. neuroshard/core/network/p2p_data.py +150 -0
  36. neuroshard/core/swarm/__init__.py +106 -0
  37. neuroshard/core/swarm/aggregation.py +729 -0
  38. neuroshard/core/swarm/buffers.py +643 -0
  39. neuroshard/core/swarm/checkpoint.py +709 -0
  40. neuroshard/core/swarm/compute.py +624 -0
  41. neuroshard/core/swarm/diloco.py +844 -0
  42. neuroshard/core/swarm/factory.py +1288 -0
  43. neuroshard/core/swarm/heartbeat.py +669 -0
  44. neuroshard/core/swarm/logger.py +487 -0
  45. neuroshard/core/swarm/router.py +658 -0
  46. neuroshard/core/swarm/service.py +640 -0
  47. neuroshard/core/training/__init__.py +29 -0
  48. neuroshard/core/training/checkpoint.py +600 -0
  49. neuroshard/core/training/distributed.py +1602 -0
  50. neuroshard/core/training/global_tracker.py +617 -0
  51. neuroshard/core/training/production.py +276 -0
  52. neuroshard/governance_cli.py +729 -0
  53. neuroshard/grpc_server.py +895 -0
  54. neuroshard/runner.py +3223 -0
  55. neuroshard/sdk/__init__.py +92 -0
  56. neuroshard/sdk/client.py +990 -0
  57. neuroshard/sdk/errors.py +101 -0
  58. neuroshard/sdk/types.py +282 -0
  59. neuroshard/tracker/__init__.py +0 -0
  60. neuroshard/tracker/server.py +864 -0
  61. neuroshard/ui/__init__.py +0 -0
  62. neuroshard/ui/app.py +102 -0
  63. neuroshard/ui/templates/index.html +1052 -0
  64. neuroshard/utils/__init__.py +0 -0
  65. neuroshard/utils/autostart.py +81 -0
  66. neuroshard/utils/hardware.py +121 -0
  67. neuroshard/utils/serialization.py +90 -0
  68. neuroshard/version.py +1 -0
  69. nexaroa-0.0.111.dist-info/METADATA +283 -0
  70. nexaroa-0.0.111.dist-info/RECORD +78 -0
  71. nexaroa-0.0.111.dist-info/WHEEL +5 -0
  72. nexaroa-0.0.111.dist-info/entry_points.txt +4 -0
  73. nexaroa-0.0.111.dist-info/licenses/LICENSE +190 -0
  74. nexaroa-0.0.111.dist-info/top_level.txt +2 -0
  75. protos/__init__.py +0 -0
  76. protos/neuroshard.proto +651 -0
  77. protos/neuroshard_pb2.py +160 -0
  78. protos/neuroshard_pb2_grpc.py +1298 -0
@@ -0,0 +1,201 @@
1
+ import socket
2
+ import threading
3
+ import logging
4
+ import requests
5
+
6
+ logger = logging.getLogger(__name__)
7
+
8
+ class NATTraverser:
9
+ """
10
+ Handles UPnP port mapping and Public IP discovery.
11
+ Uses a simple SSDP implementation to avoid heavy dependencies like miniupnpc.
12
+ """
13
+ def __init__(self):
14
+ self.local_ip = self._get_local_ip()
15
+ self.public_ip = None
16
+ self.gateway_url = None
17
+ self.mapped_ports = []
18
+
19
+ def _get_local_ip(self):
20
+ s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
21
+ try:
22
+ # doesn't even have to be reachable
23
+ s.connect(('10.255.255.255', 1))
24
+ IP = s.getsockname()[0]
25
+ except Exception:
26
+ IP = '127.0.0.1'
27
+ finally:
28
+ s.close()
29
+ return IP
30
+
31
+ def discover_public_ip(self):
32
+ """Attempt to get the public IP address from external services."""
33
+ try:
34
+ services = [
35
+ 'https://api.ipify.org',
36
+ 'https://ifconfig.me/ip',
37
+ 'https://icanhazip.com'
38
+ ]
39
+ for service in services:
40
+ try:
41
+ self.public_ip = requests.get(service, timeout=3).text.strip()
42
+ logger.info(f"Detected Public IP: {self.public_ip}")
43
+ return self.public_ip
44
+ except:
45
+ continue
46
+ except Exception:
47
+ pass
48
+ return None
49
+
50
+ def attempt_upnp_mapping(self, port: int, protocol: str = "TCP", description: str = "NeuroShard Node"):
51
+ """
52
+ Attempt to map a port using UPnP (IGD).
53
+ This is a simplified pure-python implementation of the UPnP SOAP protocol.
54
+ """
55
+ if not self.gateway_url:
56
+ if not self._discover_gateway():
57
+ logger.warning("UPnP: No gateway found.")
58
+ return False
59
+
60
+ logger.info(f"UPnP: Attempting to map {self.public_ip or '*'}:{port} -> {self.local_ip}:{port} ({protocol})")
61
+
62
+ # AddPortMapping SOAP Request
63
+ # Service type usually: urn:schemas-upnp-org:service:WANIPConnection:1
64
+ # or urn:schemas-upnp-org:service:WANPPPConnection:1
65
+
66
+ service_types = [
67
+ "urn:schemas-upnp-org:service:WANIPConnection:1",
68
+ "urn:schemas-upnp-org:service:WANPPPConnection:1"
69
+ ]
70
+
71
+ for service_type in service_types:
72
+ payload = f"""<?xml version="1.0"?>
73
+ <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
74
+ <s:Body>
75
+ <u:AddPortMapping xmlns:u="{service_type}">
76
+ <NewRemoteHost></NewRemoteHost>
77
+ <NewExternalPort>{port}</NewExternalPort>
78
+ <NewProtocol>{protocol}</NewProtocol>
79
+ <NewInternalPort>{port}</NewInternalPort>
80
+ <NewInternalClient>{self.local_ip}</NewInternalClient>
81
+ <NewEnabled>1</NewEnabled>
82
+ <NewPortMappingDescription>{description}</NewPortMappingDescription>
83
+ <NewLeaseDuration>0</NewLeaseDuration>
84
+ </u:AddPortMapping>
85
+ </s:Body>
86
+ </s:Envelope>"""
87
+
88
+ try:
89
+ headers = {
90
+ 'SOAPAction': f'"{service_type}#AddPortMapping"',
91
+ 'Content-Type': 'text/xml'
92
+ }
93
+ # Construct full control URL
94
+ # In a full impl, we parse the XML from discovery.
95
+ # Here we take a shortcut assumption or need to improve _discover_gateway to return full control URL.
96
+ control_url = self.control_url
97
+
98
+ resp = requests.post(control_url, data=payload, headers=headers, timeout=2)
99
+ if resp.status_code == 200:
100
+ logger.info(f"UPnP: Successfully mapped port {port}")
101
+ self.mapped_ports.append(port)
102
+ return True
103
+ except Exception as e:
104
+ logger.debug(f"UPnP Attempt failed for {service_type}: {e}")
105
+
106
+ logger.warning("UPnP: Failed to map port.")
107
+ return False
108
+
109
+ def _discover_gateway(self):
110
+ """
111
+ Discover UPnP gateway via SSDP (Simple Service Discovery Protocol).
112
+ Returns True if found and sets self.control_url.
113
+ """
114
+ SSDP_ADDR = "239.255.255.250"
115
+ SSDP_PORT = 1900
116
+ SSDP_MX = 2
117
+ SSDP_ST = "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
118
+
119
+ ssdpRequest = f"M-SEARCH * HTTP/1.1\r\n" + \
120
+ f"HOST: {SSDP_ADDR}:{SSDP_PORT}\r\n" + \
121
+ f"MAN: \"ssdp:discover\"\r\n" + \
122
+ f"MX: {SSDP_MX}\r\n" + \
123
+ f"ST: {SSDP_ST}\r\n" + \
124
+ "\r\n"
125
+
126
+ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
127
+ sock.settimeout(3)
128
+
129
+ try:
130
+ sock.sendto(ssdpRequest.encode(), (SSDP_ADDR, SSDP_PORT))
131
+ while True:
132
+ data, addr = sock.recvfrom(1024)
133
+ response = data.decode()
134
+
135
+ # Parse LOCATION header
136
+ import re
137
+ location_match = re.search(r"LOCATION: (.*)", response, re.IGNORECASE)
138
+ if location_match:
139
+ location_url = location_match.group(1).strip()
140
+ logger.debug(f"UPnP: Found gateway description at {location_url}")
141
+
142
+ # Fetch the XML description
143
+ try:
144
+ xml_resp = requests.get(location_url, timeout=2).text
145
+
146
+ # Find ControlURL for WANIPConnection or WANPPPConnection
147
+ # Very simple regex parsing to avoid xml deps overhead if possible,
148
+ # but xml.etree is standard.
149
+ import xml.etree.ElementTree as ET
150
+ root = ET.fromstring(xml_resp)
151
+
152
+ ns = {'': 'urn:schemas-upnp-org:device-1-0'} # Default NS often tricky in UPnP XML
153
+ # We'll search recursively for serviceType
154
+
155
+ control_url_path = None
156
+ base_url = location_url.rsplit('/', 1)[0]
157
+ if "<URLBase>" in xml_resp:
158
+ # Some devices use URLBase
159
+ pass
160
+
161
+ # Naive string search for service + controlURL to be robust against NS weirdness
162
+ # Look for WANIPConnection
163
+ services = root.findall(".//*{urn:schemas-upnp-org:device-1-0}service")
164
+ if not services:
165
+ # Try without namespace or wildcard
166
+ services = root.findall(".//service")
167
+
168
+ for svc in services:
169
+ sType = svc.findtext("{urn:schemas-upnp-org:device-1-0}serviceType") or svc.findtext("serviceType")
170
+ if sType and ("WANIPConnection" in sType or "WANPPPConnection" in sType):
171
+ cURL = svc.findtext("{urn:schemas-upnp-org:device-1-0}controlURL") or svc.findtext("controlURL")
172
+ if cURL:
173
+ control_url_path = cURL
174
+ break
175
+
176
+ if control_url_path:
177
+ # Handle relative URL
178
+ if not control_url_path.startswith("http"):
179
+ if control_url_path.startswith("/"):
180
+ # Absolute path relative to IP:Port
181
+ from urllib.parse import urljoin
182
+ self.control_url = urljoin(location_url, control_url_path)
183
+ else:
184
+ self.control_url = urljoin(location_url, control_url_path)
185
+ else:
186
+ self.control_url = control_url_path
187
+
188
+ self.gateway_url = location_url
189
+ return True
190
+
191
+ except Exception as e:
192
+ logger.debug(f"Error parsing gateway XML: {e}")
193
+ continue
194
+
195
+ except (socket.timeout, OSError) as e:
196
+ logger.debug(f"UPnP Discovery failed: {e}")
197
+ finally:
198
+ sock.close()
199
+
200
+ return False
201
+