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