code wiki / (root) / nx_offer_ledger_gate.nx

nx_offer_ledger_gate.nx source

↩ module page · 61 lines · 3064 B

1// nx_offer_ledger_gate.nx -- offer state machine + the full acquisition LOOP. 2// 3// State machine: open->PENDING->SENT->ACCEPTED, decline path, bad transitions 4// refused. Then the LOOP CLOSURE: a TO_ACQUIRE target whose offer is ACCEPTED 5// becomes OWNED (nx_fetch_rights) + licensed (nx_rights_ledger) -> product-usable. 6// Returns 0 iff every check holds; nonzero pinpoints the first failure. 7 8import "nx_syscalls.nx" 9import "nx_offer_ledger.nx" 10import "nx_rights_ledger.nx" 11import "nx_fetch_rights.nx" 12 13func main() -> i64 { 14 let ol: *NxOfferLedger = nx_offer_ledger_new(16) 15 16 // open offer 1 for target 7 (contact 100) -> PENDING 17 if nx_offer_open(ol, 1, 7, 100, 1000) != NX_OF_OK { return 1 } 18 if nx_offer_state(ol, 1) != NX_OF_PENDING { return 2 } 19 // cannot accept before sending 20 if nx_offer_accept(ol, 1, 1100) != NX_OF_ERR_BAD_TRANSITION { return 3 } 21 // send -> SENT 22 if nx_offer_send(ol, 1, 1200) != NX_OF_OK { return 4 } 23 if nx_offer_state(ol, 1) != NX_OF_SENT { return 5 } 24 // accept -> ACCEPTED 25 if nx_offer_accept(ol, 1, 1300) != NX_OF_OK { return 6 } 26 if nx_offer_state(ol, 1) != NX_OF_ACCEPTED { return 7 } 27 if nx_offer_is_accepted(ol, 7) != 1 { return 8 } 28 if nx_offer_is_accepted(ol, 8) != 0 { return 9 } 29 // accepted is terminal: can't re-send or decline 30 if nx_offer_send(ol, 1, 1400) != NX_OF_ERR_BAD_TRANSITION { return 10 } 31 if nx_offer_decline(ol, 1, 1400) != NX_OF_ERR_BAD_TRANSITION { return 11 } 32 33 // offer 2 for target 9 -> decline 34 if nx_offer_open(ol, 2, 9, 200, 2000) != NX_OF_OK { return 12 } 35 if nx_offer_decline(ol, 2, 2100) != NX_OF_OK { return 13 } 36 if nx_offer_state(ol, 2) != NX_OF_DECLINED { return 14 } 37 if nx_offer_is_accepted(ol, 9) != 0 { return 15 } 38 if nx_offer_send(ol, 2, 2200) != NX_OF_ERR_BAD_TRANSITION { return 16 } 39 40 // not-found + tally 41 if nx_offer_state(ol, 999) != -1 { return 17 } 42 if nx_offer_accept(ol, 999, 3000) != NX_OF_ERR_NOT_FOUND { return 18 } 43 if nx_offer_count_by_state(ol, NX_OF_ACCEPTED) != 1 { return 19 } 44 45 // ===== LOOP CLOSURE: accepted -> OWNED + licensed -> product-usable ===== 46 let pol: *NxFrPolicy = nx_fr_new(16) 47 let rl: *NxRightsLedger = nx_rights_ledger_new(16) 48 // target 7 starts TO_ACQUIRE: NOT product-usable, NOT licensed 49 if nx_fr_set(pol, "talent7.example" as *u8, NX_FR_TO_ACQUIRE) != 0 { return 20 } 50 if nx_fr_may_use_in_product(nx_fr_classify(pol, "talent7.example" as *u8)) != 0 { return 21 } 51 if nx_rights_is_licensed(rl, 7, NX_USE_MERCH, 5000) != 0 { return 22 } 52 // offer for 7 was ACCEPTED -> close the loop 53 if nx_offer_is_accepted(ol, 7) != 1 { return 23 } 54 if nx_fr_set(pol, "talent7.example" as *u8, NX_FR_OWNED) != 0 { return 24 } 55 if nx_rights_grant(rl, 1, 7, NX_USE_MERCH, 4000, 100000) != NX_RL_OK { return 25 } 56 // now product-usable AND licensed 57 if nx_fr_may_use_in_product(nx_fr_classify(pol, "talent7.example" as *u8)) != 1 { return 26 } 58 if nx_rights_is_licensed(rl, 7, NX_USE_MERCH, 5000) != 1 { return 27 } 59 60 return 0 61}