code wiki / _hdl_build / nx_hub_replicate.nx
nx_hub_replicate.nx source
↩ module page · 33 lines · 2164 B
1// nx_hub_replicate.nx -- LIB: HUB-TO-HUB content replication (operator: "hub to hub"). Gossip spreads the INDEX;
2// this pulls the actual BYTES a hub lacks and verifies them fail-closed before trusting. Flow: gossip_select gives
3// the rows this hub is missing -> for each, read the source (the row's url field; live = nx_https_get_stream, gate =
4// a local path) -> recompute SHA-256 and demand it equals the row's content-id -> only then adopt + re-announce.
5// A hub that replicated content becomes a new SOURCE (mesh: A->B, then B->C). Composes nx_dist_publish (content-id)
6// + nx_dist_index (row parsing). Fail-closed integrity = a corrupt/hostile source can never poison the mesh. No TLS.
7// license_tier: ORIGINAL
8import "nx_dist_publish.nx"
9import "nx_dist_index.nx"
10import "nx_syscalls.nx"
11const K_MAGIC_1024: i64 = 1024
12
13func hr_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 }
14// extract field #idx (0=name 1=cid 2=size 3=magnet 4=url) of a TAB-delimited content row into out (0-terminated).
15func hr_field(row: *u8, rlen: i64, idx: i64, out: *u8) -> i64 {
16 var fs: i64=0; var f: i64=0
17 while f<idx { let t: i64=di_tab(row, fs, rlen); fs=t+1; f=f+1 }
18 let fe: i64=di_tab(row, fs, rlen)
19 var o: i64=0; var k: i64=fs; while k<fe { out[o]=row[k]; o=o+1; k=k+1 } out[o]=0 as u8; return o
20}
21// FAIL-CLOSED integrity: the file at `path` is a valid replica of `expected_cid` iff SHA-256(bytes)==expected_cid.
22func hr_verify_replica(path: *u8, expected_cid: *u8) -> i64 {
23 let got: *u8 = sys_mmap(128); let n: i64 = dp_content_id(path, got)
24 if n<0 { return 0 }
25 return hr_streq(got, expected_cid)
26}
27// replicate ONE row: pull its source (url field) and verify vs its cid. Returns 1 if the replica is authentic.
28// (Live: fetch url via nx_https_get_stream to a temp, then verify. Gate: url is a local source path.)
29func hr_replicate_row(row: *u8, rlen: i64) -> i64 {
30 let cid: *u8 = sys_mmap(128); hr_field(row, rlen, 1, cid)
31 let url: *u8 = sys_mmap(K_MAGIC_1024); hr_field(row, rlen, 4, url)
32 return hr_verify_replica(url, cid)
33}