code wiki / (root) / nx_vesicle.nx

nx_vesicle.nx source

↩ module page · 113 lines · 3981 B

1// nx_vesicle.nx -- typed payload edge between cells. 2// 3// Biology: vesicles are membrane-bound packets that transport 4// cargo between cells (or between organelles). Nishi vesicle is 5// the inter-cell message primitive: cell A produces a payload, 6// wraps it in a vesicle (with methyl-seal of the originator), 7// sends to cell B; B unwraps + verifies + processes. 8// 9// Per [[feedback-pathway-tropism-block-composition-location-agnostic]]: 10// pathway edges are vesicles. Per Captain Moroni: every inter-cell 11// message carries methyl seal so the receiver knows it's from a 12// substrate-self peer, not a forgery. 13// 14// Composes: 15// nx_cell -- vesicles transit between cells 16// nx_methyl -- vesicle's authenticity proof 17// nx_brane -- receiving brane checks CAP_PEER_MESSAGE 18// nx_pathway -- pathway edges are typed vesicle channels 19// nx_provenance_chain -- vesicle send/recv are transforms 20 21import "nx_syscalls.nx" 22import "nx_tier.nx" 23import "nx_methyl.nx" 24 25const NX_VES_OK: nx_int = 0 26const NX_VES_ERR_BAD_MARK: nx_int = 1 27const NX_VES_ERR_BAD_KIND: nx_int = 2 28 29// ===== Sealed enum: NxVesiclePayloadKind ========================== 30// 31// Caller-extensible (>= NX_VK_N_KINDS allowed for custom payloads). 32 33const NX_VK_BYTES: nx_int = 0 // opaque byte buffer 34const NX_VK_TENSOR: nx_int = 1 // *NxTensor handle 35const NX_VK_PROVENANCE: nx_int = 2 // *NxProvenanceLink for chaining 36const NX_VK_ANTIBODY: nx_int = 3 // detection signature 37const NX_VK_DECOY_TOKEN: nx_int = 4 // tripwire token to share 38const NX_VK_HUNT_REPORT: nx_int = 5 // kill announcement to peers 39const NX_VK_GATHER_REPORT: nx_int = 6 // harvest announcement 40const NX_VK_N_KINDS: nx_int = 7 41 42struct NxVesicle { 43 from_cell_id: nx_int, 44 to_cell_id: nx_int, 45 payload_kind: nx_int, 46 payload_ptr: *u8, 47 payload_len: nx_size, 48 methyl: *NxMethylMark, 49 sent_us: nx_size, 50 delivered: nx_int, // 1 if receiver acked 51} 52 53func nx_vesicle_kind_is_known(k: nx_int) -> nx_int { 54 if k < 0 { return 0 } 55 if k >= NX_VK_N_KINDS { return 0 } 56 return 1 57} 58 59func nx_vesicle_seal(from_cell_id: nx_int, 60 to_cell_id: nx_int, 61 payload_kind: nx_int, 62 payload_ptr: *u8, 63 payload_len: nx_size, 64 methyl: *NxMethylMark, 65 now_us: nx_size) -> *NxVesicle { 66 if (methyl as i64) == 0 { return (0 as i64) as *NxVesicle } 67 let v: *NxVesicle = (sys_mmap(56)) as *NxVesicle 68 v.from_cell_id = from_cell_id 69 v.to_cell_id = to_cell_id 70 v.payload_kind = payload_kind 71 v.payload_ptr = payload_ptr 72 v.payload_len = payload_len 73 v.methyl = methyl 74 v.sent_us = now_us 75 v.delivered = 0 76 return v 77} 78 79// ===== nx_vesicle_verify ========================================== 80// 81// Receiver-side: check the methyl mark validates as self for the 82// claimed originator. Returns OK or BAD_MARK. Caller should refuse 83// to process a vesicle that fails verify (or treat it as xenocell 84// foreign-action evidence). 85 86func nx_vesicle_verify(v: *NxVesicle, 87 now_us: nx_size, 88 max_age_us: nx_size, 89 allowed_originator: nx_int) -> nx_int { 90 if (v.methyl as i64) == 0 { return NX_VES_ERR_BAD_MARK } 91 if nx_methyl_is_self(v.methyl, now_us, max_age_us, allowed_originator) != 1 { 92 return NX_VES_ERR_BAD_MARK 93 } 94 return NX_VES_OK 95} 96 97// ===== nx_vesicle_ack ============================================ 98// 99// Receiver acks delivery. delivered transitions 0 -> 1. Used by 100// pathway scheduler for at-least-once delivery accounting. 101 102func nx_vesicle_ack(v: *NxVesicle) -> nx_int { 103 v.delivered = 1 104 return NX_VES_OK 105} 106 107func nx_vesicle_is_delivered(v: *NxVesicle) -> nx_int { 108 return v.delivered 109} 110 111func nx_vesicle_payload_kind(v: *NxVesicle) -> nx_int { 112 return v.payload_kind 113}