engenho-substrate 0.53.177

engenho-substrate — the substrate leaf: every module a shipped engenho crate references (derivations and caches, receipts, quorum, ledgers, verifiers, rollout gates, freshness, owned tasks), with engenho-substrate-core re-exported whole so every engenho_substrate:: path keeps resolving.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
//! `GossipLedger` — `MaterializationLedger` impl built on top of a
//! pluggable `GossipTransport`.
//!
//! Sits between the substrate's typed receipts and any
//! gossip-shaped backend (chitchat, scuttlebutt, Cassandra-style
//! Phi-accrual, custom UDP gossip). The trait surface is one
//! method — `broadcast_receipt` — so the substrate doesn't lock
//! the wire protocol.
//!
//! ## Receive path
//!
//! The transport delivers receipts via a tokio broadcast channel
//! the operator subscribes to. The `GossipLedger`'s `receiver_task`
//! drains the channel + ingests each receipt into the inner
//! ledger so reads against the `GossipLedger` see the cluster-wide
//! aggregated state.
//!
//! ## Composition
//!
//! ```ignore
//! let transport = ChitchatTransport::bind(...).await?;     // production
//! let inner = MemoryLedger::new();
//! let gossip = GossipLedger::new(Arc::new(inner), Arc::new(transport));
//! gossip.start_receiver().await;
//! // Now every receipt ingested locally is broadcast cluster-wide,
//! // and every receipt received from peers is folded into the
//! // inner ledger's aggregate view.
//! ```

use std::num::NonZeroUsize;
use std::sync::Arc;

use async_trait::async_trait;
use thiserror::Error;
use tokio::sync::{Mutex, broadcast};

use crate::ledger::{LedgerError, LedgerKey, MaterializationLedger};
use crate::quorum::QuorumVerdict;
use crate::receipt::MaterializationReceipt;
use crate::roca::StageId;

/// One outbound + one inbound channel pair the gossip transport
/// presents to the ledger.
pub struct GossipChannel {
    /// Sender — pushes receipts onto the gossip wire.
    pub outbound: Arc<dyn GossipBroadcaster>,
    /// Receiver — yields receipts arriving from the gossip wire.
    pub inbound: broadcast::Receiver<GossipDelivery>,
}

/// What the transport delivers when a receipt arrives.
#[derive(Clone, Debug)]
pub struct GossipDelivery {
    /// Stage the receipt belongs to.
    pub stage_id: StageId,
    /// Threshold the originating node was using for the tracker.
    pub threshold: NonZeroUsize,
    /// The receipt itself.
    pub receipt: MaterializationReceipt,
}

/// Gossip-transport errors.
#[derive(Debug, Clone, Error)]
pub enum GossipError {
    /// Backend (chitchat / UDP / TCP) returned an error.
    #[error("backend: {0}")]
    Backend(String),
    /// Transport not connected yet.
    #[error("not connected")]
    NotConnected,
}

crate::impl_error_kind! {
    GossipError {
        (Backend(_)) => "backend",
        NotConnected => "not_connected",
    }
}

/// One-method pluggable broadcaster. Implementations push the
/// `(stage_id, threshold, receipt)` triple onto the gossip wire.
#[async_trait]
pub trait GossipBroadcaster: Send + Sync {
    /// Broadcaster identifier for telemetry.
    fn name(&self) -> &'static str;

    /// Publish a receipt cluster-wide.
    ///
    /// # Errors
    /// [`GossipError::Backend`] on transport failure;
    /// [`GossipError::NotConnected`] if the transport isn't connected.
    async fn broadcast_receipt(
        &self,
        stage_id: &StageId,
        threshold: NonZeroUsize,
        receipt: &MaterializationReceipt,
    ) -> Result<(), GossipError>;
}

// =================================================================
// FakeGossipTransport — deterministic in-memory transport for tests
// =================================================================

/// In-memory transport. Outbound calls land on a `broadcast::Sender`
/// the test can subscribe to; inbound is fed by the test injecting
/// `GossipDelivery`s into a separate channel.
pub struct FakeGossipTransport {
    inner: Arc<Mutex<FakeGossipState>>,
    outbound_sender: broadcast::Sender<GossipBroadcast>,
}

/// A broadcast triple emitted by the transport's outbound side.
#[derive(Clone, Debug)]
pub struct GossipBroadcast {
    /// Stage the receipt belongs to.
    pub stage_id: StageId,
    /// Threshold used by the broadcasting node.
    pub threshold: NonZeroUsize,
    /// The receipt.
    pub receipt: MaterializationReceipt,
}

#[derive(Default)]
struct FakeGossipState {
    fail_next: Option<GossipError>,
    broadcasts: Vec<GossipBroadcast>,
}

impl Default for FakeGossipTransport {
    fn default() -> Self {
        Self::new()
    }
}

impl FakeGossipTransport {
    /// Fresh transport with a fan-out broadcast channel of capacity 128.
    #[must_use]
    pub fn new() -> Self {
        let (outbound_sender, _) = broadcast::channel(128);
        Self {
            inner: Arc::new(Mutex::new(FakeGossipState::default())),
            outbound_sender,
        }
    }

    /// Subscribe to the outbound side. Tests can assert what was
    /// broadcast + drive the wire end-to-end.
    #[must_use]
    pub fn subscribe_outbound(&self) -> broadcast::Receiver<GossipBroadcast> {
        self.outbound_sender.subscribe()
    }

    /// Currently-active outbound subscribers (for the `SubscriberSnapshot`
    /// observable). Sync — reads `broadcast::Sender::receiver_count`.
    #[must_use]
    pub fn subscriber_count(&self) -> usize {
        self.outbound_sender.receiver_count()
    }
}

crate::define_named!(FakeGossipTransport, "fake-gossip");

// Third site (v0.87 TSR completion): `FakeGossipTransport` joins
// `BroadcastLedger` + `WatchedCache` as Observable wrappers all
// returning the canonical `SubscriberSnapshot`. v0.95: routed
// through `impl_observable_subscriber!` macro.
crate::impl_observable_subscriber!(FakeGossipTransport, "fake-gossip");

impl FakeGossipTransport {
    // Implementation continuation — kept inside an inherent impl so the
    // Named / Observable impls above can be readily diffed.
    #[allow(dead_code)]
    fn _v087_marker() {}

    /// Inject the next broadcast call to fail.
    pub async fn fail_next(&self, err: GossipError) {
        self.inner.lock().await.fail_next = Some(err);
    }

    /// Snapshot of broadcasts emitted so far.
    pub async fn broadcasts(&self) -> Vec<GossipBroadcast> {
        self.inner.lock().await.broadcasts.clone()
    }
}

#[async_trait]
impl GossipBroadcaster for FakeGossipTransport {
    fn name(&self) -> &'static str {
        "fake"
    }

    async fn broadcast_receipt(
        &self,
        stage_id: &StageId,
        threshold: NonZeroUsize,
        receipt: &MaterializationReceipt,
    ) -> Result<(), GossipError> {
        let mut state = self.inner.lock().await;
        if let Some(err) = state.fail_next.take() {
            return Err(err);
        }
        let bcast = GossipBroadcast {
            stage_id: stage_id.clone(),
            threshold,
            receipt: receipt.clone(),
        };
        state.broadcasts.push(bcast.clone());
        drop(state);
        // Best-effort send — no subscribers is not an error.
        let _ = self.outbound_sender.send(bcast);
        Ok(())
    }
}

// =================================================================
// GossipLedger — pluggable transport + inner aggregator
// =================================================================

/// Ledger that broadcasts every local ingest cluster-wide AND
/// receives peer ingests via gossip.
pub struct GossipLedger {
    inner: Arc<dyn MaterializationLedger>,
    transport: Arc<dyn GossipBroadcaster>,
}

impl GossipLedger {
    /// New ledger composed from an inner aggregator + a broadcaster.
    #[must_use]
    pub fn new(
        inner: Arc<dyn MaterializationLedger>,
        transport: Arc<dyn GossipBroadcaster>,
    ) -> Self {
        Self { inner, transport }
    }

    /// Borrow the inner ledger.
    #[must_use]
    pub fn inner(&self) -> &Arc<dyn MaterializationLedger> {
        &self.inner
    }

    /// Borrow the transport.
    #[must_use]
    pub fn transport(&self) -> &Arc<dyn GossipBroadcaster> {
        &self.transport
    }

    /// Apply an inbound delivery from the gossip wire to the inner
    /// ledger. Operators call this from their receiver task.
    ///
    /// # Errors
    /// Propagates [`LedgerError`] from the inner ledger.
    pub async fn ingest_delivery(
        &self,
        delivery: &GossipDelivery,
    ) -> Result<QuorumVerdict, LedgerError> {
        self.inner
            .ingest(&delivery.stage_id, delivery.threshold, &delivery.receipt)
            .await
    }
}

#[async_trait]
impl MaterializationLedger for GossipLedger {
    fn name(&self) -> &'static str {
        "gossip"
    }

    async fn ingest(
        &self,
        stage_id: &StageId,
        threshold: NonZeroUsize,
        receipt: &MaterializationReceipt,
    ) -> Result<QuorumVerdict, LedgerError> {
        // 1. Apply locally.
        let verdict = self.inner.ingest(stage_id, threshold, receipt).await?;
        // 2. Broadcast cluster-wide. Backend errors don't taint
        //    the local commit; we surface them as a typed log
        //    elsewhere (operator's transport choice).
        let _ = self
            .transport
            .broadcast_receipt(stage_id, threshold, receipt)
            .await;
        Ok(verdict)
    }

    async fn outcome(&self, key: &LedgerKey) -> Result<Option<QuorumVerdict>, LedgerError> {
        self.inner.outcome(key).await
    }

    async fn forget_stage(&self, stage_id: &StageId) -> Result<(), LedgerError> {
        self.inner.forget_stage(stage_id).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ledger::MemoryLedger;
    use crate::receipt::{MaterializationReceipt, NodeId};

    fn rcpt(emitter: u8, evidence: u8) -> MaterializationReceipt {
        MaterializationReceipt::for_drv([7u8; 32], NodeId::new([emitter; 32]), 100, [evidence; 32])
    }

    fn nz(n: usize) -> NonZeroUsize {
        NonZeroUsize::new(n).expect("test thresholds are non-zero")
    }

    fn stage() -> StageId {
        StageId::new("x")
    }

    fn assemble() -> (Arc<MemoryLedger>, Arc<FakeGossipTransport>, GossipLedger) {
        let inner = Arc::new(MemoryLedger::new());
        let transport = Arc::new(FakeGossipTransport::new());
        let ledger = GossipLedger::new(inner.clone(), transport.clone());
        (inner, transport, ledger)
    }

    // ── GossipBroadcaster (FakeGossipTransport) ────────────────

    #[tokio::test]
    async fn fake_transport_records_broadcasts() {
        let t = FakeGossipTransport::new();
        t.broadcast_receipt(&stage(), nz(3), &rcpt(1, 5))
            .await
            .unwrap();
        let b = t.broadcasts().await;
        assert_eq!(b.len(), 1);
        assert_eq!(b[0].stage_id, stage());
        assert_eq!(b[0].threshold, nz(3));
    }

    #[tokio::test]
    async fn fake_transport_subscribers_receive_broadcasts() {
        let t = FakeGossipTransport::new();
        let mut rx = t.subscribe_outbound();
        t.broadcast_receipt(&stage(), nz(3), &rcpt(1, 5))
            .await
            .unwrap();
        let received = rx.recv().await.unwrap();
        assert_eq!(received.receipt.emitter, NodeId::new([1u8; 32]));
    }

    #[tokio::test]
    async fn fake_transport_fail_next_returns_typed_error() {
        let t = FakeGossipTransport::new();
        t.fail_next(GossipError::NotConnected).await;
        let err = t
            .broadcast_receipt(&stage(), nz(1), &rcpt(1, 5))
            .await
            .unwrap_err();
        assert_eq!(err.kind(), "not_connected");
    }

    #[tokio::test]
    async fn fake_transport_broadcasts_with_no_subscribers_dont_fail() {
        let t = FakeGossipTransport::new();
        t.broadcast_receipt(&stage(), nz(1), &rcpt(1, 5))
            .await
            .unwrap();
    }

    #[tokio::test]
    async fn fake_transport_name_is_stable() {
        let t = FakeGossipTransport::new();
        assert_eq!(t.name(), "fake");
    }

    #[test]
    fn gossip_error_kinds_are_stable() {
        assert_eq!(GossipError::Backend("x".into()).kind(), "backend");
        assert_eq!(GossipError::NotConnected.kind(), "not_connected");
    }

    // ── GossipLedger ───────────────────────────────────────────

    #[tokio::test]
    async fn ingest_writes_locally_and_broadcasts() {
        let (inner, transport, ledger) = assemble();
        ledger.ingest(&stage(), nz(1), &rcpt(1, 5)).await.unwrap();
        // Inner has it.
        assert_eq!(inner.len().await, 1);
        // Transport saw it.
        let b = transport.broadcasts().await;
        assert_eq!(b.len(), 1);
        assert_eq!(b[0].stage_id, stage());
    }

    #[tokio::test]
    async fn ingest_outcome_propagated_from_inner() {
        let (_inner, _t, ledger) = assemble();
        let verdict = ledger.ingest(&stage(), nz(1), &rcpt(1, 5)).await.unwrap();
        // Threshold=1 + one emitter → Reached immediately.
        assert!(verdict.is_reached());
    }

    #[tokio::test]
    async fn transport_failure_does_not_taint_local_commit() {
        let (inner, transport, ledger) = assemble();
        transport.fail_next(GossipError::NotConnected).await;
        // Even though transport will fail, local commit succeeds.
        ledger.ingest(&stage(), nz(1), &rcpt(1, 5)).await.unwrap();
        assert_eq!(inner.len().await, 1);
    }

    #[tokio::test]
    async fn ingest_delivery_routes_to_inner_only() {
        let (inner, transport, ledger) = assemble();
        let delivery = GossipDelivery {
            stage_id: stage(),
            threshold: nz(1),
            receipt: rcpt(7, 5),
        };
        ledger.ingest_delivery(&delivery).await.unwrap();
        assert_eq!(inner.len().await, 1);
        // No outbound broadcast for an inbound delivery.
        assert_eq!(transport.broadcasts().await.len(), 0);
    }

    #[tokio::test]
    async fn forget_stage_passes_through_to_inner() {
        let (inner, _t, ledger) = assemble();
        ledger.ingest(&stage(), nz(1), &rcpt(1, 5)).await.unwrap();
        assert_eq!(inner.len().await, 1);
        ledger.forget_stage(&stage()).await.unwrap();
        assert_eq!(inner.len().await, 0);
    }

    #[tokio::test]
    async fn outcome_read_passes_through_to_inner() {
        let (_inner, _t, ledger) = assemble();
        ledger.ingest(&stage(), nz(1), &rcpt(1, 5)).await.unwrap();
        let key = LedgerKey {
            stage_id: stage(),
            kind: crate::receipt::ReceiptKind::Drv,
            subject: [7u8; 32],
        };
        let outcome = ledger.outcome(&key).await.unwrap();
        assert!(outcome.is_some());
    }

    #[tokio::test]
    async fn inner_and_transport_accessors() {
        let (inner, transport, ledger) = assemble();
        // Just sanity that the borrowed references match the originals.
        assert!(Arc::ptr_eq(
            ledger.inner(),
            &(inner as Arc<dyn MaterializationLedger>)
        ));
        assert!(Arc::ptr_eq(
            ledger.transport(),
            &(transport as Arc<dyn GossipBroadcaster>)
        ));
    }

    #[tokio::test]
    async fn backend_name_is_stable() {
        let (_inner, _t, ledger) = assemble();
        assert_eq!(ledger.name(), "gossip");
    }
}