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 / om / tur-ledger / check.sh

573 lines · 31217 bytes Raw Blame History
1#!/usr/bin/env bash
2# TUR ledger gate (Aienet-0). Builds om/tur-ledger/tur-ledger.om with the pinned omc, runs the
3# suite against it (must PASS), then builds each mutant and runs the same suite (each must FAIL).
4# Usage: OMC=/path/to/seed/out/omc bash om/tur-ledger/check.sh (OMEGA_OMC is accepted too)
5# Env: TUR_LEDGER_OUT=path copies the built binary; CRASH_N (200), MUTANT_CRASH_N, CRASH_SEED (7).
6# Needs: sha256sum, OpenSSL 3, strace (syscall injection), flock(1), timeout(1). Host only; no network, no GPU.
7set -u
8here=$(cd "$(dirname "$0")" && pwd)
9root=$(cd "$here/../.." && pwd)
10src="$here/tur-ledger.om"
11sha="$root/om/vendor/sha256.om"
12OMC=${OMC:-${OMEGA_OMC:-}}
13: "${OMC:?set OMC to the seed compiler (omega-mv seed/out/omc)}"
14PIN=d6290c7663c2b4bafb59eb3fdf812476e977f4c144def71095c6248ef9b96b84
15got=$(sha256sum < "$OMC" | cut -d' ' -f1)
16[ "$got" = "$PIN" ] || { echo "FAIL omc sha256 $got, pinned $PIN"; exit 1; }
17CRASH_N=${CRASH_N:-200}
18b=$(mktemp -d); keys=$(mktemp -d /dev/shm/tur-ledger-test-keys.XXXXXX)
19trap 'rm -rf "$b" "$keys"' EXIT
20umask 077
21# Disposable test-only oracle keys, never production credentials or implementation dependencies.
22for role in issuer verifier stranger; do
23 openssl genpkey -algorithm EC -pkeyopt ec_paramgen_curve:P-256 -out "$keys/$role" || exit 1
24done
25pubhex() { openssl pkey -in "$keys/$1" -pubout -outform DER 2>/dev/null | od -An -v -tx1 | tr -d ' \n' | cut -c53-; }
26issuer=$(pubhex issuer); verifier=$(pubhex verifier)
27
28zero=$(printf '0%.0s' $(seq 64))
29hexn() { printf '%064x' "$1"; }
30J7=$(printf '7%.0s' $(seq 64)); J8=$(printf '8%.0s' $(seq 64))
31RA=$(printf 'a%.0s' $(seq 64)); RB=$(printf 'b%.0s' $(seq 64))
32
33build() { # out source
34 { printf 'om2\n'; cat "$sha" "$root/om/vendor/ec.om" "$2"; } | "$OMC" > "$1" 2> "$1.err" || { echo "compile: $(cat "$1.err")"; return 1; }
35 chmod 700 "$1"
36}
37
38# Trusted provisioning and signing are TEST ORACLES. The ledger never signs and never creates pins.
39provision() {
40 local d=$1 req=req
41 [ "$d" = "$b/pf" ] && req=acct
42 mkdir -p "$d"
43 [ -f "$d/authority" ] || printf 'genesis tur-ledger-2 %s %s %s %s %s prov ver %s %s %s' "$zero" "$issuer" "$verifier" "$RA" "$req" "$RA" "$RB" "$J7" > "$d/authority"
44}
45canonical() { # config, unsigned body, optional reservation authorization digest
46 cat "$1/authority"; printf '\n%s\n' "$2"
47 [ -z "${3:-}" ] || printf '%s\n' "$3"
48}
49reservehash() {
50 local d=$1 job=$2 line rec
51 [ -f "$d/journal" ] || return 0
52 line=$(grep " reserve $job " "$d/journal" | head -n 1)
53 [ -n "$line" ] || return 0
54 rec=${line#* }; rec=${rec#* }; rec=${rec% *}; rec=${rec% *}
55 canonical "$d" "$rec" | sha256sum | cut -c1-64
56}
57sign() { # key, dir, unsigned body, reserve digest
58 canonical "$2" "$3" "${4:-}" | openssl dgst -sha256 -sign "$keys/$1" | od -An -v -tx1 | tr -d ' \n'
59}
60prepare() { # d original-command... -> signed argv
61 local d=$1; shift; provision "$d"
62 argv=("$@")
63 local rh= body isig vsig
64 case "$1" in
65 prefund) ;;
66 reserve) argv+=("$RA") ;;
67 settle) [ "$#" = 6 ] || return 0; argv+=("$J7" "$RB"); rh=$(reservehash "$d" "$2") ;;
68 refund) rh=$(reservehash "$d" "$2") ;;
69 *) return 0 ;;
70 esac
71 body="${argv[*]}"; isig=$(sign issuer "$d" "$body" "$rh"); argv+=("$isig")
72 if [ "$1" = settle ]; then vsig=$(sign verifier "$d" "$body" "$rh"); argv+=("$vsig"); fi
73}
74invoke() { local d=$1; shift; prepare "$d" "$@"; "$T" --journal "$d" "${argv[@]}"; }
75
76# ---- suite: every test prints PASS/FAIL; T is the binary, f counts failures ----
77f=0
78ok() { echo "PASS $1"; }
79no() { echo "FAIL $1: $2"; f=$((f + 1)); }
80x() { # dir args... -> sets o (stdout) e (stderr) r (rc)
81 local d=$1; shift
82 o=$(invoke "$d" "$@" 2> "$b/err"); r=$?; e=$(cat "$b/err")
83}
84want() { # name rc stdout dir args...
85 local n=$1 wr=$2 wo=$3; shift 3
86 x "$@"
87 if [ "$r" = "$wr" ] && [ "$o" = "$wo" ]; then ok "$n"; else no "$n" "exit $r out '$o' err '$e', wanted exit $wr out '$wo'"; fi
88}
89nsettle() { grep -c " settle $1 " "$2/journal"; }
90
91# Independent chain check with sha256sum: SEQ counts from 1, PREV is the previous HASH, HASH is the
92# sha256 of the bytes before " HASH".
93chaincheck() { # journal -> 0 when every line verifies
94 local prev=$zero n=0 line body h p s
95 while IFS= read -r line; do
96 n=$((n + 1)); body=${line% *}; h=${line##* }
97 s=${line%% *}; p=${line#* }; p=${p%% *}
98 [ "$s" = "$n" ] && [ "$p" = "$prev" ] || return 1
99 [ "$(printf '%s' "$body" | sha256sum | cut -c1-64)" = "$h" ] || return 1
100 prev=$h
101 done < "$1"
102}
103# forge: write a journal with a valid chain from record bodies on stdin (one per line).
104forge() { # journal
105 local prev=$zero n=0 body h
106 : > "$1"
107 while IFS= read -r rec; do
108 n=$((n + 1)); body="$n $prev $rec"
109 h=$(printf '%s' "$body" | sha256sum | cut -c1-64)
110 printf '%s %s\n' "$body" "$h" >> "$1"; prev=$h
111 done
112}
113
114t_oracle() { # tests/turing-network/tur_r0_oracle.om scenario through the CLI, one JOB per escrow
115 local d=$b/oracle; rm -rf "$d"
116 want oracle.prefund 0 "prefunded p-req req 100" "$d" prefund p-req req 100
117 want oracle.overdraft-101 7 "" "$d" reserve "$J7" req prov ver 101
118 want oracle.reserve-7-40 0 "reserved $J7 req prov ver 40" "$d" reserve "$J7" req prov ver 40
119 want oracle.req-after-reserve 0 "balance req 60" "$d" balance req
120 want oracle.second-reservation 9 "" "$d" reserve "$J7" req prov ver 10
121 want oracle.settle-unknown-job 8 "" "$d" settle "$J8" "$RA" 27 3 10
122 want oracle.change-mismatch 12 "" "$d" settle "$J7" "$RA" 27 4 10
123 want oracle.settle 0 "settled $J7 $RA 27 3 10" "$d" settle "$J7" "$RA" 27 3 10
124 want oracle.zero-replay 11 "" "$d" settle "$J7" "$RB" 0 0 0
125 want oracle.requester 0 "balance req 70" "$d" balance req
126 want oracle.provider 0 "balance prov 27" "$d" balance prov
127 want oracle.verifier 0 "balance ver 3" "$d" balance ver
128 want oracle.observer 0 "balance obs 0" "$d" balance obs
129 x "$d" audit
130 case "$o" in "audit ok records 4 "*" prefunded 100 balances 100 escrow 0") ok oracle.audit ;; *) no oracle.audit "exit $r '$o'" ;; esac
131 if chaincheck "$d/journal"; then ok oracle.chain-sha256sum; else no oracle.chain-sha256sum "sha256sum disagrees"; fi
132}
133
134t_prefund() {
135 local d=$b/pf; rm -rf "$d"
136 want prefund.first 0 "prefunded p1 acct 5" "$d" prefund p1 acct 5
137 want prefund.dup 0 "dup p1 acct 5" "$d" prefund p1 acct 5
138 want prefund.conflict-amount 13 "" "$d" prefund p1 acct 6
139 want prefund.conflict-account 16 "" "$d" prefund p1 other 5
140 want prefund.balance 0 "balance acct 5" "$d" balance acct
141 want usage.bad-job 2 "" "$d" reserve "$(printf 'A%.0s' $(seq 64))" acct p v 1
142 want usage.leading-zero 2 "" "$d" prefund p2 acct 05
143 want usage.zero-amount 2 "" "$d" prefund p2 acct 0
144 want usage.arity 2 "" "$d" settle "$J7" "$RA" 1 1
145 want usage.command 2 "" "$d" mint acct 1
146 want usage.u64-overflow 2 "" "$d" prefund p2 acct 18446744073709551616
147}
148
149t_once() { # sequential exactly-once
150 local d=$b/once; rm -rf "$d"
151 x "$d" prefund p1 req 50; x "$d" reserve "$J7" req prov ver 20
152 want once.settle 0 "settled $J7 $RA 15 4 1" "$d" settle "$J7" "$RA" 15 4 1
153 cp "$d/journal" "$b/once.before"
154 want once.same-receipt 0 "already-settled $J7 $RA 15 4 1" "$d" settle "$J7" "$RA" 15 4 1
155 want once.other-receipt 11 "" "$d" settle "$J7" "$RB" 15 4 1
156 want once.same-receipt-other-split 11 "" "$d" settle "$J7" "$RA" 16 3 1
157 want once.refund-after-settle 10 "" "$d" refund "$J7"
158 if cmp -s "$d/journal" "$b/once.before" && [ "$(nsettle "$J7" "$d")" = 1 ]; then ok once.journal-unchanged; else no once.journal-unchanged "journal changed or settle count $(nsettle "$J7" "$d")"; fi
159}
160
161t_concurrent() { # 20 concurrent settle processes: exactly one settlement
162 local d=$b/conc i n0 n11 nalr
163 for mode in distinct same; do
164 rm -rf "$d"; x "$d" prefund p1 req 50; x "$d" reserve "$J7" req prov ver 20
165 for i in $(seq 1 20); do
166 if [ $mode = distinct ]; then rc=$(hexn "$i"); else rc=$RA; fi
167 prepare "$d" settle "$J7" "$rc" 15 4 1
168 ( "$T" --journal "$d" "${argv[@]}" > "$b/c.$i.out" 2>/dev/null; echo $? > "$b/c.$i.rc" ) &
169 done
170 wait
171 n0=$(cat "$b"/c.*.rc | grep -cx 0); n11=$(cat "$b"/c.*.rc | grep -cx 11)
172 nst=$(cat "$b"/c.*.out | grep -c '^settled '); nalr=$(cat "$b"/c.*.out | grep -c '^already-settled ')
173 x "$d" audit; ar=$r
174 if [ $mode = distinct ]; then
175 [ "$n0" = 1 ] && [ "$n11" = 19 ] && [ "$nst" = 1 ]; c=$?
176 else
177 [ "$n0" = 20 ] && [ "$nst" = 1 ] && [ "$nalr" = 19 ]; c=$?
178 fi
179 if [ $c = 0 ] && [ "$(nsettle "$J7" "$d")" = 1 ] && [ "$ar" = 0 ]; then ok "concurrent.settle-$mode (settled $nst, already $nalr, refused $n11)"; else no "concurrent.settle-$mode" "exit0 $n0 refused11 $n11 settled $nst already $nalr journal-settles $(nsettle "$J7" "$d") audit $ar"; fi
180 rm -f "$b"/c.*
181 done
182 rm -rf "$d"; x "$d" prefund p1 req 1
183 for i in $(seq 1 20); do
184 prepare "$d" reserve "$(hexn "$i")" req prov ver 1
185 ( "$T" --journal "$d" "${argv[@]}" > /dev/null 2>&1; echo $? > "$b/c.$i.rc" ) &
186 done
187 wait
188 n0=$(cat "$b"/c.*.rc | grep -cx 0); n7=$(cat "$b"/c.*.rc | grep -cx 7); x "$d" audit
189 case "$r $o $n0 $n7" in "0 audit ok records 3 "*" jobs 1 open 1 "*" 1 19") ok "concurrent.double-debit (one reserve, nineteen insufficient-funds)" ;; *) no concurrent.double-debit "exit0 $n0 exit7 $n7 audit $r '$o'" ;; esac
190 rm -f "$b"/c.*
191}
192
193t_lock() { # the whole command waits for flock(LOCK_EX) on the journal
194 local d=$b/lock; rm -rf "$d"; x "$d" prefund p1 req 5
195 flock "$d/journal" sleep 3 & lp=$!
196 sleep 0.5
197 timeout 1 "$T" --journal "$d" balance req > /dev/null 2>&1; r1=$?
198 wait $lp
199 x "$d" balance req
200 if [ "$r1" = 124 ] && [ "$r" = 0 ]; then ok "lock.blocks-while-held (timeout exit $r1, then exit $r)"; else no lock.blocks-while-held "while held exit $r1 (want 124), after exit $r"; fi
201}
202
203t_fsync() { # the record is pwritten and fsynced before the reply reaches stdout
204 local d=$b/fs; rm -rf "$d"; x "$d" prefund p1 req 5
205 prepare "$d" reserve "$J7" req prov ver 1
206 strace -f -qq -e trace=pwrite64,fsync,write -o "$b/st" "$T" --journal "$d" "${argv[@]}" > /dev/null 2>&1 || true
207 # order of the journal pwrite, the fsync of the same fd, and the reply on fd 1
208 local wl fl ol fd
209 wl=$(grep -n 'pwrite64(' "$b/st" | tail -n 1); fd=$(printf '%s' "$wl" | sed -n 's/.*pwrite64(\([0-9]*\),.*/\1/p'); wl=${wl%%:*}
210 fl=$(grep -n "fsync($fd) *= 0" "$b/st" | tail -n 1); fl=${fl%%:*}
211 ol=$(grep -n 'write(1, "' "$b/st" | head -n 1); ol=${ol%%:*}
212 cp "$b/st" "$b/strace.last"
213 if [ -n "$wl" ] && [ -n "$fl" ] && [ -n "$ol" ] && [ "$wl" -lt "$fl" ] && [ "$fl" -lt "$ol" ]; then ok "fsync.before-ack (pwrite64 line $wl < fsync($fd) line $fl < write(1) line $ol)"; else no fsync.before-ack "pwrite '$wl' fsync '$fl' reply '$ol'"; fi
214}
215
216t_crash() { # kill -9 at random points over CRASH_N reserve/settle commands, retrying each one
217 local c=$b/crash k=$b/ctrl i j op rc0 rc1 killed=0 done0=0 durable=0 lost=0 badaudit=0 badretry=0
218 rm -rf "$c" "$k"; x "$c" prefund p0 req 1000000; x "$k" prefund p0 req 1000000
219 RANDOM=${CRASH_SEED:-7}
220 for i in $(seq 1 "$CRASH_N"); do
221 j=$(( (i + 1) / 2 ))
222 if [ $((i % 2)) = 1 ]; then op="reserve $(hexn "$j") req prov ver 10"; else op="settle $(hexn "$j") $(hexn $((j + 4096))) 6 3 1"; fi
223 invoke "$k" $op > /dev/null 2>&1 || badretry=$((badretry + 1))
224 prepare "$c" $op
225 "$T" --journal "$c" "${argv[@]}" > /dev/null 2>&1 & p=$!
226 sleep "0.00$((RANDOM % 8))$((RANDOM % 10))"
227 kill -9 $p 2> /dev/null; { wait $p; } 2> /dev/null; rc0=$?
228 if [ "$rc0" = 137 ]; then killed=$((killed + 1)); else done0=$((done0 + 1)); fi
229 x "$c" audit; [ "$r" = 0 ] || badaudit=$((badaudit + 1))
230 x "$c" $op; rc1=$r
231 case "$rc1 $o" in
232 "0 already-"*) [ "$rc0" = 137 ] && durable=$((durable + 1)) ;;
233 "0 reserved "*|"0 settled "*) [ "$rc0" = 137 ] && lost=$((lost + 1)) ;;
234 *) badretry=$((badretry + 1)) ;;
235 esac
236 done
237 local same=1 a
238 for a in req prov ver; do
239 [ "$("$T" --journal "$c" balance $a)" = "$("$T" --journal "$k" balance $a)" ] || same=0
240 done
241 local dup; dup=$(grep ' settle ' "$c/journal" | cut -d' ' -f4 | sort | uniq -d | wc -l)
242 x "$c" audit; local ca=$o; x "$k" audit
243 local ident=no; cmp -s "$c/journal" "$k/journal" && ident=yes
244 echo "crash: n $CRASH_N killed $killed (durable-before-kill $durable, not-yet-durable $lost) completed-before-kill $done0 journal-identical-to-control $ident"
245 echo "crash: $ca"
246 if [ "$badaudit" = 0 ] && [ "$badretry" = 0 ] && [ "$same" = 1 ] && [ "$dup" = 0 ] && [ "${ca#audit ok records }" != "$ca" ] && [ "${ca#* head * }" = "${o#* head * }" ]; then ok "crash.kill9-$CRASH_N (audit after every kill, no double settle, balances equal control)"; else no crash.kill9 "badaudit $badaudit badretry $badretry same $same dup-settles $dup audit '$ca' control '$o'"; fi
247}
248
249t_torn() { # a torn last line is truncated and recorded; the records before it stay
250 local d=$b/torn; rm -rf "$d"; x "$d" prefund p1 req 9; x "$d" reserve "$J7" req prov ver 4
251 cp "$d/journal" "$b/torn.good"; x "$d" balance req; local before=$o
252 printf '4 %s settle %s' "$zero" "$J7" >> "$d/journal"
253 local want; want=$(tail -c +$(( $(stat -c %s "$b/torn.good") + 1 )) "$d/journal" | sha256sum | cut -c1-64)
254 local n; n=$(( $(stat -c %s "$d/journal") - $(stat -c %s "$b/torn.good") ))
255 x "$d" balance req
256 if [ "$r" = 0 ] && [ "$o" = "$before" ] && [ "$e" = "recovered torn-tail $n bytes sha256 $want" ] && cmp -s "$b/torn.good" <(head -c "$(stat -c %s "$b/torn.good")" "$d/journal") && tail -n 1 "$d/journal" | grep -q " recover $n $want " && chaincheck "$d/journal"; then ok "torn.unterminated ($e)"; else no torn.unterminated "exit $r out '$o' err '$e'"; fi
257 # A newline-terminated bad record is corruption, never an automatic rollback.
258 local last; last=$(tail -n 1 "$d/journal" | cut -d' ' -f1); local lh; lh=$(tail -n 1 "$d/journal"); lh=${lh##* }
259 printf '%s %s refund %s %s\n' $((last + 1)) "$lh" "$J7" "$zero" >> "$d/journal"
260 cp "$d/journal" "$b/torn.bad"
261 raw_want corrupt.terminated-bad-hash 5 "$d" show "$J7"
262 # The same final record cut short without its newline is an incomplete tail.
263 local s keep; s=$(stat -c %s "$d/journal"); keep=$(head -n -1 "$d/journal" | wc -c)
264 truncate -s $((s - 10)) "$d/journal"
265 x "$d" audit
266 if [ "$r" = 0 ] && [ "${e#recovered torn-tail $((s - 10 - keep)) bytes}" != "$e" ] && chaincheck "$d/journal"; then ok "torn.cut-last-record ($e)"; else no torn.cut-last-record "exit $r out '$o' err '$e'"; fi
267}
268
269t_corrupt() { # a bad line before the last refuses the ledger and changes nothing
270 local d=$b/cor; rm -rf "$d"; x "$d" prefund p1 req 100; x "$d" reserve "$J7" req prov ver 40; x "$d" balance req
271 cp "$d/journal" "$b/cor.good"
272 sed -i '2s/ req 100 / req 900 /' "$d/journal"
273 cmp -s "$d/journal" "$b/cor.good" && no corrupt.mutation "sed did not change the journal"
274 cp "$d/journal" "$b/cor.bad"
275 want corrupt.amount-edited 5 "" "$d" balance req
276 if cmp -s "$d/journal" "$b/cor.bad"; then ok corrupt.journal-untouched; else no corrupt.journal-untouched "journal changed"; fi
277 cp "$b/cor.good" "$d/journal"; sed -i '2d' "$d/journal"
278 want corrupt.line-deleted 5 "" "$d" audit
279 { sed -n 1p "$b/cor.good"; sed -n 3p "$b/cor.good"; sed -n 2p "$b/cor.good"; } > "$d/journal"
280 want corrupt.lines-swapped 5 "" "$d" audit
281 cp "$b/cor.good" "$d/journal"; sed -i '1s/genesis tur-ledger-2/genesis tur-ledger-1/' "$d/journal"
282 want corrupt.genesis-edited 5 "" "$d" audit
283}
284
285t_conserve() { # valid signatures do not bypass escrow/conservation rules, including replay
286 local d=$b/cons; rm -rf "$d"; provision "$d"
287 x "$d" prefund p1 req 100; x "$d" reserve "$J7" req prov ver 40
288 cp "$d/journal" "$b/cons.before"
289 prepare "$d" settle "$J7" "$RA" 30 30 10
290 local rec="${argv[*]}" prev n body h
291 prev=$(tail -n 1 "$d/journal"); prev=${prev##* }; n=4
292 body="$n $prev $rec"; h=$(printf '%s' "$body" | sha256sum | cut -c1-64)
293 printf '%s %s\n' "$body" "$h" >> "$d/journal"
294 if chaincheck "$d/journal"; then ok conserve.forged-chain-valid; else no conserve.forged-chain-valid "forge broke chain"; fi
295 want conserve.payout-over-escrow 6 "" "$d" audit
296 want conserve.refused-for-every-command 6 "" "$d" balance prov
297}
298
299raw_want() { # name rc d exact args (no oracle signing)
300 local name=$1 expected=$2 d=$3; shift 3
301 local before after rr
302 before=$(sha256sum "$d/journal" 2>/dev/null || true)
303 "$T" --journal "$d" "$@" > "$b/raw.out" 2> "$b/raw.err"; rr=$?
304 after=$(sha256sum "$d/journal" 2>/dev/null || true)
305 if [ "$rr" = "$expected" ] && [ "$before" = "$after" ]; then ok "$name (exit $rr, unchanged journal)"; else no "$name" "exit $rr expected $expected, before '$before' after '$after'"; fi
306}
307t_unsigned() {
308 local d=$b/unsigned; rm -rf "$d"; provision "$d"
309 "$T" --journal "$d" audit > /dev/null 2>&1
310 raw_want auth.unsigned-prefund 2 "$d" prefund forged req 100
311}
312t_auth() {
313 local d=$b/auth body rh sig
314 t_unsigned
315 rm -rf "$d"; x "$d" prefund p1 req 100; x "$d" reserve "$J7" req prov ver 40
316 raw_want auth.unsigned-reserve 2 "$d" reserve "$J8" req prov ver 5 "$RA"
317 raw_want auth.unsigned-settle 2 "$d" settle "$J7" "$RA" 27 3 10 "$J7" "$RB"
318 raw_want auth.unsigned-refund 2 "$d" refund "$J7"
319 prepare "$d" prefund p2 req 5
320 body="prefund p2 req 5"; argv[4]=$(sign stranger "$d" "$body")
321 raw_want auth.wrong-issuer-key 16 "$d" "${argv[@]}"
322 prepare "$d" prefund p2 req 5; argv[3]=6
323 raw_want auth.changed-amount 16 "$d" "${argv[@]}"
324 prepare "$d" reserve "$J8" req prov ver 5; argv[6]=$RB
325 raw_want auth.changed-quote 16 "$d" "${argv[@]}"
326 prepare "$d" reserve "$J8" req thief ver 5
327 raw_want auth.account-spoof 16 "$d" "${argv[@]}"
328 prepare "$d" settle "$J7" "$RA" 27 3 10
329 body="${argv[*]:0:8}"; rh=$(reservehash "$d" "$J7")
330 argv[9]=$(sign stranger "$d" "$body" "$rh")
331 raw_want auth.wrong-verifier-key 16 "$d" "${argv[@]}"
332 prepare "$d" settle "$J7" "$RA" 27 3 10
333 argv[8]=$(sign stranger "$d" "$body" "$rh")
334 raw_want auth.unauthorized-settle 16 "$d" "${argv[@]}"
335 prepare "$d" settle "$J7" "$RA" 27 3 10; argv[1]=$J8
336 raw_want auth.changed-job 16 "$d" "${argv[@]}"
337 prepare "$d" settle "$J7" "$RA" 27 3 10; argv[2]=$RB
338 raw_want auth.forged-receipt-hash 16 "$d" "${argv[@]}"
339 prepare "$d" settle "$J7" "$RA" 27 3 10; argv[7]=$RA
340 raw_want auth.mutated-attestation-digest 16 "$d" "${argv[@]}"
341 prepare "$d" settle "$J7" "$RA" 27 3 10; argv[6]=$RB
342 raw_want auth.mutated-signed-job-digest 16 "$d" "${argv[@]}"
343 prepare "$d" settle "$J7" "$RA" 27 3 10; argv[9]="${argv[8]}"
344 raw_want auth.issuer-is-not-verifier 16 "$d" "${argv[@]}"
345 # SIGKILL at stdout write entry is after journal pwrite + fsync and before acknowledgement.
346 prepare "$d" settle "$J7" "$RA" 27 3 10
347 strace -qq -e trace=pwrite64,fsync,write -e inject=write:signal=SIGKILL -o "$b/kill-after-fsync" "$T" --journal "$d" "${argv[@]}" > /dev/null 2>&1
348 local killed=$?
349 if [ "$killed" = 137 ] && grep -q 'fsync(.*) *= 0' "$b/kill-after-fsync"; then ok auth.kill-after-fsync-before-ack; else no auth.kill-after-fsync-before-ack "exit $killed, missing fsync"; fi
350 want auth.lost-response-retry 0 "already-settled $J7 $RA 27 3 10" "$d" settle "$J7" "$RA" 27 3 10
351 want auth.honest-provider 0 "balance prov 27" "$d" balance prov
352 prepare "$d" settle "$J7" "$RA" 27 3 10
353 body="settle $J7 $RA 27 3 10 $J7 $RA"
354 argv[7]=$RA; argv[8]=$(sign issuer "$d" "$body" "$rh"); argv[9]=$(sign verifier "$d" "$body" "$rh")
355 raw_want auth.changed-evidence-conflicts-even-when-signed 11 "$d" "${argv[@]}"
356 cp "$d/journal" "$b/auth.good"
357 { cat "$d/authority"; printf '\nprefund forged req 100\n'; } | forge "$d/journal"
358 raw_want auth.unsigned-replay-last 5 "$d" audit
359 prepare "$d" prefund p2 req 5; argv[4]=$(sign stranger "$d" "prefund p2 req 5")
360 { cat "$d/authority"; printf '\n%s\n' "${argv[*]}"; } | forge "$d/journal"
361 raw_want auth.wrong-key-replay-last 5 "$d" audit
362 printf 'genesis tur-ledger-1\nprefund p1 req 100\n' | forge "$d/journal"
363 raw_want auth.old-journal-refused 5 "$d" audit
364 cp "$b/auth.good" "$d/journal"
365 local authority; authority=$(cat "$d/authority")
366 printf '%s' "${authority/$RA/$J8}" > "$d/authority"
367 raw_want auth.changed-policy-pins 5 "$d" audit
368}
369
370# Exercise the production allocator/record path with a small, completely full
371# key table. Only KCAP changes; crypto, replay, accounting and append are intact.
372t_capacity() {
373 local source=$1 d=$b/capacity T=$b/capacity-ledger id
374 sed 's/^const KCAP ulong 65536$/const KCAP ulong 10/' "$source" > "$b/capacity.om"
375 if ! build "$T" "$b/capacity.om"; then no capacity.build "compile failed"; return; fi
376 echo "capacity-source $(sha256sum "$source" | cut -c1-64) KCAP=10"
377 echo "capacity-bin $(sha256sum "$T" | cut -c1-64)"
378 rm -rf "$d"
379 want capacity.prefund 0 "prefunded p1 req 100" "$d" prefund p1 req 100
380 want capacity.reserve-settle 0 "reserved $J7 req prov ver 40" "$d" reserve "$J7" req prov ver 40
381 want capacity.reserve-refund 0 "reserved $J8 req prov ver 40" "$d" reserve "$J8" req prov ver 40
382 # Three accounts, two jobs and five prefund IDs occupy all ten slots.
383 for id in p2 p3 p4 p5; do
384 want "capacity.fill-$id" 0 "prefunded $id req 1" "$d" prefund "$id" req 1
385 done
386 prepare "$d" prefund overflow req 1
387 raw_want capacity.new-prefund-refused 14 "$d" "${argv[@]}"
388 prepare "$d" reserve "$RB" req prov ver 1
389 raw_want capacity.new-reserve-refused 14 "$d" "${argv[@]}"
390 want capacity.settle 0 "settled $J7 $RA 27 3 10" "$d" settle "$J7" "$RA" 27 3 10
391 want capacity.refund 0 "refunded $J8" "$d" refund "$J8"
392 prepare "$d" settle "$J7" "$RA" 27 3 10
393 raw_want capacity.settle-repeat 0 "$d" "${argv[@]}"
394 prepare "$d" refund "$J8"
395 raw_want capacity.refund-repeat 0 "$d" "${argv[@]}"
396 want capacity.provider 0 "balance prov 27" "$d" balance prov
397 want capacity.verifier 0 "balance ver 3" "$d" balance ver
398 x "$d" audit
399 if [ "$r" = 0 ] && [[ "$o" = *" open 0 settled 1 refunded 1 "* ]]; then
400 ok capacity.audit-closed
401 else no capacity.audit-closed "exit $r out '$o'"; fi
402 d=$b/capacity-partial
403 rm -rf "$d"
404 want capacity.partial-prefund 0 "prefunded p1 req 100" "$d" prefund p1 req 100
405 for id in p2 p3 p4 p5 p6 p7 p8; do
406 want "capacity.partial-fill-$id" 0 "prefunded $id req 1" "$d" prefund "$id" req 1
407 done
408 # Only one slot remains, but a first reservation needs a job and two role accounts.
409 prepare "$d" reserve "$J7" req prov ver 40
410 raw_want capacity.partial-reserve-refused 14 "$d" "${argv[@]}"
411 want capacity.last-slot-prefund 0 "prefunded p9 req 1" "$d" prefund p9 req 1
412 prepare "$d" prefund overflow req 1
413 raw_want capacity.partial-full-prefund-refused 14 "$d" "${argv[@]}"
414}
415
416t_audit_io() {
417 local d=$b/audit-io attempt before before_size rr jfd fl ol
418 rm -rf "$d"; x "$d" prefund base req 100
419 before=$(sha256sum "$d/journal")
420 before_size=$(stat -c %s "$d/journal")
421 prepare "$d" prefund unsynced req 5
422 for attempt in 1 2; do
423 strace -qq -e trace=pwrite64,fsync,ftruncate,write -e inject=fsync:error=EIO -o "$b/io.$attempt.trace" "$T" --journal "$d" "${argv[@]}" > "$b/io.out" 2> "$b/io.err"; rr=$?
424 if [ "$rr" = 3 ] && [ ! -s "$b/io.out" ] && [ "$(sha256sum "$d/journal")" = "$before" ] && [ "$(stat -c %s "$d/journal")" = "$before_size" ]; then
425 ok "io.failed-sync-rollback-$attempt (exit 3, original journal, size $before_size)"
426 else no "io.failed-sync-rollback-$attempt" "exit $rr out '$(cat "$b/io.out")', journal restored $([ "$(sha256sum "$d/journal")" = "$before" ] && echo yes || echo no)"; fi
427 done
428 x "$d" audit
429 if [ "$r" = 0 ] && [ -z "$e" ]; then ok io.no-torn-tail-after-rollback; else no io.no-torn-tail-after-rollback "exit $r err '$e'"; fi
430 prepare "$d" prefund unsynced req 5
431 strace -qq -e trace=pwrite64,fsync,write -o "$b/io.retry.trace" "$T" --journal "$d" "${argv[@]}" > "$b/io.out" 2> "$b/io.err"; rr=$?
432 if [ "$rr" = 0 ] && [ "$(cat "$b/io.out")" = 'prefunded unsynced req 5' ] && grep -q 'fsync(.*) *= 0' "$b/io.retry.trace"; then
433 ok io.retry-writes-and-syncs
434 else no io.retry-writes-and-syncs "exit $rr out '$(cat "$b/io.out")'"; fi
435 # Each duplicate acknowledgement must cross a fresh durability barrier too.
436 local op
437 for op in prefund reserve settle refund; do
438 case $op in
439 prefund) prepare "$d" prefund base req 100 ;;
440 reserve) x "$d" reserve "$J7" req prov ver 40; prepare "$d" reserve "$J7" req prov ver 40 ;;
441 settle) x "$d" settle "$J7" "$RA" 27 3 10; prepare "$d" settle "$J7" "$RA" 27 3 10 ;;
442 refund) x "$d" reserve "$J8" req prov ver 40; x "$d" refund "$J8"; prepare "$d" refund "$J8" ;;
443 esac
444 before=$(sha256sum "$d/journal")
445 strace -qq -e trace=openat,pwrite64,fsync,write -e inject=fsync:error=EIO -o "$b/dup.$op.trace" "$T" --journal "$d" "${argv[@]}" > "$b/io.out" 2> "$b/io.err"; rr=$?
446 jfd=$(sed -n 's/.*openat(.*\/journal",.*) *= \([0-9][0-9]*\)$/\1/p' "$b/dup.$op.trace" | head -n 1)
447 if [ "$rr" = 3 ] && [ ! -s "$b/io.out" ] && [ "$(sha256sum "$d/journal")" = "$before" ] && [ -n "$jfd" ] && grep -q "fsync($jfd) *= -1 EIO" "$b/dup.$op.trace"; then
448 ok "io.duplicate-$op-needs-sync"
449 else no "io.duplicate-$op-needs-sync" "exit $rr out '$(cat "$b/io.out")'"; fi
450 strace -qq -e trace=openat,pwrite64,fsync,write -o "$b/dup.$op.retry.trace" "$T" --journal "$d" "${argv[@]}" > "$b/io.out" 2> "$b/io.err"; rr=$?
451 jfd=$(sed -n 's/.*openat(.*\/journal",.*) *= \([0-9][0-9]*\)$/\1/p' "$b/dup.$op.retry.trace" | head -n 1)
452 fl=$(grep -n "fsync($jfd) *= 0" "$b/dup.$op.retry.trace" | tail -n 1); fl=${fl%%:*}
453 ol=$(grep -n 'write(1, "' "$b/dup.$op.retry.trace" | head -n 1); ol=${ol%%:*}
454 if [ "$rr" = 0 ] && [ -n "$jfd" ] && [ -n "$fl" ] && [ -n "$ol" ] && [ "$fl" -lt "$ol" ] && [ "$(sha256sum "$d/journal")" = "$before" ]; then
455 ok "io.duplicate-$op-retry-synced"
456 else no "io.duplicate-$op-retry-synced" "exit $rr, missing fsync or changed journal"; fi
457 done
458}
459
460t_audit_corrupt() {
461 local d=$b/audit-tail line body hash first
462 rm -rf "$d"; x "$d" prefund base req 100; x "$d" reserve "$J7" req prov ver 40
463 want corrupt.acknowledged-settle 0 "settled $J7 $RA 27 3 10" "$d" settle "$J7" "$RA" 27 3 10
464 cp "$d/journal" "$b/audit-tail.good"
465 sed '$s/ 27 3 10 / 28 3 10 /' "$b/audit-tail.good" > "$d/journal"
466 cp "$d/journal" "$b/audit-tail.bad"
467 raw_want corrupt.acknowledged-tail-audit 5 "$d" audit
468 cp "$b/audit-tail.bad" "$d/journal"
469 prepare "$d" refund "$J7"
470 raw_want corrupt.acknowledged-tail-refund 5 "$d" "${argv[@]}"
471 cp "$b/audit-tail.bad" "$d/journal"
472 raw_want corrupt.acknowledged-tail-show 5 "$d" show "$J7"
473 # Keep body/signatures valid but damage only the final chain hash.
474 line=$(tail -n 1 "$b/audit-tail.good"); body=${line% *}; hash=${line##* }
475 first=0; [ "${hash:0:1}" = 0 ] && first=1
476 { head -n -1 "$b/audit-tail.good"; printf '%s %s%s\n' "$body" "$first" "${hash:1}"; } > "$d/journal"
477 raw_want corrupt.signed-tail-bad-hash 5 "$d" audit
478 cp "$b/audit-tail.good" "$d/journal"
479 want corrupt.original-settlement-preserved 0 "job $J7 settled req prov ver 40 $RA 27 3 10" "$d" show "$J7"
480}
481
482t_audit_argv_phase() {
483 local d=$b/audit-argv
484 rm -rf "$d"; x "$d" prefund base req 100
485 prepare "$d" prefund p2 req 5
486 raw_want usage.fused-name-arguments 2 "$d" prefund 'p2 req' 5 "${argv[4]}"
487 prepare "$d" prefund p3 req 5
488 raw_want usage.fused-command-argument 2 "$d" 'prefund p3' req 5 "${argv[4]}"
489 prepare "$d" reserve "$J7" req prov ver 40
490 raw_want usage.fused-role-arguments 2 "$d" reserve "$J7" 'req prov' ver 40 "$RA" "${argv[7]}"
491 d=$b/audit-phase
492 rm -rf "$d"; x "$d" prefund base req 100
493 want phase.reserve 0 "reserved $J7 req prov ver 40" "$d" reserve "$J7" req prov ver 40
494 want phase.reserve-open-retry 0 "already-reserved $J7 req prov ver 40" "$d" reserve "$J7" req prov ver 40
495 x "$d" settle "$J7" "$RA" 27 3 10
496 prepare "$d" reserve "$J7" req prov ver 40
497 raw_want phase.reserve-after-settle 10 "$d" "${argv[@]}"
498 x "$d" reserve "$J8" req prov ver 40; x "$d" refund "$J8"
499 prepare "$d" reserve "$J8" req prov ver 40
500 raw_want phase.reserve-after-refund 10 "$d" "${argv[@]}"
501}
502
503t_audit() { t_audit_io; t_audit_corrupt; t_audit_argv_phase; }
504
505suite() {
506 f=0
507 t_oracle; t_prefund; t_once; t_concurrent; t_lock; t_fsync; t_torn; t_corrupt; t_conserve; t_auth; t_audit; t_crash
508 t_capacity "${1:-$src}"
509 [ "$f" = 0 ]
510}
511
512echo "omc $(sha256sum "$OMC" | cut -c1-64)"
513echo "src $(sha256sum "$src" | cut -c1-64) om/tur-ledger/tur-ledger.om"
514echo "sha $(sha256sum "$sha" | cut -c1-64) om/vendor/sha256.om"
515echo "ec $(sha256sum "$root/om/vendor/ec.om" | cut -c1-64) omega-mv@6999fa9e6dae4847c155ef4eacaece74f491d3ca"
516echo "gate $(sha256sum "$0" | cut -c1-64)"
517openssl version
518gate=0
519if [ -n "${TUR_LEDGER_AUTH_ONLY:-}" ]; then
520 T=$TUR_LEDGER_AUTH_ONLY
521 t_unsigned
522 [ "$f" = 0 ]; exit $?
523fi
524if [ -n "${TUR_LEDGER_CAPACITY_SOURCE:-}" ]; then
525 t_capacity "$TUR_LEDGER_CAPACITY_SOURCE"
526 [ "$f" = 0 ]; exit $?
527fi
528if [ -n "${TUR_LEDGER_AUDIT_SOURCE:-}" ]; then
529 T=$b/audit-ledger
530 build "$T" "$TUR_LEDGER_AUDIT_SOURCE" || exit 1
531 echo "audit-source $(sha256sum "$TUR_LEDGER_AUDIT_SOURCE" | cut -c1-64)"
532 t_audit
533 [ "$f" = 0 ]; exit $?
534fi
535T=$b/tur-ledger
536if build "$T" "$src"; then
537 echo "bin $(sha256sum "$T" | cut -c1-64)"
538 [ -n "${TUR_LEDGER_OUT:-}" ] && cp "$T" "$TUR_LEDGER_OUT"
539 if suite; then echo "SUITE PASS"; else echo "SUITE FAIL ($f)"; gate=1; fi
540else
541 echo "FAIL build"; gate=1
542fi
543
544mutant() { # name sed-expression: the suite must fail against the mutant
545 local m=$b/$1.om
546 sed "$2" "$src" > "$m"
547 if cmp -s "$src" "$m"; then echo "FAIL mutant $1: mutation did not apply"; gate=1; return; fi
548 T=$b/$1
549 if ! build "$T" "$m" > "$b/$1.build"; then echo "FAIL mutant $1: $(cat "$b/$1.build")"; gate=1; return; fi
550 suite "$m" > "$b/$1.log" 2>&1
551 if [ "$f" != 0 ]; then echo "PASS mutant $1 killed ($f failing: $(grep '^FAIL' "$b/$1.log" | cut -d: -f1 | sed 's/^FAIL //' | tr '\n' ' '))"; else echo "FAIL mutant $1 survived"; gate=1; fi
552}
553CRASH_N=${MUTANT_CRASH_N:-$CRASH_N}
554mutant no-fsync '/^sy = fsync fd$/,/^end$/d'
555mutant no-lock '/^lk = flock fd 2$/,/^end$/d'
556mutant no-exactly-once '/^if eq sph 2$/,/^end$/d'
557mutant no-hash-chain '/^if not hsame h$/,/^end$/d;/^if not psame$/,/^end$/d'
558mutant no-conservation '/^if not conserve$/,/^end$/d'
559mutant no-escrow-rule '/^ret vrule get d3 k pp pv bk$/s/.*/ret 0/'
560mutant no-torn-recovery '/^tb = sub get st SFSZ get st SGOOD$/s/.*/tb = ulong 0/'
561mutant no-issuer-signature '/^if not authsig add b fields 3$/,/^end$/d'
562mutant no-verifier-signature '/^if not authsig add add b fields 1 4$/,/^end$/d'
563mutant no-owner-pins '/^if not and cfeq 6 add b 2 and cfeq 7 add b 3 cfeq 8 add b 4$/,/^end$/d'
564mutant capacity-lockup '/^if ne wrec 0$/i\if gt add get st SKEYS 4 KCAP\ndie 14\nend'
565mutant no-slot-preflight '/^if gt add get st SKEYS needed KCAP$/,/^end$/d'
566mutant no-sync-rollback '/^rollback fd off$/d'
567mutant no-duplicate-sync '/^if ne fsync int get st SFD 0$/,/^end$/d'
568mutant terminated-tail-recovery '/^fn rend e ulong to int$/,/^put st SOVF 0$/s/^ret 5$/if eq e get st SFSZ\nret 0\nend\nret 5/'
569mutant argv-whitespace '/^if le c 32$/,/^end$/d'
570mutant reserve-closed-phase '/^if ne get d4 sub j 1 1$/,/^end$/d'
571
572[ "$gate" = 0 ] && echo "TUR_LEDGER gate: PASS" || echo "TUR_LEDGER gate: FAIL"
573[ "$gate" = 0 ]