code wiki / _hdl_build / nx_transport_gate.nx
nx_transport_gate.nx source
↩ module page · 66 lines · 4003 B
1// nx_transport_gate.nx -- proves the SOVEREIGN transport CONTRACT (nx_transport). A frame handed to a
2// transport must come out the other side byte-identical + in FIFO order, and the open/send/recv/close
3// lifecycle + honest drops (oversized/full/closed -> -1, empty -> 0) must hold. Any transport adapter
4// (browser WebSocket, NishiOS datagram) MUST satisfy this -> it is the seam where WebSocket is just a
5// swappable pipe. Reference impl = the loopback ring. NEG-controls give the checker teeth. 0=GREEN/1=RED.
6import "nx_syscalls.nx"
7import "nx_transport.nx"
8
9func g_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
10func g_n(v: i64) -> i64 {
11 var m: i64=v; if m<0{g_p("-\x00" as *u8);m=0-m}
12 let t: *u8=sys_mmap(24); var k: i64=0; if m==0{t[0]=48 as u8;k=1} else {while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1}}
13 let o: *u8=sys_mmap(24); var j: i64=0; while j<k{o[j]=t[k-1-j];j=j+1} sys_write(1,o,k); return 0 }
14func chk(cond: i64, name: *u8, pp: *i64, tp: *i64) -> i64 {
15 tp[0]=tp[0]+1; g_p(name)
16 if cond==1 { pp[0]=pp[0]+1; g_p(" ok\n\x00" as *u8) } else { g_p(" FAIL\n\x00" as *u8) }
17 return 0 }
18func bytes_eq(a: *u8, al: i64, b: *u8, bl: i64) -> i64 {
19 if al != bl { return 0 }
20 var i: i64=0; while i<al { if a[i]!=b[i] { return 0 } i=i+1 }
21 return 1 }
22
23func main() -> i64 {
24 g_p("=== nx_transport_gate: SOVEREIGN transport contract -- frames move intact + in order, any pipe ===\n\x00" as *u8)
25 let pp: *i64 = sys_mmap(8) as *i64; pp[0]=0
26 let tp: *i64 = sys_mmap(8) as *i64; tp[0]=0
27 let mem: *u8 = sys_mmap(TP_REGION + 4096)
28 let h: *u8 = (tp_open(mem)) as *u8
29
30 // three distinct frames (a media V frame, an audio A frame, a chat C frame -- opaque to the transport)
31 let fa: *u8 = sys_mmap(16); fa[0]=0x56 as u8; fa[1]=1 as u8; fa[2]=2 as u8; fa[3]=3 as u8; fa[4]=4 as u8 // len 5
32 let fb: *u8 = sys_mmap(16); fb[0]=0x41 as u8; fb[1]=0x99 as u8; fb[2]=0x7f as u8 // len 3
33 let fc: *u8 = sys_mmap(16); var i: i64=0; while i<10 { fc[i]=(0x43 + i) as u8; i=i+1 } // len 10
34
35 chk(tp_send(h, fa, 5)==5, "send A -> 5\x00" as *u8, pp, tp)
36 chk(tp_send(h, fb, 3)==3, "send B -> 3\x00" as *u8, pp, tp)
37 chk(tp_send(h, fc, 10)==10, "send C -> 10\x00" as *u8, pp, tp)
38 chk(tp_pending(h)==3, "3 frames pending\x00" as *u8, pp, tp)
39
40 let out: *u8 = sys_mmap(4096)
41 // FIFO + byte-identical
42 let r1: i64 = tp_recv(h, out, 4096); chk(bytes_eq(out, r1, fa, 5), "recv A byte-identical (FIFO)\x00" as *u8, pp, tp)
43 let r2: i64 = tp_recv(h, out, 4096); chk(bytes_eq(out, r2, fb, 3), "recv B byte-identical\x00" as *u8, pp, tp)
44 let r3: i64 = tp_recv(h, out, 4096); chk(bytes_eq(out, r3, fc, 10), "recv C byte-identical\x00" as *u8, pp, tp)
45 chk(tp_recv(h, out, 4096)==0, "recv on empty -> 0 (not -1, not garbage)\x00" as *u8, pp, tp)
46
47 // NEG: oversized frame -> honest drop (-1), pending unchanged
48 chk(tp_send(h, fc, TP_FRAME_CAP + 1)==(0-1), "NEG oversized -> -1 drop\x00" as *u8, pp, tp)
49
50 // NEG: fill the ring, next send drops (-1)
51 var s: i64 = 0; var okfill: i64 = 1
52 while s < TP_MAX_FRAMES { if tp_send(h, fa, 5)!=5 { okfill=0 } s=s+1 }
53 chk(okfill==1, "fill ring to capacity\x00" as *u8, pp, tp)
54 chk(tp_send(h, fa, 5)==(0-1), "NEG full ring -> -1 drop\x00" as *u8, pp, tp)
55
56 // drain + close semantics
57 var d: i64 = 0; while d < TP_MAX_FRAMES { tp_recv(h, out, 4096); d=d+1 }
58 tp_close(h)
59 chk(tp_recv(h, out, 4096)==(0-1), "closed + empty -> -1\x00" as *u8, pp, tp)
60 chk(tp_send(h, fa, 5)==(0-1), "send after close -> -1\x00" as *u8, pp, tp)
61
62 g_p(" SCORE: \x00" as *u8); g_n(pp[0]); g_p("/\x00" as *u8); g_n(tp[0]); g_p("\n\x00" as *u8)
63 if pp[0]==tp[0] { g_p("TRANSPORT-GATE verdict=GREEN -- frames move intact+in-order over the sovereign contract; WebSocket is just one impl\n\x00" as *u8); return 0 }
64 g_p("TRANSPORT-GATE verdict=RED\n\x00" as *u8)
65 return 1
66}