code wiki / (root) / nx_fin_rules.nx

nx_fin_rules.nx source

↩ module page · 74 lines · 3231 B

1// nx_fin_rules.nx -- R6 of THE NISHI FINANCIAL ECOSYSTEM: the SOVEREIGN RULE-PACK STORE. This is what 2// makes the engine "robust for anyone / any jurisdiction / any insurer" (#6/#11/#25): the audit/debt rule 3// packs (NCCI bundle pairs, MUE limits, Medicare benchmarks, ...) are DATA stored in the sovereign 4// seg-store, keyed by a pack id, NOT hardcoded and NOT gate-supplied. A different jurisdiction/insurer = 5// a different stored pack; the same engine runs on whichever pack you load. 6// 7// A pack is an i64 array (callers interleave structured packs, e.g. bundle = [A0,B0,A1,B1,...], 8// MUE = [code0,max0,...], benchmark = [code0,allowed0,...]). Stored little-endian, length-prefixed, under 9// key "rule:<packid>:<kind>". Reuses nx_seg_store (append-only, content-safe). Commit segid = sys_now_us() 10// (per-op commits must NOT collide on ms-resolution -- see reference-segstore-segid-use-now-us). No floats, 11// no hardware writes. license_tier: ORIGINAL 12import "nx_syscalls.nx" 13import "nx_seg_store.nx" 14 15// little-endian 8-byte write of a (non-negative) i64; returns new offset. 16func rp_w64(buf: *u8, off: i64, v: i64) -> i64 { 17 var x: i64 = v 18 var i: i64 = 0 19 while i < 8 { buf[off + i] = (x & 0xff) as u8; x = x / 256; i = i + 1 } 20 return off + 8 21} 22// little-endian 8-byte read. 23func rp_r64(buf: *u8, off: i64) -> i64 { 24 var v: i64 = 0 25 var mul: i64 = 1 26 var i: i64 = 0 27 while i < 8 { v = v + (buf[off + i] as i64) * mul; mul = mul * 256; i = i + 1 } 28 return v 29} 30 31// build a storage key "rule:<packid>:<kind>" into out (NUL-terminated); returns length. 32func rp_key(packid: *u8, kind: *u8, out: *u8) -> i64 { 33 var o: i64 = 0 34 let p: *u8 = "rule:\x00" as *u8 35 var i: i64 = 0 36 while p[i] != (0 as u8) { out[o] = p[i]; o = o + 1; i = i + 1 } 37 i = 0 38 while packid[i] != (0 as u8) { out[o] = packid[i]; o = o + 1; i = i + 1 } 39 out[o] = 0x3a as u8; o = o + 1 // ':' 40 i = 0 41 while kind[i] != (0 as u8) { out[o] = kind[i]; o = o + 1; i = i + 1 } 42 out[o] = 0 as u8 43 return o 44} 45 46// STORE an i64 array `vals[0..n)` under (packid, kind). Returns 0 ok, negative on store error. 47func rp_put(prefix: *u8, packid: *u8, kind: *u8, vals: *i64, n: i64) -> i64 { 48 let key: *u8 = sys_mmap(256) 49 rp_key(packid, kind, key) 50 let buf: *u8 = sys_mmap(8 * (n + 2) + 16) 51 var o: i64 = rp_w64(buf, 0, n) 52 var i: i64 = 0 53 while i < n { o = rp_w64(buf, o, vals[i]); i = i + 1 } 54 let w: *i64 = ss_begin() 55 if ss_add(w, 1, key, buf, o) != 0 { return 0 - 10 } 56 if ss_commit(prefix, w, sys_now_us()) != 0 { return 0 - 11 } 57 return 0 58} 59 60// LOAD the i64 array stored under (packid, kind) into out[0..cap). Returns element count, or -1 if absent. 61func rp_get(prefix: *u8, packid: *u8, kind: *u8, out: *i64, cap: i64) -> i64 { 62 let key: *u8 = sys_mmap(256) 63 rp_key(packid, kind, key) 64 let h: *i64 = ss_open(prefix) 65 if (h as i64) == 0 { return 0 - 1 } 66 let pp: *i64 = sys_mmap(16) as *i64 67 let ll: *i64 = sys_mmap(16) as *i64 68 if ss_hget(h, key, pp, ll) != 1 { return 0 - 1 } 69 let b: *u8 = pp[0] as *u8 70 let n: i64 = rp_r64(b, 0) 71 var i: i64 = 0 72 while i < n { if i < cap { out[i] = rp_r64(b, 8 * (i + 1)) } i = i + 1 } 73 return n 74}