interplane.Get Omega
Read in your language

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

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