nx_hypha_test.nx source
↩ module page · 161 lines · 7344 B
1// nx_hypha_test.nx -- smoke for nx_hypha.
2
3import "nx_syscalls.nx"
4import "nx_hypha.nx"
5
6func main() -> i64 {
7 let now: nx_size = 1000
8
9 // 1: mode enum validity
10 if nx_em_mode_is_valid(NX_EM_COMPUTE) != 1 { return 1 }
11 if nx_em_mode_is_valid(NX_EM_INGEST) != 1 { return 2 }
12 if nx_em_mode_is_valid(-1) != 0 { return 3 }
13 if nx_em_mode_is_valid(8) != 0 { return 4 }
14 if NX_EM_N_MODES != 8 { return 5 }
15
16 // 2: direction enum validity
17 if nx_em_dir_is_valid(NX_EM_DIR_SHARE) != 1 { return 6 }
18 if nx_em_dir_is_valid(NX_EM_DIR_RECEIVE) != 1 { return 7 }
19 if nx_em_dir_is_valid(-1) != 0 { return 8 }
20 if nx_em_dir_is_valid(2) != 0 { return 9 }
21
22 // 3: verdict enum validity
23 if nx_hy_verdict_is_valid(NX_HY_OK) != 1 { return 10 }
24 if nx_hy_verdict_is_valid(NX_HY_ERR_NULL_INPUT) != 1 { return 11 }
25 if nx_hy_verdict_is_valid(-1) != 0 { return 12 }
26 if nx_hy_verdict_is_valid(7) != 0 { return 13 }
27
28 // 4: niche capability -- gaming workstation offers all modes
29 if nx_em_niche_can_offer(0, NX_EM_COMPUTE) != 1 { return 14 }
30 if nx_em_niche_can_offer(0, NX_EM_STORAGE) != 1 { return 15 }
31 if nx_em_niche_can_offer(0, NX_EM_KNOWLEDGE) != 1 { return 16 }
32 if nx_em_niche_can_offer(0, NX_EM_PATHWAY) != 1 { return 17 }
33
34 // 5: NAS offers storage but limited compute
35 if nx_em_niche_can_offer(3, NX_EM_STORAGE) != 1 { return 18 }
36 if nx_em_niche_can_offer(3, NX_EM_CHROMATIN) != 1 { return 19 }
37 if nx_em_niche_can_offer(3, NX_EM_COMPUTE) != 0 { return 20 }
38 if nx_em_niche_can_offer(3, NX_EM_PATHWAY) != 0 { return 21 }
39
40 // 6: smartphone offers signal/label/knowledge/ingest only
41 if nx_em_niche_can_offer(5, NX_EM_SIGNAL) != 1 { return 22 }
42 if nx_em_niche_can_offer(5, NX_EM_LABEL) != 1 { return 23 }
43 if nx_em_niche_can_offer(5, NX_EM_KNOWLEDGE) != 1 { return 24 }
44 if nx_em_niche_can_offer(5, NX_EM_INGEST) != 1 { return 25 }
45 if nx_em_niche_can_offer(5, NX_EM_COMPUTE) != 0 { return 26 }
46 if nx_em_niche_can_offer(5, NX_EM_STORAGE) != 0 { return 27 }
47
48 // 7: embedded sensor offers SIGNAL only
49 if nx_em_niche_can_offer(40, NX_EM_SIGNAL) != 1 { return 28 }
50 if nx_em_niche_can_offer(40, NX_EM_LABEL) != 0 { return 29 }
51 if nx_em_niche_can_offer(40, NX_EM_COMPUTE) != 0 { return 30 }
52
53 // 8: refused niches offer nothing
54 if nx_em_niche_can_offer(50, NX_EM_SIGNAL) != 0 { return 31 }
55 if nx_em_niche_can_offer(53, NX_EM_LABEL) != 0 { return 32 }
56
57 // 9: fresh peer state defaults
58 let p: *NxPeerExchangeState = nx_hy_peer_state_new(42, 0, 6) // GAMING + COMMODITY_INTEL
59 if p.peer_id != 42 { return 33 }
60 if p.peer_niche != 0 { return 34 }
61 if p.peer_silicon_trust != 6 { return 35 }
62 if p.starter_quota_remaining != NX_HY_STARTER_QUOTA_DEFAULT { return 36 }
63 if nx_hy_total_shared(p) != 0 { return 37 }
64 if nx_hy_total_received(p) != 0 { return 38 }
65
66 // 10: record exchange + verify counters
67 let rc1: nx_int = nx_hy_record_exchange(p, NX_EM_COMPUTE,
68 NX_EM_DIR_SHARE, now)
69 if rc1 != NX_HY_OK { return 39 }
70 if nx_hy_share_count(p, NX_EM_COMPUTE) != 1 { return 40 }
71
72 let rc2: nx_int = nx_hy_record_exchange(p, NX_EM_STORAGE,
73 NX_EM_DIR_RECEIVE, now + 10)
74 if rc2 != NX_HY_OK { return 41 }
75 if nx_hy_receive_count(p, NX_EM_STORAGE) != 1 { return 42 }
76 // Starter quota decrements on receive
77 if p.starter_quota_remaining != NX_HY_STARTER_QUOTA_DEFAULT - 1 { return 43 }
78 if p.last_exchange_us != now + 10 { return 44 }
79
80 // 11: invalid mode rejected
81 let bad_mode: nx_int = nx_hy_record_exchange(p, 99,
82 NX_EM_DIR_SHARE, now)
83 if bad_mode != NX_HY_ERR_INVALID_MODE { return 45 }
84
85 // 12: invalid direction rejected
86 let bad_dir: nx_int = nx_hy_record_exchange(p, NX_EM_COMPUTE,
87 99, now)
88 if bad_dir != NX_HY_ERR_INVALID_DIRECTION { return 46 }
89
90 // 13: total counts aggregate
91 nx_hy_record_exchange(p, NX_EM_SIGNAL, NX_EM_DIR_SHARE, now)
92 nx_hy_record_exchange(p, NX_EM_LABEL, NX_EM_DIR_RECEIVE, now)
93 if nx_hy_total_shared(p) != 2 { return 47 }
94 if nx_hy_total_received(p) != 2 { return 48 }
95
96 // 14: reciprocity verdict -- new peer (quota remaining) -> OK
97 let v_new: nx_int = nx_hy_reciprocity_verdict(p)
98 if v_new != NX_HY_OK { return 49 }
99
100 // 15: drain starter quota + verify reciprocity now matters
101 var i: nx_int = 0
102 while i < NX_HY_STARTER_QUOTA_DEFAULT {
103 nx_hy_record_exchange(p, NX_EM_SIGNAL, NX_EM_DIR_RECEIVE, now)
104 i = i + 1
105 }
106 if p.starter_quota_remaining != 0 { return 50 }
107 // Now share=2, receive=2+100=102 -> ratio ~2% well below 30% -> VIOLATION
108 let v_violation: nx_int = nx_hy_reciprocity_verdict(p)
109 if v_violation != NX_HY_ERR_RECIPROCITY_VIOLATION { return 51 }
110
111 // 16: bring share count up to restore reciprocity
112 // need: share*10 >= total*3 where total = share+receive
113 // total now = 2 + 102 = 104; need share*10 >= 312 -> share >= 31.2 -> >= 32
114 // we're at share=2, so add 30 more shares -> share=32
115 var j: nx_int = 0
116 while j < 30 {
117 nx_hy_record_exchange(p, NX_EM_COMPUTE, NX_EM_DIR_SHARE, now)
118 j = j + 1
119 }
120 // share=32, receive=102, total=134
121 // 32*10 = 320 >= 134*3 = 402? No, 320 < 402. Need more shares.
122 // 134*3/10 = 40.2 -> need >=41 shares. Currently 32; add 10 more = 42.
123 var k: nx_int = 0
124 while k < 10 {
125 nx_hy_record_exchange(p, NX_EM_COMPUTE, NX_EM_DIR_SHARE, now)
126 k = k + 1
127 }
128 // share=42, total=144; 42*10=420 >= 144*3=432? 420 < 432. Add 2 more.
129 nx_hy_record_exchange(p, NX_EM_COMPUTE, NX_EM_DIR_SHARE, now)
130 nx_hy_record_exchange(p, NX_EM_COMPUTE, NX_EM_DIR_SHARE, now)
131 // share=44, total=146; 44*10=440 >= 146*3=438? YES -> OK
132 let v_recovered: nx_int = nx_hy_reciprocity_verdict(p)
133 if v_recovered != NX_HY_OK { return 52 }
134
135 // 17: can_initiate_share -- sovereignty-critical KNOWLEDGE on
136 // commodity peer rejected (trust > 3)
137 let v_know: nx_int = nx_hy_can_initiate_share(p, NX_EM_KNOWLEDGE)
138 if v_know != NX_HY_ERR_TRUST_INSUFFICIENT { return 53 }
139
140 // 18: KNOWLEDGE OK on SBC_RPI peer (trust = 3)
141 let p_rpi: *NxPeerExchangeState = nx_hy_peer_state_new(99, 0, 3)
142 let v_know_rpi: nx_int = nx_hy_can_initiate_share(p_rpi, NX_EM_KNOWLEDGE)
143 if v_know_rpi != NX_HY_OK { return 54 }
144
145 // 19: SIGNAL OK on any trust level
146 let v_sig: nx_int = nx_hy_can_initiate_share(p, NX_EM_SIGNAL)
147 if v_sig != NX_HY_OK { return 55 }
148
149 // 20: null input handling
150 let null_p: *NxPeerExchangeState = (0 as i64) as *NxPeerExchangeState
151 if nx_hy_record_exchange(null_p, NX_EM_COMPUTE,
152 NX_EM_DIR_SHARE, now) != NX_HY_ERR_NULL_INPUT { return 56 }
153 if nx_hy_reciprocity_verdict(null_p) != NX_HY_ERR_NULL_INPUT { return 57 }
154 if nx_hy_can_initiate_share(null_p, NX_EM_COMPUTE) != NX_HY_ERR_NULL_INPUT { return 58 }
155 if nx_hy_share_count(null_p, NX_EM_COMPUTE) != 0 { return 59 }
156 if nx_hy_receive_count(null_p, NX_EM_COMPUTE) != 0 { return 60 }
157 if nx_hy_total_shared(null_p) != 0 { return 61 }
158 if nx_hy_total_received(null_p) != 0 { return 62 }
159
160 return 0
161}