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 / seed / authority / store-gate.om

269 lines · 5040 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note Durable 32-word authority record store. Caller supplies the record and generation.
3note Writes a temporary 256-byte file, fsyncs it, then atomically renames it into place.
4note Reads require exactly 256 bytes; a short or torn file is refused. Store calls do not lock; the
5note dispatcher holds lock.om around its read-check-persist window.
6text recp "build/authority-store/rec"
7text tmpp "build/authority-store/tmp"
8text genp "build/authority-store/gen"
9global sb uchar 256
10global stail uchar 1
11global spkt uchar 256
12global sn ulong 1
13global spath uchar 128
14global splen ulong 1
15global sgerr ulong 1
16fn put16 v ulong to void
17put spkt get sn 0 uchar and v 255
18put spkt add get sn 0 1 uchar shr v 8
19put sn 0 add get sn 0 2
20end
21fn setrec to void
22put splen 0 len recp
23for i ulong len recp
24put spath i get recp i
25end
26end
27fn settmp to void
28put splen 0 len tmpp
29for i ulong len tmpp
30put spath i get tmpp i
31end
32end
33fn setgen to void
34put splen 0 len genp
35for i ulong len genp
36put spath i get genp i
37end
38end
39fn pack2 to ulong
40put sn 0 0
41put16 2
42put16 len tmpp
43for i ulong len tmpp
44put spkt add get sn 0 i get tmpp i
45end
46put sn 0 add get sn 0 len tmpp
47put16 len recp
48for i ulong len recp
49put spkt add get sn 0 i get recp i
50end
51put sn 0 add get sn 0 len recp
52ret get sn 0
53end
54fn enc r view ulong 32 to void
55for i ulong 32
56v = get r i
57for j ulong 8
58put sb add mul i 8 j uchar and shr v mul j 8 255
59end
60end
61end
62fn dec r edit ulong 32 to void
63for i ulong 32
64v = ulong 0
65for j ulong 8
66v = or v shl ulong get sb add mul i 8 j mul j 8
67end
68put r i v
69end
70end
71note p10 effect
72fn writerec r view ulong 32 to ulong
73enc r
74settmp
75fd = fopen spath 0 get splen 0 577 384
76if lt fd 0
77ret 10
78end
79n = write int fd sb 0 256
80if ne n 256
81note p10 allow E1 primary write error wins over cleanup close error
82cleanup = fclose int fd
83ret 11
84end
85if ne fsync int fd 0
86note p10 allow E1 primary sync error wins over cleanup close error
87cleanup = fclose int fd
88ret 12
89end
90if ne fclose int fd 0
91ret 13
92end
93pn = pack2
94rr = renames spkt 0 pn 0
95if lt rr 0
96ret 14
97end
98ret 0
99end
100note p10 effect
101fn readrec r edit ulong 32 to ulong
102setrec
103fd = fopen spath 0 get splen 0 0 0
104if lt fd 0
105ret 20
106end
107n = read int fd sb 0 256
108extra = long -1
109if eq n 256
110extra = read int fd stail 0 1
111end
112if ne fclose int fd 0
113ret 21
114end
115if or ne n 256 ne extra 0
116ret 22
117end
118dec r
119ret 0
120end
121note p10 effect
122fn readgen to ulong
123put sgerr 0 0
124setgen
125fd = fopen spath 0 get splen 0 0 0
126if lt fd 0
127put sgerr 0 30
128ret 0
129end
130n = read int fd sb 0 8
131extra = long -1
132if eq n 8
133extra = read int fd stail 0 1
134end
135if ne fclose int fd 0
136put sgerr 0 31
137ret 0
138end
139if or ne n 8 ne extra 0
140put sgerr 0 32
141ret 0
142end
143v = ulong 0
144for j ulong 8
145v = or v shl ulong get sb j mul j 8
146end
147ret v
148end
149note p10 effect
150fn writegen v ulong to ulong
151setgen
152fd = fopen spath 0 get splen 0 577 384
153if lt fd 0
154ret 40
155end
156for j ulong 8
157put sb j uchar and shr v mul j 8 255
158end
159n = write int fd sb 0 8
160if ne n 8
161note p10 allow E1 primary write error wins over cleanup close error
162cleanup = fclose int fd
163ret 41
164end
165if ne fsync int fd 0
166note p10 allow E1 primary sync error wins over cleanup close error
167cleanup = fclose int fd
168ret 42
169end
170if ne fclose int fd 0
171ret 43
172end
173ret 0
174end
175note p10 pure
176fn storevalid r view ulong 32 g ulong to ulong
177if ne get r 0 1
178ret 51
179end
180if ne get r 4 g
181ret 52
182end
183ret 0
184end
185note SPDX-License-Identifier: AGPL-3.0-only
186note Store checks: exact read/write, torn file refusal, generation mismatch and atomic replace.
187global checks ulong 1
188global fails ulong 1
189global report uchar 64
190text title "store checks="
191text dir "build/authority-store"
192fn check got ulong want ulong to void
193put checks 0 add get checks 0 1
194if ne got want
195put fails 0 add get fails 0 1
196end
197end
198fn fill r edit ulong 32 seed ulong gen ulong to void
199for i ulong 32
200put r i add seed i
201end
202put r 0 1
203put r 1 1
204put r 3 8
205put r 4 gen
206end
207fn summary to int
208for i ulong len title
209put report i get title i
210end
211n = get checks 0
212at = len title
213put report at uchar add 48 div n 100
214put report add at 1 uchar add 48 rem div n 10 10
215put report add at 2 uchar add 48 rem n 10
216put report add at 3 32
217put report add at 4 uchar add 48 div get fails 0 100
218put report add at 5 uchar add 48 rem div get fails 0 10 10
219put report add at 6 uchar add 48 rem get fails 0 10
220put report add at 7 10
221w = write 1 report 0 add at 8
222if ne w long add at 8
223ret 2
224end
225if ne get fails 0 0
226ret 1
227end
228ret 0
229end
230fn main to int
231if ne makedir dir 0 len dir 448 0
232put sb 0 0
233end
234r = new ulong 32 0
235q = new ulong 32 0
236fill r 100 1
237check writerec r 0
238fill q 0 0
239check readrec q 0
240for i ulong 32
241check get q i get r i
242end
243check storevalid q 1 0
244check writegen 1 0
245check readgen 1
246check storevalid q 2 52
247setrec
248fd = fopen spath 0 get splen 0 577 384
249if lt fd 0
250check 0 1
251else
252n = write int fd sb 0 3
253if ne fclose int fd 0
254check 0 1
255end
256check readrec q 22
257end
258check writerec r 0
259check writegen 2 0
260check readgen 2
261fill r 200 2
262check writerec r 0
263check readrec q 0
264for i ulong 32
265check get q i get r i
266end
267check storevalid q 2 0
268ret summary
269end