nx_pex_harvest_gate.nx source
↩ module page · 82 lines · 4750 B
1// nx_pex_harvest_gate.nx -- SOVEREIGN gate for the PEX HARVEST PLUMBING in the worker
2// (tg_pex_append + tg_pex_merge). nx_pex_gate already proves the BEP-11 protocol on a real socket;
3// THIS proves the worker's shared-file integration: forked children append harvested peers under
4// flock, the parent merges them into the peer set DEDUPED, concurrent appends keep the 6-byte
5// framing intact, and a torn tail record is ignored (never OOB). Imports the worker (main stripped)
6// so it exercises the REAL functions, not a copy (the nx_torrent_stream_gate pattern). license_tier: ORIGINAL
7//
8// module: nishi-core.torrent.pex_harvest_gate
9// depends: nishi-core.torrent.get
10import "nx_torrent_get.nx"
11import "nx_gate_verdict.nx"
12
13func hg_w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
14func hg_wn(v: i64) -> i64 {
15 let t: *u8 = sys_mmap(28); var m: i64 = v; if m < 0 { m = 0 - m; sys_write(1, "-" as *u8, 1) }
16 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 }
17 let b: *u8 = sys_mmap(28); var i: i64 = 0; while i < k { b[i] = t[k - 1 - i]; i = i + 1 }; sys_write(1, b, k); return 0
18}
19
20func main() -> i64 {
21 let path: *u8 = "/tmp/_nx_pex_harvest.pex" as *u8
22 let zf: i64 = sys_openat_wr(path, 0x1a4); if zf >= 0 { sys_close(zf) } // start empty (O_TRUNC)
23
24 let ips: *i64 = sys_mmap(8 * 128) as *i64; let pts: *i64 = sys_mmap(8 * 128) as *i64
25 ips[0] = (1 << 24) | (2 << 16) | (3 << 8) | 4; pts[0] = 6881
26 ips[1] = (10 << 24) | (20 << 16) | (30 << 8) | 40; pts[1] = 51413
27 ips[2] = (192 << 24) | (168 << 16) | (0 << 8) | 9; pts[2] = 12345
28 tg_pex_append(path, ips, pts, 3)
29
30 // T1: merge harvested peers into an empty set -> 3 peers, exact addresses
31 let mip: *i64 = sys_mmap(8 * 128) as *i64; let mpo: *i64 = sys_mmap(8 * 128) as *i64
32 let h1: i64 = tg_pex_merge(path, mip, mpo, 0, 80)
33 var t1: i64 = 0
34 if h1 == 3 { if mip[0] == ips[0] { if mpo[0] == pts[0] { if mip[2] == ips[2] { if mpo[2] == pts[2] { t1 = 1 } } } } }
35
36 // T2: merge AGAIN into the now-populated set -> dedup, still 3 (no duplicate connects)
37 let h2: i64 = tg_pex_merge(path, mip, mpo, h1, 80)
38 var t2: i64 = 0; if h2 == 3 { t2 = 1 }
39
40 // T3: CONCURRENT append integrity -- 20 forked children each append 1 distinct peer under flock;
41 // parent merges -> all 20 present, no lost/torn record (the multi-child harvest path is real).
42 let path2: *u8 = "/tmp/_nx_pex_harvest_conc.pex" as *u8
43 let zf2: i64 = sys_openat_wr(path2, 0x1a4); if zf2 >= 0 { sys_close(zf2) }
44 var c: i64 = 0
45 while c < 20 {
46 let pid: i64 = sys_fork()
47 if pid == 0 {
48 let cip: *i64 = sys_mmap(16) as *i64; let cpo: *i64 = sys_mmap(16) as *i64
49 cip[0] = (100 << 24) | (0 << 16) | (0 << 8) | c; cpo[0] = 20000 + c
50 tg_pex_append(path2, cip, cpo, 1)
51 sys_exit(0)
52 }
53 c = c + 1
54 }
55 var rc: i64 = 0; while rc < 20 { let stx: *i64 = sys_mmap(16) as *i64; sys_wait4(0 - 1, stx, 0); rc = rc + 1 }
56 let cmip: *i64 = sys_mmap(8 * 128) as *i64; let cmpo: *i64 = sys_mmap(8 * 128) as *i64
57 let h3: i64 = tg_pex_merge(path2, cmip, cmpo, 0, 80)
58 var t3: i64 = 0; if h3 == 20 { t3 = 1 }
59
60 // T4: torn-tail robustness -- append a 4-byte fragment, merge -> still 20 (fragment ignored, no OOB)
61 let frag: i64 = __syscall(SYS_OPENAT, AT_FDCWD, path2 as i64, 0x441, 0x1a4, 0, 0)
62 if frag >= 0 { let fb: *u8 = sys_mmap(8); fb[0] = 9 as u8; fb[1] = 9 as u8; fb[2] = 9 as u8; fb[3] = 9 as u8; cl_write_n(frag, fb, 4); sys_close(frag) }
63 let tmip: *i64 = sys_mmap(8 * 128) as *i64; let tmpo: *i64 = sys_mmap(8 * 128) as *i64
64 let h4: i64 = tg_pex_merge(path2, tmip, tmpo, 0, 80)
65 var t4: i64 = 0; if h4 == 20 { t4 = 1 }
66
67 hg_w("PEX-HARVEST-GATE t1_append_merge=" as *u8); hg_wn(t1)
68 hg_w(" t2_dedup=" as *u8); hg_wn(t2)
69 hg_w(" t3_concurrent_append=" as *u8); hg_wn(t3); hg_w(" got=" as *u8); hg_wn(h3)
70 hg_w(" t4_torn_tail_ignored=" as *u8); hg_wn(t4)
71 var pass: i64 = 0
72 if t1 == 1 { if t2 == 1 { if t3 == 1 { if t4 == 1 { pass = 1 } } } }
73 // MIGRATED onto nx_gate_verdict by nx_gate_dry_apply (D001, minimal form): every check
74 // row above is untouched, so the PASS/FAIL vector cannot change; only the hand-rolled
75 // verdict emission is replaced by the ONE shared base class. Proven by nx_gate_migrate verify.
76 let ctr__dry: *i64 = gv_ctr()
77 ctr__dry[0] = pass
78 ctr__dry[1] = 1
79 let rc__dry: i64 = gv_verdict("PEX-HARVEST-GATE" as *u8, ctr__dry, "teeth unchanged; verdict emission migrated onto the shared base class" as *u8)
80 sys_exit(rc__dry)
81 return rc__dry
82}