matrix-compute-node 0.1.0__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.
- matrix_compute_node-0.1.0.dist-info/LICENSE +190 -0
- matrix_compute_node-0.1.0.dist-info/METADATA +71 -0
- matrix_compute_node-0.1.0.dist-info/RECORD +35 -0
- matrix_compute_node-0.1.0.dist-info/WHEEL +5 -0
- matrix_compute_node-0.1.0.dist-info/entry_points.txt +2 -0
- matrix_compute_node-0.1.0.dist-info/top_level.txt +1 -0
- src/__init__.py +52 -0
- src/attestation/__init__.py +114 -0
- src/attestation/base.py +226 -0
- src/attestation/nvml_attest.py +1000 -0
- src/attestation/sev_snp_attest.py +568 -0
- src/attestation/simulated.py +325 -0
- src/attestation/tdx_attest.py +596 -0
- src/auth/__init__.py +19 -0
- src/auth/hashcash.py +245 -0
- src/auth/l402_gate.py +553 -0
- src/config/__init__.py +4 -0
- src/config/settings.py +730 -0
- src/crypto/__init__.py +4 -0
- src/crypto/signer.py +257 -0
- src/engine/__init__.py +46 -0
- src/engine/compute_core.py +409 -0
- src/engine/enclave_compute.py +587 -0
- src/engine/scheduler.py +213 -0
- src/engine/session_manager.py +563 -0
- src/main.py +224 -0
- src/p2p/__init__.py +5 -0
- src/p2p/servicer.py +893 -0
- src/p2p/tunnel_client.py +604 -0
- src/relay/__init__.py +5 -0
- src/relay/nostr_client.py +266 -0
- src/settings.py +8 -0
- src/test_doubles/__init__.py +5 -0
- src/test_doubles/mock_nostr_relay.py +332 -0
- src/test_doubles/mock_settlement.py +410 -0
src/main.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"""Daemon runner and entrypoint for matrix-compute-node."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from concurrent import futures
|
|
6
|
+
import logging
|
|
7
|
+
import signal
|
|
8
|
+
import sys
|
|
9
|
+
import threading
|
|
10
|
+
import time
|
|
11
|
+
from typing import Any, Optional, Sequence
|
|
12
|
+
|
|
13
|
+
import grpc
|
|
14
|
+
|
|
15
|
+
from matrix_contracts.schemas.discovery import BondStatus, PricingSpecs
|
|
16
|
+
from matrix_contracts.v1 import pb2_grpc
|
|
17
|
+
from src.attestation.nvml_attest import HardwareAttestationEngine
|
|
18
|
+
from src.auth.l402_gate import L402PreAllocationGate
|
|
19
|
+
from src.config.settings import NodeConfig
|
|
20
|
+
from src.crypto.signer import WorkerSigner
|
|
21
|
+
from src.engine.compute_core import ComputeEngine
|
|
22
|
+
from src.p2p.servicer import MatrixComputeServicer
|
|
23
|
+
from src.relay.nostr_client import NostrRelayClient
|
|
24
|
+
|
|
25
|
+
logger = logging.getLogger("matrix_compute_node")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class WorkerDaemon:
|
|
29
|
+
"""
|
|
30
|
+
Worker daemon orchestrating hardware attestation, Nostr relay announcement,
|
|
31
|
+
and P2P gRPC compute servicing with strict L402 pre-allocation protection.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
config: NodeConfig,
|
|
37
|
+
nostr_client: Optional[NostrRelayClient] = None,
|
|
38
|
+
settlement_client: Optional[Any] = None,
|
|
39
|
+
) -> None:
|
|
40
|
+
self.config = config
|
|
41
|
+
self.stop_event = threading.Event()
|
|
42
|
+
|
|
43
|
+
# 1. Initialize Cryptographic Signer
|
|
44
|
+
self.signer = WorkerSigner(privkey_hex=config.worker_privkey_hex)
|
|
45
|
+
|
|
46
|
+
# 2. Hardware Attestation Engine
|
|
47
|
+
self.attestation_engine = HardwareAttestationEngine(
|
|
48
|
+
signer=self.signer,
|
|
49
|
+
device_index=config.gpu_device_index,
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
# 3. L402 Pre-Allocation Security Gate
|
|
53
|
+
self.gate = L402PreAllocationGate(
|
|
54
|
+
settlement_client_or_url=settlement_client or config.settlement_api_url,
|
|
55
|
+
verification_key=config.settlement_verification_key,
|
|
56
|
+
min_price_rate=config.sats_per_gflop,
|
|
57
|
+
min_task_sats=config.min_task_sats,
|
|
58
|
+
cache_ttl_seconds=getattr(config, "l402_cache_ttl_seconds", 300.0),
|
|
59
|
+
max_cache_size=getattr(config, "l402_max_cache_size", 10000),
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
# 4. Compute Engine
|
|
63
|
+
self.engine = ComputeEngine(prefer_gpu=True)
|
|
64
|
+
|
|
65
|
+
# 5. P2P gRPC Servicer
|
|
66
|
+
self.servicer = MatrixComputeServicer(
|
|
67
|
+
signer=self.signer,
|
|
68
|
+
gate=self.gate,
|
|
69
|
+
engine=self.engine,
|
|
70
|
+
config=self.config,
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
# 6. Reverse Tunnel Client (for Outbound NAT Traversal)
|
|
74
|
+
self.tunnel_client = None
|
|
75
|
+
is_nat_mode = bool(config.enable_outbound_nat or config.nat_mode == "reverse_tunnel")
|
|
76
|
+
if is_nat_mode and config.relay_grpc_endpoint:
|
|
77
|
+
from src.p2p.tunnel_client import ReverseTunnelClient
|
|
78
|
+
self.tunnel_client = ReverseTunnelClient(
|
|
79
|
+
relay_endpoint=config.relay_grpc_endpoint,
|
|
80
|
+
signer=self.signer,
|
|
81
|
+
servicer=self.servicer,
|
|
82
|
+
heartbeat_interval_seconds=getattr(config, "tunnel_heartbeat_interval_seconds", 30.0),
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
# 7. gRPC Inbound Server (only bound if p2p_port > 0 or not outbound NAT)
|
|
86
|
+
self.server = None
|
|
87
|
+
self.bound_port = 0
|
|
88
|
+
if not (is_nat_mode and config.p2p_port == 0):
|
|
89
|
+
self.server = grpc.server(
|
|
90
|
+
futures.ThreadPoolExecutor(max_workers=config.grpc_max_workers),
|
|
91
|
+
options=[
|
|
92
|
+
("grpc.max_receive_message_length", config.grpc_max_message_bytes),
|
|
93
|
+
("grpc.max_send_message_length", config.grpc_max_message_bytes),
|
|
94
|
+
("grpc.keepalive_permit_without_calls", 1),
|
|
95
|
+
("grpc.http2.min_ping_interval_without_data_ms", 5000),
|
|
96
|
+
("grpc.http2.max_ping_strikes", 0),
|
|
97
|
+
],
|
|
98
|
+
)
|
|
99
|
+
pb2_grpc.add_MatrixComputeServiceServicer_to_server(self.servicer, self.server)
|
|
100
|
+
bind_address = f"{config.p2p_host}:{config.p2p_port}"
|
|
101
|
+
self.bound_port = self.server.add_insecure_port(bind_address)
|
|
102
|
+
|
|
103
|
+
# 8. Nostr Relay Client
|
|
104
|
+
self.nostr_client = nostr_client or NostrRelayClient(
|
|
105
|
+
relays=config.nostr_relays,
|
|
106
|
+
signer=self.signer,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
def start(self) -> None:
|
|
110
|
+
"""Starts daemon services and registers announcement."""
|
|
111
|
+
logger.info("Starting matrix-compute-node daemon...")
|
|
112
|
+
logger.info(f"Worker Public Key (npub): {self.signer.get_npub()}")
|
|
113
|
+
logger.info(f"Worker Public Key (hex): {self.signer.get_pubkey_hex()}")
|
|
114
|
+
|
|
115
|
+
# 1. Attest hardware
|
|
116
|
+
self.hardware_specs = self.attestation_engine.detect_hardware()
|
|
117
|
+
logger.info(
|
|
118
|
+
f"Detected Hardware: {self.hardware_specs.gpu_model} "
|
|
119
|
+
f"({self.hardware_specs.vram_mb} MB VRAM, {self.hardware_specs.peak_fp32_tflops} TFLOPS)"
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
# 2. Start gRPC server and/or reverse tunnel
|
|
123
|
+
if self.server is not None:
|
|
124
|
+
self.server.start()
|
|
125
|
+
logger.info(f"P2P gRPC server listening on {self.config.p2p_host}:{self.bound_port or self.config.p2p_port}")
|
|
126
|
+
|
|
127
|
+
if self.tunnel_client is not None:
|
|
128
|
+
self.tunnel_client.start()
|
|
129
|
+
logger.info(f"Reverse tunnel client started towards {self.config.relay_grpc_endpoint}")
|
|
130
|
+
|
|
131
|
+
# 3. Announce to Nostr relays
|
|
132
|
+
self._announce_once()
|
|
133
|
+
|
|
134
|
+
def _announce_once(self) -> bool:
|
|
135
|
+
"""Builds and publishes a fresh Kind 30000 announcement event."""
|
|
136
|
+
try:
|
|
137
|
+
self.nostr_client.connect()
|
|
138
|
+
pricing_specs = PricingSpecs(
|
|
139
|
+
sats_per_gflop=self.config.sats_per_gflop,
|
|
140
|
+
min_task_sats=self.config.min_task_sats,
|
|
141
|
+
)
|
|
142
|
+
bond_status = BondStatus(
|
|
143
|
+
staked_amount_sats=100000,
|
|
144
|
+
bond_txid="genesis_bond_txid_001",
|
|
145
|
+
)
|
|
146
|
+
multiaddr = self.config.resolve_advertised_multiaddr()
|
|
147
|
+
lightning_pubkey = self.config.lightning_routing_pubkey or self.signer.get_pubkey_hex()
|
|
148
|
+
|
|
149
|
+
event = self.nostr_client.build_announce_event(
|
|
150
|
+
signer=self.signer,
|
|
151
|
+
hardware_specs=self.hardware_specs,
|
|
152
|
+
pricing_specs=pricing_specs,
|
|
153
|
+
bond_status=bond_status,
|
|
154
|
+
p2p_multiaddr=multiaddr,
|
|
155
|
+
lightning_pubkey=lightning_pubkey,
|
|
156
|
+
d_tag=self.config.d_tag,
|
|
157
|
+
nat_mode=self.config.nat_mode,
|
|
158
|
+
relay_endpoint=self.config.relay_grpc_endpoint,
|
|
159
|
+
)
|
|
160
|
+
ok = self.nostr_client.publish_announce(event)
|
|
161
|
+
if ok:
|
|
162
|
+
logger.info(
|
|
163
|
+
f"Worker availability announced on Nostr relays (Kind 30000) "
|
|
164
|
+
f"with multiaddr={multiaddr}, lightning_pubkey={lightning_pubkey[:16]}..."
|
|
165
|
+
)
|
|
166
|
+
return ok
|
|
167
|
+
except Exception as ex:
|
|
168
|
+
logger.warning(f"Nostr announcement skipped/deferred: {ex}")
|
|
169
|
+
return False
|
|
170
|
+
|
|
171
|
+
def stop(self, grace: float = 2.0) -> None:
|
|
172
|
+
"""Stops the daemon cleanly."""
|
|
173
|
+
logger.info("Shutting down matrix-compute-node...")
|
|
174
|
+
self.stop_event.set()
|
|
175
|
+
self.nostr_client.disconnect()
|
|
176
|
+
if self.tunnel_client is not None:
|
|
177
|
+
self.tunnel_client.stop()
|
|
178
|
+
if self.server is not None:
|
|
179
|
+
self.server.stop(grace=grace)
|
|
180
|
+
logger.info("Daemon stopped successfully")
|
|
181
|
+
|
|
182
|
+
def run_forever(self) -> None:
|
|
183
|
+
"""Runs the daemon blocking until SIGINT/SIGTERM."""
|
|
184
|
+
self.start()
|
|
185
|
+
last_announce = time.time()
|
|
186
|
+
try:
|
|
187
|
+
while not self.stop_event.is_set():
|
|
188
|
+
time.sleep(0.5)
|
|
189
|
+
if time.time() - last_announce >= 30.0:
|
|
190
|
+
self._announce_once()
|
|
191
|
+
last_announce = time.time()
|
|
192
|
+
except KeyboardInterrupt:
|
|
193
|
+
pass
|
|
194
|
+
finally:
|
|
195
|
+
self.stop()
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def main(args: Optional[Sequence[str]] = None) -> int:
|
|
199
|
+
"""CLI entrypoint."""
|
|
200
|
+
config = NodeConfig.load(args)
|
|
201
|
+
logging.basicConfig(
|
|
202
|
+
level=getattr(logging, config.log_level.upper(), logging.INFO),
|
|
203
|
+
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
daemon = WorkerDaemon(config)
|
|
207
|
+
|
|
208
|
+
def _signal_handler(signum: int, frame: Any) -> None:
|
|
209
|
+
logger.info(f"Signal {signum} received, stopping...")
|
|
210
|
+
daemon.stop()
|
|
211
|
+
sys.exit(0)
|
|
212
|
+
|
|
213
|
+
try:
|
|
214
|
+
signal.signal(signal.SIGINT, _signal_handler)
|
|
215
|
+
signal.signal(signal.SIGTERM, _signal_handler)
|
|
216
|
+
except (ValueError, AttributeError):
|
|
217
|
+
pass
|
|
218
|
+
|
|
219
|
+
daemon.run_forever()
|
|
220
|
+
return 0
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
if __name__ == "__main__":
|
|
224
|
+
sys.exit(main())
|