aien-protocols-mv
Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem
Brought by drakestapleton
aien-protocols-mv / evidence / aienet0-20261028 / settlement / settle-run.sh.at-run
| 1 | #!/bin/sh |
| 2 | # Aienet-0 P0: one job end to end with escrow settlement in the TUR ledger. |
| 3 | # |
| 4 | # usage: scripts/aienet0/settle-run.sh EVIDENCE_DIR [honest|tampered-receipt] |
| 5 | # |
| 6 | # Fresh ledger journal, prefund the requester, sign the job WITHOUT sending |
| 7 | # it (aienet0-run bundle), reserve escrow on its dag_id, then send exactly |
| 8 | # that bundle (aienet0-run run --resend). Settlement is decided only by the |
| 9 | # job path: on VERIFIED with this dag_id the escrow is settled against the |
| 10 | # printed MJR1 sha256 and the settle is repeated (must be already-settled, |
| 11 | # journal unchanged); on anything else the escrow is refunded and never |
| 12 | # settled. Then audit and balances. The mode only chooses the worker's |
| 13 | # behaviour (tampered-receipt: the worker corrupts its signed receipt) and |
| 14 | # what this script expects at the end; it does not steer the decision. |
| 15 | # |
| 16 | # Every command, its output and exit code go to EVIDENCE_DIR/MODE/transcript.txt; |
| 17 | # the journal and the job artifacts stay under EVIDENCE_DIR/MODE. Exit 0 only |
| 18 | # when the outcome is the expected one for the mode. |
| 19 | # |
| 20 | # Environment (as for controls.sh): RUN SERVE LISTEN CONNECT KEYS PINS |
| 21 | # RECOMPUTE RECOMPUTE_SHA256 WKEYS WPINS WSTATE, plus LEDGER (tur-ledger, |
| 22 | # aien-sovereign-core #792). Split (integer TUR test credits): prefund 100, |
| 23 | # reserve 40, settle provider 27, verifier 3, back to requester 10. |
| 24 | set -u |
| 25 | E=${1:?usage: settle-run.sh EVIDENCE_DIR [honest|tampered-receipt]} |
| 26 | MODE=${2:-honest} |
| 27 | case $MODE in |
| 28 | honest) SX= ;; |
| 29 | tampered-receipt) SX='--misbehave tamper-receipt' ;; |
| 30 | *) echo "unknown mode $MODE" >&2; exit 2 ;; |
| 31 | esac |
| 32 | for v in RUN SERVE LISTEN CONNECT KEYS PINS RECOMPUTE RECOMPUTE_SHA256 WKEYS WPINS WSTATE LEDGER; do |
| 33 | eval "x=\${$v:-}" |
| 34 | [ -n "$x" ] || { echo "settle-run.sh: set $v" >&2; exit 2; } |
| 35 | done |
| 36 | D=$E/$MODE |
| 37 | J=$D/journal |
| 38 | if [ -e "$D" ]; then echo "settle-run.sh: $D exists; use a fresh evidence dir" >&2; exit 2; fi |
| 39 | mkdir -p "$J" "$D/out" |
| 40 | T=$D/transcript.txt |
| 41 | : >"$T" |
| 42 | |
| 43 | # step NAME CMD...: run, append "$ CMD", its stdout+stderr and "rc=N" to the |
| 44 | # transcript; output also kept in $D/NAME.out, exit code in $rc. |
| 45 | step() { |
| 46 | name=$1; shift |
| 47 | printf '$ %s\n' "$*" >>"$T" |
| 48 | "$@" >"$D/$name.out" 2>&1 |
| 49 | rc=$? |
| 50 | cat "$D/$name.out" >>"$T" |
| 51 | printf 'rc=%s\n\n' "$rc" >>"$T" |
| 52 | } |
| 53 | note() { printf '# %s\n' "$*" >>"$T"; } |
| 54 | jsha() { sha256sum "$J/journal" | cut -d' ' -f1; } |
| 55 | fail() { note "RESULT FAIL $MODE: $*"; cat "$T"; echo "RESULT FAIL $MODE: $*"; exit 1; } |
| 56 | |
| 57 | note "mode $MODE, $(date -u +%Y-%m-%dT%H:%M:%SZ), ledger $LEDGER sha256 $(sha256sum "$LEDGER" | cut -d' ' -f1)" |
| 58 | step prefund "$LEDGER" --journal "$J" prefund pf-1 requester 100 |
| 59 | [ $rc = 0 ] || fail "prefund rc=$rc" |
| 60 | |
| 61 | step bundle "$RUN" bundle --keys "$KEYS" --out "$D/out/job.an0b" |
| 62 | [ $rc = 0 ] || fail "bundle rc=$rc" |
| 63 | dag=$(awk '$1 == "BUNDLE" { print $2 }' "$D/bundle.out") |
| 64 | [ -n "$dag" ] || fail "no BUNDLE line" |
| 65 | |
| 66 | step reserve "$LEDGER" --journal "$J" reserve "$dag" requester provider verifier 40 |
| 67 | [ $rc = 0 ] || fail "reserve rc=$rc" |
| 68 | |
| 69 | # The job: worker in the background, coordinator sends the reserved bundle. |
| 70 | scmd="$SERVE $SX --connect $CONNECT --keys $WKEYS --pins $WPINS --state $WSTATE --connect-timeout 60" |
| 71 | note "serve: $scmd" |
| 72 | $scmd >"$D/serve.out" 2>"$D/serve.err" </dev/null & |
| 73 | sp=$! |
| 74 | step run "$RUN" run --resend "$D/out/job.an0b" --listen "$LISTEN" --keys "$KEYS" --pins "$PINS" \ |
| 75 | --out "$D/out" --recompute "$RECOMPUTE" --recompute-sha256 "$RECOMPUTE_SHA256" --accept-timeout 90 |
| 76 | run_rc=$rc |
| 77 | wait $sp |
| 78 | src=$? |
| 79 | note "serve rc=$src stdout: $(cat "$D/serve.out")" |
| 80 | note "serve stderr: $(tr '\n' ' ' <"$D/serve.err")" |
| 81 | |
| 82 | vline=$(grep '^VERIFIED ' "$D/run.out") |
| 83 | vdag=$(printf '%s\n' "$vline" | awk '{ print $2 }') |
| 84 | vrcpt=$(printf '%s\n' "$vline" | awk '{ print $3 }') |
| 85 | if [ "$run_rc" = 0 ] && [ "$vdag" = "$dag" ] && [ -n "$vrcpt" ]; then |
| 86 | note "decision: VERIFIED $dag -> settle" |
| 87 | step settle "$LEDGER" --journal "$J" settle "$dag" "$vrcpt" 27 3 10 |
| 88 | [ $rc = 0 ] || fail "settle rc=$rc" |
| 89 | before=$(jsha) |
| 90 | step settle-again "$LEDGER" --journal "$J" settle "$dag" "$vrcpt" 27 3 10 |
| 91 | after=$(jsha) |
| 92 | note "journal sha256 before repeat $before after $after" |
| 93 | [ $rc = 0 ] && grep -q '^already-settled ' "$D/settle-again.out" || fail "repeat settle rc=$rc" |
| 94 | [ "$before" = "$after" ] || fail "repeat settle changed the journal" |
| 95 | decision=settled |
| 96 | else |
| 97 | note "decision: not VERIFIED (run rc=$run_rc) -> refund" |
| 98 | step refund "$LEDGER" --journal "$J" refund "$dag" |
| 99 | [ $rc = 0 ] || fail "refund rc=$rc" |
| 100 | decision=refunded |
| 101 | fi |
| 102 | |
| 103 | step audit "$LEDGER" --journal "$J" audit |
| 104 | [ $rc = 0 ] || fail "audit rc=$rc" |
| 105 | step show "$LEDGER" --journal "$J" show "$dag" |
| 106 | bal= |
| 107 | for a in requester provider verifier; do |
| 108 | step "balance-$a" "$LEDGER" --journal "$J" balance "$a" |
| 109 | [ $rc = 0 ] || fail "balance $a rc=$rc" |
| 110 | bal="$bal $(awk '{ print $3 }' "$D/balance-$a.out")" |
| 111 | done |
| 112 | bal=${bal# } |
| 113 | settles=$(awk '$3 == "settle"' "$J/journal" | wc -l | tr -d ' ') |
| 114 | note "decision $decision, balances requester provider verifier: $bal, settle records in journal: $settles" |
| 115 | (cd "$D" && sha256sum journal/journal out/*) >"$D/SHA256SUMS" |
| 116 | |
| 117 | case $MODE in |
| 118 | honest) want_decision=settled; want_bal='70 27 3'; want_settles=1 ;; |
| 119 | tampered-receipt) want_decision=refunded; want_bal='100 0 0'; want_settles=0 ;; |
| 120 | esac |
| 121 | [ "$decision" = "$want_decision" ] || fail "decision $decision, want $want_decision" |
| 122 | [ "$bal" = "$want_bal" ] || fail "balances $bal, want $want_bal" |
| 123 | [ "$settles" = "$want_settles" ] || fail "settle records $settles, want $want_settles" |
| 124 | note "RESULT PASS $MODE: $decision, balances $bal" |
| 125 | cat "$T" |
| 126 | echo "RESULT PASS $MODE: $decision $dag, balances requester provider verifier $bal" |