interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / core / sovereign-core / om / tur-ledger / tur-ledger.om

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