code wiki / _hdl_build / nx_dist_index_gate.nx

nx_dist_index_gate.nx source

↩ module page · 66 lines · 4225 B

1// nx_dist_index_gate.nx -- proves the Napster-style registry end to end, composing the ENABLER (nx_dist_publish) 2// with the INDEX (nx_dist_index): three files land -> content-ids -> published to the registry -> peers announce 3// they hold one -> a searcher DISCOVERS content by name and learns WHO HAS IT by cid. Idempotent + append-only. 4// expect_exit: 0 license_tier: ORIGINAL 5import "nx_dist_publish.nx" 6import "nx_g_puts_lib.nx" 7import "nx_dist_index.nx" 8import "nx_syscalls.nx" 9 10func g_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 } 11func g_fixture(path: *u8, content: *u8, n: i64) -> i64 { let fd: i64=sys_openat_wr(path, 0x1a4); if fd<0 { return 0-1 } sys_write(fd, content, n); sys_close(fd); return 0 } 12 13func main() -> i64 { 14 g_puts("=== nx_dist_index_gate: the Napster-style registry (enabler + index, end to end) ===\n" as *u8) 15 let st: *i64 = sys_mmap(64) as *i64; st[0]=0; st[1]=0 16 17 g_fixture("/tmp/di_f1.bin" as *u8, "abc" as *u8, 3) 18 g_fixture("/tmp/di_f2.bin" as *u8, "abcd" as *u8, 4) 19 g_fixture("/tmp/di_f3.bin" as *u8, "hello" as *u8, 5) 20 let cid1: *u8 = sys_mmap(128); dp_content_id("/tmp/di_f1.bin" as *u8, cid1) 21 let cid2: *u8 = sys_mmap(128); dp_content_id("/tmp/di_f2.bin" as *u8, cid2) 22 let cid3: *u8 = sys_mmap(128); dp_content_id("/tmp/di_f3.bin" as *u8, cid3) 23 24 let creg: *u8 = sys_mmap(65536); var clen: i64=0 25 let preg: *u8 = sys_mmap(65536); var plen: i64=0 26 let mag: *u8 = sys_mmap(512); let url: *u8 = sys_mmap(512) 27 28 // PUBLISH three landed files through the enabler -> the index 29 dp_magnet(cid1, "model.bin" as *u8, mag); dp_dl_url(cid1, "model.bin" as *u8, url) 30 clen = di_publish(creg, clen, "model.bin" as *u8, cid1, 3, mag, url) 31 dp_magnet(cid2, "dataset.tar" as *u8, mag); dp_dl_url(cid2, "dataset.tar" as *u8, url) 32 clen = di_publish(creg, clen, "dataset.tar" as *u8, cid2, 4, mag, url) 33 dp_magnet(cid3, "hello.txt" as *u8, mag); dp_dl_url(cid3, "hello.txt" as *u8, url) 34 clen = di_publish(creg, clen, "hello.txt" as *u8, cid3, 5, mag, url) 35 36 g_ck("T1 three items published to the registry" as *u8, di_count_rows(creg, clen)==3, st) 37 38 // idempotent: re-publishing the same cid is a no-op (additive-only, no dup) 39 dp_magnet(cid1, "model.bin" as *u8, mag); dp_dl_url(cid1, "model.bin" as *u8, url) 40 let clen2: i64 = di_publish(creg, clen, "model.bin" as *u8, cid1, 3, mag, url) 41 g_ck("T2 duplicate cid is idempotent (still 3 rows)" as *u8, (clen2==clen) & (di_count_rows(creg, clen2)==3), st) 42 43 // ANNOUNCE: three peers hold model.bin 44 plen = di_announce(preg, plen, cid1, "peerA@lan" as *u8) 45 plen = di_announce(preg, plen, cid1, "peerB@wan" as *u8) 46 plen = di_announce(preg, plen, cid1, "peerC@hub2" as *u8) 47 g_ck("T3 three peers announced they hold model.bin" as *u8, di_count_peers(preg, plen, cid1)==3, st) 48 49 g_ck("T4 search 'dataset' discovers exactly 1 item" as *u8, di_search_name(creg, clen, "dataset" as *u8)==1, st) 50 51 let out: *u8 = sys_mmap(512); let rl: i64 = di_lookup(creg, clen, cid2, out) 52 g_ck("T5 lookup by cid returns the dataset.tar row" as *u8, (rl>0) & (di_contains(out, rl, "dataset.tar" as *u8)), st) 53 54 // the Napster property: discover WHAT by name + WHO-HAS-IT by cid, in one registry 55 g_ck("T6 NAPSTER: discover content by name AND its holders by cid" as *u8, 56 (di_search_name(creg, clen, "model" as *u8)>=1) & (di_count_peers(preg, plen, cid1)==3), st) 57 58 g_puts(" --- content registry (what exists) ---\n" as *u8); sys_write(1, creg, clen) 59 g_puts(" --- peer registry (who has what) ---\n" as *u8); sys_write(1, preg, plen) 60 61 g_puts("\n PASS " as *u8); if st[0]==st[1] { g_puts("ALL" as *u8) } else { g_puts("PARTIAL" as *u8) } 62 let p: *u8=sys_mmap(8); p[0]=(48+st[0]) as u8; p[1]=47 as u8; p[2]=(48+st[1]) as u8; p[3]=0 as u8 63 g_puts(" ("); g_puts(p); g_puts(")\n" as *u8) 64 if st[0]==st[1] { g_puts("=== GREEN (Napster registry: publish -> announce -> discover -> who-has-it) ===\n" as *u8); sys_exit(0); return 0 } 65 g_puts("=== RED ===\n" as *u8); sys_exit(1); return 1 66}