engenho-substrate 0.53.184

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
//! `QuorumTracker` — the one quorum fold.
//!
//! Every place engenho asks "have enough distinct voters agreed?"
//! answers through [`Tally::verdict`]. There is no second copy of the
//! arithmetic: a ledger's read path, revoada's majority check and its
//! promotion gate all fold through this module.
//!
//!   * [`QuorumTracker`] is the fold scoped to one materialization
//!     target (a `(kind, subject)` pair) and fed
//!     [`MaterializationReceipt`]s. It is the substrate's
//!     eventually-consistent confirmation primitive.
//!   * [`Tally`] is the same fold over any voter and evidence type.
//!     It is public so a voter set that is not a receipt stream (for
//!     example revoada's configured voters) counts through this code
//!     instead of re-deriving it.
//!
//! The fold reports one of three states ([`QuorumState`]):
//!
//!   * `Pending`: fewer distinct voters than the threshold.
//!   * `Reached`: at least the threshold, and every voter holds the
//!     same evidence.
//!   * `Dissent`: at least the threshold, but voters disagree on the
//!     evidence. The consumer must investigate (re-derivation,
//!     eviction, alert).
//!
//! ## What the types rule out
//!
//!   * **A zero threshold.** It is a [`NonZeroUsize`]. A zero-vote
//!     quorum would be satisfied by an empty set; the old constructor
//!     clamped it to one silently, and now the caller has to decide.
//!   * **A verdict nobody folded.** [`QuorumVerdict`]'s fields are
//!     private and [`Tally::verdict`] is its only constructor, so code
//!     outside this module cannot build one (E0451). A ledger cannot
//!     answer `Reached` from arithmetic of its own; the in-memory
//!     ledger's read path once did, reporting 1 of 3 confirmations as
//!     reached.
//!   * **A voter counted twice.** Votes are a map keyed by voter; a
//!     voter that votes again replaces its earlier evidence.
//!
//! What the types do not rule out: a caller can still hand the fold
//! the wrong threshold. That is checked by the tests at each call
//! site, not by a type.
//!
//! ## Why per-(kind, subject) tracking
//!
//! A node can confirm many things; a tracker is scoped to ONE
//! materialization target. Composition: a stage with N targets
//! holds N trackers.

use std::collections::{BTreeMap, BTreeSet};
use std::num::NonZeroUsize;

use crate::receipt::{MaterializationReceipt, NodeId, ReceiptKind};

/// Which of the three states a quorum fold reached.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum QuorumState {
    /// Fewer distinct voters than the threshold.
    Pending,
    /// At least the threshold, and every voter holds the same evidence.
    Reached,
    /// At least the threshold, but voters disagree on the evidence.
    Dissent,
}

/// The verdict of the one quorum fold.
///
/// Sealed: every field is private and [`Tally::verdict`] is the only
/// constructor, so holding a `Reached` verdict means the fold ran on
/// the tracker's own threshold.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct QuorumVerdict {
    state: QuorumState,
    confirmed: usize,
    threshold: NonZeroUsize,
    evidence_variants: usize,
}

impl QuorumVerdict {
    /// Which of the three states the fold reached.
    #[must_use]
    pub fn state(&self) -> QuorumState {
        self.state
    }

    /// True when the state is [`QuorumState::Reached`].
    #[must_use]
    pub fn is_reached(&self) -> bool {
        self.state == QuorumState::Reached
    }

    /// Distinct voters counted.
    #[must_use]
    pub fn confirmed(&self) -> usize {
        self.confirmed
    }

    /// The threshold the fold counted against.
    #[must_use]
    pub fn threshold(&self) -> NonZeroUsize {
        self.threshold
    }

    /// Distinct pieces of evidence among the counted voters. 0 means
    /// no votes yet, 1 means every voter agrees, 2 or more is
    /// disagreement.
    #[must_use]
    pub fn evidence_variants(&self) -> usize {
        self.evidence_variants
    }
}

/// Distinct voters, each holding one piece of evidence, counted
/// against a non-zero threshold. The fold behind [`QuorumTracker`].
#[derive(Clone, Debug)]
pub struct Tally<V, E> {
    threshold: NonZeroUsize,
    /// `voter -> evidence`. The most recent vote per voter wins.
    votes: BTreeMap<V, E>,
}

impl<V: Ord, E: Ord> Tally<V, E> {
    /// An empty tally that needs `threshold` distinct voters.
    #[must_use]
    pub fn new(threshold: NonZeroUsize) -> Self {
        Self {
            threshold,
            votes: BTreeMap::new(),
        }
    }

    /// An empty tally that needs a strict majority of `voters`:
    /// `voters / 2 + 1` of them.
    #[must_use]
    pub fn majority_of(voters: NonZeroUsize) -> Self {
        Self::new(NonZeroUsize::MIN.saturating_add(voters.get() / 2))
    }

    /// The threshold this tally counts against.
    #[must_use]
    pub fn threshold(&self) -> NonZeroUsize {
        self.threshold
    }

    /// Record `voter`'s evidence and return the verdict after it. A
    /// voter that votes again replaces its earlier evidence (the voter
    /// is the authority for its own claim).
    pub fn record(&mut self, voter: V, evidence: E) -> QuorumVerdict {
        self.votes.insert(voter, evidence);
        self.verdict()
    }

    /// Distinct voters counted so far.
    #[must_use]
    pub fn confirmed_count(&self) -> usize {
        self.votes.len()
    }

    /// Distinct pieces of evidence among the counted voters.
    #[must_use]
    pub fn evidence_variants(&self) -> usize {
        self.votes.values().collect::<BTreeSet<_>>().len()
    }

    /// The distinct voters counted so far, in order.
    pub fn voters(&self) -> impl Iterator<Item = &V> {
        self.votes.keys()
    }

    /// Forget every vote. The threshold stays.
    pub fn reset(&mut self) {
        self.votes.clear();
    }

    /// The fold: the only place a [`QuorumVerdict`] is built.
    #[must_use]
    pub fn verdict(&self) -> QuorumVerdict {
        let confirmed = self.confirmed_count();
        let evidence_variants = self.evidence_variants();
        let state = if confirmed < self.threshold.get() {
            QuorumState::Pending
        } else if evidence_variants > 1 {
            QuorumState::Dissent
        } else {
            QuorumState::Reached
        };
        QuorumVerdict {
            state,
            confirmed,
            threshold: self.threshold,
            evidence_variants,
        }
    }
}

/// Per-target receipt accumulator: a [`Tally`] of emitters and their
/// evidence hashes, scoped to one `(kind, subject)` pair.
#[derive(Debug, Clone)]
pub struct QuorumTracker {
    kind: ReceiptKind,
    subject: [u8; 32],
    tally: Tally<NodeId, [u8; 32]>,
}

impl QuorumTracker {
    /// New tracker for `(kind, subject)` that needs `threshold`
    /// distinct emitters.
    #[must_use]
    pub fn new(kind: ReceiptKind, subject: [u8; 32], threshold: NonZeroUsize) -> Self {
        Self {
            kind,
            subject,
            tally: Tally::new(threshold),
        }
    }

    /// Subject the tracker is gating on.
    #[must_use]
    pub fn subject(&self) -> &[u8; 32] {
        &self.subject
    }

    /// Kind the tracker is gating on.
    #[must_use]
    pub fn kind(&self) -> &ReceiptKind {
        &self.kind
    }

    /// The threshold the tracker counts against.
    #[must_use]
    pub fn threshold(&self) -> NonZeroUsize {
        self.tally.threshold()
    }

    /// Current distinct-emitter count.
    #[must_use]
    pub fn confirmed_count(&self) -> usize {
        self.tally.confirmed_count()
    }

    /// True once the fold is past `Pending` (enough distinct emitters),
    /// whether or not their evidence agrees.
    #[must_use]
    pub fn has_quorum(&self) -> bool {
        self.verdict().state() != QuorumState::Pending
    }

    /// Distinct evidence-hash count. 1 = all emitters agree.
    /// 2+ = dissent. 0 = no receipts yet.
    #[must_use]
    pub fn evidence_variants(&self) -> usize {
        self.tally.evidence_variants()
    }

    /// Snapshot of distinct confirming emitters.
    #[must_use]
    pub fn emitters(&self) -> Vec<NodeId> {
        self.tally.voters().copied().collect()
    }

    /// Ingest a receipt. Returns the post-ingest verdict.
    ///
    /// Receipts for a DIFFERENT (kind, subject) are ignored: callers
    /// route per tracker. Re-emission from the same emitter overwrites
    /// the prior evidence hash (the emitter is the authority for its
    /// own claim).
    pub fn ingest(&mut self, receipt: &MaterializationReceipt) -> QuorumVerdict {
        if receipt.kind != self.kind || receipt.subject != self.subject {
            return self.verdict();
        }
        self.tally.record(receipt.emitter, receipt.evidence_hash)
    }

    /// The current verdict, without ingesting anything.
    #[must_use]
    pub fn verdict(&self) -> QuorumVerdict {
        self.tally.verdict()
    }

    /// Forget every confirmation — useful after evicting a target.
    pub fn reset(&mut self) {
        self.tally.reset();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

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

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

    fn other_subject_receipt(emitter: u8) -> MaterializationReceipt {
        MaterializationReceipt::for_drv(
            [99u8; 32], // different subject
            NodeId::new([emitter; 32]),
            100,
            [0u8; 32],
        )
    }

    fn other_kind_receipt(emitter: u8) -> MaterializationReceipt {
        MaterializationReceipt::for_nar([1u8; 32], NodeId::new([emitter; 32]), 100, [0u8; 32])
    }

    fn tracker(threshold: usize) -> QuorumTracker {
        QuorumTracker::new(ReceiptKind::Drv, [1u8; 32], nz(threshold))
    }

    #[test]
    fn starts_pending_with_zero_confirmations() {
        let v = tracker(3).verdict();
        assert_eq!(v.state(), QuorumState::Pending);
        assert_eq!(v.confirmed(), 0);
        assert_eq!(v.threshold(), nz(3));
        assert_eq!(v.evidence_variants(), 0);
    }

    #[test]
    fn reaches_quorum_when_threshold_distinct_emitters_agree() {
        let mut t = tracker(3);
        let v1 = t.ingest(&r(1, 5));
        assert_eq!((v1.state(), v1.confirmed()), (QuorumState::Pending, 1));
        let v2 = t.ingest(&r(2, 5));
        assert_eq!((v2.state(), v2.confirmed()), (QuorumState::Pending, 2));
        let v3 = t.ingest(&r(3, 5));
        assert!(v3.is_reached());
        assert_eq!((v3.confirmed(), v3.threshold()), (3, nz(3)));
    }

    #[test]
    fn duplicate_emitter_doesnt_increment_count() {
        let mut t = tracker(3);
        t.ingest(&r(1, 5));
        t.ingest(&r(1, 5)); // same emitter, same evidence
        t.ingest(&r(1, 5));
        assert_eq!(t.confirmed_count(), 1);
        assert!(!t.has_quorum());
    }

    #[test]
    fn dissent_when_evidence_variants_diverge_past_quorum() {
        let mut t = tracker(2);
        t.ingest(&r(1, 5));
        let v = t.ingest(&r(2, 6)); // different evidence
        assert_eq!(v.state(), QuorumState::Dissent);
        assert_eq!((v.confirmed(), v.evidence_variants()), (2, 2));
    }

    #[test]
    fn dissent_overrides_reached_when_thresholds_both_met() {
        let mut t = tracker(2);
        t.ingest(&r(1, 5));
        assert!(t.ingest(&r(2, 5)).is_reached());
        let v = t.ingest(&r(3, 7)); // Adds dissent.
        assert_eq!(v.state(), QuorumState::Dissent);
    }

    #[test]
    fn disagreement_below_threshold_is_still_pending() {
        let mut t = tracker(3);
        t.ingest(&r(1, 5));
        let v = t.ingest(&r(2, 6));
        assert_eq!(v.state(), QuorumState::Pending);
        assert_eq!(v.evidence_variants(), 2);
    }

    #[test]
    fn most_recent_evidence_per_emitter_wins() {
        let mut t = tracker(1);
        t.ingest(&r(1, 5));
        t.ingest(&r(1, 6)); // emitter 1 changes its mind
        assert_eq!(t.confirmed_count(), 1);
        // Only one evidence hash now → no dissent.
        assert_eq!(t.evidence_variants(), 1);
        assert!(t.verdict().is_reached());
    }

    #[test]
    fn ignores_receipts_with_different_subject() {
        let mut t = tracker(1);
        t.ingest(&other_subject_receipt(1));
        assert_eq!(t.confirmed_count(), 0);
    }

    #[test]
    fn ignores_receipts_with_different_kind() {
        let mut t = tracker(1);
        t.ingest(&other_kind_receipt(1));
        assert_eq!(t.confirmed_count(), 0);
    }

    #[test]
    fn verdict_reports_the_trackers_own_threshold() {
        let mut t = tracker(5);
        let v = t.ingest(&r(1, 5));
        assert_eq!(v.threshold(), nz(5));
        assert_eq!(t.threshold(), nz(5));
        assert_eq!(
            t.verdict(),
            v,
            "reading the verdict is the same fold as ingesting"
        );
    }

    #[test]
    fn reset_clears_state() {
        let mut t = tracker(2);
        t.ingest(&r(1, 5));
        t.ingest(&r(2, 5));
        assert!(t.has_quorum());
        t.reset();
        assert_eq!(t.confirmed_count(), 0);
        assert!(!t.has_quorum());
        assert_eq!(t.threshold(), nz(2), "reset keeps the threshold");
    }

    #[test]
    fn emitters_returns_distinct_set() {
        let mut t = tracker(3);
        t.ingest(&r(1, 5));
        t.ingest(&r(2, 5));
        t.ingest(&r(1, 5)); // duplicate
        assert_eq!(t.emitters().len(), 2);
    }

    #[test]
    fn majority_of_is_a_strict_majority() {
        let cases = [(1, 1), (2, 2), (3, 2), (4, 3), (5, 3), (6, 4), (7, 4)];
        for (voters, needed) in cases {
            let t = Tally::<u8, ()>::majority_of(nz(voters));
            assert_eq!(t.threshold(), nz(needed), "majority of {voters}");
        }
    }

    #[test]
    fn tally_counts_distinct_voters_once() {
        let mut t = Tally::majority_of(nz(5));
        for voter in [1u8, 1, 2, 2] {
            t.record(voter, ());
        }
        assert_eq!(t.verdict().state(), QuorumState::Pending);
        assert!(t.record(3, ()).is_reached());
    }
}