Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-sovereign-core-mv/blob/main/evidence/aienet0-tur-ledger-20261027/audit-v2-before.om.at-run # aien-sovereign-core-mv AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-sovereign-core-mv / evidence / aienet0-tur-ledger-20261027 / audit-v2-before.om.at-run` 1609 lines ยท 26458 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note tur-ledger: the Aienet-0 TUR test-credit escrow ledger. One append-only journal, one writer 3 note at a time, settle exactly once per JOB. Arithmetic is tests/turing-network/tur_r0_oracle.om 4 note applied per JOB: reserve moves AMOUNT from REQ into that JOB's escrow; settle pays PAYP to 5 note PROV, PAYV to VER and BACK to REQ, refused unless PAYP <= escrow, PAYV <= escrow - PAYP and 6 note BACK = escrow - PAYP - PAYV; a JOB is reserved once and settled or refunded once. 7 note Build: check.sh composes om2, vendor/sha256.om, vendor/ec.om and this program for pinned omc. 8 note All external mutations require an issuer DER P-256/SHA-256 signature (hex final argument). 9 note Settle additionally requires the independent verifier signature; reserve adds QUOTE_SHA256; 10 note settle adds SIGNED_JOB_SHA256 AN0V_SHA256 before its two signatures. See README for canonical bytes. 11 note Journal DIR/journal: SEQ PREV KIND FIELDS... HASH. Genesis is the exact DIR/authority config. 12 note Version 2 refuses unsigned/version-1 journals. The hash chain is not an authorization mechanism. 13 note Every command opens DIR/journal, takes flock LOCK_EX on it for the whole command and replays 14 note it: SEQ, PREV and HASH are checked on every line, then the record is applied. A LAST line that 15 note is unterminated, unparseable or whose HASH does not match its own bytes is a torn tail: it is 16 note truncated, fsynced, and a "recover N SHA" record (N torn bytes, SHA their sha256) is appended. 17 note A complete self-consistent line whose SEQ or PREV does not link (a deleted or reordered 18 note record), or any bad line before the last, is corruption and the ledger refuses (exit 5). 19 note After replay the ledger checks conservation (exit 6): the sum 20 note of balances plus open escrow equals the sum of prefunds, open escrow equals the open JOBs' 21 note amounts, and every settled JOB paid out exactly its amount. A new record is written with 22 note pwrite and fsynced before the command prints its result. 23 note Exit: 0 ok (also dup / already-*), 2 usage, 3 journal io, 4 lock, 5 journal corrupt, 24 note 6 conservation violated, 7 insufficient funds, 8 unknown job, 9 job conflict, 10 wrong phase, 25 note 11 settle receipt conflict, 12 escrow rule, 13 prefund id conflict, 14 capacity, 15 overflow, 26 note 16 missing config or unauthorized signer/policy. Missing signature arguments are usage (2). 27 note Accounts, PIDs and JOBs: KCAP keys in all. A name is 1..64 bytes of [A-Za-z0-9._:-]; JOB and 28 note RECEIPT_SHA256 are 64 lowercase hex bytes; amounts are decimal u64 without leading zeros. 29 const KCAP ulong 65536 30 const KIX ulong 131072 31 const KW ulong 64 32 const SPRE ulong 0 33 const SESC ulong 1 34 const SSEQ ulong 2 35 const SGOOD ulong 3 36 const SKEYS ulong 4 37 const SRECS ulong 5 38 const SNF ulong 6 39 const SLN ulong 7 40 const SOB ulong 8 41 const SOVF ulong 9 42 const SBAD ulong 10 43 const SFLEN ulong 11 44 const SFSZ ulong 12 45 const SFD ulong 13 46 const SSLOT ulong 14 47 const SRF ulong 15 48 const SLONG ulong 16 49 const STORN ulong 17 50 const SWB ulong 18 51 global st ulong 32 52 global ga uchar 8192 53 global gs ulong 16 54 global gl ulong 16 55 global pb uchar 4200 56 global lb uchar 1024 57 global fo ulong 16 58 global fl ulong 16 59 global ob uchar 4096 60 global wb uchar 2048 61 global rb uchar 65536 62 global ph uchar 64 63 global th uchar 64 64 global kb uchar `4194304` 65 global kl ulong 65536 66 global kt ulong 65536 67 global ki ulong 131072 68 global d0 ulong 65536 69 global d1 ulong 65536 70 global d2 ulong 65536 71 global d3 ulong 65536 72 global d4 ulong 65536 73 global d5 ulong 65536 74 global d6 ulong 65536 75 global d7 ulong 65536 76 global rc uchar `4194304` 77 note Authority config is provisioned externally; no private keys or automatic trust establishment. 78 global cfg uchar 1024 79 global cfgn ulong 1 80 global cfo ulong 12 81 global cfl ulong 12 82 global ah uchar 64 83 global pa uchar `4194304` 84 global ja uchar `4194304` 85 global sa uchar `4194304` 86 text apath "/authority" 87 text auerr "refused 16 authorization\n" 88 89 fn authdie to void 90 e = write 2 auerr 0 len auerr 91 exit 16 92 end 93 94 fn hexf i ulong n ulong to bool 95 if ne get fl i n 96 ret false 97 end 98 for j ulong n 99 c = get lb add get fo i j 100 if not or and ge c 48 le c 57 and ge c 97 le c 102 101 ret false 102 end 103 end 104 ret true 105 end 106 107 fn cfeq f ulong b ulong to bool 108 if ne get cfl f get fl b 109 ret false 110 end 111 for i ulong get cfl f 112 if ne get cfg add get cfo f i get lb add get fo b i 113 ret false 114 end 115 end 116 ret true 117 end 118 119 note acfg reads one canonical genesis body, no LF. Fixed policy names bind account and node owners. 120 fn acfg to void 121 n = get gl 2 122 if or eq n 0 gt n 4000 123 die 2 124 end 125 for i ulong n 126 put pb i get ga add get gs 2 i 127 end 128 for i ulong len apath 129 put pb add n i get apath i 130 end 131 fd = fopen pb 0 add n len apath 524288 0 132 if lt fd 0 133 authdie 134 end 135 k = read int fd lb 0 len lb 136 if or le k 0 ge k long len lb 137 authdie 138 end 139 z = read int fd pb 0 1 140 cl = fclose int fd 141 if or ne z 0 ne cl 0 142 authdie 143 end 144 if not split ulong k 145 authdie 146 end 147 if ne get st SNF 12 148 authdie 149 end 150 if not and fis 0 22 fis 1 24 151 authdie 152 end 153 if not and fhex 2 and hexf 3 130 and hexf 4 130 fhex 5 154 authdie 155 end 156 if not and fname 6 and fname 7 fname 8 157 authdie 158 end 159 if not and fhex 9 and fhex 10 fhex 11 160 authdie 161 end 162 put cfgn 0 ulong k 163 for i ulong ulong k 164 put cfg i get lb i 165 end 166 for i ulong 12 167 put cfo i get fo i 168 put cfl i get fl i 169 end 170 if cfeq 3 4 171 authdie 172 end 173 if or cfeq 6 7 or cfeq 6 8 cfeq 7 8 174 authdie 175 end 176 put st SLN 0 177 end 178 179 note ahash: SHA256(config || LF || canonical unsigned body || LF [|| reserve-auth-hash || LF]). 180 note Signatures and journal framing are excluded; operation identity is the semantic hash. 181 fn ahash b ulong sig ulong job bool to void 182 hreset 183 for i ulong get cfgn 0 184 x = hpush get cfg i 185 end 186 x = hpush 10 187 o = get fo b 188 for i ulong sub sub get fo sig 1 o 189 x = hpush get lb add o i 190 end 191 x = hpush 10 192 if job 193 j = kfind 1 add b 1 194 if ne j 0 195 for i ulong 64 196 x = hpush get ja add mul sub j 1 KW i 197 end 198 x = hpush 10 199 end 200 end 201 y = hfinal 202 for i ulong 64 203 put ah i get hd i 204 end 205 end 206 207 fn authsig f ulong key ulong to bool 208 pub = new uchar 65 0 209 sg = new uchar 104 0 210 n = get fl f 211 if or lt n 16 gt n 144 212 ret false 213 end 214 if ne rem n 2 0 215 ret false 216 end 217 if not hexf f n 218 ret false 219 end 220 for i ulong 65 221 o = add get cfo key mul i 2 222 v = or shl hnibble get cfg o 4 hnibble get cfg add o 1 223 put pub i uchar v 224 end 225 for i ulong 32 226 o = mul i 2 227 put sg i uchar or shl hnibble get ah o 4 hnibble get ah add o 1 228 end 229 for i ulong div n 2 230 o = add get fo f mul i 2 231 v = shl hnibble get lb o 4 232 v = or v hnibble get lb add o 1 233 put sg add 32 i uchar v 234 end 235 sz = add 32 div n 2 236 ret eq ecdsa_verify 256 pub part sg 0 sz 32 0 237 end 238 239 fn authrec b ulong fields ulong job bool dual bool to bool 240 need = add fields 1 241 if dual 242 need = add need 1 243 end 244 if ne get st SRF need 245 ret false 246 end 247 ahash b add b fields job 248 if not authsig add b fields 3 249 ret false 250 end 251 if dual 252 if not authsig add add b fields 1 4 253 ret false 254 end 255 end 256 ret true 257 end 258 259 fn aeqhash mode ulong k ulong to bool 260 for i ulong 64 261 o = add mul k KW i 262 v = get pa o 263 if eq mode 1 264 v = get ja o 265 end 266 if eq mode 2 267 v = get sa o 268 end 269 if ne v get ah i 270 ret false 271 end 272 end 273 ret true 274 end 275 276 fn asave mode ulong k ulong to void 277 for i ulong 64 278 o = add mul k KW i 279 if eq mode 0 280 put pa o get ah i 281 end 282 if eq mode 1 283 put ja o get ah i 284 end 285 if eq mode 2 286 put sa o get ah i 287 end 288 end 289 end 290 note tw: words, separated by ~ (byte 126). Word c - 1 is the reason for exit code c (2..15). 291 text tw "refused ~usage~journal-io~lock~journal-corrupt~conservation-violated~insufficient-funds~unknown-job~job-conflict~wrong-phase~settle-receipt-conflict~escrow-rule~prefund-id-conflict~capacity~overflow~prefund~reserve~settle~refund~balance~show~audit~genesis~recover~tur-ledger-2~prefunded~dup~reserved~already-reserved~settled~already-settled~refunded~already-refunded~job ~audit ok records ~ head ~ accounts ~ jobs ~ open ~ settled ~ refunded ~ prefunded ~ balances ~ escrow ~recovered torn-tail ~ bytes sha256 ~--journal~/journal~usage: tur-ledger --journal DIR COMMAND (see om/tur-ledger/README.md for signed v2 commands)~balance ~" 292 text hx "`0123456789abcdef`" 293 294 note fst: start of word k in tw; its length goes to st SFLEN. 295 fn fst k ulong to ulong 296 c = ulong 0 297 s = ulong 0 298 for i ulong len tw 299 if eq get tw i 126 300 if eq c k 301 put st SFLEN sub i s 302 ret s 303 end 304 c = add c 1 305 s = add i 1 306 end 307 end 308 put st SFLEN 0 309 ret 0 310 end 311 312 note cadd: a + b; an overflow sets st SOVF. 313 fn cadd a ulong b ulong to ulong 314 s = add a b 315 if lt s a 316 put st SOVF 1 317 end 318 ret s 319 end 320 321 note oput / ofr / odec / ofld / okey / ohex: append to the output line ob. 322 fn oput c uchar to void 323 n = get st SOB 324 if lt n len ob 325 put ob n c 326 put st SOB add n 1 327 end 328 end 329 330 fn ofr k ulong to void 331 s = fst k 332 for i ulong get st SFLEN 333 oput get tw add s i 334 end 335 end 336 337 fn odec v ulong to void 338 p = ulong 1 339 while le p div v 10 340 p = mul p 10 341 end 342 x = v 343 while gt p 0 344 oput uchar add 48 div x p 345 x = rem x p 346 p = div p 10 347 end 348 end 349 350 fn ofld i ulong to void 351 o = get fo i 352 for j ulong get fl i 353 oput get lb add o j 354 end 355 end 356 357 fn okey k ulong to void 358 b = mul k KW 359 for j ulong get kl k 360 oput get kb add b j 361 end 362 end 363 364 fn orc k ulong to void 365 b = mul k KW 366 for j ulong 64 367 oput get rc add b j 368 end 369 end 370 371 note oflush: write ob to fd and empty it. 372 fn oflush fd int to void 373 o = ulong 0 374 m = get st SOB 375 while lt o m 376 w = write fd ob o sub m o 377 if ne w -4 378 if le w 0 379 put st SOB 0 380 ret 381 end 382 o = add o ulong w 383 end 384 end 385 put st SOB 0 386 end 387 388 note die: "refused C reason" on stderr, exit C. 389 fn die c int to void 390 put st SOB 0 391 ofr 0 392 odec ulong c 393 oput 32 394 ofr sub ulong c 1 395 oput 10 396 oflush 2 397 exit c 398 end 399 400 note lbput / lbfr / lbdec / lbarg / lbhex: build a record body in lb (length st SLN). 401 fn lbput c uchar to void 402 n = get st SLN 403 if ge n len lb 404 die 2 405 ret 406 end 407 put lb n c 408 put st SLN add n 1 409 end 410 411 fn lbfr k ulong to void 412 s = fst k 413 for i ulong get st SFLEN 414 lbput get tw add s i 415 end 416 end 417 418 fn lbdec v ulong to void 419 p = ulong 1 420 while le p div v 10 421 p = mul p 10 422 end 423 x = v 424 while gt p 0 425 lbput uchar add 48 div x p 426 x = rem x p 427 p = div p 10 428 end 429 end 430 431 fn lbarg i ulong to void 432 s = get gs i 433 for j ulong get gl i 434 lbput get ga add s j 435 end 436 end 437 438 note split: fields of lb[0 .. n) separated by single spaces into fo / fl, count in st SNF. 439 fn split n ulong to bool 440 nf = ulong 0 441 s = ulong 0 442 for i ulong n 443 if eq get lb i 32 444 if or eq i s ge nf 15 445 ret false 446 end 447 put fo nf s 448 put fl nf sub i s 449 nf = add nf 1 450 s = add i 1 451 end 452 end 453 if eq n s 454 ret false 455 end 456 put fo nf s 457 put fl nf sub n s 458 put st SNF add nf 1 459 ret true 460 end 461 462 note fis: field i equals word k. 463 fn fis i ulong k ulong to bool 464 s = fst k 465 n = get fl i 466 if ne n get st SFLEN 467 ret false 468 end 469 o = get fo i 470 for j ulong n 471 if ne get lb add o j get tw add s j 472 ret false 473 end 474 end 475 ret true 476 end 477 478 fn namec c uchar to bool 479 if and ge c 48 le c 57 480 ret true 481 end 482 if and ge c 65 le c 90 483 ret true 484 end 485 if and ge c 97 le c 122 486 ret true 487 end 488 if or or eq c 46 eq c 95 or eq c 58 eq c 45 489 ret true 490 end 491 ret false 492 end 493 494 note fname: field i is a name, 1..64 bytes of [A-Za-z0-9._:-]. 495 fn fname i ulong to bool 496 n = get fl i 497 if or eq n 0 gt n 64 498 ret false 499 end 500 o = get fo i 501 for j ulong n 502 if not namec get lb add o j 503 ret false 504 end 505 end 506 ret true 507 end 508 509 note fhex: field i is 64 lowercase hex bytes. 510 fn fhex i ulong to bool 511 if ne get fl i 64 512 ret false 513 end 514 o = get fo i 515 for j ulong 64 516 c = get lb add o j 517 if not or and ge c 48 le c 57 and ge c 97 le c 102 518 ret false 519 end 520 end 521 ret true 522 end 523 524 note fnum: field i as a decimal u64 (no sign, no leading zero); a bad field sets st SBAD. 525 fn fnum i ulong to ulong 526 n = get fl i 527 o = get fo i 528 if or eq n 0 gt n 20 529 put st SBAD 1 530 ret 0 531 end 532 if and gt n 1 eq get lb o 48 533 put st SBAD 1 534 ret 0 535 end 536 v = ulong 0 537 for j ulong n 538 c = get lb add o j 539 if or lt c 48 gt c 57 540 put st SBAD 1 541 ret 0 542 end 543 d = ulong sub c 48 544 if gt v div sub `18446744073709551615` d 10 545 put st SBAD 1 546 ret 0 547 end 548 v = add mul v 10 d 549 end 550 ret v 551 end 552 553 note Keys: type 0 account (d0 balance), 1 job (d0 req, d1 prov, d2 ver key ids, d3 amount, 554 note d4 phase 1 reserved 2 settled 3 refunded, d5 payp, d6 payv, d7 back, rc receipt), 555 note 2 prefund id (d0 account key id, d1 amount). ki is an open-addressed index of key id + 1. 556 fn khash t ulong i ulong to ulong 557 h = xor `14695981039346656037` t 558 o = get fo i 559 for j ulong get fl i 560 h = mul xor h ulong get lb add o j `1099511628211` 561 end 562 ret h 563 end 564 565 fn keq k ulong t ulong i ulong to bool 566 if ne get kt k t 567 ret false 568 end 569 n = get fl i 570 if ne get kl k n 571 ret false 572 end 573 b = mul k KW 574 o = get fo i 575 for j ulong n 576 if ne get kb add b j get lb add o j 577 ret false 578 end 579 end 580 ret true 581 end 582 583 note kfind: key id + 1 of (t, field i), or 0 with the free slot in st SSLOT. 584 fn kfind t ulong i ulong to ulong 585 m = sub KIX 1 586 s = and khash t i m 587 for p ulong KIX 588 e = get ki s 589 if eq e 0 590 put st SSLOT s 591 ret 0 592 end 593 if keq sub e 1 t i 594 ret e 595 end 596 s = and add s 1 m 597 end 598 trap 9 599 end 600 601 note kget: key id + 1 of (t, field i), added when new; 0 when the table is full. 602 fn kget t ulong i ulong to ulong 603 e = kfind t i 604 if ne e 0 605 ret e 606 end 607 k = get st SKEYS 608 if ge k KCAP 609 ret 0 610 end 611 n = get fl i 612 b = mul k KW 613 o = get fo i 614 for j ulong n 615 put kb add b j get lb add o j 616 end 617 put kl k n 618 put kt k t 619 put ki get st SSLOT add k 1 620 put st SKEYS add k 1 621 ret add k 1 622 end 623 624 note cred: account k += v; esub: open escrow -= v. Overflow and underflow set st SOVF. 625 fn cred k ulong v ulong to void 626 put d0 k cadd get d0 k v 627 end 628 629 fn esub v ulong to void 630 e = get st SESC 631 if lt e v 632 put st SOVF 1 633 ret 634 end 635 put st SESC sub e v 636 end 637 638 note Record checks at field base b (the KIND). live is a command, not replay. Result: 0 apply, 639 note 1 identical repeat (live only), else a refusal code. Replay refuses any nonzero result. 640 fn vpre b ulong live bool to int 641 if ne get st SRF 5 642 ret 2 643 end 644 if not and fname add b 1 fname add b 2 645 ret 2 646 end 647 put st SBAD 0 648 a = fnum add b 3 649 if or ne get st SBAD 0 eq a 0 650 ret 2 651 end 652 if not and cfeq 6 add b 2 authrec b 4 false false 653 ret 16 654 end 655 p = kfind 2 add b 1 656 if ne p 0 657 k = sub p 1 658 if and aeqhash 0 k and eq get d1 k a keq get d0 k 0 add b 2 659 ret 1 660 end 661 ret 13 662 end 663 ac = kfind 0 add b 2 664 bal = ulong 0 665 if ne ac 0 666 bal = get d0 sub ac 1 667 end 668 put st SOVF 0 669 x = cadd bal a 670 y = cadd get st SPRE a 671 if or ne get st SOVF 0 lt y x 672 ret 15 673 end 674 note New prefund id plus requester only if the account is not present. 675 needed = ulong 1 676 if eq ac 0 677 needed = ulong 2 678 end 679 if gt add get st SKEYS needed KCAP 680 ret 14 681 end 682 ret 0 683 end 684 685 fn apre b ulong to int 686 p = kget 2 add b 1 687 ac = kget 0 add b 2 688 if or eq p 0 eq ac 0 689 ret 14 690 end 691 put st SBAD 0 692 a = fnum add b 3 693 k = sub p 1 694 c = sub ac 1 695 put d0 k c 696 put d1 k a 697 asave 0 k 698 cred c a 699 put st SPRE cadd get st SPRE a 700 ret 0 701 end 702 703 fn jsame k ulong b ulong a ulong to bool 704 if ne get d3 k a 705 ret false 706 end 707 if not keq get d0 k 0 add b 2 708 ret false 709 end 710 if not keq get d1 k 0 add b 3 711 ret false 712 end 713 if not keq get d2 k 0 add b 4 714 ret false 715 end 716 ret true 717 end 718 719 fn vres b ulong live bool to int 720 if ne get st SRF 8 721 ret 2 722 end 723 if not fhex add b 1 724 ret 2 725 end 726 if not and fname add b 2 and fname add b 3 fname add b 4 727 ret 2 728 end 729 put st SBAD 0 730 a = fnum add b 5 731 if or ne get st SBAD 0 eq a 0 732 ret 2 733 end 734 if not and cfeq 6 add b 2 and cfeq 7 add b 3 cfeq 8 add b 4 735 ret 16 736 end 737 if not and fhex add b 6 authrec b 7 false false 738 ret 16 739 end 740 j = kfind 1 add b 1 741 if ne j 0 742 if and and live aeqhash 1 sub j 1 jsame sub j 1 b a 743 ret 1 744 end 745 ret 9 746 end 747 r = kfind 0 add b 2 748 if eq r 0 749 ret 7 750 end 751 if lt get d0 sub r 1 a 752 ret 7 753 end 754 note Reserve allocates its job and any missing distinct pinned role accounts. 755 needed = ulong 1 756 p = kfind 0 add b 3 757 v = kfind 0 add b 4 758 if eq p 0 759 needed = add needed 1 760 end 761 if eq v 0 762 needed = add needed 1 763 end 764 if gt add get st SKEYS needed KCAP 765 ret 14 766 end 767 ret 0 768 end 769 770 fn ares b ulong to int 771 j = kget 1 add b 1 772 r = kget 0 add b 2 773 p = kget 0 add b 3 774 v = kget 0 add b 4 775 if or or eq j 0 eq r 0 or eq p 0 eq v 0 776 ret 14 777 end 778 put st SBAD 0 779 a = fnum add b 5 780 k = sub j 1 781 q = sub r 1 782 put d0 k q 783 asave 1 k 784 put d1 k sub p 1 785 put d2 k sub v 1 786 put d3 k a 787 put d4 k 1 788 put d0 q sub get d0 q a 789 put st SESC cadd get st SESC a 790 ret 0 791 end 792 793 note ssame: settled JOB k carries the receipt and amounts of the settle record at b. 794 fn ssame k ulong b ulong to bool 795 o = get fo add b 2 796 r = mul k KW 797 for j ulong 64 798 if ne get rc add r j get lb add o j 799 ret false 800 end 801 end 802 put st SBAD 0 803 if ne get d5 k fnum add b 3 804 ret false 805 end 806 if ne get d6 k fnum add b 4 807 ret false 808 end 809 if ne get d7 k fnum add b 5 810 ret false 811 end 812 ret true 813 end 814 815 note sdup: a settle of an already settled JOB: an identical repeat is 1, anything else 11. 816 fn sdup k ulong b ulong live bool to int 817 if and and live aeqhash 2 k ssame k b 818 ret 1 819 end 820 ret 11 821 end 822 823 note vrule: the oracle's settle rule on escrow e. 824 fn vrule e ulong pp ulong pv ulong bk ulong to int 825 if gt pp e 826 ret 12 827 end 828 r = sub e pp 829 if gt pv r 830 ret 12 831 end 832 r = sub r pv 833 if ne bk r 834 ret 12 835 end 836 ret 0 837 end 838 839 fn vset b ulong live bool to int 840 if ne get st SRF 10 841 ret 2 842 end 843 if not and fhex add b 1 fhex add b 2 844 ret 2 845 end 846 put st SBAD 0 847 pp = fnum add b 3 848 pv = fnum add b 4 849 bk = fnum add b 5 850 if ne get st SBAD 0 851 ret 2 852 end 853 if not and fhex add b 6 fhex add b 7 854 ret 2 855 end 856 if not authrec b 8 true true 857 ret 16 858 end 859 j = kfind 1 add b 1 860 if eq j 0 861 ret 8 862 end 863 k = sub j 1 864 sph = get d4 k 865 if eq sph 2 866 ret sdup k b live 867 end 868 if eq sph 3 869 ret 10 870 end 871 if live 872 ret vrule get d3 k pp pv bk 873 end 874 ret 0 875 end 876 877 fn aset b ulong to int 878 j = kfind 1 add b 1 879 k = sub j 1 880 put st SBAD 0 881 pp = fnum add b 3 882 pv = fnum add b 4 883 bk = fnum add b 5 884 put d4 k 2 885 asave 2 k 886 put d5 k pp 887 put d6 k pv 888 put d7 k bk 889 o = get fo add b 2 890 r = mul k KW 891 for i ulong 64 892 put rc add r i get lb add o i 893 end 894 cred get d0 k bk 895 cred get d1 k pp 896 cred get d2 k pv 897 esub get d3 k 898 ret 0 899 end 900 901 fn vref b ulong live bool to int 902 if ne get st SRF 3 903 ret 2 904 end 905 if not fhex add b 1 906 ret 2 907 end 908 if not authrec b 2 true false 909 ret 16 910 end 911 j = kfind 1 add b 1 912 if eq j 0 913 ret 8 914 end 915 rph = get d4 sub j 1 916 if and live eq rph 3 917 ret 1 918 end 919 if ne rph 1 920 ret 10 921 end 922 ret 0 923 end 924 925 fn aref b ulong to int 926 j = kfind 1 add b 1 927 k = sub j 1 928 put d4 k 3 929 cred get d0 k get d3 k 930 esub get d3 k 931 ret 0 932 end 933 934 note rapply: check and apply the record at field 2 of a replayed line; 0 or a refusal code. 935 fn rapply to int 936 b = ulong 2 937 if eq get st SSEQ 0 938 o = get fo b 939 n = sub sub get fo sub get st SNF 1 1 o 940 if ne n get cfgn 0 941 ret 5 942 end 943 for i ulong n 944 if ne get cfg i get lb add o i 945 ret 5 946 end 947 end 948 ret 0 949 end 950 if fis b 15 951 if ne vpre b false 0 952 ret 5 953 end 954 ret apre b 955 end 956 if fis b 16 957 if ne vres b false 0 958 ret 5 959 end 960 ret ares b 961 end 962 if fis b 17 963 if ne vset b false 0 964 ret 5 965 end 966 ret aset b 967 end 968 if fis b 18 969 if ne vref b false 0 970 ret 5 971 end 972 ret aref b 973 end 974 if fis b 23 975 put st SBAD 0 976 x = fnum 3 977 if and and eq get st SRF 3 eq get st SBAD 0 fhex 4 978 ret 0 979 end 980 ret 5 981 end 982 ret 5 983 end 984 985 fn hsame h ulong to bool 986 o = get fo h 987 for j ulong 64 988 if ne get hd j get lb add o j 989 ret false 990 end 991 end 992 ret true 993 end 994 995 fn psame to bool 996 if not fhex 1 997 ret false 998 end 999 o = get fo 1 1000 for j ulong 64 1001 if ne get ph j get lb add o j 1002 ret false 1003 end 1004 end 1005 ret true 1006 end 1007 1008 note chain: the line's HASH is the sha256 of its bytes before " HASH" (else 1: the line itself is 1009 note damaged) and its PREV is the previous line's HASH (else 2: the line does not link). 0 when both hold. 1010 fn chain h ulong to int 1011 hreset 1012 m = sub get fo h 1 1013 for i ulong m 1014 x = hpush get lb i 1015 end 1016 y = hfinal 1017 if not hsame h 1018 ret 1 1019 end 1020 if not psame 1021 ret 2 1022 end 1023 ret 0 1024 end 1025 1026 note rline: format and chain checks of lb[0 .. n). 0 when the line belongs to the chain, 1 when the 1027 note line itself is damaged (could be a torn write), 2 when an intact line does not link. 1028 fn rline n ulong to int 1029 if not split n 1030 ret 1 1031 end 1032 nf = get st SNF 1033 if lt nf 4 1034 ret 1 1035 end 1036 h = sub nf 1 1037 if not fhex h 1038 ret 1 1039 end 1040 c = chain h 1041 if ne c 0 1042 ret c 1043 end 1044 put st SBAD 0 1045 sq = fnum 0 1046 if or ne get st SBAD 0 ne sq add get st SSEQ 1 1047 ret 2 1048 end 1049 put st SRF sub nf 3 1050 ret 0 1051 end 1052 1053 note rend: a complete line ends at file offset e. 0, or the code that refuses the journal. 1054 fn rend e ulong to int 1055 c = int 1 1056 if eq get st SLONG 0 1057 c = rline get st SLN 1058 end 1059 if ne c 0 1060 if and eq c 1 eq e get st SFSZ 1061 put st STORN 1 1062 ret 0 1063 end 1064 ret 5 1065 end 1066 put st SOVF 0 1067 r = rapply 1068 if ne get st SOVF 0 1069 ret 15 1070 end 1071 if ne r 0 1072 ret r 1073 end 1074 o = get fo sub get st SNF 1 1075 for j ulong 64 1076 put ph j get lb add o j 1077 end 1078 put st SSEQ add get st SSEQ 1 1079 put st SGOOD e 1080 put st SRECS add get st SRECS 1 1081 put st SLN 0 1082 ret 0 1083 end 1084 1085 note replay: read the journal; st SGOOD ends the last good line. 0, or a refusal code. 1086 fn replay to int 1087 fd = int get st SFD 1088 z = long 0 1089 fsz = fseek fd 0 2 1090 if lt fsz 0 1091 ret 3 1092 end 1093 put st SFSZ ulong fsz 1094 pos = ulong 0 1095 while lt pos ulong fsz 1096 r = pread fd rb 0 len rb long pos 1097 if ne r -4 1098 if le r 0 1099 ret 3 1100 end 1101 for i ulong ulong r 1102 c = get rb i 1103 if eq c 10 1104 e = rend add add pos i 1 1105 if ne e 0 1106 ret e 1107 end 1108 else 1109 n = get st SLN 1110 if lt n 1023 1111 put lb n c 1112 put st SLN add n 1 1113 else 1114 put st SLONG 1 1115 end 1116 end 1117 end 1118 pos = add pos ulong r 1119 end 1120 end 1121 ret 0 1122 end 1123 1124 note tornh: sha256 of the torn bytes [st SGOOD, st SFSZ) into th. 1125 fn tornh to bool 1126 fd = int get st SFD 1127 pos = get st SGOOD 1128 end1 = get st SFSZ 1129 hreset 1130 while lt pos end1 1131 m = sub end1 pos 1132 if gt m len rb 1133 m = len rb 1134 end 1135 r = pread fd rb 0 m long pos 1136 if ne r -4 1137 if le r 0 1138 ret false 1139 end 1140 for i ulong ulong r 1141 x = hpush get rb i 1142 end 1143 pos = add pos ulong r 1144 end 1145 end 1146 y = hfinal 1147 for j ulong 64 1148 put th j get hd j 1149 end 1150 ret true 1151 end 1152 1153 note wput: append to the write buffer wb (st SWB). 1154 fn wput c uchar to void 1155 n = get st SWB 1156 put wb n c 1157 put st SWB add n 1 1158 end 1159 1160 note wrec: append the record whose body is lb[0 .. st SLN) and fsync it. 0, or 3. 1161 fn wrec to int 1162 put st SWB 0 1163 v = add get st SSEQ 1 1164 p = ulong 1 1165 while le p div v 10 1166 p = mul p 10 1167 end 1168 while gt p 0 1169 wput uchar add 48 div v p 1170 v = rem v p 1171 p = div p 10 1172 end 1173 wput 32 1174 for i ulong 64 1175 wput get ph i 1176 end 1177 wput 32 1178 for i ulong get st SLN 1179 wput get lb i 1180 end 1181 hreset 1182 for i ulong get st SWB 1183 x = hpush get wb i 1184 end 1185 y = hfinal 1186 wput 32 1187 for i ulong 64 1188 wput get hd i 1189 end 1190 wput 10 1191 fd = int get st SFD 1192 off = get st SGOOD 1193 m = get st SWB 1194 o = ulong 0 1195 while lt o m 1196 w = pwrite fd wb o sub m o long add off o 1197 if ne w -4 1198 if le w 0 1199 ret 3 1200 end 1201 o = add o ulong w 1202 end 1203 end 1204 sy = fsync fd 1205 if ne sy 0 1206 ret 3 1207 end 1208 for i ulong 64 1209 put ph i get hd i 1210 end 1211 put st SSEQ add get st SSEQ 1 1212 put st SGOOD add off m 1213 put st SRECS add get st SRECS 1 1214 ret 0 1215 end 1216 1217 note conserve: sum of balances + open escrow = sum of prefunds; open escrow = open JOB amounts; 1218 note every settled JOB paid out exactly its amount. 1219 fn conserve to bool 1220 sum = ulong 0 1221 esc = ulong 0 1222 put st SOVF 0 1223 for k ulong get st SKEYS 1224 t = get kt k 1225 if eq t 0 1226 sum = cadd sum get d0 k 1227 end 1228 if eq t 1 1229 cph = get d4 k 1230 if eq cph 1 1231 esc = cadd esc get d3 k 1232 end 1233 if eq cph 2 1234 paid = cadd cadd get d5 k get d6 k get d7 k 1235 if ne paid get d3 k 1236 ret false 1237 end 1238 end 1239 end 1240 end 1241 if ne esc get st SESC 1242 ret false 1243 end 1244 if ne cadd sum esc get st SPRE 1245 ret false 1246 end 1247 ret eq get st SOVF 0 1248 end 1249 1250 note body: lb = arguments 3 .. c - 1 joined by spaces, split into fields; st SRF is the count. 1251 fn body c ulong to void 1252 put st SLN 0 1253 for i ulong c 1254 if ge i 3 1255 if gt i 3 1256 lbput 32 1257 end 1258 lbarg i 1259 end 1260 end 1261 if not split get st SLN 1262 die 2 1263 end 1264 put st SRF get st SNF 1265 end 1266 1267 note reply: word k, then the command's fields after the KIND, on stdout. 1268 fn reply k ulong to void 1269 put st SOB 0 1270 ofr k 1271 n = get st SNF 1272 if fis 0 15 1273 n = sub n 1 1274 end 1275 if fis 0 16 1276 n = sub n 2 1277 end 1278 if fis 0 17 1279 n = sub n 4 1280 end 1281 if fis 0 18 1282 n = sub n 1 1283 end 1284 for i ulong n 1285 if gt i 0 1286 oput 32 1287 ofld i 1288 end 1289 end 1290 oput 10 1291 oflush 1 1292 end 1293 1294 note wcmd: finish a write command whose check gave r: print the repeat word d, refuse, or 1295 note write the record, fsync it, and print the word w. 1296 fn wcmd r int w ulong d ulong to void 1297 if eq r 1 1298 reply d 1299 ret 1300 end 1301 if ne r 0 1302 if eq r 16 1303 authdie 1304 end 1305 die r 1306 end 1307 if ne wrec 0 1308 die 3 1309 end 1310 reply w 1311 end 1312 1313 fn cbal to void 1314 if or ne get st SRF 2 not fname 1 1315 die 2 1316 end 1317 a = kfind 0 1 1318 v = ulong 0 1319 if ne a 0 1320 v = get d0 sub a 1 1321 end 1322 put st SOB 0 1323 ofr 49 1324 ofld 1 1325 oput 32 1326 odec v 1327 oput 10 1328 oflush 1 1329 end 1330 1331 fn cshow to void 1332 if or ne get st SRF 2 not fhex 1 1333 die 2 1334 end 1335 j = kfind 1 1 1336 if eq j 0 1337 die 8 1338 end 1339 k = sub j 1 1340 p = get d4 k 1341 put st SOB 0 1342 ofr 33 1343 ofld 1 1344 oput 32 1345 if eq p 1 1346 ofr 27 1347 end 1348 if eq p 2 1349 ofr 29 1350 end 1351 if eq p 3 1352 ofr 31 1353 end 1354 oput 32 1355 okey get d0 k 1356 oput 32 1357 okey get d1 k 1358 oput 32 1359 okey get d2 k 1360 oput 32 1361 odec get d3 k 1362 if eq p 2 1363 oput 32 1364 orc k 1365 oput 32 1366 odec get d5 k 1367 oput 32 1368 odec get d6 k 1369 oput 32 1370 odec get d7 k 1371 end 1372 oput 10 1373 oflush 1 1374 end 1375 1376 fn caudit to void 1377 if ne get st SRF 1 1378 die 2 1379 end 1380 na = ulong 0 1381 nj = ulong 0 1382 no = ulong 0 1383 ns = ulong 0 1384 nr = ulong 0 1385 sum = ulong 0 1386 for k ulong get st SKEYS 1387 t = get kt k 1388 if eq t 0 1389 na = add na 1 1390 sum = add sum get d0 k 1391 end 1392 if eq t 1 1393 nj = add nj 1 1394 p = get d4 k 1395 if eq p 1 1396 no = add no 1 1397 end 1398 if eq p 2 1399 ns = add ns 1 1400 end 1401 if eq p 3 1402 nr = add nr 1 1403 end 1404 end 1405 end 1406 put st SOB 0 1407 ofr 34 1408 odec get st SRECS 1409 ofr 35 1410 for i ulong 64 1411 oput get ph i 1412 end 1413 ofr 36 1414 odec na 1415 ofr 37 1416 odec nj 1417 ofr 38 1418 odec no 1419 ofr 39 1420 odec ns 1421 ofr 40 1422 odec nr 1423 ofr 41 1424 odec get st SPRE 1425 ofr 42 1426 odec sum 1427 ofr 43 1428 odec get st SESC 1429 oput 10 1430 oflush 1 1431 end 1432 1433 note aeq: argument i equals word k. 1434 fn aeq i ulong k ulong to bool 1435 s = fst k 1436 n = get gl i 1437 if ne n get st SFLEN 1438 ret false 1439 end 1440 o = get gs i 1441 for j ulong n 1442 if ne get ga add o j get tw add s j 1443 ret false 1444 end 1445 end 1446 ret true 1447 end 1448 1449 note opendb: DIR (argument 2) made if absent (0700), DIR/journal opened O_RDWR | O_CREAT | 1450 note O_CLOEXEC = 524354, created 0600. 1451 fn opendb to void 1452 n = get gl 2 1453 if or eq n 0 gt n 4000 1454 die 2 1455 end 1456 o = get gs 2 1457 for j ulong n 1458 put pb j get ga add o j 1459 end 1460 md = makedir pb 0 n 448 1461 if and lt md 0 ne md -17 1462 die 3 1463 end 1464 s = fst 47 1465 for j ulong get st SFLEN 1466 put pb add n j get tw add s j 1467 end 1468 fd = fopen pb 0 add n get st SFLEN 524354 384 1469 if lt fd 0 1470 die 3 1471 end 1472 put st SFD ulong fd 1473 end 1474 1475 note dsync: fsync DIR so a newly created journal's directory entry is durable. 1476 note Flags: O_RDONLY | O_DIRECTORY (arm64 0x4000) | O_CLOEXEC = 540672. 1477 fn dsync to void 1478 dfd = fopen pb 0 get gl 2 540672 0 1479 if lt dfd 0 1480 die 3 1481 end 1482 if ne fsync int dfd 0 1483 die 3 1484 end 1485 x = fclose int dfd 1486 end 1487 1488 note recover: truncate a torn tail, then write genesis (empty journal) and the recover record. 1489 fn recover to void 1490 tb = sub get st SFSZ get st SGOOD 1491 if gt tb 0 1492 if not tornh 1493 die 3 1494 end 1495 fd = int get st SFD 1496 if ne ftrunc fd long get st SGOOD 0 1497 die 3 1498 end 1499 if ne fsync fd 0 1500 die 3 1501 end 1502 end 1503 if eq get st SSEQ 0 1504 put st SLN 0 1505 for i ulong get cfgn 0 1506 lbput get cfg i 1507 end 1508 if ne wrec 0 1509 die 3 1510 end 1511 dsync 1512 end 1513 if gt tb 0 1514 put st SLN 0 1515 lbfr 23 1516 lbput 32 1517 lbdec tb 1518 lbput 32 1519 for i ulong 64 1520 lbput get th i 1521 end 1522 if ne wrec 0 1523 die 3 1524 end 1525 put st SOB 0 1526 ofr 44 1527 odec tb 1528 ofr 45 1529 for i ulong 64 1530 oput get th i 1531 end 1532 oput 10 1533 oflush 2 1534 end 1535 end 1536 1537 fn main to int 1538 c = argcnt 1539 if or lt c 4 gt c 13 1540 put st SOB 0 1541 ofr 48 1542 oput 10 1543 oflush 2 1544 ret 2 1545 end 1546 o = ulong 0 1547 for i ulong ulong c 1548 r = argread i ga o sub len ga o 1549 if lt r 0 1550 die 2 1551 end 1552 put gs i o 1553 put gl i ulong r 1554 o = add o ulong r 1555 end 1556 if not aeq 1 46 1557 die 2 1558 end 1559 acfg 1560 for i ulong 64 1561 put ph i 48 1562 end 1563 opendb 1564 fd = int get st SFD 1565 lk = flock fd 2 1566 if ne lk 0 1567 die 4 1568 end 1569 rp = replay 1570 if ne rp 0 1571 die rp 1572 end 1573 recover 1574 if not conserve 1575 die 6 1576 end 1577 n = ulong c 1578 body n 1579 if fis 0 15 1580 wcmd vpre 0 true 25 26 1581 ret 0 1582 end 1583 if fis 0 16 1584 wcmd vres 0 true 27 28 1585 ret 0 1586 end 1587 if fis 0 17 1588 wcmd vset 0 true 29 30 1589 ret 0 1590 end 1591 if fis 0 18 1592 wcmd vref 0 true 31 32 1593 ret 0 1594 end 1595 if fis 0 19 1596 cbal 1597 ret 0 1598 end 1599 if fis 0 20 1600 cshow 1601 ret 0 1602 end 1603 if fis 0 21 1604 caudit 1605 ret 0 1606 end 1607 die 2 1608 ret 2 1609 end