nx_offer_ledger_store_gate.nx source
↩ module page · 42 lines · 1957 B
1// nx_offer_ledger_store_gate.nx -- durability gate for the offer ledger.
2// Build offers in 3 states -> save -> reload into a FRESH ledger -> states +
3// is_accepted survived, AND the state machine resumes on the reloaded ledger.
4
5import "nx_syscalls.nx"
6import "nx_offer_ledger.nx"
7import "nx_offer_ledger_store.nx"
8
9func main() -> i64 {
10 let ol: *NxOfferLedger = nx_offer_ledger_new(16)
11 // offer 1 target 7 -> ACCEPTED
12 if nx_offer_open(ol, 1, 7, 100, 1000) != NX_OF_OK { return 1 }
13 if nx_offer_send(ol, 1, 1200) != NX_OF_OK { return 2 }
14 if nx_offer_accept(ol, 1, 1300) != NX_OF_OK { return 3 }
15 // offer 2 target 9 -> DECLINED
16 if nx_offer_open(ol, 2, 9, 200, 2000) != NX_OF_OK { return 4 }
17 if nx_offer_decline(ol, 2, 2100) != NX_OF_OK { return 5 }
18 // offer 3 target 11 -> PENDING
19 if nx_offer_open(ol, 3, 11, 300, 3000) != NX_OF_OK { return 6 }
20
21 // SAVE then reload into a brand-new ledger
22 if nx_offer_save(ol, "/tmp/nx_offers-" as *u8, 1) != 0 { return 7 }
23 let ol2: *NxOfferLedger = nx_offer_load("/tmp/nx_offers-" as *u8)
24
25 // counts + states survived
26 if nx_offer_count_by_state(ol2, NX_OF_ACCEPTED) != 1 { return 8 }
27 if nx_offer_count_by_state(ol2, NX_OF_DECLINED) != 1 { return 9 }
28 if nx_offer_count_by_state(ol2, NX_OF_PENDING) != 1 { return 10 }
29 if nx_offer_state(ol2, 1) != NX_OF_ACCEPTED { return 11 }
30 if nx_offer_state(ol2, 2) != NX_OF_DECLINED { return 12 }
31 if nx_offer_state(ol2, 3) != NX_OF_PENDING { return 13 }
32 if nx_offer_is_accepted(ol2, 7) != 1 { return 14 }
33 if nx_offer_is_accepted(ol2, 9) != 0 { return 15 }
34
35 // state machine resumes on the RELOADED ledger: send the pending offer 3
36 if nx_offer_send(ol2, 3, 3500) != NX_OF_OK { return 16 }
37 if nx_offer_state(ol2, 3) != NX_OF_SENT { return 17 }
38 // accepted offer 1 still terminal after reload
39 if nx_offer_send(ol2, 1, 3600) != NX_OF_ERR_BAD_TRANSITION { return 18 }
40
41 return 0
42}