sc-statement-store 32.0.0

Substrate statement store.
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
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! Subscription logic for statement store.
//!
//! Manages subscriptions to statement topics and notifies subscribers when new statements arrive.
//! Uses multiple matcher tasks to handle subscriptions concurrently, each responsible for a subset
//! of subscriptions. Each matcher task maintains its own list of subscriptions and matches incoming
//! statements against them. When a new statement is submitted, it is sent to all matcher tasks for
//! processing. If a statement matches a subscription's filter, it is sent to the subscriber via an
//! async channel.
//!
//! This design allows for efficient handling of a large number of subscriptions and statements and
//! can be scaled by adjusting the number of matcher tasks.

// Buffer size for the matcher task channels, to backpressure the submission senders.
// This value is generous to allow for bursts of statements without dropping any or backpressuring
// too early.
const MATCHERS_TASK_CHANNEL_BUFFER_SIZE: usize = 80_000;

// Buffer size for individual subscriptions.
const SUBSCRIPTION_BUFFER_SIZE: usize = 128;
const STOP_RESERVE_CHANNEL_SLOTS: usize = 1;

/// Maximum number of active filters attached to one statement subscription.
///
/// Keeps one subscription useful for multiplexing, while bounding internal per-event filter-id
/// metadata to 128 `u64`s, i.e. 1 KiB before `Vec` overhead.
pub const MAX_FILTERS_PER_SUBSCRIPTION: usize = 128;
// Keep replay batches bounded by raw statement bytes. The JSON response is roughly twice this size
// because statements are hex-encoded.
pub(crate) const REPLAY_CHUNK_RAW_BYTES: usize = 4 * 1024 * 1024;
use futures::{Stream, StreamExt};
use itertools::Itertools;
use parking_lot::Mutex;

use crate::LOG_TARGET;
use sc_utils::id_sequence::SeqID;
use sp_core::{traits::SpawnNamed, Bytes, Encode};
pub use sp_statement_store::StatementStore;
use sp_statement_store::{
	FilterId, LiveStatementEvent, OptimizedTopicFilter, Result, Statement, StatementEvent, Topic,
	MAX_TOPICS,
};
use std::{
	collections::{hash_map::Entry, HashMap, HashSet, VecDeque},
	sync::{atomic::AtomicU64, Arc},
};

/// Error returned when attaching a filter to a multi-filter subscription fails
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AddFilterError {
	/// The subscription already has the maximum number of active filters
	LimitReached,
	/// The filter could not be registered: the store or the matcher is gone, or the subscription
	/// is no longer active
	Stopped,
}

/// Trait for initiating statement store subscriptions from the RPC module.
pub trait StatementStoreSubscriptionApi: Send + Sync {
	/// Subscribe to statements matching the topic filter.
	///
	/// Returns existing matching statements, a sender channel to send matched statements and a
	/// stream for receiving matched statements when they arrive.
	fn subscribe_statement(
		&self,
		topic_filter: OptimizedTopicFilter,
	) -> Result<(Vec<Vec<u8>>, async_channel::Sender<StatementEvent>, SubscriptionStatementsStream)>;
}

/// Creates multi-filter subscriptions for the RPC module
pub trait MultiFilterSubscriptionApi: Send + Sync {
	/// Creates an empty subscription that can receive filters dynamically
	fn create_subscription(&self) -> (SubscriptionHandle, MultiFilterEventStream);
}

/// Provides cursor-based replay for dynamically attached filters.
pub(crate) trait ReplaySnapshotProvider: Send + Sync {
	/// Capture the committed admission watermark and enqueue filter registration atomically with
	/// respect to submit.
	/// Returns the registered watermark, or `None` when registration could not be enqueued.
	///
	/// `enqueue` is called at most once, with the submit lock held: it must not block or call
	/// back into the store.
	fn register_replay(&self, enqueue: &mut dyn FnMut(u64) -> bool) -> Result<Option<u64>>;

	/// Read the next replay batch from the persisted admission sequence.
	fn replay_batch(
		&self,
		filter: &OptimizedTopicFilter,
		cursor: u64,
		watermark: u64,
	) -> Result<ReplayBatch>;
}

pub(crate) struct ReplayBatch {
	pub(crate) statements: Vec<Vec<u8>>,
	pub(crate) cursor: u64,
	pub(crate) done: bool,
}

/// A handle that attaches, removes, and inspects filters for one multi-filter subscription
#[derive(Clone)]
pub struct SubscriptionHandle {
	pub(crate) sub_id: SeqID,
	pub(crate) inner: Arc<Mutex<SubscriptionHandleInner>>,
	pub(crate) matchers: SubscriptionsMatchersHandlers,
	pub(crate) snapshot_provider: Arc<dyn ReplaySnapshotProvider>,
}

pub(crate) struct SubscriptionHandleInner {
	active_filter_ids: HashSet<FilterId>,
	next_filter_id: u64,
}

impl SubscriptionHandleInner {
	pub(crate) fn new() -> Self {
		Self { active_filter_ids: HashSet::new(), next_filter_id: 0 }
	}
}

impl SubscriptionHandle {
	/// Attaches a filter and returns its id
	pub fn add_filter(
		&self,
		filter: OptimizedTopicFilter,
	) -> std::result::Result<FilterId, AddFilterError> {
		let mut inner = self.inner.lock();
		if inner.active_filter_ids.len() >= MAX_FILTERS_PER_SUBSCRIPTION {
			return Err(AddFilterError::LimitReached);
		}

		let filter_id = FilterId::new(inner.next_filter_id);
		let sub_id = self.sub_id;

		let (result_tx, result_rx) = async_channel::bounded(1);
		let mut result_tx = Some(result_tx);
		let Some(_watermark) = self
			.snapshot_provider
			.register_replay(&mut |watermark| {
				let result_tx =
					result_tx.take().expect("register_replay invokes enqueue at most once; qed");
				self.matchers
					.try_send_by_seq_id(
						sub_id,
						MatcherMessage::RegisterFilter {
							sub_id,
							filter_id,
							filter: filter.clone(),
							watermark,
							result_tx,
						},
					)
					.is_ok()
			})
			.map_err(|_| AddFilterError::Stopped)?
		else {
			return Err(AddFilterError::Stopped);
		};
		match result_rx.recv_blocking() {
			Ok(result) => result?,
			Err(_) => return Err(AddFilterError::Stopped),
		}
		inner.next_filter_id = inner.next_filter_id.wrapping_add(1);
		inner.active_filter_ids.insert(filter_id);
		Ok(filter_id)
	}

	/// Removes a filter from this subscription
	pub fn remove_filter(&self, filter_id: FilterId) -> bool {
		let mut inner = self.inner.lock();
		if !inner.active_filter_ids.remove(&filter_id) {
			return false;
		}
		self.matchers.send_by_seq_id(
			self.sub_id,
			MatcherMessage::RemoveFilter { sub_id: self.sub_id, filter_id },
		);
		true
	}
}

struct PendingReplay {
	filter_id: FilterId,
	filter: OptimizedTopicFilter,
	cursor: u64,
	watermark: u64,
}

pub(crate) struct MultiFilterSubscriptionState {
	pending_replays: VecDeque<PendingReplay>,
	stopped: bool,
	stop_emitted: bool,
}

impl MultiFilterSubscriptionState {
	pub(crate) fn new() -> Self {
		Self { pending_replays: VecDeque::new(), stopped: false, stop_emitted: false }
	}

	fn record_filter_added(
		&mut self,
		filter_id: FilterId,
		filter: OptimizedTopicFilter,
		watermark: u64,
	) {
		self.pending_replays
			.push_back(PendingReplay { filter_id, filter, cursor: 0, watermark });
	}

	fn record_filter_removed(&mut self, filter_id: FilterId) {
		self.pending_replays.retain(|replay| replay.filter_id != filter_id);
	}

	fn next_event(
		&mut self,
		snapshot_provider: &dyn ReplaySnapshotProvider,
	) -> Option<MultiFilterSubscriptionEvent> {
		if !self.stopped {
			if let Some(event) = self.next_replay_event(snapshot_provider) {
				return Some(event);
			}
		}

		if self.stopped && !self.stop_emitted {
			self.stop_emitted = true;
			return Some(MultiFilterSubscriptionEvent::Stop);
		}
		None
	}

	fn next_replay_event(
		&mut self,
		snapshot_provider: &dyn ReplaySnapshotProvider,
	) -> Option<MultiFilterSubscriptionEvent> {
		let replay = self.pending_replays.front_mut()?;
		let filter_id = replay.filter_id;
		if replay.cursor >= replay.watermark {
			self.pending_replays.pop_front();
			return Some(MultiFilterSubscriptionEvent::ReplayDone { filter_id });
		}
		let batch =
			match snapshot_provider.replay_batch(&replay.filter, replay.cursor, replay.watermark) {
				Ok(batch) => batch,
				Err(e) => {
					log::error!(
						target: LOG_TARGET,
						"Stopping subscription: replaying filter {:?} from {} failed: {:?}",
						filter_id,
						replay.cursor,
						e
					);
					self.stopped = true;
					return None;
				},
			};
		replay.cursor = batch.cursor;
		if batch.done && batch.statements.is_empty() {
			self.pending_replays.pop_front();
			return Some(MultiFilterSubscriptionEvent::ReplayDone { filter_id });
		}
		Some(MultiFilterSubscriptionEvent::ReplayStatements {
			filter_id,
			statements: batch.statements,
		})
	}

	fn new_statement_event(
		&mut self,
		hash: sp_statement_store::Hash,
		encoded: Vec<u8>,
		filter_ids: &HashSet<FilterId>,
	) -> Option<MultiFilterSubscriptionEvent> {
		let matched_filter_ids: Vec<FilterId> = filter_ids.iter().copied().collect();

		if matched_filter_ids.is_empty() {
			return None;
		}

		Some(MultiFilterSubscriptionEvent::NewStatement(LiveStatementEvent {
			hash,
			encoded,
			matched_filter_ids,
		}))
	}
}

/// Event emitted by a multi-filter subscription
#[derive(Debug, Clone)]
pub enum MultiFilterSubscriptionEvent {
	/// Replay statements for a newly attached filter
	ReplayStatements {
		/// Filter that produced this replay batch
		filter_id: FilterId,
		/// SCALE-encoded statements included in this replay batch
		statements: Vec<Vec<u8>>,
	},
	/// Replay completed for a newly attached filter
	ReplayDone {
		/// Filter whose replay completed
		filter_id: FilterId,
	},
	/// Live statement event matched one or more active filters
	NewStatement(LiveStatementEvent),
	/// Subscription stopped: the subscriber fell behind its buffer, or replaying a filter failed
	Stop,
}

/// Stream of multi-filter subscription events
pub struct MultiFilterEventStream {
	sub_id: SeqID,
	matchers: SubscriptionsMatchersHandlers,
	rx: async_channel::Receiver<MultiFilterSubscriptionEvent>,
}

impl Stream for MultiFilterEventStream {
	type Item = MultiFilterSubscriptionEvent;

	fn poll_next(
		self: std::pin::Pin<&mut Self>,
		cx: &mut std::task::Context<'_>,
	) -> std::task::Poll<Option<Self::Item>> {
		self.get_mut().rx.poll_next_unpin(cx)
	}
}

impl Drop for MultiFilterEventStream {
	fn drop(&mut self) {
		self.matchers
			.send_by_seq_id(self.sub_id, MatcherMessage::Unsubscribe(self.sub_id));
	}
}

/// Messages sent to matcher tasks.
enum MatcherMessage {
	/// A new statement has been submitted, tagged with the store sequence number.
	NewStatement(u64, Statement),
	/// A new subscription has been created.
	Subscribe { info: IndexedSubscription, tx: async_channel::Sender<StatementEvent> },
	/// A new multi-filter subscription has been created
	SubscribeEmpty {
		seq_id: SeqID,
		snapshot_provider: Arc<dyn ReplaySnapshotProvider>,
		tx: async_channel::Sender<MultiFilterSubscriptionEvent>,
	},
	/// Register a filter at a store watermark. The matcher drains its cursor replay before
	/// processing later live-statement messages from this channel.
	RegisterFilter {
		sub_id: SeqID,
		filter_id: FilterId,
		filter: OptimizedTopicFilter,
		watermark: u64,
		result_tx: async_channel::Sender<std::result::Result<(), AddFilterError>>,
	},
	/// Remove a filter from an existing multi-filter subscription
	RemoveFilter { sub_id: SeqID, filter_id: FilterId },

	/// Unsubscribe the subscription with the given ID.
	Unsubscribe(SeqID),
}

// Handle to manage all subscriptions.
pub struct SubscriptionsHandle {
	// Sequence generator for subscription IDs, atomic for thread safety.
	// Subscription creation is expensive enough that we don't worry about overflow here.
	id_sequence: AtomicU64,
	//  Subscriptions matchers handlers.
	matchers: SubscriptionsMatchersHandlers,
}

impl SubscriptionsHandle {
	/// Create a new SubscriptionsHandle with the given task spawner and number of filter workers.
	pub(crate) fn new(
		task_spawner: Box<dyn SpawnNamed>,
		num_matcher_workers: usize,
	) -> SubscriptionsHandle {
		let mut subscriptions_matchers_senders = Vec::with_capacity(num_matcher_workers);

		for task in 0..num_matcher_workers {
			let (subscription_matcher_sender, subscription_matcher_receiver) =
				async_channel::bounded(MATCHERS_TASK_CHANNEL_BUFFER_SIZE);
			subscriptions_matchers_senders.push(subscription_matcher_sender);
			task_spawner.spawn_blocking(
				"statement-store-subscription-filters",
				Some("statement-store"),
				Box::pin(async move {
					let mut subscriptions = SubscriptionsInfo::new();
					log::debug!(
						target: LOG_TARGET,
						"Started statement subscription matcher task: {task}"
					);
					loop {
						let res = subscription_matcher_receiver.recv().await;
						match res {
							Ok(MatcherMessage::NewStatement(seq, statement)) => {
								subscriptions.notify_matching_filters(seq, &statement);
							},
							Ok(MatcherMessage::Subscribe { info, tx }) => {
								subscriptions.subscribe(info, tx);
							},
							Ok(MatcherMessage::SubscribeEmpty {
								seq_id,
								snapshot_provider,
								tx,
							}) => {
								subscriptions.subscribe_empty(seq_id, snapshot_provider, tx);
							},
							Ok(MatcherMessage::RegisterFilter {
								sub_id,
								filter_id,
								filter,
								watermark,
								result_tx,
							}) => {
								let result = subscriptions
									.register_filter(sub_id, filter_id, filter, watermark);
								let registered = result.is_ok();
								let _ = result_tx.try_send(result);
								if registered {
									subscriptions.drain_ready_events(sub_id);
								}
							},
							Ok(MatcherMessage::RemoveFilter { sub_id, filter_id }) => {
								subscriptions.remove_filter(sub_id, filter_id);
							},
							Ok(MatcherMessage::Unsubscribe(seq_id)) => {
								subscriptions.unsubscribe(seq_id);
							},
							Err(_) => {
								// Expected when the subscription manager is dropped at shutdown.
								log::debug!(
									target: LOG_TARGET,
									"Statement subscription matcher channel closed: {task}"
								);
								break;
							},
						};
					}
				}),
			);
		}
		SubscriptionsHandle {
			id_sequence: AtomicU64::new(0),
			matchers: SubscriptionsMatchersHandlers::new(subscriptions_matchers_senders),
		}
	}

	// Generate the next unique subscription ID.
	fn next_id(&self) -> SeqID {
		let id = self.id_sequence.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
		SeqID::from(id)
	}

	/// Subscribe to statements matching the topic filter.
	pub(crate) fn subscribe(
		&self,
		topic_filter: OptimizedTopicFilter,
		watermark: u64,
	) -> (async_channel::Sender<StatementEvent>, SubscriptionStatementsStream) {
		let next_id = self.next_id();
		let (tx, rx) = async_channel::bounded(SUBSCRIPTION_BUFFER_SIZE);
		let subscription_info = IndexedSubscription {
			topic_filter: topic_filter.clone(),
			seq_id: next_id,
			filter_key: SubscriptionFilterKey::Fixed,
			watermark,
		};
		let subscription_tx = tx.clone();

		let result = (
			tx,
			SubscriptionStatementsStream {
				rx,
				sub_id: subscription_info.seq_id,
				matchers: self.matchers.clone(),
			},
		);

		self.matchers.send_by_seq_id(
			subscription_info.seq_id,
			MatcherMessage::Subscribe { info: subscription_info, tx: subscription_tx },
		);
		result
	}

	pub(crate) fn subscribe_empty(
		&self,
		snapshot_provider: Arc<dyn ReplaySnapshotProvider>,
	) -> (SeqID, MultiFilterEventStream) {
		let sub_id = self.next_id();
		let (tx, rx) =
			async_channel::bounded(SUBSCRIPTION_BUFFER_SIZE + STOP_RESERVE_CHANNEL_SLOTS);
		self.matchers.send_by_seq_id(
			sub_id,
			MatcherMessage::SubscribeEmpty { seq_id: sub_id, snapshot_provider, tx },
		);

		let stream = MultiFilterEventStream { sub_id, matchers: self.matchers.clone(), rx };
		(sub_id, stream)
	}

	pub(crate) fn notify(&self, seq: u64, statement: Statement) {
		self.matchers.send_all(seq, statement);
	}

	pub(crate) fn matchers(&self) -> SubscriptionsMatchersHandlers {
		self.matchers.clone()
	}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
enum SubscriptionFilterKey {
	Fixed,
	Dynamic(FilterId),
}

enum SubscriptionRecord {
	SingleFilter {
		tx: async_channel::Sender<StatementEvent>,
		filter: OptimizedTopicFilter,
	},
	MultiFilter {
		filters: HashMap<FilterId, OptimizedTopicFilter>,
		state: MultiFilterSubscriptionState,
		tx: async_channel::Sender<MultiFilterSubscriptionEvent>,
		snapshot_provider: Arc<dyn ReplaySnapshotProvider>,
	},
}

enum MatchedSubscription {
	Statements,
	Live(HashSet<FilterId>),
}

enum ReadyEventDelivery {
	Continue,
	Stopped,
	Closed,
}

enum PostLiveSendAction {
	None,
	Unsubscribe,
}

type IndexedSubscriptionKey = (SeqID, SubscriptionFilterKey);

// Information about all subscriptions.
// Each matcher task will have its own instance of this struct.
struct SubscriptionsInfo {
	// Subscriptions organized by topic for MatchAll filters.
	//
	// Maps each topic to an array of HashMaps, where the array is indexed by
	// `(number_of_topics_in_filter - 1)`. For example, a subscription requiring
	// topics [A, B] (2 topics) will be stored at index 1 under both topic A and B.
	//
	// This structure allows efficient matching: when a statement arrives with N topics,
	// we only need to check subscriptions that require exactly N or fewer topics.
	subscriptions_match_all_by_topic:
		HashMap<Topic, [HashMap<IndexedSubscriptionKey, IndexedSubscription>; MAX_TOPICS]>,
	// Subscriptions organized by topic for MatchAny filters.
	subscriptions_match_any_by_topic:
		HashMap<Topic, HashMap<IndexedSubscriptionKey, IndexedSubscription>>,
	// Subscriptions that listen with Any filter (i.e., no topic filtering).
	subscriptions_any: HashMap<IndexedSubscriptionKey, IndexedSubscription>,
	// Mapping from subscription ID to subscription state.
	by_sub_id: HashMap<SeqID, SubscriptionRecord>,
}

// Information about one indexed subscription filter.
#[derive(Clone, Debug)]
struct IndexedSubscription {
	// The filter used for this subscription.
	topic_filter: OptimizedTopicFilter,
	// The unique ID of this subscription.
	seq_id: SeqID,
	// The filter key within the subscription.
	filter_key: SubscriptionFilterKey,
	// Store sequence-number boundary captured when this entry was registered: at subscription
	// creation for a fixed filter, at `add_filter` for a dynamic one.
	watermark: u64,
}

impl SubscriptionsInfo {
	fn new() -> SubscriptionsInfo {
		SubscriptionsInfo {
			subscriptions_match_all_by_topic: HashMap::new(),
			subscriptions_match_any_by_topic: HashMap::new(),
			subscriptions_any: HashMap::new(),
			by_sub_id: HashMap::new(),
		}
	}

	// Subscribe a new subscription.
	fn subscribe(
		&mut self,
		subscription_info: IndexedSubscription,
		tx: async_channel::Sender<StatementEvent>,
	) {
		self.by_sub_id.insert(
			subscription_info.seq_id,
			SubscriptionRecord::SingleFilter { tx, filter: subscription_info.topic_filter.clone() },
		);
		self.index_filter(subscription_info);
	}

	fn subscribe_empty(
		&mut self,
		seq_id: SeqID,
		snapshot_provider: Arc<dyn ReplaySnapshotProvider>,
		tx: async_channel::Sender<MultiFilterSubscriptionEvent>,
	) {
		self.by_sub_id.insert(
			seq_id,
			SubscriptionRecord::MultiFilter {
				filters: HashMap::new(),
				state: MultiFilterSubscriptionState::new(),
				tx,
				snapshot_provider,
			},
		);
	}

	#[cfg(test)]
	fn add_filter(
		&mut self,
		sub_id: SeqID,
		filter_id: FilterId,
		filter: OptimizedTopicFilter,
		snapshot_hashes: Vec<sp_statement_store::Hash>,
	) {
		self.add_filter_with_cursor(sub_id, filter_id, filter, snapshot_hashes.len() as u64);
		self.drain_ready_events(sub_id);
	}

	fn register_filter(
		&mut self,
		sub_id: SeqID,
		filter_id: FilterId,
		filter: OptimizedTopicFilter,
		watermark: u64,
	) -> std::result::Result<(), AddFilterError> {
		let Some(SubscriptionRecord::MultiFilter { filters, .. }) = self.by_sub_id.get(&sub_id)
		else {
			return Err(AddFilterError::Stopped);
		};
		if filters.contains_key(&filter_id) {
			return Err(AddFilterError::Stopped);
		}
		let output_closed = matches!(
			self.by_sub_id.get(&sub_id),
			Some(SubscriptionRecord::MultiFilter { tx, .. }) if tx.is_closed()
		);
		if output_closed {
			self.unsubscribe(sub_id);
			return Err(AddFilterError::Stopped);
		}
		self.add_filter_with_cursor(sub_id, filter_id, filter, watermark);
		Ok(())
	}

	fn add_filter_with_cursor(
		&mut self,
		sub_id: SeqID,
		filter_id: FilterId,
		filter: OptimizedTopicFilter,
		watermark: u64,
	) {
		let filter_key = SubscriptionFilterKey::Dynamic(filter_id);
		{
			let Some(SubscriptionRecord::MultiFilter { filters, state, .. }) =
				self.by_sub_id.get_mut(&sub_id)
			else {
				return;
			};

			let Entry::Vacant(entry) = filters.entry(filter_id) else {
				return;
			};
			entry.insert(filter.clone());
			state.record_filter_added(filter_id, filter.clone(), watermark);
		}
		self.index_filter(IndexedSubscription {
			topic_filter: filter,
			seq_id: sub_id,
			filter_key,
			watermark,
		});
	}

	fn remove_filter(&mut self, sub_id: SeqID, filter_id: FilterId) {
		let Some(record) = self.by_sub_id.get_mut(&sub_id) else {
			return;
		};
		let SubscriptionRecord::MultiFilter { filters, state, .. } = record else {
			return;
		};
		let Some(filter) = filters.remove(&filter_id) else {
			return;
		};
		state.record_filter_removed(filter_id);
		self.remove_indexed_filter(sub_id, SubscriptionFilterKey::Dynamic(filter_id), &filter);
		self.drain_ready_events(sub_id);
	}

	fn send_stop(tx: &async_channel::Sender<MultiFilterSubscriptionEvent>) {
		let _ = tx.try_send(MultiFilterSubscriptionEvent::Stop);
		tx.close();
	}

	fn send_ready_event(
		state: &mut MultiFilterSubscriptionState,
		tx: &async_channel::Sender<MultiFilterSubscriptionEvent>,
		event: MultiFilterSubscriptionEvent,
	) -> ReadyEventDelivery {
		if tx.is_closed() {
			return ReadyEventDelivery::Closed;
		}
		if matches!(event, MultiFilterSubscriptionEvent::Stop) {
			Self::send_stop(tx);
			return ReadyEventDelivery::Stopped;
		}
		if tx.len() >= SUBSCRIPTION_BUFFER_SIZE {
			state.stopped = true;
			Self::send_stop(tx);
			return ReadyEventDelivery::Stopped;
		}
		match tx.try_send(event) {
			Ok(()) => ReadyEventDelivery::Continue,
			Err(async_channel::TrySendError::Full(_)) => {
				state.stopped = true;
				Self::send_stop(tx);
				ReadyEventDelivery::Stopped
			},
			Err(async_channel::TrySendError::Closed(_)) => ReadyEventDelivery::Closed,
		}
	}

	fn drain_ready_events(&mut self, sub_id: SeqID) {
		loop {
			let result = {
				let Some(record) = self.by_sub_id.get_mut(&sub_id) else {
					return;
				};
				let SubscriptionRecord::MultiFilter { state, tx, snapshot_provider, .. } = record
				else {
					return;
				};
				let Some(event) = state.next_event(snapshot_provider.as_ref()) else {
					return;
				};
				match Self::send_ready_event(state, tx, event) {
					ReadyEventDelivery::Continue => Ok(()),
					ReadyEventDelivery::Stopped => return,
					ReadyEventDelivery::Closed => Err(()),
				}
			};
			if result.is_err() {
				self.unsubscribe(sub_id);
				return;
			}
		}
	}

	/// Emit a live statement matched for a multi-filter subscription.
	fn deliver_live_statement(
		&mut self,
		sub_id: SeqID,
		hash: sp_statement_store::Hash,
		encoded: Vec<u8>,
		matched_filter_ids: HashSet<FilterId>,
	) {
		let action = {
			let Some(SubscriptionRecord::MultiFilter { state, tx, .. }) =
				self.by_sub_id.get_mut(&sub_id)
			else {
				return;
			};

			match state.new_statement_event(hash, encoded, &matched_filter_ids) {
				Some(event) => match Self::send_ready_event(state, tx, event) {
					ReadyEventDelivery::Closed => PostLiveSendAction::Unsubscribe,
					ReadyEventDelivery::Continue | ReadyEventDelivery::Stopped => {
						PostLiveSendAction::None
					},
				},
				None => PostLiveSendAction::None,
			}
		};

		match action {
			PostLiveSendAction::Unsubscribe => self.unsubscribe(sub_id),
			PostLiveSendAction::None => {},
		}
	}

	fn index_filter(&mut self, subscription_info: IndexedSubscription) {
		let index_key = (subscription_info.seq_id, subscription_info.filter_key);
		match &subscription_info.topic_filter {
			OptimizedTopicFilter::Any => {
				self.subscriptions_any.insert(index_key, subscription_info);
			},
			OptimizedTopicFilter::MatchAll(topics) => {
				for topic in topics {
					self.subscriptions_match_all_by_topic.entry(*topic).or_default()
						[topics.len() - 1]
						.insert(index_key, subscription_info.clone());
				}
			},
			OptimizedTopicFilter::MatchAny(topics) => {
				for topic in topics {
					self.subscriptions_match_any_by_topic
						.entry(*topic)
						.or_default()
						.insert(index_key, subscription_info.clone());
				}
			},
		};
	}

	// Deliver the statement at store sequence number `seq` to every matching subscription.
	// Subscriptions and filters registered above `seq` are skipped: the statement predates them, so
	// it is covered by the subscribe-time snapshot
	// (`StatementStoreSubscriptionApi::subscribe_statement`) for a fixed filter, or by the
	// admission-cursor replay (`ReplaySnapshotProvider::replay_batch`) for a dynamic one.
	fn notify_matching_filters(&mut self, seq: u64, statement: &Statement) {
		let mut matches = HashMap::new();
		self.collect_match_all_subscribers(seq, statement, &mut matches);
		self.collect_match_any_subscribers(seq, statement, &mut matches);
		self.collect_any_subscribers(seq, &mut matches);

		let encoded = statement.encode();
		let bytes_to_send: Bytes = encoded.clone().into();
		let mut needs_unsubscribing = HashSet::new();

		for (sub_id, matched) in matches {
			match matched {
				MatchedSubscription::Statements => {
					let Some(SubscriptionRecord::SingleFilter { tx, .. }) =
						self.by_sub_id.get(&sub_id)
					else {
						continue;
					};
					if let Err(err) = tx.try_send(StatementEvent::NewStatements {
						statements: vec![bytes_to_send.clone()],
						remaining: None,
					}) {
						log::debug!(
							target: LOG_TARGET,
							"Failed to send statement to subscriber {:?}: {:?} unsubscribing it", sub_id, err
						);
						needs_unsubscribing.insert(sub_id);
					}
				},
				MatchedSubscription::Live(filter_ids) if !filter_ids.is_empty() => {
					self.deliver_live_statement(
						sub_id,
						statement.hash(),
						encoded.clone(),
						filter_ids,
					);
				},
				_ => {},
			}
		}

		for sub_id in needs_unsubscribing {
			self.unsubscribe(sub_id);
		}
	}

	// Record a subscription filter as matched, unless the statement's sequence number is below the
	// watermark captured when it was registered (exactly-once delivery: such statements are covered
	// by the subscribe-time snapshot or the admission-cursor replay instead).
	fn record_match(
		matches: &mut HashMap<SeqID, MatchedSubscription>,
		subscription: &IndexedSubscription,
		seq: u64,
	) {
		if seq < subscription.watermark {
			return;
		}
		match subscription.filter_key {
			SubscriptionFilterKey::Fixed => {
				matches.entry(subscription.seq_id).or_insert(MatchedSubscription::Statements);
			},
			SubscriptionFilterKey::Dynamic(filter_id) => {
				let entry = matches
					.entry(subscription.seq_id)
					.or_insert_with(|| MatchedSubscription::Live(HashSet::new()));
				if let MatchedSubscription::Live(filter_ids) = entry {
					filter_ids.insert(filter_id);
				}
			},
		}
	}

	// Collect all subscribers with MatchAny filters that match the given statement.
	fn collect_match_any_subscribers(
		&self,
		seq: u64,
		statement: &Statement,
		matches: &mut HashMap<SeqID, MatchedSubscription>,
	) {
		for statement_topic in statement.topics() {
			if let Some(subscriptions) = self.subscriptions_match_any_by_topic.get(statement_topic)
			{
				for subscription in subscriptions.values() {
					Self::record_match(matches, subscription, seq);
				}
			}
		}
	}

	// Collect all subscribers with MatchAll filters that match the given statement.
	fn collect_match_all_subscribers(
		&self,
		seq: u64,
		statement: &Statement,
		matches: &mut HashMap<SeqID, MatchedSubscription>,
	) {
		let num_topics = statement.topics().len();

		// Check all combinations of topics in the statement to find matching subscriptions.
		// This works well because the maximum allowed topics is small (MAX_TOPICS = 4).
		for num_topics_to_check in 1..=num_topics {
			for topics_combination in statement.topics().iter().combinations(num_topics_to_check) {
				// Find the topic with the fewest subscriptions to minimize the number of checks.
				let Some(Some(topic_with_fewest)) = topics_combination
					.iter()
					.map(|topic| self.subscriptions_match_all_by_topic.get(*topic))
					.min_by_key(|subscriptions| {
						subscriptions.map_or(0, |subscriptions_by_length| {
							subscriptions_by_length[num_topics_to_check - 1].len()
						})
					})
				else {
					continue;
				};

				for subscription in topic_with_fewest[num_topics_to_check - 1]
					.values()
					.filter(|subscription| subscription.topic_filter.matches(statement))
				{
					Self::record_match(matches, subscription, seq);
				}
			}
		}
	}

	// Collect all subscribers that don't filter by topic and want to receive all statements.
	fn collect_any_subscribers(&self, seq: u64, matches: &mut HashMap<SeqID, MatchedSubscription>) {
		for subscription in self.subscriptions_any.values() {
			Self::record_match(matches, subscription, seq);
		}
	}

	// Unsubscribe a subscription by its ID.
	fn unsubscribe(&mut self, id: SeqID) {
		let Some(entry) = self.by_sub_id.remove(&id) else {
			return;
		};

		match entry {
			SubscriptionRecord::SingleFilter { filter, .. } => {
				self.remove_indexed_filter(id, SubscriptionFilterKey::Fixed, &filter);
			},
			SubscriptionRecord::MultiFilter { filters, .. } => {
				for (filter_id, filter) in filters {
					self.remove_indexed_filter(
						id,
						SubscriptionFilterKey::Dynamic(filter_id),
						&filter,
					);
				}
			},
		}
	}

	fn remove_indexed_filter(
		&mut self,
		id: SeqID,
		filter_key: SubscriptionFilterKey,
		filter: &OptimizedTopicFilter,
	) {
		let topics = match filter {
			OptimizedTopicFilter::Any => {
				self.subscriptions_any.remove(&(id, filter_key));
				return;
			},
			OptimizedTopicFilter::MatchAll(topics) => topics,
			OptimizedTopicFilter::MatchAny(topics) => topics,
		};

		// Remove subscription from relevant maps.
		for topic in topics {
			// Check MatchAny map.
			if let Entry::Occupied(mut entry) = self.subscriptions_match_any_by_topic.entry(*topic)
			{
				entry.get_mut().remove(&(id, filter_key));
				if entry.get().is_empty() {
					entry.remove();
				}
			}
			// Check MatchAll map.
			if let Entry::Occupied(mut entry) = self.subscriptions_match_all_by_topic.entry(*topic)
			{
				for subscriptions in entry.get_mut().iter_mut() {
					if subscriptions.remove(&(id, filter_key)).is_some() {
						break;
					}
				}
				if entry.get().iter().all(|s| s.is_empty()) {
					entry.remove();
				}
			}
		}
	}
}

// Handlers to communicate with subscription matcher tasks.
#[derive(Clone)]
pub struct SubscriptionsMatchersHandlers {
	// Channels to send messages to matcher tasks.
	matchers: Vec<async_channel::Sender<MatcherMessage>>,
}

impl SubscriptionsMatchersHandlers {
	/// Create new SubscriptionsMatchersHandlers with the given matcher task senders.
	fn new(matchers: Vec<async_channel::Sender<MatcherMessage>>) -> SubscriptionsMatchersHandlers {
		SubscriptionsMatchersHandlers { matchers }
	}

	// Send a message to the matcher task responsible for the given subscription ID.
	fn send_by_seq_id(&self, id: SeqID, message: MatcherMessage) {
		// If matchers channels are full we backpressure the sender, in this case it will be the
		// processing of new statements.
		if let Err(err) = self.try_send_by_seq_id(id, message) {
			log::error!(
				target: LOG_TARGET,
				"Failed to send statement to matcher task: {:?}", err
			);
		}
	}

	fn try_send_by_seq_id(
		&self,
		id: SeqID,
		message: MatcherMessage,
	) -> std::result::Result<(), async_channel::TrySendError<MatcherMessage>> {
		self.sender_by_seq_id(id).try_send(message)
	}

	fn sender_by_seq_id(&self, id: SeqID) -> async_channel::Sender<MatcherMessage> {
		let index: u64 = id.into();
		self.matchers[index as usize % self.matchers.len()].clone()
	}

	// Send a new statement, tagged with its store sequence number, to all matcher tasks.
	fn send_all(&self, seq: u64, statement: Statement) {
		for sender in &self.matchers {
			if let Err(err) =
				sender.send_blocking(MatcherMessage::NewStatement(seq, statement.clone()))
			{
				log::error!(
					target: LOG_TARGET,
					"Failed to send message to matcher task: {:?}", err
				);
			}
		}
	}
}

// Stream of statements for a subscription.
pub struct SubscriptionStatementsStream {
	// Channel to receive statements.
	pub rx: async_channel::Receiver<StatementEvent>,
	// Subscription ID, used for cleanup on drop.
	sub_id: SeqID,
	// Reference to the matchers for cleanup.
	matchers: SubscriptionsMatchersHandlers,
}

// When the stream is dropped, unsubscribe from the matchers.
impl Drop for SubscriptionStatementsStream {
	fn drop(&mut self) {
		self.matchers
			.send_by_seq_id(self.sub_id, MatcherMessage::Unsubscribe(self.sub_id));
	}
}

impl Stream for SubscriptionStatementsStream {
	type Item = StatementEvent;

	fn poll_next(
		mut self: std::pin::Pin<&mut Self>,
		cx: &mut std::task::Context<'_>,
	) -> std::task::Poll<Option<Self::Item>> {
		self.rx.poll_next_unpin(cx)
	}
}

#[cfg(test)]
mod tests {

	use crate::tests::signed_statement;

	use super::*;
	use sp_core::Decode;
	use sp_statement_store::Topic;
	use std::{
		sync::{mpsc, Mutex as StdMutex},
		thread,
		time::{Duration, Instant},
	};

	fn unwrap_statement(item: StatementEvent) -> Bytes {
		match item {
			StatementEvent::NewStatements { mut statements, .. } => {
				assert_eq!(statements.len(), 1, "Expected exactly one statement in batch");
				statements.remove(0)
			},
		}
	}

	fn fixed_subscription(seq_id: u64, topic_filter: OptimizedTopicFilter) -> IndexedSubscription {
		IndexedSubscription {
			topic_filter,
			seq_id: SeqID::from(seq_id),
			filter_key: SubscriptionFilterKey::Fixed,
			watermark: 0,
		}
	}

	fn live_event_for(statement: &Statement, filter_ids: Vec<FilterId>) -> LiveStatementEvent {
		LiveStatementEvent {
			hash: statement.hash(),
			encoded: statement.encode(),
			matched_filter_ids: filter_ids,
		}
	}

	struct TestReplaySnapshotProvider {
		statements: HashMap<sp_statement_store::Hash, Vec<u8>>,
		snapshot_hashes: Vec<sp_statement_store::Hash>,
		batch_len: u64,
	}

	impl TestReplaySnapshotProvider {
		fn with_snapshot(snapshot: &[Statement], statements: &[Statement]) -> Self {
			Self {
				statements: statements
					.iter()
					.map(|statement| (statement.hash(), statement.encode()))
					.collect(),
				snapshot_hashes: snapshot.iter().map(Statement::hash).collect(),
				batch_len: snapshot.len() as u64,
			}
		}
	}

	impl ReplaySnapshotProvider for TestReplaySnapshotProvider {
		fn register_replay(&self, enqueue: &mut dyn FnMut(u64) -> bool) -> Result<Option<u64>> {
			let watermark = self.snapshot_hashes.len() as u64;
			Ok(enqueue(watermark).then_some(watermark))
		}

		fn replay_batch(
			&self,
			_filter: &OptimizedTopicFilter,
			cursor: u64,
			watermark: u64,
		) -> Result<ReplayBatch> {
			let end = watermark.min(self.snapshot_hashes.len() as u64).min(cursor + self.batch_len);
			let statements = self.snapshot_hashes[cursor as usize..end as usize]
				.iter()
				.filter_map(|hash| self.statements.get(hash).cloned())
				.collect();
			Ok(ReplayBatch { statements, cursor: end, done: end >= watermark })
		}
	}

	struct BlockingReplaySnapshotProvider {
		statements: HashMap<sp_statement_store::Hash, Vec<u8>>,
		snapshot_hashes: Vec<sp_statement_store::Hash>,
		scan_started: mpsc::SyncSender<()>,
		continue_scan: StdMutex<mpsc::Receiver<()>>,
	}

	impl ReplaySnapshotProvider for BlockingReplaySnapshotProvider {
		fn register_replay(&self, enqueue: &mut dyn FnMut(u64) -> bool) -> Result<Option<u64>> {
			let watermark = self.snapshot_hashes.len() as u64;
			Ok(enqueue(watermark).then_some(watermark))
		}

		fn replay_batch(
			&self,
			_filter: &OptimizedTopicFilter,
			cursor: u64,
			watermark: u64,
		) -> Result<ReplayBatch> {
			self.scan_started.send(()).expect("test receiver remains open");
			self.continue_scan
				.lock()
				.expect("test mutex is not poisoned")
				.recv()
				.expect("test sender remains open");
			let end = watermark.min(self.snapshot_hashes.len() as u64);
			let statements = self.snapshot_hashes[cursor as usize..end as usize]
				.iter()
				.filter_map(|hash| self.statements.get(hash).cloned())
				.collect();
			Ok(ReplayBatch { statements, cursor: end, done: end >= watermark })
		}
	}

	struct ReplayLoadBlockingProvider {
		body: Vec<u8>,
		load_started: mpsc::SyncSender<()>,
		continue_load: StdMutex<mpsc::Receiver<()>>,
	}

	impl ReplaySnapshotProvider for ReplayLoadBlockingProvider {
		fn register_replay(&self, enqueue: &mut dyn FnMut(u64) -> bool) -> Result<Option<u64>> {
			Ok(enqueue(1).then_some(1))
		}

		fn replay_batch(
			&self,
			_filter: &OptimizedTopicFilter,
			cursor: u64,
			watermark: u64,
		) -> Result<ReplayBatch> {
			assert_eq!(cursor, 0);
			assert_eq!(watermark, 1);
			self.load_started.send(()).expect("test receiver remains open");
			self.continue_load
				.lock()
				.expect("test mutex is not poisoned")
				.recv()
				.expect("test sender remains open");
			Ok(ReplayBatch { statements: vec![self.body.clone()], cursor: 1, done: true })
		}
	}

	fn recv_multi_filter_event(
		stream: &MultiFilterEventStream,
		timeout: Duration,
	) -> Option<MultiFilterSubscriptionEvent> {
		let deadline = Instant::now() + timeout;
		loop {
			match stream.rx.try_recv() {
				Ok(event) => return Some(event),
				Err(async_channel::TryRecvError::Closed) => return None,
				Err(async_channel::TryRecvError::Empty) if Instant::now() < deadline => {
					thread::sleep(Duration::from_millis(1));
				},
				Err(async_channel::TryRecvError::Empty) => return None,
			}
		}
	}

	#[test]
	fn multi_filter_replays_before_live_submissions_committed_during_snapshot() {
		let subscriptions =
			SubscriptionsHandle::new(Box::new(sp_core::testing::TaskExecutor::new()), 1);
		let filter_id = FilterId::new(0);
		let replayed = signed_statement(41);
		let delayed = signed_statement(40);
		let delayed_hash = delayed.hash();
		let live = signed_statement(42);
		let (scan_started_tx, scan_started_rx) = mpsc::sync_channel(1);
		let (continue_scan_tx, continue_scan_rx) = mpsc::sync_channel(1);
		let provider = Arc::new(BlockingReplaySnapshotProvider {
			statements: [(replayed.hash(), replayed.encode())].into_iter().collect(),
			snapshot_hashes: vec![replayed.hash()],
			scan_started: scan_started_tx,
			continue_scan: StdMutex::new(continue_scan_rx),
		});
		let (sub_id, stream) = subscriptions.subscribe_empty(provider.clone());
		let handle = SubscriptionHandle {
			sub_id,
			inner: Arc::new(Mutex::new(SubscriptionHandleInner::new())),
			matchers: subscriptions.matchers(),
			snapshot_provider: provider,
		};

		let add_filter = thread::spawn(move || handle.add_filter(OptimizedTopicFilter::Any));
		scan_started_rx.recv().expect("snapshot scan starts");

		// Both post-watermark live events wait behind replay.
		subscriptions.notify(0, replayed.clone());
		subscriptions.notify(1, delayed);
		subscriptions.notify(2, live.clone());
		continue_scan_tx.send(()).expect("snapshot scan is still waiting");
		assert_eq!(add_filter.join().expect("add-filter thread does not panic"), Ok(filter_id));

		assert!(matches!(
			recv_multi_filter_event(&stream, Duration::from_secs(1)),
			Some(MultiFilterSubscriptionEvent::ReplayStatements { statements, .. })
				if statements == vec![replayed.encode()]
		));
		assert!(matches!(
			recv_multi_filter_event(&stream, Duration::from_secs(1)),
			Some(MultiFilterSubscriptionEvent::ReplayDone { filter_id: done }) if done == filter_id
		));
		assert!(matches!(
			recv_multi_filter_event(&stream, Duration::from_secs(1)),
			Some(MultiFilterSubscriptionEvent::NewStatement(event))
				if event.hash == delayed_hash && event.matched_filter_ids == vec![filter_id]
		));
		assert!(matches!(
			recv_multi_filter_event(&stream, Duration::from_secs(1)),
			Some(MultiFilterSubscriptionEvent::NewStatement(event))
				if event.hash == live.hash() && event.matched_filter_ids == vec![filter_id]
		));
		assert!(recv_multi_filter_event(&stream, Duration::from_millis(10)).is_none());
	}

	#[test]
	fn add_filter_returns_before_matcher_drains_replay() {
		let subscriptions =
			SubscriptionsHandle::new(Box::new(sp_core::testing::TaskExecutor::new()), 1);
		let statement = signed_statement(43);
		let (load_started_tx, load_started_rx) = mpsc::sync_channel(1);
		let (continue_load_tx, continue_load_rx) = mpsc::sync_channel(1);
		let provider = Arc::new(ReplayLoadBlockingProvider {
			body: statement.encode(),
			load_started: load_started_tx,
			continue_load: StdMutex::new(continue_load_rx),
		});
		let (sub_id, _stream) = subscriptions.subscribe_empty(provider.clone());
		let handle = SubscriptionHandle {
			sub_id,
			inner: Arc::new(Mutex::new(SubscriptionHandleInner::new())),
			matchers: subscriptions.matchers(),
			snapshot_provider: provider,
		};
		let (result_tx, result_rx) = mpsc::sync_channel(1);
		let add_filter = thread::spawn(move || {
			result_tx
				.send(handle.add_filter(OptimizedTopicFilter::Any))
				.expect("test receiver remains open");
		});

		load_started_rx.recv().expect("matcher starts loading replay");
		let result = result_rx.recv_timeout(Duration::from_secs(1));
		continue_load_tx.send(()).expect("replay load is still waiting");
		add_filter.join().expect("add-filter thread does not panic");
		assert_eq!(result, Ok(Ok(FilterId::new(0))));
	}

	#[test]
	fn multi_filter_delivers_re_admitted_hash_skipped_during_replay() {
		let mut subscriptions = SubscriptionsInfo::new();
		let sub_id = SeqID::from(12);
		let filter_id = FilterId::new(1);
		let topic = Topic::from([9u8; 32]);
		let filter = OptimizedTopicFilter::MatchAny(vec![topic].into_iter().collect());
		let (tx, rx) = async_channel::bounded::<MultiFilterSubscriptionEvent>(10);
		let mut statement = signed_statement(42);
		statement.set_topic(0, topic);
		// The statement has an admission entry inside the replay range, but its body is no longer
		// retrievable, so the replay skips that sequence number.
		let provider = Arc::new(TestReplaySnapshotProvider::with_snapshot(
			std::slice::from_ref(&statement),
			&[],
		));

		subscriptions.subscribe_empty(sub_id, provider, tx);
		subscriptions.add_filter(sub_id, filter_id, filter, vec![statement.hash()]);

		// The body could not be loaded, so the replay finishes without emitting it.
		assert!(matches!(
			rx.try_recv(),
			Ok(MultiFilterSubscriptionEvent::ReplayDone { filter_id: done }) if done == filter_id
		));

		// The statement is re-admitted at a new sequence. It was never delivered through replay,
		// so it must reach the subscriber as a live event.
		subscriptions.notify_matching_filters(1, &statement);

		match rx.try_recv() {
			Ok(MultiFilterSubscriptionEvent::NewStatement(event)) => {
				assert_eq!(event.hash, statement.hash());
				assert_eq!(event.matched_filter_ids, vec![filter_id]);
			},
			other => {
				panic!("statement skipped during replay must be delivered live, got {other:?}")
			},
		}

		subscriptions.notify_matching_filters(2, &statement);
		assert!(matches!(
			rx.try_recv(),
			Ok(MultiFilterSubscriptionEvent::NewStatement(event))
				if event.hash == statement.hash() && event.matched_filter_ids == vec![filter_id]
		));
	}

	#[test]
	fn multi_filter_replay_resumes_from_the_returned_cursor() {
		let mut subscriptions = SubscriptionsInfo::new();
		let sub_id = SeqID::from(11);
		let filter_id = FilterId::new(1);
		let topic = Topic::from([9u8; 32]);
		let filter = OptimizedTopicFilter::MatchAny(vec![topic].into_iter().collect());
		let (tx, rx) = async_channel::bounded::<MultiFilterSubscriptionEvent>(10);
		let replayed: Vec<Statement> = (0..3u8)
			.map(|seed| {
				let mut statement = signed_statement(seed);
				statement.set_topic(0, topic);
				statement
			})
			.collect();
		let mut provider = TestReplaySnapshotProvider::with_snapshot(&replayed, &replayed);
		provider.batch_len = 1;

		subscriptions.subscribe_empty(sub_id, Arc::new(provider), tx);
		subscriptions.add_filter(
			sub_id,
			filter_id,
			filter,
			replayed.iter().map(Statement::hash).collect(),
		);

		let mut delivered = Vec::new();
		let mut replay_done = 0;
		while let Ok(event) = rx.try_recv() {
			match event {
				MultiFilterSubscriptionEvent::ReplayStatements { filter_id: id, statements } => {
					assert_eq!(id, filter_id);
					delivered.extend(statements);
				},
				MultiFilterSubscriptionEvent::ReplayDone { filter_id: id } => {
					assert_eq!(id, filter_id);
					replay_done += 1;
				},
				other => panic!("expected replay events, got {other:?}"),
			}
		}
		assert_eq!(delivered, replayed.iter().map(Statement::encode).collect::<Vec<_>>());
		assert_eq!(replay_done, 1);
	}

	#[test]
	fn multi_filter_pushes_ready_live_events_without_request_next() {
		let mut subscriptions = SubscriptionsInfo::new();
		let sub_id = SeqID::from(7);
		let filter_id = FilterId::new(1);
		let topic = Topic::from([9u8; 32]);
		let filter = OptimizedTopicFilter::MatchAny(vec![topic].into_iter().collect());
		let (tx, rx) = async_channel::bounded::<MultiFilterSubscriptionEvent>(10);
		let mut statement = signed_statement(42);
		statement.set_topic(0, topic);
		let provider = Arc::new(TestReplaySnapshotProvider::with_snapshot(
			&[],
			std::slice::from_ref(&statement),
		));

		subscriptions.subscribe_empty(sub_id, provider, tx);
		subscriptions.add_filter(sub_id, filter_id, filter, vec![]);
		assert!(matches!(
			rx.try_recv(),
			Ok(MultiFilterSubscriptionEvent::ReplayDone { filter_id: done_filter })
				if done_filter == filter_id
		));

		subscriptions.notify_matching_filters(0, &statement);

		match rx.try_recv() {
			Ok(MultiFilterSubscriptionEvent::NewStatement(event)) => {
				assert_eq!(event.hash, statement.hash());
				assert_eq!(event.matched_filter_ids, vec![filter_id]);
			},
			other => panic!("expected pushed live statement, got {other:?}"),
		}
	}

	#[test]
	fn multi_filter_reserves_output_slot_for_stop() {
		let mut subscriptions = SubscriptionsInfo::new();
		let sub_id = SeqID::from(8);
		let filter_id = FilterId::new(1);
		let topic = Topic::from([9u8; 32]);
		let filter = OptimizedTopicFilter::MatchAny(vec![topic].into_iter().collect());
		let mut statements =
			Vec::with_capacity(SUBSCRIPTION_BUFFER_SIZE + STOP_RESERVE_CHANNEL_SLOTS);
		for seed in 0..=SUBSCRIPTION_BUFFER_SIZE as u64 {
			let mut statement = signed_statement(seed as u8);
			statement.set_topic(0, topic);
			statements.push(statement);
		}
		let provider = Arc::new(TestReplaySnapshotProvider::with_snapshot(&[], &statements));
		let (tx, rx) = async_channel::bounded::<MultiFilterSubscriptionEvent>(
			SUBSCRIPTION_BUFFER_SIZE + STOP_RESERVE_CHANNEL_SLOTS,
		);

		subscriptions.subscribe_empty(sub_id, provider, tx);
		subscriptions.add_filter(sub_id, filter_id, filter, vec![]);
		assert!(matches!(
			rx.try_recv(),
			Ok(MultiFilterSubscriptionEvent::ReplayDone { filter_id: done_filter })
				if done_filter == filter_id
		));

		for statement in statements.iter().take(SUBSCRIPTION_BUFFER_SIZE) {
			subscriptions.notify_matching_filters(0, statement);
		}
		assert_eq!(rx.len(), SUBSCRIPTION_BUFFER_SIZE);

		subscriptions.notify_matching_filters(0, &statements[SUBSCRIPTION_BUFFER_SIZE]);
		assert_eq!(rx.len(), SUBSCRIPTION_BUFFER_SIZE + STOP_RESERVE_CHANNEL_SLOTS);

		for _ in 0..SUBSCRIPTION_BUFFER_SIZE {
			assert!(matches!(rx.try_recv(), Ok(MultiFilterSubscriptionEvent::NewStatement(_))));
		}
		assert!(matches!(rx.try_recv(), Ok(MultiFilterSubscriptionEvent::Stop)));
		assert!(rx.is_closed());
	}

	#[test]
	fn multi_filter_closed_channel_is_reported_as_closed_before_buffer_limit() {
		let mut state = MultiFilterSubscriptionState::new();
		let (tx, rx) = async_channel::bounded::<MultiFilterSubscriptionEvent>(
			SUBSCRIPTION_BUFFER_SIZE + STOP_RESERVE_CHANNEL_SLOTS,
		);
		for seed in 0..SUBSCRIPTION_BUFFER_SIZE {
			tx.try_send(MultiFilterSubscriptionEvent::NewStatement(live_event_for(
				&signed_statement(seed as u8),
				vec![FilterId::new(1)],
			)))
			.expect("channel capacity exceeds subscription buffer size; qed");
		}
		drop(rx);

		assert!(matches!(
			SubscriptionsInfo::send_ready_event(
				&mut state,
				&tx,
				MultiFilterSubscriptionEvent::NewStatement(live_event_for(
					&signed_statement(42),
					vec![FilterId::new(1)],
				)),
			),
			ReadyEventDelivery::Closed
		));
		assert!(matches!(
			SubscriptionsInfo::send_ready_event(
				&mut state,
				&tx,
				MultiFilterSubscriptionEvent::Stop,
			),
			ReadyEventDelivery::Closed
		));
		assert!(!state.stopped);
	}

	#[test]
	fn test_subscribe_unsubscribe() {
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx1, _rx1) = async_channel::bounded::<StatementEvent>(10);
		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);
		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAll(vec![topic1, topic2].into_iter().collect()),
		);
		subscriptions.subscribe(sub_info1.clone(), tx1);
		assert!(subscriptions.subscriptions_match_all_by_topic.contains_key(&topic1));
		assert!(subscriptions.subscriptions_match_all_by_topic.contains_key(&topic2));
		assert!(subscriptions.by_sub_id.contains_key(&sub_info1.seq_id));
		assert!(!subscriptions
			.subscriptions_any
			.contains_key(&(sub_info1.seq_id, sub_info1.filter_key)));

		subscriptions.unsubscribe(sub_info1.seq_id);
		assert!(!subscriptions.subscriptions_match_all_by_topic.contains_key(&topic1));
		assert!(!subscriptions.subscriptions_match_all_by_topic.contains_key(&topic2));
	}

	#[test]
	fn test_subscribe_any() {
		let mut subscriptions = SubscriptionsInfo::new();
		let (tx1, _rx1) = async_channel::bounded::<StatementEvent>(10);
		let sub_info1 = fixed_subscription(1, OptimizedTopicFilter::Any);
		subscriptions.subscribe(sub_info1.clone(), tx1);
		assert!(subscriptions
			.subscriptions_any
			.contains_key(&(sub_info1.seq_id, sub_info1.filter_key)));
		assert!(subscriptions.by_sub_id.contains_key(&sub_info1.seq_id));
		subscriptions.unsubscribe(sub_info1.seq_id);
		assert!(!subscriptions
			.subscriptions_any
			.contains_key(&(sub_info1.seq_id, sub_info1.filter_key)));
	}

	#[test]
	fn test_subscribe_match_any() {
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx1, _rx1) = async_channel::bounded::<StatementEvent>(10);
		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);
		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAny(vec![topic1, topic2].into_iter().collect()),
		);
		subscriptions.subscribe(sub_info1.clone(), tx1);
		assert!(subscriptions.subscriptions_match_any_by_topic.contains_key(&topic1));
		assert!(subscriptions.subscriptions_match_any_by_topic.contains_key(&topic2));
		assert!(subscriptions.by_sub_id.contains_key(&sub_info1.seq_id));
		assert!(!subscriptions
			.subscriptions_any
			.contains_key(&(sub_info1.seq_id, sub_info1.filter_key)));

		subscriptions.unsubscribe(sub_info1.seq_id);
		assert!(!subscriptions.subscriptions_match_all_by_topic.contains_key(&topic1));
		assert!(!subscriptions.subscriptions_match_all_by_topic.contains_key(&topic2));
	}

	#[test]
	fn test_notify_matching_filters_any() {
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx1, rx1) = async_channel::bounded::<StatementEvent>(10);
		let sub_info1 = fixed_subscription(1, OptimizedTopicFilter::Any);
		subscriptions.subscribe(sub_info1.clone(), tx1);

		let statement = signed_statement(1);
		subscriptions.notify_matching_filters(0, &statement);

		let received = unwrap_statement(rx1.try_recv().expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement);
	}

	#[test]
	fn test_notify_matching_filters_match_all() {
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx1, rx1) = async_channel::bounded::<StatementEvent>(10);
		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);
		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAll(vec![topic1, topic2].into_iter().collect()),
		);
		subscriptions.subscribe(sub_info1.clone(), tx1);

		let mut statement = signed_statement(1);
		statement.set_topic(0, topic2);
		subscriptions.notify_matching_filters(0, &statement);

		// Should not receive yet, only one topic matched.
		assert!(rx1.try_recv().is_err());

		statement.set_topic(1, topic1);
		subscriptions.notify_matching_filters(1, &statement);

		let received = unwrap_statement(rx1.try_recv().expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement);
	}

	#[test]
	fn test_notify_matching_filters_match_any() {
		let mut subscriptions = SubscriptionsInfo::new();
		let (tx1, rx1) = async_channel::bounded::<StatementEvent>(10);
		let (tx2, rx2) = async_channel::bounded::<StatementEvent>(10);

		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);
		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAny(vec![topic1, topic2].into_iter().collect()),
		);

		let sub_info2 = fixed_subscription(
			2,
			OptimizedTopicFilter::MatchAny(vec![topic2].into_iter().collect()),
		);

		subscriptions.subscribe(sub_info1.clone(), tx1);
		subscriptions.subscribe(sub_info2.clone(), tx2);

		let mut statement = signed_statement(1);
		statement.set_topic(0, topic1);
		statement.set_topic(1, topic2);
		subscriptions.notify_matching_filters(0, &statement);

		let received = unwrap_statement(rx1.try_recv().expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement);

		let received = unwrap_statement(rx2.try_recv().expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement);
	}

	#[tokio::test]
	async fn test_subscription_handle_with_different_workers_number() {
		for num_workers in 1..5 {
			let subscriptions_handle = SubscriptionsHandle::new(
				Box::new(sp_core::testing::TaskExecutor::new()),
				num_workers,
			);

			let topic1 = Topic::from([8u8; 32]);
			let topic2 = Topic::from([9u8; 32]);

			let streams = (0..5)
				.into_iter()
				.map(|_| {
					subscriptions_handle.subscribe(
						OptimizedTopicFilter::MatchAll(vec![topic1, topic2].into_iter().collect()),
						0,
					)
				})
				.collect::<Vec<_>>();

			let mut statement = signed_statement(1);
			statement.set_topic(0, topic2);
			subscriptions_handle.notify(0, statement.clone());

			statement.set_topic(1, topic1);
			subscriptions_handle.notify(1, statement.clone());

			for (_tx, mut stream) in streams {
				let received =
					unwrap_statement(stream.next().await.expect("Should receive statement"));
				let decoded_statement: Statement =
					Statement::decode(&mut &received.0[..]).expect("Should decode statement");
				assert_eq!(decoded_statement, statement);
			}
		}
	}

	#[tokio::test]
	async fn test_handle_unsubscribe() {
		let subscriptions_handle =
			SubscriptionsHandle::new(Box::new(sp_core::testing::TaskExecutor::new()), 2);

		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);

		let (tx, mut stream) = subscriptions_handle.subscribe(
			OptimizedTopicFilter::MatchAll(vec![topic1, topic2].into_iter().collect()),
			0,
		);

		let mut statement = signed_statement(1);
		statement.set_topic(0, topic1);
		statement.set_topic(1, topic2);

		// Send a statement and verify it's received.
		subscriptions_handle.notify(0, statement.clone());

		let received = unwrap_statement(stream.next().await.expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement);

		// Drop the stream to trigger unsubscribe.
		drop(stream);

		// Give some time for the unsubscribe message to be processed.
		tokio::time::sleep(std::time::Duration::from_millis(1000)).await;

		// Send another statement after unsubscribe.
		let mut statement2 = signed_statement(2);
		statement2.set_topic(0, topic1);
		statement2.set_topic(1, topic2);
		subscriptions_handle.notify(1, statement2.clone());

		// The tx channel should be closed/disconnected since the subscription was removed.
		// Give some time for the notification to potentially arrive (it shouldn't).
		tokio::time::sleep(std::time::Duration::from_millis(1000)).await;

		// The sender should fail to send since the subscription is gone.
		// We verify by checking that the tx channel is disconnected.
		assert!(tx.is_closed(), "Sender should be closed after unsubscribe");
	}

	#[test]
	fn test_unsubscribe_nonexistent() {
		let mut subscriptions = SubscriptionsInfo::new();
		// Unsubscribing a non-existent subscription should not panic.
		subscriptions.unsubscribe(SeqID::from(999));
		// Verify internal state is still valid.
		assert!(subscriptions.by_sub_id.is_empty());
		assert!(subscriptions.subscriptions_any.is_empty());
		assert!(subscriptions.subscriptions_match_all_by_topic.is_empty());
		assert!(subscriptions.subscriptions_match_any_by_topic.is_empty());
	}

	#[test]
	fn test_multiple_subscriptions_same_topic() {
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx1, rx1) = async_channel::bounded::<StatementEvent>(10);
		let (tx2, rx2) = async_channel::bounded::<StatementEvent>(10);
		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);

		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAll(vec![topic1, topic2].into_iter().collect()),
		);
		let sub_info2 = fixed_subscription(
			2,
			OptimizedTopicFilter::MatchAll(vec![topic1, topic2].into_iter().collect()),
		);

		subscriptions.subscribe(sub_info1.clone(), tx1);
		subscriptions.subscribe(sub_info2.clone(), tx2);

		// Both subscriptions should be registered under each topic.
		assert_eq!(
			subscriptions
				.subscriptions_match_all_by_topic
				.get(&topic1)
				.unwrap()
				.iter()
				.map(|s| s.len())
				.sum::<usize>(),
			2
		);
		assert_eq!(
			subscriptions
				.subscriptions_match_all_by_topic
				.get(&topic2)
				.unwrap()
				.iter()
				.map(|s| s.len())
				.sum::<usize>(),
			2
		);

		// Send a matching statement.
		let mut statement = signed_statement(1);
		statement.set_topic(0, topic1);
		statement.set_topic(1, topic2);
		subscriptions.notify_matching_filters(0, &statement);

		// Both should receive.
		assert!(rx1.try_recv().is_ok());
		assert!(rx2.try_recv().is_ok());

		// Unsubscribe one.
		subscriptions.unsubscribe(sub_info1.seq_id);

		// Only one subscription should remain.
		assert_eq!(
			subscriptions
				.subscriptions_match_all_by_topic
				.get(&topic1)
				.unwrap()
				.iter()
				.map(|s| s.len())
				.sum::<usize>(),
			1
		);
		assert_eq!(
			subscriptions
				.subscriptions_match_all_by_topic
				.get(&topic2)
				.unwrap()
				.iter()
				.map(|s| s.len())
				.sum::<usize>(),
			1
		);
		assert!(!subscriptions.by_sub_id.contains_key(&sub_info1.seq_id));
		assert!(subscriptions.by_sub_id.contains_key(&sub_info2.seq_id));

		// Send another statement.
		subscriptions.notify_matching_filters(1, &statement);

		// Only sub2 should receive.
		assert!(rx2.try_recv().is_ok());
		assert!(rx1.try_recv().is_err());
	}

	#[test]
	fn test_subscriber_auto_unsubscribe_on_channel_full() {
		let mut subscriptions = SubscriptionsInfo::new();

		// Create a channel with capacity 1.
		let (tx1, rx1) = async_channel::bounded::<StatementEvent>(1);
		let topic1 = Topic::from([8u8; 32]);

		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAny(vec![topic1].into_iter().collect()),
		);
		subscriptions.subscribe(sub_info1.clone(), tx1);

		let mut statement = signed_statement(1);
		statement.set_topic(0, topic1);

		// First notification should succeed.
		subscriptions.notify_matching_filters(0, &statement);
		assert!(rx1.try_recv().is_ok());

		// Fill the channel.
		subscriptions.notify_matching_filters(1, &statement);
		// Channel is now full.

		// Next notification should trigger auto-unsubscribe.
		subscriptions.notify_matching_filters(2, &statement);

		// Subscription should be removed.
		assert!(!subscriptions.by_sub_id.contains_key(&sub_info1.seq_id));
		assert!(!subscriptions.subscriptions_match_any_by_topic.contains_key(&topic1));
	}

	#[test]
	fn test_match_any_receives_once_per_statement() {
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx1, rx1) = async_channel::bounded::<StatementEvent>(10);
		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);

		// Subscribe to MatchAny with both topics.
		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAny(vec![topic1, topic2].into_iter().collect()),
		);
		subscriptions.subscribe(sub_info1.clone(), tx1);

		// Create a statement that matches BOTH topics.
		let mut statement = signed_statement(1);
		statement.set_topic(0, topic1);
		statement.set_topic(1, topic2);

		subscriptions.notify_matching_filters(0, &statement);

		// Should receive exactly once, not twice.
		let received = unwrap_statement(rx1.try_recv().expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement);

		// No more messages.
		assert!(rx1.try_recv().is_err());
	}

	#[test]
	fn test_match_all_receives_once_per_statement() {
		// A `MatchAll` subscriber must receive each matching statement exactly once, even when it
		// is registered under several of the statement's topics and the matcher therefore
		// encounters it across multiple topic combinations.
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx1, rx1) = async_channel::bounded::<StatementEvent>(10);
		let (tx2, _rx2) = async_channel::bounded::<StatementEvent>(10);
		let (tx3, _rx3) = async_channel::bounded::<StatementEvent>(10);

		let topic1 = Topic::from([1u8; 32]);
		let topic2 = Topic::from([2u8; 32]);
		let topic3 = Topic::from([3u8; 32]);
		let topic4 = Topic::from([4u8; 32]);

		// The subscription under test: MatchAll on topic1 AND topic2 (stored under both topics).
		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAll(vec![topic1, topic2].into_iter().collect()),
		);
		subscriptions.subscribe(sub_info1, tx1);

		// Extra MatchAll subscriptions on topic3 so the matcher encounters sub_info1 across
		// several topic combinations of the statement below. They do not match the statement
		// themselves (topic4 is absent).
		let sub_info2 = fixed_subscription(
			2,
			OptimizedTopicFilter::MatchAll(vec![topic3, topic4].into_iter().collect()),
		);
		let sub_info3 = fixed_subscription(
			3,
			OptimizedTopicFilter::MatchAll(vec![topic3, topic4].into_iter().collect()),
		);
		subscriptions.subscribe(sub_info2, tx2);
		subscriptions.subscribe(sub_info3, tx3);

		// Statement carrying topic1, topic2 and topic3.
		let mut statement = signed_statement(1);
		statement.set_topic(0, topic1);
		statement.set_topic(1, topic2);
		statement.set_topic(2, topic3);

		subscriptions.notify_matching_filters(0, &statement);

		// sub_info1 must receive the statement exactly once.
		let received = unwrap_statement(rx1.try_recv().expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement);

		assert!(
			rx1.try_recv().is_err(),
			"MatchAll subscriber must receive each statement only once"
		);
	}

	#[test]
	fn test_match_all_with_single_topic_matches_statement_with_two_topics() {
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx1, rx1) = async_channel::bounded::<StatementEvent>(10);
		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);

		// Subscribe with MatchAll on only topic1.
		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAll(vec![topic1].into_iter().collect()),
		);
		subscriptions.subscribe(sub_info1.clone(), tx1);

		// Create a statement that has BOTH topic1 and topic2.
		let mut statement = signed_statement(1);
		statement.set_topic(0, topic1);
		statement.set_topic(1, topic2);

		subscriptions.notify_matching_filters(0, &statement);

		// Should receive because the statement contains topic1 (which is the only required topic).
		let received = unwrap_statement(rx1.try_recv().expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement);

		// No more messages.
		assert!(rx1.try_recv().is_err());
	}

	#[test]
	fn test_match_all_no_matching_topics() {
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx1, rx1) = async_channel::bounded::<StatementEvent>(10);
		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);
		let topic3 = Topic::from([10u8; 32]);

		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAll(vec![topic1, topic2].into_iter().collect()),
		);
		subscriptions.subscribe(sub_info1.clone(), tx1);

		// Statement with completely different topics.
		let mut statement = signed_statement(1);
		statement.set_topic(0, topic3);

		subscriptions.notify_matching_filters(0, &statement);

		// Should not receive anything.
		assert!(rx1.try_recv().is_err());
	}

	#[test]
	fn test_match_all_with_unsubscribed_topic_first_in_statement() {
		// This test guards against returning early when one statement topic has no subscriptions.
		// The matcher must still check later topic combinations that can match.
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx1, rx1) = async_channel::bounded::<StatementEvent>(10);
		// topic1 will have NO subscriptions
		let topic1 = Topic::from([1u8; 32]);
		// topic2 WILL have a subscription
		let topic2 = Topic::from([2u8; 32]);

		// Subscribe only to topic2 with MatchAll filter.
		let sub_info1 = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAll(vec![topic2].into_iter().collect()),
		);
		subscriptions.subscribe(sub_info1, tx1);

		// Create a statement with BOTH topics. topic1 comes first (lower bytes).
		// When iterating combinations(1), [topic1] is checked before [topic2].
		// Since topic1 has no subscriptions, the buggy `return` exits early,
		// preventing the [topic2] combination from being checked.
		let mut statement = signed_statement(1);
		statement.set_topic(0, topic1);
		statement.set_topic(1, topic2);

		subscriptions.notify_matching_filters(0, &statement);

		// The receive succeeds only if the matcher checks the [topic2] combination.
		let received = unwrap_statement(
			rx1.try_recv()
				.expect("Should receive statement from a later matching topic combination"),
		);
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement);
	}

	#[tokio::test]
	async fn test_handle_with_match_any_filter() {
		let subscriptions_handle =
			SubscriptionsHandle::new(Box::new(sp_core::testing::TaskExecutor::new()), 2);

		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);

		let (_tx, mut stream) = subscriptions_handle.subscribe(
			OptimizedTopicFilter::MatchAny(vec![topic1, topic2].into_iter().collect()),
			0,
		);

		// Statement matching only topic1.
		let mut statement1 = signed_statement(1);
		statement1.set_topic(0, topic1);
		subscriptions_handle.notify(0, statement1.clone());

		let received = unwrap_statement(stream.next().await.expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement1);

		// Statement matching only topic2.
		let mut statement2 = signed_statement(2);
		statement2.set_topic(0, topic2);
		subscriptions_handle.notify(1, statement2.clone());

		let received = unwrap_statement(stream.next().await.expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement2);
	}

	#[tokio::test]
	async fn test_handle_with_any_filter() {
		let subscriptions_handle =
			SubscriptionsHandle::new(Box::new(sp_core::testing::TaskExecutor::new()), 2);

		let (_tx, mut stream) = subscriptions_handle.subscribe(OptimizedTopicFilter::Any, 0);

		// Send statements with various topics.
		let statement1 = signed_statement(1);
		subscriptions_handle.notify(0, statement1.clone());

		let received = unwrap_statement(stream.next().await.expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement1);

		let mut statement2 = signed_statement(2);
		statement2.set_topic(0, Topic::from([99u8; 32]));
		subscriptions_handle.notify(1, statement2.clone());

		let received = unwrap_statement(stream.next().await.expect("Should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement2);
	}

	#[tokio::test]
	async fn test_handle_multiple_subscribers_different_filters() {
		let subscriptions_handle =
			SubscriptionsHandle::new(Box::new(sp_core::testing::TaskExecutor::new()), 2);

		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);

		// Subscriber 1: MatchAll on topic1 and topic2.
		let (_tx1, mut stream1) = subscriptions_handle.subscribe(
			OptimizedTopicFilter::MatchAll(vec![topic1, topic2].into_iter().collect()),
			0,
		);

		// Subscriber 2: MatchAny on topic1.
		let (_tx2, mut stream2) = subscriptions_handle
			.subscribe(OptimizedTopicFilter::MatchAny(vec![topic1].into_iter().collect()), 0);

		// Subscriber 3: Any.
		let (_tx3, mut stream3) = subscriptions_handle.subscribe(OptimizedTopicFilter::Any, 0);

		// Statement matching only topic1.
		let mut statement1 = signed_statement(1);
		statement1.set_topic(0, topic1);
		subscriptions_handle.notify(0, statement1.clone());

		// stream1 should NOT receive (needs both topics).
		// stream2 should receive (MatchAny topic1).
		// stream3 should receive (Any).

		let received2 = unwrap_statement(stream2.next().await.expect("stream2 should receive"));
		let decoded2: Statement = Statement::decode(&mut &received2.0[..]).unwrap();
		assert_eq!(decoded2, statement1);

		let received3 = unwrap_statement(stream3.next().await.expect("stream3 should receive"));
		let decoded3: Statement = Statement::decode(&mut &received3.0[..]).unwrap();
		assert_eq!(decoded3, statement1);

		// Statement matching both topics.
		let mut statement2 = signed_statement(2);
		statement2.set_topic(0, topic1);
		statement2.set_topic(1, topic2);
		subscriptions_handle.notify(1, statement2.clone());

		// All should receive.
		let received1 = unwrap_statement(stream1.next().await.expect("stream1 should receive"));
		let decoded1: Statement = Statement::decode(&mut &received1.0[..]).unwrap();
		assert_eq!(decoded1, statement2);

		let received2 = unwrap_statement(stream2.next().await.expect("stream2 should receive"));
		let decoded2: Statement = Statement::decode(&mut &received2.0[..]).unwrap();
		assert_eq!(decoded2, statement2);

		let received3 = unwrap_statement(stream3.next().await.expect("stream3 should receive"));
		let decoded3: Statement = Statement::decode(&mut &received3.0[..]).unwrap();
		assert_eq!(decoded3, statement2);
	}

	#[test]
	fn test_statement_without_topics_matches_only_any_filter() {
		let mut subscriptions = SubscriptionsInfo::new();

		let (tx_match_all, rx_match_all) = async_channel::bounded::<StatementEvent>(10);
		let (tx_match_any, rx_match_any) = async_channel::bounded::<StatementEvent>(10);
		let (tx_any, rx_any) = async_channel::bounded::<StatementEvent>(10);

		let topic1 = Topic::from([8u8; 32]);
		let topic2 = Topic::from([9u8; 32]);

		// Subscribe with MatchAll filter.
		let sub_match_all = fixed_subscription(
			1,
			OptimizedTopicFilter::MatchAll(vec![topic1, topic2].into_iter().collect()),
		);
		subscriptions.subscribe(sub_match_all, tx_match_all);

		// Subscribe with MatchAny filter.
		let sub_match_any = fixed_subscription(
			2,
			OptimizedTopicFilter::MatchAny(vec![topic1, topic2].into_iter().collect()),
		);
		subscriptions.subscribe(sub_match_any, tx_match_any);

		// Subscribe with Any filter.
		let sub_any = fixed_subscription(3, OptimizedTopicFilter::Any);
		subscriptions.subscribe(sub_any, tx_any);

		// Create a statement without any topics set.
		let statement = signed_statement(1);
		assert!(statement.topics().is_empty(), "Statement should have no topics");

		// Notify all matching filters.
		subscriptions.notify_matching_filters(0, &statement);

		// Any should receive (matches all statements regardless of topics).
		let received =
			unwrap_statement(rx_any.try_recv().expect("Any filter should receive statement"));
		let decoded_statement: Statement =
			Statement::decode(&mut &received.0[..]).expect("Should decode statement");
		assert_eq!(decoded_statement, statement);

		// MatchAll should NOT receive (statement has no topics, filter requires topic1 AND topic2).
		assert!(
			rx_match_all.try_recv().is_err(),
			"MatchAll should not receive statement without topics"
		);

		// MatchAny should NOT receive (statement has no topics, filter requires topic1 OR topic2).
		assert!(
			rx_match_any.try_recv().is_err(),
			"MatchAny should not receive statement without topics"
		);
	}
}