noatun 0.1.3

Noatun is an in-process, distributed database with materialized view support.
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
//! The distributor contains all logic needed to distribute events between noatun
//! instances. It does not contain any actual IO code. Instead, something like
//! `crate::communication::DatabaseCommunication` can be used to communicate over UDP or similar.
use crate::colors::*;
use crate::cutoff::{Acceptability, CutOffHashPos};
use crate::database::{DatabaseSession, DatabaseSessionMut};
use crate::mini_pather::MiniPather;
use crate::noatun_instant::Instant;
use crate::{Database, Message, MessageExt, MessageFrame, MessageHeader, MessageId};
use anyhow::Result;
use arcshift::ArcShift;
use arrayvec::ArrayString;
use indexmap::map::Entry;
use indexmap::{IndexMap, IndexSet};
use metrics::{describe_gauge, gauge, Gauge, Unit};
use rand::random;
use savefile_derive::Savefile;
use std::collections::{HashMap, VecDeque};
use std::fmt::{Debug, Display, Formatter};
use std::hash::Hash;
use std::io::Cursor;
use std::sync::{Arc, RwLock};
use std::time::Duration;
use tracing::{debug, error, info, trace, warn};

#[derive(Debug)]
pub(crate) struct MessageSourceInfo {
    /// The node we first heard this message from. The firstactual transmitter of the complete
    /// message, if any such available, otherwise the first node that mentioned the message
    pub(crate) original_source: EphemeralNodeId,
    /// True if we have observed the actual message, not just a messageid mentioned in
    /// ReportHeads or similar
    pub(crate) transmitter_seen: bool,
    /// True if `origina_source` is the only source from which we have heard this particular
    /// message
    pub(crate) other_transmitters: bool,
}

#[derive(Default, Debug)]
pub(crate) struct RecentMessages {
    pub(crate) recent_messages: IndexMap<MessageId, MessageSourceInfo>,
}
impl RecentMessages {
    /// actual_transmission is to be set to true if message was transmitted in full, not just
    /// mentioned. This is used to help us understand if a node has a message because of a
    /// retransmission. I.e, if actual_transmission=false, this event itself can't be such a
    /// retransmission.
    fn record_message_source(
        &mut self,
        message: MessageId,
        source: EphemeralNodeId,
        actual_transmission: bool,
    ) {
        if self.recent_messages.len() > 10000 {
            self.recent_messages.drain(0..2500);
        }
        match self.recent_messages.entry(message) {
            Entry::Vacant(e) => {
                e.insert(MessageSourceInfo {
                    original_source: source,
                    transmitter_seen: actual_transmission,
                    other_transmitters: false,
                });
            }
            Entry::Occupied(mut e) => {
                let item = e.get_mut();
                if actual_transmission && !item.transmitter_seen {
                    item.transmitter_seen = true;
                    item.original_source = source;
                } else if actual_transmission && source != item.original_source {
                    item.other_transmitters = true;
                }
            }
        }
    }
}

// TODO(future): Consider the responsibilities of 'communciation.rs' and 'distributor.rs'
// And the latter should perhaps be split into submodules.

#[derive(Debug, Default)]
enum UpToSpeedStatus {
    /// We don't know how up-to-date we are with respect to the node
    #[default]
    Uninitialized,
    /// The node has some update heads that we lack.
    ///
    /// The tuple member gives the message ids of the missing messages.
    HeadsNeeded(Vec<MessageId>),
    /// We are up-to-date with respect to this node.
    ///
    /// That is, we know everything it knows.
    UpToSpeed,
}

impl UpToSpeedStatus {
    pub(crate) fn is_up_to_speed(&self) -> bool {
        match self {
            UpToSpeedStatus::Uninitialized | UpToSpeedStatus::HeadsNeeded(_) => false,
            UpToSpeedStatus::UpToSpeed => true,
        }
    }
}

/// Information about closest peers
#[derive(Debug)]
pub struct PeerInfo {
    /// How up-to-date we are with respect to this neighbor
    up_to_speed: UpToSpeedStatus,

    /// The last time an UpdateHeads message was received from this peer
    pub(crate) last_seen: Instant,
}
impl PeerInfo {
    /// Return an uninitialized PeerInfo
    pub fn new(now: Instant) -> PeerInfo {
        PeerInfo {
            up_to_speed: Default::default(),
            last_seen: now,
        }
    }
}

impl Neighborhood {
    /// GC the neighborhood state if timer elapsed
    pub fn gc_if_necessary(
        &mut self,
        our_node_id: EphemeralNodeId,
        periodic_message: Duration,
        now: Instant,
    ) {
        {
            let mut pather = self.fast_pather.write().unwrap();
            if pather.my_id() != our_node_id.raw_u16() {
                info!("identical ephemeral node_id detected, changing our id");
                *pather = MiniPather::new(our_node_id.raw_u16());
                self.peers.clear();
                self.metric_neighbor_count.set(0.0);
                drop(pather);
                return;
            }
        }

        if now.saturating_duration_since(self.last_gc) > periodic_message {
            self.last_gc = now;

            self.inhibited_request_upstream
                .retain(|_k, v| now.saturating_duration_since(v.0) < 32 * periodic_message);

            let mut removed = vec![];
            self.peers.retain(|k, v| {
                let retained = now.saturating_duration_since(v.last_seen) < 3 * periodic_message;
                if !retained {
                    removed.push(*k);
                }
                retained
            });
            self.metric_neighbor_count.set(self.peers.len() as f64);

            let mut pather = self.fast_pather.write().unwrap();
            for item in removed {
                pather.remove_neighbor(item.raw_u16());
            }
        }
    }

    /// Insert the given peer into the neighborhood
    pub fn get_insert_peer(&mut self, peer_id: EphemeralNodeId, now: Instant) -> &mut PeerInfo {
        let mut len = self.peers.len();
        let t = self.peers.entry(peer_id).or_insert_with(|| {
            len += 1;
            PeerInfo::new(now)
        });
        self.metric_neighbor_count.set(len as f64);
        t.last_seen = now;
        t
    }

    pub(crate) fn get_peer(&self, peer_id: EphemeralNodeId) -> Option<&PeerInfo> {
        self.peers.get(&peer_id)
    }
    /// Get the list of neighbors
    pub fn get_neighbors(&self) -> Vec<EphemeralNodeId> {
        let mut t: Vec<_> = self.peers.keys().copied().collect();
        t.sort_unstable();
        t
    }
}

/// Data structure to detect duplicate requests.
///
/// If multiple parts of the algorithm decide to do the same request,
/// we wish to merge these and not emit two identical requests on the net.
#[derive(Debug)]
pub struct DuplicationChecker<T> {
    memory: IndexMap<T, Instant>,
    gc_counter: usize,
    interval: Duration,
}
impl<T: Eq + Hash> DuplicationChecker<T> {
    /// Create a new DuplicationChecker instance, with the
    /// given interval.
    ///
    /// Messages are kept in the checker for approximately the given interval.
    pub fn new(interval: Duration) -> DuplicationChecker<T> {
        DuplicationChecker {
            memory: Default::default(),
            gc_counter: 0,
            interval,
        }
    }
    /// Returns true if duplicate.
    ///
    /// The item is added to the checker, allowing future checks for the same
    /// item to return true.
    pub fn is_duplicate(&mut self, id: T, now: Instant) -> bool {
        self.gc_counter += 1;
        if self.gc_counter > 10000 {
            self.gc_counter = 0;
            self.memory
                .retain(|_k, v| now.saturating_duration_since(*v) <= 2 * self.interval);
        }
        match self.memory.entry(id) {
            Entry::Occupied(mut e) => {
                let prev_elapsed = now.saturating_duration_since(*e.get());
                if prev_elapsed > self.interval {
                    *e.get_mut() = now;
                    false
                } else {
                    true
                }
            }
            Entry::Vacant(e) => {
                e.insert(now);
                false
            }
        }
    }
}

// TODO(future): Maybe nodes that notice that they have more neighbors than basically anybody,
// should try to change node-id to a small number, so they can be natural, efficient relays for everybody.

/// Information about the current node's neighborhood
#[derive(Debug)]
pub struct Neighborhood {
    /// The set of neighbors
    pub peers: IndexMap<EphemeralNodeId, PeerInfo>,
    /// Neighbor-tracker with logic to figure out when retransmits need to be sent
    pub fast_pather: Arc<RwLock<MiniPather>>,

    last_gc: Instant,
    /// The source we've observed for recent messages.
    pub(crate) recent_messages_source: RecentMessages,

    /// Messages that we were totally going to request from upstream, but didn't
    /// because we figured a peer would do it. request_inhibited_based_on_node_numbers has
    /// a counter that is increased when adding stuff here, and decreased whenever we
    /// receive a message in here without having requested it.
    pub(crate) inhibited_request_upstream: IndexMap<MessageId, (Instant, EphemeralNodeId)>,

    /// The number of other peers detected in the network
    metric_neighbor_count: Gauge,
}

impl Neighborhood {
    /// Return a new neighborhood struct with the given pather
    pub fn new(now: Instant, pather: Arc<RwLock<MiniPather>>) -> Neighborhood {
        let neighbor_count = gauge!("neighbor_count");
        describe_gauge!(
            "neighbor_count",
            Unit::Count,
            "Number of distinct neighbors detected"
        );

        Self {
            peers: Default::default(),
            fast_pather: pather,
            last_gc: now,
            recent_messages_source: Default::default(),
            inhibited_request_upstream: Default::default(),
            metric_neighbor_count: neighbor_count,
        }
    }

    /// request_from = the origin of the request we're about to respond to
    /// self_node = ourselves
    pub(crate) fn is_request_upstream_inhibited(
        &mut self,
        request_from: EphemeralNodeId,
        message_ids: &[MessageId],
        now: Instant,
        periodic_interval: Duration,
    ) -> bool {
        let mut fast_pather_guard = self.fast_pather.write().unwrap();
        let fast_pather = &mut *fast_pather_guard;

        if let Some(ordinal) = fast_pather.should_i_ask_for_retransmission(request_from.raw_u16()) {
            let mut inhibit = true;
            for msg in message_ids {
                match self.inhibited_request_upstream.entry(*msg) {
                    Entry::Occupied(o) => {
                        let periods = ((now.saturating_duration_since(o.get().0).as_millis()
                            as u64)
                            / (periodic_interval.as_millis().max(1) as u64))
                            as usize;
                        if periods > ordinal {
                            inhibit = false;
                            o.swap_remove();
                        }
                    }
                    Entry::Vacant(o) => {
                        if ordinal != 0 {
                            o.insert((now, request_from));
                        } else {
                            inhibit = false;
                        }
                    }
                }
            }
            inhibit
        } else {
            // They can't hear us.
            true
        }
    }

    /// Record that the given message has been received over the network
    pub fn record_message(&mut self, message: &DistributorMessage) {
        match message {
            DistributorMessage::ReportHeads { heads, source, .. } => {
                for head in heads {
                    self.recent_messages_source
                        .record_message_source(head.msg, *source, false);
                }
            }
            DistributorMessage::SyncAllQuery(_) => {}
            DistributorMessage::SyncAllRequest(_) => {}
            DistributorMessage::SyncAllAck(_) => {}
            DistributorMessage::RequestUpstream {
                source: _,
                query: _,
                ..
            } => {}
            DistributorMessage::UpstreamResponse {
                source, messages, ..
            } => {
                for msg in messages {
                    self.recent_messages_source
                        .record_message_source(msg.id, *source, false);
                }
            }
            DistributorMessage::SendMessageAndAllDescendants { .. } => {}
            DistributorMessage::Message {
                source,
                message: msg,
                ..
            } => {
                self.inhibited_request_upstream
                    .swap_remove(&msg.message_id());

                self.recent_messages_source
                    .record_message_source(msg.message_id(), *source, true);
            }
        }
    }
}

//TODO(future): Consider merging this with NeighborHood?
/// Output buffer with state
#[derive(Debug)]
pub struct QueryableOutbuffer {
    outbuf: VecDeque<DistributorMessage>,

    request_upstream_message_inhibit: DuplicationChecker<MessageId>,
    periodic_message_interval: Duration,
    recently_sent_upstream_responses_for: DuplicationChecker<MessageId>,

    recently_sent_message_ids: DuplicationChecker<MessageId>,
    gc_timer: Instant,
    parentless_messages: IndexMap</*missing parent*/ MessageId, Vec<AccumulatedMessage>>,
}

impl QueryableOutbuffer {
    pub(crate) fn gc_if_necessary(&mut self, now: Instant) {
        if now.saturating_duration_since(self.gc_timer) > 2 * self.periodic_message_interval {
            self.gc_timer = now;
            let mut ok = 0;
            let mut size_so_far = 0;
            for (_msg_id, msg_objs) in self.parentless_messages.iter().rev() {
                let mut bad = false;
                for msg_obj in msg_objs {
                    size_so_far += msg_obj.ram_size_estimate();
                    if size_so_far < 50_000_000 {
                    } else {
                        bad = true;
                        break;
                    }
                }
                if !bad {
                    ok += 1;
                }
            }
            if ok != self.parentless_messages.len() {
                debug_assert!(ok < self.parentless_messages.len());
                self.parentless_messages.drain(0..ok);
            }
        }
    }

    /// Return true if no message are queued for sending
    pub fn is_empty(&self) -> bool {
        self.outbuf.is_empty()
    }
    /// Pop the first message from the output queue, if any
    pub fn pop_front(&mut self) -> Option<DistributorMessage> {
        let msg = self.outbuf.pop_front();
        msg
    }
    /// Add a message to the send queue
    pub fn push_back(&mut self, msg: DistributorMessage) {
        self.outbuf.push_back(msg);
    }

    /// Add multiple messages to the send queue
    pub fn extend<I: IntoIterator<Item = DistributorMessage>>(&mut self, items: I) {
        self.outbuf.extend(items);
    }
}

impl QueryableOutbuffer {
    fn new(periodic_message_interval: Duration, now: Instant) -> Self {
        Self {
            outbuf: Default::default(),
            request_upstream_message_inhibit: DuplicationChecker::new(
                2 * periodic_message_interval,
            ),
            periodic_message_interval,
            recently_sent_upstream_responses_for: DuplicationChecker::new(
                2 * periodic_message_interval,
            ),
            recently_sent_message_ids: DuplicationChecker::new(2 * periodic_message_interval),
            gc_timer: now,
            parentless_messages: Default::default(),
        }
    }

    fn upstream_response_blocked(&mut self, id: MessageId, now: Instant) -> bool {
        self.recently_sent_upstream_responses_for
            .is_duplicate(id, now)
    }

    fn request_upstream(
        &mut self,
        messages_to_request: &[MessageId],
        self_node: EphemeralNodeId,
        request_from: EphemeralNodeId,
        neighbors: &mut Neighborhood,
        now: Instant,
        uninhibitable: bool,
    ) {
        if !uninhibitable
            && neighbors.is_request_upstream_inhibited(
                request_from,
                messages_to_request,
                now,
                self.periodic_message_interval,
            )
        {
            return;
        }

        let query: Vec<_> = messages_to_request
            .iter()
            .filter_map(|msg| {
                let is_dup = self
                    .request_upstream_message_inhibit
                    .is_duplicate(*msg, now);
                if is_dup {
                    None
                } else {
                    Some((*msg, 4))
                }
            })
            .collect();

        if !query.is_empty() {
            self.outbuf.push_back(DistributorMessage::RequestUpstream {
                query,
                source: self_node,
                destination: request_from,
            });
        }
    }
}

// Principle
// The node that is 'most ahead' (highest MessageId) has responsibility.
// If knows all the heads of other node, just sends perfect updates.
// Otherwise:
// Must request messages until it has complete picture

/// A serialized [`MessageFrame`].
#[derive(Debug, Savefile, Clone)]
pub struct SerializedMessage {
    id: MessageId,
    parents: Vec<MessageId>,
    data: Vec<u8>,
}

impl SerializedMessage {
    /// Create a serialized message from a header and payload
    pub fn from_header_and_body<M: Message>(
        header: MessageHeader,
        payload: M,
    ) -> Result<SerializedMessage> {
        Self::new(MessageFrame { header, payload })
    }

    /// Retain parents for which the closure returns true.
    ///
    /// ALl other parents are removed
    pub fn retain_parents(&mut self, mut predicate: impl FnMut(MessageId) -> bool) {
        self.parents.retain(move |id| predicate(*id));
    }
    /// Return the message id for this message
    pub fn message_id(&self) -> MessageId {
        self.id
    }
    /// Convert to [`MessageFrame<M>`]
    pub fn to_message<M: Message>(self) -> Result<MessageFrame<M>> {
        let reader = Cursor::new(&self.data);
        Ok(MessageFrame {
            header: MessageHeader {
                id: self.id,
                parents: self.parents,
            },
            payload: M::deserialize(&self.data[reader.position() as usize..])?,
        })
    }
    /// Like [`Self::to_message`], but does not take ownership
    pub fn to_message_from_ref<M: Message>(&self) -> Result<MessageFrame<M>> {
        let reader = Cursor::new(&self.data);
        Ok(MessageFrame {
            header: MessageHeader {
                id: self.id,
                parents: self.parents.clone(),
            },
            payload: M::deserialize(&self.data[reader.position() as usize..])?,
        })
    }
    /// Create a new instance based on the given message frame
    pub fn new<M: Message>(m: MessageFrame<M>) -> Result<SerializedMessage> {
        let mut data = vec![];
        m.payload.serialize(&mut data)?;
        Ok(SerializedMessage {
            id: m.header.id,
            parents: m.header.parents.clone(),
            data,
        })
    }
}

/// A message, a set of parents and a query count
#[derive(Debug, Savefile, Clone)]
pub struct MessageSubGraphNode {
    pub(crate) id: MessageId,
    parents: Vec<MessageId>,
    query_count: usize,
}

/// A set of parents, and a query count
pub struct MessageSubGraphNodeValue {
    parents: Vec<MessageId>,
    query_count: usize,
}

/// An address
///
/// Truncated to at most 40 bytes
#[derive(Savefile, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
pub struct Address(ArrayString<40>);

impl PartialEq<str> for Address {
    fn eq(&self, other: &str) -> bool {
        &self.0 == other
    }
}
impl PartialEq<Address> for str {
    fn eq(&self, other: &Address) -> bool {
        self == &other.0
    }
}

impl Address {
    /// The maximum address length fully supported by noatun, in bytes
    pub const MAX_LENGTH: usize = 40;
    /// Convert from a value that implements Display, to Self
    pub fn from(value: impl Display) -> Self {
        use std::fmt::Write;
        let mut x = ArrayString::<40>::new();
        #[cfg(debug_assertions)]
        {
            let mut s = String::new();
            write!(&mut s, "{value}").unwrap();
            assert!(
                s.len() <= 40,
                "address {} (len={}) was too long",
                s,
                s.len()
            );
        }
        write!(&mut x, "{value}").unwrap();
        Address(x)
    }
}

impl Debug for Address {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl Display for Address {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// An ephemeral node id.
///
/// Each node has an ephemeral node id, at any time.
///
/// This id is not necessarily globally unique. However, when nodes detect a neighbor
/// with the same id, the id is re-randomized.
///
/// This is used by the distributor, but not by the database itself.
///
/// Under the hood, this value is a 16-bit integer, which gives enough entropy
/// for approximately a few tens of neighbors. However, we don't require that no
/// collisions occur - collisions mean we change the id dynamically. So we can
/// probably allow 1000 neighbors, which is enough for what Noatun tries to do a
/// and be.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Savefile, PartialOrd, Ord)]
pub struct EphemeralNodeId(u16);

impl Display for EphemeralNodeId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "#{}", colored_int(self.0.into()))
    }
}
impl Debug for EphemeralNodeId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "#{}", colored_int(self.0.into()))
    }
}

#[cfg(test)]
pub(crate) static NON_RANDOM_EPHEMERAL_NODE_ID_COUNTER: std::sync::atomic::AtomicU16 =
    std::sync::atomic::AtomicU16::new(0);

impl EphemeralNodeId {
    /// Return the inner 16 bit value
    pub fn raw_u16(self) -> u16 {
        self.0
    }
    /// Create an instance wrapping the given 16 bit value
    pub fn new(value: u16) -> Self {
        EphemeralNodeId(value)
    }
    /// Create a random EphemeralNodeId
    pub fn random() -> EphemeralNodeId {
        #[cfg(test)]
        {
            if crate::FOR_TEST_NON_RANDOM_ID {
                let id = NON_RANDOM_EPHEMERAL_NODE_ID_COUNTER
                    .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
                return EphemeralNodeId(id);
            }
        }
        EphemeralNodeId(random())
    }
}

//TODO(future): Add a quick-join mechanism. A newly started node can force all nodes to send an
//extra ReportHeads, after which the newly joined nodes sends one of its own. Thus we
//get rid of the 1-periodic msg delay of waiting for ReportHeads-messages

/// An updatehead, and the node we believe wrote this update head (if known)
#[derive(Debug, Savefile, Clone)]
pub struct Head {
    /// The message id
    pub msg: MessageId,
    /// The origin of said message, if known
    pub origin: Option<EphemeralNodeId>,
}

/// A message sent by the distributor
///
/// This describes every message of the distributor protocol
#[derive(Debug, Savefile, Clone)]
pub enum DistributorMessage {
    /// Report all update heads for the sender
    ReportHeads {
        /// The sender of the message
        source: EphemeralNodeId,
        /// The cutoff hash for the sender
        cutoff: CutOffHashPos,
        /// The update heads of the sender
        heads: Vec<Head>,
        /// Neighbors known by sender
        ///
        neighbors: Vec<EphemeralNodeId>,
    },
    /// A query to tell if the listed messages are known.
    ///
    /// If they are not, they should be requested by SyncAllRequest.
    /// I.e, if the node receiving a SyncAllRequest doesn't have any of the messages
    /// listed, they should be requested using SyncAllRequest.
    SyncAllQuery(Vec<MessageId>),
    /// The given messages should be sent.
    SyncAllRequest(Vec<MessageId>),
    /// Sent only when doing a full sync
    SyncAllAck(Vec<MessageId>),
    /// Report a cut in the source node message graph
    RequestUpstream {
        /// Sender of this distributor message
        source: EphemeralNodeId,
        /// the usize is How many levels to ascend from message
        query: Vec<(MessageId, usize)>,
        /// Destination of request
        destination: EphemeralNodeId,
    },
    /// Response to a RequestUpstream, giving information about the message graph
    UpstreamResponse {
        /// Sender of this distributor message
        source: EphemeralNodeId,
        /// Destination of response
        dest: EphemeralNodeId,
        /// Messages
        messages: Vec<MessageSubGraphNode>,
    },
    /// Command the recipient to send all descendants of the given messages
    SendMessageAndAllDescendants {
        /// Sender of this distributor message
        source: EphemeralNodeId,
        /// Node that is asked to send all descendants
        destination: EphemeralNodeId,
        /// Messages whose descendants should be sent
        message_id: Vec<MessageId>,
    },
    /// Actual messages
    Message {
        /// Sender of this distributor message
        source: EphemeralNodeId,
        /// Message
        message: SerializedMessage,
        /// True if we demand recipient to ack this message
        demand_ack: bool,

        /// Original sender of this message, as far as we know.
        /// If this message was created on the node that sent it, this means it cannot be squelched.
        origin: EphemeralNodeId,
        /// True if this message was transmitted because it was explicitly requested
        explicit_retransmit: bool,
    },
}

impl DistributorMessage {
    /// Source of this message
    pub fn source(&self) -> Option<EphemeralNodeId> {
        match self {
            DistributorMessage::ReportHeads { source, .. } => Some(*source),
            DistributorMessage::SyncAllQuery(_) => None,
            DistributorMessage::SyncAllRequest(_) => None,
            DistributorMessage::SyncAllAck(_) => None,
            DistributorMessage::RequestUpstream { source, .. } => Some(*source),
            DistributorMessage::UpstreamResponse { source, .. } => Some(*source),
            DistributorMessage::SendMessageAndAllDescendants { source, .. } => Some(*source),
            DistributorMessage::Message { source, .. } => Some(*source),
        }
    }
    /// Debug representation of this message
    pub fn debug_format<M: Message>(&self) -> Result<String> {
        Ok(match self {
            DistributorMessage::ReportHeads {
                cutoff,
                heads,
                source,
                neighbors,
            } => {
                format!(
                    "{}: cutoff: {cutoff}, heads: {:?}, src: {:?}, neigh: {:?}",
                    lightgray("ReportHeads"),
                    heads,
                    source,
                    neighbors
                )
            }
            DistributorMessage::SyncAllQuery(messages) => {
                format!("{}: Messages: {:?}", lightgreen("SyncAllQuery"), messages)
            }
            DistributorMessage::SyncAllRequest(messages) => {
                format!("{}: Messages: {:?}", orange("SyncAllRequest"), messages)
            }
            DistributorMessage::SyncAllAck(messages) => {
                format!("{}: Messages: {:?}", lightbluegreen("SyncAllAck"), messages)
            }
            DistributorMessage::RequestUpstream {
                source,
                query,
                destination,
            } => {
                format!(
                    "{}: Query: {:?}, src: {:?}, dst: {:?}",
                    red("RequestUpstream"),
                    query,
                    source,
                    destination
                )
            }
            DistributorMessage::UpstreamResponse {
                source,
                dest,
                messages,
            } => {
                format!(
                    "{}: Messages: {:?}, src: {:?}, dst: {:?}",
                    lightbrown("UpstreamResponse"),
                    messages,
                    source,
                    dest
                )
            }
            DistributorMessage::SendMessageAndAllDescendants {
                source,
                message_id,
                destination,
            } => {
                format!(
                    "{}: messages: {:?}, src: {:?}, dst: {:?}",
                    pink("SendMessageAndAllDescendants"),
                    message_id,
                    source,
                    destination
                )
            }
            DistributorMessage::Message {
                source,
                message,
                demand_ack,
                origin,
                explicit_retransmit,
            } => {
                let msg: MessageFrame<M> = message.to_message_from_ref()?;
                format!(
                    "{}: id = {}, parents = {:?}, need_ack = {}, msg = {:?}, src = {:?}, origin = {:?}, retransmit = {}",
                    turqouise("Message"),
                    msg.header.id,
                    msg.header.parents,
                    demand_ack,
                    msg.payload,
                    source,
                    origin,
                    explicit_retransmit
                )
            }
        })
    }
}

#[derive(Debug)]
enum SyncAllState {
    NotActive,
    Starting,
    /// Query messages starting at 'MessageId', inclusive
    BeginQuery(MessageId),
    /// The current query concerns messages a..=b (i.e, note, inclusive of 'b')
    QueryActive(
        /*a*/ MessageId,
        /*b*/ MessageId,
        IndexSet<MessageId>,
    ),
}

impl SyncAllState {
    pub fn idle(&self) -> bool {
        match self {
            SyncAllState::NotActive => true,
            SyncAllState::Starting
            | SyncAllState::BeginQuery(_)
            | SyncAllState::QueryActive(_, _, _) => false,
        }
    }
}

/// Status of distributor, including information about peers
#[derive(Debug)]
pub struct DistributorStatus {
    nominal: bool,
    most_recent_clockdrift: HashMap<EphemeralNodeId, Instant>,
    most_recent_unsynced: HashMap<EphemeralNodeId, Instant>,
    have_heard_peer: bool,
    last_gc: Instant,
}

impl DistributorStatus {
    fn gc_if_necessary(&mut self, now: Instant) {
        if now.saturating_duration_since(self.last_gc).as_millis() > 60000 {
            self.last_gc = now;
            self.most_recent_unsynced
                .retain(|_k, v| now.saturating_duration_since(*v).as_millis() < 60000);
            self.most_recent_clockdrift
                .retain(|_k, v| now.saturating_duration_since(*v).as_millis() < 60000);
        }
    }
}

/// The distributor ensures the messages of a Database are synchronized
/// to other peers in the network.
#[derive(Debug)]
pub struct Distributor {
    #[doc(hidden)]
    pub ephemeral_node_id: ArcShift<EphemeralNodeId>,
    // A sync-all request is in progress.
    // It sends all Messages in MessageId-order (which guarantees that all
    // parents will be sent before any children.
    sync_all_inprogress: SyncAllState,

    distributor_state: DistributorStatus,

    periodic_message_interval: Duration,
    // TODO(future): Clean this up. The repsonsibilities of `QueryableOutbuffer` vs `Neighborhood`
    // are very unclear
    // TODO(future): This should not be public.
    #[doc(hidden)]
    pub outbuf: QueryableOutbuffer,
    #[doc(hidden)]
    pub neighborhood: Neighborhood,
    auto_resync: bool,
}

/// Synchronization status
#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Status {
    /// Synchronization has been achieved
    Nominal,
    /// Synchronization could not be achieved, because clocks are too far off
    BadClocksDetected,
    /// Nodes are not synchronized
    OutOfSync,
    /// There are no peers to synchronize with
    NoPeers,
    /// Synchronization is happening
    Synchronizing,
}

impl Display for Status {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Status::Nominal => {
                write!(f, "Nominal")
            }
            Status::BadClocksDetected => {
                write!(f, "BadClocksDetected")
            }
            Status::OutOfSync => {
                write!(f, "OutOfSync")
            }
            Status::NoPeers => {
                write!(f, "NoPeers")
            }
            Status::Synchronizing => {
                write!(f, "Synchronizing")
            }
        }
    }
}

/// Truncate the given name to a limited length 'Address'.
pub fn truncate_to_address(name: &str) -> Address {
    if name.len() <= Address::MAX_LENGTH {
        return Address::from(name);
    }
    for i in (1..=Address::MAX_LENGTH).rev() {
        if name.is_char_boundary(i) {
            return Address::from(name.split_at(i).0);
        }
    }
    Address::from("?")
}

#[derive(Debug)]
pub(crate) struct AccumulatedMessage {
    msg: SerializedMessage,
    /// The sender of this message. Might not be original origin, since
    /// message relaying occurs
    source: EphemeralNodeId,
    /// Equal to source for messages created _at_ source.
    origin: EphemeralNodeId,
    need_ack: bool,
    /// Is explicit re-transmission (i.e, not an automatic forwarding or organic reception)
    explicit_retransmit: bool,
}

impl AccumulatedMessage {
    fn ram_size_estimate(&self) -> usize {
        self.msg.data.len() + std::mem::size_of::<Self>()
    }
}

impl Distributor {
    /// The interval between periodic messages
    pub fn periodic_message_interval(&self) -> Duration {
        self.periodic_message_interval
    }
    const BATCH_SIZE: usize = 20;

    /// Create a new distributor
    pub fn new(
        periodic_message_interval: Duration,
        mut initial_node_id: ArcShift<EphemeralNodeId>,
        now: Instant,
        mini_pather: Option<Arc<RwLock<MiniPather>>>,
        auto_resync: bool,
    ) -> Distributor {
        let node = initial_node_id.get().raw_u16();
        Self {
            sync_all_inprogress: SyncAllState::NotActive,
            distributor_state: DistributorStatus {
                nominal: false,
                most_recent_clockdrift: Default::default(),
                most_recent_unsynced: Default::default(),
                have_heard_peer: false,
                last_gc: now,
            },
            periodic_message_interval,
            neighborhood: Neighborhood::new(
                now,
                mini_pather.unwrap_or(Arc::new(RwLock::new(MiniPather::new(node)))),
            ),
            ephemeral_node_id: initial_node_id,
            outbuf: QueryableOutbuffer::new(periodic_message_interval, now),
            auto_resync,
        }
    }

    /// The current ephemeral node id for this distributor
    pub fn node_id(&mut self) -> EphemeralNodeId {
        *self.ephemeral_node_id.get()
    }

    /// Returns the current status of the system with respect to clock drift.
    ///
    /// If clock drift has been observed with the last 60 seconds, it will be reported
    /// by this method.
    pub fn get_status(&self, now: Instant) -> Status {
        for drift in self.distributor_state.most_recent_clockdrift.values() {
            if now.saturating_duration_since(*drift).as_millis() < 60000 {
                return Status::BadClocksDetected;
            }
        }

        for unsync in self.distributor_state.most_recent_unsynced.values() {
            let unsync_t = now.saturating_duration_since(*unsync).as_millis();
            if unsync_t < 60000 {
                return Status::OutOfSync;
            }
        }
        if !self.distributor_state.have_heard_peer {
            return Status::NoPeers;
        }
        if self.distributor_state.nominal {
            return Status::Nominal;
        }
        Status::Synchronizing
    }

    /// Call this to retrieve a message that should be sent periodically
    pub fn get_periodic_message<MSG: Message + 'static>(
        &mut self,
        database: &DatabaseSession<MSG>,
        now: Instant,
    ) -> Result<Vec<DistributorMessage>> {
        self.neighborhood.gc_if_necessary(
            *self.ephemeral_node_id.get(),
            self.periodic_message_interval,
            now,
        );

        self.outbuf.gc_if_necessary(now);

        self.distributor_state.gc_if_necessary(now);

        let mut heads = database
            .get_update_heads()
            .iter()
            .map(|msg_id| {
                let origin = self
                    .neighborhood
                    .recent_messages_source
                    .recent_messages
                    .get(msg_id)
                    .map(|x| x.original_source);
                Head {
                    msg: *msg_id,
                    origin,
                }
            })
            .collect::<Vec<_>>();
        heads.sort_by_key(|head| head.msg);
        let mut temp = vec![DistributorMessage::ReportHeads {
            source: *self.ephemeral_node_id.get(),
            cutoff: database.current_cutoff_state()?,
            heads,
            neighbors: self.neighborhood.get_neighbors(),
        }];
        let sync_from = match &self.sync_all_inprogress {
            SyncAllState::NotActive => None,
            SyncAllState::Starting => Some(MessageId::ZERO),
            SyncAllState::BeginQuery(start) => Some(*start),
            SyncAllState::QueryActive(from, _to, _request_identity) => Some(*from),
        };
        if let Some(sync_from) = sync_from {
            let cur_batch = database.get_messages_at_or_after(sync_from, Self::BATCH_SIZE)?;
            info!(
                "All messages after {:?} turned out to be {:?}",
                sync_from, cur_batch
            );
            if cur_batch.is_empty() {
                self.sync_all_inprogress = SyncAllState::NotActive;
            } else {
                self.sync_all_inprogress = SyncAllState::QueryActive(
                    *cur_batch.first().unwrap(),
                    *cur_batch.last().unwrap(),
                    cur_batch.iter().copied().collect(),
                );
                temp.push(DistributorMessage::SyncAllQuery(cur_batch));
            }
        }

        Ok(temp)
    }

    // Legacy, for some old tests
    #[cfg(test)]
    pub(crate) fn receive_message2<MSG: Message>(
        &mut self,
        database: &mut Database<MSG>,
        input: impl Iterator<Item = DistributorMessage>,
        now: Instant,
    ) -> Result<Vec<DistributorMessage>> {
        self.receive_message(
            database,
            input.map(|x| (None, x.source().unwrap_or(EphemeralNodeId(0)), x)),
            now,
        )?;
        let ret = self.outbuf.outbuf.drain(..).collect();
        self.outbuf = QueryableOutbuffer::new(self.periodic_message_interval, now);
        Ok(ret)
    }

    /// Call this when a node id collision has been detected.
    ///
    /// This can often be detected quicker by lower layers, this is available
    /// as a public method. A new unique node id will be generated.
    pub fn report_node_id_collision(&mut self) {
        self.ephemeral_node_id.update(EphemeralNodeId::random());
    }

    /// Note, loopback messages should be detected by caller
    /// This method will interpret incoming node-ids identical to our own as a node id
    /// collision, not as a message loopback
    pub fn receive_message<MSG: Message>(
        &mut self,
        database: &mut Database<MSG>,
        input: impl Iterator<Item = (Option<Address>, EphemeralNodeId, DistributorMessage)>,
        now: Instant,
    ) -> Result<()> {
        let mut database = database.begin_session_mut()?;
        database.maybe_advance_cutoff()?;
        let mut accumulated_heads: IndexMap<
            MessageId,
            Vec<(
                /*source:*/ EphemeralNodeId,
                /*origin:*/ Option<EphemeralNodeId>,
            )>,
        > = IndexMap::new();
        let mut accumulated_request_upstream = IndexMap::new();
        let mut accumulated_upstream_responses: IndexMap<
            MessageId,
            /*parents*/
            (
                EphemeralNodeId, /*src*/
                EphemeralNodeId, /*dst*/
                MessageSubGraphNodeValue,
            ),
        > = IndexMap::new();
        let mut accumulated_send_msg_and_descendants = IndexMap::new();
        let mut accumulated_serialized = vec![];
        let mut accumulated_sync_all_queries = IndexSet::new();
        let mut accumulated_sync_all_requests = IndexSet::new();

        let our_node_id = *self.ephemeral_node_id.get();
        let mut collision = false;
        let mut check_node_id_collision = |observed: EphemeralNodeId| {
            if observed == our_node_id {
                collision = true;
            }
        };

        for (_src_addr, src, item) in input {
            self.neighborhood.record_message(&item);

            self.distributor_state.have_heard_peer = true;
            match item {
                DistributorMessage::SyncAllQuery(query) => {
                    accumulated_sync_all_queries.extend(query);
                }
                DistributorMessage::SyncAllRequest(requests) => {
                    accumulated_sync_all_requests.extend(requests);
                }

                DistributorMessage::ReportHeads {
                    source,
                    cutoff: cutoff_hash,
                    heads,
                    neighbors,
                } => {
                    check_node_id_collision(source);
                    for pass in 0..2 {
                        match database.is_acceptable_cutoff_hash(cutoff_hash)? {
                            Acceptability::Previous | Acceptability::Nominal => {
                                // If the neighbor has no neighbors of its own, it's just starting up.
                                // Let's wait a bit before acting on its messages

                                self.neighborhood
                                    .fast_pather
                                    .write()
                                    .unwrap()
                                    .report_neighbors(
                                        source.raw_u16(),
                                        neighbors.iter().map(|x| x.raw_u16()),
                                    );

                                let peer = self.neighborhood.get_insert_peer(source, now);

                                match &mut peer.up_to_speed {
                                    UpToSpeedStatus::Uninitialized => {
                                        peer.up_to_speed = UpToSpeedStatus::HeadsNeeded(
                                            heads.iter().map(|x| x.msg).collect(),
                                        );
                                    }
                                    UpToSpeedStatus::HeadsNeeded(needed) => {
                                        needed.retain(|x| {
                                            !database.contains_message(*x).unwrap_or(false)
                                        });
                                        if needed.is_empty() {
                                            peer.up_to_speed = UpToSpeedStatus::UpToSpeed;
                                        }
                                    }
                                    UpToSpeedStatus::UpToSpeed => {}
                                }

                                if self.sync_all_inprogress.idle() && neighbors.is_empty() == false
                                {
                                    for head in heads {
                                        let sources =
                                            accumulated_heads.entry(head.msg).or_default();
                                        if !sources.contains(&(source, head.origin)) {
                                            sources.push((source, head.origin));
                                        }
                                    }
                                }

                                debug!("Acceptability: Nominal");

                                self.distributor_state.most_recent_unsynced.remove(&src);
                                self.distributor_state.most_recent_clockdrift.remove(&src);

                                self.neighborhood
                                    .fast_pather
                                    .write()
                                    .unwrap()
                                    .report_own_neighbors(
                                        self.neighborhood.peers.keys().map(|x| x.raw_u16()),
                                    );

                                debug_assert!(neighbors.is_sorted());
                                break;
                            }
                            Acceptability::Unacceptable => {
                                debug!("Acceptability: Unacceptable");
                                {
                                    self.distributor_state
                                        .most_recent_unsynced
                                        .insert(src, Instant::now());
                                }

                                if self.sync_all_inprogress.idle() && self.auto_resync {
                                    self.sync_all_inprogress = SyncAllState::Starting;
                                }
                                break;
                            }
                            Acceptability::Undecided(advance) => {
                                debug!("Acceptability: Undecided");
                                // We know this won't advance too far, because is_acceptable_cutoff_hash
                                // never advances far
                                database.advance_cutoff(advance)?;
                                if pass == 1 {
                                    // If we get here, in pass 1, it means we apparently didn't
                                    // advance to the correct place in the first pass. This is
                                    // not expected.
                                    error!(
                                        "unexpected case, cutoff hash considered undecided twice"
                                    )
                                }
                            }
                            Acceptability::UnacceptablePeerClockDrift => {
                                /*if self.sync_all_inprogress.idle() {
                                    accumulated_heads.extend(heads);
                                }*/
                                info!("Acceptability: Clockdrift");
                                self.distributor_state
                                    .most_recent_clockdrift
                                    .insert(src, Instant::now());
                                break;
                            }
                        }
                    }
                }
                DistributorMessage::RequestUpstream {
                    source,
                    query,
                    destination,
                } => {
                    check_node_id_collision(source);

                    for (msg, count) in query {
                        // TODO(future): Consider if this is fast enough to do unbatched here? (and is batching really faster?)
                        if !database.contains_message(msg)? {
                            continue;
                        }
                        trace!("Considering {:?} query: {:?}", source, msg);
                        if destination == *self.ephemeral_node_id.get() {
                            let accum_count = accumulated_request_upstream
                                .entry(msg)
                                .or_insert((0usize, source));
                            accum_count.0 = (accum_count.0).max(count);
                            accum_count.1 = (accum_count.1).min(source);
                        }
                    }
                }
                DistributorMessage::UpstreamResponse {
                    source,
                    dest,
                    messages,
                } => {
                    check_node_id_collision(source);

                    if dest == *self.ephemeral_node_id.get() {
                        for msg in messages {
                            let val = MessageSubGraphNodeValue {
                                parents: msg.parents,
                                query_count: msg.query_count,
                            };
                            match accumulated_upstream_responses.entry(msg.id) {
                                Entry::Occupied(mut e) => {
                                    if e.get_mut().0 < source {
                                        e.insert((source, dest, val));
                                    }
                                }
                                Entry::Vacant(e) => {
                                    e.insert((source, dest, val));
                                }
                            }
                        }
                    }
                }
                DistributorMessage::SendMessageAndAllDescendants {
                    source,
                    message_id,
                    destination,
                } => {
                    check_node_id_collision(source);

                    if destination == *self.ephemeral_node_id.get() {
                        for msg in message_id {
                            match accumulated_send_msg_and_descendants.entry(msg) {
                                Entry::Occupied(mut e) => {
                                    if *e.get() > source {
                                        e.insert(source);
                                    }
                                }
                                Entry::Vacant(e) => {
                                    e.insert(source);
                                }
                            }
                        }
                    }
                }
                DistributorMessage::Message {
                    source,
                    message: mut msg,
                    demand_ack: need_ack,
                    origin,
                    explicit_retransmit,
                } => {
                    check_node_id_collision(source);

                    database.remove_cutoff_parents(&mut msg.parents);
                    accumulated_serialized.push(AccumulatedMessage {
                        msg,
                        need_ack,
                        source,
                        origin,
                        explicit_retransmit,
                    });
                }
                DistributorMessage::SyncAllAck(acked) => match &mut self.sync_all_inprogress {
                    SyncAllState::QueryActive(from, to, items) => {
                        debug!("Processing active query: {:?}..{:?}", from, to);
                        for ack in &acked {
                            items.swap_remove(ack);
                        }
                        debug!("Active items: {:?}, after acks: {:?}", items, acked);
                        if items.is_empty() {
                            info!("Advance state of active query to {:?}", *to);
                            self.sync_all_inprogress = SyncAllState::BeginQuery(to.successor());
                        }
                    }
                    SyncAllState::NotActive => {}
                    SyncAllState::Starting => {}
                    SyncAllState::BeginQuery(_) => {}
                },
            }
        }

        self.process_reported_heads(&mut database, accumulated_heads, now)?;

        accumulated_request_upstream.sort_keys();

        self.process_request_upstream(&mut database, accumulated_request_upstream, now)?;
        self.process_upstream_response(&mut database, accumulated_upstream_responses, now)?;
        self.process_send_message_all_descendants(
            &mut database,
            accumulated_send_msg_and_descendants,
            now,
        )?;

        for i in 0..1000 {
            if i == 1000 - 1 {
                error!("Unexpected iteration count {}", i);
            }
            // Temp is messages that previously couldn't be imported because they
            // were missing their parents. One or more parents have now become known.
            let temp = self.process_received_messages(
                &mut database,
                std::mem::take(&mut accumulated_serialized),
                now,
            )?;
            if temp.is_empty() {
                break;
            }
            accumulated_serialized = temp;
        }
        self.process_sync_all_queries(&mut database, accumulated_sync_all_queries)?;
        self.process_sync_all_requests(&mut database, accumulated_sync_all_requests, now)?;

        if collision {
            self.report_node_id_collision();
        }

        database.commit()?;
        Ok(())
    }

    fn process_sync_all_queries<MSG: Message>(
        &mut self,
        database: &mut DatabaseSessionMut<MSG>,
        accumulated_sync_all_queries: IndexSet<MessageId>,
    ) -> Result<()> {
        let mut request = vec![];
        let mut acks = vec![];
        for query in accumulated_sync_all_queries {
            if !database.contains_message(query)? {
                request.push(query);
            } else {
                acks.push(query);
            }
        }
        if !request.is_empty() {
            self.outbuf
                .push_back(DistributorMessage::SyncAllRequest(request));
        }
        if !acks.is_empty() {
            self.outbuf.push_back(DistributorMessage::SyncAllAck(acks));
        }
        Ok(())
    }

    fn process_sync_all_requests<MSG: Message>(
        &mut self,
        database: &mut DatabaseSessionMut<MSG>,
        accumulated_sync_all_requests: IndexSet<MessageId>,
        now: Instant,
    ) -> Result<()> {
        for request in accumulated_sync_all_requests {
            match database.load_message(request) {
                Ok(msg) => {
                    if self
                        .outbuf
                        .recently_sent_message_ids
                        .is_duplicate(msg.id(), now)
                        == false
                    {
                        self.outbuf.push_back(DistributorMessage::Message {
                            source: *self.ephemeral_node_id.get(),
                            origin: self
                                .neighborhood
                                .recent_messages_source
                                .recent_messages
                                .get(&msg.id())
                                .map(|x| x.original_source)
                                .unwrap_or(*self.ephemeral_node_id.get()),
                            message: SerializedMessage::new(msg)?,
                            demand_ack: true,
                            explicit_retransmit: true,
                        });
                    }
                }
                Err(err) => {
                    warn!(
                        "Received request for message {:?}, that we couldn't load: {:?}",
                        request, err
                    );
                }
            }
        }
        Ok(())
    }

    fn process_reported_heads<MSG: Message>(
        &mut self,
        database: &mut DatabaseSessionMut<MSG>,
        accumulated_heads: IndexMap<
            MessageId,
            Vec<(
                /*source:*/ EphemeralNodeId,
                /*origin:*/ Option<EphemeralNodeId>,
            )>,
        >,
        now: Instant,
    ) -> Result<()> {
        self.distributor_state.nominal = true;

        let mut messages_to_request_from_source = IndexMap::<_, Vec<_>>::new();
        for (message, srcs) in accumulated_heads {
            assert!(!srcs.is_empty());
            if database.contains_message(message)? {
                continue;
            }

            let (min_src, _origin) = if srcs.len() == 1 {
                srcs[0]
            } else {
                srcs.iter()
                    .min_by_key(|(src, _)| {
                        let score = self
                            .neighborhood
                            .get_peer(*src)
                            .map(|x| {
                                if x.up_to_speed.is_up_to_speed() {
                                    0u8
                                } else {
                                    1
                                }
                            })
                            .unwrap_or(2);
                        (score, src)
                    })
                    .copied()
                    .unwrap()
            };

            messages_to_request_from_source
                .entry(min_src)
                .or_default()
                .push(message);
        }
        for (src, msgs) in messages_to_request_from_source.into_iter() {
            self.outbuf.request_upstream(
                &msgs,
                *self.ephemeral_node_id.get(),
                src,
                &mut self.neighborhood,
                now,
                false,
            );
            self.distributor_state.nominal = false;
        }
        Ok(())
    }
    fn process_request_upstream<MSG: Message>(
        &mut self,
        database: &mut DatabaseSessionMut<MSG>,
        accumulated_heads: IndexMap<MessageId, (usize, /*src:*/ EphemeralNodeId)>,
        now: Instant,
    ) -> Result<()> {
        let mut by_src: IndexMap<EphemeralNodeId, Vec<(MessageId, usize)>> = IndexMap::new();

        for (msg, (count, src)) in accumulated_heads {
            by_src.entry(src).or_default().push((msg, count));
        }
        for (src, heads) in by_src {
            let messages: Vec<MessageSubGraphNode> = database
                .get_upstream_of(heads.into_iter())?
                .filter(|x| self.outbuf.upstream_response_blocked(x.0.id, now) == false)
                .map(|(msg, query_count)| MessageSubGraphNode {
                    id: msg.id,
                    parents: msg.parents,
                    query_count,
                })
                .collect();

            if !messages.is_empty() {
                self.outbuf.push_back(DistributorMessage::UpstreamResponse {
                    messages,
                    dest: src,
                    source: *self.ephemeral_node_id.get(),
                });
            }
        }

        Ok(())
    }
    fn process_upstream_response<MSG: Message>(
        &mut self,
        database: &mut DatabaseSessionMut<MSG>,
        upstream_response: IndexMap<
            MessageId,
            /*parents*/
            (
                EphemeralNodeId, /*src*/
                EphemeralNodeId, /*dst*/
                MessageSubGraphNodeValue,
            ),
        >,
        now: Instant,
    ) -> Result<()> {
        let mut unknowns: IndexMap<EphemeralNodeId, Vec<(MessageId, usize)>> = IndexMap::new();
        let mut send_cmds = IndexMap::new();
        for (msg_id, (msg_source, msg_dest, msg_value)) in upstream_response.iter() {
            if database.contains_message(*msg_id)? {
                continue; //We already have this one
            }
            if *msg_dest != *self.ephemeral_node_id.get() {
                continue;
            }

            let mut err = Ok(());

            let missing_parents = msg_value
                .parents
                .iter()
                .copied()
                .filter(|x| {
                    let have_parent = database
                        .contains_message(*x)
                        .map_err(|e| {
                            error!("Error: {e:?}");
                            err = Err(e);
                        })
                        .is_ok_and(|x| x);
                    !have_parent
                })
                .collect::<Vec<_>>();
            debug_assert!(err.is_ok());
            err?;

            if missing_parents.is_empty() {
                // We have all the parents, a perfect msg to request!
                info!(
                    "Requesting msg.id={:?}, because we have all its parents: {:?}",
                    msg_id, msg_value.parents
                );

                send_cmds.insert(*msg_id, msg_source);
                continue;
            }

            unknowns.entry(*msg_source).or_default().extend(
                missing_parents
                    .iter()
                    .map(|x| (*x, (2 * msg_value.query_count).min(256))),
            );
        }
        if send_cmds.is_empty() == false {
            let mut msg_by_source = IndexMap::<_, Vec<_>>::new();
            for (k, v) in send_cmds {
                msg_by_source.entry(*v).or_default().push(k);
            }
            for (src, messages) in msg_by_source {
                self.outbuf
                    .push_back(DistributorMessage::SendMessageAndAllDescendants {
                        message_id: messages,
                        source: *self.ephemeral_node_id.get(),
                        destination: src,
                    });
            }
        }
        if unknowns.is_empty() == false {
            for (src_node, unknowns) in unknowns {
                let unknowns = unknowns.into_iter().map(|x| x.0).collect::<Vec<_>>();
                self.outbuf.request_upstream(
                    &unknowns,
                    *self.ephemeral_node_id.get(),
                    src_node,
                    &mut self.neighborhood,
                    now,
                    true,
                );
            }
        }

        Ok(())
    }
    fn process_send_message_all_descendants<MSG: Message>(
        &mut self,
        database: &mut DatabaseSessionMut<MSG>,
        mut message_list: IndexMap<MessageId, EphemeralNodeId>,
        now: Instant,
    ) -> Result<()> {
        let mut message_list: VecDeque<_> = message_list.drain(..).collect();
        while let Some((msg, src)) = message_list.pop_front() {
            let msg = match database.load_message(msg) {
                Ok(msg) => msg,
                Err(err) => {
                    tracing::warn!("could not find requested message {:?}: {:?}", msg, err);
                    continue;
                }
            };
            let msg_id = msg.id();

            if self
                .outbuf
                .recently_sent_message_ids
                .is_duplicate(msg.id(), now)
                == false
            {
                self.outbuf.push_back(DistributorMessage::Message {
                    origin: self
                        .neighborhood
                        .recent_messages_source
                        .recent_messages
                        .get(&msg_id)
                        .map(|x| x.original_source)
                        .unwrap_or(*self.ephemeral_node_id.get()),
                    message: SerializedMessage::new(msg)?,
                    source: *self.ephemeral_node_id.get(),
                    demand_ack: false,
                    explicit_retransmit: true,
                });
            }

            let children = database.get_message_children(msg_id)?;
            message_list.extend(children.iter().map(|x| (*x, src)));
            #[cfg(debug_assertions)]
            {
                let mut children = children;
                let mut actual_children = vec![];
                for child_msg in database.get_all_messages()? {
                    if child_msg.header.parents.contains(&msg_id) {
                        actual_children.push(child_msg.id());
                    }
                }
                actual_children.sort();
                children.sort();
                assert_eq!(actual_children, children);
            }
        }

        Ok(())
    }

    /// Returns list of messages whose parents are now known
    fn process_received_messages<MSG: Message>(
        &mut self,
        database: &mut DatabaseSessionMut<MSG>,
        mut message_list: Vec<AccumulatedMessage>,
        _now: Instant,
    ) -> Result<Vec<AccumulatedMessage>> {
        let mut released_list = Vec::new();

        message_list.sort_by_key(|x| x.msg.id);

        let mut chosen_messages = IndexMap::new();
        'msg_iter: for AccumulatedMessage {
            msg,
            source,
            origin,
            need_ack,
            explicit_retransmit,
        } in message_list.into_iter()
        {
            for parent in msg.parents.iter() {
                if database.contains_message(*parent)? == false
                    && !chosen_messages.contains_key(parent)
                // message_list is sorted by id (i.e, also by time), so parent should be found here
                {
                    warn!(
                        "Could not apply message {:?} because parent {:?} is not known",
                        msg.id, parent
                    );

                    let parent = *parent;

                    let accum = AccumulatedMessage {
                        msg,
                        source,
                        origin,
                        need_ack,
                        explicit_retransmit,
                    };
                    match self.outbuf.parentless_messages.entry(parent) {
                        Entry::Occupied(mut e) => {
                            if !e.get().iter().any(|x| x.msg.id == accum.msg.id) {
                                e.get_mut().push(accum);
                            }
                        }
                        Entry::Vacant(e) => {
                            e.insert(vec![accum]);
                        }
                    }
                    continue 'msg_iter;
                }
            }

            let already_present = database.contains_message(msg.id)?;
            if !already_present {
                if self
                    .neighborhood
                    .fast_pather
                    .write()
                    .unwrap()
                    .should_i_forward(origin.raw_u16(), source.raw_u16())
                {
                    self.outbuf.push_back(DistributorMessage::Message {
                        source: *self.ephemeral_node_id.get(),
                        message: msg.clone(),
                        demand_ack: false,
                        origin,
                        explicit_retransmit,
                    });
                }
            }

            if let Some(released) = self.outbuf.parentless_messages.swap_remove(&msg.id) {
                released_list.extend(released);
            }

            chosen_messages.insert(msg.id, (msg, need_ack));
        }

        let mut to_ack = vec![];
        let messages: Vec<_> = chosen_messages
            .into_values()
            .map(|(x, need_ack)| (x.to_message(), need_ack))
            .filter_map(|(x, need_ack)| match x {
                Ok(x) => {
                    if need_ack {
                        to_ack.push(x.header.id);
                    }
                    Some(x)
                }
                Err(x) => {
                    error!("Message could not be deserialized: {:?}", x);
                    None
                }
            })
            .inspect(|x| {
                debug!("Append received message: {:?}", x);
            })
            .collect();

        database.append_many(messages.iter(), false, false)?;
        if !to_ack.is_empty() {
            self.outbuf
                .push_back(DistributorMessage::SyncAllAck(to_ack));
        }
        Ok(released_list)
    }
}

#[cfg(test)]
mod tests {
    use super::truncate_to_address;
    #[test]
    fn do_test_truncate() {
        assert_eq!(
            truncate_to_address("012345678901234567890123456789012345678﷽")
                .0
                .as_str(),
            "012345678901234567890123456789012345678"
        );
        assert_eq!(
            truncate_to_address("0123456789012345678901234567890123456789A")
                .0
                .as_str(),
            "0123456789012345678901234567890123456789"
        );
        assert_eq!(truncate_to_address("abcd").0.as_str(), "abcd");
        assert_eq!(truncate_to_address("0123456789").0.as_str(), "0123456789");
        assert_eq!(
            truncate_to_address("01234567890123456789012345678901234567◌")
                .0
                .as_str(),
            "01234567890123456789012345678901234567"
        );
    }
}