veilid-core 0.5.3

Core library used to create a Veilid node and operate it as part of an application
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
use super::*;

mod answer;
mod coders;
mod debug;
mod descriptor_mode;
mod destination;
mod error;
mod fanout;
mod message;
mod message_header;
mod operation_waiter;
mod rendered_operation;
mod rpc_app_call;
mod rpc_app_message;
mod rpc_find_node;
mod rpc_get_value;
mod rpc_inspect_value;
mod rpc_return_receipt;
mod rpc_route;
mod rpc_set_value;
mod rpc_signal;
mod rpc_status;
mod rpc_transact_begin;
mod rpc_transact_command;
mod rpc_validate_dial_info;
mod rpc_value_changed;
mod rpc_watch_value;
mod rpc_worker;
mod sender_info;
mod sender_peer_info;
mod waitable_reply;

#[cfg(any(test, feature = "test-util"))]
#[doc(hidden)]
pub mod tests_rpc_processor;

pub use coders::encode_nonce as capnp_encode_nonce;
pub use coders::encode_public_key as capnp_encode_public_key;

#[cfg(feature = "unstable-blockstore")]
mod rpc_find_block;
#[cfg(feature = "unstable-blockstore")]
mod rpc_supply_block;

#[cfg(feature = "unstable-tunnels")]
mod rpc_cancel_tunnel;
#[cfg(feature = "unstable-tunnels")]
mod rpc_complete_tunnel;
#[cfg(feature = "unstable-tunnels")]
mod rpc_start_tunnel;

pub(crate) use answer::*;
pub(crate) use coders::{
    canonical_message_builder_to_bytes_writer_packed,
    canonical_message_builder_to_bytes_writer_unpacked, decode_node_info, decode_private_route,
    encode_node_info, encode_private_route, encode_route_hop, RPCDecodeContext, TransactCommand,
};
pub(crate) use descriptor_mode::*;
pub(crate) use destination::*;
pub(crate) use error::*;
pub(crate) use fanout::*;
pub(crate) use rpc_status::StatusResult;
pub(crate) use sender_info::*;
pub(crate) use waitable_reply::WaitableReplyContext;

use coders::*;
use message::*;
use message_header::*;
use operation_waiter::*;
use rendered_operation::*;
use rpc_worker::*;
use sender_peer_info::*;
use waitable_reply::*;

use crypto::*;
use network_manager::*;
use routing_table::*;
use storage_manager::*;

impl_veilid_log_facility!("rpc");

/////////////////////////////////////////////////////////////////////

const RPC_WORKERS_PER_CORE: u32 = 16;

/////////////////////////////////////////////////////////////////////

#[derive(Copy, Clone, Debug)]
#[must_use]
enum RPCKind {
    Question,
    Statement,
    Answer,
}

/////////////////////////////////////////////////////////////////////

#[derive(Debug, Clone)]
#[must_use]
pub struct RPCProcessorStartupContext {
    pub startup_lock: Arc<StartupLock>,
}
impl RPCProcessorStartupContext {
    pub fn new() -> Self {
        Self {
            startup_lock: Arc::new(StartupLock::new()),
        }
    }
}
impl Default for RPCProcessorStartupContext {
    fn default() -> Self {
        Self::new()
    }
}

/////////////////////////////////////////////////////////////////////

#[derive(Debug)]
#[must_use]
struct RPCProcessorInner {
    /// The channel for sending RPCs to the workers
    rpc_send_channel: Option<flume::Sender<RPCWorkerRequest>>,
    /// The stop source for the RPC workers
    rpc_stop_source: Option<StopSource>,
    /// The join handles for the RPC workers
    rpc_worker_join_handles: Vec<MustJoinHandle<()>>,
    /// The dequeue latency stats for the RPC workers
    rpc_worker_dequeue_latency: LatencyStats,
    /// The processing latency stats for the RPC workers
    rpc_worker_process_latency: LatencyStats,
    /// The dequeue latency stats accounting for the RPC workers
    rpc_worker_dequeue_latency_accounting: LatencyStatsAccounting,
    /// The processing latency stats accounting for the RPC workers
    rpc_worker_process_latency_accounting: LatencyStatsAccounting,
    /// The processing latency stats and accounting for the RPC workers by operation kind
    rpc_worker_process_latency_and_accounting_by_operation_kind:
        BTreeMap<&'static str, (LatencyStats, LatencyStatsAccounting)>,
}

#[derive(Debug)]
#[must_use]
pub(crate) struct RPCProcessor {
    /// The component registry
    registry: VeilidComponentRegistry,
    /// The inner state of the RPC processor
    inner: Mutex<RPCProcessorInner>,
    /// The timeout for a direct RPC sent to a node with no extra hops
    timeout: TimestampDuration,
    /// The queue length for the RPC send/recv channels
    queue_size: u32,
    /// The number of RPC workers to spin up
    concurrency: u32,
    /// The RPCs that are waiting for a reply
    waiting_rpc_table: OperationWaiter<Message, Option<Arc<QuestionContext>>>,
    /// The app calls that are waiting for a reply
    waiting_app_call_table: OperationWaiter<Bytes, ()>,
    /// The context for the startup of the RPC processor
    startup_context: RPCProcessorStartupContext,
}

impl_veilid_component!(RPCProcessor);

impl RPCProcessor {
    fn new_inner() -> RPCProcessorInner {
        RPCProcessorInner {
            rpc_send_channel: None,
            rpc_stop_source: None,
            rpc_worker_join_handles: Vec::new(),
            rpc_worker_dequeue_latency: LatencyStats::default(),
            rpc_worker_process_latency: LatencyStats::default(),
            rpc_worker_dequeue_latency_accounting: LatencyStatsAccounting::new(),
            rpc_worker_process_latency_accounting: LatencyStatsAccounting::new(),
            rpc_worker_process_latency_and_accounting_by_operation_kind: BTreeMap::new(),
        }
    }

    pub fn new(
        registry: VeilidComponentRegistry,
        startup_context: RPCProcessorStartupContext,
    ) -> Self {
        // make local copy of node id for easy access
        let (concurrency, queue_size, timeout_us) = {
            let config = registry.config();

            // set up channel
            let mut concurrency = config.network.rpc.concurrency;
            let queue_size = config.network.rpc.queue_size;
            let timeout_us = TimestampDuration::new(ms_to_us(config.network.rpc.timeout_ms));
            if concurrency == 0 {
                concurrency = get_concurrency();
                if concurrency == 0 {
                    concurrency = 1;
                }

                // Default RPC concurrency is the number of CPUs * 16 rpc workers per core, as a single worker takes about 1% CPU when relaying and 16% is reasonable for baseline plus relay
                concurrency *= RPC_WORKERS_PER_CORE;
            }
            (concurrency, queue_size, timeout_us)
        };

        Self {
            waiting_rpc_table: OperationWaiter::new(registry.clone()),
            waiting_app_call_table: OperationWaiter::new(registry.clone()),
            registry,
            inner: Mutex::new(Self::new_inner()),
            timeout: timeout_us,
            queue_size,
            concurrency,
            startup_context,
        }
    }

    /////////////////////////////////////
    // Initialization

    fn log_facilities_impl(&self) -> VeilidComponentLogFacilities {
        VeilidComponentLogFacilities::new()
            .with_facility(
                VeilidComponentLogFacility::try_new_with_tags("rpc", ["#common"]).unwrap(),
            )
            .with_facility(
                VeilidComponentLogFacility::try_new_with_tags(
                    "rpc_message",
                    ["#dht", "#fanout", "#verbose"],
                )
                .unwrap(),
            )
            .with_facility(VeilidComponentLogFacility::try_new_with_tags("dht", ["#dht"]).unwrap())
            .with_facility(
                VeilidComponentLogFacility::try_new_with_tags(
                    "network_result",
                    ["#dht", "#fanout", "#verbose"],
                )
                .unwrap(),
            )
            .with_facility(
                VeilidComponentLogFacility::try_new_with_tags("fanout", ["#fanout"]).unwrap(),
            )
    }

    #[expect(clippy::unused_async)]
    async fn init_async(&self) -> EyreResult<()> {
        Ok(())
    }

    #[expect(clippy::unused_async)]
    async fn post_init_async(&self) -> EyreResult<()> {
        Ok(())
    }

    #[expect(clippy::unused_async)]
    async fn pre_terminate_async(&self) {
        // Ensure things have shut down
        assert!(
            self.startup_context.startup_lock.is_shut_down(),
            "should have shut down by now"
        );
    }

    #[expect(clippy::unused_async)]
    async fn terminate_async(&self) {}

    //////////////////////////////////////////////////////////////////////

    #[cfg_attr(feature = "instrument", instrument(level = "debug", skip_all, err, fields(__VEILID_LOG_KEY = self.log_key())))]
    pub fn startup(&self) -> EyreResult<()> {
        veilid_log!(self debug "starting rpc processor startup");

        let guard = self.startup_context.startup_lock.startup()?;
        {
            let mut inner = self.inner.lock();

            let channel = flume::bounded(self.queue_size as usize);
            inner.rpc_send_channel = Some(channel.0.clone());
            inner.rpc_stop_source = Some(StopSource::new());
        }

        self.startup_rpc_workers()?;

        guard.success();

        veilid_log!(self debug "finished rpc processor startup");

        Ok(())
    }

    #[cfg_attr(feature = "instrument", instrument(level = "debug", skip_all, fields(__VEILID_LOG_KEY = self.log_key())))]
    pub async fn shutdown(&self) {
        veilid_log!(self debug "starting rpc processor shutdown");
        let guard = self
            .startup_context
            .startup_lock
            .shutdown()
            .await
            .expect_or_log("should be started up");

        self.shutdown_rpc_workers().await;

        veilid_log!(self debug "resetting rpc processor state");

        // Release the rpc processor
        *self.inner.lock() = Self::new_inner();

        guard.success();
        veilid_log!(self debug "finished rpc processor shutdown");
    }

    //////////////////////////////////////////////////////////////////////

    /// Get waiting app call id for debugging purposes
    pub fn get_app_call_ids(&self) -> Vec<OperationId> {
        self.waiting_app_call_table.get_operation_ids()
    }

    /// Incorporate 'sender peer info' sent along with an RPC message
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    fn process_sender_peer_info(
        &self,
        sender_node_id: NodeId,
        sender_peer_info: &SenderPeerInfo,
    ) -> RPCNetworkResult<Option<NodeRef>> {
        let Some(peer_info) = sender_peer_info.opt_peer_info() else {
            return Ok(NetworkResult::value(None));
        };

        // Ensure the sender peer info is for the actual sender specified in the envelope
        if !peer_info.node_ids().contains(&sender_node_id) {
            // Attempted to update peer info for the wrong node id
            self.network_manager()
                .address_filter()
                .punish_node_id(sender_node_id, PunishmentReason::WrongSenderPeerInfo);

            return Ok(NetworkResult::invalid_message(
                "attempt to update peer info for non-sender node id",
            ));
        }

        // Register the peer info in its translated routing domain
        // Translation is done at deserialization time
        veilid_log!(self debug target: "network_result", "processing sender peer info: id={} pi={:?}", sender_node_id, sender_peer_info);
        let sender_nr = match self
            .routing_table()
            .register_node_with_peer_info(peer_info, false)
        {
            Ok(v) => v.unfiltered(),
            Err(e) => {
                self.network_manager().address_filter().punish_node_id(
                    sender_node_id,
                    PunishmentReason::FailedToRegisterSenderPeerInfo,
                );
                return Ok(NetworkResult::invalid_message(e));
            }
        };

        Ok(NetworkResult::value(Some(sender_nr)))
    }

    //////////////////////////////////////////////////////////////////////

    /// Search the public internet routing domain for a single node and add
    /// it to the routing table and return the node reference
    /// If no node was found in the timeout, this returns None
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn public_internet_peer_search(
        &self,
        node_id: NodeId,
        safety_selection: SafetySelection,
    ) -> TimeoutOr<Result<Option<NodeRef>, RPCError>> {
        let routing_table = self.routing_table();
        let routing_domain = RoutingDomain::PublicInternet;

        // Ignore own node
        if routing_table.matches_own_node_id(std::slice::from_ref(&node_id)) {
            return TimeoutOr::Value(Err(RPCError::network("can't search for own node id")));
        }

        // If nobody knows where this node is, ask the DHT for it
        let config = self.config();
        let node_count = config.network.dht.max_find_node_count as usize;
        // let consensus_count = config.network.dht.resolve_node_count as usize;
        // let consensus_width = config.network.dht.consensus_width as usize;
        let fanout_tasks = config.network.dht.resolve_node_fanout as usize;
        let timeout = TimestampDuration::from(ms_to_us(config.network.dht.resolve_node_timeout_ms));

        // Routine to call to generate fanout
        let result = Arc::new(Mutex::new(Option::<NodeRef>::None));

        let registry = self.registry();
        let node_id2 = node_id.clone();
        let call_routine = Arc::new(move |next_node: NodeRef| {
            let registry = registry.clone();
            let node_id = node_id2.clone();
            let safety_selection = safety_selection.clone();
            Box::pin(async move {
                let this = registry.rpc_processor();
                match Box::pin(this.rpc_call_find_node(
                    Destination::direct(
                        next_node.routing_domain_filtered(routing_domain),
                        Some(safety_selection.clone()),
                    ),
                    node_id.to_hash_coordinate(),
                    vec![],
                ))
                .await?
                {
                    NetworkResult::Timeout => Ok(FanoutCallOutput {
                        peer_info_list: vec![],
                        disposition: FanoutCallDisposition::Timeout,
                    }),
                    NetworkResult::ServiceUnavailable(_)
                    | NetworkResult::NoConnection(_)
                    | NetworkResult::AlreadyExists(_)
                    | NetworkResult::InvalidMessage(_) => Ok(FanoutCallOutput {
                        peer_info_list: vec![],
                        disposition: FanoutCallDisposition::Rejected,
                    }),
                    NetworkResult::Value(v) => Ok(FanoutCallOutput {
                        peer_info_list: v.answer,
                        disposition: FanoutCallDisposition::Accepted,
                    }),
                }
            }) as PinBoxFuture<FanoutCallResult>
        });

        // Routine to call to check if we're done at each step
        let result2 = result.clone();
        let node_id2 = node_id.clone();
        let check_done = Arc::new(move |_: &FanoutResult| -> FanoutDoneDisposition {
            let Ok(Some(nr)) = routing_table.lookup_node_id(node_id2.clone()) else {
                return FanoutDoneDisposition::NotDone;
            };

            // ensure we have some dial info for the entry already,
            // and that the node is still alive
            // if not, we should keep looking for better info
            if nr.state(Timestamp::now_non_decreasing()).is_alive() && nr.has_any_dial_info() {
                *result2.lock() = Some(nr);
                return FanoutDoneDisposition::DoneEarly;
            }

            FanoutDoneDisposition::NotDone
        });

        // Call the fanout
        let routing_table = self.routing_table();
        let fanout_call = FanoutCall::new(
            &routing_table,
            FanoutCallParams {
                name: format!(
                    "public_internet_peer_search({})",
                    Timestamp::now_increasing()
                ),
                hash_coordinate: node_id.to_hash_coordinate(),
                node_count,
                fanout_tasks,
                consensus_count: 0,
                consensus_width: 0,
                timeout,
            },
            empty_fanout_peer_info_filter(),
            call_routine,
            check_done,
        );

        match fanout_call.run(vec![], FanoutQueueMode::Unthrottled).await {
            Ok(fanout_result) => {
                if matches!(fanout_result.kind, FanoutResultKind::Timeout) {
                    TimeoutOr::timeout()
                } else {
                    TimeoutOr::value(Ok(result.lock().take()))
                }
            }
            Err(e) => TimeoutOr::value(Err(e)),
        }
    }

    /// Search the DHT for a specific node corresponding to a key unless we
    /// have that node in our routing table already, and return the node reference
    /// Note: This routine can possibly be recursive, hence the PinBoxFuture async form
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    pub fn resolve_node(
        &self,
        node_id: NodeId,
        safety_selection: SafetySelection,
    ) -> PinBoxFuture<'_, Result<Option<NodeRef>, RPCError>> {
        let registry = self.registry();
        Box::pin(
            async move {
                let this = registry.rpc_processor();

                let _guard = this
                    .startup_context
                    .startup_lock
                    .enter()
                    .map_err(RPCError::map_try_again("not started up"))?;

                let routing_table = this.routing_table();

                // First see if we have the node in our routing table already
                let mut existing_nr = None;
                if let Some(nr) = routing_table
                    .lookup_node_id(node_id.clone())
                    .map_err(RPCError::internal)?
                {
                    existing_nr = Some(nr.clone());

                    // ensure we have some dial info for the entry already,
                    // and that the node is still alive
                    // if not, we should do the find_node anyway
                    if nr.state(Timestamp::now_non_decreasing()).is_alive()
                        && nr.has_any_dial_info()
                    {
                        return Ok(Some(nr));
                    }
                }

                // Search routing domains for peer
                // xxx: Eventually add other routing domains here
                let nr = match this
                    .public_internet_peer_search(node_id, safety_selection)
                    .await
                {
                    TimeoutOr::Timeout => None,
                    TimeoutOr::Value(Ok(v)) => v,
                    TimeoutOr::Value(Err(e)) => {
                        return Err(e);
                    }
                };

                // Either return the node we just resolved or a dead one we found in the routing table to try again
                Ok(nr.or(existing_nr))
            }
            .in_current_span(),
        )
    }

    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn wait_for_reply(
        &self,
        waitable_reply: WaitableReply,
        debug_string: String,
    ) -> Result<TimeoutOr<(Message, AnswerContext)>, RPCError> {
        let id = waitable_reply.handle.id();
        let res = self
            .waiting_rpc_table
            .wait_for_op(waitable_reply.handle, waitable_reply.context.timeout)
            .await;
        match res {
            Err(e) => {
                #[cfg(feature = "verbose-tracing")]
                let context_debug = {
                    let routing_table = &*self.routing_table();
                    format!("{}\n", waitable_reply.context.debug(routing_table))
                };
                #[cfg(not(feature = "verbose-tracing"))]
                let context_debug = format!(
                    "flow={}",
                    waitable_reply.context.send_data_result.unique_flow().flow
                );
                veilid_log!(self debug "RPC Lost (id={} {} {}): {}", id, debug_string, e, context_debug);
                self.record_lost_answer(&waitable_reply.context);

                Err(e)
            }
            Ok(TimeoutOr::Timeout) => {
                #[cfg(feature = "verbose-tracing")]
                let context_debug = {
                    let routing_table = &*self.routing_table();
                    format!("{}\n", waitable_reply.context.debug(routing_table))
                };
                #[cfg(not(feature = "verbose-tracing"))]
                let context_debug = format!(
                    "flow={}",
                    waitable_reply.context.send_data_result.unique_flow().flow
                );
                veilid_log!(self debug "RPC Lost (id={} {} Timeout): {}", id, debug_string, context_debug);
                self.record_lost_answer(&waitable_reply.context);

                Ok(TimeoutOr::Timeout)
            }
            Ok(TimeoutOr::Value((rpcreader, latency))) => {
                // Reply received
                let recv_ts = Timestamp::now_non_decreasing();

                // Record answer received
                self.record_answer_received(
                    recv_ts,
                    rpcreader.header.body_len,
                    &waitable_reply.context,
                );

                // Ensure the reply comes over the private route that was requested
                if let Some(reply_private_route) =
                    waitable_reply.context.opt_reply_private_route.clone()
                {
                    match &rpcreader.header.detail {
                        RPCMessageHeaderDetail::Direct(_) => {
                            return Err(RPCError::protocol(
                                "should have received reply over private route or stub",
                            ));
                        }
                        RPCMessageHeaderDetail::SafetyRouted(sr) => {
                            let public_key = self
                                .routing_table()
                                .public_key(sr.direct.envelope.get_crypto_kind());
                            if public_key != reply_private_route {
                                return Err(RPCError::protocol(
                                    "should have received reply from safety route to a stub",
                                ));
                            }
                        }
                        RPCMessageHeaderDetail::PrivateRouted(pr) => {
                            if pr.private_route != reply_private_route {
                                return Err(RPCError::protocol(
                                    "received reply over the wrong private route",
                                ));
                            }
                        }
                    };
                }

                Ok(TimeoutOr::Value((
                    rpcreader,
                    AnswerContext {
                        latency,
                        waitable_reply_context: waitable_reply.context,
                    },
                )))
            }
        }
    }

    /// Wrap an operation with a private route inside a safety route
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn wrap_with_route(
        &self,
        routing_domain: RoutingDomain,
        safety_selection: SafetySelection,
        remote_private_route: Arc<PrivateRoute>,
        opt_reply_private_route: Option<PublicKey>,
        signed_message_data: Bytes,
    ) -> RPCNetworkResult<RenderedOperation> {
        let routing_table = self.routing_table();
        let crypto = self.crypto();
        let rss = routing_table.route_spec_store();

        // Get useful private route properties
        let pr_is_stub = remote_private_route.is_stub();
        let pr_pubkey = remote_private_route.public_key.clone();
        let crypto_kind = remote_private_route.crypto_kind();
        let Some(vcrypto) = crypto.get_async(crypto_kind) else {
            return Err(RPCError::internal(
                "crypto not available for selected private route",
            ));
        };

        // Compile the safety route with the private route
        let sequencing = safety_selection.get_sequencing();
        let params = CompileRouteParams {
            safety_selection,
            private_route: remote_private_route,
            opt_reply_pr_pubkey: opt_reply_private_route.clone(),
        };
        let compiled_route: Arc<CompiledRoute> =
            network_result_try!(rss.compile_route(&params).await.to_rpc_network_result()?);
        let sr_is_stub = compiled_route.safety_route.is_stub();
        let sr_pubkey = compiled_route.safety_route.public_key.clone();

        // Encrypt routed operation
        // Xmsg + ENC(Xmsg, DH(PKapr, SKbsr))
        let nonce = vcrypto.random_nonce().await;
        let dh_secret = vcrypto
            .cached_dh(&pr_pubkey, &compiled_route.secret)
            .await
            .map_err(RPCError::map_internal("dh failed"))?;
        let enc_msg_data = vcrypto
            .encrypt_aead(signed_message_data, &nonce, &dh_secret, None)
            .await
            .map_err(RPCError::map_internal("encryption failed"))?;

        // Make the routed operation
        let operation =
            RoutedOperation::new(routing_domain, sequencing, vec![], nonce, enc_msg_data)?;

        // Prepare route operation
        let route_operation =
            RPCOperationRoute::new(compiled_route.safety_route.clone(), operation);
        let ssni_route =
            self.get_sender_peer_info(&Destination::direct(compiled_route.first_hop.clone(), None));
        let operation = RPCOperation::new_statement(
            RPCStatement::new(RPCStatementDetail::Route(Box::new(route_operation))),
            ssni_route,
        );
        let op_id = operation.op_id();

        let signed_operation = RPCSignedOperation::sign(&crypto, &operation, None).await?;

        // Convert message to bytes and return it
        let mut route_msg = ::capnp::message::Builder::new_default();
        let mut route_operation = route_msg.init_root::<veilid_capnp::signed_operation::Builder>();
        signed_operation.encode(&mut route_operation)?;
        let message = canonical_message_builder_to_bytes_writer_packed(route_msg, |size| {
            BytesWriter::with_capacity(size)
        })?
        .into_inner()
        .freeze();

        // Get locked routes for all routes in use
        let mut opt_locked_routes: Option<LockedRoutes> = None;
        let opt_safety_route = if sr_is_stub {
            None
        } else {
            let mut new_locked_routes =
                rss.lock_allocated_routes_by_keys(std::slice::from_ref(&sr_pubkey));
            if let Some(locked_routes) = &mut opt_locked_routes {
                locked_routes.take_from(&mut new_locked_routes);
            } else {
                opt_locked_routes = Some(new_locked_routes);
            }
            Some(sr_pubkey)
        };
        let opt_remote_private_route = if pr_is_stub {
            None
        } else {
            let mut new_locked_routes =
                rss.lock_remote_routes_by_keys(std::slice::from_ref(&pr_pubkey));
            if let Some(locked_routes) = &mut opt_locked_routes {
                locked_routes.take_from(&mut new_locked_routes);
            } else {
                opt_locked_routes = Some(new_locked_routes);
            }
            Some(pr_pubkey)
        };
        if let Some(reply_private_route) = &opt_reply_private_route {
            let mut new_locked_routes =
                rss.lock_allocated_routes_by_keys(std::slice::from_ref(reply_private_route));
            if let Some(locked_routes) = &mut opt_locked_routes {
                locked_routes.take_from(&mut new_locked_routes);
            } else {
                opt_locked_routes = Some(new_locked_routes);
            }
        }

        let out = RenderedOperation {
            outer_op_id: op_id,
            message,
            node_ref: compiled_route.first_hop.clone(),
            opt_relay_di: None,
            opt_safety_route,
            opt_remote_private_route,
            opt_reply_private_route,
            opt_locked_routes,
        };

        Ok(NetworkResult::value(out))
    }

    /// Produce a byte buffer that represents the wire encoding of the entire
    /// unencrypted envelope body for a RPC message. This incorporates
    /// wrapping a private and/or safety route if they are specified.
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn render_signed_operation(
        &self,
        dest: Destination,
        operation: RPCOperation,
        signing_params: Option<RPCSigningParams>,
    ) -> RPCNetworkResult<RenderedOperation> {
        let registry = self.registry();

        let rpc_processor = registry.rpc_processor();
        let crypto = registry.crypto();
        let out: NetworkResult<RenderedOperation>;

        // Encode message to a builder and make a message reader for it
        // Then produce the message as an unencrypted byte buffer

        // Sign question if desired
        let op_id = operation.op_id();
        let signed_operation =
            RPCSignedOperation::sign(&crypto, &operation, signing_params).await?;
        let message = {
            let mut msg_builder = ::capnp::message::Builder::new_default();
            let mut op_builder = msg_builder.init_root::<veilid_capnp::signed_operation::Builder>();
            signed_operation.encode(&mut op_builder)?;
            canonical_message_builder_to_bytes_writer_packed(msg_builder, |size| {
                BytesWriter::with_capacity(size)
            })?
            .into_inner()
            .freeze()
        };

        // Get reply private route if we are asking for one to be used in our 'respond to'
        let opt_reply_private_route = match operation.kind() {
            RPCOperationKind::Question(q) => match q.respond_to() {
                RespondTo::Sender => None,
                RespondTo::PrivateRoute(pr) => Some(pr.public_key.clone()),
            },
            RPCOperationKind::Statement(_) | RPCOperationKind::Answer(_) => None,
        };

        // To where are we sending the request
        match dest {
            Destination::Relay {
                relay_di,
                node: ref node_ref,
            } => {
                // Send to a node directly via a relay dialinfo

                // Reply private route should be None here, even for questions
                if opt_reply_private_route.is_some() {
                    return Err(RPCError::internal(
                        "reply private route should never be specified for relay destinations",
                    ));
                }

                // If no safety route is being used, and we're not sending to a private
                // route, we can use a direct envelope instead of routing
                out = NetworkResult::value(RenderedOperation {
                    outer_op_id: op_id,
                    message,
                    node_ref: node_ref.sequencing_filtered(
                        relay_di
                            .protocol_type()
                            .sequence_ordering()
                            .strict_sequencing(),
                    ),
                    opt_relay_di: Some(relay_di),
                    opt_safety_route: None,
                    opt_remote_private_route: None,
                    opt_reply_private_route: None,
                    opt_locked_routes: None,
                });
            }
            Destination::Direct {
                node: ref node_ref,
                safety_selection,
            } => {
                // Send to a node without a private route
                // --------------------------------------

                // Handle the existence of safety route
                match safety_selection {
                    SafetySelection::Unsafe(sequencing) => {
                        // Apply safety selection sequencing requirement if it is more strict than the node_ref's sequencing requirement
                        let mut node_ref = node_ref.clone();
                        if sequencing > node_ref.sequencing() {
                            node_ref.set_sequencing(sequencing)
                        }

                        // Reply private route should be None here, even for questions
                        if opt_reply_private_route.is_some() {
                            return Err(RPCError::internal("reply private route should never be specified for direct destinations"));
                        }

                        // If no safety route is being used, and we're not sending to a private
                        // route, we can use a direct envelope instead of routing
                        out = NetworkResult::value(RenderedOperation {
                            outer_op_id: op_id,
                            message,
                            node_ref,
                            opt_relay_di: None,
                            opt_safety_route: None,
                            opt_remote_private_route: None,
                            opt_reply_private_route: None,
                            opt_locked_routes: None,
                        });
                    }
                    SafetySelection::Safe(_) => {
                        // For now we only private-route over PublicInternet
                        let routing_domain = RoutingDomain::PublicInternet;

                        // No private route was specified for the request
                        // but we are using a safety route, so we must create an empty private route
                        // Destination relay is ignored for safety routed operations
                        let peer_info = match node_ref.get_peer_info(routing_domain) {
                            None => {
                                return Ok(NetworkResult::no_connection_other(
                                    "No peer info for stub private route",
                                ))
                            }
                            Some(pi) => pi,
                        };
                        let private_route = Arc::new(PrivateRoute::new_stub(
                            match node_ref.best_public_key(routing_domain) {
                                Some(pk) => pk,
                                None => {
                                    return Ok(NetworkResult::no_connection_other(
                                        "No best node id for stub private route",
                                    ));
                                }
                            },
                            RouteNode::PeerInfo(peer_info),
                        ));

                        // Wrap with safety route
                        out = rpc_processor
                            .wrap_with_route(
                                routing_domain,
                                safety_selection.clone(),
                                private_route,
                                opt_reply_private_route,
                                message,
                            )
                            .await?;

                        if enabled!(target: "rpc_message", Level::DEBUG) {
                            let (route_op_id, safety_route) = match &out {
                                NetworkResult::Value(v) => (
                                    Some(v.outer_op_id.as_u64()),
                                    v.opt_safety_route.as_ref().map(tracing::field::display),
                                ),
                                _ => (None, None),
                            };

                            veilid_log!(rpc_processor debug target: "rpc_message", fields:
                                dir = "send",
                                kind = "route",
                                route_op_id,
                                op_id = op_id.as_u64(),
                                ?node_ref,
                                safety_route,
                                desc = "wrap_with_route"
                            );
                        }
                    }
                };
            }
            Destination::PrivateRoute {
                private_route,
                safety_selection,
            } => {
                // For now we only private-route over PublicInternet
                let routing_domain = RoutingDomain::PublicInternet;

                // Send to private route
                // ---------------------
                // Reply with 'route' operation
                out = rpc_processor
                    .wrap_with_route(
                        routing_domain,
                        safety_selection,
                        private_route.clone(),
                        opt_reply_private_route,
                        message,
                    )
                    .await?;

                if enabled!(target: "rpc_message", Level::DEBUG) {
                    let (route_op_id, safety_route) = match &out {
                        NetworkResult::Value(v) => (
                            Some(v.outer_op_id.as_u64()),
                            v.opt_safety_route.as_ref().map(tracing::field::display),
                        ),
                        _ => (None, None),
                    };

                    veilid_log!(rpc_processor debug target: "rpc_message", fields:
                        dir = "send",
                        kind = "route",
                        route_op_id,
                        op_id = op_id.as_u64(),
                        safety_route,
                        private_route = tracing::field::display(&private_route.public_key),
                        desc = "wrap_with_route"
                    );
                }
            }
        }

        Ok(out)
    }

    /// Get signed node info to package with RPC messages to improve
    /// routing table caching when it is okay to do so
    /// Also check target's timestamp of our own node info, to see if we should send that
    /// And send our timestamp of the target's node info so they can determine if they should update us on their next rpc
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    fn get_sender_peer_info(&self, dest: &Destination) -> SenderPeerInfo {
        // veilid_log!(self debug target: "network_result", "get_sender_peer_info: {:?}", dest);

        let routing_table = self.routing_table();
        // Don't do this if the sender is to remain private
        // Otherwise we would be attaching the original sender's identity to the final destination,
        // thus defeating the purpose of the safety route entirely :P
        let Some(UnsafeRoutingInfo {
            opt_node,
            opt_routing_domain,
        }) = dest.get_unsafe_routing_info(&routing_table)
        else {
            return SenderPeerInfo::default();
        };
        let Some(node) = opt_node else {
            // If this is going over a private route, don't bother sending any sender peer info
            // The other side won't accept it because peer info sent over a private route
            // could be used to deanonymize the private route's endpoint
            return SenderPeerInfo::default();
        };
        let Some(routing_domain) = opt_routing_domain else {
            // No routing domain for target, no node info is safe to send here
            // Only a stale connection or no connection exists, or an unexpected
            // relay was used, possibly due to the destination switching relays
            // in a race condition with our send
            return SenderPeerInfo::default();
        };

        // Get the target's node info timestamp
        let target_node_info_ts = node.node_info_ts(routing_domain);
        // veilid_log!(self debug target: "network_result", "target_node_info_ts: rd={:?} ts={}", routing_domain, target_node_info_ts);

        // Return whatever peer info we have even if the network class is not yet valid
        // That away we overwrite any prior existing valid-network-class nodeinfo in the remote routing table
        let routing_table = self.routing_table();

        if let Some(published_peer_info) = routing_table.get_published_peer_info(routing_domain) {
            // If the target has not yet seen our published peer info, send it along if we have it
            if !node.has_seen_our_node_info_ts(routing_domain) {
                return SenderPeerInfo::new(published_peer_info, target_node_info_ts);
            }
        }
        SenderPeerInfo::new_no_peer_info(target_node_info_ts)
    }

    /// Record failure to send to node or route
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    fn record_send_failure(
        &self,
        rpc_kind: RPCKind,
        send_ts: Timestamp,
        node_ref: NodeRef,
        safety_route: Option<PublicKey>,
        remote_private_route: Option<PublicKey>,
    ) {
        veilid_log!(self debug target: "network_result", "send failure: rpc_kind={:?} send_ts={}, node_ref={}, safety_route={:?}, remote_private_route={:?}",
            rpc_kind, send_ts, node_ref, safety_route, remote_private_route
        );

        let wants_answer = matches!(rpc_kind, RPCKind::Question);

        // Record for node if this was not sent via a route
        if safety_route.is_none() && remote_private_route.is_none() {
            node_ref.stats_failed_to_send(send_ts, wants_answer);

            // Also clear the last_connections for the entry so we make a new connection next time
            node_ref.clear_last_flows();

            return;
        }

        let routing_table = self.routing_table();
        let rss = routing_table.route_spec_store();

        if let Some(sr_pubkey) = &safety_route {
            // If safety route was in use, record failure to send there
            rss.update_allocated_route_stats(send_ts, sr_pubkey, |s| -> VeilidAPIResult<()> {
                s.record_send_failed();
                Ok(())
            });
        } else if let Some(pr_pubkey) = &remote_private_route {
            // If no safety route was in use, then it's the private route's fault if we have one
            rss.update_remote_route_stats(send_ts, pr_pubkey, |s| -> VeilidAPIResult<()> {
                s.record_send_failed();
                Ok(())
            });
        }
    }

    /// Record question lost to node or route
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    fn record_lost_answer(&self, context: &WaitableReplyContext) {
        // Record for node if this was not sent via a route
        if context.opt_safety_route.is_none() && context.opt_remote_private_route.is_none() {
            context
                .node_ref
                .stats_lost_answer(context.send_data_result.sequence_ordering());

            // Also clear the last_connections for the entry so we make a new connection next time
            context.node_ref.clear_last_flows();

            return;
        }
        // Get route spec store
        let routing_table = self.routing_table();
        let rss = routing_table.route_spec_store();

        // If safety route was used, record question lost there
        if let Some(sr_pubkey) = &context.opt_safety_route {
            rss.update_allocated_route_stats(
                context.send_ts,
                sr_pubkey,
                |s| -> VeilidAPIResult<()> {
                    s.record_lost_answer(&context.destination);
                    Ok(())
                },
            );
        }
        // If remote private route was used, record question lost there
        if let Some(pr_pubkey) = &context.opt_remote_private_route {
            rss.update_remote_route_stats(context.send_ts, pr_pubkey, |s| -> VeilidAPIResult<()> {
                s.record_lost_answer(&context.destination);
                Ok(())
            });
        }
        // If reply private route was used, record question lost there
        // Skip if reply private route is the same as the safety route to avoid double-counting
        if let Some(rpr_pubkey) = &context.opt_reply_private_route {
            if context.opt_safety_route.as_ref() != Some(rpr_pubkey) {
                rss.update_allocated_route_stats(
                    context.send_ts,
                    rpr_pubkey,
                    |s| -> VeilidAPIResult<()> {
                        s.record_lost_answer(&context.destination);
                        Ok(())
                    },
                );
            }
        }
    }

    /// Record success sending to node or route
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    #[expect(clippy::too_many_arguments)]
    fn record_send_success(
        &self,
        rpc_kind: RPCKind,
        send_ts: Timestamp,
        bytes: ByteCount,
        node_ref: NodeRef,
        safety_route: Option<PublicKey>,
        remote_private_route: Option<PublicKey>,
        ordering: SequenceOrdering,
    ) {
        // Record for node if this was not sent via a route
        if safety_route.is_none() && remote_private_route.is_none() {
            let wants_answer = matches!(rpc_kind, RPCKind::Question);
            let is_answer = matches!(rpc_kind, RPCKind::Answer);

            if is_answer {
                node_ref.stats_answer_sent(bytes);
            } else {
                node_ref.stats_question_sent(send_ts, bytes, wants_answer, ordering);
            }
            return;
        }

        // Get route spec store
        let routing_table = self.routing_table();
        let rss = routing_table.route_spec_store();

        // If safety route was used, record send there
        if let Some(sr_pubkey) = &safety_route {
            rss.update_allocated_route_stats(send_ts, sr_pubkey, |s| {
                s.record_sent(send_ts, bytes);
                Ok(())
            });
        }

        // If remote private route was used, record send there
        if let Some(pr_pubkey) = &remote_private_route {
            rss.update_remote_route_stats(send_ts, pr_pubkey, |s| {
                s.record_sent(send_ts, bytes);
                Ok(())
            });
        }
    }

    /// Record answer received from node or route
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    fn record_answer_received(
        &self,
        recv_ts: Timestamp,
        bytes: ByteCount,
        context: &WaitableReplyContext,
    ) {
        // Record stats for remote node if this was direct
        if context.opt_safety_route.is_none()
            && context.opt_remote_private_route.is_none()
            && context.opt_reply_private_route.is_none()
        {
            context.node_ref.stats_answer_rcvd(
                context.send_ts,
                recv_ts,
                bytes,
                context.send_data_result.sequence_ordering(),
            );
            return;
        }
        // Get route spec store
        let routing_table = self.routing_table();
        let rss = routing_table.route_spec_store();

        // Get latency for all local routes
        let mut total_local_latency = TimestampDuration::new(0u64);
        let total_latency: TimestampDuration = recv_ts.duration_since(context.send_ts);

        // If safety route was used, record route there
        if let Some(sr_pubkey) = &context.opt_safety_route {
            rss.update_allocated_route_stats(context.send_ts, sr_pubkey, |s| {
                // Record received bytes
                s.record_answer_received(recv_ts, bytes, &context.destination);

                // If we used a safety route to send, use our last tested latency
                total_local_latency = total_local_latency.saturating_add(s.latency_stats().average);
                Ok(())
            });
        }

        // If local private route was used, record route there
        // Skip if reply private route is the same as the safety route to avoid double-counting
        if let Some(pr_pubkey) = &context.opt_reply_private_route {
            if context.opt_safety_route.as_ref() != Some(pr_pubkey) {
                rss.update_allocated_route_stats(context.send_ts, pr_pubkey, |s| {
                    // Record received bytes
                    s.record_answer_received(recv_ts, bytes, &context.destination);

                    // If we used a private route to receive, use our last tested latency
                    total_local_latency =
                        total_local_latency.saturating_add(s.latency_stats().average);
                    Ok(())
                });
            }
        }

        // If remote private route was used, record there
        if let Some(rpr_pubkey) = &context.opt_remote_private_route {
            rss.update_remote_route_stats(context.send_ts, rpr_pubkey, |s| {
                // Record received bytes
                s.record_answer_received(recv_ts, bytes, &context.destination);

                // The remote route latency is recorded using the total latency minus the total local latency
                let remote_latency = total_latency.saturating_sub(total_local_latency);
                s.record_latency(remote_latency);
                Ok(())
            });

            // If we sent to a private route without a safety route
            // We need to mark our own node info as having been seen so we can optimize sending it
            if let Err(e) = rss.mark_remote_private_route_seen_our_node_info(rpr_pubkey, recv_ts) {
                veilid_log!(self debug "private route missing: {}", e);
            }

            // We can't record local route latency if a remote private route was used because
            // there is no way other than the prior latency estimation to determine how much time was spent
            // in the remote private route
            // Instead, we rely on local route testing to give us latency numbers for our local routes
        } else {
            // If no remote private route was used, then record half the total latency on our local routes
            // This is fine because if we sent with a local safety route,
            // then we must have received with a local private route too, per the design rules
            if let Some(sr_pubkey) = &context.opt_safety_route {
                rss.update_allocated_route_stats(context.send_ts, sr_pubkey, |s| {
                    s.record_latency(total_latency.div(2));
                    Ok(())
                });
            }
            // Skip if reply private route is the same as the safety route to avoid double-counting
            if let Some(pr_pubkey) = &context.opt_reply_private_route {
                if context.opt_safety_route.as_ref() != Some(pr_pubkey) {
                    rss.update_allocated_route_stats(context.send_ts, pr_pubkey, |s| {
                        s.record_latency(total_latency.div(2));
                        Ok(())
                    });
                }
            }
        }
    }

    /// Record question or statement received from node or route
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    fn record_question_received(&self, msg: &Message) {
        let recv_ts = msg.header.timestamp;
        let bytes = msg.header.body_len;

        let routing_table = self.routing_table();
        let rss = routing_table.route_spec_store();

        // Process messages based on how they were received
        match &msg.header.detail {
            // Process direct messages
            RPCMessageHeaderDetail::Direct(_) => {
                if let Some(sender_nr) = msg.opt_sender_nr.clone() {
                    sender_nr.stats_question_rcvd(recv_ts, bytes);
                }
            }
            // Process messages that arrived with no private route (private route stub)
            RPCMessageHeaderDetail::SafetyRouted(d) => {
                // This may record nothing if the remote safety route is not also
                // a remote private route that been imported, but that's okay
                rss.update_allocated_route_stats(recv_ts, &d.remote_safety_route, |s| {
                    s.record_question_received(recv_ts, bytes);
                    Ok(())
                });
            }
            // Process messages that arrived to our private route
            RPCMessageHeaderDetail::PrivateRouted(d) => {
                // This may record nothing if the remote safety route is not also
                // a remote private route that been imported, but that's okay
                // it could also be a node id if no remote safety route was used
                // in which case this also will do nothing
                rss.update_remote_route_stats(recv_ts, &d.remote_safety_route, |s| {
                    s.record_question_received(recv_ts, bytes);
                    Ok(())
                });

                // Record for our local private route we received over
                rss.update_allocated_route_stats(recv_ts, &d.private_route, |s| {
                    s.record_question_received(recv_ts, bytes);
                    Ok(())
                });
            }
        }
    }

    /// Get the default timeout for RPCs sent to a via a specific safety selection
    pub fn get_safety_selection_timeout(
        &self,
        safety_selection: &SafetySelection,
    ) -> TimestampDuration {
        // For both safety routes to direct destinations, and to private routes, the effective hop count is
        // twice that of the safety selection's choice, either because the private route doubles the hop count (probably)
        // or because we manually double it when contacting non-private destinations
        let effective_hops = safety_selection.get_hop_count() * 2;

        // Timeout should be the default timeout plus a half timeout for each hop
        // Anything more than that is going to drag performance down and routes should be reselected
        // that can survive the timeout rather than accepting slow performance
        self.timeout
            .saturating_add(self.timeout.div(2).saturating_mul(effective_hops as u64))
    }

    /// Get the default timeout for RPCs sent to a specific destination
    pub fn get_destination_timeout(&self, dest: &Destination) -> TimestampDuration {
        self.get_safety_selection_timeout(&dest.get_safety_selection())
    }

    /// Issue a question over the network, possibly using an anonymized route
    /// Optionally keeps a context to be passed to the answer processor when an answer is received
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn question(
        &self,
        dest: Destination,
        question: RPCQuestion,
        opt_signer_keypair: Option<KeyPair>,
        context: Option<QuestionContext>,
    ) -> RPCNetworkResult<WaitableReply> {
        // Get sender peer info if we should send that
        let spi = self.get_sender_peer_info(&dest);

        // Wrap question in operation
        let operation = RPCOperation::new_question(question, spi);
        let op_id = operation.op_id();

        // Get signing parameters
        let signing_params = match opt_signer_keypair {
            Some(signer_keypair) => Some(RPCSigningParams {
                signer_keypair,
                destination_key: dest.destination_key()?,
            }),
            None => None,
        };
        let signer = signing_params.as_ref().map(|x| x.signer_keypair.key());

        // Log rpc send
        veilid_log!(self debug target: "rpc_message", fields: dir = "send", kind = "question", op_id = op_id.as_u64(), desc = operation.kind().desc(), ?dest, ?signer );

        // Produce rendered operation
        let RenderedOperation {
            outer_op_id,
            message,
            node_ref,
            opt_relay_di,
            opt_safety_route,
            opt_remote_private_route,
            opt_reply_private_route,
            opt_locked_routes,
        } = network_result_try!(
            Box::pin(
                self.render_signed_operation(dest.clone(), operation, signing_params)
                    .measure_debug(
                        TimestampDuration::new_ms(200),
                        veilid_log_dbg!(self, "RPCProcessor::question render_signed_operation"),
                    )
            )
            .await?
        );

        // Calculate answer timeout
        let timeout = self.get_destination_timeout(&dest);

        // Set up op id eventual
        let handle = self
            .waiting_rpc_table
            .add_op_waiter(op_id, context.map(Arc::new));

        // Send question
        let bytes: ByteCount = (message.len() as u64).into();
        #[allow(unused_variables)]
        let message_len = message.len();
        let res = self
            .network_manager()
            .send_envelope(node_ref.clone(), opt_relay_di.clone(), message)
            .await
            .map_err(|e| {
                // If we're returning an error, clean up
                let send_ts = Timestamp::now();
                self.record_send_failure(
                    RPCKind::Question,
                    send_ts,
                    node_ref.unfiltered(),
                    opt_safety_route.clone(),
                    opt_remote_private_route.clone(),
                );
                RPCError::network(e)
            })?;
        // Take send timestamp -after- send is attempted to exclude TCP connection time which
        // may unfairly punish some nodes, randomly, based on their being in the connection table or not
        let send_ts = Timestamp::now();
        let send_data_result = network_result_value_or_log!(self res => [ format!(": outer_op_id = {}, node_ref={}, opt_relay_di={}, message.len={}", outer_op_id, node_ref, opt_relay_di.as_ref().map(|x| x.to_string()).unwrap_or_else(String::new), message_len) ] {
                // If we couldn't send we're still cleaning up
                self.record_send_failure(RPCKind::Question, send_ts, node_ref.unfiltered(), opt_safety_route, opt_remote_private_route);
                network_result_raise!(res);
            }
        );

        // Successfully sent
        self.record_send_success(
            RPCKind::Question,
            send_ts,
            bytes,
            node_ref.unfiltered(),
            opt_safety_route.clone(),
            opt_remote_private_route.clone(),
            send_data_result.sequence_ordering(),
        );

        // Ref the connection so it doesn't go away until we're done with the waitable reply
        let opt_connection_ref_scope =
            send_data_result.unique_flow().connection_id.and_then(|id| {
                self.network_manager()
                    .connection_manager()
                    .try_connection_ref_scope(id)
            });

        // Pass back waitable reply completion
        Ok(NetworkResult::value(WaitableReply::new(
            handle,
            opt_connection_ref_scope,
            WaitableReplyContext {
                timeout,
                node_ref: node_ref.unfiltered(),
                send_ts,
                send_data_result,
                opt_safety_route,
                opt_remote_private_route,
                opt_reply_private_route,
                opt_locked_routes,
                destination: dest,
            },
        )))
    }

    /// Issue a statement over the network, possibly using an anonymized route
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn statement(
        &self,
        dest: Destination,
        statement: RPCStatement,
        opt_signer_keypair: Option<KeyPair>,
        route_op_id: Option<OperationId>,
    ) -> RPCNetworkResult<()> {
        // Get sender peer info if we should send that
        let spi = self.get_sender_peer_info(&dest);

        // Wrap statement in operation
        let operation = RPCOperation::new_statement(statement, spi);

        // Get signing parameters
        let signing_params = match opt_signer_keypair {
            Some(signer_keypair) => Some(RPCSigningParams {
                signer_keypair,
                destination_key: dest.destination_key()?,
            }),
            None => None,
        };
        let signer = signing_params.as_ref().map(|x| x.signer_keypair.key());

        // Log rpc send
        veilid_log!(self debug target: "rpc_message", fields: dir = "send", kind = "statement", ?route_op_id, op_id = operation.op_id().as_u64(), desc = operation.kind().desc(), ?dest, ?signer);

        // Produce rendered operation
        let RenderedOperation {
            outer_op_id,
            message,
            node_ref,
            opt_relay_di,
            opt_safety_route: safety_route,
            opt_remote_private_route: remote_private_route,
            opt_reply_private_route: _,
            opt_locked_routes: locked_routes,
        } = network_result_try!(
            Box::pin(
                self.render_signed_operation(dest, operation, signing_params)
                    .measure_debug(
                        TimestampDuration::new_ms(200),
                        veilid_log_dbg!(self, "RPCProcessor::statement render_signed_operation"),
                    )
            )
            .await?
        );

        // Send statement
        let bytes: ByteCount = (message.len() as u64).into();
        #[allow(unused_variables)]
        let message_len = message.len();
        let res = self
            .network_manager()
            .send_envelope(node_ref.clone(), opt_relay_di.clone(), message)
            .await
            .map_err(|e| {
                // If we're returning an error, clean up
                let send_ts = Timestamp::now();
                self.record_send_failure(
                    RPCKind::Statement,
                    send_ts,
                    node_ref.unfiltered(),
                    safety_route.clone(),
                    remote_private_route.clone(),
                );
                RPCError::network(e)
            })?;
        // Take send timestamp -after- send is attempted to exclude TCP connection time which
        // may unfairly punish some nodes, randomly, based on their being in the connection table or not
        let send_ts = Timestamp::now();
        let send_data_result = network_result_value_or_log!(self res => [ format!(": outer_op_id = {}, node_ref={}, opt_relay_di={}, message.len={}", outer_op_id, node_ref, opt_relay_di.as_ref().map(|x| x.to_string()).unwrap_or_else(String::new), message_len) ] {
                // If we couldn't send we're still cleaning up
                self.record_send_failure(RPCKind::Statement, send_ts, node_ref.unfiltered(), safety_route.clone(), remote_private_route.clone());
                network_result_raise!(res);
            }
        );

        // Successfully sent
        self.record_send_success(
            RPCKind::Statement,
            send_ts,
            bytes,
            node_ref.unfiltered(),
            safety_route,
            remote_private_route,
            send_data_result.sequence_ordering(),
        );

        // Statements are one-shot, so we can drop the locks
        drop(locked_routes);

        Ok(NetworkResult::value(()))
    }
    /// Issue a reply over the network, possibly using an anonymized route
    /// The request must want a response, or this routine fails
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn answer(
        &self,
        request: Message,
        answer: RPCAnswer,
        opt_signer_keypair: Option<KeyPair>,
    ) -> RPCNetworkResult<()> {
        // Extract destination from respond_to
        let dest = network_result_try!(self.get_respond_to_destination(&request));

        // Get sender signed node info if we should send that
        let spi = self.get_sender_peer_info(&dest);

        // Wrap answer in operation
        let operation = RPCOperation::new_answer(request.operation.op_id(), answer, spi);

        // Get signing parameters
        let signing_params = match opt_signer_keypair {
            Some(signer_keypair) => Some(RPCSigningParams {
                signer_keypair,
                destination_key: dest.destination_key()?,
            }),
            None => None,
        };
        let signer = signing_params.as_ref().map(|x| x.signer_keypair.key());

        // Log rpc send
        veilid_log!(self debug target: "rpc_message", fields: dir = "send", kind = "answer", op_id = operation.op_id().as_u64(), desc = operation.kind().desc(), ?dest, ?signer);

        // Produce rendered operation
        let RenderedOperation {
            outer_op_id,
            message,
            node_ref,
            opt_relay_di,
            opt_safety_route: safety_route,
            opt_remote_private_route: remote_private_route,
            opt_reply_private_route: _,
            opt_locked_routes: locked_routes,
        } = network_result_try!(
            Box::pin(
                self.render_signed_operation(dest, operation, signing_params)
                    .measure_debug(
                        TimestampDuration::new_ms(200),
                        veilid_log_dbg!(self, "RPCProcessor::answer render_signed_operation"),
                    )
            )
            .await?
        );

        // Send the reply
        let bytes: ByteCount = (message.len() as u64).into();
        #[allow(unused_variables)]
        let message_len = message.len();
        let res = self
            .network_manager()
            .send_envelope(node_ref.clone(), opt_relay_di.clone(), message)
            .await
            .map_err(|e| {
                // If we're returning an error, clean up
                let send_ts = Timestamp::now();
                self.record_send_failure(
                    RPCKind::Answer,
                    send_ts,
                    node_ref.unfiltered(),
                    safety_route.clone(),
                    remote_private_route.clone(),
                );
                RPCError::network(e)
            })?;
        // Take send timestamp -after- send is attempted to exclude TCP connection time which
        // may unfairly punish some nodes, randomly, based on their being in the connection table or not
        let send_ts = Timestamp::now();
        let send_data_result = network_result_value_or_log!(self res => [ format!(": outer_op_id = {}, node_ref={}, opt_relay_di={}, message.len={}", outer_op_id, node_ref, opt_relay_di.as_ref().map(|x| x.to_string()).unwrap_or_else(String::new), message_len) ] {
                // If we couldn't send we're still cleaning up
                self.record_send_failure(RPCKind::Answer, send_ts, node_ref.unfiltered(), safety_route.clone(), remote_private_route.clone());
                network_result_raise!(res);
            }
        );

        // Reply successfully sent
        self.record_send_success(
            RPCKind::Answer,
            send_ts,
            bytes,
            node_ref.unfiltered(),
            safety_route,
            remote_private_route,
            send_data_result.sequence_ordering(),
        );

        // Answers are the end of the server-side of an RPC operation, so we can drop the locks
        // because each RPC operation is one-shot from the server-side
        drop(locked_routes);

        Ok(NetworkResult::value(()))
    }

    /// Decoding RPC from the wire
    /// This performs a capnp decode on the data, and if it passes the capnp schema
    /// it performs the signature and cryptographic validation required to pass the operation up for processing
    /// Returns the operation and its signing key if provided
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn decode_rpc_operation(
        &self,
        encoded_msg: MessageEncoded,
    ) -> Result<(RPCOperation, Option<PublicKey>, MessageEncoded), RPCError> {
        let decode_context = RPCDecodeContext {
            registry: self.registry(),
            origin_routing_domain: encoded_msg.header.routing_domain(),
        };

        let signed_operation = {
            let reader = encoded_msg.data.get_reader()?;
            let op_reader = reader.get_root::<veilid_capnp::signed_operation::Reader>()?;
            RPCSignedOperation::decode(&decode_context, &op_reader)?
        };

        // Validate the RPC message
        self.validate_rpc_signed_operation(&signed_operation, &encoded_msg)
            .await?;
        let opt_signer = signed_operation
            .signer_signature()
            .map(|x| x.signer.clone());

        // Decode the operation itself
        let operation = signed_operation.decode_operation(&decode_context)?;

        // Validate the operation
        self.validate_rpc_operation(&operation).await?;

        Ok((operation, opt_signer, encoded_msg))
    }

    /// Validation of RPC signature if provided
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn validate_rpc_signed_operation(
        &self,
        signed_operation: &RPCSignedOperation,
        encoded_msg: &MessageEncoded,
    ) -> Result<(), RPCError> {
        // Validate the RPC signed operation
        let destination_key = match &encoded_msg.header.detail {
            RPCMessageHeaderDetail::Direct(d) => {
                let routing_table = self.routing_table();
                routing_table.public_key(d.envelope.get_crypto_kind())
            }
            RPCMessageHeaderDetail::SafetyRouted(s) => {
                let routing_table = self.routing_table();
                routing_table.public_key(s.direct.envelope.get_crypto_kind())
            }
            RPCMessageHeaderDetail::PrivateRouted(p) => p.private_route.clone(),
        };

        let crypto = self.crypto();
        signed_operation.validate(&crypto, destination_key).await?;

        Ok(())
    }

    /// Cryptographic RPC validation and sanitization
    ///
    /// This code may modify the RPC operation to remove elements that are inappropriate for this node
    /// or reject the RPC operation entirely. For example, PeerInfo in fanout peer lists may be
    /// removed if they are deemed inappropriate for this node, without rejecting the entire operation.
    ///
    /// We do this as part of the RPC network layer to ensure that any RPC operations that are
    /// processed have already been validated cryptographically and it is not the job of the
    /// caller or receiver. This does not mean the operation is 'semantically correct'. For
    /// complex operations that require stateful validation and a more robust context than
    /// 'signatures', the caller must still perform whatever validation is necessary
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn validate_rpc_operation(&self, operation: &RPCOperation) -> Result<(), RPCError> {
        // If this is an answer, get the question context for this answer
        // If we received an answer for a question we did not ask, this will return an error
        let question_context = if let RPCOperationKind::Answer(_) = operation.kind() {
            let op_id = operation.op_id();
            self.waiting_rpc_table.get_op_context(op_id)?
        } else {
            None
        };

        // Validate the RPC operation
        let validate_context = RPCValidateContext {
            registry: self.registry(),
            question_context: question_context.as_ref().map(|x| x.as_ref()),
        };
        operation.validate(&validate_context).await
    }

    //////////////////////////////////////////////////////////////////////
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "rpc", skip_all, fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    async fn process_rpc_message(&self, encoded_msg: MessageEncoded) -> RPCNetworkResult<()> {
        let start_ts = Timestamp::now();

        // Decode operation appropriately based on header detail
        let msg = match &encoded_msg.header.detail {
            RPCMessageHeaderDetail::Direct(detail) => {
                // Get sender node id
                let sender_node_id = detail.envelope.get_sender_id();

                // Get the routing domain this message came over
                let origin_routing_domain = detail.routing_domain;

                // Decode and validate the RPC operation
                let decode_res = self
                    .decode_rpc_operation(encoded_msg)
                    .measure_debug(TimestampDuration::new_ms(200), |dur| {
                        veilid_log!(self debug "decode_rpc_operation Direct: {}", dur);
                    })
                    .await;
                let (operation, opt_signer, encoded_msg) = match decode_res {
                    Ok(v) => v,
                    Err(e) => {
                        match e {
                            // Invalid messages that should be punished
                            RPCError::Protocol(_) | RPCError::InvalidFormat(_) => {
                                veilid_log!(self debug "Invalid RPC Operation: {}", e);

                                // Punish nodes that send direct undecodable crap
                                self.network_manager().address_filter().punish_node_id(
                                    sender_node_id,
                                    PunishmentReason::FailedToDecodeOperation,
                                );
                            }
                            // Ignored messages that should be dropped
                            RPCError::Ignore(_) | RPCError::Network(_) | RPCError::TryAgain(_) => {
                                veilid_log!(self trace "Dropping RPC Operation: {}", e);
                            }
                            // Internal errors that deserve louder logging
                            RPCError::Unimplemented(_) | RPCError::Internal(_) => {
                                veilid_log!(self error "Error decoding RPC operation: {}", e);
                            }
                        };
                        return Ok(NetworkResult::invalid_message(e));
                    }
                };

                // Get the sender noderef, incorporating sender's peer info
                let sender_peer_info = operation.sender_peer_info();
                let mut opt_sender_nr: Option<NodeRef> = network_result_try!(self
                .process_sender_peer_info(sender_node_id.clone(), &sender_peer_info)? => {
                    veilid_log!(self debug target:"network_result", "Sender PeerInfo: {:?}", sender_peer_info);
                    veilid_log!(self debug target:"network_result", "From Operation: {:?}", operation.kind());
                    veilid_log!(self debug target:"network_result", "With Detail: {:?}", encoded_msg.header.detail);
                });
                // look up sender node, in case it's different than our peer due to relaying
                if opt_sender_nr.is_none() {
                    opt_sender_nr = match self.routing_table().lookup_node_id(sender_node_id) {
                        Ok(v) => v,
                        Err(e) => {
                            // If this fails it's not the other node's fault. We should be able to look up a
                            // node ref for a registered sender node id that just sent a message to us
                            // because it is registered with an existing connection at the very least.
                            return Ok(NetworkResult::no_connection_other(e));
                        }
                    }
                }

                // Update the 'seen our node info' timestamp to determine if this node needs a
                // 'node info update' ping
                if let Some(sender_nr) = &opt_sender_nr {
                    let new_ts = sender_peer_info.target_node_info_ts();
                    if let Some(old_ts) =
                        sender_nr.set_seen_our_node_info_ts(origin_routing_domain, new_ts)
                    {
                        veilid_log!(self debug target: "network_result", "Setting seen_our_node_info_ts on {}: {} -> {}", sender_nr, old_ts, new_ts );
                    }
                }

                // Make the RPC message
                Message {
                    header: encoded_msg.header,
                    operation,
                    opt_signer,
                    opt_sender_nr,
                }
            }
            RPCMessageHeaderDetail::SafetyRouted(_) | RPCMessageHeaderDetail::PrivateRouted(_) => {
                // Decode and validate the RPC operation
                let (operation, opt_signer, encoded_msg) = match self
                    .decode_rpc_operation(encoded_msg)
                    .measure_debug(TimestampDuration::new_ms(200), |dur| {
                        veilid_log!(self debug "decode_rpc_operation Routed: {}", dur);
                    })
                    .await
                {
                    Ok(v) => v,
                    Err(e) => {
                        match e {
                            // Invalid messages that should be punished
                            RPCError::Protocol(_) | RPCError::InvalidFormat(_) => {
                                veilid_log!(self debug "Invalid routed RPC Operation: {}", e);

                                // XXX: Punish routes that send routed undecodable crap
                                // self.network_manager().address_filter().punish_route_id(xxx, PunishmentReason::FailedToDecodeRoutedMessage);
                            }
                            // Ignored messages that should be dropped
                            RPCError::Ignore(_) | RPCError::Network(_) | RPCError::TryAgain(_) => {
                                veilid_log!(self trace "Dropping routed RPC Operation: {}", e);
                            }
                            // Internal errors that deserve louder logging
                            RPCError::Unimplemented(_) | RPCError::Internal(_) => {
                                veilid_log!(self error "Error decoding routed RPC operation: {}", e);
                            }
                        };

                        return Ok(NetworkResult::invalid_message(e));
                    }
                };

                // Make the RPC message
                Message {
                    header: encoded_msg.header,
                    operation,
                    opt_signer,
                    opt_sender_nr: None,
                }
            }
        };

        let desc = msg.operation.kind().desc();
        let signer = msg.opt_signer.as_ref();
        let route_op_id = match &msg.header.detail {
            RPCMessageHeaderDetail::Direct(_) => None,
            RPCMessageHeaderDetail::SafetyRouted(s) => Some(s.route_op_id),
            RPCMessageHeaderDetail::PrivateRouted(p) => Some(p.route_op_id),
        };

        // Process stats for questions/statements received
        match msg.operation.kind() {
            RPCOperationKind::Question(_) => {
                self.record_question_received(&msg);

                if let Some(sender_nr) = msg.opt_sender_nr.clone() {
                    sender_nr.stats_question_rcvd(msg.header.timestamp, msg.header.body_len);
                }

                // Log rpc receive
                veilid_log!(self debug target: "rpc_message", fields: dir = "recv", kind = "question", ?route_op_id, op_id = msg.operation.op_id().as_u64(), desc, header = ?msg.header, operation = ?msg.operation, signer = ?signer);
            }
            RPCOperationKind::Statement(_) => {
                if let Some(sender_nr) = msg.opt_sender_nr.clone() {
                    sender_nr.stats_question_rcvd(msg.header.timestamp, msg.header.body_len);
                }

                // Log rpc receive
                veilid_log!(self debug target: "rpc_message", fields: dir = "recv", kind = "statement", ?route_op_id, op_id = msg.operation.op_id().as_u64(), desc, header = ?msg.header, operation = ?msg.operation, signer = ?signer);
            }
            RPCOperationKind::Answer(_) => {
                // Answer stats are processed in wait_for_reply

                // Log rpc receive
                veilid_log!(self debug target: "rpc_message", fields: dir = "recv", kind = "answer", ?route_op_id, op_id = msg.operation.op_id().as_u64(), desc, header = ?msg.header, operation = ?msg.operation, signer = ?signer);
            }
        };

        // Process specific message kind
        let out = match msg.operation.kind() {
            RPCOperationKind::Question(q) => match q.detail() {
                RPCQuestionDetail::StatusQ(_) => {
                    pin_dyn_future_closure!(self.process_status_q(msg)).await
                }
                RPCQuestionDetail::FindNodeQ(_) => {
                    pin_dyn_future_closure!(self.process_find_node_q(msg)).await
                }
                RPCQuestionDetail::AppCallQ(_) => {
                    pin_dyn_future_closure!(self.process_app_call_q(msg)).await
                }
                RPCQuestionDetail::GetValueQ(_) => {
                    pin_dyn_future_closure!(self.process_get_value_q(msg)).await
                }
                RPCQuestionDetail::SetValueQ(_) => {
                    pin_dyn_future_closure!(self.process_set_value_q(msg)).await
                }
                RPCQuestionDetail::WatchValueQ(_) => {
                    pin_dyn_future_closure!(self.process_watch_value_q(msg)).await
                }
                RPCQuestionDetail::InspectValueQ(_) => {
                    pin_dyn_future_closure!(self.process_inspect_value_q(msg)).await
                }
                RPCQuestionDetail::TransactBeginQ(_) => {
                    pin_dyn_future_closure!(self.process_transact_begin_q(msg)).await
                }
                RPCQuestionDetail::TransactCommandQ(_) => {
                    pin_dyn_future_closure!(self.process_transact_command_q(msg)).await
                }
                #[cfg(feature = "unstable-blockstore")]
                RPCQuestionDetail::SupplyBlockQ(_) => {
                    pin_dyn_future_closure!(self.process_supply_block_q(msg)).await
                }
                #[cfg(feature = "unstable-blockstore")]
                RPCQuestionDetail::FindBlockQ(_) => {
                    pin_dyn_future_closure!(self.process_find_block_q(msg)).await
                }
                #[cfg(feature = "unstable-tunnels")]
                RPCQuestionDetail::StartTunnelQ(_) => {
                    pin_dyn_future_closure!(self.process_start_tunnel_q(msg)).await
                }
                #[cfg(feature = "unstable-tunnels")]
                RPCQuestionDetail::CompleteTunnelQ(_) => {
                    pin_dyn_future_closure!(self.process_complete_tunnel_q(msg)).await
                }
                #[cfg(feature = "unstable-tunnels")]
                RPCQuestionDetail::CancelTunnelQ(_) => {
                    pin_dyn_future_closure!(self.process_cancel_tunnel_q(msg)).await
                }
            },
            RPCOperationKind::Statement(s) => match s.detail() {
                RPCStatementDetail::ValidateDialInfo(_) => {
                    pin_dyn_future_closure!(self.process_validate_dial_info(msg)).await
                }
                RPCStatementDetail::Route(_) => {
                    pin_dyn_future_closure!(self.process_route(msg)).await
                }
                RPCStatementDetail::ValueChanged(_) => {
                    pin_dyn_future_closure!(self.process_value_changed(msg)).await
                }
                RPCStatementDetail::Signal(_) => {
                    pin_dyn_future_closure!(self.process_signal(msg)).await
                }
                RPCStatementDetail::ReturnReceipt(_) => {
                    pin_dyn_future_closure!(self.process_return_receipt(msg)).await
                }
                RPCStatementDetail::AppMessage(_) => self.process_app_message(msg),
            },
            RPCOperationKind::Answer(_) => {
                let op_id = msg.operation.op_id();
                if let Err(e) = self.waiting_rpc_table.complete_op_waiter(op_id, msg) {
                    match e {
                        RPCError::Unimplemented(_) | RPCError::Internal(_) => {
                            veilid_log!(self error "Error in RPC operation: id = {}: {}", op_id, e);
                        }
                        RPCError::InvalidFormat(_)
                        | RPCError::Protocol(_)
                        | RPCError::Network(_)
                        | RPCError::TryAgain(_) => {
                            veilid_log!(self debug "Could not complete RPC operation: id = {}: {}", op_id, e);
                        }
                        RPCError::Ignore(e) => {
                            veilid_log!(self debug "RPC operation ignored: id = {}: {}", op_id, e);
                        }
                    };
                    // Don't throw an error here because it's okay if the original operation timed out
                }
                Ok(NetworkResult::value(()))
            }
        };

        // Log per-kind latency
        {
            let latency = TimestampDuration::since(start_ts);
            let mut inner = self.inner.lock();
            let entry = inner
                .rpc_worker_process_latency_and_accounting_by_operation_kind
                .entry(desc)
                .or_default();

            entry.0 = entry.1.record_latency(latency);
        }
        out
    }
}