omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / apps / interplane-forge / community / 00core.om
| 1 | note community/00core.om -- the community runner hub (protocol ipcc1, spark-connector docs/PROTOCOL.md). |
| 2 | note Keys register with a signed message; a registered key asks for a job, reruns a published salvage |
| 3 | note part on its own machine and posts a signed result; the hub checks the signature, the job, the |
| 4 | note revision and the compiler, stores every accepted message and appends one ledger line, and |
| 5 | note records every refusal with its reason. The hub never executes, compiles or opens anything a |
| 6 | note runner sends: messages are parsed as fixed-form text, and every file name built from one is |
| 7 | note lowercase hex of a fixed length below the store root. |
| 8 | note Store: INTERPLANE_COMMUNITY_DIR (absolute, ending in /); the slice answers "off" without it. |
| 9 | note Composed after om/tls/ec.om (ecdsa_verify_raw) and the salvage slice (published parts). |
| 10 | |
| 11 | note cmgv: scalar cells |
| 12 | global cmgv ulong 32 |
| 13 | global cmroot uchar 1024 |
| 14 | global cmproot uchar 1024 |
| 15 | global cmpth uchar 4096 |
| 16 | note cmfo / cmfn: offset and length in fbody of each parsed field (index constants cmf*) |
| 17 | global cmfo ulong 16 |
| 18 | global cmfn ulong 16 |
| 19 | note cmpub: the sender's public point; cmkid: its key id; cmsha: the message SHA-256 (hex) |
| 20 | global cmpub uchar 65 |
| 21 | global cmkid uchar 64 |
| 22 | global cmsha uchar 64 |
| 23 | global cmpick uchar 64 |
| 24 | |
| 25 | const cmrl ulong 0 |
| 26 | const cmpl ulong 1 |
| 27 | const cmkind ulong 2 |
| 28 | const cmsigo ulong 3 |
| 29 | const cmboot ulong 4 |
| 30 | const cmon ulong 5 |
| 31 | const cmcur ulong 6 |
| 32 | const cmlock ulong 7 |
| 33 | const cmcn ulong 8 |
| 34 | const cmck ulong 9 |
| 35 | const cmcg ulong 10 |
| 36 | const cmctie ulong 11 |
| 37 | const cmchub ulong 12 |
| 38 | const cmcsec ulong 13 |
| 39 | const cmrate ulong 14 |
| 40 | const cmdj ulong 20 |
| 41 | const cmdm ulong 21 |
| 42 | const cmlen ulong 22 |
| 43 | const cmrw ulong 27 |
| 44 | const cmrc ulong 28 |
| 45 | note a ledger or refusal log is set aside as <name>.<seconds> once it reaches 4 MiB (cm_app) |
| 46 | const cmrotb ulong 4194304 |
| 47 | const cmprl ulong 29 |
| 48 | |
| 49 | note field indexes |
| 50 | const cmfkey ulong 0 |
| 51 | const cmftime ulong 1 |
| 52 | const cmfthird ulong 2 |
| 53 | const cmfjob ulong 3 |
| 54 | const cmfpart ulong 4 |
| 55 | const cmfcomp ulong 5 |
| 56 | const cmfbin ulong 6 |
| 57 | const cmfout ulong 7 |
| 58 | const cmfexit ulong 8 |
| 59 | const cmfsig ulong 9 |
| 60 | |
| 61 | note protocol bounds (form and meaning, not volume caps): a message is at most 4096 bytes (the |
| 62 | note longest valid one is under 1 KiB), clock skew 300 s (replay window of signed messages), a job |
| 63 | note answers within 3600 s. Volume is watched, not refused: alarm thresholds per key 150 signed |
| 64 | note messages (after the signature and replay checks) an hour, all keys 3600, refusals 3600 (cm_tally); |
| 65 | note the edge (nginx) limits request rate. |
| 66 | const cmmaxb ulong 4096 |
| 67 | const cmskew ulong 300 |
| 68 | const cmjlife ulong 3600 |
| 69 | const cmakey ulong 150 |
| 70 | const cmaall ulong 3600 |
| 71 | const cmaref ulong 3600 |
| 72 | text cmtalm "alarms" |
| 73 | text cmtalw " ALARM " |
| 74 | text cmtalp " passed " |
| 75 | text cmtalh " in the hour: " |
| 76 | text cmtamsg "signed-messages" |
| 77 | text cmtaref "refusals" |
| 78 | text cmtrfn "refused" |
| 79 | |
| 80 | text cmenv "INTERPLANE_COMMUNITY_DIR=" |
| 81 | text cmpenv "INTERPLANE_COMMUNITY_PUB=" |
| 82 | text cmdirs "keys/jobs/used/msgs/parts/rate/" |
| 83 | text cmtlk "writer.lock" |
| 84 | text cmtkeys "keys/" |
| 85 | text cmtjobs "jobs/" |
| 86 | text cmtused "used/" |
| 87 | text cmtmsgs "msgs/" |
| 88 | text cmtparts "parts/" |
| 89 | text cmtrate "rate/" |
| 90 | text cmtall "all" |
| 91 | text cmtk "/k/" |
| 92 | text cmtg "/g/" |
| 93 | text cmtv ".v" |
| 94 | text cmtled "ledger" |
| 95 | text cmtref "refused" |
| 96 | text cmtdeny "deny" |
| 97 | text cmtnew ".new" |
| 98 | text cmtip "ipcc1 " |
| 99 | text cmtreg "register" |
| 100 | text cmtnext "next" |
| 101 | text cmtres "result" |
| 102 | text cmtlv "leave" |
| 103 | text cmnpub "pub" |
| 104 | text cmnkey "key" |
| 105 | text cmntime "time" |
| 106 | text cmnrate "rate" |
| 107 | text cmnnon "nonce" |
| 108 | text cmnjob "job" |
| 109 | text cmnpart "part" |
| 110 | text cmncomp "compiler" |
| 111 | text cmnbin "binary" |
| 112 | text cmnout "stdout" |
| 113 | text cmnexit "exit" |
| 114 | text cmnsig "sig" |
| 115 | text cmhex "0123456789abcdef" |
| 116 | text cmdkey "key " |
| 117 | text cmdpart "part " |
| 118 | text cmxsig "signal" |
| 119 | text cmxtmo "timeout" |
| 120 | text cmxomc "omc" |
| 121 | text cmxomcs "omcsignal" |
| 122 | text cmxomct "omctimeout" |
| 123 | text cmxcell "cell" |
| 124 | text cmsp " " |
| 125 | text cmnl "\n" |
| 126 | text cmdash "-" |
| 127 | |
| 128 | note refusal reasons (docs/PROTOCOL.md) |
| 129 | const cmemal ulong 1 |
| 130 | const cmesize ulong 2 |
| 131 | const cmeclk ulong 3 |
| 132 | const cmesig ulong 4 |
| 133 | const cmeunr ulong 5 |
| 134 | const cmeden ulong 6 |
| 135 | const cmejob ulong 7 |
| 136 | const cmemis ulong 8 |
| 137 | const cmedup ulong 9 |
| 138 | const cmeoff ulong 12 |
| 139 | const cmeio ulong 13 |
| 140 | text cmw1 "malformed" |
| 141 | text cmw2 "size" |
| 142 | text cmw3 "clock" |
| 143 | text cmw4 "signature" |
| 144 | text cmw5 "unregistered" |
| 145 | text cmw6 "denied" |
| 146 | text cmw7 "job" |
| 147 | text cmw8 "mismatch" |
| 148 | text cmw9 "duplicate" |
| 149 | text cmw12 "off" |
| 150 | text cmw13 "store" |
| 151 | |
| 152 | note ---- small helpers |
| 153 | fn cm_now to ulong |
| 154 | ret ulong div realns 1000000000 |
| 155 | end |
| 156 | |
| 157 | fn cm_hv c uchar to ulong |
| 158 | if and ge c 48 le c 57 |
| 159 | ret sub ulong c 48 |
| 160 | end |
| 161 | if and ge c 97 le c 102 |
| 162 | ret sub ulong c 87 |
| 163 | end |
| 164 | ret 16 |
| 165 | end |
| 166 | |
| 167 | fn cm_ishex s view uchar * to bool |
| 168 | for i ulong len s bound 65536 |
| 169 | if gt cm_hv get s i 15 |
| 170 | ret false |
| 171 | end |
| 172 | end |
| 173 | ret true |
| 174 | end |
| 175 | |
| 176 | fn cm_eq a view uchar * b view uchar * to bool |
| 177 | if ne len a len b |
| 178 | ret false |
| 179 | end |
| 180 | for i ulong len a bound 65536 |
| 181 | if ne get a i get b i |
| 182 | ret false |
| 183 | end |
| 184 | end |
| 185 | ret true |
| 186 | end |
| 187 | |
| 188 | fn cm_tohex s view uchar * d edit uchar * to void |
| 189 | for i ulong len s bound 32768 |
| 190 | put d mul i 2 get cmhex ulong shr get s i 4 |
| 191 | put d add mul i 2 1 get cmhex ulong and get s i 15 |
| 192 | end |
| 193 | end |
| 194 | |
| 195 | fn cm_unhex s view uchar * d edit uchar * to bool |
| 196 | if ne len s mul len d 2 |
| 197 | ret false |
| 198 | end |
| 199 | for i ulong len d bound 32768 |
| 200 | a = cm_hv get s mul i 2 |
| 201 | b = cm_hv get s add mul i 2 1 |
| 202 | if or gt a 15 gt b 15 |
| 203 | ret false |
| 204 | end |
| 205 | put d i uchar or shl a 4 b |
| 206 | end |
| 207 | ret true |
| 208 | end |
| 209 | |
| 210 | note cm_dec: a decimal of 1..19 digits, no leading zero unless "0"; 2^64-1 when malformed |
| 211 | fn cm_dec s view uchar * to ulong |
| 212 | bad = ulong 18446744073709551615 |
| 213 | if or eq len s 0 gt len s 19 |
| 214 | ret bad |
| 215 | end |
| 216 | if and gt len s 1 eq get s 0 48 |
| 217 | ret bad |
| 218 | end |
| 219 | v = ulong 0 |
| 220 | for i ulong len s bound 19 |
| 221 | c = get s i |
| 222 | if or lt c 48 gt c 57 |
| 223 | ret bad |
| 224 | end |
| 225 | v = add mul v 10 sub ulong c 48 |
| 226 | end |
| 227 | ret v |
| 228 | end |
| 229 | |
| 230 | fn cm_decput d edit uchar * o ulong v ulong to ulong |
| 231 | t = new uchar 20 0 |
| 232 | i = ulong 20 |
| 233 | x = v |
| 234 | for it ulong 20 |
| 235 | i = sub i 1 |
| 236 | put t i uchar add rem x 10 48 |
| 237 | x = div x 10 |
| 238 | if eq x 0 |
| 239 | break |
| 240 | end |
| 241 | end |
| 242 | k = o |
| 243 | for j ulong sub 20 i bound 20 |
| 244 | put d k get t add i j |
| 245 | k = add k 1 |
| 246 | end |
| 247 | ret k |
| 248 | end |
| 249 | |
| 250 | fn cm_cp d edit uchar * o ulong s view uchar * to ulong |
| 251 | for i ulong len s bound 65536 |
| 252 | put d add o i get s i |
| 253 | end |
| 254 | ret add o len s |
| 255 | end |
| 256 | |
| 257 | note ---- paths: cmpth = root, then appended pieces |
| 258 | fn cm_p0 to void |
| 259 | n = get cmgv cmrl |
| 260 | for i ulong n bound 1024 |
| 261 | put cmpth i get cmroot i |
| 262 | end |
| 263 | put cmgv cmpl n |
| 264 | end |
| 265 | |
| 266 | fn cm_pa s view uchar * to void |
| 267 | n = get cmgv cmpl |
| 268 | for i ulong len s bound 4096 |
| 269 | put cmpth add n i get s i |
| 270 | end |
| 271 | put cmgv cmpl add n len s |
| 272 | end |
| 273 | |
| 274 | note cm_pcp: a copy of cmpth into d; its length |
| 275 | fn cm_pcp d edit uchar * to ulong |
| 276 | n = get cmgv cmpl |
| 277 | for i ulong n bound 4096 |
| 278 | put d i get cmpth i |
| 279 | end |
| 280 | ret n |
| 281 | end |
| 282 | |
| 283 | fn cm_pkid to void |
| 284 | k = new uchar 64 0 |
| 285 | for i ulong 64 |
| 286 | put k i get cmkid i |
| 287 | end |
| 288 | cm_pa k |
| 289 | end |
| 290 | |
| 291 | note ---- files under cmpth |
| 292 | fn cm_rd b edit uchar * to long |
| 293 | fd = fopen cmpth 0 get cmgv cmpl 557056 0 |
| 294 | if lt fd 0 |
| 295 | ret fd |
| 296 | end |
| 297 | n = ulong 0 |
| 298 | rc = long 0 |
| 299 | while lt n len b bound 9223372036854775807 |
| 300 | r = read int fd b n sub len b n |
| 301 | if lt r 0 |
| 302 | rc = r |
| 303 | break |
| 304 | end |
| 305 | if eq r 0 |
| 306 | break |
| 307 | end |
| 308 | n = add n ulong r |
| 309 | end |
| 310 | x = fclose int fd |
| 311 | if lt rc 0 |
| 312 | ret rc |
| 313 | end |
| 314 | ret long n |
| 315 | end |
| 316 | |
| 317 | fn cm_wfd fd long d view uchar * to bool |
| 318 | n = ulong 0 |
| 319 | while lt n len d bound 9223372036854775807 |
| 320 | r = write int fd d n sub len d n |
| 321 | if le r 0 |
| 322 | ret false |
| 323 | end |
| 324 | n = add n ulong r |
| 325 | end |
| 326 | ret true |
| 327 | end |
| 328 | |
| 329 | note cm_wnew: create cmpth with O_EXCL (0600) holding d; false when it exists or fails |
| 330 | fn cm_wnew d view uchar * to bool |
| 331 | fd = fopen cmpth 0 get cmgv cmpl 557249 384 |
| 332 | if lt fd 0 |
| 333 | ret false |
| 334 | end |
| 335 | ok = cm_wfd fd d |
| 336 | s = fsync int fd |
| 337 | x = fclose int fd |
| 338 | ret and ok eq s 0 |
| 339 | end |
| 340 | |
| 341 | note cm_app: append d to cmpth (created 0600 when absent); one write per line keeps lines whole. |
| 342 | note A log that has reached cmrotb bytes is first renamed to <name>.<unix seconds> (kept, served at |
| 343 | note /community/<name>.<seconds>, never deleted), so no file grows toward forged's file-size limit; |
| 344 | note the store's size is the operator's to watch (the gate measures it). |
| 345 | fn cm_app d view uchar * to bool |
| 346 | sb = new uchar 128 0 |
| 347 | fd = fopen cmpth 0 get cmgv cmpl 558145 384 |
| 348 | if lt fd 0 |
| 349 | ret false |
| 350 | end |
| 351 | r = fstat int fd sb 0 128 |
| 352 | sz = ulong 0 |
| 353 | for i ulong 8 |
| 354 | sz = or sz shl ulong get sb add 48 i mul i 8 |
| 355 | end |
| 356 | if and eq r 0 ge sz cmrotb |
| 357 | x = fclose int fd |
| 358 | if not cm_rot |
| 359 | ret false |
| 360 | end |
| 361 | fd = fopen cmpth 0 get cmgv cmpl 558145 384 |
| 362 | if lt fd 0 |
| 363 | ret false |
| 364 | end |
| 365 | end |
| 366 | ok = cm_wfd fd d |
| 367 | x = fclose int fd |
| 368 | ret ok |
| 369 | end |
| 370 | |
| 371 | note cm_rot: rename cmpth to cmpth.<unix seconds> (no replacement); cmpth is unchanged afterwards |
| 372 | fn cm_rot to bool |
| 373 | a = new uchar 4100 0 |
| 374 | b = new uchar 4130 0 |
| 375 | p = new uchar 8300 0 |
| 376 | n = cm_pcp a |
| 377 | k = cm_cp b 0 part a 0 n |
| 378 | put b k 46 |
| 379 | k = cm_decput b add k 1 cm_now |
| 380 | put p 0 2 |
| 381 | put p 1 0 |
| 382 | o = cm_pk p 2 part a 0 n |
| 383 | o = cm_pk p o part b 0 k |
| 384 | r = renames p 0 o 1 |
| 385 | ret or eq r 0 eq r -17 |
| 386 | end |
| 387 | |
| 388 | note cm_repl: write d to "<cmpth>.new", fsync, then rename it over cmpth (records) |
| 389 | fn cm_repl d view uchar * to bool |
| 390 | ret cm_repl2 d true |
| 391 | end |
| 392 | |
| 393 | note cm_replq: the same without fsync, for volume counters (a count lost in a crash is harmless) |
| 394 | fn cm_replq d view uchar * to bool |
| 395 | ret cm_repl2 d false |
| 396 | end |
| 397 | |
| 398 | fn cm_repl2 d view uchar * sync bool to bool |
| 399 | a = new uchar 4100 0 |
| 400 | b = new uchar 4100 0 |
| 401 | p = new uchar 8300 0 |
| 402 | n = cm_pcp a |
| 403 | cm_pa cmtnew |
| 404 | nb = cm_pcp b |
| 405 | fd = fopen cmpth 0 get cmgv cmpl 557633 384 |
| 406 | if lt fd 0 |
| 407 | ret false |
| 408 | end |
| 409 | ok = cm_wfd fd d |
| 410 | s = long 0 |
| 411 | if sync |
| 412 | s = fsync int fd |
| 413 | end |
| 414 | x = fclose int fd |
| 415 | if not and ok eq s 0 |
| 416 | ret false |
| 417 | end |
| 418 | put p 0 2 |
| 419 | put p 1 0 |
| 420 | k = cm_pk p 2 part b 0 nb |
| 421 | ret cm_ren p k part a 0 n |
| 422 | end |
| 423 | |
| 424 | fn cm_ren p edit uchar * k ulong b view uchar * to bool |
| 425 | o = cm_pk p k b |
| 426 | r = renames p 0 o 0 |
| 427 | ret eq r 0 |
| 428 | end |
| 429 | |
| 430 | fn cm_pk p edit uchar * o ulong s view uchar * to ulong |
| 431 | put p o uchar and len s 255 |
| 432 | put p add o 1 uchar shr len s 8 |
| 433 | for i ulong len s bound 65536 |
| 434 | put p add add o 2 i get s i |
| 435 | end |
| 436 | ret add add o 2 len s |
| 437 | end |
| 438 | |
| 439 | fn cm_exists to bool |
| 440 | ret eq faccess cmpth 0 get cmgv cmpl 0 0 |
| 441 | end |
| 442 | |
| 443 | fn cm_mkdir to bool |
| 444 | r = makedir cmpth 0 get cmgv cmpl 448 |
| 445 | ret or eq r 0 eq r -17 |
| 446 | end |
| 447 | |
| 448 | note ---- start-up: the store root and the read-only root from the environment, the store's |
| 449 | note directories, and the salvage store. INTERPLANE_COMMUNITY_DIR is the writable store (records). |
| 450 | note INTERPLANE_COMMUNITY_PUB holds what the hub only reads: dist/ (the join script, the runner, |
| 451 | note the compiler, SHA256SUMS: what volunteers download) and deny (the operator's list). In |
| 452 | note production it is a host folder outside forge-data, bound read-only at /community directly |
| 453 | note under the sandbox's read-only root, so no path the server can write or rename leads to it. |
| 454 | note Both are required; without either the slice is off. |
| 455 | fn cm_on to bool |
| 456 | if eq get cmgv cmboot 1 |
| 457 | ret eq get cmgv cmon 1 |
| 458 | end |
| 459 | put cmgv cmboot 1 |
| 460 | put cmgv cmon 0 |
| 461 | if not slboot |
| 462 | ret false |
| 463 | end |
| 464 | b = new uchar 1024 0 |
| 465 | n = cm_env cmenv b |
| 466 | if or lt n 2 gt n 900 |
| 467 | ret false |
| 468 | end |
| 469 | if not cm_rootok part b 0 ulong n |
| 470 | ret false |
| 471 | end |
| 472 | for i ulong ulong n bound 1024 |
| 473 | put cmroot i get b i |
| 474 | end |
| 475 | put cmgv cmrl ulong n |
| 476 | n = cm_env cmpenv b |
| 477 | if or lt n 2 gt n 900 |
| 478 | ret false |
| 479 | end |
| 480 | if not cm_rootok part b 0 ulong n |
| 481 | ret false |
| 482 | end |
| 483 | for i ulong ulong n bound 1024 |
| 484 | put cmproot i get b i |
| 485 | end |
| 486 | put cmgv cmprl ulong n |
| 487 | if not cm_mkall |
| 488 | ret false |
| 489 | end |
| 490 | put cmgv cmon 1 |
| 491 | ret true |
| 492 | end |
| 493 | |
| 494 | note cm_q0: cmpth = the read-only root |
| 495 | fn cm_q0 to void |
| 496 | n = get cmgv cmprl |
| 497 | for i ulong n bound 1024 |
| 498 | put cmpth i get cmproot i |
| 499 | end |
| 500 | put cmgv cmpl n |
| 501 | end |
| 502 | |
| 503 | note cm_rootok: absolute, ends in /, no control byte, no segment starting with a dot |
| 504 | fn cm_rootok b view uchar * to bool |
| 505 | if or lt len b 2 gt len b 900 |
| 506 | ret false |
| 507 | end |
| 508 | if or ne get b 0 47 ne get b sub len b 1 47 |
| 509 | ret false |
| 510 | end |
| 511 | for i ulong len b bound 1024 |
| 512 | c = get b i |
| 513 | if or lt c 32 eq c 127 |
| 514 | ret false |
| 515 | end |
| 516 | end |
| 517 | for i ulong sub len b 1 bound 1024 |
| 518 | if and eq get b i 47 eq get b add i 1 46 |
| 519 | ret false |
| 520 | end |
| 521 | end |
| 522 | ret true |
| 523 | end |
| 524 | |
| 525 | fn cm_env pre view uchar * b edit uchar * to long |
| 526 | e = new uchar 1100 0 |
| 527 | for i ulong ulong envcnt bound 1000000 |
| 528 | n = envread i e 0 1100 |
| 529 | if gt n long len pre |
| 530 | if cm_eq part e 0 len pre pre |
| 531 | m = sub ulong n len pre |
| 532 | if gt m len b |
| 533 | ret -1 |
| 534 | end |
| 535 | for j ulong m bound 1100 |
| 536 | put b j get e add len pre j |
| 537 | end |
| 538 | ret long m |
| 539 | end |
| 540 | end |
| 541 | end |
| 542 | ret -1 |
| 543 | end |
| 544 | |
| 545 | note cm_mkall: the root and its six directories (names in cmdirs, each ending in /) |
| 546 | fn cm_mkall to bool |
| 547 | cm_p0 |
| 548 | if not cm_mkdir |
| 549 | ret false |
| 550 | end |
| 551 | s = ulong 0 |
| 552 | for i ulong len cmdirs |
| 553 | if eq get cmdirs i 47 |
| 554 | cm_p0 |
| 555 | cm_pa part cmdirs s add sub i s 1 |
| 556 | if not cm_mkdir |
| 557 | ret false |
| 558 | end |
| 559 | s = add i 1 |
| 560 | end |
| 561 | end |
| 562 | ret true |
| 563 | end |
| 564 | |
| 565 | note cm_lock: the writer lock (flock on <root>writer.lock); held until the request ends |
| 566 | fn cm_lock to bool |
| 567 | cm_p0 |
| 568 | cm_pa cmtlk |
| 569 | fd = fopen cmpth 0 get cmgv cmpl 557122 384 |
| 570 | if lt fd 0 |
| 571 | ret false |
| 572 | end |
| 573 | r = flock int fd 2 |
| 574 | if ne r 0 |
| 575 | x = fclose int fd |
| 576 | ret false |
| 577 | end |
| 578 | put cmgv cmlock ulong fd |
| 579 | ret true |
| 580 | end |
| 581 | |
| 582 | fn cm_unlock to void |
| 583 | fd = get cmgv cmlock |
| 584 | if ne fd 0 |
| 585 | x = fclose int fd |
| 586 | put cmgv cmlock 0 |
| 587 | end |
| 588 | end |