interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / social / 30store.om

929 lines · 15543 bytes Raw Blame History
1note social/30store.om: the social store. ~/forge-data/social.log is the only source of truth:
2note one JSON event per line, {"seq":N,"ts":S,"did":"did:key:z…","action":A,"target":T,"prev":H}
3note where H = hex sha256 of the previous line's bytes (64 zeros for line 1). social.head holds
4note "<seq> <hex sha256 of line seq>\n" and is replaced (rename) after every append, so a changed,
5note dropped or truncated line anywhere is detected by soload, which then disables social (fail closed).
6note Memory: string intern pool (ids 1..), relation set (kind, actor, target), counts, accounts,
7note names, sessions, used nonces, per-DID write counter. All rebuilt from the log at start / SIGHUP.
8note ---- key buffer gskb and the intern pool
9fn sokclr to void
10put gskbl 0 0
11put gskov 0 0
12end
13fn sokc c uchar to void
14n = get gskbl 0
15if ge n len gskb
16put gskov 0 1
17ret
18end
19put gskb n c
20put gskbl 0 add n 1
21end
22fn sokt t view uchar * to void
23for i ulong len t bound 512
24sokc get t i
25end
26end
27note sokid: append the bytes of string id
28fn sokid id ulong to void
29o = get gsso id
30for i ulong get gssl id bound 4096
31sokc get gsstr add o i
32end
33end
34fn sokhash to ulong
35h = ulong 14695981039346656037
36for i ulong get gskbl 0 bound 512
37h = xor h ulong get gskb i
38h = mul h 1099511628211
39end
40ret h
41end
42fn sokeq id ulong to bool
43if ne get gssl id get gskbl 0
44ret false
45end
46o = get gsso id
47for i ulong get gskbl 0 bound 512
48if ne get gsstr add o i get gskb i
49ret false
50end
51end
52ret true
53end
54fn soslot to ulong
55m = sub len gssh 1
56s = and sokhash m
57for it ulong len gssh
58id = get gssh s
59if eq id 0
60ret s
61end
62if sokeq id
63ret s
64end
65s = and add s 1 m
66end
67ret len gssh
68end
69note sofind: id of the string in gskb, 0 when absent
70fn sofind to ulong
71if ne get gskov 0 0
72ret 0
73end
74s = soslot
75if ge s len gssh
76ret 0
77end
78ret get gssh s
79end
80note sointern: id of the string in gskb, created when absent; 0 when the pool is full
81fn sointern to ulong
82if ne get gskov 0 0
83ret 0
84end
85s = soslot
86if ge s len gssh
87ret 0
88end
89id = get gssh s
90if ne id 0
91ret id
92end
93id = get gsns 0
94kl = get gskbl 0
95o = get gsstrl 0
96if or ge id len gsso gt add o kl len gsstr
97ret 0
98end
99for i ulong kl bound 512
100put gsstr add o i get gskb i
101end
102put gsso id o
103put gssl id kl
104put gsstrl 0 add o kl
105put gsns 0 add id 1
106put gssh s id
107ret id
108end
109note sokpre: gskb starts with t
110fn sokpre t view uchar * to bool
111if lt get gskbl 0 len t
112ret false
113end
114for i ulong len t bound 512
115if ne get gskb i get t i
116ret false
117end
118end
119ret true
120end
121note ---- relations: kind 1 star, 2 fav, 3 watch, 4 follow; actor and target are string ids
122fn sorkey kind ulong a ulong b ulong to ulong
123ret or or shl kind 48 shl a 24 b
124end
125fn sorslot k ulong to ulong
126m = sub len gsrk 1
127s = and shr mul k 11400714819323198485 40 m
128for it ulong len gsrk
129v = get gsrk s
130if or eq v 0 eq v k
131ret s
132end
133s = and add s 1 m
134end
135ret len gsrk
136end
137fn soron kind ulong a ulong b ulong to bool
138k = sorkey kind a b
139s = sorslot k
140if ge s len gsrk
141ret false
142end
143ret and eq get gsrk s k ne get gsrv s 0
144end
145note soset: set the relation; true when it changed (counts follow)
146fn soset kind ulong a ulong b ulong on bool to bool
147k = sorkey kind a b
148s = sorslot k
149if ge s len gsrk
150ret false
151end
152cur = and eq get gsrk s k ne get gsrv s 0
153if and cur on
154ret false
155end
156if and not cur not on
157ret false
158end
159put gsrk s k
160v = uchar 0
161if on
162v = uchar 1
163end
164put gsrv s v
165sobump kind a b on
166ret true
167end
168fn soinc v ulong on bool to ulong
169if on
170ret add v 1
171end
172if eq v 0
173ret v
174end
175ret sub v 1
176end
177fn sobump kind ulong a ulong b ulong on bool to void
178if eq kind 1
179put gscst b soinc get gscst b on
180end
181if eq kind 2
182put gscfv b soinc get gscfv b on
183end
184if eq kind 3
185put gscwt b soinc get gscwt b on
186end
187if eq kind 4
188put gscfl b soinc get gscfl b on
189put gscfg a soinc get gscfg a on
190end
191end
192note ---- strict line parser over ln (cursor gspc, failure flag gsbad)
193fn sofail to void
194put gsbad 0 1
195end
196fn solit ln view uchar * t view uchar * to void
197p = get gspc 0
198if gt add p len t len ln
199sofail
200ret
201end
202for i ulong len t bound 64
203if ne get ln add p i get t i
204sofail
205ret
206end
207end
208put gspc 0 add p len t
209end
210fn sonum ln view uchar * to ulong
211p = get gspc 0
212v = ulong 0
213k = ulong 0
214for it ulong 16
215if ge add p k len ln
216break
217end
218c = get ln add p k
219if or lt c 48 gt c 57
220break
221end
222v = add mul v 10 ulong sub c 48
223k = add k 1
224end
225if or eq k 0 gt k 15
226sofail
227end
228if and gt k 1 eq get ln p 48
229sofail
230end
231put gspc 0 add p k
232ret v
233end
234fn sostrc c uchar to bool
235if and ge c 97 le c 122
236ret true
237end
238if and ge c 65 le c 90
239ret true
240end
241if and ge c 48 le c 58
242ret true
243end
244ret or or eq c 45 eq c 46 or eq c 95 eq c 47
245end
246note sostr: append the string at the cursor (charset [A-Za-z0-9:._/-]) to gskb up to the closing quote
247fn sostr ln view uchar * to ulong
248p = get gspc 0
249k = ulong 0
250for it ulong 400
251if ge add p k len ln
252sofail
253ret k
254end
255c = get ln add p k
256if eq c 34
257break
258end
259if not sostrc c
260sofail
261ret k
262end
263sokc c
264k = add k 1
265end
266put gspc 0 add p k
267ret k
268end
269note soact: action code of the bytes at the cursor (1..12), 0 when unknown
270fn soact ln view uchar * to ulong
271a = ulong 0
272a = sotrya ln a tsastar 1
273a = sotrya ln a tsaunst 2
274a = sotrya ln a tsafav 3
275a = sotrya ln a tsaunfv 4
276a = sotrya ln a tsawat 5
277a = sotrya ln a tsaunwt 6
278a = sotrya ln a tsafol 7
279a = sotrya ln a tsaunfl 8
280a = sotrya ln a tsaccnt 9
281a = sotrya ln a tsaname 10
282a = sotrya ln a tsasess 11
283a = sotrya ln a tsalogo 12
284if eq a 0
285sofail
286ret 0
287end
288put gspc 0 add get gspc 0 soatx a false
289ret a
290end
291fn sotrya ln view uchar * a ulong t view uchar * code ulong to ulong
292if ne a 0
293ret a
294end
295p = get gspc 0
296if gt add p add len t 1 len ln
297ret a
298end
299for i ulong len t bound 16
300if ne get ln add p i get t i
301ret a
302end
303end
304if ne get ln add p len t 34
305ret a
306end
307ret code
308end
309note soatx: length of action a's name; when w, also append it to the log line
310fn soatx a ulong w bool to ulong
311if eq a 1
312ret soatw tsastar w
313end
314if eq a 2
315ret soatw tsaunst w
316end
317if eq a 3
318ret soatw tsafav w
319end
320if eq a 4
321ret soatw tsaunfv w
322end
323if eq a 5
324ret soatw tsawat w
325end
326if eq a 6
327ret soatw tsaunwt w
328end
329if eq a 7
330ret soatw tsafol w
331end
332if eq a 8
333ret soatw tsaunfl w
334end
335if eq a 9
336ret soatw tsaccnt w
337end
338if eq a 10
339ret soatw tsaname w
340end
341if eq a 11
342ret soatw tsasess w
343end
344ret soatw tsalogo w
345end
346fn soatw t view uchar * w bool to ulong
347if w
348solt t
349end
350ret len t
351end
352note soprevok: the 64 hex chars at the cursor equal hex(gsprev)
353fn soprevok ln view uchar * to void
354p = get gspc 0
355if gt add p 64 len ln
356sofail
357ret
358end
359for i ulong 32
360b = ulong get gsprev i
361if ne get ln add p mul i 2 sohexc shr b 4
362sofail
363end
364if ne get ln add add p mul i 2 1 sohexc and b 15
365sofail
366end
367end
368put gspc 0 add p 64
369end
370note soapply: parse and apply one line (no newline); false = corrupt (state may be partly applied)
371fn soapply ln view uchar * to bool
372put gsbad 0 0
373put gspc 0 0
374solit ln tsl1
375seq = sonum ln
376solit ln tsl2
377ts = sonum ln
378solit ln tsl3
379sokclr
380sokt tsdidp
381dl = sostr ln
382if or lt dl 40 gt dl 60
383sofail
384end
385did = sointern
386solit ln tsl4
387a = soact ln
388solit ln tsl5
389sokclr
390tl = sostr ln
391solit ln tsl6
392soprevok ln
393solit ln tsl7
394if or ne get gspc 0 len ln ne seq add get gsseq 0 1
395sofail
396end
397if or eq did 0 ne get gsbad 0 0
398ret false
399end
400if not soevent a did ts
401ret false
402end
403sochain ln seq
404ret true
405end
406fn sochain ln view uchar * seq ulong to void
407c = new ulong 40 0
408h = new uchar 32 0
409sha256_init c
410sha256_update c ln
411sha256_final c h
412for i ulong 32
413put gsprv2 i get gsprev i
414put gsprev i get h i
415end
416put gsseq 0 seq
417end
418note soevent: apply action a by did at ts with the target in gskb
419fn soevent a ulong did ulong ts ulong to bool
420soratev did ts
421if le a 6
422if not sokpre tsradp
423if or fradsrc not sokpre tsgitp
424ret false
425end
426end
427t = sointern
428if eq t 0
429ret false
430end
431x = soset add div sub a 1 2 1 did t eq rem a 2 1
432ret true
433end
434if le a 8
435if not sokpre tsdidp
436ret false
437end
438t = sointern
439if or eq t 0 eq t did
440ret false
441end
442x = soset 4 did t eq a 7
443ret true
444end
445if eq a 9
446ret soevacc did
447end
448if eq a 10
449ret soevname did
450end
451if eq a 11
452ret soevsess did ts
453end
454ret soevlogo did
455end
456fn soevacc did ulong to bool
457if ne get gskbl 0 0
458ret false
459end
460put gsacc did 1
461ret true
462end
463fn soevname did ulong to bool
464if not sonameok
465ret false
466end
467n = sointern
468if eq n 0
469ret false
470end
471o = get gsnmo n
472if and ne o 0 ne o did
473ret false
474end
475old = get gsnm did
476if ne old 0
477put gsnmo old 0
478end
479put gsnm did n
480put gsnmo n did
481ret true
482end
483note sonameok: gskb is a valid display name [a-z0-9-]{1,39}, no leading/trailing '-'
484fn sonameok to bool
485n = get gskbl 0
486if or eq n 0 gt n 39
487ret false
488end
489if or eq get gskb 0 45 eq get gskb sub n 1 45
490ret false
491end
492for i ulong n bound 64
493c = get gskb i
494ok = or and ge c 97 le c 122 and ge c 48 le c 57
495if not or ok eq c 45
496ret false
497end
498end
499ret true
500end
501note session target "<64 hex session hash>.<32 hex nonce>"
502fn soevsess did ulong ts ulong to bool
503if ne get gskbl 0 97
504ret false
505end
506if ne get gskb 64 46
507ret false
508end
509nb = new uchar 34 0
510put nb 0 110
511put nb 1 58
512for i ulong 32
513put nb add i 2 get gskb add i 65
514end
515put gskbl 0 64
516s = sointern
517sokclr
518sokt nb
519n = sointern
520if or eq s 0 eq n 0
521ret false
522end
523put gssd s did
524put gsse s add ts 2592000
525put gsnu n 1
526ret true
527end
528fn soevlogo did ulong to bool
529if ne get gskbl 0 64
530ret false
531end
532s = sofind
533if ne s 0
534if eq get gssd s did
535put gssd s 0
536end
537end
538ret true
539end
540note ---- per-DID write counter (rebuilt from the log; 30 writes per minute)
541fn soratev did ulong ts ulong to void
542m = div ts 60
543if eq get gsrlm did m
544put gsrlc did add get gsrlc did 1
545ret
546end
547put gsrlm did m
548put gsrlc did 1
549end
550fn sordok did ulong to bool
551m = div get gsnow 0 60
552if ne get gsrlm did m
553ret true
554end
555ret lt get gsrlc did 30
556end
557note ---- reset and replay
558fn soreset to void
559for i ulong len gssh
560put gssh i 0
561end
562for i ulong len gsrk
563put gsrk i 0
564put gsrv i 0
565end
566for i ulong len gsso
567soclrid i
568end
569put gsns 0 1
570put gsstrl 0 0
571put gsseq 0 0
572for i ulong 32
573put gsprev i 0
574put gsprv2 i 0
575end
576end
577fn soclrid i ulong to void
578put gscst i 0
579put gscfv i 0
580put gscwt i 0
581put gscfl i 0
582put gscfg i 0
583put gsacc i 0
584put gsnm i 0
585put gsnmo i 0
586put gssd i 0
587put gsse i 0
588put gsnu i 0
589put gsrlm i 0
590put gsrlc i 0
591end
592note soreplay: 0 when every line parses and chains; else -(line number); -1000000-errno on read error
593fn soreplay to long
594sopdd
595sopt tslog
596fd = fopen gspth 0 get gspthl 0 524288 0
597if eq fd -2
598ret 0
599end
600if lt fd 0
601ret sub -1000000 fd
602end
603ln = new uchar 1024 0
604n = ulong 0
605line = ulong 0
606for it ulong 1000000
607r = read int fd gsrb 0 len gsrb
608if lt r 0
609x = fclose int fd
610ret sub -1000000 r
611end
612if eq r 0
613break
614end
615for i ulong ulong r
616c = get gsrb i
617if eq c 10
618line = add line 1
619if not soapply part ln 0 n
620x = fclose int fd
621ret neg long line
622end
623n = ulong 0
624else
625if ge n 1023
626x = fclose int fd
627ret neg long add line 1
628end
629put ln n c
630n = add n 1
631end
632end
633end
634x = fclose int fd
635if ne n 0
636ret neg long add line 1
637end
638ret 0
639end
640note sohdchk: social.head agrees with the log (absent head = "0 <zeros>"); the head may lag by
641note one line (crash between append and head rename) and is then rewritten
642fn sohdchk to bool
643sopdd
644sopt tshead
645fd = fopen gspth 0 get gspthl 0 524288 0
646hs = ulong 0
647hl = long 0
648hd = new uchar 128 0
649if ge fd 0
650hl = read int fd gshd 0 len gshd
651x = fclose int fd
652if or lt hl 66 gt hl 90
653ret false
654end
655end
656if lt fd 0
657if ne fd -2
658ret false
659end
660end
661if ge fd 0
662put gspc 0 0
663put gsbad 0 0
664note hd: a local copy, since a global is never a function argument
665for i ulong ulong hl
666put hd i get gshd i
667end
668hs = sonum part hd 0 ulong hl
669if ne get gsbad 0 0
670ret false
671end
672end
673ret sohdcmp hs ulong hl
674end
675fn sohdcmp hs ulong hl ulong to bool
676s = get gsseq 0
677p = get gspc 0
678if eq hs s
679if eq s 0
680ret true
681end
682ret sohdhex p hl 0
683end
684if ne add hs 1 s
685ret false
686end
687if ne hs 0
688if not sohdhex p hl 1
689ret false
690end
691end
692ret sohdput
693end
694note sohdhex: gshd[p] is ' ', then 64 hex of gsprev (w 0) or gsprv2 (w 1), then '\n' at the end
695fn sohdhex p ulong hl ulong w ulong to bool
696if ne hl add p 66
697ret false
698end
699if or ne get gshd p 32 ne get gshd add p 65 10
700ret false
701end
702d = new uchar 32 0
703for i ulong 32
704v = get gsprev i
705if eq w 1
706v = get gsprv2 i
707end
708put d i v
709end
710x = new uchar 64 0
711sohex d x
712for i ulong 64
713if ne get gshd add add p 1 i get x i
714ret false
715end
716end
717ret true
718end
719note sohdput: write social.head.tmp then rename over social.head
720fn sohdput to bool
721d = new uchar 96 0
722n = sodec get gsseq 0 d
723put d n 32
724x = new uchar 64 0
725soprevhx x
726for i ulong 64
727put d add add n 1 i get x i
728end
729put d add n 65 10
730sopdd
731sopt tshtmp
732r = unlink gspth 0 get gspthl 0
733if not sowfile part d 0 add n 66
734ret false
735end
736soavb
737soavpth
738sopdd
739sopt tshead
740soavpth
741r = renames gspk 0 get gspkl 0 0
742ret eq r 0
743end
744note soprevhx: 64 lowercase hex chars of gsprev into x
745fn soprevhx x edit uchar * to void
746for i ulong 32
747b = ulong get gsprev i
748put x mul i 2 sohexc shr b 4
749put x add mul i 2 1 sohexc and b 15
750end
751end
752note sodec: decimal of v into d at 0; returns the length
753fn sodec v ulong d edit uchar * to ulong
754t = new uchar 20 0
755k = ulong 0
756x = v
757for it ulong 20
758put t k uchar add rem x 10 48
759k = add k 1
760x = div x 10
761if eq x 0
762break
763end
764end
765for i ulong k
766put d i get t sub sub k 1 i
767end
768ret k
769end
770note ---- appending one event (single writer; flock guards against a second instance)
771fn solc c uchar to void
772n = get gslnl 0
773if lt n len gsln
774put gsln n c
775put gslnl 0 add n 1
776end
777end
778fn solt t view uchar * to void
779for i ulong len t bound 512
780solc get t i
781end
782end
783fn solnum v ulong to void
784d = new uchar 20 0
785k = sodec v d
786for i ulong k
787solc get d i
788end
789end
790note soemit: append event a by string id did (a "did:key:" string) with the target in gskb
791fn soemit a ulong did ulong to bool
792if eq get gsok 0 0
793ret false
794end
795put gslnl 0 0
796solt tsl1
797solnum add get gsseq 0 1
798solt tsl2
799solnum get gsnow 0
800solt tsl3
801o = get gsso did
802for i ulong sub get gssl did len tsdidp bound 64
803solc get gsstr add add o len tsdidp i
804end
805solt tsl4
806x = soatx a true
807solt tsl5
808for i ulong get gskbl 0 bound 512
809solc get gskb i
810end
811solt tsl6
812hx = new uchar 64 0
813soprevhx hx
814solt hx
815solt tsl7
816ret soemitw
817end
818fn soemitw to bool
819n = get gslnl 0
820if ge n sub len gsln 1
821ret false
822end
823ln = new uchar 1024 0
824for i ulong n
825put ln i get gsln i
826end
827put gsln n 10
828sopdd
829sopt tslog
830fd = fopen gspth 0 get gspthl 0 525377 384
831if lt fd 0
832ret false
833end
834l = flock int fd 2
835w = write int fd gsln 0 add n 1
836s = fdsync int fd
837u = flock int fd 8
838x = fclose int fd
839if ne w long add n 1
840put gsok 0 0
841ret false
842end
843if not soapply part ln 0 n
844put gsok 0 0
845ret false
846end
847if not sohdput
848put gsok 0 0
849ret false
850end
851ret true
852end
853note ---- load (start and SIGHUP)
854fn sokeyld to bool
855k = new uchar 32 0
856sopdd
857sopt tskeyf
858fd = fopen gspth 0 get gspthl 0 524288 0
859if eq fd -2
860if not sorand k
861ret false
862end
863if not sowfile k
864ret false
865end
866fd = fopen gspth 0 get gspthl 0 524288 0
867end
868if lt fd 0
869ret false
870end
871r = read int fd gskey 0 32
872x = fclose int fd
873ret eq r 32
874end
875fn soownb to void
876sokclr
877sokt tsdidp
878sokt tsowndid
879d = sointern
880put gsown 0 d
881sokclr
882sokt tfowner
883n = sointern
884put gsnm d n
885put gsnmo n d
886end
887note soload: 0 = loaded and verified (social on); <0 = social disabled (log tamper or I/O error)
888fn soload to long
889put gsok 0 0
890sodirset
891sopclr
892for i ulong sub get gsddl 0 1 bound 512
893sopc get gsdd i
894end
895r = makedir gspth 0 get gspthl 0 448
896sopdd
897sopt tstmpd
898r = makedir gspth 0 get gspthl 0 448
899if not sokeyld
900flog tsekey
901ret -1
902end
903soreset
904soownb
905e = soreplay
906if ne e 0
907soreplerr e
908ret e
909end
910if not sohdchk
911flog tsehead
912ret -2
913end
914put gsok 0 1
915ret 0
916end
917fn soreplerr e long to void
918d = new uchar 64 0
919for i ulong len tsetamp
920put d i get tsetamp i
921end
922v = ulong 0
923if lt e 0
924v = ulong neg e
925end
926n = add len tsetamp sodec v part d len tsetamp 20
927put d n 10
928flog part d 0 add n 1
929end