code wiki / (root) / nx_rights_ledger_store_gate.nx

nx_rights_ledger_store_gate.nx source

↩ module page · 38 lines · 1936 B

1// nx_rights_ledger_store_gate.nx -- durability gate for the rights ledger. 2// 3// Proves a license SURVIVES restart: grant + revoke in memory -> save to the 4// append-only store -> LOAD into a BRAND-NEW ledger (the simulated restart) -> 5// every is_licensed answer is identical, including the revoke and the expiry. 6// Returns 0 iff durable + correct; a nonzero code pinpoints the first failure. 7 8import "nx_syscalls.nx" 9import "nx_rights_ledger.nx" 10import "nx_rights_ledger_store.nx" 11 12func main() -> i64 { 13 let l: *NxRightsLedger = nx_rights_ledger_new(16) 14 if nx_rights_grant(l, 1, 7, NX_USE_MERCH, 1000, 10000) != NX_RL_OK { return 1 } 15 if nx_rights_grant(l, 2, 7, NX_USE_WEB_GALLERY, 2000, 10000) != NX_RL_OK { return 2 } 16 if nx_rights_revoke(l, 2, 4000) != NX_RL_OK { return 3 } 17 if nx_rights_grant(l, 3, 9, NX_USE_AD_CAMPAIGN, 3000, 10000) != NX_RL_OK { return 4 } 18 19 // persist, then reopen from disk into a fresh ledger (simulated restart) 20 if nx_rights_save(l, "/tmp/nx_rl_dur-" as *u8, 1) != 0 { return 5 } 21 let l2: *NxRightsLedger = nx_rights_load("/tmp/nx_rl_dur-" as *u8) 22 23 // count survived 24 if nx_rights_count(l2) != 3 { return 6 } 25 // a live grant survived the restart 26 if nx_rights_is_licensed(l2, 7, NX_USE_MERCH, 5000) != 1 { return 7 } 27 // the REVOKE survived (still DENY after reload) 28 if nx_rights_is_licensed(l2, 7, NX_USE_WEB_GALLERY, 5000) != 0 { return 8 } 29 if nx_rights_state(l2, 7, NX_USE_WEB_GALLERY, 5000) != NX_RL_REVOKED { return 9 } 30 // a second talent's grant survived 31 if nx_rights_is_licensed(l2, 9, NX_USE_AD_CAMPAIGN, 5000) != 1 { return 10 } 32 // expiry is still computed correctly on the reloaded ledger 33 if nx_rights_is_licensed(l2, 7, NX_USE_MERCH, 99999) != 0 { return 11 } 34 // default-deny still holds for a never-granted talent, post-reload 35 if nx_rights_is_licensed(l2, 42, NX_USE_MERCH, 5000) != 0 { return 12 } 36 37 return 0 38}