interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / nodes / snap.om
| 1 | om2 |
| 2 | note nodes/snap.om: the Radicle node snapshot writer behind the forge /status page and the factory floor. |
| 3 | note nodes-snap write the snapshot once (tests, by hand) |
| 4 | note nodes-snap follow aien-nodes.service: write once, then follow `rad node events` (the node's own |
| 5 | note change stream) and write again after events, at most once per 30 s; no |
| 6 | note clock: with no event nothing runs. Exits 6 when the stream ends (node |
| 7 | note restarted); the service restarts it. |
| 8 | note It reads the node and never writes to it: |
| 9 | note sqlite3 -readonly node.db repo-sync-status: which head of OUR sigrefs each node has synced |
| 10 | note sqlite3 -readonly policies.db seeding scope |
| 11 | note rad node status connected peers |
| 12 | note sqlite3, env and rad stay host programs (argv exec, no shell, empty environment; rad gets HOME |
| 13 | note only); reading their output, the checks and the counting are here. Output, written to a temp file |
| 14 | note and renamed over the old one, one item per line: |
| 15 | note 2 / unix seconds / our node id / connected peers / then per repository "RID scope copies behind reach wait" |
| 16 | note scope 2 all, 1 followed, 0 not seeded here |
| 17 | note copies other nodes that have a record of our signed refs for that repository |
| 18 | note behind of those, how many are not at our current head |
| 19 | note reach seconds our current head took to reach the slowest copy that has it |
| 20 | note wait seconds our current head has been out while some copy still lacks it (0 when none behind) |
| 21 | note A repository with no record of our own head counts copies only (behind, reach and wait 0). |
| 22 | note An input line that is not "letters/digits RID|0-1|0-1|digits|digits" is skipped. Every repository |
| 23 | note the node knows is listed; readers show only their own public allow-list. The file is mode 0600. |
| 24 | note Exit (once): 0 written; 2 an argument other than "follow"; 3 policies query failed; 4 sync query |
| 25 | note failed; 5 write failed. |
| 26 | text sqlb "/usr/bin/sqlite3" |
| 27 | text sqlro "-readonly" |
| 28 | text ndb "/home/drakestapleton/.radicle/node/node.db" |
| 29 | text pdb "/home/drakestapleton/.radicle/node/policies.db" |
| 30 | text q1 "SELECT substr(o.repo,5), o.node = 'z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK', o.head = coalesce(m.head,''), o.timestamp, coalesce(m.timestamp,0) FROM \"repo-sync-status\" o LEFT JOIN \"repo-sync-status\" m ON m.repo = o.repo AND m.node = 'z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK' WHERE o.repo LIKE 'rad:%' ORDER BY o.repo" |
| 31 | text q2 "SELECT substr(id,5), scope FROM seeding WHERE id LIKE 'rad:%' AND policy = 'allow' ORDER BY id" |
| 32 | text envb "/usr/bin/env" |
| 33 | text envh "HOME=/home/drakestapleton" |
| 34 | text radb "/home/drakestapleton/.local/bin/rad" |
| 35 | text rnode "node" |
| 36 | text rstat "status" |
| 37 | text revts "events" |
| 38 | text devnul "/dev/null" |
| 39 | text nid "z6MkfZDue1TbaNFi3ZzcD9uiTeduPLC52ucdFVuLKZwexxhK" |
| 40 | text outp "/home/drakestapleton/.local/state/aien-factory/nodes.txt" |
| 41 | text tmpp "/home/drakestapleton/.local/state/aien-factory/.nodes.txt.new" |
| 42 | global pk uchar 4096 |
| 43 | global pkl ulong 1 |
| 44 | global pkr ulong 1 |
| 45 | global cap uchar 1048576 |
| 46 | global capl ulong 1 |
| 47 | global pol uchar 65536 |
| 48 | global poln ulong 1 |
| 49 | global ob uchar 131072 |
| 50 | global obl ulong 1 |
| 51 | global pfd uchar 8 |
| 52 | global wst uchar 4 |
| 53 | global erfd long 1 |
| 54 | global junk uchar 65536 |
| 55 | global nowms ulong 1 |
| 56 | global l ulong 8 |
| 57 | global g ulong 8 |
| 58 | note ---- packed argument list pk (u16 count, then u16 length + bytes per record), as server/util.om fpk |
| 59 | fn kb to void |
| 60 | put pk 0 0 |
| 61 | put pk 1 0 |
| 62 | put pkl 0 2 |
| 63 | end |
| 64 | fn kc c uchar to void |
| 65 | n = get pkl 0 |
| 66 | if lt n 4095 |
| 67 | put pk n c |
| 68 | put pkl 0 add n 1 |
| 69 | r = get pkr 0 |
| 70 | k = add or ulong get pk r shl ulong get pk add r 1 8 1 |
| 71 | put pk r uchar and k 255 |
| 72 | put pk add r 1 uchar shr k 8 |
| 73 | end |
| 74 | end |
| 75 | fn ka t view uchar * to void |
| 76 | n = get pkl 0 |
| 77 | if ge n 4094 |
| 78 | ret |
| 79 | end |
| 80 | put pkr 0 n |
| 81 | put pk n 0 |
| 82 | put pk add n 1 0 |
| 83 | put pkl 0 add n 2 |
| 84 | c = add or ulong get pk 0 shl ulong get pk 1 8 1 |
| 85 | put pk 0 uchar and c 255 |
| 86 | put pk 1 uchar shr c 8 |
| 87 | for i ulong len t bound 4096 |
| 88 | kc get t i |
| 89 | end |
| 90 | end |
| 91 | note ---- run pk: stdout into cap (at most 1 MiB, 20 s), stdin and stderr /dev/null; exit status or < 0 |
| 92 | fn pfdv i ulong to int |
| 93 | b = mul i 4 |
| 94 | v = or ulong get pfd b shl ulong get pfd add b 1 8 |
| 95 | v = or v shl ulong get pfd add b 2 16 |
| 96 | v = or v shl ulong get pfd add b 3 24 |
| 97 | ret int v |
| 98 | end |
| 99 | fn child wfd int to void |
| 100 | nul = fopen devnul 0 len devnul 2 0 |
| 101 | if lt nul 0 |
| 102 | exit 126 |
| 103 | end |
| 104 | x = dupfd int nul 0 0 |
| 105 | x = dupfd wfd 1 0 |
| 106 | x = dupfd int nul 2 0 |
| 107 | x = execprog pk 0 get pkl 0 |
| 108 | exit 127 |
| 109 | end |
| 110 | fn drain fd int dl long to bool |
| 111 | for it ulong 100000000 |
| 112 | left = sub dl long monons |
| 113 | if le left 0 |
| 114 | ret false |
| 115 | end |
| 116 | w = waitio fd 1 left |
| 117 | if lt w 0 |
| 118 | if ne w -4 |
| 119 | ret false |
| 120 | end |
| 121 | continue |
| 122 | end |
| 123 | if eq w 0 |
| 124 | continue |
| 125 | end |
| 126 | n = get capl 0 |
| 127 | if ge n len cap |
| 128 | ret false |
| 129 | end |
| 130 | r = read fd cap n sub len cap n |
| 131 | if eq r 0 |
| 132 | ret true |
| 133 | end |
| 134 | if lt r 0 |
| 135 | if and ne r -4 ne r -11 |
| 136 | ret false |
| 137 | end |
| 138 | continue |
| 139 | end |
| 140 | put capl 0 add n ulong r |
| 141 | end |
| 142 | ret false |
| 143 | end |
| 144 | note spawn: start pk with its stdout on a new pipe; the read end goes to erfd; the pid or < 0 |
| 145 | fn spawn to long |
| 146 | r = pipefds pfd 0 8 524288 |
| 147 | if lt r 0 |
| 148 | ret -2 |
| 149 | end |
| 150 | rfd = pfdv 0 |
| 151 | wfd = pfdv 1 |
| 152 | pid = fork |
| 153 | if lt pid 0 |
| 154 | x = fclose rfd |
| 155 | x = fclose wfd |
| 156 | ret -2 |
| 157 | end |
| 158 | if eq pid 0 |
| 159 | child wfd |
| 160 | exit 127 |
| 161 | end |
| 162 | x = fclose wfd |
| 163 | put erfd 0 long rfd |
| 164 | ret pid |
| 165 | end |
| 166 | fn run to long |
| 167 | put capl 0 0 |
| 168 | pid = spawn |
| 169 | if lt pid 0 |
| 170 | ret -2 |
| 171 | end |
| 172 | rfd = int get erfd 0 |
| 173 | ok = drain rfd add long monons 20000000000 |
| 174 | x = fclose rfd |
| 175 | if not ok |
| 176 | x = killpid int pid 9 |
| 177 | end |
| 178 | w = waitpid int pid wst 0 4 0 |
| 179 | if or not ok ne w pid |
| 180 | ret -1 |
| 181 | end |
| 182 | s = or ulong get wst 0 shl ulong get wst 1 8 |
| 183 | if ne and s 127 0 |
| 184 | ret -3 |
| 185 | end |
| 186 | ret long and shr s 8 255 |
| 187 | end |
| 188 | note ---- output buffer ob |
| 189 | note obov: 1 once a byte did not fit in ob; wout then refuses, so a cut-off last line is never written |
| 190 | global obov ulong 1 |
| 191 | fn oc c uchar to void |
| 192 | n = get obl 0 |
| 193 | if lt n len ob |
| 194 | put ob n c |
| 195 | put obl 0 add n 1 |
| 196 | else |
| 197 | put obov 0 1 |
| 198 | end |
| 199 | end |
| 200 | fn ot t view uchar * to void |
| 201 | for i ulong len t bound 4096 |
| 202 | oc get t i |
| 203 | end |
| 204 | end |
| 205 | fn onum v ulong to void |
| 206 | d = new uchar 20 0 |
| 207 | n = ulong 0 |
| 208 | x = v |
| 209 | for k ulong 20 |
| 210 | put d n uchar add rem x 10 48 |
| 211 | n = add n 1 |
| 212 | x = div x 10 |
| 213 | if eq x 0 |
| 214 | break |
| 215 | end |
| 216 | end |
| 217 | for k ulong n bound 20 |
| 218 | oc get d sub sub n k 1 |
| 219 | end |
| 220 | end |
| 221 | fn alnum c uchar to bool |
| 222 | if and ge c 48 le c 57 |
| 223 | ret true |
| 224 | end |
| 225 | if and ge c 97 le c 122 |
| 226 | ret true |
| 227 | end |
| 228 | if and ge c 65 le c 90 |
| 229 | ret true |
| 230 | end |
| 231 | ret false |
| 232 | end |
| 233 | note ---- peers: rows of the rad node status table that start "│ z" and carry the connected mark ✓ |
| 234 | fn by3 p ulong a uchar b uchar c uchar to bool |
| 235 | if ne get cap p a |
| 236 | ret false |
| 237 | end |
| 238 | if ne get cap add p 1 b |
| 239 | ret false |
| 240 | end |
| 241 | ret eq get cap add p 2 c |
| 242 | end |
| 243 | fn peerln p ulong e ulong to bool |
| 244 | if or lt sub e p 6 gt sub e p 4000 |
| 245 | ret false |
| 246 | end |
| 247 | if not by3 p 226 148 130 |
| 248 | ret false |
| 249 | end |
| 250 | if not by3 add p 2 130 32 122 |
| 251 | ret false |
| 252 | end |
| 253 | q = add p 5 |
| 254 | while lt add q 2 e bound 4096 |
| 255 | if by3 q 226 156 147 |
| 256 | ret true |
| 257 | end |
| 258 | q = add q 1 |
| 259 | end |
| 260 | ret false |
| 261 | end |
| 262 | fn lnend p ulong n ulong to ulong |
| 263 | e = p |
| 264 | while lt e n bound 1048576 |
| 265 | if eq get cap e 10 |
| 266 | ret e |
| 267 | end |
| 268 | e = add e 1 |
| 269 | end |
| 270 | ret n |
| 271 | end |
| 272 | fn peers to ulong |
| 273 | c = ulong 0 |
| 274 | p = ulong 0 |
| 275 | n = get capl 0 |
| 276 | while lt p n bound 1048576 |
| 277 | e = lnend p n |
| 278 | if peerln p e |
| 279 | c = add c 1 |
| 280 | end |
| 281 | p = add e 1 |
| 282 | end |
| 283 | ret c |
| 284 | end |
| 285 | note ---- one sync line at cap[p, e) into l: 0 RID start, 1 RID length, 2 is-us, 3 at-our-head, |
| 286 | note 4 its time (ms), 5 our time (ms); false when the line is not exactly that form |
| 287 | fn pnum p ulong e ulong k ulong to ulong |
| 288 | v = ulong 0 |
| 289 | q = p |
| 290 | while lt q e bound 21 |
| 291 | if gt sub q p 19 |
| 292 | ret 0 |
| 293 | end |
| 294 | c = get cap q |
| 295 | if eq c 124 |
| 296 | break |
| 297 | end |
| 298 | if or lt c 48 gt c 57 |
| 299 | ret 0 |
| 300 | end |
| 301 | v = add mul v 10 sub ulong c 48 |
| 302 | q = add q 1 |
| 303 | end |
| 304 | if or eq q p gt sub q p 19 |
| 305 | ret 0 |
| 306 | end |
| 307 | put l k v |
| 308 | ret q |
| 309 | end |
| 310 | fn pline p ulong e ulong to bool |
| 311 | q = p |
| 312 | while lt q e bound 70 |
| 313 | if gt sub q p 64 |
| 314 | ret false |
| 315 | end |
| 316 | c = get cap q |
| 317 | if eq c 124 |
| 318 | break |
| 319 | end |
| 320 | if not alnum c |
| 321 | ret false |
| 322 | end |
| 323 | q = add q 1 |
| 324 | end |
| 325 | n = sub q p |
| 326 | if or eq n 0 or gt n 64 ge q e |
| 327 | ret false |
| 328 | end |
| 329 | put l 0 p |
| 330 | put l 1 n |
| 331 | for f ulong 4 |
| 332 | r = pnum add q 1 e add f 2 |
| 333 | if eq r 0 |
| 334 | ret false |
| 335 | end |
| 336 | if and lt f 3 ge r e |
| 337 | ret false |
| 338 | end |
| 339 | if and eq f 3 ne r e |
| 340 | ret false |
| 341 | end |
| 342 | q = r |
| 343 | end |
| 344 | if or gt get l 2 1 gt get l 3 1 |
| 345 | ret false |
| 346 | end |
| 347 | ret true |
| 348 | end |
| 349 | note ---- the repository being counted, in g: 0 RID start, 1 RID length (0 = none yet), 2 copies, |
| 350 | note 3 behind, 4 reach (s), 5 our time (ms, 0 = no record of our head) |
| 351 | fn samerid to bool |
| 352 | if ne get g 1 get l 1 |
| 353 | ret false |
| 354 | end |
| 355 | a = get g 0 |
| 356 | b = get l 0 |
| 357 | for i ulong get g 1 bound 64 |
| 358 | if ne get cap add a i get cap add b i |
| 359 | ret false |
| 360 | end |
| 361 | end |
| 362 | ret true |
| 363 | end |
| 364 | note scope: the seeding line "RID|all" or "RID|followed" for the repository in g, from pol |
| 365 | fn scope to ulong |
| 366 | a = get g 0 |
| 367 | n = get g 1 |
| 368 | p = ulong 0 |
| 369 | m = get poln 0 |
| 370 | while lt p m bound 65536 |
| 371 | if le add add p n 1 m |
| 372 | same = true |
| 373 | for i ulong n bound 64 |
| 374 | if ne get pol add p i get cap add a i |
| 375 | same = false |
| 376 | end |
| 377 | end |
| 378 | if and same eq get pol add p n 124 |
| 379 | c = get pol add add p n 1 |
| 380 | if eq c 97 |
| 381 | ret 2 |
| 382 | end |
| 383 | if eq c 102 |
| 384 | ret 1 |
| 385 | end |
| 386 | ret 0 |
| 387 | end |
| 388 | end |
| 389 | while lt p m bound 65536 |
| 390 | if eq get pol p 10 |
| 391 | break |
| 392 | end |
| 393 | p = add p 1 |
| 394 | end |
| 395 | p = add p 1 |
| 396 | end |
| 397 | ret 0 |
| 398 | end |
| 399 | fn emit to void |
| 400 | n = get g 1 |
| 401 | if eq n 0 |
| 402 | ret |
| 403 | end |
| 404 | a = get g 0 |
| 405 | for i ulong n bound 64 |
| 406 | oc get cap add a i |
| 407 | end |
| 408 | oc 32 |
| 409 | onum scope |
| 410 | oc 32 |
| 411 | onum get g 2 |
| 412 | oc 32 |
| 413 | onum get g 3 |
| 414 | oc 32 |
| 415 | onum get g 4 |
| 416 | oc 32 |
| 417 | w = ulong 0 |
| 418 | o = get g 5 |
| 419 | nw = get nowms 0 |
| 420 | if gt get g 3 0 |
| 421 | if and gt o 0 gt nw o |
| 422 | w = div sub nw o 1000 |
| 423 | end |
| 424 | end |
| 425 | onum w |
| 426 | oc 10 |
| 427 | end |
| 428 | fn gnew to void |
| 429 | put g 0 get l 0 |
| 430 | put g 1 get l 1 |
| 431 | put g 2 0 |
| 432 | put g 3 0 |
| 433 | put g 4 0 |
| 434 | put g 5 get l 5 |
| 435 | end |
| 436 | fn acc to void |
| 437 | if eq get l 2 1 |
| 438 | ret |
| 439 | end |
| 440 | put g 2 add get g 2 1 |
| 441 | o = get g 5 |
| 442 | if eq o 0 |
| 443 | ret |
| 444 | end |
| 445 | if eq get l 3 0 |
| 446 | put g 3 add get g 3 1 |
| 447 | ret |
| 448 | end |
| 449 | ts = get l 4 |
| 450 | if gt ts o |
| 451 | d = div sub ts o 1000 |
| 452 | if gt d get g 4 |
| 453 | put g 4 d |
| 454 | end |
| 455 | end |
| 456 | end |
| 457 | fn rows to void |
| 458 | put g 1 0 |
| 459 | p = ulong 0 |
| 460 | n = get capl 0 |
| 461 | while lt p n bound 1048576 |
| 462 | e = lnend p n |
| 463 | if pline p e |
| 464 | if not samerid |
| 465 | emit |
| 466 | gnew |
| 467 | end |
| 468 | acc |
| 469 | end |
| 470 | p = add e 1 |
| 471 | end |
| 472 | emit |
| 473 | end |
| 474 | fn wout to bool |
| 475 | if ne get obov 0 0 |
| 476 | ret false |
| 477 | end |
| 478 | fd = fopen tmpp 0 len tmpp 524865 384 |
| 479 | if lt fd 0 |
| 480 | ret false |
| 481 | end |
| 482 | p = ulong 0 |
| 483 | n = get obl 0 |
| 484 | while lt p n bound 1000000 |
| 485 | w = write int fd ob p sub n p |
| 486 | if le w 0 |
| 487 | x = fclose int fd |
| 488 | ret false |
| 489 | end |
| 490 | p = add p ulong w |
| 491 | end |
| 492 | x = fdsync int fd |
| 493 | x = fclose int fd |
| 494 | kb |
| 495 | ka tmpp |
| 496 | ka outp |
| 497 | ret eq renames pk 0 get pkl 0 0 0 |
| 498 | end |
| 499 | fn once to int |
| 500 | put nowms 0 ulong div realns 1000000 |
| 501 | kb |
| 502 | ka sqlb |
| 503 | ka sqlro |
| 504 | ka pdb |
| 505 | ka q2 |
| 506 | if ne run 0 |
| 507 | ret 3 |
| 508 | end |
| 509 | n = get capl 0 |
| 510 | if gt n len pol |
| 511 | ret 3 |
| 512 | end |
| 513 | for i ulong n bound 65536 |
| 514 | put pol i get cap i |
| 515 | end |
| 516 | put poln 0 n |
| 517 | kb |
| 518 | ka envb |
| 519 | ka envh |
| 520 | ka radb |
| 521 | ka rnode |
| 522 | ka rstat |
| 523 | pr = ulong 0 |
| 524 | if eq run 0 |
| 525 | pr = peers |
| 526 | end |
| 527 | kb |
| 528 | ka sqlb |
| 529 | ka sqlro |
| 530 | ka ndb |
| 531 | ka q1 |
| 532 | if ne run 0 |
| 533 | ret 4 |
| 534 | end |
| 535 | put obl 0 0 |
| 536 | put obov 0 0 |
| 537 | oc 50 |
| 538 | oc 10 |
| 539 | onum div get nowms 0 1000 |
| 540 | oc 10 |
| 541 | ot nid |
| 542 | oc 10 |
| 543 | onum pr |
| 544 | oc 10 |
| 545 | rows |
| 546 | if not wout |
| 547 | ret 5 |
| 548 | end |
| 549 | ret 0 |
| 550 | end |
| 551 | note follow: the node's event stream is the change signal. Any event marks the snapshot dirty; a |
| 552 | note dirty snapshot is written once 30 s have passed since the last write. Idle, it only waits. |
| 553 | fn follow to int |
| 554 | st = once |
| 555 | last = long monons |
| 556 | kb |
| 557 | ka envb |
| 558 | ka envh |
| 559 | ka radb |
| 560 | ka rnode |
| 561 | ka revts |
| 562 | pid = spawn |
| 563 | if lt pid 0 |
| 564 | ret 6 |
| 565 | end |
| 566 | rfd = int get erfd 0 |
| 567 | dirty = false |
| 568 | for it ulong 4000000000 |
| 569 | tmo = long 3600000000000 |
| 570 | if dirty |
| 571 | tmo = sub add last 30000000000 long monons |
| 572 | if le tmo 0 |
| 573 | st = once |
| 574 | last = long monons |
| 575 | dirty = false |
| 576 | continue |
| 577 | end |
| 578 | end |
| 579 | w = waitio rfd 1 tmo |
| 580 | if lt w 0 |
| 581 | if ne w -4 |
| 582 | break |
| 583 | end |
| 584 | continue |
| 585 | end |
| 586 | if gt w 0 |
| 587 | r = read rfd junk 0 len junk |
| 588 | if eq r 0 |
| 589 | break |
| 590 | end |
| 591 | if lt r 0 |
| 592 | if and ne r -4 ne r -11 |
| 593 | break |
| 594 | end |
| 595 | continue |
| 596 | end |
| 597 | dirty = true |
| 598 | end |
| 599 | end |
| 600 | x = fclose rfd |
| 601 | x = killpid int pid 9 |
| 602 | w = waitpid int pid wst 0 4 0 |
| 603 | ret 6 |
| 604 | end |
| 605 | note main: no argument writes once; exactly "follow" follows; anything else is exit 2 with nothing written |
| 606 | text tfollow "follow" |
| 607 | global argb uchar 16 |
| 608 | fn main to int |
| 609 | n = argcnt |
| 610 | if eq n 1 |
| 611 | ret once |
| 612 | end |
| 613 | if ne n 2 |
| 614 | ret 2 |
| 615 | end |
| 616 | r = argread 1 argb 0 16 |
| 617 | if ne r long len tfollow |
| 618 | ret 2 |
| 619 | end |
| 620 | for i ulong len tfollow |
| 621 | if ne get argb i get tfollow i |
| 622 | ret 2 |
| 623 | end |
| 624 | end |
| 625 | ret follow |
| 626 | end |