commonware-consensus 2026.9.0

Order opaque messages in a Byzantine environment.
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
use crate::{
    simplex::{
        scheme::Scheme,
        types::{
            Attributable, Certificate, Finalization, Finalize, Kind, Notarization, Notarize,
            Nullification, Nullify, Proposal, Subject, Vote,
        },
    },
    types::{Participant, Round as Rnd},
};
use commonware_cryptography::{Digest, certificate::Verification};
use commonware_parallel::Strategy;
use commonware_runtime::telemetry::traces::TracedExt as _;
use commonware_utils::{non_empty, ordered::Set};
use rand::rngs::StdRng;
use rand_core::{CryptoRng, SeedableRng};
use std::{future::Future, mem, sync::Arc};
use tracing::{Instrument as _, Span, info_span};

/// Runs a CPU-bound job through [Strategy::spawn], entering `span` on the worker thread and
/// instrumenting the returned future so the offloaded work stays attributed to the caller's trace.
fn offload<P, F, T>(len: usize, span: Span, strategy: &P, job: F) -> impl Future<Output = T> + Send
where
    P: Strategy,
    F: FnOnce(P) -> T + Send + 'static,
    T: Send + 'static,
{
    let worker_span = span.clone();
    strategy
        .spawn(len, move |strategy| worker_span.in_scope(|| job(strategy)))
        .instrument(span)
}

/// Certification progress for one kind of vote.
///
/// Each kind certifies independently: a view can legitimately certify both
/// a notarization and a nullification.
struct Certification<V> {
    /// Verified votes required to recover a certificate.
    quorum: usize,
    /// Whether the scheme benefits from batching signature verification.
    batchable: bool,
    /// Progress toward a certificate.
    state: State<V>,
}

/// The state of a [Certification].
enum State<V> {
    /// No certificate yet. Votes accumulate toward a quorum.
    Incomplete {
        /// Votes awaiting signature verification.
        pending: Vec<V>,
        /// Votes with verified signatures, held for certificate recovery.
        verified: Vec<V>,
    },
    /// A certificate exists. Further votes are dropped.
    Complete,
}

impl<V> Certification<V> {
    /// Creates an empty [State::Incomplete] whose vote buffers allocate lazily.
    const fn new(quorum: usize, batchable: bool) -> Self {
        Self {
            quorum,
            batchable,
            state: State::Incomplete {
                pending: Vec::new(),
                verified: Vec::new(),
            },
        }
    }

    /// Buffers a vote for verification (or, if already verified, for
    /// certificate recovery). The caller owns signer uniqueness. Votes that
    /// arrive after completion are dropped.
    fn add(&mut self, vote: V, is_verified: bool) {
        // Completed certifications drop subsequent votes without allocating.
        let State::Incomplete { pending, verified } = &mut self.state else {
            return;
        };

        // Verified votes may accumulate to quorum. A batchable pending buffer
        // only needs the remaining unverified slots, while a non-batchable
        // pending buffer is consumed after each vote.
        let initial_capacity = if is_verified || self.batchable {
            self.quorum.saturating_sub(verified.len()).max(1)
        } else {
            1
        };
        let votes = if is_verified { verified } else { pending };
        if votes.capacity() == 0 {
            votes.reserve_exact(initial_capacity);
        }
        votes.push(vote);
    }

    /// Returns true if a batch verification should run: pending votes exist,
    /// the quorum is unmet, and (for batchable schemes) the buffers together
    /// could reach it.
    const fn should_verify(&self) -> bool {
        match &self.state {
            State::Incomplete { pending, verified } => {
                !pending.is_empty()
                    && verified.len() < self.quorum
                    && (!self.batchable || verified.len() + pending.len() >= self.quorum)
            }
            State::Complete => false,
        }
    }

    /// Runs `f` over the buffered votes and stores the verified set it
    /// returns, or `None` if a batch is not worth verifying (see
    /// [Self::should_verify]).
    ///
    /// `f` receives the pending and previously verified votes and returns the
    /// new verified set plus the signers that failed verification. Returns
    /// the number of votes processed alongside those signers.
    async fn try_verify<F, Fut>(&mut self, f: F) -> Option<(usize, Vec<Participant>)>
    where
        F: FnOnce(Vec<V>, Vec<V>) -> Fut,
        Fut: Future<Output = (Vec<V>, Vec<Participant>)>,
    {
        if !self.should_verify() {
            return None;
        }
        let State::Incomplete { pending, verified } = &mut self.state else {
            unreachable!("certification complete despite should_verify");
        };
        let batch = pending.len();
        let (pending, prior) = (mem::take(pending), mem::take(verified));
        let (votes, invalid) = f(pending, prior).await;
        let State::Incomplete { verified, .. } = &mut self.state else {
            unreachable!("certification completed mid-verification");
        };
        *verified = votes;
        Some((batch, invalid))
    }

    /// Completes with a verified quorum, surrendering it for certificate
    /// recovery, or `None` if the quorum is unmet.
    fn try_complete(&mut self) -> Option<Vec<V>> {
        let State::Incomplete { verified, .. } = &mut self.state else {
            return None;
        };
        if verified.len() < self.quorum {
            return None;
        }
        let votes = mem::take(verified);
        self.complete();
        Some(votes)
    }

    /// Completes, dropping all buffered votes.
    fn complete(&mut self) {
        self.state = State::Complete;
    }

    /// Returns true if a certificate exists.
    const fn is_complete(&self) -> bool {
        matches!(self.state, State::Complete)
    }

    /// Retains only the votes matching `f`.
    fn retain(&mut self, f: impl Fn(&V) -> bool) {
        if let State::Incomplete { pending, verified } = &mut self.state {
            pending.retain(|v| f(v));
            verified.retain(|v| f(v));
        }
    }
}

/// How the selected proposal changed after an update.
pub(super) struct ProposalUpdate {
    /// Whether the selected proposal changed.
    pub(super) changed: bool,
    /// Whether an existing proposal was replaced with a different one.
    pub(super) replaced: bool,
}

/// What the round knows about its proposal, and how it was learned.
pub(super) enum ProposalState<D: Digest> {
    /// No proposal is known.
    Unknown,
    /// Learned from the leader's notarize vote. An authoritative proposal may
    /// replace it.
    Leader(Proposal<D>),
    /// Learned from a verified certificate or locally constructed finalize.
    /// Never replaced.
    Certificate(Proposal<D>),
}

impl<D: Digest> ProposalState<D> {
    /// Returns the proposal, if known.
    const fn proposal(&self) -> Option<&Proposal<D>> {
        match self {
            Self::Unknown => None,
            Self::Leader(proposal) | Self::Certificate(proposal) => Some(proposal),
        }
    }

    /// Updates the proposal from a leader vote or authoritative evidence.
    ///
    /// An unknown proposal accepts either source. An authoritative proposal
    /// may supersede a leader vote. All other transitions are ignored.
    ///
    /// Returns the selected proposal if its value changed. Certifying the
    /// leader-selected proposal returns `None`.
    fn update(&mut self, next: Self) -> Option<&Proposal<D>> {
        match (&*self, &next) {
            (Self::Unknown, Self::Leader(_) | Self::Certificate(_)) => {}
            (Self::Leader(_), Self::Certificate(_)) => {}
            _ => return None,
        }

        let changed = self.proposal() != next.proposal();
        *self = next;
        changed.then(|| self.proposal().expect("updated proposal must be known"))
    }
}

/// `Verifier` is a utility for tracking and verifying consensus messages.
///
/// For schemes where [`Verifier::is_batchable()`](commonware_cryptography::certificate::Verifier::is_batchable)
/// returns `true` (such as [ed25519], [bls12381_multisig] and [bls12381_threshold]), this struct collects
/// messages and defers verification until enough messages exist to potentially reach a quorum, enabling
/// efficient batch verification. For schemes where `is_batchable()` returns `false` (such as [secp256r1]),
/// signatures are verified eagerly as they arrive since there is no batching benefit.
///
/// To avoid unnecessary verification, it also tracks the number of already verified messages (ensuring
/// we no longer attempt to verify messages after a quorum of valid messages have already been verified).
///
/// Once polled, async verification moves the pending batch and accumulated verified votes into
/// the worker. Do not cancel an in-flight verification unless the verifier will also be discarded.
///
/// [ed25519]: crate::simplex::scheme::ed25519
/// [bls12381_multisig]: crate::simplex::scheme::bls12381_multisig
/// [bls12381_threshold]: crate::simplex::scheme::bls12381_threshold
/// [secp256r1]: crate::simplex::scheme::secp256r1
pub struct Verifier<S: Scheme<D>, D: Digest> {
    /// Signing scheme used to verify votes and assemble certificates.
    scheme: Arc<S>,

    /// The round being certified.
    round: Rnd,

    /// The round's leader, once identified.
    leader: Option<Participant>,

    /// The round's proposal, once known.
    ///
    /// A known leader's notarize vote may supply the initial proposal. An
    /// independently authenticated proposal is authoritative and may replace
    /// it, or be established before the leader is identified.
    proposal: ProposalState<D>,

    /// Notarize certification progress.
    notarize: Certification<Notarize<S, D>>,
    /// Nullify certification progress.
    nullify: Certification<Nullify<S>>,
    /// Finalize certification progress.
    finalize: Certification<Finalize<S, D>>,
}

impl<S: Scheme<D>, D: Digest> Verifier<S, D> {
    /// Creates a new `Verifier`.
    ///
    /// # Arguments
    ///
    /// * `round` - The round being certified.
    /// * `scheme` - Scheme handle used to verify and aggregate votes.
    /// * `quorum` - Number of votes (2f+1) required to reach a quorum.
    pub fn new(round: Rnd, scheme: impl Into<Arc<S>>, quorum: u32) -> Self {
        // Hold quorum as usize to simplify comparisons against queue lengths.
        let quorum = quorum as usize;
        let batchable = S::is_batchable();
        Self {
            scheme: scheme.into(),

            round,

            leader: None,
            proposal: ProposalState::Unknown,

            notarize: Certification::new(quorum, batchable),
            nullify: Certification::new(quorum, batchable),
            finalize: Certification::new(quorum, batchable),
        }
    }

    /// Returns the ordered participant set.
    pub(super) fn participants(&self) -> &Set<S::PublicKey> {
        self.scheme.participants()
    }

    /// Returns the round's leader, once known.
    pub const fn leader(&self) -> Option<Participant> {
        self.leader
    }

    /// Returns the round's proposal, once known.
    pub const fn proposal(&self) -> Option<&Proposal<D>> {
        self.proposal.proposal()
    }

    /// Attempts to construct a certificate from verified votes: the first kind
    /// (notarization, then nullification, then finalization) with a verified
    /// quorum. Call repeatedly to drain every constructible kind.
    ///
    /// Once recovery starts, it consumes the verified votes. Do not cancel unless
    /// the verifier will also be discarded.
    pub async fn try_construct_certificate(
        &mut self,
        strategy: &impl Strategy,
    ) -> Option<Certificate<S, D>> {
        if let Some(notarizes) = self.notarize.try_complete() {
            let span = info_span!(
                "simplex.batcher.try_construct_notarization",
                epoch = self.round.epoch().traced(),
                view = self.round.view().traced()
            );
            let scheme = Arc::clone(&self.scheme);
            let notarization = offload(notarizes.len(), span, strategy, move |strategy| {
                Notarization::from_owned_notarizes(
                    scheme.as_ref(),
                    non_empty![@notarizes],
                    &strategy,
                )
                .expect("verified notarize quorum must assemble")
            })
            .await;
            return Some(Certificate::Notarization(notarization));
        }

        if let Some(nullifies) = self.nullify.try_complete() {
            let span = info_span!(
                "simplex.batcher.try_construct_nullification",
                epoch = self.round.epoch().traced(),
                view = self.round.view().traced()
            );
            let scheme = Arc::clone(&self.scheme);
            let nullification = offload(nullifies.len(), span, strategy, move |strategy| {
                Nullification::from_owned_nullifies(
                    scheme.as_ref(),
                    non_empty![@nullifies],
                    &strategy,
                )
                .expect("verified nullify quorum must assemble")
            })
            .await;
            return Some(Certificate::Nullification(nullification));
        }

        if let Some(finalizes) = self.finalize.try_complete() {
            let span = info_span!(
                "simplex.batcher.try_construct_finalization",
                epoch = self.round.epoch().traced(),
                view = self.round.view().traced()
            );
            let scheme = Arc::clone(&self.scheme);
            let finalization = offload(finalizes.len(), span, strategy, move |strategy| {
                Finalization::from_owned_finalizes(
                    scheme.as_ref(),
                    non_empty![@finalizes],
                    &strategy,
                )
                .expect("verified finalize quorum must assemble")
            })
            .await;
            return Some(Certificate::Finalization(finalization));
        }

        None
    }

    /// Returns true if a certificate of `kind` exists.
    pub(super) const fn has_certificate(&self, kind: Kind) -> bool {
        match kind {
            Kind::Notarization => self.notarize.is_complete(),
            Kind::Nullification => self.nullify.is_complete(),
            Kind::Finalization => self.finalize.is_complete(),
        }
    }

    /// Records that a certificate of `kind` exists, dropping its buffered votes.
    pub(super) fn record_certificate(&mut self, kind: Kind) {
        match kind {
            Kind::Notarization => self.notarize.complete(),
            Kind::Nullification => self.nullify.complete(),
            Kind::Finalization => self.finalize.complete(),
        }
    }

    /// Tries to set the proposal from the known leader's notarize, dropping
    /// buffered votes for any other proposal (they cannot contribute to a
    /// certificate).
    ///
    /// Does nothing if the leader is unknown, `notarize` is not from the
    /// leader, or the proposal state rejects the transition.
    fn try_set_proposal_from_leader(&mut self, notarize: &Notarize<S, D>) {
        if self.leader != Some(notarize.signer()) {
            return;
        }
        self.set_proposal(ProposalState::Leader(notarize.proposal.clone()));
    }

    /// Updates the proposal state and, if the selected proposal changes, drops
    /// buffered notarize and finalize votes for any other proposal.
    ///
    /// Returns whether the selected proposal changed and whether the accepted
    /// change replaced an existing proposal. Rejected transitions and a
    /// provenance-only change return `false` for both.
    pub(super) fn set_proposal(&mut self, proposal: ProposalState<D>) -> ProposalUpdate {
        let replaced = self.proposal.proposal().is_some();
        let Some(proposal) = self.proposal.update(proposal) else {
            return ProposalUpdate {
                changed: false,
                replaced: false,
            };
        };
        self.notarize.retain(|n| &n.proposal == proposal);
        self.finalize.retain(|f| &f.proposal == proposal);
        ProposalUpdate {
            changed: true,
            replaced,
        }
    }

    /// Adds a [Vote] message to the batch for later verification.
    ///
    /// If the message has already been verified (e.g., we built it), it is stored
    /// directly for certificate recovery. Otherwise, it is added to the appropriate
    /// pending queue. Notarize and finalize votes for a proposal other than the
    /// known proposal are dropped since they cannot contribute to a certificate.
    ///
    /// If a leader is known and the message is a [Vote::Notarize] from that leader,
    /// this method may reveal the leader proposal.
    ///
    /// Callers must add at most one vote of each kind per signer. The batcher's
    /// [`Round`](super::Round) enforces this while recording votes.
    ///
    /// # Arguments
    ///
    /// * `msg` - The [Vote] message to add.
    /// * `verified` - A boolean indicating if the message has already been verified.
    pub fn add(&mut self, msg: Vote<S, D>, verified: bool) {
        match msg {
            Vote::Notarize(notarize) => {
                self.try_set_proposal_from_leader(&notarize);

                // If the proposal is known and the message is not for it, drop it
                if let Some(proposal) = self.proposal()
                    && proposal != &notarize.proposal
                {
                    return;
                }
                self.notarize.add(notarize, verified);
            }
            Vote::Nullify(nullify) => {
                self.nullify.add(nullify, verified);
            }
            Vote::Finalize(finalize) => {
                // If the proposal is known and the message is not for it, drop it
                if let Some(proposal) = self.proposal()
                    && proposal != &finalize.proposal
                {
                    return;
                }
                self.finalize.add(finalize, verified);
            }
        }
    }

    /// Sets the leader for the current consensus view. Setting the same
    /// leader again is a no-op (stable leaders are re-stamped as the voter's
    /// current view advances through a term).
    ///
    /// `notarize` carries the leader's already-received vote, if any. Its
    /// proposal is learned when none is known. An authoritative proposal is
    /// never replaced.
    ///
    /// # Panics
    ///
    /// Panics if a different leader was already set or if `notarize` is not
    /// from `leader`. Both values are locally derived, so neither assertion
    /// can fire on adversarial input.
    pub fn set_leader(&mut self, leader: Participant, notarize: Option<&Notarize<S, D>>) {
        if let Some(existing) = self.leader {
            // Enforces the stable-leader contract (see `Elector::elect`).
            assert_eq!(existing, leader, "leader changed within round");
        }
        self.leader = Some(leader);
        if let Some(notarize) = notarize {
            assert_eq!(notarize.signer(), leader, "notarize must be from leader");
            self.try_set_proposal_from_leader(notarize);
        }
    }

    /// Batch verifies pending [Vote::Notarize] messages, if worthwhile: the
    /// proposal is known (notarizes reference one proposal) and the buffers
    /// warrant a batch (see [Certification::should_verify]).
    ///
    /// It uses `S::verify_attestations` for efficient batch verification, run as one CPU-bound job
    /// submitted through [Strategy::spawn] so a parallel strategy hosts it on its own pool
    /// instead of occupying the calling task.
    ///
    /// # Arguments
    ///
    /// * `rng` - Randomness source used by schemes that require batching randomness.
    ///
    /// # Returns
    ///
    /// The number of votes processed and the signer indices for whom verification
    /// failed, or `None` if verification was not worthwhile.
    pub async fn try_verify_notarizes<R: CryptoRng>(
        &mut self,
        rng: &mut R,
        strategy: &impl Strategy,
    ) -> Option<(usize, Vec<Participant>)> {
        // Until the proposal is known, notarizes may reference many different
        // proposals.
        if matches!(self.proposal, ProposalState::Unknown) {
            return None;
        }
        self.notarize
            .try_verify(|notarizes, mut verified_notarizes| {
                let span = info_span!(
                    "simplex.batcher.verify_notarizes",
                    epoch = self.round.epoch().traced(),
                    view = self.round.view().traced()
                );
                let scheme = Arc::clone(&self.scheme);
                let mut rng = StdRng::from_rng(rng);
                offload(notarizes.len(), span, strategy, move |strategy| {
                    let (proposals, attestations): (Vec<_>, Vec<_>) = notarizes
                        .into_iter()
                        .map(|n| (n.proposal, n.attestation))
                        .unzip();
                    // All proposals here are equal: pending votes are filtered to the
                    // selected proposal before verification becomes ready.
                    let proposal = &proposals[0];

                    let Verification { verified, invalid } = scheme.verify_attestations::<_, D, _>(
                        &mut rng,
                        Subject::Notarize { proposal },
                        attestations,
                        &strategy,
                    );

                    verified_notarizes.extend(verified.into_iter().zip(proposals).map(
                        |(attestation, proposal)| Notarize {
                            proposal,
                            attestation,
                        },
                    ));
                    (verified_notarizes, invalid)
                })
            })
            .await
    }

    /// Batch verifies pending [Vote::Nullify] messages, if worthwhile (see
    /// [Certification::should_verify]).
    ///
    /// It uses `S::verify_attestations` for efficient batch verification, run as one CPU-bound job
    /// submitted through [Strategy::spawn] so a parallel strategy hosts it on its own pool
    /// instead of occupying the calling task.
    ///
    /// # Arguments
    ///
    /// * `rng` - Randomness source used by schemes that require batching randomness.
    ///
    /// # Returns
    ///
    /// The number of votes processed and the signer indices for whom verification
    /// failed, or `None` if verification was not worthwhile.
    pub async fn try_verify_nullifies<R: CryptoRng>(
        &mut self,
        rng: &mut R,
        strategy: &impl Strategy,
    ) -> Option<(usize, Vec<Participant>)> {
        self.nullify
            .try_verify(|nullifies, mut verified_nullifies| {
                let span = info_span!(
                    "simplex.batcher.verify_nullifies",
                    epoch = self.round.epoch().traced(),
                    view = self.round.view().traced()
                );
                let round = nullifies[0].round;
                let scheme = Arc::clone(&self.scheme);
                let mut rng = StdRng::from_rng(rng);
                offload(nullifies.len(), span, strategy, move |strategy| {
                    let Verification { verified, invalid } = scheme.verify_attestations::<_, D, _>(
                        &mut rng,
                        Subject::Nullify { round },
                        nullifies.into_iter().map(|nullify| nullify.attestation),
                        &strategy,
                    );

                    verified_nullifies.extend(
                        verified
                            .into_iter()
                            .map(|attestation| Nullify { round, attestation }),
                    );
                    (verified_nullifies, invalid)
                })
            })
            .await
    }

    /// Batch verifies pending [Vote::Finalize] messages, if worthwhile: the
    /// proposal is known (finalizes reference one proposal) and the buffers
    /// warrant a batch (see [Certification::should_verify]).
    ///
    /// It uses `S::verify_attestations` for efficient batch verification, run as one CPU-bound job
    /// submitted through [Strategy::spawn] so a parallel strategy hosts it on its own pool
    /// instead of occupying the calling task.
    ///
    /// # Arguments
    ///
    /// * `rng` - Randomness source used by schemes that require batching randomness.
    ///
    /// # Returns
    ///
    /// The number of votes processed and the signer indices for whom verification
    /// failed, or `None` if verification was not worthwhile.
    pub async fn try_verify_finalizes<R: CryptoRng>(
        &mut self,
        rng: &mut R,
        strategy: &impl Strategy,
    ) -> Option<(usize, Vec<Participant>)> {
        // Until the proposal is known (from the leader's vote or a verified
        // certificate, which suffices even when the leader is unknown, e.g. a
        // round only learned about through certificates), finalizes may
        // reference many different proposals.
        if matches!(self.proposal, ProposalState::Unknown) {
            return None;
        }
        self.finalize
            .try_verify(|finalizes, mut verified_finalizes| {
                let span = info_span!(
                    "simplex.batcher.verify_finalizes",
                    epoch = self.round.epoch().traced(),
                    view = self.round.view().traced()
                );
                let scheme = Arc::clone(&self.scheme);
                let mut rng = StdRng::from_rng(rng);
                offload(finalizes.len(), span, strategy, move |strategy| {
                    let (proposals, attestations): (Vec<_>, Vec<_>) = finalizes
                        .into_iter()
                        .map(|n| (n.proposal, n.attestation))
                        .unzip();
                    let proposal = &proposals[0];

                    let Verification { verified, invalid } = scheme.verify_attestations::<_, D, _>(
                        &mut rng,
                        Subject::Finalize { proposal },
                        attestations,
                        &strategy,
                    );

                    verified_finalizes.extend(verified.into_iter().zip(proposals).map(
                        |(attestation, proposal)| Finalize {
                            proposal,
                            attestation,
                        },
                    ));
                    (verified_finalizes, invalid)
                })
            })
            .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        simplex::{
            mocks::wrapped,
            scheme::{
                bls12381_multisig,
                bls12381_threshold::{
                    standard as bls12381_threshold_std, vrf as bls12381_threshold_vrf,
                },
                ed25519, secp256r1,
            },
        },
        types::{Epoch, Round, View},
    };
    use commonware_cryptography::{
        bls12381::primitives::variant::{MinPk, MinSig},
        certificate::mocks::Fixture,
        ed25519::PublicKey,
        sha256::Digest as Sha256,
    };
    use commonware_macros::test_async;
    use commonware_parallel::Sequential;
    use commonware_utils::{Faults, N3f1, TestRng, test_rng};

    const NAMESPACE: &[u8] = b"test";

    impl<V> Certification<V> {
        /// Returns the pending buffer (empty once complete).
        fn pending(&self) -> &[V] {
            match &self.state {
                State::Incomplete { pending, .. } => pending,
                State::Complete => &[],
            }
        }

        /// Returns the verified buffer (empty once complete).
        fn verified(&self) -> &[V] {
            match &self.state {
                State::Incomplete { verified, .. } => verified,
                State::Complete => &[],
            }
        }

        /// Returns the capacities of the pending and verified vote buffers.
        fn capacities(&self) -> (usize, usize) {
            match &self.state {
                State::Incomplete { pending, verified } => {
                    (pending.capacity(), verified.capacity())
                }
                State::Complete => (0, 0),
            }
        }
    }

    /// A round reserves vote storage only for phases that receive votes.
    #[test]
    fn test_verifier_allocates_vote_buffers_lazily() {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = ed25519::fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let round = Round::new(Epoch::new(0), View::new(1));
        let mut verifier =
            Verifier::<ed25519::Scheme, Sha256>::new(round, schemes[0].clone(), quorum);

        assert_eq!(verifier.notarize.capacities(), (0, 0));
        assert_eq!(verifier.nullify.capacities(), (0, 0));
        assert_eq!(verifier.finalize.capacities(), (0, 0));

        let notarize = create_notarize(&schemes[0], round, View::zero(), 1);
        verifier.add(Vote::Notarize(notarize), false);
        let (pending, verified) = verifier.notarize.capacities();
        assert!(pending >= quorum as usize);
        assert_eq!(verified, 0);
        assert_eq!(verifier.nullify.capacities(), (0, 0));
        assert_eq!(verifier.finalize.capacities(), (0, 0));

        verifier.record_certificate(Kind::Notarization);
        assert_eq!(verifier.notarize.capacities(), (0, 0));
    }

    #[test_async]
    async fn test_non_batchable_certification_avoids_repeated_quorum_reservations() {
        let quorum = 64;
        let mut certification = Certification::new(quorum, false);
        certification.add(1u8, false);
        certification
            .try_verify(|pending, mut verified| async move {
                verified.extend(pending);
                (verified, Vec::new())
            })
            .await
            .expect("non-batchable pending votes must verify eagerly");

        certification.add(2u8, false);
        let (pending, _) = certification.capacities();
        assert!(
            pending < quorum,
            "a single eagerly verified vote should not reserve a quorum-sized buffer"
        );
    }

    #[test_async]
    async fn test_batchable_certification_reserves_only_remaining_quorum() {
        let quorum = 64;
        let mut certification = Certification::new(quorum, true);
        for vote in 0..quorum {
            certification.add(vote, false);
        }
        certification
            .try_verify(|mut pending, _| async move {
                pending.pop().expect("quorum batch must be non-empty");
                (pending, Vec::new())
            })
            .await
            .expect("a quorum of pending votes must trigger batch verification");

        certification.add(quorum, false);
        let (pending, _) = certification.capacities();
        assert!(
            pending < quorum,
            "one replacement vote should not reserve a quorum-sized buffer"
        );
    }

    // Helper function to create a sample digest
    fn sample_digest(v: u8) -> Sha256 {
        Sha256::from([v; 32]) // Simple fixed digest for testing
    }

    // Helper to create a Notarize message for any signing scheme
    fn create_notarize<S: Scheme<Sha256>>(
        scheme: &S,
        round: Round,
        parent_view: View,
        payload_val: u8,
    ) -> Notarize<S, Sha256> {
        let proposal = Proposal::new(round, parent_view, sample_digest(payload_val));
        Notarize::sign(scheme, proposal).unwrap()
    }

    // Helper to create a Nullify message for any signing scheme
    fn create_nullify<S: Scheme<Sha256>>(scheme: &S, round: Round) -> Nullify<S> {
        Nullify::sign::<Sha256>(scheme, round).unwrap()
    }

    // Helper to create a Finalize message for any signing scheme
    fn create_finalize<S: Scheme<Sha256>>(
        scheme: &S,
        round: Round,
        parent_view: View,
        payload_val: u8,
    ) -> Finalize<S, Sha256> {
        let proposal = Proposal::new(round, parent_view, sample_digest(payload_val));
        Finalize::sign(scheme, proposal).unwrap()
    }

    fn add_notarize<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );

        let round = Round::new(Epoch::new(0), View::new(1));
        let leader_notarize = create_notarize(&schemes[0], round, View::new(0), 1);
        let verified_notarize = create_notarize(&schemes[1], round, View::new(0), 1);
        let pending_notarize = create_notarize(&schemes[2], round, View::new(0), 1);
        let notarize_diff = create_notarize(&schemes[3], round, View::new(0), 2);

        verifier.add(Vote::Notarize(leader_notarize.clone()), false);
        assert_eq!(verifier.notarize.pending().len(), 1);
        assert_eq!(verifier.notarize.verified().len(), 0);

        verifier.add(Vote::Notarize(verified_notarize), true);
        assert_eq!(verifier.notarize.pending().len(), 1);
        assert_eq!(verifier.notarize.verified().len(), 1);

        verifier.set_leader(leader_notarize.signer(), Some(&leader_notarize));
        assert_eq!(verifier.leader(), Some(leader_notarize.signer()));
        assert_eq!(verifier.proposal(), Some(&leader_notarize.proposal));
        assert_eq!(verifier.notarize.pending().len(), 1);

        verifier.add(Vote::Notarize(pending_notarize), false);
        assert_eq!(verifier.notarize.pending().len(), 2);

        verifier.add(Vote::Notarize(notarize_diff), false);
        assert_eq!(verifier.notarize.pending().len(), 2);

        let mut verifier2 = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round2 = Round::new(Epoch::new(0), View::new(2));
        let notarize_non_leader = create_notarize(&schemes[1], round2, View::new(1), 3);
        let notarize_leader = create_notarize(&schemes[0], round2, View::new(1), 3);

        verifier2.set_leader(notarize_leader.signer(), None);
        verifier2.add(Vote::Notarize(notarize_non_leader), false);
        assert_eq!(verifier2.leader(), Some(notarize_leader.signer()));
        assert!(verifier2.proposal().is_none());
        assert_eq!(verifier2.notarize.pending().len(), 1);

        verifier2.add(Vote::Notarize(notarize_leader.clone()), false);
        assert_eq!(verifier2.proposal(), Some(&notarize_leader.proposal));
        assert_eq!(verifier2.notarize.pending().len(), 2);
    }

    #[test]
    fn test_add_notarize() {
        add_notarize(bls12381_threshold_vrf::fixture::<MinSig, _>);
        add_notarize(bls12381_threshold_vrf::fixture::<MinPk, _>);
        add_notarize(bls12381_threshold_std::fixture::<MinSig, _>);
        add_notarize(bls12381_threshold_std::fixture::<MinPk, _>);
        add_notarize(bls12381_multisig::fixture::<MinSig, _>);
        add_notarize(bls12381_multisig::fixture::<MinPk, _>);
        add_notarize(ed25519::fixture);
        add_notarize(secp256r1::fixture);
    }

    fn set_leader<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );

        let round = Round::new(Epoch::new(0), View::new(1));
        let leader_notarize = create_notarize(&schemes[0], round, View::new(0), 1);
        let other_notarize = create_notarize(&schemes[1], round, View::new(0), 1);

        verifier.add(Vote::Notarize(other_notarize), false);
        assert_eq!(verifier.notarize.pending().len(), 1);

        let leader = leader_notarize.signer();
        verifier.set_leader(leader, None);
        assert_eq!(verifier.leader(), Some(leader));
        assert!(verifier.proposal().is_none());
        assert_eq!(verifier.notarize.pending().len(), 1);

        verifier.add(Vote::Notarize(leader_notarize.clone()), false);
        assert_eq!(verifier.proposal(), Some(&leader_notarize.proposal));
        assert_eq!(verifier.notarize.pending().len(), 2);

        let mut verifier2 = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        verifier2.add(Vote::Notarize(leader_notarize.clone()), true);
        verifier2.set_leader(leader, Some(&leader_notarize));
        assert_eq!(verifier2.leader(), Some(leader));
        assert_eq!(verifier2.proposal(), Some(&leader_notarize.proposal));

        // A verified notarization supersedes a conflicting proposal learned
        // from the leader's vote.
        let notarized_proposal = Proposal::new(round, View::new(0), sample_digest(2));
        assert_ne!(notarized_proposal, leader_notarize.proposal);
        assert!(
            verifier2
                .set_proposal(ProposalState::Certificate(notarized_proposal.clone()))
                .changed
        );
        assert_eq!(verifier2.proposal(), Some(&notarized_proposal));

        // Re-adopting the same proposal reports no change.
        assert!(
            !verifier2
                .set_proposal(ProposalState::Certificate(notarized_proposal.clone()))
                .changed
        );

        // The first notarization remains authoritative.
        let conflicting_notarized_proposal = Proposal::new(round, View::new(0), sample_digest(3));
        assert!(
            !verifier2
                .set_proposal(ProposalState::Certificate(conflicting_notarized_proposal))
                .changed
        );
        assert_eq!(verifier2.proposal(), Some(&notarized_proposal));

        // If the notarization arrives first, setting the leader cannot replace
        // its proposal with a conflicting buffered leader vote.
        let mut verifier3 = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        assert!(
            verifier3
                .set_proposal(ProposalState::Certificate(notarized_proposal.clone()))
                .changed
        );
        assert!(verifier3.leader().is_none());
        assert_eq!(verifier3.proposal(), Some(&notarized_proposal));
        verifier3.set_leader(leader, Some(&leader_notarize));
        assert_eq!(verifier3.leader(), Some(leader));
        assert_eq!(verifier3.proposal(), Some(&notarized_proposal));
    }

    #[test]
    fn test_set_leader() {
        set_leader(bls12381_threshold_vrf::fixture::<MinSig, _>);
        set_leader(bls12381_threshold_vrf::fixture::<MinPk, _>);
        set_leader(bls12381_threshold_std::fixture::<MinSig, _>);
        set_leader(bls12381_threshold_std::fixture::<MinPk, _>);
        set_leader(bls12381_multisig::fixture::<MinSig, _>);
        set_leader(bls12381_multisig::fixture::<MinPk, _>);
        set_leader(ed25519::fixture);
        set_leader(secp256r1::fixture);
    }

    async fn ready_and_verify_notarizes<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));
        let notarizes: Vec<_> = schemes
            .iter()
            .map(|scheme| create_notarize(scheme, round, View::new(0), 1))
            .collect();

        assert!(!verifier.notarize.should_verify());

        verifier.set_leader(notarizes[0].signer(), None);
        verifier.add(Vote::Notarize(notarizes[0].clone()), false);
        // Non-batchable schemes verify immediately when pending votes exist
        assert_eq!(!verifier.notarize.should_verify(), S::is_batchable());
        assert_eq!(verifier.notarize.pending().len(), 1);

        verifier.add(Vote::Notarize(notarizes[1].clone()), false);
        assert_eq!(!verifier.notarize.should_verify(), S::is_batchable());
        verifier.add(Vote::Notarize(notarizes[2].clone()), false);
        assert_eq!(!verifier.notarize.should_verify(), S::is_batchable());
        verifier.add(Vote::Notarize(notarizes[3].clone()), false);
        assert!(verifier.notarize.should_verify());
        assert_eq!(verifier.notarize.pending().len(), 4);

        let (batch, failed_bulk) = verifier
            .try_verify_notarizes(&mut rng, &Sequential)
            .await
            .unwrap();
        assert_eq!(batch, 4);
        assert!(failed_bulk.is_empty());
        assert_eq!(verifier.notarize.verified().len(), 4);
        assert!(verifier.notarize.pending().is_empty());
        assert!(!verifier.notarize.should_verify());

        let mut verifier2 = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round2 = Round::new(Epoch::new(0), View::new(2));
        let leader_vote = create_notarize(&schemes[0], round2, View::new(1), 10);
        let mut faulty_vote = create_notarize(&schemes[1], round2, View::new(1), 10);
        verifier2.set_leader(leader_vote.signer(), None);
        verifier2.add(Vote::Notarize(leader_vote.clone()), false);
        faulty_vote.attestation.signer = Participant::from_usize(schemes.len() + 10);
        verifier2.add(Vote::Notarize(faulty_vote.clone()), false);

        for scheme in schemes.iter().skip(2).take(quorum as usize - 2) {
            verifier2.add(
                Vote::Notarize(create_notarize(scheme, round2, View::new(1), 10)),
                false,
            );
        }
        assert!(verifier2.notarize.should_verify());

        let (batch, failed_second) = verifier2
            .try_verify_notarizes(&mut rng, &Sequential)
            .await
            .unwrap();
        assert_eq!(batch, quorum as usize);
        assert!(
            verifier2
                .notarize
                .verified()
                .iter()
                .any(|notarize| notarize == &leader_vote)
        );
        assert_eq!(failed_second, vec![faulty_vote.signer()]);
    }

    #[test_async]
    async fn test_ready_and_verify_notarizes() {
        ready_and_verify_notarizes(bls12381_threshold_vrf::fixture::<MinSig, _>).await;
        ready_and_verify_notarizes(bls12381_threshold_vrf::fixture::<MinPk, _>).await;
        ready_and_verify_notarizes(bls12381_threshold_std::fixture::<MinSig, _>).await;
        ready_and_verify_notarizes(bls12381_threshold_std::fixture::<MinPk, _>).await;
        ready_and_verify_notarizes(bls12381_multisig::fixture::<MinSig, _>).await;
        ready_and_verify_notarizes(bls12381_multisig::fixture::<MinPk, _>).await;
        ready_and_verify_notarizes(ed25519::fixture).await;
        ready_and_verify_notarizes(secp256r1::fixture).await;
    }

    fn add_nullify<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));
        let pending_nullify = create_nullify(&schemes[0], round);
        let verified_nullify = create_nullify(&schemes[1], round);

        verifier.add(Vote::Nullify(pending_nullify), false);
        assert_eq!(verifier.nullify.pending().len(), 1);
        assert_eq!(verifier.nullify.verified().len(), 0);

        verifier.add(Vote::Nullify(verified_nullify), true);
        assert_eq!(verifier.nullify.pending().len(), 1);
        assert_eq!(verifier.nullify.verified().len(), 1);
    }

    #[test]
    fn test_add_nullify() {
        add_nullify(bls12381_threshold_vrf::fixture::<MinSig, _>);
        add_nullify(bls12381_threshold_vrf::fixture::<MinPk, _>);
        add_nullify(bls12381_threshold_std::fixture::<MinSig, _>);
        add_nullify(bls12381_threshold_std::fixture::<MinPk, _>);
        add_nullify(bls12381_multisig::fixture::<MinSig, _>);
        add_nullify(bls12381_multisig::fixture::<MinPk, _>);
        add_nullify(ed25519::fixture);
        add_nullify(secp256r1::fixture);
    }

    async fn ready_and_verify_nullifies<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));
        let nullifies: Vec<_> = schemes
            .iter()
            .map(|scheme| create_nullify(scheme, round))
            .collect();

        verifier.add(Vote::Nullify(nullifies[0].clone()), true);
        assert_eq!(verifier.nullify.verified().len(), 1);

        verifier.add(Vote::Nullify(nullifies[1].clone()), false);
        // Non-batchable schemes verify immediately when pending votes exist
        assert_eq!(!verifier.nullify.should_verify(), S::is_batchable());
        verifier.add(Vote::Nullify(nullifies[2].clone()), false);
        assert_eq!(!verifier.nullify.should_verify(), S::is_batchable());
        verifier.add(Vote::Nullify(nullifies[3].clone()), false);
        assert!(verifier.nullify.should_verify());
        assert_eq!(verifier.nullify.pending().len(), 3);

        let (batch, failed) = verifier
            .try_verify_nullifies(&mut rng, &Sequential)
            .await
            .unwrap();
        assert_eq!(batch, 3);
        assert!(failed.is_empty());
        assert_eq!(verifier.nullify.verified().len(), 4);
        assert!(verifier.nullify.pending().is_empty());
        assert!(!verifier.nullify.should_verify());
    }

    #[test_async]
    async fn test_ready_and_verify_nullifies() {
        ready_and_verify_nullifies(bls12381_threshold_vrf::fixture::<MinSig, _>).await;
        ready_and_verify_nullifies(bls12381_threshold_vrf::fixture::<MinPk, _>).await;
        ready_and_verify_nullifies(bls12381_threshold_std::fixture::<MinSig, _>).await;
        ready_and_verify_nullifies(bls12381_threshold_std::fixture::<MinPk, _>).await;
        ready_and_verify_nullifies(bls12381_multisig::fixture::<MinSig, _>).await;
        ready_and_verify_nullifies(bls12381_multisig::fixture::<MinPk, _>).await;
        ready_and_verify_nullifies(ed25519::fixture).await;
        ready_and_verify_nullifies(secp256r1::fixture).await;
    }

    fn add_finalize<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));
        let finalize_a = create_finalize(&schemes[0], round, View::new(0), 1);
        let finalize_b = create_finalize(&schemes[1], round, View::new(0), 2);
        let verified_a = create_finalize(&schemes[2], round, View::new(0), 1);
        let rejected_b = create_finalize(&schemes[3], round, View::new(0), 2);

        verifier.add(Vote::Finalize(finalize_b), false);
        assert_eq!(verifier.finalize.pending().len(), 1);
        assert_eq!(verifier.finalize.verified().len(), 0);

        verifier.add(Vote::Finalize(finalize_a.clone()), false);
        assert_eq!(verifier.finalize.pending().len(), 2);

        verifier.set_leader(finalize_a.signer(), None);
        assert!(verifier.proposal().is_none());
        let leader_notarize = create_notarize(&schemes[0], round, View::new(0), 1);
        verifier.try_set_proposal_from_leader(&leader_notarize);
        assert_eq!(verifier.finalize.pending().len(), 1);
        assert_eq!(verifier.finalize.pending()[0], finalize_a);
        assert_eq!(verifier.finalize.verified().len(), 0);

        verifier.add(Vote::Finalize(verified_a), true);
        assert_eq!(verifier.finalize.pending().len(), 1);
        assert_eq!(verifier.finalize.verified().len(), 1);

        verifier.add(Vote::Finalize(rejected_b), false);
        assert_eq!(verifier.finalize.pending().len(), 1);
        assert_eq!(verifier.finalize.verified().len(), 1);
    }

    #[test]
    fn test_add_finalize() {
        add_finalize(bls12381_threshold_vrf::fixture::<MinSig, _>);
        add_finalize(bls12381_threshold_vrf::fixture::<MinPk, _>);
        add_finalize(bls12381_threshold_std::fixture::<MinSig, _>);
        add_finalize(bls12381_threshold_std::fixture::<MinPk, _>);
        add_finalize(bls12381_multisig::fixture::<MinSig, _>);
        add_finalize(bls12381_multisig::fixture::<MinPk, _>);
        add_finalize(ed25519::fixture);
        add_finalize(secp256r1::fixture);
    }

    async fn ready_and_verify_finalizes<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));
        let finalizes: Vec<_> = schemes
            .iter()
            .map(|scheme| create_finalize(scheme, round, View::new(0), 1))
            .collect();

        assert!(!verifier.finalize.should_verify());

        let leader_notarize = create_notarize(&schemes[0], round, View::new(0), 1);
        verifier.set_leader(finalizes[0].signer(), Some(&leader_notarize));

        verifier.add(Vote::Finalize(finalizes[0].clone()), true);
        assert_eq!(verifier.finalize.verified().len(), 1);
        assert!(verifier.finalize.pending().is_empty());

        verifier.add(Vote::Finalize(finalizes[1].clone()), false);
        // Non-batchable schemes verify immediately when pending votes exist
        assert_eq!(!verifier.finalize.should_verify(), S::is_batchable());
        verifier.add(Vote::Finalize(finalizes[2].clone()), false);
        assert_eq!(!verifier.finalize.should_verify(), S::is_batchable());
        verifier.add(Vote::Finalize(finalizes[3].clone()), false);
        assert!(verifier.finalize.should_verify());

        let (batch, failed) = verifier
            .try_verify_finalizes(&mut rng, &Sequential)
            .await
            .unwrap();
        assert_eq!(batch, 3);
        assert!(failed.is_empty());
        assert_eq!(verifier.finalize.verified().len(), 4);
        assert!(verifier.finalize.pending().is_empty());
        assert!(!verifier.finalize.should_verify());
    }

    #[test_async]
    async fn test_ready_and_verify_finalizes() {
        ready_and_verify_finalizes(bls12381_threshold_vrf::fixture::<MinSig, _>).await;
        ready_and_verify_finalizes(bls12381_threshold_vrf::fixture::<MinPk, _>).await;
        ready_and_verify_finalizes(bls12381_threshold_std::fixture::<MinSig, _>).await;
        ready_and_verify_finalizes(bls12381_threshold_std::fixture::<MinPk, _>).await;
        ready_and_verify_finalizes(bls12381_multisig::fixture::<MinSig, _>).await;
        ready_and_verify_finalizes(bls12381_multisig::fixture::<MinPk, _>).await;
        ready_and_verify_finalizes(ed25519::fixture).await;
        ready_and_verify_finalizes(secp256r1::fixture).await;
    }

    fn leader_proposal_filters_messages<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));
        let proposal_a = Proposal::new(round, View::new(0), sample_digest(10));
        let proposal_b = Proposal::new(round, View::new(0), sample_digest(20));

        let leader_notarize = Notarize::sign(&schemes[0], proposal_a.clone()).unwrap();
        let pending_notarize_b = Notarize::sign(&schemes[1], proposal_b.clone()).unwrap();
        let verified_notarize_a = Notarize::sign(&schemes[2], proposal_a.clone()).unwrap();
        let verified_notarize_b = Notarize::sign(&schemes[3], proposal_b.clone()).unwrap();
        let pending_finalize_a = Finalize::sign(&schemes[0], proposal_a.clone()).unwrap();
        let pending_finalize_b = Finalize::sign(&schemes[1], proposal_b.clone()).unwrap();
        let verified_finalize_a = Finalize::sign(&schemes[2], proposal_a.clone()).unwrap();
        let verified_finalize_b = Finalize::sign(&schemes[3], proposal_b).unwrap();

        verifier.add(Vote::Notarize(leader_notarize.clone()), false);
        verifier.add(Vote::Notarize(pending_notarize_b), false);
        verifier.add(Vote::Notarize(verified_notarize_a), true);
        verifier.add(Vote::Notarize(verified_notarize_b), true);
        verifier.add(Vote::Finalize(pending_finalize_a), false);
        verifier.add(Vote::Finalize(pending_finalize_b), false);
        verifier.add(Vote::Finalize(verified_finalize_a), true);
        verifier.add(Vote::Finalize(verified_finalize_b), true);

        assert_eq!(verifier.notarize.pending().len(), 2);
        assert_eq!(verifier.notarize.verified().len(), 2);
        assert_eq!(verifier.finalize.pending().len(), 2);
        assert_eq!(verifier.finalize.verified().len(), 2);

        verifier.set_leader(leader_notarize.signer(), Some(&leader_notarize));

        assert_eq!(verifier.notarize.pending().len(), 1);
        assert_eq!(verifier.notarize.pending()[0].proposal, proposal_a);
        assert_eq!(verifier.notarize.verified().len(), 1);
        assert_eq!(verifier.notarize.verified()[0].proposal, proposal_a);
        assert_eq!(verifier.finalize.pending().len(), 1);
        assert_eq!(verifier.finalize.pending()[0].proposal, proposal_a);
        assert_eq!(verifier.finalize.verified().len(), 1);
        assert_eq!(verifier.finalize.verified()[0].proposal, proposal_a);
    }

    #[test]
    fn test_leader_proposal_filters_messages() {
        leader_proposal_filters_messages(bls12381_threshold_vrf::fixture::<MinSig, _>);
        leader_proposal_filters_messages(bls12381_threshold_vrf::fixture::<MinPk, _>);
        leader_proposal_filters_messages(bls12381_threshold_std::fixture::<MinSig, _>);
        leader_proposal_filters_messages(bls12381_threshold_std::fixture::<MinPk, _>);
        leader_proposal_filters_messages(bls12381_multisig::fixture::<MinSig, _>);
        leader_proposal_filters_messages(bls12381_multisig::fixture::<MinPk, _>);
        leader_proposal_filters_messages(ed25519::fixture);
        leader_proposal_filters_messages(secp256r1::fixture);
    }

    /// Re-stamping the same leader is scheme-independent bookkeeping, so one
    /// scheme is enough.
    #[test]
    fn test_set_leader_twice_same_value_is_noop() {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = ed25519::fixture(&mut rng, NAMESPACE, 3);
        let mut verifier = Verifier::<ed25519::Scheme, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            3,
        );
        let leader = Participant::new(0);
        verifier.set_leader(leader, None);
        verifier.set_leader(leader, None);
    }

    /// Changing an already-set leader is scheme-independent bookkeeping, so
    /// one scheme is enough.
    #[test]
    #[should_panic(expected = "leader changed within round")]
    fn test_set_leader_change_panics() {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = ed25519::fixture(&mut rng, NAMESPACE, 3);
        let mut verifier = Verifier::<ed25519::Scheme, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            3,
        );
        verifier.set_leader(Participant::new(0), None);
        verifier.set_leader(Participant::new(1), None);
    }

    /// The notarize handed to set_leader must come from the elected leader.
    /// This is a caller contract, not adversarial-input filtering.
    #[test]
    #[should_panic(expected = "notarize must be from leader")]
    fn test_set_leader_rejects_foreign_notarize() {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = ed25519::fixture(&mut rng, NAMESPACE, 3);
        let mut verifier = Verifier::<ed25519::Scheme, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            3,
        );
        let notarize = create_notarize(
            &schemes[1],
            Round::new(Epoch::new(0), View::new(1)),
            View::new(0),
            10,
        );
        verifier.set_leader(Participant::new(0), Some(&notarize));
    }

    async fn notarizes_wait_for_quorum<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));
        let leader_vote = create_notarize(&schemes[0], round, View::new(0), 1);

        verifier.set_leader(leader_vote.signer(), None);
        verifier.add(Vote::Notarize(leader_vote), false);
        // Non-batchable schemes verify immediately when pending votes exist
        assert_eq!(
            !verifier.notarize.should_verify(),
            S::is_batchable(),
            "Batchable schemes wait for quorum, non-batchable verify immediately"
        );

        for scheme in schemes.iter().skip(1).take(quorum as usize - 1) {
            verifier.add(
                Vote::Notarize(create_notarize(scheme, round, View::new(0), 1)),
                false,
            );
        }
        assert!(
            verifier.notarize.should_verify(),
            "Should be ready at quorum"
        );

        let (batch, _) = verifier
            .try_verify_notarizes(&mut rng, &Sequential)
            .await
            .unwrap();
        assert_eq!(batch, quorum as usize);
        assert!(!verifier.notarize.should_verify());
    }

    #[test_async]
    async fn test_notarizes_wait_for_quorum() {
        notarizes_wait_for_quorum(bls12381_threshold_vrf::fixture::<MinSig, _>).await;
        notarizes_wait_for_quorum(bls12381_threshold_vrf::fixture::<MinPk, _>).await;
        notarizes_wait_for_quorum(bls12381_threshold_std::fixture::<MinSig, _>).await;
        notarizes_wait_for_quorum(bls12381_threshold_std::fixture::<MinPk, _>).await;
        notarizes_wait_for_quorum(bls12381_multisig::fixture::<MinSig, _>).await;
        notarizes_wait_for_quorum(bls12381_multisig::fixture::<MinPk, _>).await;
        notarizes_wait_for_quorum(ed25519::fixture).await;
        notarizes_wait_for_quorum(secp256r1::fixture).await;
    }

    async fn ready_notarizes_without_leader<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 3);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));

        let notarizes: Vec<_> = schemes
            .iter()
            .take(quorum as usize)
            .map(|scheme| create_notarize(scheme, round, View::new(0), 1))
            .collect();

        for vote in notarizes.iter() {
            verifier.add(Vote::Notarize(vote.clone()), false);
        }

        // Without the leader's proposal, verification is refused and the
        // buffered votes are untouched
        assert!(
            verifier
                .try_verify_notarizes(&mut rng, &Sequential)
                .await
                .is_none(),
            "Should not verify without leader/proposal set"
        );
        assert_eq!(verifier.notarize.pending().len(), quorum as usize);

        verifier.set_leader(notarizes[0].signer(), Some(&notarizes[0]));
        assert!(
            verifier
                .try_verify_notarizes(&mut rng, &Sequential)
                .await
                .is_some(),
            "Should verify once leader is set"
        );
    }

    #[test_async]
    async fn test_ready_notarizes_without_leader_or_proposal() {
        ready_notarizes_without_leader(bls12381_threshold_vrf::fixture::<MinSig, _>).await;
        ready_notarizes_without_leader(bls12381_threshold_vrf::fixture::<MinPk, _>).await;
        ready_notarizes_without_leader(bls12381_threshold_std::fixture::<MinSig, _>).await;
        ready_notarizes_without_leader(bls12381_threshold_std::fixture::<MinPk, _>).await;
        ready_notarizes_without_leader(bls12381_multisig::fixture::<MinSig, _>).await;
        ready_notarizes_without_leader(bls12381_multisig::fixture::<MinPk, _>).await;
        ready_notarizes_without_leader(ed25519::fixture).await;
        ready_notarizes_without_leader(secp256r1::fixture).await;
    }

    async fn ready_finalizes_without_leader<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 3);
        let quorum = N3f1::quorum(schemes.len());
        let test_verifier_finalize = || {
            let mut verifier = Verifier::<S, Sha256>::new(
                Round::new(Epoch::new(0), View::new(1)),
                schemes[0].clone(),
                quorum,
            );
            let round = Round::new(Epoch::new(0), View::new(1));
            let finalizes: Vec<_> = schemes
                .iter()
                .take(quorum as usize)
                .map(|scheme| create_finalize(scheme, round, View::new(0), 1))
                .collect();

            for finalize in finalizes.iter() {
                verifier.add(Vote::Finalize(finalize.clone()), false);
            }

            (verifier, finalizes[0].clone())
        };

        let (mut verifier, finalize) = test_verifier_finalize();

        assert!(
            verifier
                .try_verify_finalizes(&mut rng, &Sequential)
                .await
                .is_none(),
            "Should not verify without leader/proposal set"
        );

        verifier.set_leader(finalize.signer(), None);
        assert!(
            verifier
                .try_verify_finalizes(&mut rng, &Sequential)
                .await
                .is_none(),
            "Should not verify with a leader but no proposal"
        );
        assert_eq!(verifier.finalize.pending().len(), quorum as usize);

        let (mut verifier, finalize) = test_verifier_finalize();

        verifier.set_proposal(ProposalState::Certificate(finalize.proposal.clone()));
        assert!(verifier.leader.is_none());
        assert!(
            verifier
                .try_verify_finalizes(&mut rng, &Sequential)
                .await
                .is_some(),
            "Should verify with a proposal and no leader"
        );
    }

    #[test_async]
    async fn test_ready_finalizes_without_leader_or_proposal() {
        ready_finalizes_without_leader(bls12381_threshold_vrf::fixture::<MinSig, _>).await;
        ready_finalizes_without_leader(bls12381_threshold_vrf::fixture::<MinPk, _>).await;
        ready_finalizes_without_leader(bls12381_threshold_std::fixture::<MinSig, _>).await;
        ready_finalizes_without_leader(bls12381_threshold_std::fixture::<MinPk, _>).await;
        ready_finalizes_without_leader(bls12381_multisig::fixture::<MinSig, _>).await;
        ready_finalizes_without_leader(bls12381_multisig::fixture::<MinPk, _>).await;
        ready_finalizes_without_leader(ed25519::fixture).await;
        ready_finalizes_without_leader(secp256r1::fixture).await;
    }

    async fn certificate_proposal_allows_finalize_verification<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(3)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(3));
        let conflicting = Proposal::new(round, View::new(2), sample_digest(8));
        let proposal = Proposal::new(round, View::new(2), sample_digest(9));

        verifier.set_leader(Participant::new(0), None);
        verifier.add(
            Vote::Notarize(Notarize::sign(&schemes[0], conflicting.clone()).unwrap()),
            false,
        );
        assert_eq!(verifier.proposal(), Some(&conflicting));

        // A certificate-set proposal overrides the vote-learned one.
        let update = verifier.set_proposal(ProposalState::Certificate(proposal.clone()));
        assert!(update.changed && update.replaced);
        assert_eq!(verifier.proposal(), Some(&proposal));

        for scheme in schemes.iter().take(quorum as usize) {
            verifier.add(
                Vote::Finalize(Finalize::sign(scheme, proposal.clone()).unwrap()),
                false,
            );
        }
        let (batch, failed) = verifier
            .try_verify_finalizes(&mut rng, &Sequential)
            .await
            .expect("finalizes should verify against the certificate proposal");
        assert_eq!(batch, quorum as usize);
        assert!(failed.is_empty());
    }

    #[test_async]
    async fn test_certificate_proposal_allows_finalize_verification() {
        certificate_proposal_allows_finalize_verification(
            bls12381_threshold_vrf::fixture::<MinSig, _>,
        )
        .await;
        certificate_proposal_allows_finalize_verification(
            bls12381_threshold_vrf::fixture::<MinPk, _>,
        )
        .await;
        certificate_proposal_allows_finalize_verification(
            bls12381_threshold_std::fixture::<MinSig, _>,
        )
        .await;
        certificate_proposal_allows_finalize_verification(
            bls12381_threshold_std::fixture::<MinPk, _>,
        )
        .await;
        certificate_proposal_allows_finalize_verification(bls12381_multisig::fixture::<MinSig, _>)
            .await;
        certificate_proposal_allows_finalize_verification(bls12381_multisig::fixture::<MinPk, _>)
            .await;
        certificate_proposal_allows_finalize_verification(ed25519::fixture).await;
        certificate_proposal_allows_finalize_verification(secp256r1::fixture).await;
    }

    #[test_async]
    async fn test_nonbatchable_finalize_accounting_tracks_proposal_override() {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = secp256r1::fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len()) as usize;
        let mut verifier = Verifier::<_, Sha256>::new(
            Round::new(Epoch::new(333), View::new(7)),
            schemes[0].clone(),
            quorum.try_into().unwrap(),
        );
        let round = Round::new(Epoch::new(333), View::new(7));
        let proposal_a = Proposal::new(round, View::new(6), sample_digest(1));
        let proposal_b = Proposal::new(round, View::new(6), sample_digest(2));

        verifier.set_leader(Participant::new(0), None);
        verifier.add(
            Vote::Notarize(Notarize::sign(&schemes[0], proposal_a.clone()).unwrap()),
            false,
        );
        verifier.add(
            Vote::Finalize(Finalize::sign(&schemes[0], proposal_a).unwrap()),
            false,
        );
        let (batch, failed) = verifier
            .try_verify_finalizes(&mut rng, &Sequential)
            .await
            .expect("nonbatchable schemes verify eagerly");
        assert_eq!(batch, 1);
        assert!(failed.is_empty());
        assert_eq!(verifier.finalize.verified().len(), 1);

        // The override drops the verified finalize for the old proposal.
        verifier.set_proposal(ProposalState::Certificate(proposal_b.clone()));
        assert!(verifier.finalize.verified().is_empty());

        for scheme in schemes.iter().take(quorum).skip(1) {
            verifier.add(
                Vote::Finalize(Finalize::sign(scheme, proposal_b.clone()).unwrap()),
                false,
            );
            let (batch, failed) = verifier
                .try_verify_finalizes(&mut rng, &Sequential)
                .await
                .expect("nonbatchable schemes verify eagerly");
            assert_eq!(batch, 1);
            assert!(failed.is_empty());
        }
        assert_eq!(verifier.finalize.verified().len(), quorum - 1);

        verifier.add(
            Vote::Finalize(Finalize::sign(&schemes[quorum], proposal_b).unwrap()),
            false,
        );
        let (batch, failed) = verifier
            .try_verify_finalizes(&mut rng, &Sequential)
            .await
            .expect("nonbatchable schemes verify eagerly");
        assert_eq!(batch, 1);
        assert!(failed.is_empty());
        assert_eq!(verifier.finalize.verified().len(), quorum);
    }

    fn verify_notarizes_empty<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 3);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));
        let leader_notarize = create_notarize(&schemes[0], round, View::new(0), 1);
        verifier.set_leader(leader_notarize.signer(), Some(&leader_notarize));
        assert!(verifier.notarize.pending().is_empty());
        assert!(!verifier.notarize.should_verify());
    }

    #[test]
    fn test_verify_notarizes_empty_pending_when_forced() {
        verify_notarizes_empty(bls12381_threshold_vrf::fixture::<MinSig, _>);
        verify_notarizes_empty(bls12381_threshold_vrf::fixture::<MinPk, _>);
        verify_notarizes_empty(bls12381_threshold_std::fixture::<MinSig, _>);
        verify_notarizes_empty(bls12381_threshold_std::fixture::<MinPk, _>);
        verify_notarizes_empty(bls12381_multisig::fixture::<MinSig, _>);
        verify_notarizes_empty(bls12381_multisig::fixture::<MinPk, _>);
        verify_notarizes_empty(ed25519::fixture);
        verify_notarizes_empty(secp256r1::fixture);
    }

    async fn verify_nullifies_empty<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 3);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        assert!(verifier.nullify.pending().is_empty());
        assert!(!verifier.nullify.should_verify());
        assert!(
            verifier
                .try_verify_nullifies(&mut rng, &Sequential)
                .await
                .is_none()
        );
        assert_eq!(verifier.nullify.verified().len(), 0);
    }

    #[test_async]
    async fn test_verify_nullifies_empty_pending() {
        verify_nullifies_empty(bls12381_threshold_vrf::fixture::<MinSig, _>).await;
        verify_nullifies_empty(bls12381_threshold_vrf::fixture::<MinPk, _>).await;
        verify_nullifies_empty(bls12381_threshold_std::fixture::<MinSig, _>).await;
        verify_nullifies_empty(bls12381_threshold_std::fixture::<MinPk, _>).await;
        verify_nullifies_empty(bls12381_multisig::fixture::<MinSig, _>).await;
        verify_nullifies_empty(bls12381_multisig::fixture::<MinPk, _>).await;
        verify_nullifies_empty(ed25519::fixture).await;
        verify_nullifies_empty(secp256r1::fixture).await;
    }

    async fn verify_finalizes_empty<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 3);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        verifier.set_leader(Participant::new(0), None);
        assert!(verifier.finalize.pending().is_empty());
        assert!(!verifier.finalize.should_verify());
        assert!(
            verifier
                .try_verify_finalizes(&mut rng, &Sequential)
                .await
                .is_none()
        );
        assert_eq!(verifier.finalize.verified().len(), 0);
    }

    #[test_async]
    async fn test_verify_finalizes_empty_pending() {
        verify_finalizes_empty(bls12381_threshold_vrf::fixture::<MinSig, _>).await;
        verify_finalizes_empty(bls12381_threshold_vrf::fixture::<MinPk, _>).await;
        verify_finalizes_empty(bls12381_threshold_std::fixture::<MinSig, _>).await;
        verify_finalizes_empty(bls12381_threshold_std::fixture::<MinPk, _>).await;
        verify_finalizes_empty(bls12381_multisig::fixture::<MinSig, _>).await;
        verify_finalizes_empty(bls12381_multisig::fixture::<MinPk, _>).await;
        verify_finalizes_empty(ed25519::fixture).await;
        verify_finalizes_empty(secp256r1::fixture).await;
    }

    async fn ready_notarizes_exact_quorum<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));

        let leader_vote = create_notarize(&schemes[0], round, View::new(0), 1);
        verifier.set_leader(leader_vote.signer(), None);
        verifier.add(Vote::Notarize(leader_vote), true);
        assert_eq!(verifier.notarize.verified().len(), 1);

        for (i, scheme) in schemes.iter().enumerate().skip(1).take(quorum as usize - 1) {
            let is_last = i == quorum as usize - 1;
            verifier.add(
                Vote::Notarize(create_notarize(scheme, round, View::new(0), 1)),
                false,
            );
            if is_last {
                assert!(
                    verifier.notarize.should_verify(),
                    "Should be ready at exact quorum"
                );
            } else if S::is_batchable() {
                // Batchable schemes wait for quorum
                assert!(!verifier.notarize.should_verify());
            } else {
                // Non-batchable schemes verify immediately when pending votes exist
                assert!(verifier.notarize.should_verify());
            }
        }

        let (batch, failed) = verifier
            .try_verify_notarizes(&mut rng, &Sequential)
            .await
            .unwrap();
        assert_eq!(batch, quorum as usize - 1);
        assert!(failed.is_empty());
        assert_eq!(verifier.notarize.verified().len(), quorum as usize);
        assert!(!verifier.notarize.should_verify());
    }

    #[test_async]
    async fn test_ready_notarizes_exact_quorum() {
        ready_notarizes_exact_quorum(bls12381_threshold_vrf::fixture::<MinSig, _>).await;
        ready_notarizes_exact_quorum(bls12381_threshold_vrf::fixture::<MinPk, _>).await;
        ready_notarizes_exact_quorum(bls12381_threshold_std::fixture::<MinSig, _>).await;
        ready_notarizes_exact_quorum(bls12381_threshold_std::fixture::<MinPk, _>).await;
        ready_notarizes_exact_quorum(bls12381_multisig::fixture::<MinSig, _>).await;
        ready_notarizes_exact_quorum(bls12381_multisig::fixture::<MinPk, _>).await;
        ready_notarizes_exact_quorum(ed25519::fixture).await;
        ready_notarizes_exact_quorum(secp256r1::fixture).await;
    }

    fn ready_nullifies_exact_quorum<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));

        verifier.add(Vote::Nullify(create_nullify(&schemes[0], round)), true);
        assert_eq!(verifier.nullify.verified().len(), 1);

        let pending_schemes: Vec<_> = schemes.iter().take(quorum as usize).skip(1).collect();
        for (i, scheme) in pending_schemes.iter().enumerate() {
            let is_last = i == pending_schemes.len() - 1;
            verifier.add(Vote::Nullify(create_nullify(scheme, round)), false);
            if is_last {
                assert!(verifier.nullify.should_verify());
            } else if S::is_batchable() {
                // Batchable schemes wait for quorum
                assert!(!verifier.nullify.should_verify());
            } else {
                // Non-batchable schemes verify immediately when pending votes exist
                assert!(verifier.nullify.should_verify());
            }
        }
    }

    #[test]
    fn test_ready_nullifies_exact_quorum() {
        ready_nullifies_exact_quorum(bls12381_threshold_vrf::fixture::<MinSig, _>);
        ready_nullifies_exact_quorum(bls12381_threshold_vrf::fixture::<MinPk, _>);
        ready_nullifies_exact_quorum(bls12381_threshold_std::fixture::<MinSig, _>);
        ready_nullifies_exact_quorum(bls12381_threshold_std::fixture::<MinPk, _>);
        ready_nullifies_exact_quorum(bls12381_multisig::fixture::<MinSig, _>);
        ready_nullifies_exact_quorum(bls12381_multisig::fixture::<MinPk, _>);
        ready_nullifies_exact_quorum(ed25519::fixture);
        ready_nullifies_exact_quorum(secp256r1::fixture);
    }

    fn ready_finalizes_exact_quorum<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));
        let leader_finalize = create_finalize(&schemes[0], round, View::new(0), 1);
        let leader_notarize = create_notarize(&schemes[0], round, View::new(0), 1);
        verifier.set_leader(leader_finalize.signer(), Some(&leader_notarize));
        verifier.add(Vote::Finalize(leader_finalize), true);
        assert_eq!(verifier.finalize.verified().len(), 1);

        let pending_schemes: Vec<_> = schemes.iter().take(quorum as usize).skip(1).collect();
        for (i, scheme) in pending_schemes.iter().enumerate() {
            let is_last = i == pending_schemes.len() - 1;
            verifier.add(
                Vote::Finalize(create_finalize(scheme, round, View::new(0), 1)),
                false,
            );
            if is_last {
                assert!(verifier.finalize.should_verify());
            } else if S::is_batchable() {
                // Batchable schemes wait for quorum
                assert!(!verifier.finalize.should_verify());
            } else {
                // Non-batchable schemes verify immediately when pending votes exist
                assert!(verifier.finalize.should_verify());
            }
        }
    }

    #[test]
    fn test_ready_finalizes_exact_quorum() {
        ready_finalizes_exact_quorum(bls12381_threshold_vrf::fixture::<MinSig, _>);
        ready_finalizes_exact_quorum(bls12381_threshold_vrf::fixture::<MinPk, _>);
        ready_finalizes_exact_quorum(bls12381_threshold_std::fixture::<MinSig, _>);
        ready_finalizes_exact_quorum(bls12381_threshold_std::fixture::<MinPk, _>);
        ready_finalizes_exact_quorum(bls12381_multisig::fixture::<MinSig, _>);
        ready_finalizes_exact_quorum(bls12381_multisig::fixture::<MinPk, _>);
        ready_finalizes_exact_quorum(ed25519::fixture);
        ready_finalizes_exact_quorum(secp256r1::fixture);
    }

    fn ready_notarizes_quorum_already_met_by_verified<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        assert!(
            schemes.len() > quorum as usize,
            "test requires more validators than the quorum"
        );
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));

        // Pre-load the leader vote as if it had already been processed.
        let leader_vote = create_notarize(&schemes[0], round, View::new(0), 1);
        verifier.set_leader(leader_vote.signer(), None);
        verifier.add(Vote::Notarize(leader_vote), false);

        // Mark enough verified notarizes to satisfy the quorum outright.
        for scheme in schemes.iter().take(quorum as usize) {
            verifier.add(
                Vote::Notarize(create_notarize(scheme, round, View::new(0), 1)),
                true,
            );
        }
        assert_eq!(verifier.notarize.verified().len(), quorum as usize);
        assert!(
            !verifier.notarize.should_verify(),
            "Should not be ready if quorum already met by verified messages"
        );

        // Additional pending votes must not flip readiness in this situation.
        let extra_vote = create_notarize(&schemes[quorum as usize], round, View::new(0), 1);
        verifier.add(Vote::Notarize(extra_vote), false);
        assert!(
            !verifier.notarize.should_verify(),
            "Should not be ready if quorum already met by verified messages"
        );
    }

    #[test]
    fn test_ready_notarizes_quorum_already_met_by_verified() {
        ready_notarizes_quorum_already_met_by_verified(
            bls12381_threshold_vrf::fixture::<MinSig, _>,
        );
        ready_notarizes_quorum_already_met_by_verified(bls12381_threshold_vrf::fixture::<MinPk, _>);
        ready_notarizes_quorum_already_met_by_verified(
            bls12381_threshold_std::fixture::<MinSig, _>,
        );
        ready_notarizes_quorum_already_met_by_verified(bls12381_threshold_std::fixture::<MinPk, _>);
        ready_notarizes_quorum_already_met_by_verified(bls12381_multisig::fixture::<MinSig, _>);
        ready_notarizes_quorum_already_met_by_verified(bls12381_multisig::fixture::<MinPk, _>);
        ready_notarizes_quorum_already_met_by_verified(ed25519::fixture);
        ready_notarizes_quorum_already_met_by_verified(secp256r1::fixture);
    }

    fn ready_nullifies_quorum_already_met_by_verified<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        assert!(
            schemes.len() > quorum as usize,
            "test requires more validators than the quorum"
        );
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));

        // First mark a quorum's worth of verified nullifies.
        for scheme in schemes.iter().take(quorum as usize) {
            verifier.add(Vote::Nullify(create_nullify(scheme, round)), true);
        }
        assert_eq!(verifier.nullify.verified().len(), quorum as usize);
        assert!(
            !verifier.nullify.should_verify(),
            "Should not be ready if quorum already met by verified messages"
        );

        // Pending messages alone cannot transition the batch to ready.
        let extra_nullify = create_nullify(&schemes[quorum as usize], round);
        verifier.add(Vote::Nullify(extra_nullify), false);
        assert!(
            !verifier.nullify.should_verify(),
            "Should not be ready if quorum already met by verified messages"
        );
    }

    #[test]
    fn test_ready_nullifies_quorum_already_met_by_verified() {
        ready_nullifies_quorum_already_met_by_verified(
            bls12381_threshold_vrf::fixture::<MinSig, _>,
        );
        ready_nullifies_quorum_already_met_by_verified(bls12381_threshold_vrf::fixture::<MinPk, _>);
        ready_nullifies_quorum_already_met_by_verified(
            bls12381_threshold_std::fixture::<MinSig, _>,
        );
        ready_nullifies_quorum_already_met_by_verified(bls12381_threshold_std::fixture::<MinPk, _>);
        ready_nullifies_quorum_already_met_by_verified(bls12381_multisig::fixture::<MinSig, _>);
        ready_nullifies_quorum_already_met_by_verified(bls12381_multisig::fixture::<MinPk, _>);
        ready_nullifies_quorum_already_met_by_verified(ed25519::fixture);
        ready_nullifies_quorum_already_met_by_verified(secp256r1::fixture);
    }

    fn ready_finalizes_quorum_already_met_by_verified<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        assert!(
            schemes.len() > quorum as usize,
            "test requires more validators than the quorum"
        );
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));

        // Prime the leader state so the quorum is already satisfied by verified finalizes.
        let leader_notarize = create_notarize(&schemes[0], round, View::new(0), 1);
        verifier.set_leader(leader_notarize.signer(), Some(&leader_notarize));

        // Feed exactly the number of verified finalizes required to hit the quorum.
        for scheme in schemes.iter().take(quorum as usize) {
            verifier.add(
                Vote::Finalize(create_finalize(scheme, round, View::new(0), 1)),
                true,
            );
        }
        assert_eq!(verifier.finalize.verified().len(), quorum as usize);
        assert!(
            !verifier.finalize.should_verify(),
            "Should not be ready if quorum already met by verified messages"
        );

        // Ensure additional pending finalizes do not incorrectly trigger readiness.
        let extra_finalize = create_finalize(&schemes[quorum as usize], round, View::new(0), 1);
        verifier.add(Vote::Finalize(extra_finalize), false);
        assert!(
            !verifier.finalize.should_verify(),
            "Should not be ready if quorum already met by verified messages"
        );
    }

    #[test]
    fn test_ready_finalizes_quorum_already_met_by_verified() {
        ready_finalizes_quorum_already_met_by_verified(
            bls12381_threshold_vrf::fixture::<MinSig, _>,
        );
        ready_finalizes_quorum_already_met_by_verified(bls12381_threshold_vrf::fixture::<MinPk, _>);
        ready_finalizes_quorum_already_met_by_verified(
            bls12381_threshold_std::fixture::<MinSig, _>,
        );
        ready_finalizes_quorum_already_met_by_verified(bls12381_threshold_std::fixture::<MinPk, _>);
        ready_finalizes_quorum_already_met_by_verified(bls12381_multisig::fixture::<MinSig, _>);
        ready_finalizes_quorum_already_met_by_verified(bls12381_multisig::fixture::<MinPk, _>);
        ready_finalizes_quorum_already_met_by_verified(ed25519::fixture);
        ready_finalizes_quorum_already_met_by_verified(secp256r1::fixture);
    }

    #[test_async]
    async fn test_certification_lifecycle() {
        // Non-batchable schemes verify eagerly whenever votes are pending.
        let mut eager = Certification::<u64>::new(3, false);
        eager.add(1, false);
        assert!(eager.should_verify());

        // Certification owns the pending and verified buffer lifecycle; its
        // caller owns signer uniqueness. Opaque values keep this test focused
        // on that boundary.
        let mut votes = Certification::<u64>::new(3, true);
        votes.add(1, false);
        votes.add(2, true);
        assert_eq!(votes.pending(), &[1]);
        assert_eq!(votes.verified(), &[2]);

        // Batchable schemes wait until the buffers could reach quorum.
        assert!(!votes.should_verify());
        votes.add(3, false);
        assert!(votes.should_verify());

        // Verification consumes both buffers and stores the new verified set.
        // Below quorum, recovery is refused.
        let (batch, invalid) = votes
            .try_verify(|pending, verified| async move {
                assert_eq!(pending, vec![1, 3]);
                assert_eq!(verified, vec![2]);
                (vec![1, 2], vec![])
            })
            .await
            .unwrap();
        assert_eq!(batch, 2);
        assert!(invalid.is_empty());
        assert!(votes.pending().is_empty());
        assert_eq!(votes.try_complete(), None);

        // At quorum, recovery completes the phase and consumes the votes.
        votes.add(3, true);
        assert_eq!(votes.try_complete(), Some(vec![1, 2, 3]));
        assert!(votes.is_complete());
        votes.add(5, false);
        assert!(votes.pending().is_empty());
        assert!(votes.verified().is_empty());
        assert!(
            votes
                .try_verify(|_, _| async { unreachable!() })
                .await
                .is_none()
        );
        assert_eq!(votes.try_complete(), None);

        votes.complete();
        assert!(votes.is_complete());

        // Network certificates complete without any votes.
        let mut votes = Certification::<u64>::new(3, true);
        votes.add(1, false);
        votes.complete();
        assert!(votes.is_complete());
        assert!(votes.pending().is_empty());
    }

    /// The leader's late notarize must still set the proposal after the
    /// notarize kind is certified, even though the vote itself is dropped.
    fn late_leader_vote_after_certification<S, F>(mut fixture: F)
    where
        S: Scheme<Sha256, PublicKey = PublicKey>,
        F: FnMut(&mut TestRng, &[u8], u32) -> Fixture<S>,
    {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<S, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));

        verifier.record_certificate(Kind::Notarization);
        verifier.set_leader(Participant::new(0), None);
        assert_eq!(verifier.leader(), Some(Participant::new(0)));
        assert!(verifier.proposal().is_none());

        let leader_notarize = create_notarize(&schemes[0], round, View::new(0), 1);
        let proposal = leader_notarize.proposal.clone();
        verifier.add(Vote::Notarize(leader_notarize), false);
        assert_eq!(verifier.proposal(), Some(&proposal));
        assert!(verifier.notarize.pending().is_empty());

        // Certifying one kind leaves the others accumulating
        verifier.add(Vote::Nullify(create_nullify(&schemes[0], round)), false);
        assert_eq!(verifier.nullify.pending().len(), 1);
    }

    #[test]
    fn test_late_leader_vote_after_certification() {
        late_leader_vote_after_certification(ed25519::fixture);
    }

    #[test_async]
    #[should_panic(expected = "verified notarize quorum must assemble")]
    async fn test_construct_panics_on_recovery_failure() {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = ed25519::fixture(&mut rng, NAMESPACE, 5);
        let schemes: Vec<_> = schemes
            .into_iter()
            .map(|scheme| wrapped::Scheme::new(scheme, wrapped::Behavior::RecoveryFailure))
            .collect();
        let quorum = N3f1::quorum(schemes.len());
        let quorum_size = usize::try_from(quorum).expect("quorum exceeds usize::MAX");
        let round = Round::new(Epoch::new(0), View::new(1));
        let mut verifier = Verifier::<_, Sha256>::new(round, schemes[0].clone(), quorum);

        let leader_notarize = create_notarize(&schemes[0], round, View::new(0), 1);
        verifier.set_leader(leader_notarize.signer(), Some(&leader_notarize));
        for scheme in schemes.iter().take(quorum_size) {
            verifier.add(
                Vote::Notarize(create_notarize(scheme, round, View::new(0), 1)),
                true,
            );
        }

        let _ = verifier.try_construct_certificate(&Sequential).await;
    }

    #[test_async]
    async fn test_construct_drains_kinds_in_order() {
        let mut rng = test_rng();
        let Fixture { schemes, .. } = ed25519::fixture(&mut rng, NAMESPACE, 5);
        let quorum = N3f1::quorum(schemes.len());
        let mut verifier = Verifier::<_, Sha256>::new(
            Round::new(Epoch::new(0), View::new(1)),
            schemes[0].clone(),
            quorum,
        );
        let round = Round::new(Epoch::new(0), View::new(1));

        // Give every kind a pre-verified quorum
        let leader_notarize = create_notarize(&schemes[0], round, View::new(0), 1);
        verifier.set_leader(leader_notarize.signer(), Some(&leader_notarize));
        for scheme in schemes.iter().take(quorum as usize) {
            verifier.add(
                Vote::Notarize(create_notarize(scheme, round, View::new(0), 1)),
                true,
            );
            verifier.add(Vote::Nullify(create_nullify(scheme, round)), true);
            verifier.add(
                Vote::Finalize(create_finalize(scheme, round, View::new(0), 1)),
                true,
            );
        }

        assert!(matches!(
            verifier.try_construct_certificate(&Sequential).await,
            Some(Certificate::Notarization(_))
        ));
        assert!(matches!(
            verifier.try_construct_certificate(&Sequential).await,
            Some(Certificate::Nullification(_))
        ));
        assert!(matches!(
            verifier.try_construct_certificate(&Sequential).await,
            Some(Certificate::Finalization(_))
        ));
        assert!(
            verifier
                .try_construct_certificate(&Sequential)
                .await
                .is_none()
        );
    }
}