-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblocks.py
More file actions
845 lines (658 loc) · 28.6 KB
/
blocks.py
File metadata and controls
845 lines (658 loc) · 28.6 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
from __future__ import annotations
import base64
import json
from dataclasses import dataclass
from typing import Any, Dict, Tuple
from registry import BLOCKS
from blocknet_client import BlockNetClient
from block import BaseBlock
# ---------------- existing blocks ----------------
@dataclass
class BlockNetPutBlock(BaseBlock):
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38887"))
token = str(params.get("token", ""))
key = str(params.get("key", ""))
mime = str(params.get("mime", "application/octet-stream"))
data = payload if isinstance(payload, (bytes, bytearray)) else str(payload).encode("utf-8", errors="replace")
cli = BlockNetClient(relay=relay, token=token)
j = cli.put(bytes(data), key=key, mime=mime)
ref = j.get("ref", "")
return ref, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_put", BlockNetPutBlock)
@dataclass
class BlockNetGetBlock(BaseBlock):
"""
Params:
relay, token
mode: "auto" | "ref" | "key"
as: "auto" | "text" | "bytes"
Payload: ref string OR key string (depending on mode)
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38887"))
token = str(params.get("token", ""))
mode = str(params.get("mode", "auto")).lower()
as_mode = str(params.get("as", "auto")).lower()
s = str(payload or "").strip()
if not s:
return "", {"ok": False, "error": "empty payload"}
cli = BlockNetClient(relay=relay, token=token)
if mode == "ref" or (mode == "auto" and s.startswith("obj_")):
status, hdrs, data = cli.get_ref(s)
used = {"mode": "ref", "ref": s}
else:
status, hdrs, data = cli.get_key(s)
used = {"mode": "key", "key": s}
ok = (status == 200)
if as_mode == "bytes":
out: Any = data
elif as_mode == "text":
out = data.decode("utf-8", errors="replace")
else:
ctype = (hdrs.get("Content-Type") or hdrs.get("content-type") or "").lower()
if ctype.startswith("text/") or "json" in ctype:
out = data.decode("utf-8", errors="replace")
else:
out = data
meta = {"ok": ok, "status": status, "headers": hdrs, **used}
if not ok:
try:
meta["error_body"] = data.decode("utf-8", errors="replace")
except Exception:
pass
return out, meta
BLOCKS.register("blocknet_get", BlockNetGetBlock)
@dataclass
class BlockNetStatsBlock(BaseBlock):
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38887"))
token = str(params.get("token", ""))
cli = BlockNetClient(relay=relay, token=token)
j = cli.stats()
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_stats", BlockNetStatsBlock)
@dataclass
class BlockNetHeartbeatBlock(BaseBlock):
"""
Params:
relay, token, id
Payload:
optional json string/dict for stats
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38887"))
token = str(params.get("token", ""))
cid = str(params.get("id", "client1"))
stats: Dict[str, Any] = {}
if isinstance(payload, dict):
stats = payload
elif payload:
try:
stats = json.loads(str(payload))
except Exception:
stats = {"payload": str(payload)}
cli = BlockNetClient(relay=relay, token=token)
j = cli.heartbeat(cid, stats)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_heartbeat", BlockNetHeartbeatBlock)
# ---------------- NEW: API blocks (core/media/randomx/web/p2pool) ----------------
def _api_prefix(params: Dict[str, Any]) -> str:
pfx = str(params.get("api_prefix", "/v1") or "/v1").strip()
if not pfx.startswith("/"):
pfx = "/" + pfx
return pfx.rstrip("/")
@dataclass
class BlockNetPingBlock(BaseBlock):
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_ping(prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_ping", BlockNetPingBlock)
@dataclass
class BlockNetTextToVecBlock(BaseBlock):
"""
Payload: text (string)
Params: relay, token, api_prefix, dim, normalize, output
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
text = "" if payload is None else str(payload)
dim = int(params.get("dim", 1024))
normalize = bool(params.get("normalize", True))
output = str(params.get("output", "b64f32"))
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_texttovec(text, dim=dim, normalize=normalize, output=output, prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_texttovec", BlockNetTextToVecBlock)
def _b64_bytes(data: bytes) -> str:
return base64.b64encode(data).decode("ascii")
@dataclass
class BlockNetImageToVecBlock(BaseBlock):
"""
Payload:
- bytes: raw image bytes
- str: treated as already-base64 (unless params.base64=false, then it becomes utf8 bytes)
- dict: sent as-is (advanced)
Params:
relay, token, api_prefix, dim, normalize, output, base64 (default True)
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
dim = int(params.get("dim", 1024))
normalize = bool(params.get("normalize", True))
output = str(params.get("output", "b64f32"))
treat_str_as_b64 = bool(params.get("base64", True))
if isinstance(payload, dict):
body = dict(payload)
else:
if isinstance(payload, (bytes, bytearray)):
b64 = _b64_bytes(bytes(payload))
else:
s = "" if payload is None else str(payload)
b64 = s if treat_str_as_b64 else _b64_bytes(s.encode("utf-8", errors="replace"))
body = {"image_b64": b64, "dim": dim, "normalize": normalize, "output": output}
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_imagetovec(body, prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_imagetovec", BlockNetImageToVecBlock)
@dataclass
class BlockNetVideoToVecBlock(BaseBlock):
"""
Same conventions as imagetovec, but uses video_b64.
Params additionally: max_frames (optional)
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
dim = int(params.get("dim", 1024))
normalize = bool(params.get("normalize", True))
output = str(params.get("output", "b64f32"))
max_frames = int(params.get("max_frames", 256))
treat_str_as_b64 = bool(params.get("base64", True))
if isinstance(payload, dict):
body = dict(payload)
else:
if isinstance(payload, (bytes, bytearray)):
b64 = _b64_bytes(bytes(payload))
else:
s = "" if payload is None else str(payload)
b64 = s if treat_str_as_b64 else _b64_bytes(s.encode("utf-8", errors="replace"))
body = {"video_b64": b64, "dim": dim, "normalize": normalize, "output": output, "max_frames": max_frames}
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_videotovec(body, prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_videotovec", BlockNetVideoToVecBlock)
@dataclass
class BlockNetVectorTextBlock(BaseBlock):
"""
POST /vectortext
Payload:
- dict: full body for vectortext (recommended)
- str: becomes prompt; params.payload supplies payload string
Params:
relay, token, api_prefix
payload, key, lexicon_key, context_key
idf_key, tokens_key
max_tokens, topk, seed
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
if isinstance(payload, dict):
body = dict(payload)
else:
# simple mode: payload is prompt, plus params
body = {
"prompt": "" if payload is None else str(payload),
"payload": str(params.get("payload", "")),
}
# pass-through first-class routing keys
for k in ("key", "lexicon_key", "context_key", "idf_key", "tokens_key"):
if k in params and k not in body:
body[k] = params.get(k)
# optional inline lexicon/context blocks (can be json/dict/str)
if "lexicon" in params and "lexicon" not in body:
body["lexicon"] = params.get("lexicon")
if "context" in params and "context" not in body:
body["context"] = params.get("context")
# generation controls
if "max_tokens" in params and "max_tokens" not in body:
body["max_tokens"] = int(params.get("max_tokens"))
if "topk" in params and "topk" not in body:
body["topk"] = int(params.get("topk"))
if "seed" in params and "seed" not in body:
body["seed"] = params.get("seed")
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_vectortext(body, prefix=pfx)
# best pipeline output is the generated text if present
out = j.get("generated", j)
return out, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_vectortext", BlockNetVectorTextBlock)
@dataclass
class BlockNetRandomXStatusBlock(BaseBlock):
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_randomx_status(prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_randomx_status", BlockNetRandomXStatusBlock)
@dataclass
class BlockNetRandomXHashBlock(BaseBlock):
"""
Payload:
- bytes: becomes data_b64
- str: becomes data_hex (if looks hex) else data_b64(utf8)
- dict: sent as-is
Params:
relay, token, api_prefix, seed_hex
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
seed_hex = str(params.get("seed_hex", "")).strip()
if not seed_hex:
return "", {"ok": False, "error": "missing seed_hex param"}
if isinstance(payload, dict):
body = dict(payload)
body.setdefault("seed_hex", seed_hex)
else:
body: Dict[str, Any] = {"seed_hex": seed_hex}
if isinstance(payload, (bytes, bytearray)):
body["data_b64"] = _b64_bytes(bytes(payload))
else:
s = "" if payload is None else str(payload).strip()
is_hex = all(c in "0123456789abcdefABCDEF" for c in s) and (len(s) % 2 == 0) and len(s) > 0
if is_hex:
body["data_hex"] = s
else:
body["data_b64"] = _b64_bytes(s.encode("utf-8", errors="replace"))
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_randomx_hash(body, prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_randomx_hash", BlockNetRandomXHashBlock)
@dataclass
class BlockNetRandomXHashBatchBlock(BaseBlock):
"""
POST /randomx/hash_batch
Params:
relay, token, api_prefix, seed_hex
max_items (optional): truncate items list (default 0 = no truncate)
Payload:
- dict: treated as full request body (seed_hex filled from params if missing)
- list: treated as items[]
- str: JSON list or JSON object
- bytes: treated as ONE item (data_b64)
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
seed_hex = str(params.get("seed_hex", "")).strip()
payload_seed = ""
if isinstance(payload, dict):
payload_seed = str(payload.get("seed_hex", "")).strip()
if not seed_hex and not payload_seed:
return "", {"ok": False, "error": "missing seed_hex param"}
# Build request body
if isinstance(payload, dict):
body: Dict[str, Any] = dict(payload)
if seed_hex:
body.setdefault("seed_hex", seed_hex)
elif isinstance(payload, list):
body = {"seed_hex": seed_hex, "items": payload}
elif isinstance(payload, (bytes, bytearray)):
body = {"seed_hex": seed_hex, "items": [{"data_b64": _b64_bytes(bytes(payload))}]}
else:
s = "" if payload is None else str(payload).strip()
if not s:
return "", {"ok": False, "error": "empty payload (expected items JSON)"}
try:
parsed = json.loads(s)
except Exception:
return "", {"ok": False, "error": "payload must be JSON list or object"}
if isinstance(parsed, dict):
body = dict(parsed)
if seed_hex:
body.setdefault("seed_hex", seed_hex)
elif isinstance(parsed, list):
body = {"seed_hex": seed_hex, "items": parsed}
else:
return "", {"ok": False, "error": "payload JSON must be an object or array"}
if "items" not in body or not isinstance(body["items"], list):
return "", {"ok": False, "error": "missing items[] (must be a JSON array)"}
# Optional truncate
try:
max_items = int(params.get("max_items", 0))
except Exception:
max_items = 0
if max_items > 0:
body["items"] = body["items"][:max_items]
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_randomx_hash_batch(body, prefix=pfx)
# Pipeline-friendly output
out = j.get("results", j)
return out, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_randomx_hash_batch", BlockNetRandomXHashBatchBlock)
@dataclass
class BlockNetRandomXScanBlock(BaseBlock):
"""
POST /randomx/scan
Payload:
- dict: full request body (recommended)
- bytes: treated as blob (blob_b64)
- str: treated as blob_hex if hex-looking else blob_b64(utf8)
Params:
relay, token, api_prefix
seed_hex (required unless in payload dict)
nonce_offset (default 39)
start_nonce (default 0)
iters (default 200000)
target64 (required unless in payload dict)
max_results (default 4)
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
# If caller gave dict, pass through (fill missing from params)
if isinstance(payload, dict):
body: Dict[str, Any] = dict(payload)
else:
body = {}
# seed_hex
if "seed_hex" not in body:
seed_hex = str(params.get("seed_hex", "")).strip()
if not seed_hex:
return "", {"ok": False, "error": "missing seed_hex (param or payload dict)"}
body["seed_hex"] = seed_hex
# target64
if "target64" not in body:
if "target64" in params:
body["target64"] = int(params.get("target64"))
else:
return "", {"ok": False, "error": "missing target64 (param or payload dict)"}
# convenience blob fill if not provided
if "blob_hex" not in body and "blob_b64" not in body:
if isinstance(payload, (bytes, bytearray)):
body["blob_b64"] = _b64_bytes(bytes(payload))
elif payload is not None and not isinstance(payload, dict):
s = str(payload).strip()
is_hex = all(c in "0123456789abcdefABCDEF" for c in s) and (len(s) % 2 == 0) and len(s) > 0
if is_hex:
body["blob_hex"] = s
else:
body["blob_b64"] = _b64_bytes(s.encode("utf-8", errors="replace"))
# defaults / optional knobs
body.setdefault("nonce_offset", int(params.get("nonce_offset", 39)))
body.setdefault("start_nonce", int(params.get("start_nonce", 0)))
body.setdefault("iters", int(params.get("iters", 200000)))
body.setdefault("max_results", int(params.get("max_results", 4)))
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_randomx_scan(body, prefix=pfx)
# pipeline-friendly output: found list if present
out = j.get("found", j)
return out, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_randomx_scan", BlockNetRandomXScanBlock)
@dataclass
class BlockNetWebFetchBlock(BaseBlock):
"""
Payload:
- str: url
- dict: request body for /web/fetch
Params:
relay, token, api_prefix, mode, include_js, max_bytes, max_scripts
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
if isinstance(payload, dict):
body = dict(payload)
else:
body = {"url": str(payload or "")}
if not body.get("url"):
return "", {"ok": False, "error": "missing url"}
for k in ("mode", "include_js", "max_bytes", "max_scripts"):
if k in params and k not in body:
body[k] = params.get(k)
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_web_fetch(body, prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_web_fetch", BlockNetWebFetchBlock)
@dataclass
class BlockNetWebJsBlock(BaseBlock):
"""
Payload:
- str: url
- dict: request body for /web/js
Params:
relay, token, api_prefix, fetch_bodies, max_scripts
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
if isinstance(payload, dict):
body = dict(payload)
else:
body = {"url": str(payload or "")}
if not body.get("url"):
return "", {"ok": False, "error": "missing url"}
for k in ("fetch_bodies", "max_scripts"):
if k in params and k not in body:
body[k] = params.get(k)
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_web_js(body, prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_web_js", BlockNetWebJsBlock)
@dataclass
class BlockNetWebLinksBlock(BaseBlock):
"""
POST /web/links
Payload:
- str: url
- dict: request body for /web/links
Params:
relay, token, api_prefix
same_origin (bool) # optional
external_only (bool) # optional
max_links (int) # optional
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
if isinstance(payload, dict):
body = dict(payload)
else:
body = {"url": str(payload or "")}
if not body.get("url"):
return "", {"ok": False, "error": "missing url"}
for k in ("same_origin", "external_only", "max_links"):
if k in params and k not in body:
body[k] = params.get(k)
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_web_links(body, prefix=pfx)
# nice pipeline output if server returns a list
out = j.get("links", j.get("urls", j))
return out, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_web_links", BlockNetWebLinksBlock)
@dataclass
class BlockNetWebRssFindBlock(BaseBlock):
"""
POST /web/rss_find
Payload:
- str: url
- dict: request body for /web/rss_find
Params:
relay, token, api_prefix
max_feeds (int) # optional
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
if isinstance(payload, dict):
body = dict(payload)
else:
body = {"url": str(payload or "")}
if not body.get("url"):
return "", {"ok": False, "error": "missing url"}
if "max_feeds" in params and "max_feeds" not in body:
body["max_feeds"] = params.get("max_feeds")
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_web_rss_find(body, prefix=pfx)
# nice pipeline output if server returns a list
out = j.get("feeds", j.get("urls", j))
return out, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_web_rss_find", BlockNetWebRssFindBlock)
# ---- P2Pool blocks ----
@dataclass
class BlockNetP2PoolOpenBlock(BaseBlock):
"""
Opens a session. Output is session token by default (best for pipelines).
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_p2pool_open(prefix=pfx)
session = j.get("session", "")
return session, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_p2pool_open", BlockNetP2PoolOpenBlock)
@dataclass
class BlockNetP2PoolPollBlock(BaseBlock):
"""
Payload: session token (string) OR dict body
Params: max_msgs
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
max_msgs = int(params.get("max_msgs", 32))
cli = BlockNetClient(relay=relay, token=token)
if isinstance(payload, dict):
session = str(payload.get("session", "") or "")
max_msgs = int(payload.get("max_msgs", max_msgs))
else:
session = str(payload or "")
if not session:
return "", {"ok": False, "error": "missing session"}
j = cli.api_p2pool_poll(session, max_msgs=max_msgs, prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_p2pool_poll", BlockNetP2PoolPollBlock)
@dataclass
class BlockNetP2PoolJobBlock(BaseBlock):
"""
Payload: session token
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
session = str(payload or "").strip()
if not session:
return "", {"ok": False, "error": "missing session"}
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_p2pool_job(session, prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_p2pool_job", BlockNetP2PoolJobBlock)
@dataclass
class BlockNetP2PoolSubmitBlock(BaseBlock):
"""
Payload:
- dict: {session, job_id, nonce, result}
- str: treated as result; requires params.session/job_id/nonce
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
if isinstance(payload, dict):
body = dict(payload)
else:
body = {
"session": str(params.get("session", "")),
"job_id": str(params.get("job_id", "")),
"nonce": str(params.get("nonce", "")),
"result": str(payload or ""),
}
for k in ("session", "job_id", "nonce", "result"):
if not str(body.get(k, "")).strip():
return "", {"ok": False, "error": f"missing {k}"}
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_p2pool_submit(body, prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_p2pool_submit", BlockNetP2PoolSubmitBlock)
@dataclass
class BlockNetP2PoolCloseBlock(BaseBlock):
"""
Payload: session token
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
session = str(payload or "").strip()
if not session:
return "", {"ok": False, "error": "missing session"}
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_p2pool_close(session, prefix=pfx)
return j, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_p2pool_close", BlockNetP2PoolCloseBlock)
@dataclass
class BlockNetP2PoolScanBlock(BaseBlock):
"""
POST /p2pool/scan
Payload:
- str: session token
- dict: full request body {session, start_nonce, iters, max_results, nonce_offset, poll_first}
Params:
relay, token, api_prefix
session (if payload is not session)
start_nonce (default 0)
iters (default 200000)
max_results (default 4)
nonce_offset (default 39)
poll_first (default False)
"""
def execute(self, payload: Any, *, params: Dict[str, Any]) -> Tuple[Any, Dict[str, Any]]:
relay = str(params.get("relay", "127.0.0.1:38888"))
token = str(params.get("token", ""))
pfx = _api_prefix(params)
if isinstance(payload, dict):
body: Dict[str, Any] = dict(payload)
else:
sess = str(payload or "").strip()
if not sess:
sess = str(params.get("session", "")).strip()
body = {"session": sess}
if not str(body.get("session", "")).strip():
return "", {"ok": False, "error": "missing session"}
# optional knobs
body.setdefault("start_nonce", int(params.get("start_nonce", 0)))
body.setdefault("iters", int(params.get("iters", 200000)))
body.setdefault("max_results", int(params.get("max_results", 4)))
body.setdefault("nonce_offset", int(params.get("nonce_offset", 39)))
body.setdefault("poll_first", bool(params.get("poll_first", False)))
cli = BlockNetClient(relay=relay, token=token)
j = cli.api_p2pool_scan(body, prefix=pfx)
# pipeline-friendly output: found list if present
out = j.get("found", j)
return out, {"ok": bool(j.get("ok", False)), "response": j}
BLOCKS.register("blocknet_p2pool_scan", BlockNetP2PoolScanBlock)