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 / apps / interplane-forge / community / 00core.om

588 lines · 12506 bytes Raw Blame History
1note community/00core.om -- the community runner hub (protocol ipcc1, spark-connector docs/PROTOCOL.md).
2note Keys register with a signed message; a registered key asks for a job, reruns a published salvage
3note part on its own machine and posts a signed result; the hub checks the signature, the job, the
4note revision and the compiler, stores every accepted message and appends one ledger line, and
5note records every refusal with its reason. The hub never executes, compiles or opens anything a
6note runner sends: messages are parsed as fixed-form text, and every file name built from one is
7note lowercase hex of a fixed length below the store root.
8note Store: INTERPLANE_COMMUNITY_DIR (absolute, ending in /); the slice answers "off" without it.
9note Composed after om/tls/ec.om (ecdsa_verify_raw) and the salvage slice (published parts).
10
11note cmgv: scalar cells
12global cmgv ulong 32
13global cmroot uchar 1024
14global cmproot uchar 1024
15global cmpth uchar 4096
16note cmfo / cmfn: offset and length in fbody of each parsed field (index constants cmf*)
17global cmfo ulong 16
18global cmfn ulong 16
19note cmpub: the sender's public point; cmkid: its key id; cmsha: the message SHA-256 (hex)
20global cmpub uchar 65
21global cmkid uchar 64
22global cmsha uchar 64
23global cmpick uchar 64
24
25const cmrl ulong 0
26const cmpl ulong 1
27const cmkind ulong 2
28const cmsigo ulong 3
29const cmboot ulong 4
30const cmon ulong 5
31const cmcur ulong 6
32const cmlock ulong 7
33const cmcn ulong 8
34const cmck ulong 9
35const cmcg ulong 10
36const cmctie ulong 11
37const cmchub ulong 12
38const cmcsec ulong 13
39const cmrate ulong 14
40const cmdj ulong 20
41const cmdm ulong 21
42const cmlen ulong 22
43const cmrw ulong 27
44const cmrc ulong 28
45note a ledger or refusal log is set aside as <name>.<seconds> once it reaches 4 MiB (cm_app)
46const cmrotb ulong 4194304
47const cmprl ulong 29
48
49note field indexes
50const cmfkey ulong 0
51const cmftime ulong 1
52const cmfthird ulong 2
53const cmfjob ulong 3
54const cmfpart ulong 4
55const cmfcomp ulong 5
56const cmfbin ulong 6
57const cmfout ulong 7
58const cmfexit ulong 8
59const cmfsig ulong 9
60
61note protocol bounds (form and meaning, not volume caps): a message is at most 4096 bytes (the
62note longest valid one is under 1 KiB), clock skew 300 s (replay window of signed messages), a job
63note answers within 3600 s. Volume is watched, not refused: alarm thresholds per key 150 signed
64note messages (after the signature and replay checks) an hour, all keys 3600, refusals 3600 (cm_tally);
65note the edge (nginx) limits request rate.
66const cmmaxb ulong 4096
67const cmskew ulong 300
68const cmjlife ulong 3600
69const cmakey ulong 150
70const cmaall ulong 3600
71const cmaref ulong 3600
72text cmtalm "alarms"
73text cmtalw " ALARM "
74text cmtalp " passed "
75text cmtalh " in the hour: "
76text cmtamsg "signed-messages"
77text cmtaref "refusals"
78text cmtrfn "refused"
79
80text cmenv "INTERPLANE_COMMUNITY_DIR="
81text cmpenv "INTERPLANE_COMMUNITY_PUB="
82text cmdirs "keys/jobs/used/msgs/parts/rate/"
83text cmtlk "writer.lock"
84text cmtkeys "keys/"
85text cmtjobs "jobs/"
86text cmtused "used/"
87text cmtmsgs "msgs/"
88text cmtparts "parts/"
89text cmtrate "rate/"
90text cmtall "all"
91text cmtk "/k/"
92text cmtg "/g/"
93text cmtv ".v"
94text cmtled "ledger"
95text cmtref "refused"
96text cmtdeny "deny"
97text cmtnew ".new"
98text cmtip "ipcc1 "
99text cmtreg "register"
100text cmtnext "next"
101text cmtres "result"
102text cmtlv "leave"
103text cmnpub "pub"
104text cmnkey "key"
105text cmntime "time"
106text cmnrate "rate"
107text cmnnon "nonce"
108text cmnjob "job"
109text cmnpart "part"
110text cmncomp "compiler"
111text cmnbin "binary"
112text cmnout "stdout"
113text cmnexit "exit"
114text cmnsig "sig"
115text cmhex "0123456789abcdef"
116text cmdkey "key "
117text cmdpart "part "
118text cmxsig "signal"
119text cmxtmo "timeout"
120text cmxomc "omc"
121text cmxomcs "omcsignal"
122text cmxomct "omctimeout"
123text cmxcell "cell"
124text cmsp " "
125text cmnl "\n"
126text cmdash "-"
127
128note refusal reasons (docs/PROTOCOL.md)
129const cmemal ulong 1
130const cmesize ulong 2
131const cmeclk ulong 3
132const cmesig ulong 4
133const cmeunr ulong 5
134const cmeden ulong 6
135const cmejob ulong 7
136const cmemis ulong 8
137const cmedup ulong 9
138const cmeoff ulong 12
139const cmeio ulong 13
140text cmw1 "malformed"
141text cmw2 "size"
142text cmw3 "clock"
143text cmw4 "signature"
144text cmw5 "unregistered"
145text cmw6 "denied"
146text cmw7 "job"
147text cmw8 "mismatch"
148text cmw9 "duplicate"
149text cmw12 "off"
150text cmw13 "store"
151
152note ---- small helpers
153fn cm_now to ulong
154ret ulong div realns 1000000000
155end
156
157fn cm_hv c uchar to ulong
158if and ge c 48 le c 57
159ret sub ulong c 48
160end
161if and ge c 97 le c 102
162ret sub ulong c 87
163end
164ret 16
165end
166
167fn cm_ishex s view uchar * to bool
168for i ulong len s bound 65536
169if gt cm_hv get s i 15
170ret false
171end
172end
173ret true
174end
175
176fn cm_eq a view uchar * b view uchar * to bool
177if ne len a len b
178ret false
179end
180for i ulong len a bound 65536
181if ne get a i get b i
182ret false
183end
184end
185ret true
186end
187
188fn cm_tohex s view uchar * d edit uchar * to void
189for i ulong len s bound 32768
190put d mul i 2 get cmhex ulong shr get s i 4
191put d add mul i 2 1 get cmhex ulong and get s i 15
192end
193end
194
195fn cm_unhex s view uchar * d edit uchar * to bool
196if ne len s mul len d 2
197ret false
198end
199for i ulong len d bound 32768
200a = cm_hv get s mul i 2
201b = cm_hv get s add mul i 2 1
202if or gt a 15 gt b 15
203ret false
204end
205put d i uchar or shl a 4 b
206end
207ret true
208end
209
210note cm_dec: a decimal of 1..19 digits, no leading zero unless "0"; 2^64-1 when malformed
211fn cm_dec s view uchar * to ulong
212bad = ulong 18446744073709551615
213if or eq len s 0 gt len s 19
214ret bad
215end
216if and gt len s 1 eq get s 0 48
217ret bad
218end
219v = ulong 0
220for i ulong len s bound 19
221c = get s i
222if or lt c 48 gt c 57
223ret bad
224end
225v = add mul v 10 sub ulong c 48
226end
227ret v
228end
229
230fn cm_decput d edit uchar * o ulong v ulong to ulong
231t = new uchar 20 0
232i = ulong 20
233x = v
234for it ulong 20
235i = sub i 1
236put t i uchar add rem x 10 48
237x = div x 10
238if eq x 0
239break
240end
241end
242k = o
243for j ulong sub 20 i bound 20
244put d k get t add i j
245k = add k 1
246end
247ret k
248end
249
250fn cm_cp d edit uchar * o ulong s view uchar * to ulong
251for i ulong len s bound 65536
252put d add o i get s i
253end
254ret add o len s
255end
256
257note ---- paths: cmpth = root, then appended pieces
258fn cm_p0 to void
259n = get cmgv cmrl
260for i ulong n bound 1024
261put cmpth i get cmroot i
262end
263put cmgv cmpl n
264end
265
266fn cm_pa s view uchar * to void
267n = get cmgv cmpl
268for i ulong len s bound 4096
269put cmpth add n i get s i
270end
271put cmgv cmpl add n len s
272end
273
274note cm_pcp: a copy of cmpth into d; its length
275fn cm_pcp d edit uchar * to ulong
276n = get cmgv cmpl
277for i ulong n bound 4096
278put d i get cmpth i
279end
280ret n
281end
282
283fn cm_pkid to void
284k = new uchar 64 0
285for i ulong 64
286put k i get cmkid i
287end
288cm_pa k
289end
290
291note ---- files under cmpth
292fn cm_rd b edit uchar * to long
293fd = fopen cmpth 0 get cmgv cmpl 557056 0
294if lt fd 0
295ret fd
296end
297n = ulong 0
298rc = long 0
299while lt n len b bound 9223372036854775807
300r = read int fd b n sub len b n
301if lt r 0
302rc = r
303break
304end
305if eq r 0
306break
307end
308n = add n ulong r
309end
310x = fclose int fd
311if lt rc 0
312ret rc
313end
314ret long n
315end
316
317fn cm_wfd fd long d view uchar * to bool
318n = ulong 0
319while lt n len d bound 9223372036854775807
320r = write int fd d n sub len d n
321if le r 0
322ret false
323end
324n = add n ulong r
325end
326ret true
327end
328
329note cm_wnew: create cmpth with O_EXCL (0600) holding d; false when it exists or fails
330fn cm_wnew d view uchar * to bool
331fd = fopen cmpth 0 get cmgv cmpl 557249 384
332if lt fd 0
333ret false
334end
335ok = cm_wfd fd d
336s = fsync int fd
337x = fclose int fd
338ret and ok eq s 0
339end
340
341note cm_app: append d to cmpth (created 0600 when absent); one write per line keeps lines whole.
342note A log that has reached cmrotb bytes is first renamed to <name>.<unix seconds> (kept, served at
343note /community/<name>.<seconds>, never deleted), so no file grows toward forged's file-size limit;
344note the store's size is the operator's to watch (the gate measures it).
345fn cm_app d view uchar * to bool
346sb = new uchar 128 0
347fd = fopen cmpth 0 get cmgv cmpl 558145 384
348if lt fd 0
349ret false
350end
351r = fstat int fd sb 0 128
352sz = ulong 0
353for i ulong 8
354sz = or sz shl ulong get sb add 48 i mul i 8
355end
356if and eq r 0 ge sz cmrotb
357x = fclose int fd
358if not cm_rot
359ret false
360end
361fd = fopen cmpth 0 get cmgv cmpl 558145 384
362if lt fd 0
363ret false
364end
365end
366ok = cm_wfd fd d
367x = fclose int fd
368ret ok
369end
370
371note cm_rot: rename cmpth to cmpth.<unix seconds> (no replacement); cmpth is unchanged afterwards
372fn cm_rot to bool
373a = new uchar 4100 0
374b = new uchar 4130 0
375p = new uchar 8300 0
376n = cm_pcp a
377k = cm_cp b 0 part a 0 n
378put b k 46
379k = cm_decput b add k 1 cm_now
380put p 0 2
381put p 1 0
382o = cm_pk p 2 part a 0 n
383o = cm_pk p o part b 0 k
384r = renames p 0 o 1
385ret or eq r 0 eq r -17
386end
387
388note cm_repl: write d to "<cmpth>.new", fsync, then rename it over cmpth (records)
389fn cm_repl d view uchar * to bool
390ret cm_repl2 d true
391end
392
393note cm_replq: the same without fsync, for volume counters (a count lost in a crash is harmless)
394fn cm_replq d view uchar * to bool
395ret cm_repl2 d false
396end
397
398fn cm_repl2 d view uchar * sync bool to bool
399a = new uchar 4100 0
400b = new uchar 4100 0
401p = new uchar 8300 0
402n = cm_pcp a
403cm_pa cmtnew
404nb = cm_pcp b
405fd = fopen cmpth 0 get cmgv cmpl 557633 384
406if lt fd 0
407ret false
408end
409ok = cm_wfd fd d
410s = long 0
411if sync
412s = fsync int fd
413end
414x = fclose int fd
415if not and ok eq s 0
416ret false
417end
418put p 0 2
419put p 1 0
420k = cm_pk p 2 part b 0 nb
421ret cm_ren p k part a 0 n
422end
423
424fn cm_ren p edit uchar * k ulong b view uchar * to bool
425o = cm_pk p k b
426r = renames p 0 o 0
427ret eq r 0
428end
429
430fn cm_pk p edit uchar * o ulong s view uchar * to ulong
431put p o uchar and len s 255
432put p add o 1 uchar shr len s 8
433for i ulong len s bound 65536
434put p add add o 2 i get s i
435end
436ret add add o 2 len s
437end
438
439fn cm_exists to bool
440ret eq faccess cmpth 0 get cmgv cmpl 0 0
441end
442
443fn cm_mkdir to bool
444r = makedir cmpth 0 get cmgv cmpl 448
445ret or eq r 0 eq r -17
446end
447
448note ---- start-up: the store root and the read-only root from the environment, the store's
449note directories, and the salvage store. INTERPLANE_COMMUNITY_DIR is the writable store (records).
450note INTERPLANE_COMMUNITY_PUB holds what the hub only reads: dist/ (the join script, the runner,
451note the compiler, SHA256SUMS: what volunteers download) and deny (the operator's list). In
452note production it is a host folder outside forge-data, bound read-only at /community directly
453note under the sandbox's read-only root, so no path the server can write or rename leads to it.
454note Both are required; without either the slice is off.
455fn cm_on to bool
456if eq get cmgv cmboot 1
457ret eq get cmgv cmon 1
458end
459put cmgv cmboot 1
460put cmgv cmon 0
461if not slboot
462ret false
463end
464b = new uchar 1024 0
465n = cm_env cmenv b
466if or lt n 2 gt n 900
467ret false
468end
469if not cm_rootok part b 0 ulong n
470ret false
471end
472for i ulong ulong n bound 1024
473put cmroot i get b i
474end
475put cmgv cmrl ulong n
476n = cm_env cmpenv b
477if or lt n 2 gt n 900
478ret false
479end
480if not cm_rootok part b 0 ulong n
481ret false
482end
483for i ulong ulong n bound 1024
484put cmproot i get b i
485end
486put cmgv cmprl ulong n
487if not cm_mkall
488ret false
489end
490put cmgv cmon 1
491ret true
492end
493
494note cm_q0: cmpth = the read-only root
495fn cm_q0 to void
496n = get cmgv cmprl
497for i ulong n bound 1024
498put cmpth i get cmproot i
499end
500put cmgv cmpl n
501end
502
503note cm_rootok: absolute, ends in /, no control byte, no segment starting with a dot
504fn cm_rootok b view uchar * to bool
505if or lt len b 2 gt len b 900
506ret false
507end
508if or ne get b 0 47 ne get b sub len b 1 47
509ret false
510end
511for i ulong len b bound 1024
512c = get b i
513if or lt c 32 eq c 127
514ret false
515end
516end
517for i ulong sub len b 1 bound 1024
518if and eq get b i 47 eq get b add i 1 46
519ret false
520end
521end
522ret true
523end
524
525fn cm_env pre view uchar * b edit uchar * to long
526e = new uchar 1100 0
527for i ulong ulong envcnt bound 1000000
528n = envread i e 0 1100
529if gt n long len pre
530if cm_eq part e 0 len pre pre
531m = sub ulong n len pre
532if gt m len b
533ret -1
534end
535for j ulong m bound 1100
536put b j get e add len pre j
537end
538ret long m
539end
540end
541end
542ret -1
543end
544
545note cm_mkall: the root and its six directories (names in cmdirs, each ending in /)
546fn cm_mkall to bool
547cm_p0
548if not cm_mkdir
549ret false
550end
551s = ulong 0
552for i ulong len cmdirs
553if eq get cmdirs i 47
554cm_p0
555cm_pa part cmdirs s add sub i s 1
556if not cm_mkdir
557ret false
558end
559s = add i 1
560end
561end
562ret true
563end
564
565note cm_lock: the writer lock (flock on <root>writer.lock); held until the request ends
566fn cm_lock to bool
567cm_p0
568cm_pa cmtlk
569fd = fopen cmpth 0 get cmgv cmpl 557122 384
570if lt fd 0
571ret false
572end
573r = flock int fd 2
574if ne r 0
575x = fclose int fd
576ret false
577end
578put cmgv cmlock ulong fd
579ret true
580end
581
582fn cm_unlock to void
583fd = get cmgv cmlock
584if ne fd 0
585x = fclose int fd
586put cmgv cmlock 0
587end
588end