tari_comms 6.2.0-pre.0

A peer-to-peer messaging system
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
//  Copyright 2021, The Tari Project
//
//  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
//  following conditions are met:
//
//  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
//  disclaimer.
//
//  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
//  following disclaimer in the documentation and/or other materials provided with the distribution.
//
//  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
//  products derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
//  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
//  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
//  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
//  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// mod chunking;

mod error;
pub use error::RpcServerError;

mod handle;
pub use handle::RpcServerHandle;
use handle::RpcServerRequest;

#[cfg(feature = "metrics")]
mod metrics;

pub mod mock;

mod early_close;
mod router;

use std::{
    borrow::Cow,
    cmp,
    collections::HashMap,
    convert::TryFrom,
    future::Future,
    io,
    io::ErrorKind,
    pin::Pin,
    sync::Arc,
    task::Poll,
    time::{Duration, Instant},
};

use futures::{SinkExt, StreamExt, future, stream::FuturesUnordered};
use log::*;
use prost::Message;
use router::Router;
use tokio::{
    sync::mpsc,
    task::{JoinHandle, JoinSet},
    time,
};
use tokio_stream::Stream;
use tower::{Service, make::MakeService};
use tracing::{Instrument, Level, debug, error, instrument, span, trace, warn};

use super::{
    Handshake,
    RPC_MAX_FRAME_SIZE,
    body::Body,
    context::{RequestContext, RpcCommsProvider},
    error::HandshakeRejectReason,
    message::{Request, Response, RpcMessageFlags},
    not_found::ProtocolServiceNotFound,
    status::RpcStatus,
};
use crate::{
    Bytes,
    Substream,
    bounded_executor::BoundedExecutor,
    framing,
    framing::CanonicalFraming,
    message::MessageExt,
    peer_manager::NodeId,
    proto,
    protocol::{
        ProtocolEvent,
        ProtocolId,
        ProtocolNotification,
        ProtocolNotificationRx,
        rpc,
        rpc::{
            body::BodyBytes,
            message::{RpcMethod, RpcResponse},
            server::early_close::EarlyClose,
        },
    },
    stream_id::{Id, StreamId},
};

const LOG_TARGET: &str = "comms::rpc::server";

pub trait NamedProtocolService {
    const PROTOCOL_NAME: &'static [u8];

    /// Default implementation that returns a pointer to the static protocol name.
    fn as_protocol_name(&self) -> &'static [u8] {
        Self::PROTOCOL_NAME
    }
}

pub struct RpcServer {
    builder: RpcServerBuilder,
    request_tx: mpsc::Sender<RpcServerRequest>,
    request_rx: mpsc::Receiver<RpcServerRequest>,
}

impl RpcServer {
    pub fn new() -> Self {
        Self::builder().finish()
    }

    pub fn builder() -> RpcServerBuilder {
        RpcServerBuilder::new()
    }

    pub fn add_service<S>(self, service: S) -> Router<S, ProtocolServiceNotFound>
    where
        S: MakeService<
                ProtocolId,
                Request<Bytes>,
                MakeError = RpcServerError,
                Response = Response<Body>,
                Error = RpcStatus,
            > + NamedProtocolService
            + Send
            + 'static,
        S::Future: Send + 'static,
    {
        Router::new(self, service)
    }

    pub fn get_handle(&self) -> RpcServerHandle {
        RpcServerHandle::new(self.request_tx.clone())
    }

    pub(super) async fn serve<S, TCommsProvider>(
        self,
        service: S,
        notifications: ProtocolNotificationRx<Substream>,
        comms_provider: TCommsProvider,
    ) -> Result<(), RpcServerError>
    where
        S: MakeService<
                ProtocolId,
                Request<Bytes>,
                MakeError = RpcServerError,
                Response = Response<Body>,
                Error = RpcStatus,
            > + Send
            + 'static,
        S::Service: Send + 'static,
        S::Future: Send + 'static,
        S::Service: Send + 'static,
        <S::Service as Service<Request<Bytes>>>::Future: Send + 'static,
        TCommsProvider: RpcCommsProvider + Clone + Send + 'static,
    {
        PeerRpcServer::new(self.builder, service, notifications, comms_provider, self.request_rx)
            .serve()
            .await
    }
}

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

/// The default maximum time a session may sit without receiving a request before it is closed. This
/// is well above any client's polling interval, so it only ever reclaims sessions whose peer is
/// gone.
const DEFAULT_IDLE_SESSION_TIMEOUT: Duration = Duration::from_secs(10 * 60);

/// The default ceiling applied to the deadline a client asks for. The largest deadline any client
/// in this workspace sets is the 240s block-sync deadline (sized for full blocks over Tor), so this
/// leaves generous headroom for an operator who raises it while still bounding how long a single
/// request can occupy a session. It matches [`DEFAULT_IDLE_SESSION_TIMEOUT`] deliberately: a
/// request in flight should not be able to hold a session slot for longer than an idle one may sit.
const DEFAULT_MAXIMUM_CLIENT_DEADLINE: Duration = Duration::from_secs(10 * 60);

/// How long the server will spend trying to shut a substream down gracefully before dropping it.
///
/// A close is a handful of bytes, so this only ever elapses when the peer is not reading. It is
/// deliberately short and not client-controlled: unlike a request deadline there is no legitimate
/// reason for teardown to take long, and the whole point is to stop a stalled peer from holding the
/// session's executor permit.
const SESSION_CLOSE_TIMEOUT: Duration = Duration::from_secs(5);

#[derive(Clone)]
pub struct RpcServerBuilder {
    maximum_simultaneous_sessions: Option<usize>,
    maximum_sessions_per_client: Option<usize>,
    minimum_client_deadline: Duration,
    maximum_client_deadline: Duration,
    handshake_timeout: Duration,
    maximum_pending_handshakes: usize,
    maximum_pending_handshakes_per_client: usize,
    idle_session_timeout: Option<Duration>,
    cull_oldest_peer_rpc_connection_on_full: bool,
}

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

    pub fn with_maximum_simultaneous_sessions(mut self, limit: usize) -> Self {
        self.maximum_simultaneous_sessions = Some(cmp::min(limit, BoundedExecutor::max_theoretical_tasks()));
        self
    }

    pub fn with_unlimited_simultaneous_sessions(mut self) -> Self {
        self.maximum_simultaneous_sessions = None;
        self
    }

    pub fn with_maximum_sessions_per_client(mut self, limit: usize) -> Self {
        self.maximum_sessions_per_client = Some(cmp::min(limit, BoundedExecutor::max_theoretical_tasks()));
        self
    }

    pub fn with_cull_oldest_peer_rpc_connection_on_full(mut self, cull: bool) -> Self {
        self.cull_oldest_peer_rpc_connection_on_full = cull;
        self
    }

    pub fn with_unlimited_sessions_per_client(mut self) -> Self {
        self.maximum_sessions_per_client = None;
        self
    }

    pub fn with_minimum_client_deadline(mut self, deadline: Duration) -> Self {
        self.minimum_client_deadline = deadline;
        self
    }

    /// Cap the deadline a client is allowed to ask for. The deadline arrives on the wire as a u64
    /// of seconds and bounds both the service call and each message of a streaming response, so
    /// without a ceiling a peer can simply ask for `u64::MAX` and switch off the server's own
    /// timeouts for that request.
    ///
    /// The request is clamped rather than rejected outright: a peer asking for more than the
    /// ceiling is still served, and only sees a difference if the work actually runs past the
    /// ceiling - in which case it gets a prompt `Timeout` status instead of the longer wait it
    /// asked for.
    ///
    /// Note this bounds a single service call and the gap between two messages of a stream. It
    /// does not bound a stream's total duration, which is deliberate: `sync_blocks` streams an
    /// entire initial block download in one request and a total budget would cut it short.
    pub fn with_maximum_client_deadline(mut self, deadline: Duration) -> Self {
        self.maximum_client_deadline = deadline;
        self
    }

    /// The deadline this server will actually apply for a client that asked for `requested`.
    ///
    /// Floored at `minimum_client_deadline` so that a `maximum_client_deadline` misconfigured below
    /// it cannot silently reduce every request's deadline to nothing - which would leave the node
    /// up but aborting every RPC call it is asked to serve.
    fn clamp_client_deadline(&self, requested: Duration) -> Duration {
        let capped = cmp::min(requested, self.maximum_client_deadline);
        cmp::max(capped, self.minimum_client_deadline)
    }

    /// Limit the handshakes in progress at once across all peers. Handshakes run off the accept loop, so a peer that
    /// opens a substream and never sends its handshake only holds one of these until the handshake times out. A new
    /// substream over the limit is dropped without a reply.
    pub fn with_maximum_pending_handshakes(mut self, limit: usize) -> Self {
        self.maximum_pending_handshakes = limit;
        self
    }

    /// Limit the handshakes in progress at once for a single peer. A new substream over the limit is dropped without a
    /// reply.
    pub fn with_maximum_pending_handshakes_per_client(mut self, limit: usize) -> Self {
        self.maximum_pending_handshakes_per_client = limit;
        self
    }

    /// How long a client has to complete the handshake on a new substream
    pub fn with_handshake_timeout(mut self, timeout: Duration) -> Self {
        self.handshake_timeout = timeout;
        self
    }

    /// Close a session that has not received a request for `timeout`. A session holds a slot in the
    /// global session limit for as long as its substream is open, and a peer that goes away without
    /// closing the substream (common over Tor) never releases it. Only idle time counts; a session
    /// servicing requests is never closed.
    pub fn with_idle_session_timeout(mut self, timeout: Duration) -> Self {
        self.idle_session_timeout = Some(timeout);
        self
    }

    /// Allow sessions to stay open indefinitely while idle. See [`Self::with_idle_session_timeout`]
    /// for why this is generally a bad idea outside of tests.
    pub fn with_no_idle_session_timeout(mut self) -> Self {
        self.idle_session_timeout = None;
        self
    }

    pub fn finish(self) -> RpcServer {
        let (request_tx, request_rx) = mpsc::channel(10);
        RpcServer {
            builder: self,
            request_tx,
            request_rx,
        }
    }
}

/// The default limit on RPC sessions open at once across all peers. Each session may have one request decoding at a
/// time, so this bounds how much decode memory the server can be made to hold. Use
/// [RpcServerBuilder::with_unlimited_simultaneous_sessions] to opt out.
const DEFAULT_MAXIMUM_SIMULTANEOUS_SESSIONS: usize = 100;

/// The default limit on RPC sessions a single peer may have open at once. Use
/// [RpcServerBuilder::with_unlimited_sessions_per_client] to opt out.
const DEFAULT_MAXIMUM_SESSIONS_PER_CLIENT: usize = 10;

/// The default time a client has to send its handshake, and the server to send its reply. A handshake is two frames of
/// a few bytes, but negotiation over a congested Tor circuit can cost a round trip; this also bounds how long a
/// substream that never sends one holds a pending-handshake slot.
const DEFAULT_HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(10);

/// The default limit on handshakes in progress at once, across all peers
const DEFAULT_MAXIMUM_PENDING_HANDSHAKES: usize = 32;

/// The default limit on handshakes in progress at once for a single peer. One server serves several protocols, and a
/// client may open a few sessions within one round trip (e.g. discovery, sync on connect, rebootstrap and wallet
/// pools), so a peer with more than this many unfinished handshakes is not an ordinary client.
const DEFAULT_MAXIMUM_PENDING_HANDSHAKES_PER_CLIENT: usize = 4;

/// The shortest interval between two warnings about the same kind of handshake problem
const HANDSHAKE_WARNING_INTERVAL: Duration = Duration::from_secs(60);

impl Default for RpcServerBuilder {
    fn default() -> Self {
        Self {
            maximum_simultaneous_sessions: Some(DEFAULT_MAXIMUM_SIMULTANEOUS_SESSIONS),
            maximum_sessions_per_client: Some(DEFAULT_MAXIMUM_SESSIONS_PER_CLIENT),
            minimum_client_deadline: Duration::from_secs(1),
            maximum_client_deadline: DEFAULT_MAXIMUM_CLIENT_DEADLINE,
            handshake_timeout: DEFAULT_HANDSHAKE_TIMEOUT,
            maximum_pending_handshakes: DEFAULT_MAXIMUM_PENDING_HANDSHAKES,
            maximum_pending_handshakes_per_client: DEFAULT_MAXIMUM_PENDING_HANDSHAKES_PER_CLIENT,
            idle_session_timeout: Some(DEFAULT_IDLE_SESSION_TIMEOUT),
            cull_oldest_peer_rpc_connection_on_full: false,
        }
    }
}

pub(super) struct PeerRpcServer<TSvc, TCommsProvider>
where TSvc: MakeService<ProtocolId, Request<Bytes>>
{
    executor: BoundedExecutor,
    config: RpcServerBuilder,
    service: TSvc,
    protocol_notifications: Option<ProtocolNotificationRx<Substream>>,
    comms_provider: TCommsProvider,
    request_rx: mpsc::Receiver<RpcServerRequest>,
    sessions: HashMap<NodeId, Vec<SessionInfo>>,
    tasks: FuturesUnordered<JoinHandle<(NodeId, Id)>>,
    /// Handshakes in progress. Dropping the set (when `serve` returns) aborts them, so none outlives the server.
    handshakes: JoinSet<HandshakeOutcome<<TSvc as MakeService<ProtocolId, Request<Bytes>>>::Service>>,
    /// The peer of each handshake task, and whether the task will accept the session, so its pending slot and session
    /// reservation are released however the task ends
    handshake_peers: HashMap<tokio::task::Id, (NodeId, bool)>,
    /// Pending handshakes per peer
    pending_handshakes: HashMap<NodeId, usize>,
    /// Handshakes per peer that will accept the session if the client completes them. Each holds a reservation
    /// against the per-client session limit, so an accepted handshake is never refused a session afterwards.
    accepted_pending: HashMap<NodeId, usize>,
    /// All handshakes that will accept the session, each holding a reservation against the global session limit
    accepted_pending_total: usize,
    /// Replies to substreams over a pending-handshake limit. Bounded like `handshakes`, and aborted with the server.
    capacity_rejections: JoinSet<()>,
    capacity_warning: RateLimitedWarning,
    timeout_warning: RateLimitedWarning,
    session_limit_warning: RateLimitedWarning,
}

/// Lets a warning through at most once per [HANDSHAKE_WARNING_INTERVAL], counting the ones it holds back
#[derive(Default)]
struct RateLimitedWarning {
    last: Option<Instant>,
    suppressed: usize,
}

impl RateLimitedWarning {
    /// Returns the number of occurrences since the last warning if one should be logged now
    fn occurred(&mut self) -> Option<usize> {
        let now = Instant::now();
        if self
            .last
            .is_some_and(|last| now.saturating_duration_since(last) < HANDSHAKE_WARNING_INTERVAL)
        {
            self.suppressed = self.suppressed.saturating_add(1);
            return None;
        }
        self.last = Some(now);
        let count = self.suppressed.saturating_add(1);
        self.suppressed = 0;
        Some(count)
    }
}

/// What a handshake task returns to the accept loop
struct HandshakeOutcome<S> {
    protocol: ProtocolId,
    node_id: NodeId,
    /// The service, the substream and the negotiated version, ready to start a session (the acceptance is not sent
    /// yet); or why the session was not started
    result: Result<(S, CanonicalFraming<Substream>, u32), RpcServerError>,
}

/// What the accept loop decided before handing a substream to a handshake task
enum SessionDecision<S> {
    /// Perform the handshake, then start a session with this service
    Accept(S),
    /// Reply with this rejection, and report this error
    Reject(HandshakeRejectReason, RpcServerError),
}

struct SessionInfo {
    pub(crate) peer_watch: tokio::sync::watch::Sender<()>,
    pub(crate) stream_id: Id,
}

impl<TSvc, TCommsProvider> PeerRpcServer<TSvc, TCommsProvider>
where
    TSvc: MakeService<
            ProtocolId,
            Request<Bytes>,
            MakeError = RpcServerError,
            Response = Response<Body>,
            Error = RpcStatus,
        > + Send
        + 'static,
    TSvc::Service: Send + 'static,
    <TSvc::Service as Service<Request<Bytes>>>::Future: Send + 'static,
    TSvc::Future: Send + 'static,
    TCommsProvider: RpcCommsProvider + Clone + Send + 'static,
{
    fn new(
        config: RpcServerBuilder,
        service: TSvc,
        protocol_notifications: ProtocolNotificationRx<Substream>,
        comms_provider: TCommsProvider,
        request_rx: mpsc::Receiver<RpcServerRequest>,
    ) -> Self {
        Self {
            executor: match config.maximum_simultaneous_sessions {
                Some(usize::MAX) => BoundedExecutor::allow_maximum(),
                Some(num) => BoundedExecutor::new(num),
                None => BoundedExecutor::allow_maximum(),
            },
            config,
            service,
            protocol_notifications: Some(protocol_notifications),
            comms_provider,
            request_rx,
            sessions: HashMap::new(),
            tasks: FuturesUnordered::new(),
            handshakes: JoinSet::new(),
            handshake_peers: HashMap::new(),
            pending_handshakes: HashMap::new(),
            accepted_pending: HashMap::new(),
            accepted_pending_total: 0,
            capacity_rejections: JoinSet::new(),
            capacity_warning: RateLimitedWarning::default(),
            timeout_warning: RateLimitedWarning::default(),
            session_limit_warning: RateLimitedWarning::default(),
        }
    }

    pub async fn serve(mut self) -> Result<(), RpcServerError> {
        let mut protocol_notifs = self
            .protocol_notifications
            .take()
            .expect("PeerRpcServer initialized without protocol_notifications");

        loop {
            tokio::select! {
                maybe_notif = protocol_notifs.recv() => {
                    match maybe_notif {
                        Some(notif) => self.handle_protocol_notification(notif).await?,
                        // No more protocol notifications to come, so we're done
                        None => break,
                    }
                }

                Some(Ok((node_id, stream_id))) = self.tasks.next() => {
                    self.on_session_complete(&node_id, stream_id);
                },

                Some(joined) = self.handshakes.join_next_with_id() => {
                    self.on_handshake_finished(joined);
                },

                Some(_) = self.capacity_rejections.join_next() => {},

                Some(req) = self.request_rx.recv() => {
                     self.handle_request(req).await;
                },
            }
        }

        debug!(
            target: LOG_TARGET,
            "Peer RPC server is shut down because the protocol notification stream ended"
        );

        Ok(())
    }

    async fn handle_request(&mut self, req: RpcServerRequest) {
        #[allow(clippy::enum_glob_use)]
        use RpcServerRequest::*;
        match req {
            GetNumActiveSessions(reply) => {
                let max_sessions = self
                    .config
                    .maximum_simultaneous_sessions
                    .unwrap_or_else(BoundedExecutor::max_theoretical_tasks);
                let num_active = max_sessions.saturating_sub(self.executor.num_available());
                let _ = reply.send(num_active);
            },
            GetNumActiveSessionsForPeer(node_id, reply) => {
                let num_active = self.sessions.get(&node_id).map(|v| v.len()).unwrap_or(0);
                let _ = reply.send(num_active);
            },
            CloseAllSessionsForPeer(node_id, reply) => {
                let num_closed = self.close_all_sessions(&node_id);
                let _ = reply.send(num_closed);
            },
        }
    }

    async fn handle_protocol_notification(
        &mut self,
        notification: ProtocolNotification<Substream>,
    ) -> Result<(), RpcServerError> {
        match notification.event {
            ProtocolEvent::NewInboundSubstream(node_id, substream) => {
                trace!(
                    target: LOG_TARGET,
                    "New client connection for protocol `{}` from peer `{}`",
                    String::from_utf8_lossy(&notification.protocol),
                    node_id
                );

                let framed = framing::canonical(substream, RPC_MAX_FRAME_SIZE);

                // Over either pending-handshake limit the session is refused with an explicit rejection, which a
                // client pool treats as "use an existing session" rather than a failure to connect
                let pending_for_peer = self.pending_handshakes.get(&node_id).copied().unwrap_or(0);
                if pending_for_peer >= self.config.maximum_pending_handshakes_per_client {
                    trace!(
                        target: LOG_TARGET,
                        "Rejecting RPC substream from peer `{}`: it already has {} handshake(s) in progress",
                        node_id,
                        pending_for_peer
                    );
                    self.reject_at_capacity(&notification.protocol, framed);
                    return Ok(());
                }
                if self.handshakes.len() >= self.config.maximum_pending_handshakes {
                    trace!(
                        target: LOG_TARGET,
                        "Rejecting RPC substream from peer `{}`: {} handshake(s) already in progress",
                        node_id,
                        self.handshakes.len()
                    );
                    self.reject_at_capacity(&notification.protocol, framed);
                    return Ok(());
                }

                let decision = self.decide_session(notification.protocol.clone(), &node_id).await;
                self.spawn_handshake(notification.protocol, node_id, framed, decision);
            },
        }

        Ok(())
    }

    /// Refuses a substream over a pending-handshake limit with an explicit rejection, sent from a short-lived task so
    /// the accept loop never waits on the peer. The replies are bounded like the handshakes themselves: past that,
    /// the substream is dropped without one.
    fn reject_at_capacity(&mut self, protocol: &ProtocolId, mut framed: CanonicalFraming<Substream>) {
        #[cfg(feature = "metrics")]
        metrics::handshake_capacity_rejection_counter(protocol).inc();
        #[cfg(not(feature = "metrics"))]
        let _ = protocol;
        if let Some(count) = self.capacity_warning.occurred() {
            warn!(
                target: LOG_TARGET,
                "Refused {count} RPC substream(s) because too many handshakes were in progress (at most {} per peer, \
                 {} in total)",
                self.config.maximum_pending_handshakes_per_client,
                self.config.maximum_pending_handshakes
            );
        }
        if self.capacity_rejections.len() >= self.config.maximum_pending_handshakes {
            return;
        }
        let timeout = self.config.handshake_timeout;
        self.capacity_rejections.spawn(async move {
            // Best effort: the session is refused either way
            let _result = Handshake::new(&mut framed)
                .with_timeout(timeout)
                .reject_with_reason(HandshakeRejectReason::NoServerSessionsAvailable(
                    "Too many handshakes in progress",
                ))
                .await;
        });
    }

    /// Checks whether a session for `protocol` with `node_id` can be started, before any handshake I/O
    async fn decide_session(&mut self, protocol: ProtocolId, node_id: &NodeId) -> SessionDecision<TSvc::Service> {
        let service = match self.service.make_service(protocol).await {
            Ok(service) => service,
            Err(err) => {
                trace!(
                    target: LOG_TARGET,
                    "Rejecting RPC session request for peer `{}` because {}",
                    node_id,
                    HandshakeRejectReason::ProtocolNotSupported
                );
                return SessionDecision::Reject(HandshakeRejectReason::ProtocolNotSupported, err);
            },
        };

        // The per-peer limit is enforced before the global one so that culling still happens when
        // the server is sitting on its global limit - that is precisely when reclaiming a slot from
        // a peer that is over its own quota matters. Doing the global check first made
        // `cull_oldest_peer_rpc_connection_on_full` unreachable exactly when it was needed.
        //
        // Both limits count the handshakes already accepted (reserved) as well as the running sessions: an accepted
        // handshake must never be refused a session afterwards, because the client would only see the substream close
        // on its first request (`ServerClosedRequest`, which it blames on the server) instead of an explicit rejection.
        let accepted_for_peer = self.accepted_pending.get(node_id).copied().unwrap_or(0);
        if let Err(err) = self.new_session_possible_for(node_id, accepted_for_peer) {
            return SessionDecision::Reject(
                HandshakeRejectReason::NoServerSessionsAvailable("Maximum sessions for client"),
                err,
            );
        }

        if self.executor.num_available() <= self.accepted_pending_total {
            let msg = format!("Used all {} sessions", self.executor.max_available());
            trace!(
                target: LOG_TARGET,
                "Rejecting RPC session request for peer `{}` because {}",
                node_id,
                HandshakeRejectReason::NoServerSessionsAvailable("Cannot spawn more sessions")
            );
            // A session culled above only releases its slot once that task winds down, so this
            // request is still rejected. The client's next attempt is the one that gets in.
            return SessionDecision::Reject(
                HandshakeRejectReason::NoServerSessionsAvailable("Cannot spawn more sessions"),
                RpcServerError::MaximumSessionsReached(msg),
            );
        }

        SessionDecision::Accept(service)
    }

    /// Performs the handshake (or sends the rejection) on its own task, so a slow or silent client cannot stall the
    /// accept loop. The task is bounded by the handshake timeout, and holds a pending-handshake slot until it ends.
    fn spawn_handshake(
        &mut self,
        protocol: ProtocolId,
        node_id: NodeId,
        mut framed: CanonicalFraming<Substream>,
        decision: SessionDecision<TSvc::Service>,
    ) {
        let timeout = self.config.handshake_timeout;
        let task_node_id = node_id.clone();
        let accepted = matches!(decision, SessionDecision::Accept(_));
        let handle = self.handshakes.spawn(async move {
            let result = match decision {
                SessionDecision::Reject(reason, err) => {
                    // Best effort: the session is refused either way
                    let _result = Handshake::new(&mut framed)
                        .with_timeout(timeout)
                        .reject_with_reason(reason)
                        .await;
                    Err(err)
                },
                SessionDecision::Accept(service) => {
                    // Only the client's half of the handshake happens here. The acceptance is sent by the session task
                    // once the session is registered (see `start_session`), so a client never learns it was accepted
                    // before the server counts its session, and an accepted session is never dropped afterwards.
                    match Handshake::new(&mut framed)
                        .with_timeout(timeout)
                        .receive_client_handshake()
                        .await
                    {
                        Ok(version) => {
                            debug!(
                                target: LOG_TARGET,
                                "Server negotiated RPC v{version} with client node `{task_node_id}`"
                            );
                            Ok((service, framed, version))
                        },
                        Err(err) => Err(err.into()),
                    }
                },
            };
            HandshakeOutcome {
                protocol,
                node_id: task_node_id,
                result,
            }
        });
        self.handshake_peers.insert(handle.id(), (node_id.clone(), accepted));
        if accepted {
            let reserved = self.accepted_pending.entry(node_id.clone()).or_insert(0);
            *reserved = reserved.saturating_add(1);
            self.accepted_pending_total = self.accepted_pending_total.saturating_add(1);
        }
        let pending = self.pending_handshakes.entry(node_id).or_insert(0);
        *pending = pending.saturating_add(1);
    }

    /// Releases the pending-handshake slot of a finished handshake task, and starts the session if it succeeded
    fn on_handshake_finished(
        &mut self,
        joined: Result<(tokio::task::Id, HandshakeOutcome<TSvc::Service>), tokio::task::JoinError>,
    ) {
        let task_id = match &joined {
            Ok((id, _)) => *id,
            Err(err) => err.id(),
        };
        if let Some((node_id, accepted)) = self.handshake_peers.remove(&task_id) {
            decrement(&mut self.pending_handshakes, &node_id);
            if accepted {
                decrement(&mut self.accepted_pending, &node_id);
                self.accepted_pending_total = self.accepted_pending_total.saturating_sub(1);
            }
        }

        let outcome = match joined {
            Ok((_, outcome)) => outcome,
            Err(err) => {
                error!(target: LOG_TARGET, "RPC handshake task failed: {err}");
                return;
            },
        };
        let HandshakeOutcome {
            protocol,
            node_id,
            result,
        } = outcome;
        let (service, framed, version) = match result {
            Ok(accepted) => accepted,
            Err(err) => {
                self.log_handshake_failure(&protocol, &err);
                return;
            },
        };
        // The session was reserved when the handshake was accepted, so this cannot fail unless that bookkeeping is
        // wrong. The substream is dropped if it does.
        if let Err(err) = self.start_session(protocol, &node_id, service, framed, version) {
            warn!(
                target: LOG_TARGET,
                "BUG: an accepted RPC handshake from peer `{node_id}` could not start its session: {err}"
            );
        }
    }

    fn log_handshake_failure(&mut self, protocol: &ProtocolId, err: &RpcServerError) {
        #[cfg(not(feature = "metrics"))]
        let _ = protocol;
        if matches!(err, RpcServerError::HandshakeError(rpc::RpcHandshakeError::TimedOut)) {
            #[cfg(feature = "metrics")]
            metrics::handshake_timeout_counter(protocol).inc();
            if let Some(count) = self.timeout_warning.occurred() {
                warn!(
                    target: LOG_TARGET,
                    "{count} RPC handshake(s) timed out after {:.0?} without a handshake from the client",
                    self.config.handshake_timeout
                );
            }
        }
        match err {
            err @ RpcServerError::HandshakeError(_) => {
                trace!(target: LOG_TARGET, "Handshake error: {}", err);
                #[cfg(feature = "metrics")]
                metrics::handshake_error_counter(protocol).inc();
            },
            err => {
                trace!(target: LOG_TARGET, "Unable to spawn RPC service: {}", err);
            },
        }
    }

    /// Checks that `node_id` can have one more session, counting its running sessions and the `reserved` sessions of
    /// its accepted handshakes. In cull mode, culls its oldest running sessions to make room, but only when that is
    /// enough: a request is never refused after sessions were culled for it. Returns the peer's running sessions.
    fn new_session_possible_for(&mut self, node_id: &NodeId, reserved: usize) -> Result<usize, RpcServerError> {
        let max = match self.config.maximum_sessions_per_client {
            Some(max) if max > 0 => max,
            Some(_) | None => return Ok(self.sessions.get(node_id).map_or(0, Vec::len)),
        };
        let running = self.sessions.get(node_id).map_or(0, Vec::len);
        let in_use = running.saturating_add(reserved);
        if in_use < max {
            return Ok(running);
        }
        // Reservations cannot be culled, so culling only helps if they leave room for this request
        if self.config.cull_oldest_peer_rpc_connection_on_full && reserved < max {
            let num_to_remove = in_use.saturating_sub(max).saturating_add(1);
            if let Some(session_info) = self.sessions.get_mut(node_id) {
                for _ in 0..num_to_remove {
                    let info = session_info.remove(0);
                    info!(target: LOG_TARGET, "Culling oldest RPC session for peer `{node_id}`");
                    let _ = info.peer_watch.send(());
                }
                let running = session_info.len();
                if session_info.is_empty() {
                    self.sessions.remove(node_id);
                }
                return Ok(running);
            }
        }
        trace!(
            target: LOG_TARGET,
            "Maximum RPC sessions for peer {node_id} met or exceeded. Max: {max}, running: {running}, accepted \
             handshakes: {reserved}"
        );
        if let Some(count) = self.session_limit_warning.occurred() {
            warn!(
                target: LOG_TARGET,
                "Refused {count} RPC session(s) because the peer was at its limit of {max} session(s)"
            );
        }
        Err(RpcServerError::MaxSessionsPerClientReached {
            node_id: node_id.clone(),
            max_sessions: max,
        })
    }

    fn close_all_sessions(&mut self, node_id: &NodeId) -> usize {
        let mut count = 0usize;
        if let Some(session_info) = self.sessions.get_mut(node_id) {
            for info in session_info.iter_mut() {
                count = count.saturating_add(1);
                info!(target: LOG_TARGET, "Closing RPC session {} for peer `{}`", info.stream_id, node_id);
                let _ = info.peer_watch.send(());
            }
            self.sessions.remove(node_id);
        }
        count
    }

    fn on_session_complete(&mut self, node_id: &NodeId, stream_id: Id) {
        if let Some(session_info) = self.sessions.get_mut(node_id) {
            if let Some(info) = session_info.iter_mut().find(|info| info.stream_id == stream_id) {
                info!(target: LOG_TARGET, "Session complete for {node_id} (stream id {stream_id})");
                let _ = info.peer_watch.send(());
            };
            session_info.retain(|info| info.stream_id != stream_id);
            if session_info.is_empty() {
                self.sessions.remove(node_id);
            }
        }
    }

    /// Registers and starts a session on a substream whose client handshake was received with `version`. Its session
    /// was reserved when the handshake was accepted; the limits are checked again only as a defensive invariant (if
    /// that fails, the substream is dropped before the client was told it was accepted).
    ///
    /// The session task sends the acceptance as its first action. The session is registered here, on the accept loop,
    /// before that task is spawned, so by the time the client is told it was accepted the session is already counted.
    /// If the acceptance cannot be sent within the handshake timeout, the session ends at once and releases its slot.
    fn start_session(
        &mut self,
        protocol: ProtocolId,
        node_id: &NodeId,
        service: TSvc::Service,
        mut framed: CanonicalFraming<Substream>,
        version: u32,
    ) -> Result<(), RpcServerError> {
        // Its reservation was released when the handshake finished
        let num_sessions = self.new_session_possible_for(node_id, 0)?;
        if !self.executor.can_spawn() {
            return Err(RpcServerError::MaximumSessionsReached(format!(
                "Used all {} sessions",
                self.executor.max_available()
            )));
        }

        info!(
            target: LOG_TARGET,
            "NEW SESSION for {node_id} ({num_sessions} currently active) "
        );

        let stream_id = framed.stream_id();
        let (stop_tx, stop_rx) = tokio::sync::watch::channel(());
        let config = self.config.clone();
        let comms_provider = self.comms_provider.clone();
        let session_node_id = node_id.clone();

        let node_id_clone = node_id.clone();
        let handle = self
            .executor
            .try_spawn(async move {
                if let Err(err) = Handshake::new(&mut framed)
                    .with_timeout(config.handshake_timeout)
                    .accept_version(version)
                    .await
                {
                    debug!(
                        target: LOG_TARGET,
                        "Could not send the RPC handshake acceptance to `{node_id_clone}`; ending the session: {err}"
                    );
                    return (node_id_clone, stream_id);
                }
                let service = ActivePeerRpcService::new(
                    config,
                    protocol,
                    session_node_id,
                    service,
                    framed,
                    comms_provider,
                    stop_rx,
                );
                #[cfg(feature = "metrics")]
                let num_sessions = metrics::num_sessions(&service.protocol);
                #[cfg(feature = "metrics")]
                num_sessions.inc();
                service.start().await;
                info!(target: LOG_TARGET, "END OF SESSION for {node_id_clone} ");
                #[cfg(feature = "metrics")]
                num_sessions.dec();

                (node_id_clone, stream_id)
            })
            .map_err(|e| RpcServerError::MaximumSessionsReached(format!("{e:?}")))?;

        self.tasks.push(handle);
        let mut peer_stop = vec![SessionInfo {
            peer_watch: stop_tx,
            stream_id,
        }];
        self.sessions
            .entry(node_id.clone())
            .and_modify(|entry| entry.append(&mut peer_stop))
            .or_insert(peer_stop);
        if let Some(info) = self.sessions.get(&node_id.clone()) {
            info!(
                target: LOG_TARGET,
                "NEW SESSION created for {} ({} active) ", node_id.clone(), info.len()
            );
            // Warn if `stream_id` is already in use
            if info.iter().filter(|session| session.stream_id == stream_id).count() > 1 {
                warn!(
                    target: LOG_TARGET,
                    "Stream ID {stream_id} already in use for peer {node_id}. This should not happen."
                );
            }
        }

        Ok(())
    }
}

/// Decrements `node_id`'s count, removing it at zero
fn decrement(counts: &mut HashMap<NodeId, usize>, node_id: &NodeId) {
    if let Some(count) = counts.get_mut(node_id) {
        *count = count.saturating_sub(1);
        if *count == 0 {
            counts.remove(node_id);
        }
    }
}

struct ActivePeerRpcService<TSvc, TCommsProvider> {
    config: RpcServerBuilder,
    protocol: ProtocolId,
    node_id: NodeId,
    service: TSvc,
    framed: EarlyClose<CanonicalFraming<Substream>>,
    comms_provider: TCommsProvider,
    logging_context_string: Arc<String>,
    stop_rx: tokio::sync::watch::Receiver<()>,
    /// Looked up once per session rather than for every streamed message.
    #[cfg(feature = "metrics")]
    outbound_response_bytes: tari_metrics::Histogram,
}

impl<TSvc, TCommsProvider> ActivePeerRpcService<TSvc, TCommsProvider>
where
    TSvc: Service<Request<Bytes>, Response = Response<Body>, Error = RpcStatus>,
    TCommsProvider: RpcCommsProvider + Send + Clone + 'static,
{
    pub(self) fn new(
        config: RpcServerBuilder,
        protocol: ProtocolId,
        node_id: NodeId,
        service: TSvc,
        framed: CanonicalFraming<Substream>,
        comms_provider: TCommsProvider,
        stop_rx: tokio::sync::watch::Receiver<()>,
    ) -> Self {
        Self {
            logging_context_string: Arc::new(format!(
                "stream_id: {}, peer: {}, protocol: {}, stream_id: {}",
                framed.stream_id(),
                node_id,
                framed.stream_id(),
                String::from_utf8_lossy(&protocol)
            )),

            #[cfg(feature = "metrics")]
            outbound_response_bytes: metrics::outbound_response_bytes(&protocol),
            config,
            protocol,
            node_id,
            service,
            framed: EarlyClose::new(framed),
            comms_provider,
            stop_rx,
        }
    }

    async fn start(mut self) {
        debug!(
            target: LOG_TARGET,
            "({}) Rpc server started.", self.logging_context_string,
        );
        if let Err(err) = self.run().await {
            #[cfg(feature = "metrics")]
            metrics::error_counter(&self.protocol, &err).inc();
            let level = match &err {
                RpcServerError::Io(e) => err_to_log_level(e),
                RpcServerError::EarlyClose(e) => e.io().map(err_to_log_level).unwrap_or(log::Level::Error),
                _ => log::Level::Error,
            };
            log!(
                target: LOG_TARGET,
                level,
                "({}) Rpc server exited with an error: {}",
                self.logging_context_string,
                err
            );
        }
    }

    /// Write one frame to the peer, giving up after `deadline`.
    ///
    /// Every write here goes into a yamux window the peer controls, so a peer that simply stops
    /// reading can park any unbounded `send`. Route all of them through this so the bound is
    /// structural rather than something to remember at each call site.
    async fn send_with_deadline(&mut self, msg: Bytes, deadline: Duration) -> Result<(), RpcServerError> {
        match time::timeout(deadline, self.framed.send(msg)).await {
            Ok(result) => result.map_err(Into::into),
            Err(_elapsed) => {
                debug!(
                    target: LOG_TARGET,
                    "({}) Peer did not accept a response within the deadline ({:.0?}). Aborting the session.",
                    self.logging_context_string,
                    deadline
                );

                #[cfg(feature = "metrics")]
                metrics::error_counter(&self.protocol, &RpcServerError::WriteStreamExceededDeadline).inc();
                Err(RpcServerError::WriteStreamExceededDeadline)
            },
        }
    }

    /// Shut the substream down, giving up after [`SESSION_CLOSE_TIMEOUT`] and letting it drop.
    ///
    /// `EarlyClose::poll_close` falls through to `check_for_early_close`, which returns `Pending`
    /// for a peer that is merely silent - so closing a substream whose window the peer has filled
    /// and stopped draining never completes. That is worse than one stuck task: the session holds a
    /// `BoundedExecutor` permit until `start()` returns, and nothing can reclaim it. The idle timer
    /// is not running (we are not in the select), and culling only signals `stop_rx`, which this
    /// task is no longer watching. One peer could walk the global session limit down to zero and
    /// lock every other peer out of the node.
    async fn close_framed(&mut self) -> Result<(), RpcServerError> {
        match time::timeout(SESSION_CLOSE_TIMEOUT, self.framed.close()).await {
            Ok(result) => result.map_err(Into::into),
            Err(_elapsed) => {
                debug!(
                    target: LOG_TARGET,
                    "({}) Peer did not accept the substream close within {:.0?}. Dropping it.",
                    self.logging_context_string,
                    SESSION_CLOSE_TIMEOUT
                );
                Ok(())
            },
        }
    }

    async fn run(&mut self) -> Result<(), RpcServerError> {
        // Only the time spent waiting for the next request is bounded - handling a request holds
        // this loop and cannot trip the timer. Without this, a session (and the slot it occupies in
        // the global session limit) is held for as long as the substream stays open, which for a
        // peer that has gone away without closing it is forever.
        let idle_timeout = self.config.idle_session_timeout;
        let idle_sleep = time::sleep(idle_timeout.unwrap_or(DEFAULT_IDLE_SESSION_TIMEOUT));
        tokio::pin!(idle_sleep);

        loop {
            tokio::select! {
                _ = self.stop_rx.changed() => {
                    debug!(target: LOG_TARGET, "({}) Stop signal received, closing substream.", self.logging_context_string);
                    break;
                }
                () = &mut idle_sleep, if idle_timeout.is_some() => {
                    debug!(
                        target: LOG_TARGET,
                        "({}) Closing session idle for {:.0?}.",
                        self.logging_context_string,
                        idle_timeout.unwrap_or_default()
                    );
                    break;
                }
                result = self.framed.next() => {
                    match result {
                        Some(Ok(frame)) => {
                            #[cfg(feature = "metrics")]
                            metrics::inbound_requests_bytes(&self.protocol).observe(frame.len() as f64);

                            let start = Instant::now();

                            if let Err(err) = self.handle_request(frame.freeze()).await {
                                // A write deadline means the peer stopped reading, so the window is
                                // already full and a graceful close cannot land - skip straight to
                                // dropping the substream rather than burning the close timeout.
                                let close_result = if matches!(err, RpcServerError::WriteStreamExceededDeadline) {
                                    Ok(())
                                } else {
                                    self.close_framed().await
                                };
                                if let Err(err) = close_result {
                                    let level = err.early_close_io().map(err_to_log_level).unwrap_or(log::Level::Error);

                                    log!(
                                        target: LOG_TARGET,
                                        level,
                                        "({}) Failed to close substream after socket error: {}",
                                        self.logging_context_string,
                                        err,
                                    );
                                }
                                let level = err.early_close_io().map(err_to_log_level).unwrap_or(log::Level::Error);
                                log!(
                                    target: LOG_TARGET,
                                    level,
                                    "(peer: {}, protocol: {}) Failed to handle request: {}",
                                    self.node_id,
                                    self.protocol_name(),
                                    err
                                );
                                return Err(err);
                            }
                            let elapsed = start.elapsed();
                            trace!(
                                target: LOG_TARGET,
                                "({}) RPC request completed in {:.0?}{}",
                                self.logging_context_string,
                                elapsed,
                                if elapsed.as_secs() > 5 { " (LONG REQUEST)" } else { "" }
                            );
                            if let Some(timeout) = idle_timeout {
                                idle_sleep.as_mut().reset(
                                    time::Instant::now().checked_add(timeout).unwrap_or_else(time::Instant::now),
                                );
                            }
                        },
                        Some(Err(err)) => {
                            if let Err(err) = self.close_framed().await {
                                error!(
                                    target: LOG_TARGET,
                                    "({}) Failed to close substream after socket error: {}", self.logging_context_string, err
                                );
                            }
                            return Err(err.into());
                        },
                        None => break,
                    }
                }
            }
        }

        self.close_framed().await?;
        Ok(())
    }

    #[allow(clippy::too_many_lines)]
    #[instrument(name = "rpc::server::handle_req", level="trace", skip(self, request), err, fields(request_size = request.len ()))]
    async fn handle_request(&mut self, mut request: Bytes) -> Result<(), RpcServerError> {
        if let Some(rejection) = reject_oversized_request(&request) {
            debug!(
                target: LOG_TARGET,
                "({}) Rejecting a {} byte request, larger than the maximum of {} bytes",
                self.logging_context_string,
                request.len(),
                rpc::max_request_size()
            );
            #[cfg(feature = "metrics")]
            metrics::status_error_counter(&self.protocol, super::RpcStatusCode::BadRequest).inc();
            self.send_with_deadline(rejection.to_encoded_bytes().into(), SESSION_CLOSE_TIMEOUT)
                .await?;
            return Ok(());
        }
        let decoded_msg = proto::rpc::RpcRequest::decode(&mut request)?;

        let request_id = decoded_msg.request_id;
        let method = RpcMethod::from(decoded_msg.method);
        let requested_deadline = Duration::from_secs(decoded_msg.deadline);

        // The client side deadline MUST be greater or equal to the minimum_client_deadline
        if requested_deadline < self.config.minimum_client_deadline {
            debug!(
                target: LOG_TARGET,
                "({}) Client has an invalid deadline. {}", self.logging_context_string, decoded_msg
            );
            // Let the client know that they have disobeyed the spec
            let status = RpcStatus::bad_request(&format!(
                "Invalid deadline ({:.0?}). The deadline MUST be greater than {:.0?}.",
                requested_deadline, self.config.minimum_client_deadline,
            ));
            let bad_request = proto::rpc::RpcResponse {
                request_id,
                status: status.as_code(),
                flags: RpcMessageFlags::FIN.bits().into(),
                payload: status.to_details_bytes(),
            };
            #[cfg(feature = "metrics")]
            metrics::status_error_counter(&self.protocol, status.as_status_code()).inc();
            // The client's own deadline was rejected as invalid, so there is nothing to honour
            // here - bound this on the teardown timeout instead.
            self.send_with_deadline(bad_request.to_encoded_bytes().into(), SESSION_CLOSE_TIMEOUT)
                .await?;
            return Ok(());
        }

        // ...and it is capped from above, because the deadline governs both the service call and
        // each message of a streaming response. An uncapped `u64` of seconds lets a peer disable
        // those timeouts outright and hold a session slot for as long as it likes. Clamp instead of
        // rejecting so a peer configured above our ceiling still works.
        let deadline = self.config.clamp_client_deadline(requested_deadline);
        if deadline < requested_deadline {
            debug!(
                target: LOG_TARGET,
                "({}) Client requested a deadline of {:.0?}, capping it at {:.0?}.",
                self.logging_context_string,
                requested_deadline,
                deadline
            );
        }

        let msg_flags = RpcMessageFlags::from_bits(u8::try_from(decoded_msg.flags).map_err(|_| {
            RpcServerError::ProtocolError(format!("invalid message flag: must be less than {}", u8::MAX))
        })?)
        .ok_or(RpcServerError::ProtocolError(format!(
            "invalid message flag, does not match any flags ({})",
            decoded_msg.flags
        )))?;

        if msg_flags.contains(RpcMessageFlags::FIN) {
            debug!(target: LOG_TARGET, "({}) Client sent FIN.", self.logging_context_string);
            return Ok(());
        }
        if msg_flags.contains(RpcMessageFlags::ACK) {
            debug!(
                target: LOG_TARGET,
                "({}) sending ACK response.", self.logging_context_string
            );
            let ack = proto::rpc::RpcResponse {
                request_id,
                status: RpcStatus::ok().as_code(),
                flags: RpcMessageFlags::ACK.bits().into(),
                ..Default::default()
            };
            self.send_with_deadline(ack.to_encoded_bytes().into(), deadline).await?;
            return Ok(());
        }

        trace!(
            target: LOG_TARGET,
            "({}) Request: {}, Method: {}",
            self.logging_context_string,
            decoded_msg,
            method.id()
        );

        let req = Request::with_context(
            self.create_request_context(request_id),
            method,
            decoded_msg.payload.into(),
        );

        let service_call = log_timing(
            &self.logging_context_string,
            request_id,
            "service call",
            self.service.call(req),
        );
        let service_result = time::timeout(deadline, service_call).await;
        let service_result = match service_result {
            Ok(v) => v,
            Err(_) => {
                warn!(
                    target: LOG_TARGET,
                    "{} RPC service was not able to complete within the deadline ({:.0?}). Request aborted",
                    self.logging_context_string,
                    deadline,
                );

                #[cfg(feature = "metrics")]
                metrics::error_counter(&self.protocol, &RpcServerError::ServiceCallExceededDeadline).inc();

                // Tell the client, rather than going silent and leaving it to unwind on its own
                // timeout. This matters whenever the deadline we applied is shorter than the one the
                // client asked for - a clamped request would otherwise look identical to a hung
                // server for the remainder of the client's own (longer) deadline.
                let status = RpcStatus::timed_out("RPC service did not complete within the deadline");
                let timed_out = proto::rpc::RpcResponse {
                    request_id,
                    status: status.as_code(),
                    flags: RpcMessageFlags::FIN.bits().into(),
                    payload: status.to_details_bytes(),
                };
                #[cfg(feature = "metrics")]
                metrics::status_error_counter(&self.protocol, status.as_status_code()).inc();
                self.send_with_deadline(timed_out.to_encoded_bytes().into(), deadline)
                    .await?;
                return Ok(());
            },
        };

        match service_result {
            Ok(body) => {
                self.process_body(request_id, deadline, body).await?;
            },
            Err(err) => {
                debug!(
                    target: LOG_TARGET,
                    "{} Service returned an error: {}", self.logging_context_string, err
                );
                let resp = proto::rpc::RpcResponse {
                    request_id,
                    status: err.as_code(),
                    flags: RpcMessageFlags::FIN.bits().into(),
                    payload: err.to_details_bytes(),
                };

                #[cfg(feature = "metrics")]
                metrics::status_error_counter(&self.protocol, err.as_status_code()).inc();
                self.send_with_deadline(resp.to_encoded_bytes().into(), deadline)
                    .await?;
            },
        }

        Ok(())
    }

    fn protocol_name(&self) -> Cow<'_, str> {
        String::from_utf8_lossy(&self.protocol)
    }

    #[allow(clippy::too_many_lines)]
    async fn process_body(
        &mut self,
        request_id: u32,
        deadline: Duration,
        body: Response<Body>,
    ) -> Result<(), RpcServerError> {
        trace!(target: LOG_TARGET, "Service call succeeded");

        #[cfg(feature = "metrics")]
        let protocol = self.protocol.clone();
        let mut stream = body
            .into_message()
            .map(|result| into_response(request_id, result))
            .map(move |mut message| {
                if message.payload.len() > rpc::max_response_payload_size() {
                    message = message.exceeded_message_size();
                }
                #[cfg(feature = "metrics")]
                if !message.status.is_ok() {
                    metrics::status_error_counter(&protocol, message.status).inc();
                }
                message.to_encoded_bytes()
            });

        let logging_context = self.logging_context_string.clone();
        // The next item, pulled from the stream and encoded while the previous one was being sent. `Some(None)` means
        // the stream ended during that send.
        let mut prefetched: Option<Option<Bytes>> = None;
        loop {
            let next = match prefetched.take() {
                Some(Some(msg)) => {
                    // Give an interruption that arrived during the previous send the same chance it has in the
                    // select below. The prefetched item is dropped unsent.
                    if let Err(err) = self.check_interruptions().await {
                        match err {
                            err @ RpcServerError::ClientInterruptedStream => {
                                debug!(target: LOG_TARGET, "Stream was interrupted by client: {}", err);
                                break;
                            },
                            err => {
                                error!(target: LOG_TARGET, "Stream was interrupted: {}", err);
                                return Err(err);
                            },
                        }
                    }
                    Some(msg)
                },
                Some(None) => None,
                None => {
                    let next_item = log_timing(&logging_context, request_id, "message read", stream.next());
                    let timeout = time::sleep(deadline);

                    tokio::select! {
                        // Check if the client interrupted the outgoing stream
                        Err(err) = self.check_interruptions() => {
                            match err {
                                err @ RpcServerError::ClientInterruptedStream => {
                                    debug!(target: LOG_TARGET, "Stream was interrupted by client: {}", err);
                                    break;
                                },
                                err => {
                                    error!(target: LOG_TARGET, "Stream was interrupted: {}", err);
                                    return Err(err);
                                },
                            }
                        },
                        msg = next_item => msg,

                        _ = timeout => {
                             debug!(
                                target: LOG_TARGET,
                                "({}) Failed to return result within client deadline ({:.0?})",
                                self.logging_context_string,
                                deadline
                            );

                            #[cfg(feature = "metrics")]
                            metrics::error_counter(
                                &self.protocol,
                                &RpcServerError::ReadStreamExceededDeadline,
                            )
                            .inc();
                            break;
                        }
                    } // end select!
                },
            };

            let Some(msg) = next else {
                trace!(target: LOG_TARGET, "{} Request complete", self.logging_context_string,);
                break;
            };

            #[cfg(feature = "metrics")]
            self.outbound_response_bytes.observe(msg.len() as f64);
            trace!(
                target: LOG_TARGET,
                "({}) Sending body len = {}",
                self.logging_context_string,
                msg.len()
            );

            // Bounded because this sits outside the `timeout` branch above: it is the yamux window that backs up
            // here, so a peer that opens a stream and then stops draining it parks this task - and the session slot
            // it holds - with no timer running anywhere. The outer `run()` loop cannot rescue it either; its idle
            // timer does not tick while a request is being handled. On elapse the peer is mid-frame, so the
            // substream can no longer be trusted for a subsequent request and the session ends.
            let send = self.send_with_deadline(msg, deadline);
            tokio::pin!(send);
            // While the send is in flight, pull and encode the next item. At most one item is held, so a session whose
            // peer stops reading holds one extra encoded item, for no longer than the send deadline. If the send
            // finishes first, the pending pull is dropped (a stream loses nothing when `next()` is cancelled) and the
            // top of the loop waits for it as before, under a fresh per-item deadline.
            loop {
                tokio::select! {
                    biased;
                    result = &mut send => {
                        result?;
                        break;
                    },
                    next = stream.next(), if prefetched.is_none() => {
                        prefetched = Some(next);
                    },
                }
            }
        } // end loop
        Ok(())
    }

    async fn check_interruptions(&mut self) -> Result<(), RpcServerError> {
        let check = future::poll_fn(|cx| match Pin::new(&mut self.framed).poll_next(cx) {
            Poll::Ready(Some(Ok(mut msg))) => {
                if msg.len() > rpc::max_request_size() {
                    // Never decode an oversized message; a client may only send a FIN here, which is tiny
                    debug!(
                        target: LOG_TARGET,
                        "Ignoring a {} byte message received during a streaming response (maximum {} bytes)",
                        msg.len(),
                        rpc::max_request_size()
                    );
                    return Poll::Ready(None);
                }
                let decoded_msg = match proto::rpc::RpcRequest::decode(&mut msg) {
                    Ok(msg) => msg,
                    Err(err) => {
                        error!(target: LOG_TARGET, "Client send MALFORMED response: {}", err);
                        return Poll::Ready(Some(RpcServerError::UnexpectedIncomingMessageMalformed));
                    },
                };
                let u8_bits = match u8::try_from(decoded_msg.flags) {
                    Ok(bits) => bits,
                    Err(err) => {
                        error!(target: LOG_TARGET, "Client send MALFORMED flags: {}", err);
                        return Poll::Ready(Some(RpcServerError::ProtocolError(format!(
                            "invalid message flag: must be less than {}",
                            u8::MAX
                        ))));
                    },
                };

                let msg_flags = match RpcMessageFlags::from_bits(u8_bits) {
                    Some(flags) => flags,
                    None => {
                        error!(target: LOG_TARGET, "Client send MALFORMED flags: {}", u8_bits);
                        return Poll::Ready(Some(RpcServerError::ProtocolError(format!(
                            "invalid message flag, does not match any flags ({u8_bits})"
                        ))));
                    },
                };
                if msg_flags.is_fin() {
                    Poll::Ready(Some(RpcServerError::ClientInterruptedStream))
                } else {
                    Poll::Ready(Some(RpcServerError::UnexpectedIncomingMessage(decoded_msg)))
                }
            },
            Poll::Ready(Some(Err(err))) if err.kind() == io::ErrorKind::WouldBlock => Poll::Ready(None),
            Poll::Ready(Some(Err(err))) => Poll::Ready(Some(RpcServerError::from(err))),
            Poll::Ready(None) => Poll::Ready(Some(RpcServerError::StreamClosedByRemote)),
            Poll::Pending => Poll::Ready(None),
        })
        .await;
        match check {
            Some(err) => Err(err),
            None => Ok(()),
        }
    }

    fn create_request_context(&self, request_id: u32) -> RequestContext {
        RequestContext::new(request_id, self.node_id.clone(), Box::new(self.comms_provider.clone()))
    }
}

fn is_trace_enabled() -> bool {
    log_enabled!(target: LOG_TARGET, log::Level::Trace) || tracing::enabled!(target: LOG_TARGET, Level::TRACE)
}

async fn log_timing<R, F: Future<Output = R>>(context_str: &str, request_id: u32, tag: &str, fut: F) -> R {
    // Called for every streamed message, so only pay for the span and the timing when they will be logged
    if !is_trace_enabled() {
        return fut.await;
    }
    let t = Instant::now();
    let span = span!(Level::TRACE, "rpc::internal::timing", request_id, tag);
    let ret = fut.instrument(span).await;
    let elapsed = t.elapsed();
    trace!(
        target: LOG_TARGET,
        "({}) RPC TIMING(REQ_ID={}): '{}' took {:.2}s{}",
        context_str,
        request_id,
        tag,
        elapsed.as_secs_f32(),
        if elapsed.as_secs() >= 5 { " (SLOW)" } else { "" }
    );
    ret
}

/// Just the request id of an `RpcRequest`. Decoding this skips over the other fields, including the payload, without
/// copying them.
#[derive(Clone, PartialEq, prost::Message)]
struct RpcRequestId {
    #[prost(uint32, tag = "1")]
    request_id: u32,
}

/// If `request` is larger than [rpc::RPC_MAX_REQUEST_SIZE], returns the `BadRequest` response to send instead of
/// decoding it. Only the request id is read, so that the client can match the response. No ban: a peer running a
/// version with a larger request cap may send such a request honestly.
fn reject_oversized_request(request: &Bytes) -> Option<proto::rpc::RpcResponse> {
    if request.len() <= rpc::max_request_size() {
        return None;
    }
    let request_id = RpcRequestId::decode(request.clone()).map(|r| r.request_id).unwrap_or(0);
    let status = RpcStatus::bad_request(&format!(
        "The request size exceeded the maximum allowed request size. Max = {} bytes, Got = {} bytes",
        rpc::max_request_size(),
        request.len()
    ));
    Some(proto::rpc::RpcResponse {
        request_id,
        status: status.as_code(),
        flags: RpcMessageFlags::FIN.bits().into(),
        payload: status.to_details_bytes(),
    })
}

fn into_response(request_id: u32, result: Result<BodyBytes, RpcStatus>) -> RpcResponse {
    match result {
        Ok(msg) => {
            let mut flags = RpcMessageFlags::empty();
            if msg.is_finished() {
                flags |= RpcMessageFlags::FIN;
            }
            RpcResponse {
                request_id,
                status: RpcStatus::ok().as_status_code(),
                flags,
                payload: msg.into_bytes().unwrap_or_else(Bytes::new),
            }
        },
        Err(err) => {
            debug!(target: LOG_TARGET, "Body contained an error: {}", err);
            RpcResponse {
                request_id,
                status: err.as_status_code(),
                flags: RpcMessageFlags::FIN,
                payload: Bytes::from(err.to_details_bytes()),
            }
        },
    }
}

fn err_to_log_level(err: &io::Error) -> log::Level {
    match err.kind() {
        ErrorKind::ConnectionReset |
        ErrorKind::ConnectionAborted |
        ErrorKind::BrokenPipe |
        ErrorKind::WriteZero |
        ErrorKind::UnexpectedEof => log::Level::Debug,
        _ => log::Level::Error,
    }
}

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

    #[test]
    fn client_deadline_is_capped_from_above() {
        let builder = RpcServerBuilder::new();
        assert_eq!(builder.maximum_client_deadline, DEFAULT_MAXIMUM_CLIENT_DEADLINE);

        // Anything at or below the ceiling is honoured as asked for.
        assert_eq!(
            builder.clamp_client_deadline(Duration::from_secs(1)),
            Duration::from_secs(1)
        );
        assert_eq!(
            builder.clamp_client_deadline(DEFAULT_MAXIMUM_CLIENT_DEADLINE),
            DEFAULT_MAXIMUM_CLIENT_DEADLINE
        );
        // The 240s block-sync deadline must survive untouched, or syncing full blocks over Tor
        // would start being cut short by peers.
        assert_eq!(
            builder.clamp_client_deadline(Duration::from_secs(240)),
            Duration::from_secs(240)
        );

        // Above it, the server's own limit applies...
        assert_eq!(
            builder.clamp_client_deadline(DEFAULT_MAXIMUM_CLIENT_DEADLINE + Duration::from_secs(1)),
            DEFAULT_MAXIMUM_CLIENT_DEADLINE
        );
        // ...including the case this exists for: a peer asking for an effectively infinite deadline
        // to switch off the service-call and streaming timeouts.
        assert_eq!(
            builder.clamp_client_deadline(Duration::from_secs(u64::MAX)),
            DEFAULT_MAXIMUM_CLIENT_DEADLINE
        );
    }

    #[test]
    fn maximum_client_deadline_is_configurable() {
        let builder = RpcServerBuilder::new().with_maximum_client_deadline(Duration::from_secs(30));
        assert_eq!(
            builder.clamp_client_deadline(Duration::from_secs(u64::MAX)),
            Duration::from_secs(30)
        );
        assert_eq!(
            builder.clamp_client_deadline(Duration::from_secs(5)),
            Duration::from_secs(5)
        );
    }

    #[test]
    fn a_maximum_below_the_minimum_does_not_starve_requests() {
        // An operator setting this to zero (or anything under the minimum) would otherwise leave
        // the node up while aborting every request it is asked to serve.
        let builder = RpcServerBuilder::new()
            .with_minimum_client_deadline(Duration::from_secs(1))
            .with_maximum_client_deadline(Duration::from_secs(0));

        assert_eq!(
            builder.clamp_client_deadline(Duration::from_secs(0)),
            Duration::from_secs(1)
        );
        assert_eq!(
            builder.clamp_client_deadline(Duration::from_secs(120)),
            Duration::from_secs(1)
        );
        assert_eq!(
            builder.clamp_client_deadline(Duration::from_secs(u64::MAX)),
            Duration::from_secs(1)
        );
    }
}