nx_rights_ledger_store.nx source
↩ module page · 43 lines · 1836 B
1// nx_rights_ledger_store.nx -- RUNG 2: durability for the rights ledger.
2//
3// Persists the ledger through nx_seg_store (append-only, crash-safe temp->
4// rename commit) so a license SURVIVES a restart on the NAS. The snapshot
5// value is the raw contiguous row buffer (the row count is derived from the
6// value length, so no header is needed); each save is a NEW immutable segment
7// -> ss_scan_all over "rights:snapshot" is the full audit history.
8//
9// HONEST scope: this is a whole-ledger snapshot under one key. Per-row
10// content-addressed records (a canon_cid CID per license) + key-level history
11// = rung 2b (flagged, NOT a silent cap).
12// license_tier: ORIGINAL
13
14import "nx_syscalls.nx"
15import "nx_rights_ledger.nx"
16import "nx_seg_store.nx"
17
18// Save the whole ledger as one append-only snapshot segment.
19func nx_rights_save(l: *NxRightsLedger, prefix: *u8, segid: i64) -> i64 {
20 let w: *i64 = ss_begin()
21 let vbytes: i64 = (l.count as i64) * NX_RL_ROW_BYTES
22 ss_add(w, 1, "rights:snapshot" as *u8, (l.rows as i64) as *u8, vbytes)
23 return ss_commit(prefix, w, segid)
24}
25
26// Reload the ledger from the store (simulated restart). Absent store ->
27// a fresh empty ledger (default-deny everything).
28func nx_rights_load(prefix: *u8) -> *NxRightsLedger {
29 let h: *i64 = ss_open(prefix)
30 let ptrout: *i64 = sys_mmap(16) as *i64
31 let lenout: *i64 = sys_mmap(16) as *i64
32 let rc: i64 = ss_hget(h, "rights:snapshot" as *u8, ptrout, lenout)
33 if rc != 1 { return nx_rights_ledger_new(16) }
34 let blob: *u8 = ptrout[0] as *u8
35 let vbytes: i64 = lenout[0]
36 let n: nx_size = vbytes / NX_RL_ROW_BYTES
37 let l: *NxRightsLedger = nx_rights_ledger_new(n + 16)
38 let dst: *u8 = (l.rows as i64) as *u8
39 var i: i64 = 0
40 while i < vbytes { dst[i] = blob[i]; i = i + 1 }
41 l.count = n
42 return l
43}