code wiki / _hdl_build / nx_adnet_bill_gate.nx
nx_adnet_bill_gate.nx source
↩ module page · 105 lines · 4684 B
1// nx_adnet_bill_gate.nx -- GATE for CPM/CPC billing math (nx_adnet_bill). Proves exact integer math,
2// the HONEST-BILLING refusals (missing rate / negative count -> REFUSED, never a fabricated charge),
3// the no-charge-by-default zero case, the budgetable flat ceiling, per-AD log counting, and that the
4// refusal is LOAD-BEARING through the invoice line (negative control). expect_exit: 0
5// license_tier: ORIGINAL
6import "nx_syscalls.nx"
7import "_hdl_build/nx_adnet_bill.nx"
8
9func bg_puts(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
10func bg_putn(v: i64) -> i64 { var m: i64 = v; if m < 0 { bg_puts("-" as *u8); m = 0 - m } let t: *u8 = sys_mmap(24); var k: i64 = 0; if m == 0 { t[0] = 48 as u8; k = 1 } while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } let o: *u8 = sys_mmap(24); var i: i64 = 0; while i < k { o[i] = t[k - 1 - i]; i = i + 1 } sys_write(1, o, k); return 0 }
11func bg_cat(dst: *u8, off: i64, s: *u8) -> i64 { var o: i64 = off; var i: i64 = 0; while s[i] != (0 as u8) { dst[o] = s[i]; o = o + 1; i = i + 1 } return o }
12
13func bg_has(hay: *u8, n: i64, needle: *u8) -> i64 {
14 var nl: i64 = 0
15 while needle[nl] != (0 as u8) { nl = nl + 1 }
16 if nl == 0 { return 0 }
17 var i: i64 = 0
18 while i + nl <= n {
19 var j: i64 = 0
20 var eq: i64 = 1
21 while j < nl { if hay[i + j] != needle[j] { eq = 0; break } j = j + 1 }
22 if eq == 1 { return 1 }
23 i = i + 1
24 }
25 return 0
26}
27
28func main() -> i64 {
29 bg_puts("=== nx_adnet_bill_gate ===\n" as *u8)
30 var pass: i64 = 0
31 var fail: i64 = 0
32
33 // T1 exact integer math
34 let t1: i64 = anb_total_milli(12345, 7, 2000, 250)
35 if t1 == 26440 { pass = pass + 1 } else { fail = fail + 1; bg_puts("T1 FAIL math\n" as *u8) }
36
37 // T2 refusals (missing rate / negative count)
38 var t2: i64 = 1
39 if anb_total_milli(100, 5, 0 - 1, 250) != (0 - 1) { t2 = 0 }
40 if anb_total_milli(100, 5, 2000, 0 - 1) != (0 - 1) { t2 = 0 }
41 if anb_total_milli(0 - 1, 5, 2000, 250) != (0 - 1) { t2 = 0 }
42 if t2 == 1 { pass = pass + 1 } else { fail = fail + 1; bg_puts("T2 FAIL refusals\n" as *u8) }
43
44 // T3 zero counts -> zero due (no-charge default)
45 let t3: i64 = anb_total_milli(0, 0, 2000, 250)
46 if t3 == 0 { pass = pass + 1 } else { fail = fail + 1; bg_puts("T3 FAIL zero default\n" as *u8) }
47
48 // T4 budgetable ceiling
49 var t4: i64 = 1
50 if anb_capped(26440, 20000) != 20000 { t4 = 0 }
51 if anb_capped(26440, 0 - 1) != 26440 { t4 = 0 }
52 if anb_capped(0 - 1, 100) != (0 - 1) { t4 = 0 }
53 if t4 == 1 { pass = pass + 1 } else { fail = fail + 1; bg_puts("T4 FAIL ceiling\n" as *u8) }
54
55 // T5 per-AD log counting
56 let logb: *u8 = sys_mmap(256)
57 var ln: i64 = 0
58 ln = bg_cat(logb, ln, "h1" as *u8)
59 logb[ln] = 10 as u8
60 ln = ln + 1
61 ln = bg_cat(logb, ln, "h1" as *u8)
62 logb[ln] = 10 as u8
63 ln = ln + 1
64 ln = bg_cat(logb, ln, "h2" as *u8)
65 logb[ln] = 10 as u8
66 ln = ln + 1
67 ln = bg_cat(logb, ln, "h1" as *u8)
68 logb[ln] = 10 as u8
69 ln = ln + 1
70 var t5: i64 = 1
71 if anb_count(logb, ln, "h1" as *u8) != 3 { t5 = 0 }
72 if anb_count(logb, ln, "h2" as *u8) != 1 { t5 = 0 }
73 if anb_count(logb, ln, "zz" as *u8) != 0 { t5 = 0 }
74 if t5 == 1 { pass = pass + 1 } else { fail = fail + 1; bg_puts("T5 FAIL log counts\n" as *u8) }
75
76 // T6 invoice line + LOAD-BEARING refusal (negative control)
77 let line: *u8 = sys_mmap(512)
78 let w1: i64 = anb_invoice_line("h1" as *u8, 3, 1, 2000, 250, 0 - 1, line, 512)
79 var t6: i64 = 1
80 if w1 <= 0 { t6 = 0 }
81 if bg_has(line, w1, "id=h1 imps=3 clicks=1 due_milli=256" as *u8) == 0 { t6 = 0 }
82 let w2: i64 = anb_invoice_line("h1" as *u8, 3, 1, 0 - 1, 250, 0 - 1, line, 512)
83 if w2 <= 0 { t6 = 0 }
84 if bg_has(line, w2, "due_milli=REFUSED" as *u8) == 0 { t6 = 0 }
85 if t6 == 1 { pass = pass + 1 } else { fail = fail + 1; bg_puts("T6 FAIL invoice line\n" as *u8) }
86
87 bg_puts("pass=" as *u8)
88 bg_putn(pass)
89 bg_puts(" fail=" as *u8)
90 bg_putn(fail)
91 bg_puts("\n" as *u8)
92
93 let log2: *u8 = sys_mmap(256)
94 var lo: i64 = 0
95 if fail == 0 { lo = bg_cat(log2, lo, "ADNETBILL authored=organ verdict=GREEN" as *u8) } else { lo = bg_cat(log2, lo, "ADNETBILL authored=organ verdict=RED" as *u8) }
96 log2[lo] = 10 as u8
97 lo = lo + 1
98 let fd: i64 = sys_openat_wr("knowledge/status/adnet_bill.log" as *u8, 0x1A4)
99 if fd >= 0 { sys_write(fd, log2, lo); sys_close(fd) }
100
101 if fail == 0 { bg_puts("=== ADNET-BILL-GATE verdict=GREEN ===\n" as *u8); sys_exit(0); return 0 }
102 bg_puts("=== ADNET-BILL-GATE verdict=RED ===\n" as *u8)
103 sys_exit(1)
104 return 1
105}