code wiki / _hdl_build / nx_room_fec_relay.nx

nx_room_fec_relay.nx source

↩ module page · 154 lines · 8452 B

1// nx_room_fec_relay.nx -- R1: FEC wired THROUGH the real sovereign relay. Composes the live 2// relay core (nx_room_relay: rr_init/rr_post/rr_find/arena) with the proven frame-FEC wire 3// protocol (nx_room_fec_wire). Each frame is sharded into k data + m parity DATAGRAMS; each 4// shard is posted to the relay under its own (room, peer*16+shard_idx) key; the receiver 5// collects whatever survived and reconstructs the WHOLE frame byte-exact from any k shards. 6// 7// WHY THIS IS THE REAL WIRE-IN (not another sim): it exercises the ACTUAL relay primitives 8// (rr_post stores each shard in the shared arena; rr_find locates survivors) -- the same code 9// nx_vroom_daemon runs live. The honest motivation: the live room speaks HTTP/TCP, which 10// HEAD-OF-LINE STALLS on intercontinental loss exactly like ARQ (TCP retransmits) -- a FEC 11// datagram path delivers the frame with ZERO retransmit. This gate proves the integrated path. 12// 13// SCOPE: proves daemon-side FEC transport end-to-end. The browser-client recovery shim + the 14// vd_stream wire-format change + DEPLOY to live :8446 = the operator-gated last mile. 15// main() is the SELF-VALIDATING GATE. Evidence -> knowledge/status/room_fec_relay.log. 16// license_tier: ORIGINAL 17import "nx_syscalls.nx" 18import "nx_room_relay.nx" 19import "nx_room_fec_wire.nx" 20 21const FRR_LOG: *u8 = "knowledge/status/room_fec_relay.log" 22 23func rw(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 24func rwn(fd: i64, v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{m=0-m;sys_write(fd,"-" as *u8,1)} let t: *u8=sys_mmap(28); 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} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(fd,bb,k); return 0 } 25 26// post one frame's surviving shards through the REAL relay. drop[i]==1 => that datagram is lost. 27func frr_send(st: *i64, arena: *u8, room: i64, basepeer: i64, blockid: i64, shards: *i64, n: i64, S: i64, drop: *i64, shardbuf: *u8) -> i64 { 28 var posted: i64=0 29 var i: i64=0 30 while i < n { 31 if drop[i]==0 { 32 var c: i64=0 33 while c < S { shardbuf[c] = shards[i*S+c] as u8; c=c+1 } 34 rr_post(st, arena, room, basepeer*16 + i, 1, blockid, shardbuf, S) 35 posted = posted + 1 36 } 37 i = i + 1 38 } 39 return posted 40} 41// receiver: collect surviving shards from the relay into collected[n*S]; present[i]=1 if arrived. 42func frr_collect(st: *i64, arena: *u8, room: i64, basepeer: i64, n: i64, S: i64, present: *i64, collected: *i64) -> i64 { 43 let slotb: i64 = st[1] 44 var got: i64=0 45 var i: i64=0 46 while i < n { 47 let s: i64 = rr_find(st, room, basepeer*16 + i) 48 if s >= 0 { 49 present[i]=1; got=got+1 50 var c: i64=0 51 while c < S { collected[i*S+c] = arena[s*slotb + c] as i64; c=c+1 } 52 } else { present[i]=0 } 53 i = i + 1 54 } 55 return got 56} 57 58func main() -> i64 { 59 let exp: *i64 = sys_mmap(8*512) as *i64 60 let log: *i64 = sys_mmap(8*256) as *i64 61 gf_init(exp, log) 62 63 let k: i64=8 64 let m: i64=2 65 let n: i64=k+m 66 let fl: i64=1000 67 let S: i64=(fl + k - 1) / k // 125 <= slotb 68 let G: *i64 = sys_mmap(8*n*k) as *i64 69 fec_build_G(exp, log, G, k, m) 70 71 let frame: *u8 = sys_mmap(fl) 72 var i: i64=0; while i < fl { frame[i]=((i*131 + 7) & 255) as u8; i=i+1 } 73 let data: *i64 = sys_mmap(8*k*S) as *i64 74 let shards: *i64 = sys_mmap(8*n*S) as *i64 75 fwire_encode(exp, log, G, frame, fl, k, m, S, data, shards) 76 77 // real relay state (shared arena, exactly like nx_vroom_daemon) 78 let NSLOTS: i64 = 64 79 let SLOTB: i64 = 256 80 let st: *i64 = sys_mmap((2 + 5*NSLOTS) * 8) as *i64 81 let arena: *u8 = sys_mmap(NSLOTS * SLOTB) 82 rr_init(st, NSLOTS, SLOTB) 83 84 let room: i64 = 0x1234 85 let basepeer: i64 = 100 86 let drop: *i64 = sys_mmap(8*n) as *i64 87 let present: *i64 = sys_mmap(8*n) as *i64 88 let collected: *i64 = sys_mmap(8*n*S) as *i64 89 let erased: *i64 = sys_mmap(8*n) as *i64 90 let out: *i64 = sys_mmap(8*k*S) as *i64 91 let fout: *u8 = sys_mmap(fl) 92 let shardbuf:*u8 = sys_mmap(SLOTB) 93 94 var ok: i64=1 95 96 // T1: 2 datagrams lost (<=m) -> frame recovered byte-exact THROUGH THE RELAY 97 var z: i64=0; while z < n { drop[z]=0; z=z+1 } 98 drop[2]=1; drop[7]=1 99 let posted1: i64 = frr_send(st, arena, room, basepeer, 1, shards, n, S, drop, shardbuf) 100 let got1: i64 = frr_collect(st, arena, room, basepeer, n, S, present, collected) 101 let rc1: i64 = fwire_decode(exp, log, G, collected, present, k, m, S, fl, fout, erased, out) 102 var t1: i64=0; if rc1==0 { if frame_eq(fout, frame, fl)==1 { t1=1 } } 103 if t1==0 { ok=0 } 104 105 // T2 (honest bound): 3 datagrams lost (>m) -> unrecoverable through the relay (k-1 survive) 106 rr_init(st, NSLOTS, SLOTB) // fresh relay 107 z=0; while z < n { drop[z]=0; z=z+1 } 108 drop[1]=1; drop[4]=1; drop[9]=1 109 frr_send(st, arena, room, basepeer, 2, shards, n, S, drop, shardbuf) 110 frr_collect(st, arena, room, basepeer, n, S, present, collected) 111 let rc2: i64 = fwire_decode(exp, log, G, collected, present, k, m, S, fl, fout, erased, out) 112 var t2: i64=0; if rc2==(0-1) { t2=1 } 113 if t2==0 { ok=0 } 114 115 // T3: lossless path (0 dropped) also delivers byte-exact (no regression to the clean case) 116 rr_init(st, NSLOTS, SLOTB) 117 z=0; while z < n { drop[z]=0; z=z+1 } 118 frr_send(st, arena, room, basepeer, 3, shards, n, S, drop, shardbuf) 119 frr_collect(st, arena, room, basepeer, n, S, present, collected) 120 let rc3: i64 = fwire_decode(exp, log, G, collected, present, k, m, S, fl, fout, erased, out) 121 var t3: i64=0; if rc3==0 { if frame_eq(fout, frame, fl)==1 { t3=1 } } 122 if t3==0 { ok=0 } 123 124 // T4 NEG-CONTROL: the relay actually carried the bytes -- corrupt one stored shard in the 125 // arena and the recovered frame MUST differ (proves we read real relay bytes, not the source). 126 rr_init(st, NSLOTS, SLOTB) 127 z=0; while z < n { drop[z]=0; z=z+1 } 128 frr_send(st, arena, room, basepeer, 4, shards, n, S, drop, shardbuf) 129 let s0: i64 = rr_find(st, room, basepeer*16 + 0) // shard 0 slot in the arena 130 arena[s0*SLOTB + 0] = (arena[s0*SLOTB + 0] ^ 0xFF) as u8 // corrupt a stored byte 131 frr_collect(st, arena, room, basepeer, n, S, present, collected) 132 fwire_decode(exp, log, G, collected, present, k, m, S, fl, fout, erased, out) 133 var t4: i64=0; if frame_eq(fout, frame, fl)==0 { t4=1 } // corrupted arena -> wrong frame 134 if t4==0 { ok=0 } 135 136 rw(1, "=== nx_room_fec_relay -- FEC wired THROUGH the real relay (rr_post/rr_find/arena) ===\n" as *u8) 137 rw(1, " frame=" as *u8); rwn(1, fl); rw(1, "B k=" as *u8); rwn(1, k); rw(1, " m=" as *u8); rwn(1, m); rw(1, " shards=" as *u8); rwn(1, n); rw(1, " (each an independent datagram via rr_post)\n" as *u8) 138 rw(1, " T1 lose 2 datagrams (<=m): posted=" as *u8); rwn(1, posted1); rw(1, " got=" as *u8); rwn(1, got1); rw(1, " -> frame byte-exact THROUGH RELAY: " as *u8); if t1==1 { rw(1,"PASS" as *u8) } else { rw(1,"FAIL" as *u8) } 139 rw(1, "\n T2 lose 3 (>m): unrecoverable (honest bound): " as *u8); if t2==1 { rw(1,"PASS" as *u8) } else { rw(1,"FAIL" as *u8) } 140 rw(1, "\n T3 clean (0 lost): byte-exact (no regression): " as *u8); if t3==1 { rw(1,"PASS" as *u8) } else { rw(1,"FAIL" as *u8) } 141 rw(1, "\n T4 NEG: corrupt a STORED arena byte -> recovered frame differs (bytes came from the relay): " as *u8); if t4==1 { rw(1,"PASS" as *u8) } else { rw(1,"FAIL" as *u8) } 142 rw(1, "\n CONTRAST: same loss over HTTP/TCP (the live path) head-of-line STALLS +1 RTT; this datagram-FEC path delivers with 0 retransmit.\n" as *u8) 143 rw(1, " SCOPE: daemon-side transport proven; browser recovery shim + vd_stream wire-format + DEPLOY = operator-gated.\n" as *u8) 144 if ok==1 { rw(1, "VERDICT: GREEN (FEC frames survive datagram loss through the real sovereign relay)\n" as *u8) } else { rw(1, "VERDICT: RED\n" as *u8) } 145 146 let lfd: i64 = sys_openat_append(FRR_LOG, 420) 147 if lfd >= 0 { 148 rw(lfd, "ROOMFECRELAY fl=1000 k=8 m=2 t1=" as *u8); rwn(lfd, t1); rw(lfd, " t2=" as *u8); rwn(lfd, t2); rw(lfd, " t3=" as *u8); rwn(lfd, t3); rw(lfd, " t4=" as *u8); rwn(lfd, t4) 149 if ok==1 { rw(lfd, " verdict=GREEN\n" as *u8) } else { rw(lfd, " verdict=RED\n" as *u8) } 150 sys_close(lfd) 151 } 152 if ok==1 { sys_exit(0) } else { sys_exit(1) } 153 return 0 154}