code wiki / _hdl_build / nx_dist_publish_gate.nx

nx_dist_publish_gate.nx source

↩ module page · 61 lines · 4123 B

1// nx_dist_publish_gate.nx -- proves the HUB-AS-ENABLER multi-transport publish orchestrator (nx_dist_publish): 2// one content-id (SHA-256 of the bytes) fans a landed file out into EVERY transport at once -- magnet (swarm), 3// direct HTTPS (download), content-addressed API, and the Napster-style index row. Uses the NIST sha256("abc") 4// vector so the content-id is checkable by hand. expect_exit: 0 license_tier: ORIGINAL 5import "nx_dist_publish.nx" 6import "nx_g_puts_lib.nx" 7import "nx_syscalls.nx" 8 9func g_len(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 10func g_streq(a: *u8, b: *u8) -> i64 { var i: i64=0; while a[i]!=(0 as u8) { if a[i]!=b[i] { return 0 } i=i+1 } if b[i]!=(0 as u8) { return 0 } return 1 } 11func g_starts(hay: *u8, pre: *u8) -> i64 { var i: i64=0; while pre[i]!=(0 as u8) { if hay[i]!=pre[i] { return 0 } i=i+1 } return 1 } 12func ck(name: *u8, cond: i64, st: *i64) -> i64 { st[1]=st[1]+1; if cond==1 { st[0]=st[0]+1; g_puts(" [OK] " as *u8) } else { g_puts(" [FAIL] " as *u8) } g_puts(name); g_puts("\n" as *u8); return 0 } 13 14func main() -> i64 { 15 g_puts("=== nx_dist_publish_gate: the hub-as-ENABLER (one file -> every transport) ===\n" as *u8) 16 let st: *i64 = sys_mmap(64) as *i64; st[0]=0; st[1]=0 17 18 // land a file: NIST vector "abc" 19 let fp: *u8 = "/tmp/dp_fixture_abc.bin" as *u8 20 let fd: i64 = sys_openat_wr(fp, 0x1a4) 21 if fd < 0 { g_puts("fixture open FAILED\n" as *u8); sys_exit(1); return 1 } 22 sys_write(fd, "abc" as *u8, 3); sys_close(fd) 23 24 let CID: *u8 = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" as *u8 25 let cid: *u8 = sys_mmap(128) 26 let sz: i64 = dp_content_id(fp, cid) 27 g_puts(" content-id="); g_puts(cid); g_puts(" bytes="); if sz==3 { g_puts("3" as *u8) } else { g_puts("?" as *u8) } g_puts("\n" as *u8) 28 ck("T1 content-id = sha256(\"abc\") NIST vector, size=3" as *u8, (g_streq(cid, CID) & (sz==3)), st) 29 30 let mag: *u8 = sys_mmap(512); dp_magnet(cid, "model.bin" as *u8, mag) 31 g_puts(" magnet="); g_puts(mag); g_puts("\n" as *u8) 32 ck("T2 magnet = BitTorrent v2 urn:btmh:1220<cid>&dn=model.bin" as *u8, 33 g_streq(mag, "magnet:?xt=urn:btmh:1220ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad&dn=model.bin" as *u8), st) 34 35 let url: *u8 = sys_mmap(512); dp_dl_url(cid, "model.bin" as *u8, url) 36 g_puts(" download="); g_puts(url); g_puts("\n" as *u8) 37 ck("T3 direct HTTPS download url (content-addressed path)" as *u8, 38 g_streq(url, "https://nishifamily.com/dist/ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad/model.bin" as *u8), st) 39 40 let api: *u8 = sys_mmap(256); dp_api(cid, api) 41 g_puts(" api="); g_puts(api); g_puts("\n" as *u8) 42 ck("T4 content-addressed API endpoint /api/dist/<cid>" as *u8, 43 g_streq(api, "/api/dist/ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" as *u8), st) 44 45 let irow: *u8 = sys_mmap(1024); dp_index_row("model.bin" as *u8, cid, 3, mag, url, irow) 46 // Napster-style registry row: starts with the name, TAB-delimited, carries the cid. 47 var row_ok: i64 = 0 48 if g_starts(irow, "model.bin" as *u8)==1 { if irow[9]==9 as u8 { row_ok=1 } } 49 ck("T5 distribution INDEX row (name<TAB>cid<TAB>size<TAB>magnet<TAB>url)" as *u8, row_ok, st) 50 51 // T6 the enabler property: ALL four transports materialized from the SINGLE content-id, none empty. 52 var all4: i64 = 0 53 if g_len(mag)>20 { if g_len(url)>20 { if g_len(api)>10 { if g_len(irow)>20 { all4=1 } } } } 54 ck("T6 ENABLER: one landed file -> magnet + download + api + index, all non-empty" as *u8, all4, st) 55 56 g_puts("\n PASS " as *u8); if st[0]==st[1] { g_puts("ALL" as *u8) } else { g_puts("PARTIAL" as *u8) } 57 g_puts(" ("); let p: *u8=sys_mmap(8); var a: i64=st[0]; var b: i64=st[1] 58 p[0]=(48+a) as u8; p[1]=47 as u8; p[2]=(48+b) as u8; p[3]=0 as u8; g_puts(p); g_puts(")\n" as *u8) 59 if st[0]==st[1] { g_puts("=== GREEN (the hub is an ENABLER: data lands -> torrent + download + API + index) ===\n" as *u8); sys_exit(0); return 0 } 60 g_puts("=== RED ===\n" as *u8); sys_exit(1); return 1 61}