sof 0.17.1

Solana Observer Framework for low-latency shred ingestion and plugin-driven transaction observation
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
//! Processed provider-stream ingress surfaces for SOF.
//!
//! Use this module when the upstream source is already beyond raw shreds, for
//! example Yellowstone gRPC or LaserStream-style processed transaction feeds.
//! These updates bypass SOF's packet, shred, FEC, and reconstruction stages and
//! enter directly at the plugin/derived-state transaction layer.
//!
//! Built-in mode capability summary:
//!
//! - `YellowstoneGrpc`: built-in Yellowstone transaction feed
//! - `YellowstoneGrpcTransactionStatus`: built-in Yellowstone transaction-status feed
//! - `YellowstoneGrpcAccounts`: built-in Yellowstone account-update feed
//! - `YellowstoneGrpcBlockMeta`: built-in Yellowstone block-meta feed
//! - `YellowstoneGrpcSlots`: built-in Yellowstone slot feed
//! - `LaserStream`: built-in LaserStream transaction feed
//! - `LaserStreamTransactionStatus`: built-in LaserStream transaction-status feed
//! - `LaserStreamAccounts`: built-in LaserStream account-update feed
//! - `LaserStreamBlockMeta`: built-in LaserStream block-meta feed
//! - `LaserStreamSlots`: built-in LaserStream slot feed
//! - `WebsocketTransaction`: built-in websocket `transactionSubscribe`
//! - `WebsocketLogs`: built-in websocket `logsSubscribe`
//! - `WebsocketAccount`: built-in websocket `accountSubscribe`
//! - `WebsocketProgram`: built-in websocket `programSubscribe`
//!
//! Each built-in source config can report its matching runtime mode directly
//! through `runtime_mode()`. `ProviderStreamMode::Generic` remains the typed
//! custom-adapter path and the fan-in mode when you want to combine multiple
//! heterogeneous upstream sources into one runtime ingress.
//!
//! Generic provider producers may still enqueue `TransactionViewBatch`,
//! `BlockMeta`, `RecentBlockhash`, `SlotStatus`, `ClusterTopology`,
//! `LeaderSchedule`, or `Reorg` updates directly.
//!
//! `ProviderStreamMode::Generic` is SOF's typed custom-adapter mode.
//! Your producer ingests an upstream format and maps it into one of the
//! `ProviderStreamUpdate` variants below before handing it to SOF.
//!
//! Built-in provider source configs extend that same typed surface:
//!
//! - websocket:
//!   - [`websocket::WebsocketTransactionConfig`] can target
//!     [`websocket::WebsocketPrimaryStream::Transaction`],
//!     [`websocket::WebsocketPrimaryStream::Account`], or
//!     [`websocket::WebsocketPrimaryStream::Program`]
//!   - [`websocket::WebsocketLogsConfig`] targets `logsSubscribe`
//! - Yellowstone:
//!   - [`yellowstone::YellowstoneGrpcConfig`] can target
//!     [`yellowstone::YellowstoneGrpcStream::Transaction`],
//!     [`yellowstone::YellowstoneGrpcStream::TransactionStatus`],
//!     [`yellowstone::YellowstoneGrpcStream::Accounts`], or
//!     [`yellowstone::YellowstoneGrpcStream::BlockMeta`]
//!   - [`yellowstone::YellowstoneGrpcSlotsConfig`] targets slot updates
//! - LaserStream:
//!   - [`laserstream::LaserStreamConfig`] can target
//!     [`laserstream::LaserStreamStream::Transaction`],
//!     [`laserstream::LaserStreamStream::TransactionStatus`],
//!     [`laserstream::LaserStreamStream::Accounts`], or
//!     [`laserstream::LaserStreamStream::BlockMeta`]
//!   - [`laserstream::LaserStreamSlotsConfig`] targets slot updates
//!
//! Those source selectors do not create a second runtime API. They extend the
//! existing provider config objects and emit the matching `ProviderStreamUpdate`
//! variants into the same runtime dispatch path.
//!
//! Built-in configs may also set:
//!
//! - a stable source instance label for observability via `with_source_instance(...)`
//! - whether one source is readiness-gating or optional via `with_readiness(...)`
//! - an operational role via `with_source_role(...)`
//! - an explicit arbitration priority via `with_source_priority(...)`
//! - duplicate handling via `with_source_arbitration(...)`
//!   - `EmitAll` keeps the historical behavior and forwards overlapping
//!     provider events from every source
//!   - `FirstSeen` suppresses later overlapping duplicates across sources
//!   - `FirstSeenThenPromote` keeps the first event immediate, but allows one
//!     later higher-priority duplicate to promote through
//!
//! Generic multi-source producers can reserve one stable source identity with
//! [`ProviderStreamFanIn::sender_for_source`]. The returned
//! [`ReservedProviderStreamSender`] automatically attributes every update it
//! sends to that reserved provider source.
//! Generic producers can set the same source policy directly on
//! [`ProviderSourceIdentity`] with `with_role(...)`, `with_priority(...)`, and
//! `with_arbitration(...)`.
//!
//! Generic readiness becomes source-aware only after a custom producer emits
//! [`ProviderStreamUpdate::Health`] for that reserved source. Until then,
//! `ProviderStreamMode::Generic` falls back to progress-based readiness and
//! only knows that typed updates are flowing at all.
//!
//! Fan-in duplicate arbitration is source-aware and keyed by the logical event,
//! not just by queue position. That means two sources carrying the same
//! transaction or control-plane item can now either:
//!
//! - both dispatch (`EmitAll`, the default)
//! - dispatch once (`FirstSeen`)
//! - dispatch once immediately and then allow one later higher-priority source
//!   to promote (`FirstSeenThenPromote`)
//!
//! Variant-to-runtime mapping:
//!
//! - `Transaction`:
//!   - drives `on_transaction`
//!   - drives derived-state transaction apply when enabled
//!   - synthesizes `on_recent_blockhash` from the transaction message when that
//!     hook is requested
//! - `SerializedTransaction`:
//!   - same transaction-family path, but lets SOF prefilter before full decode
//! - `TransactionLog`:
//!   - drives `on_transaction_log`
//! - `TransactionStatus`:
//!   - drives `on_transaction_status`
//! - `TransactionViewBatch`:
//!   - drives `on_transaction_view_batch`
//! - `AccountUpdate`:
//!   - drives `on_account_update`
//! - `BlockMeta`:
//!   - drives `on_block_meta`
//! - `RecentBlockhash`:
//!   - drives `on_recent_blockhash`
//! - `SlotStatus`:
//!   - drives `on_slot_status`
//! - `ClusterTopology`:
//!   - drives `on_cluster_topology`
//! - `LeaderSchedule`:
//!   - drives `on_leader_schedule`
//! - `Reorg`:
//!   - drives `on_reorg`
//! - `Health`:
//!   - updates provider health/readiness/observability
//!   - does not dispatch into plugin hooks
//!
//! # Feed Provider Transactions Into SOF
//!
//! ```no_run
//! use std::sync::Arc;
//!
//! use solana_hash::Hash;
//! use solana_keypair::Keypair;
//! use solana_message::{Message, VersionedMessage};
//! use solana_signer::Signer;
//! use solana_transaction::versioned::VersionedTransaction;
//! use sof::{
//!     event::{TxCommitmentStatus, TxKind},
//!     framework::TransactionEvent,
//!     provider_stream::{
//!         create_provider_stream_queue, ProviderStreamMode, ProviderStreamUpdate,
//!     },
//!     runtime::ObserverRuntime,
//! };
//!
//! # #[tokio::main]
//! # async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let (tx, rx) = create_provider_stream_queue(128);
//! let signer = Keypair::new();
//! let message = Message::new(&[], Some(&signer.pubkey()));
//! let transaction = VersionedTransaction::try_new(VersionedMessage::Legacy(message), &[&signer])?;
//!
//! tx.send(ProviderStreamUpdate::Transaction(TransactionEvent {
//!     slot: 1,
//!     commitment_status: TxCommitmentStatus::Processed,
//!     confirmed_slot: None,
//!     finalized_slot: None,
//!     signature: transaction.signatures.first().copied(),
//!     tx: Arc::new(transaction),
//!     kind: TxKind::NonVote,
//! }))
//! .await?;
//!
//! ObserverRuntime::new()
//!     .with_provider_stream_ingress(ProviderStreamMode::Generic, rx)
//!     .run_until(async {})
//!     .await?;
//! # Ok(())
//! # }
//! ```

use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use thiserror::Error;
use tokio::sync::mpsc;
use tokio::sync::mpsc::error::SendError;

#[cfg(any(feature = "provider-grpc", feature = "provider-websocket"))]
use std::sync::atomic::{AtomicU64, Ordering};

use crate::event::TxCommitmentStatus;
use crate::event::TxKind;
use crate::framework::{
    AccountUpdateEvent, BlockMetaEvent, ClusterTopologyEvent, LeaderScheduleEvent,
    ObservedRecentBlockhashEvent, ReorgEvent, SlotStatusEvent, TransactionEvent,
    TransactionLogEvent, TransactionStatusEvent, TransactionViewBatchEvent,
};
use agave_transaction_view::{
    transaction_data::TransactionData, transaction_view::SanitizedTransactionView,
};
use sof_types::SignatureBytes;
use solana_sdk_ids::{compute_budget, vote};
use solana_transaction::versioned::VersionedTransaction;

/// Default queue capacity for processed provider-stream ingress.
pub const DEFAULT_PROVIDER_STREAM_QUEUE_CAPACITY: usize = 8_192;

/// Identifies the processed provider family driving SOF's direct plugin ingress.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ProviderStreamMode {
    /// Generic custom provider-stream ingress supplied by the embedding application.
    ///
    /// This mode is for producers that push typed `ProviderStreamUpdate` items
    /// directly into SOF. A custom adapter converts upstream provider data into
    /// SOF's transaction, control-plane, or health updates.
    ///
    /// Use it when:
    ///
    /// - you have a processed provider that is not one of the built-in adapters
    /// - you need richer control-plane signals than the built-in processed
    ///   providers expose
    /// - you want a bounded replay/batch producer to feed SOF directly
    ///
    /// The runtime dispatch contract is:
    ///
    /// - `Transaction` / `SerializedTransaction` -> transaction-family hooks
    /// - `TransactionLog` -> `on_transaction_log`
    /// - `TransactionStatus` -> `on_transaction_status`
    /// - `TransactionViewBatch` -> `on_transaction_view_batch`
    /// - `AccountUpdate` -> `on_account_update`
    /// - `BlockMeta` -> `on_block_meta`
    /// - `RecentBlockhash` -> `on_recent_blockhash`
    /// - `SlotStatus` -> `on_slot_status`
    /// - `ClusterTopology` -> `on_cluster_topology`
    /// - `LeaderSchedule` -> `on_leader_schedule`
    /// - `Reorg` -> `on_reorg`
    /// - `Health` -> runtime health/readiness only
    Generic,
    /// Yellowstone gRPC / Geyser-style processed transaction feeds.
    YellowstoneGrpc,
    /// Yellowstone gRPC transaction-status feeds.
    YellowstoneGrpcTransactionStatus,
    /// Yellowstone gRPC account-update feeds.
    YellowstoneGrpcAccounts,
    /// Yellowstone gRPC block-meta feeds.
    YellowstoneGrpcBlockMeta,
    /// Yellowstone gRPC slot feeds.
    YellowstoneGrpcSlots,
    /// LaserStream-style processed transaction feeds.
    LaserStream,
    /// LaserStream transaction-status feeds.
    LaserStreamTransactionStatus,
    /// LaserStream account-update feeds.
    LaserStreamAccounts,
    /// LaserStream block-meta feeds.
    LaserStreamBlockMeta,
    /// LaserStream slot feeds.
    LaserStreamSlots,
    #[cfg(feature = "provider-websocket")]
    /// Websocket `transactionSubscribe` processed transaction feeds.
    WebsocketTransaction,
    #[cfg(feature = "provider-websocket")]
    /// Websocket `logsSubscribe` processed log feeds.
    WebsocketLogs,
    #[cfg(feature = "provider-websocket")]
    /// Websocket `accountSubscribe` processed account feeds.
    WebsocketAccount,
    #[cfg(feature = "provider-websocket")]
    /// Websocket `programSubscribe` processed account feeds.
    WebsocketProgram,
}

impl ProviderStreamMode {
    /// Returns the stable string label used in logs and docs.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Generic => "generic_provider",
            Self::YellowstoneGrpc => "yellowstone_grpc",
            Self::YellowstoneGrpcTransactionStatus => "yellowstone_grpc_transaction_status",
            Self::YellowstoneGrpcAccounts => "yellowstone_grpc_accounts",
            Self::YellowstoneGrpcBlockMeta => "yellowstone_grpc_block_meta",
            Self::YellowstoneGrpcSlots => "yellowstone_grpc_slots",
            Self::LaserStream => "laserstream",
            Self::LaserStreamTransactionStatus => "laserstream_transaction_status",
            Self::LaserStreamAccounts => "laserstream_accounts",
            Self::LaserStreamBlockMeta => "laserstream_block_meta",
            Self::LaserStreamSlots => "laserstream_slots",
            #[cfg(feature = "provider-websocket")]
            Self::WebsocketTransaction => "websocket_transaction",
            #[cfg(feature = "provider-websocket")]
            Self::WebsocketLogs => "websocket_logs",
            #[cfg(feature = "provider-websocket")]
            Self::WebsocketAccount => "websocket_account",
            #[cfg(feature = "provider-websocket")]
            Self::WebsocketProgram => "websocket_program",
        }
    }
}

/// Replay policy for processed provider transaction streams.
///
/// # Examples
///
/// ```rust
/// use sof::provider_stream::ProviderReplayMode;
///
/// let live = ProviderReplayMode::Live;
/// let resume = ProviderReplayMode::Resume;
/// let from_slot = ProviderReplayMode::FromSlot(1_000_000);
///
/// assert!(matches!(live, ProviderReplayMode::Live));
/// assert!(matches!(resume, ProviderReplayMode::Resume));
/// assert!(matches!(from_slot, ProviderReplayMode::FromSlot(1_000_000)));
/// ```
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum ProviderReplayMode {
    /// Start from the provider's live edge and do not request historical replay.
    Live,
    /// Start live and resume from the last tracked slot after reconnects.
    #[default]
    Resume,
    /// Request historical replay starting at one explicit slot, then continue
    /// with tracked resume behavior after reconnects.
    FromSlot(u64),
}

/// One processed provider-stream update accepted by SOF.
#[derive(Debug, Clone)]
pub enum ProviderStreamUpdate {
    /// One provider transaction mapped onto SOF's transaction hook surface.
    Transaction(TransactionEvent),
    /// One serialized provider transaction that can still be filtered before full decode.
    SerializedTransaction(SerializedTransactionEvent),
    /// One provider/websocket transaction-log notification.
    TransactionLog(TransactionLogEvent),
    /// One provider transaction-status notification.
    TransactionStatus(TransactionStatusEvent),
    /// One provider transaction-view batch mapped onto SOF's view-batch surface.
    TransactionViewBatch(TransactionViewBatchEvent),
    /// One provider account update.
    AccountUpdate(AccountUpdateEvent),
    /// One provider block-meta update.
    BlockMeta(BlockMetaEvent),
    /// One provider recent-blockhash observation.
    RecentBlockhash(ObservedRecentBlockhashEvent),
    /// One provider slot-status update.
    SlotStatus(SlotStatusEvent),
    /// One provider cluster-topology update.
    ClusterTopology(ClusterTopologyEvent),
    /// One provider leader-schedule update.
    LeaderSchedule(LeaderScheduleEvent),
    /// One provider reorg/fork update.
    Reorg(ReorgEvent),
    /// One provider source health transition observed by a built-in or generic source.
    Health(ProviderSourceHealthEvent),
}

impl ProviderStreamUpdate {
    /// Tags one provider-origin update with the source instance that produced it.
    #[must_use]
    pub fn with_provider_source(mut self, source: ProviderSourceIdentity) -> Self {
        let source = Arc::new(source);
        match &mut self {
            Self::Transaction(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::SerializedTransaction(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::TransactionLog(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::TransactionStatus(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::TransactionViewBatch(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::AccountUpdate(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::BlockMeta(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::RecentBlockhash(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::SlotStatus(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::ClusterTopology(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::LeaderSchedule(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::Reorg(event) => event.provider_source = Some(Arc::clone(&source)),
            Self::Health(event) => event.source = Arc::unwrap_or_clone(source),
        }
        self
    }
}

impl From<TransactionEvent> for ProviderStreamUpdate {
    fn from(event: TransactionEvent) -> Self {
        Self::Transaction(event)
    }
}

impl From<SerializedTransactionEvent> for ProviderStreamUpdate {
    fn from(event: SerializedTransactionEvent) -> Self {
        Self::SerializedTransaction(event)
    }
}

impl From<TransactionLogEvent> for ProviderStreamUpdate {
    fn from(event: TransactionLogEvent) -> Self {
        Self::TransactionLog(event)
    }
}

impl From<TransactionStatusEvent> for ProviderStreamUpdate {
    fn from(event: TransactionStatusEvent) -> Self {
        Self::TransactionStatus(event)
    }
}

impl From<TransactionViewBatchEvent> for ProviderStreamUpdate {
    fn from(event: TransactionViewBatchEvent) -> Self {
        Self::TransactionViewBatch(event)
    }
}

impl From<AccountUpdateEvent> for ProviderStreamUpdate {
    fn from(event: AccountUpdateEvent) -> Self {
        Self::AccountUpdate(event)
    }
}

impl From<BlockMetaEvent> for ProviderStreamUpdate {
    fn from(event: BlockMetaEvent) -> Self {
        Self::BlockMeta(event)
    }
}

impl From<ObservedRecentBlockhashEvent> for ProviderStreamUpdate {
    fn from(event: ObservedRecentBlockhashEvent) -> Self {
        Self::RecentBlockhash(event)
    }
}

impl From<SlotStatusEvent> for ProviderStreamUpdate {
    fn from(event: SlotStatusEvent) -> Self {
        Self::SlotStatus(event)
    }
}

impl From<ClusterTopologyEvent> for ProviderStreamUpdate {
    fn from(event: ClusterTopologyEvent) -> Self {
        Self::ClusterTopology(event)
    }
}

impl From<LeaderScheduleEvent> for ProviderStreamUpdate {
    fn from(event: LeaderScheduleEvent) -> Self {
        Self::LeaderSchedule(event)
    }
}

impl From<ReorgEvent> for ProviderStreamUpdate {
    fn from(event: ReorgEvent) -> Self {
        Self::Reorg(event)
    }
}

impl From<ProviderSourceHealthEvent> for ProviderStreamUpdate {
    fn from(event: ProviderSourceHealthEvent) -> Self {
        Self::Health(event)
    }
}

/// One provider source health transition observed by SOF.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ProviderSourceHealthEvent {
    /// Stable source instance identifier, for example one websocket program feed.
    pub source: ProviderSourceIdentity,
    /// Whether this source participates in readiness gating.
    pub readiness: ProviderSourceReadiness,
    /// Current health state for this source.
    pub status: ProviderSourceHealthStatus,
    /// Typed reason for the transition.
    pub reason: ProviderSourceHealthReason,
    /// Human-readable message attached to the transition.
    pub message: String,
}

/// Stable provider source instance identity used in runtime health and provider-origin events.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProviderSourceIdentity {
    /// Stable source kind, for example `WebsocketTransaction`.
    pub kind: ProviderSourceId,
    /// Runtime-unique or user-supplied source instance label.
    pub instance: Arc<str>,
    /// Relative arbitration priority for this source.
    pub priority: u16,
    /// Operational role for this source within one multi-source fan-in.
    pub role: ProviderSourceRole,
    /// Duplicate arbitration policy for this source.
    pub arbitration: ProviderSourceArbitrationMode,
}

impl ProviderSourceIdentity {
    /// Creates one provider source identity from a stable kind and instance label.
    #[must_use]
    pub fn new(kind: ProviderSourceId, instance: impl Into<Arc<str>>) -> Self {
        Self {
            kind,
            instance: instance.into(),
            priority: ProviderSourceRole::Primary.default_priority(),
            role: ProviderSourceRole::Primary,
            arbitration: ProviderSourceArbitrationMode::EmitAll,
        }
    }

    /// Returns the source kind label, for example `websocket_transaction`.
    #[must_use]
    pub fn kind_str(&self) -> &str {
        self.kind.as_str()
    }

    /// Returns the source instance label.
    #[must_use]
    pub fn instance_str(&self) -> &str {
        self.instance.as_ref()
    }

    /// Returns the relative arbitration priority for this source.
    #[must_use]
    pub const fn priority(&self) -> u16 {
        self.priority
    }

    /// Returns the configured source role.
    #[must_use]
    pub const fn role(&self) -> ProviderSourceRole {
        self.role
    }

    /// Returns the configured duplicate arbitration policy.
    #[must_use]
    pub const fn arbitration(&self) -> ProviderSourceArbitrationMode {
        self.arbitration
    }

    /// Sets one explicit arbitration priority.
    #[must_use]
    pub const fn with_priority(mut self, priority: u16) -> Self {
        self.priority = priority;
        self
    }

    /// Sets one source role and updates the default priority when the source still uses
    /// that role's previous default.
    #[must_use]
    pub const fn with_role(mut self, role: ProviderSourceRole) -> Self {
        if self.priority == self.role.default_priority() {
            self.priority = role.default_priority();
        }
        self.role = role;
        self
    }

    /// Sets one duplicate arbitration policy.
    #[must_use]
    pub const fn with_arbitration(mut self, arbitration: ProviderSourceArbitrationMode) -> Self {
        self.arbitration = arbitration;
        self
    }

    /// Builds a generated provider-source identity with a unique instance suffix.
    #[cfg(any(feature = "provider-grpc", feature = "provider-websocket"))]
    #[must_use]
    pub(crate) fn generated(kind: ProviderSourceId, label: Option<&str>) -> Self {
        static NEXT_PROVIDER_SOURCE_INSTANCE: AtomicU64 = AtomicU64::new(1);
        match label {
            Some(label) => Self::new(kind, label),
            None => {
                let instance = NEXT_PROVIDER_SOURCE_INSTANCE.fetch_add(1, Ordering::Relaxed);
                Self::new(kind.clone(), format!("{}-{instance}", kind.as_str()))
            }
        }
    }
}

impl std::fmt::Display for ProviderSourceIdentity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.kind.as_str(), self.instance)
    }
}

impl PartialEq for ProviderSourceIdentity {
    fn eq(&self, other: &Self) -> bool {
        self.kind == other.kind && self.instance == other.instance
    }
}

impl Eq for ProviderSourceIdentity {}

impl Hash for ProviderSourceIdentity {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.kind.hash(state);
        self.instance.hash(state);
    }
}

/// Stable provider source identifier used in runtime health reporting.
#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum ProviderSourceId {
    /// Generic custom provider source label supplied by the embedding app.
    Generic(Arc<str>),
    /// Built-in Yellowstone gRPC source.
    YellowstoneGrpc,
    /// Built-in Yellowstone gRPC transaction-status source.
    YellowstoneGrpcTransactionStatus,
    /// Built-in Yellowstone gRPC account source.
    YellowstoneGrpcAccounts,
    /// Built-in Yellowstone gRPC block-meta source.
    YellowstoneGrpcBlockMeta,
    /// Built-in Yellowstone gRPC slot source.
    YellowstoneGrpcSlots,
    /// Built-in LaserStream source.
    LaserStream,
    /// Built-in LaserStream transaction-status source.
    LaserStreamTransactionStatus,
    /// Built-in LaserStream account source.
    LaserStreamAccounts,
    /// Built-in LaserStream block-meta source.
    LaserStreamBlockMeta,
    /// Built-in LaserStream slot source.
    LaserStreamSlots,
    #[cfg(feature = "provider-websocket")]
    /// Built-in websocket `transactionSubscribe` source.
    WebsocketTransaction,
    #[cfg(feature = "provider-websocket")]
    /// Built-in websocket `logsSubscribe` source.
    WebsocketLogs,
    #[cfg(feature = "provider-websocket")]
    /// Built-in websocket `accountSubscribe` source.
    WebsocketAccount,
    #[cfg(feature = "provider-websocket")]
    /// Built-in websocket `programSubscribe` source.
    WebsocketProgram,
}

/// Relative operational role for one provider source inside a fan-in graph.
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum ProviderSourceRole {
    /// Lowest-latency or best-effort primary feed.
    Primary,
    /// Secondary feed expected to be healthy and often overlap the primary.
    Secondary,
    /// Lower-priority fallback feed.
    Fallback,
    /// Confirmation-only feed used mainly for richer or more trusted overlap.
    ConfirmOnly,
}

impl ProviderSourceRole {
    /// Returns the stable string label used in logs and docs.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Primary => "primary",
            Self::Secondary => "secondary",
            Self::Fallback => "fallback",
            Self::ConfirmOnly => "confirm_only",
        }
    }

    /// Returns the default arbitration priority for this role.
    #[must_use]
    pub const fn default_priority(self) -> u16 {
        match self {
            Self::Primary => 300,
            Self::Secondary => 200,
            Self::Fallback => 100,
            Self::ConfirmOnly => 400,
        }
    }
}

/// Duplicate arbitration mode for one provider source inside fan-in.
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum ProviderSourceArbitrationMode {
    /// Keep the current SOF behavior and emit overlapping duplicates from every source.
    EmitAll,
    /// First source wins for one logical event key; later duplicates are dropped.
    FirstSeen,
    /// First source wins immediately, but a later higher-priority duplicate may promote and emit.
    FirstSeenThenPromote,
}

impl ProviderSourceArbitrationMode {
    /// Returns the stable string label used in logs and docs.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::EmitAll => "emit_all",
            Self::FirstSeen => "first_seen",
            Self::FirstSeenThenPromote => "first_seen_then_promote",
        }
    }
}

impl ProviderSourceId {
    /// Returns the stable string label used in logs and error messages.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::Generic(label) => label.as_ref(),
            Self::YellowstoneGrpc => "yellowstone_grpc",
            Self::YellowstoneGrpcTransactionStatus => "yellowstone_grpc_transaction_status",
            Self::YellowstoneGrpcAccounts => "yellowstone_grpc_accounts",
            Self::YellowstoneGrpcBlockMeta => "yellowstone_grpc_block_meta",
            Self::YellowstoneGrpcSlots => "yellowstone_grpc_slots",
            Self::LaserStream => "laserstream",
            Self::LaserStreamTransactionStatus => "laserstream_transaction_status",
            Self::LaserStreamAccounts => "laserstream_accounts",
            Self::LaserStreamBlockMeta => "laserstream_block_meta",
            Self::LaserStreamSlots => "laserstream_slots",
            #[cfg(feature = "provider-websocket")]
            Self::WebsocketTransaction => "websocket_transaction",
            #[cfg(feature = "provider-websocket")]
            Self::WebsocketLogs => "websocket_logs",
            #[cfg(feature = "provider-websocket")]
            Self::WebsocketAccount => "websocket_account",
            #[cfg(feature = "provider-websocket")]
            Self::WebsocketProgram => "websocket_program",
        }
    }
}

/// Health state for one provider source feeding SOF.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ProviderSourceHealthStatus {
    /// Source is healthy and delivering updates.
    Healthy,
    /// Source is reconnecting or recovering after a disruption.
    Reconnecting,
    /// Source exhausted recovery and is no longer healthy.
    Unhealthy,
    /// Source registration was withdrawn and should be removed from tracking.
    ///
    /// This is a lifecycle control event, not a persistent steady-state health
    /// value. Runtime health and observability prune removed sources instead of
    /// surfacing them as active tracked sources.
    Removed,
}

/// Readiness class for one provider source observed by SOF.
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum ProviderSourceReadiness {
    /// This source is required for the runtime to report ready.
    Required,
    /// This source is advisory or redundant and does not gate readiness.
    Optional,
}

impl ProviderSourceReadiness {
    /// Returns the stable string label used in metrics and logs.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Required => "required",
            Self::Optional => "optional",
        }
    }
}

/// Typed reason for one provider source health transition.
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum ProviderSourceHealthReason {
    /// Source is configured and waiting for its first live session acknowledgement.
    InitialConnectPending,
    /// Subscription/setup completed and the provider acknowledged the stream.
    SubscriptionAckReceived,
    /// The provider stream ended without an explicit terminal error.
    UpstreamStreamClosedUnexpectedly,
    /// Transport-layer failure such as websocket or tonic I/O.
    UpstreamTransportFailure,
    /// Protocol or payload-shape failure.
    UpstreamProtocolFailure,
    /// Replay/backfill recovery failed.
    ReplayBackfillFailure,
    /// Reconnect budget was exhausted.
    ReconnectBudgetExhausted,
    /// Source task stopped and its registration was pruned from tracking.
    SourceRemoved,
}

impl ProviderSourceHealthReason {
    /// Returns the stable string label used in logs and runtime errors.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::InitialConnectPending => "initial_connect_pending",
            Self::SubscriptionAckReceived => "subscription_ack_received",
            Self::UpstreamStreamClosedUnexpectedly => "upstream_stream_closed_unexpectedly",
            Self::UpstreamTransportFailure => "upstream_transport_failure",
            Self::UpstreamProtocolFailure => "upstream_protocol_failure",
            Self::ReplayBackfillFailure => "replay_backfill_failure",
            Self::ReconnectBudgetExhausted => "reconnect_budget_exhausted",
            Self::SourceRemoved => "source_removed",
        }
    }
}

/// Sender type for processed provider-stream ingress.
pub type ProviderStreamSender = mpsc::Sender<ProviderStreamUpdate>;
/// Receiver type for processed provider-stream ingress.
pub type ProviderStreamReceiver = mpsc::Receiver<ProviderStreamUpdate>;
/// Shared provider source identity carried by provider-origin events.
pub type ProviderSourceRef = Arc<ProviderSourceIdentity>;

/// Duplicate provider source identity registration failure for multi-source fan-in.
#[derive(Debug, Error)]
#[error("provider fan-in already contains source identity {0}")]
pub struct ProviderSourceIdentityRegistrationError(pub ProviderSourceIdentity);

/// One reserved source identity held for the lifetime of one producer.
#[derive(Debug)]
pub(crate) struct ProviderSourceReservation {
    /// Fan-in that owns the reserved identity set.
    fan_in: ProviderStreamFanIn,
    /// Stable source identity held until the reservation is dropped.
    source: ProviderSourceIdentity,
    /// Sender used to prune generic source health when the reservation drops.
    removal_sender: Option<ProviderStreamSender>,
}

/// Deferred source-identity release that runs only after a terminal removal event is queued.
#[derive(Debug)]
struct ProviderSourceDeferredRelease {
    /// Fan-in that owns the reserved identity set.
    fan_in: ProviderStreamFanIn,
    /// Stable source identity released when the guard drops.
    source: ProviderSourceIdentity,
}

impl Drop for ProviderSourceDeferredRelease {
    fn drop(&mut self) {
        self.fan_in.release_source_identity(&self.source);
    }
}

/// One owned release guard kept alive until terminal source cleanup completes.
#[derive(Debug, Default)]
struct ProviderSourceReleaseGuard {
    /// Release one raw reserved identity directly.
    _identity: Option<ProviderSourceDeferredRelease>,
    /// Keep one built-in reservation alive until the delayed removal send completes.
    _reservation: Option<Arc<ProviderSourceReservation>>,
}

impl ProviderSourceReleaseGuard {
    /// Creates one guard that releases a reserved identity directly on drop.
    const fn for_identity(release: ProviderSourceDeferredRelease) -> Self {
        Self {
            _identity: Some(release),
            _reservation: None,
        }
    }

    #[cfg(any(feature = "provider-grpc", feature = "provider-websocket"))]
    /// Creates one guard that keeps a built-in reservation alive until drop.
    const fn for_reservation(reservation: Arc<ProviderSourceReservation>) -> Self {
        Self {
            _identity: None,
            _reservation: Some(reservation),
        }
    }
}

/// Maximum no-runtime retries for one terminal `Removed` event before releasing anyway.
const SOURCE_REMOVED_NO_RUNTIME_RETRY_ATTEMPTS: usize = 64;
/// Delay between no-runtime retries for one terminal `Removed` event.
const SOURCE_REMOVED_NO_RUNTIME_RETRY_DELAY: Duration = Duration::from_millis(5);

impl Drop for ProviderSourceReservation {
    fn drop(&mut self) {
        let release = ProviderSourceReleaseGuard::for_identity(ProviderSourceDeferredRelease {
            fan_in: self.fan_in.clone(),
            source: self.source.clone(),
        });
        if let Some(sender) = self.removal_sender.clone() {
            drop(emit_provider_source_removed(
                &sender,
                self.source.clone(),
                ProviderSourceReadiness::Optional,
                "reserved provider source sender dropped and was removed from tracking".to_owned(),
                Some(release),
            ));
        } else {
            drop(release);
        }
    }
}

/// One reserved provider source identity plus a sender bound to that reservation.
#[derive(Clone, Debug)]
pub struct ReservedProviderStreamSender {
    /// Sender bound to one reserved source identity.
    sender: ProviderStreamSender,
    /// Reservation released automatically when the last handle is dropped.
    reservation: Arc<ProviderSourceReservation>,
}

impl ReservedProviderStreamSender {
    /// Returns the reserved provider source identity for this sender.
    #[must_use]
    pub fn source(&self) -> &ProviderSourceIdentity {
        &self.reservation.source
    }

    /// Binds one outgoing update to this sender's reserved provider source.
    fn bind_update(&self, update: ProviderStreamUpdate) -> ProviderStreamUpdate {
        update.with_provider_source(self.reservation.source.clone())
    }

    /// Sends one provider update attributed to this reserved source identity.
    ///
    /// Any provider source already present on `update` is replaced with this
    /// sender's reserved source identity.
    ///
    /// # Errors
    ///
    /// Returns the underlying queue send error when the provider ingress queue
    /// is closed.
    pub async fn send(
        &self,
        update: ProviderStreamUpdate,
    ) -> Result<(), SendError<ProviderStreamUpdate>> {
        if matches!(
            update,
            ProviderStreamUpdate::Health(ProviderSourceHealthEvent {
                status: ProviderSourceHealthStatus::Removed,
                ..
            })
        ) {
            return Err(SendError(self.bind_update(update)));
        }
        self.sender.send(self.bind_update(update)).await
    }
}

/// One running provider task that should prune its source registration when it stops.
#[cfg(any(feature = "provider-grpc", feature = "provider-websocket"))]
#[derive(Debug)]
pub(crate) struct ProviderSourceTaskGuard {
    /// Sender used to publish the terminal removal event.
    sender: ProviderStreamSender,
    /// Source identity removed when the task stops.
    source: ProviderSourceIdentity,
    /// Readiness class preserved on the terminal removal event.
    readiness: ProviderSourceReadiness,
    /// Reservation kept alive until the task stops.
    reservation: Option<Arc<ProviderSourceReservation>>,
}

#[cfg(any(feature = "provider-grpc", feature = "provider-websocket"))]
impl ProviderSourceTaskGuard {
    /// Creates one guard for a running provider source task.
    #[must_use]
    pub(crate) const fn new(
        sender: ProviderStreamSender,
        source: ProviderSourceIdentity,
        readiness: ProviderSourceReadiness,
        reservation: Option<Arc<ProviderSourceReservation>>,
    ) -> Self {
        Self {
            sender,
            source,
            readiness,
            reservation,
        }
    }
}

#[cfg(any(feature = "provider-grpc", feature = "provider-websocket"))]
impl Drop for ProviderSourceTaskGuard {
    fn drop(&mut self) {
        drop(emit_provider_source_removed(
            &self.sender,
            self.source.clone(),
            self.readiness,
            "provider source task stopped and was removed from tracking".to_owned(),
            self.reservation
                .take()
                .map(ProviderSourceReleaseGuard::for_reservation),
        ));
    }
}

/// Helper for feeding one SOF provider queue from multiple provider sources.
#[derive(Clone, Debug)]
pub struct ProviderStreamFanIn {
    /// Shared sender used to fan multiple provider sources into one ingress queue.
    sender: ProviderStreamSender,
    /// Registered stable source identities reserved for this fan-in.
    identities: Arc<Mutex<HashSet<ProviderSourceIdentity>>>,
}

impl ProviderStreamFanIn {
    /// Reserves one stable source identity and returns a sender bound to that reservation.
    ///
    /// # Errors
    ///
    /// Returns an error when the same full source identity was already reserved
    /// for this fan-in.
    pub fn sender_for_source(
        &self,
        source: ProviderSourceIdentity,
    ) -> Result<ReservedProviderStreamSender, ProviderSourceIdentityRegistrationError> {
        let reservation = self.reserve_source_identity_generic(source)?;
        Ok(ReservedProviderStreamSender {
            sender: self.sender.clone(),
            reservation: Arc::new(reservation),
        })
    }

    /// Returns a cloned sender for built-in helper methods after identity checks.
    #[cfg(any(feature = "provider-grpc", feature = "provider-websocket"))]
    #[must_use]
    pub(crate) fn sender(&self) -> ProviderStreamSender {
        self.sender.clone()
    }

    /// Reserves one stable source identity for this fan-in.
    pub(crate) fn reserve_source_identity(
        &self,
        source: ProviderSourceIdentity,
    ) -> Result<ProviderSourceReservation, ProviderSourceIdentityRegistrationError> {
        let Ok(mut identities) = self.identities.lock() else {
            return Err(ProviderSourceIdentityRegistrationError(source));
        };
        if !identities.insert(source.clone()) {
            return Err(ProviderSourceIdentityRegistrationError(source));
        }
        Ok(ProviderSourceReservation {
            fan_in: self.clone(),
            source,
            removal_sender: None,
        })
    }

    /// Reserves one stable source identity for a generic producer and prunes it on drop.
    fn reserve_source_identity_generic(
        &self,
        source: ProviderSourceIdentity,
    ) -> Result<ProviderSourceReservation, ProviderSourceIdentityRegistrationError> {
        let mut reservation = self.reserve_source_identity(source)?;
        reservation.removal_sender = Some(self.sender.clone());
        Ok(reservation)
    }

    /// Releases one previously reserved stable source identity.
    pub(crate) fn release_source_identity(&self, source: &ProviderSourceIdentity) {
        if let Ok(mut identities) = self.identities.lock() {
            identities.remove(source);
        }
    }
}

/// One serialized provider-fed transaction that has not yet been materialized.
#[derive(Debug, Clone)]
pub struct SerializedTransactionEvent {
    /// Slot containing this transaction.
    pub slot: u64,
    /// Commitment status for this transaction slot when event was emitted.
    pub commitment_status: TxCommitmentStatus,
    /// Latest observed confirmed slot watermark when event was emitted.
    pub confirmed_slot: Option<u64>,
    /// Latest observed finalized slot watermark when event was emitted.
    pub finalized_slot: Option<u64>,
    /// Transaction signature if present.
    pub signature: Option<SignatureBytes>,
    /// Provider source instance when this transaction came from provider ingress.
    pub provider_source: Option<ProviderSourceRef>,
    /// Serialized transaction bytes.
    pub bytes: Box<[u8]>,
}

#[cfg(any(feature = "provider-grpc", feature = "provider-websocket"))]
#[derive(Clone, Copy, Debug, Default)]
/// Watermark tracker for provider commitments used by built-in adapters.
pub(crate) struct ProviderCommitmentWatermarks {
    /// Latest confirmed slot watermark observed from the provider.
    pub(crate) confirmed_slot: Option<u64>,
    /// Latest finalized slot watermark observed from the provider.
    pub(crate) finalized_slot: Option<u64>,
}

#[cfg(any(feature = "provider-grpc", feature = "provider-websocket"))]
impl ProviderCommitmentWatermarks {
    /// Updates commitment watermarks based on one transaction slot and commitment.
    #[inline]
    pub(crate) fn observe_transaction_commitment(
        &mut self,
        slot: u64,
        commitment_status: TxCommitmentStatus,
    ) {
        match commitment_status {
            TxCommitmentStatus::Processed => {}
            TxCommitmentStatus::Confirmed => {
                self.confirmed_slot = Some(self.confirmed_slot.unwrap_or(slot).max(slot));
            }
            TxCommitmentStatus::Finalized => {
                self.confirmed_slot = Some(self.confirmed_slot.unwrap_or(slot).max(slot));
                self.finalized_slot = Some(self.finalized_slot.unwrap_or(slot).max(slot));
            }
        }
    }

    /// Advances the confirmed watermark to include one slot.
    #[inline]
    pub(crate) fn observe_confirmed_slot(&mut self, slot: u64) {
        self.confirmed_slot = Some(self.confirmed_slot.unwrap_or(slot).max(slot));
    }

    /// Advances the finalized watermark to include one slot.
    #[inline]
    pub(crate) fn observe_finalized_slot(&mut self, slot: u64) {
        self.observe_confirmed_slot(slot);
        self.finalized_slot = Some(self.finalized_slot.unwrap_or(slot).max(slot));
    }
}

/// Creates one bounded queue for processed provider-stream updates.
///
/// # Examples
///
/// ```rust
/// use sof::provider_stream::{create_provider_stream_queue, DEFAULT_PROVIDER_STREAM_QUEUE_CAPACITY};
///
/// let (_tx, _rx) = create_provider_stream_queue(DEFAULT_PROVIDER_STREAM_QUEUE_CAPACITY);
/// ```
#[must_use]
pub fn create_provider_stream_queue(
    capacity: usize,
) -> (ProviderStreamSender, ProviderStreamReceiver) {
    mpsc::channel(capacity.max(1))
}

/// Creates one bounded queue plus a typed helper for multi-source provider fan-in.
///
/// # Examples
///
/// ```rust
/// use sof::provider_stream::{create_provider_stream_fan_in, ProviderStreamMode};
///
/// let (_fan_in, _rx) = create_provider_stream_fan_in(256);
/// let _mode = ProviderStreamMode::Generic;
/// ```
#[must_use]
pub fn create_provider_stream_fan_in(
    capacity: usize,
) -> (ProviderStreamFanIn, ProviderStreamReceiver) {
    let (sender, receiver) = create_provider_stream_queue(capacity);
    (
        ProviderStreamFanIn {
            sender,
            identities: Arc::new(Mutex::new(HashSet::new())),
        },
        receiver,
    )
}

/// Classifies provider-fed transactions consistently across built-in adapters.
pub(crate) fn classify_provider_transaction_kind(tx: &VersionedTransaction) -> TxKind {
    let mut has_vote = false;
    let mut has_non_vote_non_budget = false;
    let keys = tx.message.static_account_keys();
    for instruction in tx.message.instructions() {
        if let Some(program_id) = keys.get(usize::from(instruction.program_id_index)) {
            if *program_id == vote::id() {
                has_vote = true;
                if has_non_vote_non_budget {
                    return TxKind::Mixed;
                }
                continue;
            }
            if *program_id != compute_budget::id() {
                has_non_vote_non_budget = true;
                if has_vote {
                    return TxKind::Mixed;
                }
            }
        }
    }
    if has_vote && !has_non_vote_non_budget {
        TxKind::VoteOnly
    } else if has_vote {
        TxKind::Mixed
    } else {
        TxKind::NonVote
    }
}

/// Classifies provider-fed transaction views consistently across built-in adapters.
pub(crate) fn classify_provider_transaction_kind_view<D: TransactionData>(
    view: &SanitizedTransactionView<D>,
) -> TxKind {
    let mut has_vote = false;
    let mut has_non_vote_non_budget = false;
    for (program_id, _) in view.program_instructions_iter() {
        if *program_id == vote::id() {
            has_vote = true;
            if has_non_vote_non_budget {
                return TxKind::Mixed;
            }
            continue;
        }
        if *program_id != compute_budget::id() {
            has_non_vote_non_budget = true;
            if has_vote {
                return TxKind::Mixed;
            }
        }
    }
    if has_vote && !has_non_vote_non_budget {
        TxKind::VoteOnly
    } else if has_vote {
        TxKind::Mixed
    } else {
        TxKind::NonVote
    }
}

/// Emits one terminal provider-source removal event and prunes runtime tracking.
fn emit_provider_source_removed(
    sender: &ProviderStreamSender,
    source: ProviderSourceIdentity,
    readiness: ProviderSourceReadiness,
    message: String,
    release: Option<ProviderSourceReleaseGuard>,
) -> Option<ProviderSourceReleaseGuard> {
    let event = ProviderStreamUpdate::Health(ProviderSourceHealthEvent {
        source,
        readiness,
        status: ProviderSourceHealthStatus::Removed,
        reason: ProviderSourceHealthReason::SourceRemoved,
        message,
    });
    match sender.try_send(event) {
        Ok(()) | Err(mpsc::error::TrySendError::Closed(_)) => release,
        Err(mpsc::error::TrySendError::Full(update)) => {
            let sender = sender.clone();
            if let Ok(handle) = tokio::runtime::Handle::try_current() {
                handle.spawn(async move {
                    drop(sender.send(update).await);
                    drop(release);
                });
                None
            } else {
                std::thread::spawn(move || {
                    let mut pending_update = update;
                    for _ in 0..SOURCE_REMOVED_NO_RUNTIME_RETRY_ATTEMPTS {
                        match sender.try_send(pending_update) {
                            Ok(()) | Err(mpsc::error::TrySendError::Closed(_)) => {
                                drop(release);
                                return;
                            }
                            Err(mpsc::error::TrySendError::Full(retried_update)) => {
                                pending_update = retried_update;
                                std::thread::sleep(SOURCE_REMOVED_NO_RUNTIME_RETRY_DELAY);
                            }
                        }
                    }
                    drop(release);
                });
                None
            }
        }
    }
}

#[cfg(any(feature = "provider-grpc", feature = "provider-websocket"))]
/// Emits one terminal provider-source removal event for startup failure or early cleanup.
pub(crate) fn emit_provider_source_removed_with_reservation(
    sender: &ProviderStreamSender,
    source: ProviderSourceIdentity,
    readiness: ProviderSourceReadiness,
    message: String,
    reservation: Option<Arc<ProviderSourceReservation>>,
) {
    drop(emit_provider_source_removed(
        sender,
        source,
        readiness,
        message,
        reservation.map(ProviderSourceReleaseGuard::for_reservation),
    ));
}

#[cfg(feature = "provider-grpc")]
/// Yellowstone gRPC adapter helpers.
pub mod yellowstone;

#[cfg(feature = "provider-grpc")]
/// Helius LaserStream adapter helpers.
pub mod laserstream;

#[cfg(feature = "provider-websocket")]
/// Websocket `transactionSubscribe` adapter helpers.
pub mod websocket;

#[cfg(all(test, any(feature = "provider-grpc", feature = "provider-websocket")))]
mod tests {
    use super::*;
    use solana_instruction::Instruction;
    use solana_keypair::Keypair;
    use solana_message::{Message, VersionedMessage};
    use solana_sdk_ids::system_program;
    use solana_signer::Signer;
    use std::{sync::Arc, time::Instant};
    use tokio::runtime::Runtime;
    use tokio::time::{Duration, sleep, timeout};

    fn profile_iterations(default: usize) -> usize {
        std::env::var("SOF_PROFILE_ITERATIONS")
            .ok()
            .and_then(|value| value.parse::<usize>().ok())
            .filter(|value| *value > 0)
            .unwrap_or(default)
    }

    fn sample_mixed_transaction() -> VersionedTransaction {
        let signer = Keypair::new();
        let mut instructions = Vec::with_capacity(34);
        instructions.push(Instruction::new_with_bytes(vote::id(), &[], vec![]));
        instructions.push(Instruction::new_with_bytes(
            system_program::id(),
            &[],
            vec![],
        ));
        for _ in 0..32 {
            instructions.push(Instruction::new_with_bytes(
                compute_budget::id(),
                &[],
                vec![],
            ));
        }
        let message = Message::new(&instructions, Some(&signer.pubkey()));
        VersionedTransaction::try_new(VersionedMessage::Legacy(message), &[&signer]).expect("tx")
    }

    fn classify_provider_transaction_kind_baseline(tx: &VersionedTransaction) -> TxKind {
        let mut has_vote = false;
        let mut has_non_vote_non_budget = false;
        let keys = tx.message.static_account_keys();
        for instruction in tx.message.instructions() {
            if let Some(program_id) = keys.get(usize::from(instruction.program_id_index)) {
                if *program_id == vote::id() {
                    has_vote = true;
                    continue;
                }
                if *program_id != compute_budget::id() {
                    has_non_vote_non_budget = true;
                }
            }
        }
        if has_vote && !has_non_vote_non_budget {
            TxKind::VoteOnly
        } else if has_vote {
            TxKind::Mixed
        } else {
            TxKind::NonVote
        }
    }

    #[test]
    fn classify_provider_transaction_kind_detects_mixed() {
        let tx = sample_mixed_transaction();
        assert_eq!(classify_provider_transaction_kind(&tx), TxKind::Mixed);
    }

    #[test]
    fn reserved_provider_sender_binds_reserved_source_identity() {
        let (fan_in, mut rx) = create_provider_stream_fan_in(4);
        let sender = fan_in
            .sender_for_source(ProviderSourceIdentity::new(
                ProviderSourceId::Generic(Arc::<str>::from("custom")),
                "source-a",
            ))
            .expect("reserve source");
        let other_source = ProviderSourceIdentity::new(
            ProviderSourceId::Generic(Arc::<str>::from("other")),
            "source-b",
        );

        Runtime::new().expect("runtime").block_on(async move {
            sender
                .send(ProviderStreamUpdate::RecentBlockhash(
                    ObservedRecentBlockhashEvent {
                        slot: 7,
                        recent_blockhash: solana_hash::Hash::new_unique().to_bytes(),
                        dataset_tx_count: 0,
                        provider_source: Some(Arc::new(other_source)),
                    },
                ))
                .await
                .expect("send");

            let update = rx.recv().await.expect("provider update");
            let ProviderStreamUpdate::RecentBlockhash(event) = update else {
                panic!("expected recent blockhash update");
            };
            let source = event.provider_source.expect("bound provider source");
            assert_eq!(source.kind_str(), "custom");
            assert_eq!(source.instance_str(), "source-a");
        });
    }

    #[test]
    fn reserved_provider_sender_emits_removed_health_on_drop() {
        let (fan_in, mut rx) = create_provider_stream_fan_in(4);
        let source = ProviderSourceIdentity::new(
            ProviderSourceId::Generic(Arc::<str>::from("custom")),
            "source-a",
        );
        let sender = fan_in
            .sender_for_source(source.clone())
            .expect("reserve source");

        Runtime::new().expect("runtime").block_on(async move {
            sender
                .send(ProviderStreamUpdate::Health(ProviderSourceHealthEvent {
                    source: source.clone(),
                    readiness: ProviderSourceReadiness::Required,
                    status: ProviderSourceHealthStatus::Healthy,
                    reason: ProviderSourceHealthReason::SubscriptionAckReceived,
                    message: "source subscription acknowledged".to_owned(),
                }))
                .await
                .expect("send health");
            drop(sender);

            let first = rx.recv().await.expect("health update");
            let ProviderStreamUpdate::Health(first) = first else {
                panic!("expected first health update");
            };
            assert_eq!(first.source, source);
            assert_eq!(first.status, ProviderSourceHealthStatus::Healthy);

            let second = rx.recv().await.expect("removed update");
            let ProviderStreamUpdate::Health(second) = second else {
                panic!("expected removed health update");
            };
            assert_eq!(second.source, source);
            assert_eq!(second.status, ProviderSourceHealthStatus::Removed);
            assert_eq!(second.reason, ProviderSourceHealthReason::SourceRemoved);
        });
    }

    #[test]
    fn reserved_provider_sender_defers_identity_reuse_until_removed_is_enqueued() {
        let (fan_in, mut rx) = create_provider_stream_fan_in(1);
        let source = ProviderSourceIdentity::new(
            ProviderSourceId::Generic(Arc::<str>::from("custom")),
            "source-a",
        );
        let sender = fan_in
            .sender_for_source(source.clone())
            .expect("reserve source");

        Runtime::new().expect("runtime").block_on(async move {
            sender
                .send(ProviderStreamUpdate::Health(ProviderSourceHealthEvent {
                    source: source.clone(),
                    readiness: ProviderSourceReadiness::Required,
                    status: ProviderSourceHealthStatus::Healthy,
                    reason: ProviderSourceHealthReason::SubscriptionAckReceived,
                    message: "source subscription acknowledged".to_owned(),
                }))
                .await
                .expect("send health");
            drop(sender);

            assert!(
                fan_in.sender_for_source(source.clone()).is_err(),
                "identity should stay reserved until removed is queued"
            );

            let first = rx.recv().await.expect("health update");
            let ProviderStreamUpdate::Health(first) = first else {
                panic!("expected first health update");
            };
            assert_eq!(first.status, ProviderSourceHealthStatus::Healthy);

            let second = timeout(Duration::from_secs(1), rx.recv())
                .await
                .expect("removed update should arrive")
                .expect("removed health update");
            let ProviderStreamUpdate::Health(second) = second else {
                panic!("expected removed health update");
            };
            assert_eq!(second.status, ProviderSourceHealthStatus::Removed);
            assert_eq!(second.reason, ProviderSourceHealthReason::SourceRemoved);

            timeout(Duration::from_secs(1), async {
                loop {
                    if fan_in.sender_for_source(source.clone()).is_ok() {
                        break;
                    }
                    sleep(Duration::from_millis(10)).await;
                }
            })
            .await
            .expect("identity released after removed is enqueued");
        });
    }

    #[test]
    fn reserved_provider_sender_drop_outside_runtime_does_not_panic_when_queue_is_full() {
        let (fan_in, mut rx) = create_provider_stream_fan_in(1);
        let source = ProviderSourceIdentity::new(
            ProviderSourceId::Generic(Arc::<str>::from("custom")),
            "source-a",
        );
        let sender = fan_in
            .sender_for_source(source.clone())
            .expect("reserve source");

        Runtime::new().expect("runtime").block_on(async {
            sender
                .send(ProviderStreamUpdate::Health(ProviderSourceHealthEvent {
                    source: source.clone(),
                    readiness: ProviderSourceReadiness::Required,
                    status: ProviderSourceHealthStatus::Healthy,
                    reason: ProviderSourceHealthReason::SubscriptionAckReceived,
                    message: "source subscription acknowledged".to_owned(),
                }))
                .await
                .expect("send health");
        });

        let mut sender = Some(sender);
        let drop_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            drop(sender.take().expect("reserved sender"));
        }));
        assert!(
            drop_result.is_ok(),
            "dropping a reserved generic sender outside a tokio runtime should not panic"
        );

        Runtime::new().expect("runtime").block_on(async move {
            let first = rx.recv().await.expect("health update");
            let ProviderStreamUpdate::Health(first) = first else {
                panic!("expected first health update");
            };
            assert_eq!(first.status, ProviderSourceHealthStatus::Healthy);

            let second = timeout(Duration::from_secs(1), rx.recv())
                .await
                .expect("removed update should arrive")
                .expect("removed health update");
            let ProviderStreamUpdate::Health(second) = second else {
                panic!("expected removed health update");
            };
            assert_eq!(second.status, ProviderSourceHealthStatus::Removed);
            assert_eq!(second.reason, ProviderSourceHealthReason::SourceRemoved);

            timeout(Duration::from_secs(1), async {
                loop {
                    if fan_in.sender_for_source(source.clone()).is_ok() {
                        break;
                    }
                    sleep(Duration::from_millis(10)).await;
                }
            })
            .await
            .expect("identity released after background removal send");
        });
    }

    #[test]
    fn reserved_provider_sender_rejects_removed_health_while_alive() {
        let (fan_in, mut rx) = create_provider_stream_fan_in(4);
        let source = ProviderSourceIdentity::new(
            ProviderSourceId::Generic(Arc::<str>::from("custom")),
            "source-a",
        );
        let sender = fan_in
            .sender_for_source(source.clone())
            .expect("reserve source");

        Runtime::new().expect("runtime").block_on(async move {
            let error = sender
                .send(ProviderStreamUpdate::Health(ProviderSourceHealthEvent {
                    source: source.clone(),
                    readiness: ProviderSourceReadiness::Required,
                    status: ProviderSourceHealthStatus::Removed,
                    reason: ProviderSourceHealthReason::SourceRemoved,
                    message: "should be rejected".to_owned(),
                }))
                .await
                .expect_err("reserved sender should reject removed health while alive");
            let removed_update = error.0;
            let ProviderStreamUpdate::Health(removed) = removed_update else {
                panic!("expected removed health update in send error");
            };
            assert_eq!(removed.source, source);
            assert_eq!(removed.status, ProviderSourceHealthStatus::Removed);

            assert!(
                timeout(Duration::from_millis(50), rx.recv()).await.is_err(),
                "rejected removed update must not reach the provider queue"
            );
        });
    }

    #[test]
    fn reserved_provider_sender_releases_identity_after_bounded_no_runtime_cleanup_retry() {
        let (fan_in, _rx) = create_provider_stream_fan_in(1);
        let source = ProviderSourceIdentity::new(
            ProviderSourceId::Generic(Arc::<str>::from("custom")),
            "source-a",
        );
        let sender = fan_in
            .sender_for_source(source.clone())
            .expect("reserve source");

        fan_in
            .sender()
            .try_send(ProviderStreamUpdate::Health(ProviderSourceHealthEvent {
                source: ProviderSourceIdentity::new(
                    ProviderSourceId::Generic(Arc::<str>::from("other")),
                    "other-source",
                ),
                readiness: ProviderSourceReadiness::Optional,
                status: ProviderSourceHealthStatus::Healthy,
                reason: ProviderSourceHealthReason::SubscriptionAckReceived,
                message: "occupied".to_owned(),
            }))
            .expect("fill provider queue");

        let drop_result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            drop(sender);
        }));
        assert!(drop_result.is_ok(), "drop outside runtime should not panic");

        std::thread::sleep(Duration::from_millis(
            (SOURCE_REMOVED_NO_RUNTIME_RETRY_ATTEMPTS as u64 * 5) + 100,
        ));

        fan_in
            .sender_for_source(source)
            .expect("identity released after bounded no-runtime cleanup retry");
    }

    #[test]
    fn provider_source_task_guard_emits_removed_health_on_drop() {
        let (tx, mut rx) = create_provider_stream_queue(4);
        let source = ProviderSourceIdentity::new(
            ProviderSourceId::Generic(Arc::<str>::from("custom")),
            "source-a",
        );

        {
            let _guard = ProviderSourceTaskGuard::new(
                tx,
                source.clone(),
                ProviderSourceReadiness::Required,
                None,
            );
        }

        Runtime::new().expect("runtime").block_on(async move {
            let update = rx.recv().await.expect("provider update");
            let ProviderStreamUpdate::Health(event) = update else {
                panic!("expected health update");
            };
            assert_eq!(event.source, source);
            assert_eq!(event.status, ProviderSourceHealthStatus::Removed);
            assert_eq!(event.reason, ProviderSourceHealthReason::SourceRemoved);
        });
    }

    #[test]
    #[ignore = "profiling fixture for provider transaction kind classification A/B"]
    fn provider_transaction_kind_profile_fixture() {
        let iterations = profile_iterations(1_000_000);

        let tx = sample_mixed_transaction();

        let baseline_started = Instant::now();
        for _ in 0..iterations {
            std::hint::black_box(classify_provider_transaction_kind_baseline(&tx));
        }
        let baseline_elapsed = baseline_started.elapsed();

        let optimized_started = Instant::now();
        for _ in 0..iterations {
            std::hint::black_box(classify_provider_transaction_kind(&tx));
        }
        let optimized_elapsed = optimized_started.elapsed();

        eprintln!(
            "provider_transaction_kind_profile_fixture iterations={} baseline_us={} optimized_us={}",
            iterations,
            baseline_elapsed.as_micros(),
            optimized_elapsed.as_micros(),
        );
    }

    #[test]
    #[ignore = "profiling fixture for baseline provider tx kind classification"]
    fn provider_transaction_kind_baseline_profile_fixture() {
        let iterations = profile_iterations(1_000_000);

        let tx = sample_mixed_transaction();
        for _ in 0..iterations {
            std::hint::black_box(classify_provider_transaction_kind_baseline(&tx));
        }
    }

    #[test]
    #[ignore = "profiling fixture for optimized provider tx kind classification"]
    fn provider_transaction_kind_optimized_profile_fixture() {
        let iterations = profile_iterations(1_000_000);

        let tx = sample_mixed_transaction();
        for _ in 0..iterations {
            std::hint::black_box(classify_provider_transaction_kind(&tx));
        }
    }
}