nx_tg_autoseed_gate.nx source
↩ module page · 45 lines · 3909 B
1// nx_tg_autoseed_gate.nx -- proves tg_autoseed registers a COMPLETED download into the seeder registry with
2// the correct geometry: name, info_hash hex40, piece_length, total (== download.part FILE SIZE), srcpath --
3// and is IDEMPOTENT (calling twice yields ONE row). A wrong total/hex here would make us serve corrupt pieces
4// to peers, so this is a swarm-safety gate. license_tier: ORIGINAL depends: nx_torrent_get (tg_autoseed)
5import "nx_torrent_get.nx"
6
7func ag_p(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
8func if_s(v: i64) -> *u8 { if v==1 { return "1" as *u8 } return "0" as *u8 }
9// substring search: 1 if `ndl` (nlen bytes) occurs in hay[0..hlen), else 0
10func ag_has(hay: *u8, hlen: i64, ndl: *u8, nlen: i64) -> i64 {
11 var s: i64=0; while s+nlen<=hlen { var m: i64=1; var k: i64=0; while k<nlen { if hay[s+k]!=ndl[k] { m=0; k=nlen } else { k=k+1 } } if m==1 { return 1 } s=s+1 }
12 return 0
13}
14
15func main() -> i64 {
16 ag_p("TG-AUTOSEED-GATE authored=organ\n" as *u8)
17 sys_mkdir("/tmp/nx_as_test" as *u8, 0x1ff)
18 // a completed 3855-byte download.part (content irrelevant; tg_autoseed reads only its SIZE)
19 let pf: i64 = sys_openat_wr("/tmp/nx_as_test/download.part" as *u8, 0x1a4)
20 if pf<0 { ag_p("cannot create download.part verdict=RED\n" as *u8); sys_exit(1); return 1 }
21 let buf: *u8 = sys_mmap(4096); var i: i64=0; while i<3855 { buf[i]=(i%251) as u8; i=i+1 } cl_write_n(pf, buf, 3855); sys_close(pf)
22 // FULLY-complete torrent (tg_autoseed's guard requires done==npc): npc=1, plen=16384; download.done=1 byte set
23 let npf: i64 = sys_openat_wr("/tmp/nx_as_test/download.npc" as *u8, 0x1a4); if npf>=0 { let nbb: *u8=sys_mmap(16); nbb[0]=0 as u8; nbb[1]=0 as u8; nbb[2]=0 as u8; nbb[3]=1 as u8; nbb[4]=0 as u8; nbb[5]=0 as u8; nbb[6]=0x40 as u8; nbb[7]=0 as u8; cl_write_n(npf, nbb, 8); sys_close(npf) }
24 let dnf: i64 = sys_openat_wr("/tmp/nx_as_test/download.done" as *u8, 0x1a4); if dnf>=0 { let one: *u8=sys_mmap(4); one[0]=1 as u8; cl_write_n(dnf, one, 1); sys_close(dnf) }
25 // empty the test registry
26 let cf: i64 = sys_openat_wr("/tmp/nx_as_reg.conf" as *u8, 0x1a4); if cf>=0 { sys_close(cf) }
27 // info_hash = bytes 0..19 -> hex "000102...13"
28 let ih: *u8 = sys_mmap(24); i=0; while i<20 { ih[i]=i as u8; i=i+1 }
29 // call TWICE -> idempotent (one row)
30 tg_autoseed("/tmp/nx_as_test" as *u8, ih, 16384, "testtorrent" as *u8, "/tmp/nx_as_reg.conf" as *u8)
31 tg_autoseed("/tmp/nx_as_test" as *u8, ih, 16384, "testtorrent" as *u8, "/tmp/nx_as_reg.conf" as *u8)
32 let rf: i64 = sys_openat_rd("/tmp/nx_as_reg.conf" as *u8); let rb: *u8=sys_mmap(8192); let rn: i64=sys_read(rf,rb,8191); sys_close(rf)
33 var nl: i64=0; i=0; while i<rn { if rb[i]==(10 as u8) { nl=nl+1 } i=i+1 } // newline count == rows
34 let has_hex: i64 = ag_has(rb, rn, "000102030405060708090a0b0c0d0e0f10111213" as *u8, 40)
35 let has_total: i64 = ag_has(rb, rn, "\t3855\t" as *u8, 6) // total == the .part file size
36 let has_plen: i64 = ag_has(rb, rn, "\t16384\t" as *u8, 7)
37 let has_name: i64 = ag_has(rb, rn, "testtorrent\t" as *u8, 12)
38 let has_path: i64 = ag_has(rb, rn, "/download.part" as *u8, 14)
39 ag_p(" rows=" as *u8); if nl==1 { ag_p("1" as *u8) } else { ag_p("BAD" as *u8) }
40 ag_p(" hex=" as *u8); ag_p(if_s(has_hex)); ag_p(" total3855=" as *u8); ag_p(if_s(has_total)); ag_p(" plen=" as *u8); ag_p(if_s(has_plen)); ag_p(" name=" as *u8); ag_p(if_s(has_name)); ag_p(" path=" as *u8); ag_p(if_s(has_path)); ag_p("\n" as *u8)
41 if nl==1 { if has_hex==1 { if has_total==1 { if has_plen==1 { if has_name==1 { if has_path==1 {
42 ag_p("TG-AUTOSEED-GATE verdict=GREEN (completed torrent registered once, correct hex+plen+total-from-filesize+path; idempotent)\n" as *u8); sys_exit(0); return 0
43 } } } } } }
44 ag_p("TG-AUTOSEED-GATE verdict=RED\n" as *u8); sys_exit(1); return 1
45}