"""ComputeWire: the subsystem that routes compute/1.0 envelopes to ComputeSession.
Separate so the subsystem wiring stays a thin adapter over the session and message modules.
"""
from __future__ import annotations
from typing import Any, Mapping, Optional
from fleetmesh_node.config import ComputeConfig
from fleetmesh_node.compartment import Command, Handler, Need, Outbound, WireProtocol
from fleetmesh_node import channels as ch_mod
from fleetmesh_node import metrics as mx_mod
from fleetmesh_node.compartment import stores as st
from fleetmesh_node.planes.services.compute import executors as compute_executors
from .messages import PIURI, PIURI_V11
from .session import ComputeSession
from .types import (
ComputeDisputeRecord,
ComputeSessionSummary,
ComputeWireStateReport,
)
# -------------------------------------------------------------------------
# Compute Wire Compartment (Transport Compartment)
# -------------------------------------------------------------------------
[docs]class ComputeWire(WireProtocol):
"""The compute/1.0 and compute/1.1 wire transport compartment for meshnode."""
optional: bool = True
enabled_by: Optional[str] = 'compute'
name: str = 'compute_wire'
reads: tuple[str, ...] = ('compute', 'relays', 'transport')
config_cls = ComputeConfig
piuris: tuple[str, ...] = (PIURI, PIURI_V11)
OUTBOUND = (
Outbound('quote', reply=('accept', 'cancel'), doc='quote price and terms for compute task'),
Outbound('accept', reply=('sealed', 'cancel'), doc='accept quote and escrow payment'),
Outbound('sealed', reply=('settled', 'refute'), doc='deliver encrypted execution payload'),
Outbound('settled', doc='reveal payment preimage upon verified settlement'),
Outbound('refute', doc='refute execution proof or result divergence'),
Outbound('stream', doc='stream output tokens or chunk deltas'),
Outbound('progress', doc='stream execution progress heartbeat'),
Outbound('cancel', doc='cancel compute task execution'),
)
COMMANDS = (
Command(
key='sessions',
name='Compute Sessions',
description='list active bilateral compute sessions',
tags=('compute', 'sessions'),
permission='compute_wire/sessions',
run='list_sessions',
reply='dict',
),
Command(
key='disputes',
name='Compute Disputes',
description='list compute refutations and dispute reports filed',
tags=('compute', 'disputes'),
permission='compute_wire/disputes',
run='list_disputes',
reply='list',
),
)
HANDLERS = (
Handler('quote', run='handle_quote', doc='receive compute quotation'),
Handler('accept', run='handle_accept', doc='receive compute job acceptance'),
Handler('sealed', run='handle_sealed', doc='receive sealed compute payload'),
Handler('settled', run='handle_settled', doc='receive compute settlement preimage'),
Handler('refute', run='handle_refute', writes=('disputes',), doc='receive compute refutation dispute'),
Handler('stream', run='handle_stream', doc='receive real-time streaming output chunk'),
Handler('progress', run='handle_progress', doc='receive long-running progress heartbeat'),
Handler('cancel', run='handle_cancel', doc='receive compute job cancellation'),
)
# What it keeps. Each refute a peer sends is kept whole, and any peer
# can send one; this was a list that grew by one per refute for the life
# of the process. The newest DISPUTES_KEPT now, and `disputes_filed`
# still counts every one (the ring's inserts).
DISPUTES_KEPT = 256
STORES = (
st.Store(key='disputes', description='refutes received, as {peer, refute}, newest last',
kind=st.RING, shape=dict, bound=DISPUTES_KEPT),
)
CHANNELS = (
ch_mod.Channel('refute_received', 'A compute refutation dispute was received from a peer',
level=ch_mod.TROUBLE, say='compute dispute from {peer} for receipt {receipt}',
tags=('compute', 'wire', 'dispute')),
ch_mod.Channel('quote_received', 'A compute quotation was received from an executor peer',
level=ch_mod.NOTE, say='compute quote from {peer} price {price} msat',
tags=('compute', 'wire')),
)
METRICS = (
mx_mod.Metric(name='compute_wire_disputes', description='Compute disputes and refutations received.',
kind=mx_mod.COUNTER, counts='refute_received', tags=('compute', 'wire', 'disputes')),
mx_mod.Metric(name='compute_wire_quotes', description='Compute quotes received from peers.',
kind=mx_mod.COUNTER, counts='quote_received', tags=('compute', 'wire', 'quotes')),
)
NEEDS = (
Need('ks', 'ks'),
)
[docs] def __init__(self, node: Any = None, ks: Any = None) -> None:
super().__init__(node)
self.ks: Any = ks if ks is not None else getattr(node, 'ks', None)
self.sessions: dict[str, ComputeSession] = {} # not a store: never written - nothing constructs a ComputeSession yet; the sessions command reads it
self.disputes: st.Ring[ComputeDisputeRecord] # built and bound by the base (see STORES)
self.registry: compute_executors.ExecutorRegistry = compute_executors.DEFAULT_REGISTRY
[docs] def pinned_vk_digests(self) -> list[str]:
"""Operator-pinned Groth16 verification-key digests (cfg compute.zk_pinned_vks)."""
return list(ComputeConfig.from_dict(self.config('compute') or {}).zk_pinned_vks)
[docs] def state(self) -> ComputeWireStateReport:
return {
'active_sessions': len(self.sessions),
'disputes_filed': self.disputes.inserts,
'refusals': self.refusals.total(),
}
[docs] def list_sessions(self) -> dict[str, ComputeSessionSummary]:
return {
sid: {'role': s.role, 'peer': s.peer_pk, 'state': s.state}
for sid, s in self.sessions.items()
}
[docs] def list_disputes(self) -> list[ComputeDisputeRecord]:
return list(self.disputes)
def _my_sk(self) -> str:
ks = self.ks
if ks is not None and getattr(ks, 'node_sk', None):
return str(ks.node_sk)
return '11' * 32
def _peer_pk(self, sender: str) -> str:
s = str(sender or '').strip()
if s.startswith('did:nostr:'):
s = s[len('did:nostr:'):]
if len(s) == 64:
return s.lower()
return '22' * 32
[docs] def open_buyer_session(
self,
ask_id: str,
peer_pk: str,
job_payload: Optional[Mapping[str, Any]] = None,
) -> ComputeSession:
sess = ComputeSession(
role='buyer',
my_sk=self._my_sk(),
peer_pk=self._peer_pk(peer_pk),
ask_id=ask_id,
job_payload=job_payload,
)
self.sessions[ask_id] = sess
return sess
[docs] def open_seller_session(
self,
ask_id: str,
peer_pk: str,
) -> ComputeSession:
sess = ComputeSession(
role='seller',
my_sk=self._my_sk(),
peer_pk=self._peer_pk(peer_pk),
ask_id=ask_id,
)
self.sessions[ask_id] = sess
return sess
[docs] def handle_quote(self, inbound: Any, sender: str) -> tuple[Any, ...]:
msg = getattr(inbound, 'message', inbound)
body = msg.get('body') or {}
price = body.get('price_msat', 0)
thid = str(msg.get('thid') or body.get('ask') or msg.get('id') or '')
if thid:
sess = self.sessions.get(thid)
if sess is None:
sess = self.open_buyer_session(thid, sender)
sess.quote_msg = dict(msg)
sess.payment_hash = body.get('payment_hash')
sess.state = 'QUOTED'
self.say('quote_received', peer=sender, price=price)
return ('quote_received', sender, price)
[docs] def handle_accept(self, inbound: Any, sender: str) -> tuple[Any, ...]:
msg = getattr(inbound, 'message', inbound)
thid = msg.get('thid') or msg.get('id')
if thid:
sid = str(thid)
sess = self.sessions.get(sid)
if sess is None:
sess = self.open_seller_session(sid, sender)
sess.accept_msg = dict(msg)
sess.state = 'ACCEPTED'
return ('accept_received', sender, thid)
[docs] def handle_sealed(self, inbound: Any, sender: str) -> tuple[Any, ...]:
msg = getattr(inbound, 'message', inbound)
body = msg.get('body') or {}
thid = str(msg.get('thid') or msg.get('id') or '')
if thid:
sess = self.sessions.get(thid)
if sess is None:
sess = self.open_buyer_session(thid, sender)
sess.sealed_msg = dict(msg)
sess.payment_hash = body.get('payment_hash')
sess.result_hash = body.get('result')
sess.state = 'SEALED'
return ('sealed_received', sender, body.get('payment_hash'))
[docs] def handle_settled(self, inbound: Any, sender: str) -> tuple[Any, ...]:
msg = getattr(inbound, 'message', inbound)
thid = msg.get('thid') or msg.get('id')
if thid and str(thid) in self.sessions:
self.sessions[str(thid)].settled_msg = dict(msg)
self.sessions[str(thid)].state = 'SETTLED'
return ('settled_received', sender, thid)
[docs] def handle_refute(self, inbound: Any, sender: str) -> tuple[Any, ...]:
msg = getattr(inbound, 'message', inbound)
self.disputes.append({'peer': sender, 'refute': msg})
receipt = (msg.get('body') or {}).get('job_receipt', '')
thid = msg.get('thid') or msg.get('id')
if thid and str(thid) in self.sessions:
self.sessions[str(thid)].state = 'REFUTED'
self.say('refute_received', peer=sender, receipt=str(receipt)[:16])
return ('refute_received', sender, receipt)
[docs] def handle_stream(self, inbound: Any, sender: str) -> tuple[Any, ...]:
msg = getattr(inbound, 'message', inbound)
thid = msg.get('thid') or msg.get('id')
return ('stream_received', sender, thid, (msg.get('body') or {}).get('seq'))
[docs] def handle_progress(self, inbound: Any, sender: str) -> tuple[Any, ...]:
msg = getattr(inbound, 'message', inbound)
thid = msg.get('thid') or msg.get('id')
if thid and str(thid) in self.sessions:
self.sessions[str(thid)].progress_history.append(dict(msg))
return ('progress_received', sender, thid, (msg.get('body') or {}).get('progress'))
[docs] def handle_cancel(self, inbound: Any, sender: str) -> tuple[Any, ...]:
msg = getattr(inbound, 'message', inbound)
thid = msg.get('thid') or msg.get('id')
if thid and str(thid) in self.sessions:
self.sessions[str(thid)].state = 'CANCELLED'
return ('cancel_received', sender, thid, (msg.get('body') or {}).get('reason'))