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
omega-mv / protocol / aien-protocols / ext / microjob.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/aien-protocols/ext/microjob.om: pure-Omega port of aien-microjob (AIENET-0) |
| 3 | note Implements microjob status transitions, envelope validation, deadline checks, |
| 4 | note payout conservation, canonical job digest, and receipt digest verification. |
| 5 | note Compose after si/directives/om2.om, om/tls/sha2.om, and protocol/aien-protocols/core/types.om. |
| 6 | |
| 7 | const MJ_OK ulong 0 |
| 8 | const MJ_ERR_BAD_ID ulong 1 |
| 9 | const MJ_ERR_BAD_STATE ulong 2 |
| 10 | const MJ_ERR_DEADLINE ulong 3 |
| 11 | const MJ_ERR_BUDGET ulong 4 |
| 12 | const MJ_ERR_DIGEST ulong 5 |
| 13 | const MJ_ERR_PAYOUT ulong 6 |
| 14 | const MJ_ERR_TIMEOUT ulong 7 |
| 15 | |
| 16 | const MJ_STAT_OPEN ulong 1 |
| 17 | const MJ_STAT_CLAIMED ulong 2 |
| 18 | const MJ_STAT_SUBMITTED ulong 3 |
| 19 | const MJ_STAT_VERIFIED ulong 4 |
| 20 | const MJ_STAT_SETTLED ulong 5 |
| 21 | const MJ_STAT_REJECTED ulong 6 |
| 22 | const MJ_STAT_EXPIRED ulong 7 |
| 23 | |
| 24 | const MJ_MAX_DURATION_MS ulong 86400000 |
| 25 | |
| 26 | text mj_dom_job "aienet0:microjob:v1|" |
| 27 | text mj_dom_rcpt "aienet0:receipt:v1|" |
| 28 | |
| 29 | fn mj_status_valid s ulong to bool |
| 30 | ret and ge s MJ_STAT_OPEN le s MJ_STAT_EXPIRED |
| 31 | end |
| 32 | |
| 33 | fn mj_transition_check cur ulong nxt ulong to ulong |
| 34 | if not mj_status_valid cur |
| 35 | ret MJ_ERR_BAD_STATE |
| 36 | end |
| 37 | if not mj_status_valid nxt |
| 38 | ret MJ_ERR_BAD_STATE |
| 39 | end |
| 40 | if eq cur MJ_STAT_OPEN |
| 41 | if or eq nxt MJ_STAT_CLAIMED eq nxt MJ_STAT_EXPIRED |
| 42 | ret MJ_OK |
| 43 | end |
| 44 | ret MJ_ERR_BAD_STATE |
| 45 | end |
| 46 | if eq cur MJ_STAT_CLAIMED |
| 47 | if or eq nxt MJ_STAT_SUBMITTED or eq nxt MJ_STAT_EXPIRED eq nxt MJ_STAT_REJECTED |
| 48 | ret MJ_OK |
| 49 | end |
| 50 | ret MJ_ERR_BAD_STATE |
| 51 | end |
| 52 | if eq cur MJ_STAT_SUBMITTED |
| 53 | if or eq nxt MJ_STAT_VERIFIED eq nxt MJ_STAT_REJECTED |
| 54 | ret MJ_OK |
| 55 | end |
| 56 | ret MJ_ERR_BAD_STATE |
| 57 | end |
| 58 | if eq cur MJ_STAT_VERIFIED |
| 59 | if eq nxt MJ_STAT_SETTLED |
| 60 | ret MJ_OK |
| 61 | end |
| 62 | ret MJ_ERR_BAD_STATE |
| 63 | end |
| 64 | ret MJ_ERR_BAD_STATE |
| 65 | end |
| 66 | |
| 67 | fn mj_validate_envelope job_id view uchar 16 input_dig view uchar 32 reward ulong max_ms ulong to ulong |
| 68 | if uuid_is_nil job_id |
| 69 | ret MJ_ERR_BAD_ID |
| 70 | end |
| 71 | if digest_is_zero input_dig |
| 72 | ret MJ_ERR_DIGEST |
| 73 | end |
| 74 | if eq reward ulong 0 |
| 75 | ret MJ_ERR_BUDGET |
| 76 | end |
| 77 | if or eq max_ms ulong 0 gt max_ms MJ_MAX_DURATION_MS |
| 78 | ret MJ_ERR_TIMEOUT |
| 79 | end |
| 80 | ret MJ_OK |
| 81 | end |
| 82 | |
| 83 | fn mj_check_deadline created_ms ulong deadline_ms ulong now_ms ulong to ulong |
| 84 | if le deadline_ms created_ms |
| 85 | ret MJ_ERR_DEADLINE |
| 86 | end |
| 87 | if gt now_ms deadline_ms |
| 88 | ret MJ_ERR_DEADLINE |
| 89 | end |
| 90 | ret MJ_OK |
| 91 | end |
| 92 | |
| 93 | fn mj_check_payout escrow ulong worker_pay ulong fee ulong refund ulong to ulong |
| 94 | if gt worker_pay escrow |
| 95 | ret MJ_ERR_PAYOUT |
| 96 | end |
| 97 | rem1 = sub escrow worker_pay |
| 98 | if gt fee rem1 |
| 99 | ret MJ_ERR_PAYOUT |
| 100 | end |
| 101 | rem2 = sub rem1 fee |
| 102 | if ne refund rem2 |
| 103 | ret MJ_ERR_PAYOUT |
| 104 | end |
| 105 | ret MJ_OK |
| 106 | end |
| 107 | |
| 108 | fn mj_compute_digest job_id view uchar 16 input_dig view uchar 32 reward ulong deadline_ms ulong dst edit uchar 32 to void |
| 109 | ctx = new ulong 40 0 |
| 110 | sha256_init ctx |
| 111 | sha256_update ctx mj_dom_job |
| 112 | sha256_update ctx job_id |
| 113 | sha256_update ctx input_dig |
| 114 | buf = new uchar 16 0 |
| 115 | p0 = pt_put_u64_le buf ulong 0 reward |
| 116 | p1 = pt_put_u64_le buf p0 deadline_ms |
| 117 | sha256_update ctx part buf ulong 0 p1 |
| 118 | sha256_final ctx dst |
| 119 | end |
| 120 | |
| 121 | fn mj_compute_receipt_digest job_id view uchar 16 input_dig view uchar 32 output_dig view uchar 32 dst edit uchar 32 to void |
| 122 | ctx = new ulong 40 0 |
| 123 | sha256_init ctx |
| 124 | sha256_update ctx mj_dom_rcpt |
| 125 | sha256_update ctx job_id |
| 126 | sha256_update ctx input_dig |
| 127 | sha256_update ctx output_dig |
| 128 | sha256_final ctx dst |
| 129 | end |
| 130 | |
| 131 | fn mj_verify_receipt job_id view uchar 16 input_dig view uchar 32 output_dig view uchar 32 claimed view uchar 32 to ulong |
| 132 | if uuid_is_nil job_id |
| 133 | ret MJ_ERR_BAD_ID |
| 134 | end |
| 135 | if or digest_is_zero input_dig digest_is_zero output_dig |
| 136 | ret MJ_ERR_DIGEST |
| 137 | end |
| 138 | exp = new uchar 32 0 |
| 139 | mj_compute_receipt_digest job_id input_dig output_dig exp |
| 140 | if not digest_eq exp claimed |
| 141 | ret MJ_ERR_DIGEST |
| 142 | end |
| 143 | ret MJ_OK |
| 144 | end |