Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tests/fabric/fab_living_phase.h # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tests / fabric / fab_living_phase.h` 543 lines ยท 26485 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * fab_living_phase.h -- Fabric F5-0 in a living composition (Lane 13). 19 * 20 * One scenario, shared by the R13 living host test (the composition attached 21 * to the living World) and the stand-alone test-fabric-living (a composition 22 * that owns its World, plain and under ASan/UBSan, run twice for 23 * determinism). The caller supplies attach/close; everything else is here. 24 * 25 * machine A this machine (the living system's identity); its Capability 26 * Graph is the composition router's graph; one local Skill 27 * (FL_SKILL_LOCAL, cost 10) provides FX_OP_SCALE. 28 * machine B a second simulated machine on the loopback transport: joins, 29 * advertises FL_SKILL_REMOTE (cost 5, so ranked first) for 30 * FX_OP_SCALE and FL_SKILL_ONLY for FL_OP_REMOTE_ONLY, which 31 * nobody else provides. Its procedures run through the Fabric 32 * dispatcher (fab_dispatch.h, in-process stand-in: F5-0 has no 33 * work message). 34 * machine X enrolled nowhere (forger, wrong machine). 35 * 36 * Steps (clock in microseconds, simulated, so every run is identical): 37 * 1 B joins and advertises; A holds both entries as CQ_SRC_FABRIC, not held. 38 * 2 goal 1: route = [B remote, A local]; the Fabric candidate runs on B, 39 * passes the contract, wins and is committed; its claim says HOME_FABRIC 40 * with B's advertised digest. 41 * 3 forged advertisement (B's identity, wrong key; and a genuine one with 42 * one byte altered): FAB_E_AUTH, graph unchanged. 43 * 4 wrong AienMachineId: B relays X's record (FAB_E_MISMATCH), a message 44 * for X delivered to A (FAB_E_MISMATCH), a requirement pinned to X 45 * (SR_E_MACHINE, World unchanged), a route retargeted to X refused by 46 * the dispatcher. 47 * 5 B's procedure differs from what it advertised: dispatcher refuses 48 * (FAB_DX_DIGEST), the local candidate wins. B returns a wrong result 49 * under the right digest: the AEGIS contract rejects it, local wins. 50 * 6 mid-run loss: B's LEAVE waits in A's mailbox when the goal is routed; 51 * the dispatcher applies it first, the route is withdrawn 52 * (FAB_DX_ROUTE), the local candidate wins. 53 * 7 B rejoins (generation 2); a replay of its generation-1 traffic is 54 * FAB_E_STALE_GEN; goal runs on B again. 55 * 8 stale lease: the clock passes B's lease before A has applied it; the 56 * router already excludes B (lease check), the goal falls back to A; 57 * B's late ADVERTISE is FAB_E_LEASE_EXPIRED; after the tick B is LOST 58 * and its entries are withdrawn; the remote-only operation is refused 59 * (SR_E_NO_CANDIDATE), World unchanged. 60 */ 61 #ifndef FAB_LIVING_PHASE_H 62 #define FAB_LIVING_PHASE_H 63 64 /* Lane 32: test-build only. The production program (docs/r16-production-entry-point.md) 65 * never compiles this; the build refuses it without -DAIEN_TEST_BUILD=1. */ 66 #ifndef AIEN_TEST_BUILD 67 #error "fab_living_phase.h (Fabric living test phase with fixed test keys) is test and simulation only: build with -DAIEN_TEST_BUILD=1, never in the production program" 68 #endif 69 70 #include "fabric/fab_dispatch.h" 71 #include "fabric/fab_hmac.h" 72 #include "fabric/fab_loopback.h" 73 #include "../runtime/rx_compose_fixture.h" 74 75 #include 76 #include 77 78 /* Link-map marker (test build only): tools/r16_prod_hygiene.sh refuses any 79 * production binary that carries an aien_test_build_* symbol. */ 80 __attribute__((used)) static const char aien_test_build_fab_living_phase[] = 81 "AIEN_TEST_BUILD piece: Fabric living test phase (fixed test keys fl-key-*)"; 82 83 enum { FL_OP_REMOTE_ONLY = 2 }; 84 enum { FL_SKILL_LOCAL = 7, FL_SKILL_REMOTE = 9, FL_SKILL_ONLY = 10 }; 85 #define FL_MS 1000ull 86 #define FL_T0 (1000 * FL_MS) /* start of the scenario clock */ 87 #define FL_LEASE (500 * FL_MS) 88 89 typedef int (*FlAttachFn)(void *ctx, RxCompose *c, const char *dir, const AienMachineId *self, 90 const SrRouter *router); 91 typedef void (*FlCloseFn)(void *ctx, RxCompose *c); 92 93 typedef struct { 94 int ran, ok; 95 unsigned checks, failures; 96 uint64_t result[6]; /* committed results of the six goals */ 97 uint32_t winner[6]; /* winning candidate (0 = first route) */ 98 uint32_t remote_wins; /* goals won by the Fabric candidate */ 99 uint64_t dispatched, dispatch_refused[FAB_DX_N]; 100 int forged_wrong_key, forged_altered, relayed_record, misdelivered, stale_gen, 101 lease_expired; /* FabVerdict codes */ 102 int pinned_wrong_machine, remote_only_after_loss; /* rx_compose_run returns */ 103 int fabric_held; /* Fabric candidates ever seen held (must stay 0) */ 104 int fabric_auth_lost; /* Fabric candidates without B's carried authority (must stay 0) */ 105 uint8_t record_digest[32], fabric_digest[32]; 106 } FlReceipt; 107 108 typedef struct { 109 AienMachineId a, b, x; 110 uint8_t key_a[32], key_b[32], key_x[32]; 111 AienMachineId roster_ids[2]; 112 uint8_t roster_keys[2][32]; 113 FabRoster roster; 114 FabLoop loop; 115 AienMachineId slots[3][8]; 116 AienMachineIndex ix[3]; 117 CqCatalog cat_a, cat_b, cat_x; 118 FabHmacAuth auth_a, auth_b; 119 FabNode node_a, node_b; 120 uint8_t gen1_msg[FAB_MSG_MAX]; 121 size_t gen1_len; 122 AgSkillTable skills_a, skills_b; 123 SrRouter router; 124 FabDispatch disp; 125 CqKey own_remote, own_only, own_x; 126 RxCompose c; 127 FlReceipt *g; 128 } FlRig; 129 130 static int fl_b_bad; /* B's procedure breaks the contract */ 131 132 static uint64_t fl_sk_remote(const uint64_t *in, uint32_t n, uint32_t attempt, int *failed) { 133 (void)attempt; 134 *failed = 0; 135 return (n ? in[0] : 0) * 3 + 1 + (fl_b_bad ? 2 : 0); 136 } 137 138 #define FL_CHECK(cond, ...) do { r->g->checks++; if (!(cond)) { r->g->failures++; \ 139 fprintf(stderr, "FABRIC-LIVING FAIL %s:%d: ", __FILE__, __LINE__); \ 140 fprintf(stderr, __VA_ARGS__); fputc('\n', stderr); } } while (0) 141 142 static int fl_catalog(CqCatalog *c, AienMachineIndex *ix, AienMachineId *slots, 143 const AienMachineId *self) { 144 aien_mid_index_init(ix, slots, 8); 145 if (cq_catalog_init_canonical(c, ix, self, 8, 16) != CQ_OK) return -1; 146 uint32_t all = CQ_SRC(CQ_SRC_GRAPH) | CQ_SRC(CQ_SRC_SKILL) | CQ_SRC(CQ_SRC_FABRIC); 147 /* The same operation catalog on every machine (the JOIN compares it). */ 148 if (cq_op_define(c, FX_OP_SCALE, 0, all) != CQ_OK) return -2; 149 if (cq_op_define(c, FL_OP_REMOTE_ONLY, 0, all) != CQ_OK) return -3; 150 return 0; 151 } 152 153 /* Register one Skill of `self` in its own catalog; returns its key. */ 154 static int fl_own_skill(CqCatalog *cat, const AgSkill *s, uint32_t op, uint32_t cap, uint32_t rights, 155 uint64_t cost, uint64_t generation, CqKey *key) { 156 SrSkill k; 157 memset(&k, 0, sizeof k); 158 k.skill_id = s->id; 159 k.version = 1; 160 memcpy(k.digest, s->identity, 32); 161 k.machine = cat->self_machine; 162 CqEntry e = fx_provide(cap, op, 80 + cap, cost); 163 e.generation = generation; 164 if (rights) { /* needs authority on its own machine */ 165 e.auth_resource = 0x5000u + cap; 166 e.auth_rights = rights; 167 } 168 if (sr_register_skill(cat, &k, &e, 1) != SR_OK) return -1; 169 if (cq_catalog_build(cat) != CQ_OK) return -2; 170 e.skill_id = s->id; 171 e.skill_version = 1; 172 memcpy(e.skill_digest, s->identity, 32); 173 e.machine_id = cat->self_machine; 174 e.source = CQ_SRC_SKILL; 175 *key = cq_key_of(&e); 176 return cq_lookup(cat, key) ? 0 : -3; 177 } 178 179 static SrRequirement fl_req(uint32_t op) { 180 SrRequirement q = fx_requirement(); 181 q.need.semantic_operation = op; 182 q.local_only = 0; 183 return q; 184 } 185 186 /* Deliver what waits for B (A's traffic) without the lock: no run is active. */ 187 static void fl_pump_b(FlRig *r, uint64_t now) { 188 FabVerdict v; 189 while (fab_poll(&r->node_b, now, &v) == 1) {} 190 } 191 192 /* Inject one message into A's mailbox and apply it; returns its verdict. */ 193 static int fl_deliver_a(FlRig *r, const uint8_t *msg, size_t len, uint64_t now) { 194 FabVerdict v; 195 memset(&v, 0, sizeof v); 196 if (fab_loop_inject(&r->loop, &r->a, msg, len) != FAB_OK) return 999; 197 if (fab_poll(&r->node_a, now, &v) != 1) return 998; 198 return v.code; 199 } 200 201 /* First waiting message for `to` from `from` of `kind`, copied out. */ 202 static size_t fl_peek(FlRig *r, const AienMachineId *to, const AienMachineId *from, uint32_t kind, 203 uint8_t *out) { 204 uint8_t rec[AIEN_MID_RECORD_BYTES]; 205 aien_mid_encode(from, rec); 206 for (uint32_t i = 0; i < r->loop.n; i++) { 207 const FabLoopBox *b = &r->loop.box[i]; 208 if (!aien_mid_equal(&b->id, to)) continue; 209 for (uint32_t k = 0; k < b->count; k++) { 210 const FabLoopMsg *m = &b->q[(b->head + k) % FAB_LOOP_DEPTH]; 211 if (m->b[5] == kind && memcmp(m->b + 8, rec, sizeof rec) == 0) { 212 memcpy(out, m->b, m->len); 213 return m->len; 214 } 215 } 216 } 217 return 0; 218 } 219 220 static uint32_t fl_fabric_entries(FlRig *r, uint64_t now, uint32_t op) { 221 SrRequirement q = fl_req(op); 222 SrRoute rt[RXC_K]; 223 int n = sr_route_alternatives(&r->router, &q, NULL, now, rt, RXC_K); 224 uint32_t f = 0; 225 for (int i = 0; i < n; i++) { 226 if (rt[i].chosen.source == CQ_SRC_FABRIC) f++; 227 if (rt[i].chosen.source != CQ_SRC_FABRIC) continue; 228 if (rt[i].chosen.required_authority.held) r->g->fabric_held++; 229 /* The entry carries the authority B needs on its own machine (the 230 * routing never mints it here): resource 0x5000+cap, READ. */ 231 uint64_t want = 0x5000u + (op == FX_OP_SCALE ? 2u : 3u); 232 if (rt[i].chosen.required_authority.resource != want || 233 rt[i].chosen.required_authority.rights != RX_RIGHT_READ) 234 r->g->fabric_auth_lost++; 235 } 236 return f; 237 } 238 239 static void fl_word_digest(const uint64_t *w, uint8_t d[32]) { 240 for (int i = 0; i < 4; i++) 241 for (int b = 0; b < 8; b++) d[8 * i + b] = (uint8_t)(w[i] >> (8 * b)); 242 } 243 244 /* One goal; checks the outcome and records the winner. Returns the result 245 * struct's outcome (0 on a run error). */ 246 static int fl_goal(FlRig *r, int i, uint64_t input, uint64_t now, int want_remote_first, 247 uint32_t want_winner) { 248 RxcResult o; 249 SrRequirement q = fl_req(FX_OP_SCALE); 250 int rc = rx_compose_run(&r->c, input, &q, NULL, now, &o); 251 FL_CHECK(rc == RX_OK, "goal %d run %d", i, rc); 252 if (rc != RX_OK) return 0; 253 FL_CHECK(o.outcome == RXC_OUT_COMMITTED, "goal %d outcome %d", i, o.outcome); 254 FL_CHECK(o.result == input * 3 + 1, "goal %d result %llu", i, (unsigned long long)o.result); 255 FL_CHECK(o.winner == want_winner, "goal %d winner %u want %u", i, o.winner, want_winner); 256 if (want_remote_first) { 257 FL_CHECK(o.n_alternatives == 2 && o.route[0].verdict == SR_E_REMOTE && 258 o.route[0].chosen.source == CQ_SRC_FABRIC && 259 o.route[0].chosen.skill_id == FL_SKILL_REMOTE && o.route[0].target_known && 260 aien_mid_equal(&o.route[0].target, &r->b) && o.route[1].verdict == SR_OK && 261 o.route[1].chosen.skill_id == FL_SKILL_LOCAL, 262 "goal %d route: n=%u v0=%d src0=%u", i, o.n_alternatives, o.route[0].verdict, 263 o.route[0].chosen.source); 264 FL_CHECK(!o.route[0].chosen.required_authority.held, "goal %d Fabric candidate held", i); 265 } else { 266 FL_CHECK(o.n_alternatives == 1 && o.route[0].verdict == SR_OK && 267 o.route[0].chosen.skill_id == FL_SKILL_LOCAL, 268 "goal %d fallback route: n=%u v0=%d", i, o.n_alternatives, o.route[0].verdict); 269 } 270 /* The claim of the winner says where its Skill ran. */ 271 uint32_t w = o.winner < RXC_K ? o.winner : 0; 272 const CxObject *cl = o.cx_candidate[w] ? cx_get(&r->c.cx, o.cx_candidate[w]) : NULL; 273 FL_CHECK(cl && cl->n >= RXC_CP_WORDS, "goal %d winner claim", i); 274 if (cl && cl->n >= RXC_CP_WORDS) { 275 const uint64_t *p = cx_payload(&r->c.cx, cl); 276 int remote = want_remote_first && want_winner == 0; 277 FL_CHECK(p[RXC_CP_HOME] == (remote ? RXC_HOME_FABRIC : RXC_HOME_LOCAL), 278 "goal %d claim home %llu", i, (unsigned long long)p[RXC_CP_HOME]); 279 uint8_t d[32]; 280 fl_word_digest(p + RXC_CP_SKILLDIG0, d); 281 const uint8_t *want = remote ? r->skills_b.skill[0].identity : r->skills_a.skill[0].identity; 282 FL_CHECK(memcmp(d, want, 32) == 0, "goal %d claim digest", i); 283 if (remote) r->g->remote_wins++; 284 } 285 if (i < 6) { 286 r->g->result[i] = o.result; 287 r->g->winner[i] = o.winner; 288 } 289 return o.outcome; 290 } 291 292 static int fl_setup(FlRig *r, const AienMachineId *self) { 293 r->a = *self; 294 const char *rb = "fabric-living-B", *rx = "fabric-living-X"; 295 if (aien_mid_derive(AIEN_MID_ROOT_PROVISIONED, (const uint8_t *)rb, strlen(rb), &r->b) != AIEN_MID_OK || 296 aien_mid_derive(AIEN_MID_ROOT_PROVISIONED, (const uint8_t *)rx, strlen(rx), &r->x) != AIEN_MID_OK) 297 return -1; 298 sha256_hash((const uint8_t *)"fl-key-A", 8, r->key_a); 299 sha256_hash((const uint8_t *)"fl-key-B", 8, r->key_b); 300 sha256_hash((const uint8_t *)"fl-key-X", 8, r->key_x); 301 r->roster_ids[0] = r->a; 302 r->roster_ids[1] = r->b; 303 memcpy(r->roster_keys[0], r->key_a, 32); 304 memcpy(r->roster_keys[1], r->key_b, 32); 305 r->roster = (FabRoster){ 2, r->roster_ids }; 306 fab_loop_init(&r->loop); 307 if (fab_loop_add(&r->loop, &r->a) || fab_loop_add(&r->loop, &r->b) || 308 fab_loop_add(&r->loop, &r->x)) 309 return -2; 310 if (fl_catalog(&r->cat_a, &r->ix[0], r->slots[0], &r->a) || 311 fl_catalog(&r->cat_b, &r->ix[1], r->slots[1], &r->b) || 312 fl_catalog(&r->cat_x, &r->ix[2], r->slots[2], &r->x)) 313 return -3; 314 /* A: one local Skill for the scaled operation. */ 315 r->skills_a.n = 1; 316 r->skills_a.skill[0] = (AgSkill){ FL_SKILL_LOCAL, fx_sk_a, { 0xA7, 0x01 } }; 317 CqKey ka; 318 if (fl_own_skill(&r->cat_a, &r->skills_a.skill[0], FX_OP_SCALE, 1, 0, 10, 1, &ka)) return -4; 319 /* B: the cheaper provider of the same operation and the only provider of 320 * FL_OP_REMOTE_ONLY. */ 321 r->skills_b.n = 2; 322 r->skills_b.skill[0] = (AgSkill){ FL_SKILL_REMOTE, fl_sk_remote, { 0xF9, 0xB0, 0x01 } }; 323 r->skills_b.skill[1] = (AgSkill){ FL_SKILL_ONLY, fl_sk_remote, { 0xFA, 0xB0, 0x02 } }; 324 if (fab_hmac_auth_init(&r->auth_a, &r->a, r->key_a, 2, r->roster_ids, 325 (const uint8_t (*)[32])r->roster_keys) != FAB_OK || 326 fab_hmac_auth_init(&r->auth_b, &r->b, r->key_b, 2, r->roster_ids, 327 (const uint8_t (*)[32])r->roster_keys) != FAB_OK) 328 return -5; 329 FabConfig ca = { r->a, &r->roster, &r->cat_a, &r->loop.transport, &r->auth_a.auth, FL_LEASE, 1, NULL, 0 }; 330 FabConfig cb = { r->b, &r->roster, &r->cat_b, &r->loop.transport, &r->auth_b.auth, FL_LEASE, 1, NULL, 0 }; 331 if (fab_node_init(&r->node_a, &ca) != FAB_OK || fab_node_init(&r->node_b, &cb) != FAB_OK) 332 return -6; 333 if (fl_own_skill(&r->cat_b, &r->skills_b.skill[0], FX_OP_SCALE, 2, RX_RIGHT_READ, 5, 334 fab_entry_generation(&r->node_b, 1), &r->own_remote) || 335 fl_own_skill(&r->cat_b, &r->skills_b.skill[1], FL_OP_REMOTE_ONLY, 3, RX_RIGHT_READ, 5, 336 fab_entry_generation(&r->node_b, 1), &r->own_only)) 337 return -7; 338 r->router = (SrRouter){ &r->cat_a, &r->skills_a }; 339 if (fab_dispatch_init(&r->disp, &r->node_a) != FAB_OK || 340 fab_dispatch_add(&r->disp, &r->b, &r->skills_b) != FAB_OK) 341 return -8; 342 return 0; 343 } 344 345 static void fl_free(FlRig *r) { 346 fab_dispatch_destroy(&r->disp); 347 cq_catalog_free(&r->cat_a); 348 cq_catalog_free(&r->cat_b); 349 cq_catalog_free(&r->cat_x); 350 } 351 352 /* B joins at `now` and advertises both entries; A applies it. */ 353 static void fl_b_online(FlRig *r, uint64_t now, int a_joins) { 354 if (a_joins) FL_CHECK(fab_join(&r->node_a, FL_LEASE, now) == FAB_OK, "A join"); 355 FL_CHECK(fab_join(&r->node_b, FL_LEASE, now) == FAB_OK, "B join"); 356 fl_pump_b(r, now); 357 FL_CHECK(fab_dispatch_pump(&r->disp, now) >= 1, "A applies B's join"); 358 FL_CHECK(fab_advertise(&r->node_b, &r->own_remote, CQ_WIRE_ADVERTISE, now) == FAB_OK, "B adv"); 359 FL_CHECK(fab_advertise(&r->node_b, &r->own_only, CQ_WIRE_ADVERTISE, now) == FAB_OK, "B adv 2"); 360 FL_CHECK(fab_dispatch_pump(&r->disp, now) == 2 && r->disp.refused_msgs == 0, 361 "A applies B's advertisements (refused %llu)", (unsigned long long)r->disp.refused_msgs); 362 FL_CHECK(fab_member_live(&r->node_a, &r->b, now), "B live at A"); 363 } 364 365 /* The scenario. attach must leave r->c attached/open on `dir` with r->router. */ 366 static int fl_run(const AienMachineId *self, const char *dir, FlAttachFn attach, FlCloseFn close, 367 void *ctx, FlReceipt *g) { 368 static FlRig rig; 369 FlRig *r = &rig; 370 memset(r, 0, sizeof *r); 371 memset(g, 0, sizeof *g); 372 r->g = g; 373 g->ran = 1; 374 fl_b_bad = 0; 375 int setup = fl_setup(r, self); 376 FL_CHECK(setup == 0, "setup %d", setup); 377 if (setup) { fl_free(r); return -1; } 378 uint64_t t = FL_T0; 379 380 /* 1: B joins and advertises; A sees Fabric candidates, never held. */ 381 fl_b_online(r, t, 1); 382 FL_CHECK(fl_fabric_entries(r, t, FX_OP_SCALE) == 1, "Fabric candidate for the scaled op"); 383 FL_CHECK(fl_fabric_entries(r, t, FL_OP_REMOTE_ONLY) == 1, "Fabric candidate for the remote-only op"); 384 385 int rc = attach(ctx, &r->c, dir, self, &r->router); 386 FL_CHECK(rc == RX_OK, "attach %d", rc); 387 if (rc != RX_OK) { fl_free(r); return -1; } 388 FL_CHECK(rx_compose_set_remote(&r->c, fab_dispatch_run, &r->disp) == RX_OK, "set remote"); 389 390 /* 2: the Fabric candidate runs on B and wins. */ 391 t += 10 * FL_MS; 392 fl_goal(r, 0, 5, t, 1, 0); 393 FL_CHECK(r->disp.dispatched == 1 && r->c.remote.ran == 1, "one dispatch to B"); 394 395 /* 3: forged advertisements. */ 396 { 397 uint8_t msg[FAB_MSG_MAX]; 398 size_t len; 399 /* B's identity and a cheaper offer, tagged with a key that is not 400 * B's (an impostor sealing as B). */ 401 uint8_t rec[CQ_WIRE_BYTES]; 402 CqEntry e = *cq_lookup(&r->cat_b, &r->own_remote); 403 e.cost = 1; /* would outrank everything */ 404 FL_CHECK(cq_wire_encode(&r->cat_b, &e, CQ_WIRE_ADVERTISE, rec) == CQ_OK, "encode"); 405 FL_CHECK(fab_seal(&r->node_b, &r->a, FAB_MSG_ADVERTISE, rec, sizeof rec, t, msg, &len) == FAB_OK, "seal"); 406 fab_hmac_sig64(r->key_x, msg, len - FAB_SIG_BYTES, msg + len - FAB_SIG_BYTES); 407 g->forged_wrong_key = fl_deliver_a(r, msg, len, t); 408 FL_CHECK(g->forged_wrong_key == FAB_E_AUTH, "forged (wrong key) %d", g->forged_wrong_key); 409 /* A genuine advertisement with one byte altered in flight. The 410 * unaltered original is kept back for the replay in step 7. */ 411 FL_CHECK(fab_advertise(&r->node_b, &r->own_remote, CQ_WIRE_ADVERTISE, t) == FAB_OK, "B re-adv"); 412 len = fl_peek(r, &r->a, &r->b, FAB_MSG_ADVERTISE, msg); 413 FL_CHECK(len == FAB_MSG_MAX, "captured B advertisement"); 414 memcpy(r->gen1_msg, msg, len); 415 r->gen1_len = len; 416 msg[FAB_HDR_BYTES + 144] ^= 0x7F; /* one byte of the record */ 417 r->loop.box[0].count = 0; /* drop the original from A's mailbox */ 418 g->forged_altered = fl_deliver_a(r, msg, len, t); 419 FL_CHECK(g->forged_altered == FAB_E_AUTH, "forged (altered) %d", g->forged_altered); 420 const CqEntry *got = NULL; 421 SrRequirement q = fl_req(FX_OP_SCALE); 422 SrRoute rt; 423 FL_CHECK(sr_route(&r->router, &q, NULL, t, &rt) == SR_E_REMOTE && 424 (got = cq_lookup(&r->cat_a, &rt.key)) && got->cost == 5, 425 "forgeries changed the graph"); 426 } 427 428 /* 4: wrong AienMachineId. */ 429 { 430 uint8_t msg[FAB_MSG_MAX], rec[CQ_WIRE_BYTES]; 431 size_t len; 432 AgSkill xs = { 11, fl_sk_remote, { 0xEE } }; 433 FL_CHECK(fl_own_skill(&r->cat_x, &xs, FX_OP_SCALE, 4, RX_RIGHT_READ, 1, 1, &r->own_x) == 0, "X skill"); 434 FL_CHECK(cq_wire_encode(&r->cat_x, cq_lookup(&r->cat_x, &r->own_x), CQ_WIRE_ADVERTISE, rec) == CQ_OK, "X rec"); 435 /* B relays X's record: nobody speaks for another machine. */ 436 FL_CHECK(fab_seal(&r->node_b, &r->a, FAB_MSG_ADVERTISE, rec, sizeof rec, t, msg, &len) == FAB_OK, "seal relay"); 437 g->relayed_record = fl_deliver_a(r, msg, len, t); 438 FL_CHECK(g->relayed_record == FAB_E_MISMATCH, "relayed record %d", g->relayed_record); 439 /* B's message for X, delivered to A. */ 440 uint8_t body[8] = { 0 }; 441 body[2] = 0x07; 442 FL_CHECK(fab_seal(&r->node_b, &r->x, FAB_MSG_RENEW, body, sizeof body, t, msg, &len) == FAB_OK, "seal renew"); 443 g->misdelivered = fl_deliver_a(r, msg, len, t); 444 FL_CHECK(g->misdelivered == FAB_E_MISMATCH, "misdelivered %d", g->misdelivered); 445 /* A requirement pinned to X: refused before anything is published. */ 446 JsBranchRef before = rx_compose_state(&r->c); 447 SrRequirement q = fl_req(FX_OP_SCALE); 448 q.pin_machine_set = 1; 449 q.pin_machine = r->x; 450 RxcResult o; 451 g->pinned_wrong_machine = rx_compose_run(&r->c, 6, &q, NULL, t, &o); 452 JsBranchRef after = rx_compose_state(&r->c); 453 FL_CHECK(g->pinned_wrong_machine == SR_E_MACHINE && before.id == after.id && 454 before.gen == after.gen, "pinned to X: %d", g->pinned_wrong_machine); 455 /* A route retargeted to X is refused by the dispatcher. */ 456 SrRoute rt; 457 q.pin_machine_set = 0; 458 FL_CHECK(sr_route(&r->router, &q, NULL, t, &rt) == SR_E_REMOTE, "route to B"); 459 rt.target = r->x; 460 uint64_t res = 0; 461 uint64_t d0 = r->disp.dispatched; 462 FL_CHECK(fab_dispatch_run(&r->disp, &r->router, &rt, 6, t, &res) == FAB_DX_ROUTE && 463 r->disp.dispatched == d0, "retargeted route dispatched"); 464 } 465 466 /* 5: B runs something other than what it advertised; then a wrong 467 * result under the right digest. The local candidate wins both. */ 468 t += 10 * FL_MS; 469 r->skills_b.skill[0].identity[31] ^= 0x01; 470 fl_goal(r, 1, 7, t, 1, 1); 471 FL_CHECK(r->disp.last_refusal == FAB_DX_DIGEST, "digest refusal %d", r->disp.last_refusal); 472 r->skills_b.skill[0].identity[31] ^= 0x01; 473 fl_b_bad = 1; 474 uint64_t before_bad = r->disp.dispatched; 475 fl_goal(r, 2, 8, t, 1, 1); 476 FL_CHECK(r->disp.dispatched == before_bad + 1, "wrong result was dispatched, then rejected"); 477 fl_b_bad = 0; 478 479 /* 6: B leaves while the goal is being routed. */ 480 t += 10 * FL_MS; 481 FL_CHECK(fab_leave(&r->node_b, t) == FAB_OK, "B leave"); 482 uint64_t refused0 = r->disp.refused[FAB_DX_ROUTE]; 483 fl_goal(r, 3, 9, t, 1, 1); 484 FL_CHECK(r->disp.refused[FAB_DX_ROUTE] == refused0 + 1, "mid-run loss refused"); 485 FL_CHECK(!fab_member_live(&r->node_a, &r->b, t) && fl_fabric_entries(r, t, FX_OP_SCALE) == 0, 486 "B's entries withdrawn after LEAVE"); 487 488 /* 7: B rejoins with a new generation; its old traffic is stale. */ 489 t += 10 * FL_MS; 490 { 491 FL_CHECK(fab_node_set_generation(&r->node_b, 2) == FAB_OK, "B gen 2"); 492 /* Re-number B's entries under the new generation. */ 493 CqEntry e1 = *cq_lookup(&r->cat_b, &r->own_remote), e2 = *cq_lookup(&r->cat_b, &r->own_only); 494 e1.generation = e2.generation = fab_entry_generation(&r->node_b, 1); 495 FL_CHECK(cq_register(&r->cat_b, &e1, NULL, 0) == CQ_OK && 496 cq_register(&r->cat_b, &e2, NULL, 0) == CQ_OK && cq_catalog_build(&r->cat_b) == CQ_OK, 497 "B re-registers"); 498 fl_b_online(r, t, 0); 499 g->stale_gen = fl_deliver_a(r, r->gen1_msg, r->gen1_len, t); 500 FL_CHECK(g->stale_gen == FAB_E_STALE_GEN, "gen-1 replay %d", g->stale_gen); 501 } 502 fl_goal(r, 4, 10, t, 1, 0); 503 504 /* 8: B's lease ends. The router excludes it before A has applied the 505 * clock; B's late advertisement is refused; the remote-only op is 506 * refused once B is gone. */ 507 t += FL_LEASE + 1; 508 fl_goal(r, 5, 11, t, 0, 0); 509 { 510 FL_CHECK(fab_advertise(&r->node_b, &r->own_remote, CQ_WIRE_ADVERTISE, t) == FAB_OK, "late adv"); 511 uint8_t msg[FAB_MSG_MAX]; 512 size_t len = fl_peek(r, &r->a, &r->b, FAB_MSG_ADVERTISE, msg); 513 r->loop.box[0].count = 0; 514 g->lease_expired = fl_deliver_a(r, msg, len, t); 515 FL_CHECK(g->lease_expired == FAB_E_LEASE_EXPIRED, "late advertisement %d", g->lease_expired); 516 const FabMember *m = fab_member(&r->node_a, &r->b); 517 FL_CHECK(m && m->state == FAB_ST_LOST, "B lost"); 518 FL_CHECK(fl_fabric_entries(r, t, FX_OP_SCALE) == 0 && 519 fl_fabric_entries(r, t, FL_OP_REMOTE_ONLY) == 0, "B's entries withdrawn"); 520 JsBranchRef before = rx_compose_state(&r->c); 521 SrRequirement q = fl_req(FL_OP_REMOTE_ONLY); 522 RxcResult o; 523 g->remote_only_after_loss = rx_compose_run(&r->c, 12, &q, NULL, t, &o); 524 JsBranchRef after = rx_compose_state(&r->c); 525 FL_CHECK(g->remote_only_after_loss == SR_E_NO_CANDIDATE && before.id == after.id && 526 before.gen == after.gen, "remote-only op after loss: %d", g->remote_only_after_loss); 527 } 528 529 FL_CHECK(g->fabric_held == 0, "a Fabric candidate came back held"); 530 FL_CHECK(g->fabric_auth_lost == 0, "a Fabric candidate lost its carried authority"); 531 FL_CHECK(g->remote_wins == 2, "remote wins %u", g->remote_wins); 532 g->dispatched = r->disp.dispatched; 533 memcpy(g->dispatch_refused, r->disp.refused, sizeof g->dispatch_refused); 534 rx_compose_record_digest(&r->c.cx, g->record_digest); 535 fab_state_digest(&r->node_a, g->fabric_digest); 536 FL_CHECK(rx_compose_set_remote(&r->c, NULL, NULL) == RX_OK, "remove remote"); 537 close(ctx, &r->c); 538 fl_free(r); 539 g->ok = g->failures == 0; 540 return g->ok ? 0 : -1; 541 } 542 543 #endif /* FAB_LIVING_PHASE_H */