Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-protocols-mv/blob/main/evidence/aienet0-20261028/settlement/settle-run.sh.at-run # aien-protocols-mv Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-protocols-mv / evidence / aienet0-20261028 / settlement / settle-run.sh.at-run` 126 lines ยท 5562 bytes Raw Blame History 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" 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"