cruzbit 1.2.0

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

use cuckoofilter::{CuckooFilter, ExportedCuckooFilter};
use ed25519_compact::PublicKey;
use futures::future::Either;
use futures::stream::SplitSink;
use futures::{SinkExt, StreamExt};
use log::{error, info};
use rand::Rng;
use thiserror::Error;
use tokio::net::TcpStream;
use tokio::sync::mpsc::{channel, unbounded_channel, UnboundedSender};
use tokio::task::JoinHandle;
use tokio::time::{interval_at, sleep, timeout, Instant};
use tokio_rustls::server::TlsStream;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::http::StatusCode;
use tokio_tungstenite::tungstenite::{Bytes, Error as WsError, Message as WsMessage};
use tokio_tungstenite::{
    connect_async_tls_with_config, Connector, MaybeTlsStream, WebSocketStream,
};

use crate::block::{Block, BlockError, BlockHeader, BlockID};
use crate::block_queue::BlockQueue;
use crate::block_storage::{BlockStorage, BlockStorageError, BlockStorageNotFoundError};
use crate::block_storage_disk::BlockStorageDisk;
use crate::checkpoints::{CHECKPOINTS_ENABLED, LATEST_CHECKPOINT_HEIGHT};
use crate::constants::{
    MAX_MEMO_LENGTH, MAX_PROTOCOL_MESSAGE_LENGTH, MAX_TRANSACTIONS_TO_INCLUDE_PER_BLOCK,
    MIN_AMOUNT_CRUZBITS, MIN_FEE_CRUZBITS,
};
use crate::error::{impl_debug_error_chain, ChannelError, DataError, ErrChain, JsonError};
use crate::ledger::{BranchType, Ledger, LedgerError, LedgerNotFoundError};
use crate::ledger_disk::LedgerDisk;
use crate::miner::{Miner, MinerError};
use crate::peer_manager::{AddrChanSender, PeerManager, PeerManagerError};
use crate::peer_storage::{PeerStorage, PeerStorageError};
use crate::peer_storage_disk::PeerStorageDisk;
use crate::processor::{ProcessBlockError, Processor, ProcessorError};
use crate::protocol::{
    BalanceMessage, BalancesMessage, BlockHeaderMessage, BlockMessage, FilterBlockMessage,
    FilterResultMessage, FilterTransactionQueueMessage, FindCommonAncestorMessage, GetBlockMessage,
    GetWorkMessage, InvBlockMessage, Message, PeerAddressesMessage, PublicKeyBalance,
    PublicKeyTransactionsMessage, PushTransactionMessage, PushTransactionResultMessage,
    SubmitWorkMessage, SubmitWorkResultMessage, TipHeaderMessage, TransactionMessage,
    TransactionRelayPolicyMessage, WorkMessage,
};
use crate::shutdown::{ShutdownChanReceiver, SpawnedError};
use crate::tls::client_config;
use crate::transaction::{Transaction, TransactionError, TransactionID};
use crate::transaction_queue::TransactionQueue;
use crate::transaction_queue_memory::TransactionQueueMemory;
use crate::utils::{now_as_duration, rand_int31};

pub type EitherWebSocketStream =
    Either<WebSocketStream<MaybeTlsStream<TcpStream>>, WebSocketStream<TlsStream<TcpStream>>>;

type WsSink = SplitSink<EitherWebSocketStream, WsMessage>;

/// A peer client in the network. They all speak WebSocket protocol to each other.
/// Peers could be fully validating and mining nodes or simply wallets.
pub struct Peer {
    /// peer connection, left = outbound, right = inbound
    conn: Option<EitherWebSocketStream>,
    genesis_id: &'static BlockID,
    peer_store: Arc<PeerStorageDisk>,
    block_store: Arc<BlockStorageDisk>,
    ledger: Arc<LedgerDisk>,
    processor: Arc<Processor>,
    tx_queue: Arc<TransactionQueueMemory>,
    outbound: bool,
    /// peer-local download queue
    local_download_queue: BlockQueue,
    /// peer-local inflight queue
    local_inflight_queue: Arc<BlockQueue>,
    /// global inflight queue
    global_inflight_queue: Arc<BlockQueue>,
    ignore_blocks: HashMap<BlockID, bool>,
    continuation_block_id: Option<BlockID>,
    last_peer_addresses_received_time: Option<Instant>,
    filter: Option<CuckooFilter<DefaultHasher>>,
    addr_chan_tx: AddrChanSender,
    work: Option<PeerWork>,
    pub_keys: Vec<PublicKey>,
    memo: Option<String>,
    read_limit: u32,
    addr: SocketAddr,
    shutdown_chan_rx: ShutdownChanReceiver,
    shutdown_fns: Vec<Box<dyn Fn() + Send + Sync>>,
}

pub struct PeerWork {
    work_id: u32,
    work_block: Block,
    median_timestamp: u64,
}

type OutChanSender = UnboundedSender<Message>;

/// Timing constants
/// Time allowed to wait for WebSocket connection
pub const CONNECT_WAIT: Duration = Duration::from_secs(10);

/// Time allowed to write a message to the peer
pub const WRITE_WAIT: Duration = Duration::from_secs(30);

/// Time allowed to read the next pong message from the peer
const PONG_WAIT: Duration = Duration::from_secs(120);

/// Send pings to peer with this period. Must be less than PONG_WAIT
const PING_PERIOD: Duration = Duration::from_secs(PONG_WAIT.as_secs() / 2);

/// How often should we refresh this peer's connectivity status with storage
const PEER_STORAGE_REFRESH_PERIOD: Duration = Duration::from_secs(5 * 60);

/// How often should we request peer addresses from a peer
const GET_PEER_ADDRESSES_PERIOD: Duration = Duration::from_secs(60 * 60);

/// Time allowed between processing new blocks before we consider a blockchain sync stalled
const SYNC_WAIT: Duration = Duration::from_secs(2 * 60);

/// Maximum blocks per inv_block message
const MAX_BLOCKS_PER_INV: usize = 500;

/// Maximum local inflight queue size
const INFLIGHT_QUEUE_MAX: usize = 8;

/// Maximum local download queue size
const DOWNLOAD_QUEUE_MAX: usize = MAX_BLOCKS_PER_INV * 10;

pub const PEER_ADDR_SELF: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0);

impl Peer {
    /// Returns a new instance of a peer.
    pub fn new(
        conn: Option<EitherWebSocketStream>,
        genesis_id: &'static BlockID,
        peer_store: Arc<PeerStorageDisk>,
        block_store: Arc<BlockStorageDisk>,
        ledger: Arc<LedgerDisk>,
        processor: Arc<Processor>,
        tx_queue: Arc<TransactionQueueMemory>,
        block_queue: Arc<BlockQueue>,
        addr_chan_tx: AddrChanSender,
        addr: SocketAddr,
        shutdown_chan_rx: ShutdownChanReceiver,
    ) -> Self {
        let mut peer = Self {
            conn,
            genesis_id,
            peer_store,
            block_store,
            ledger,
            processor,
            tx_queue,
            local_download_queue: BlockQueue::new(),
            local_inflight_queue: Arc::new(BlockQueue::new()),
            global_inflight_queue: block_queue,
            ignore_blocks: HashMap::new(),
            continuation_block_id: None,
            last_peer_addresses_received_time: None,
            outbound: false,
            filter: None,
            addr_chan_tx,
            work: None,
            pub_keys: Vec::new(),
            memo: None,
            read_limit: 0,
            addr,
            shutdown_chan_rx,
            shutdown_fns: Vec::new(),
        };
        peer.update_read_limit();
        peer
    }

    /// Connects outbound to a peer.
    pub async fn connect(
        &mut self,
        nonce: u32,
        my_addr: Option<SocketAddr>,
    ) -> Result<(), PeerConnectionError> {
        let url = format!("wss://{}/{}", self.addr, &self.genesis_id);
        info!("Connecting to {url}");

        let mut request = url.into_client_request()?;
        request
            .headers_mut()
            .append("Cruzbit-Peer-Nonce", nonce.to_string().parse()?);

        if let Some(my_addr) = my_addr {
            request
                .headers_mut()
                .append("Cruzbit-Peer-Address", my_addr.to_string().parse()?);
        }

        self.peer_store.on_connect_attempt(self.addr)?;

        let tls_verify = false;
        let client_config = client_config(tls_verify);
        let (conn, _response) = match timeout(
            CONNECT_WAIT,
            connect_async_tls_with_config(
                request,
                None,
                true,
                Some(Connector::Rustls(Arc::new(client_config))),
            ),
        )
        .await
        {
            Err(err) => {
                return Err(PeerConnectionError::Timeout(self.addr, err));
            }
            Ok(Ok(v)) => v,
            Ok(Err(err)) => {
                if let WsError::Http(response) = &err {
                    if response.status() == StatusCode::TOO_MANY_REQUESTS {
                        // the peer is already connected to us inbound.
                        // mark it successful so we try it again in the future.
                        self.peer_store.on_connect_success(self.addr)?;
                        self.peer_store.on_disconnect(self.addr)?;
                    } else {
                        self.peer_store.on_connect_failure(self.addr)?;
                    }
                }

                return Err(PeerConnectionError::Connect(self.addr, err));
            }
        };

        // left is outbound, right is inbound
        self.conn = Some(EitherWebSocketStream::Left(conn));
        self.outbound = true;
        self.peer_store
            .on_connect_success(self.addr)
            .map_err(PeerConnectionError::PeerStorage)
    }

    /// Spawns the Peer's main loop.
    pub fn spawn(self) -> JoinHandle<Result<(), SpawnedError>> {
        tokio::spawn(async { self.run().await.map_err(Into::into) })
    }

    /// Runs the Peer's main loop.
    /// It manages reading and writing to the peer's WebSocket and facilitating the protocol.
    pub async fn run(mut self) -> Result<(), PeerError> {
        let conn = self.conn.take().expect("peer should be connected");
        let (mut ws_sender, mut ws_receiver) = conn.split();

        // on shutdown remove any inflight blocks this peer is no longer going to download
        {
            let local_inflight_queue = Arc::clone(&self.local_inflight_queue);
            let global_inflight_queue = Arc::clone(&self.global_inflight_queue);
            self.on_shutdown(Box::new(move || {
                while let Some(block_in_flight) = local_inflight_queue.peek() {
                    local_inflight_queue.remove(&block_in_flight, &PEER_ADDR_SELF);
                    global_inflight_queue.remove(&block_in_flight, &self.addr);
                }
            }));
        }

        // channel to send outgoing messages
        let (out_chan_tx, mut out_chan_rx) = unbounded_channel();

        // send a find common ancestor request and request peer addresses shortly after connecting
        let (on_connect_chan_tx, mut on_connect_chan_rx) = channel(1);
        tokio::spawn(async move {
            sleep(Duration::from_secs(5)).await;
            if let Err(_err) = on_connect_chan_tx.send(true).await {
                error!("failed to send on-connect, peer must have shut down");
            }
        });

        // written to by the reader to update the current work block for the peer
        let mut get_work_chan = channel::<GetWorkMessage>(1);
        let mut submit_work_chan = channel::<SubmitWorkMessage>(1);

        // register to hear about tip block changes
        let (tip_change_chan_tx, mut tip_change_chan_rx) = unbounded_channel();
        self.processor
            .register_for_tip_change(tip_change_chan_tx.clone());

        // register to hear about new transactions
        let (new_tx_chan_tx, mut new_tx_chan_rx) =
            channel(MAX_TRANSACTIONS_TO_INCLUDE_PER_BLOCK as usize);
        self.processor
            .register_for_new_transactions(new_tx_chan_tx.clone());

        // unregister from the processor on shutdown
        {
            let processor = Arc::clone(&self.processor);
            self.on_shutdown(Box::new(move || {
                processor.unregister_for_tip_change(tip_change_chan_tx.clone());
                processor.unregister_for_new_transactions(new_tx_chan_tx.clone());
            }));
        }

        // send the peer pings
        let mut ticker_ping = interval_at(Instant::now() + PING_PERIOD, PING_PERIOD);

        // update the peer store with the peer's connectivity
        let mut ticker_peer_store_refresh = interval_at(
            Instant::now() + PEER_STORAGE_REFRESH_PERIOD,
            PEER_STORAGE_REFRESH_PERIOD,
        );

        // request new peer addresses
        let mut ticker_get_peer_addresses = interval_at(
            Instant::now() + GET_PEER_ADDRESSES_PERIOD,
            GET_PEER_ADDRESSES_PERIOD,
        );

        // check to see if we need to update work for miners
        let interval = Duration::from_secs(30);
        let mut ticker_update_work_check = interval_at(Instant::now() + interval, interval);

        // update the peer store on disconnection
        if self.outbound {
            let peer_store = Arc::clone(&self.peer_store);
            self.on_shutdown(Box::new(move || {
                if let Err(err) = peer_store
                    .on_disconnect(self.addr)
                    .map_err(PeerError::PeerStorage)
                {
                    error!("{err:?}");
                }
            }));
        }

        // are we syncing?
        let mut last_new_block_time = Instant::now();
        let (mut ibd, _height) =
            PeerManager::is_initial_block_download(&self.ledger, &self.block_store)?;

        loop {
            tokio::select! {
                msg = timeout(PONG_WAIT, ws_receiver.next()) => {
                    let message = match msg {
                        Err(err) => {
                            break Err(PeerConnectionError::Timeout(self.addr, err).into())
                        }
                        Ok(Some(Ok(v))) => v,
                        Ok(Some(Err(err))) => {
                            break Err(PeerConnectionError::Websocket(err).into())
                        }
                        Ok(None) => {
                            break Err(PeerConnectionError::Dropped(self.addr).into())
                        },
                    };

                    // new message from peer
                    match message {
                        WsMessage::Text(ref json) => {
                            // sanitize inputs
                            if let Err(err) = from_utf8(json.as_bytes()).map_err(|err| PeerError::MessageNotUtf8(self.addr, DataError::String(err))) {
                                break Err(err)
                            }

                            let message = match serde_json::from_str::<Message>(json).map_err(|err| PeerError::MessageInvalid(self.addr, JsonError::Deserialize(err))) {
                                Ok(v) => v,
                                Err(err) => {
                                    break Err(err)
                                }
                            };

                            // hangup if the peer is sending oversized messages (other than blocks)
                            if !matches!(message, Message::Block(_))
                                && json.len() > MAX_PROTOCOL_MESSAGE_LENGTH
                            {
                                break Err(PeerError::MessageLengthExceeded(
                                    json.len(),
                                    message.to_string(),
                                    self.addr
                                ))
                            }

                            match message {
                                Message::Block(Some(b)) => {
                                    if let Some(block) = b.block {
                                        match self.on_block(block, ibd, &out_chan_tx).await {
                                            Ok(accepted) => {
                                                if accepted {
                                                    last_new_block_time = Instant::now();
                                                }
                                            }
                                            Err(err) => {
                                                error!("{:?}, from: {}", err, self.addr);
                                                continue
                                            }
                                        }
                                    } else {
                                        break Err(PeerError::EmptyBlockReceived(self.addr))
                                    }
                                }

                                Message::FilterAdd(fa) => {
                                    if let Err(err) = self.on_filter_add(fa.public_keys, &out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::FilterLoad(fl) => {
                                    if let Err(err) = self.on_filter_load(fl.r#type, fl.filter, &out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::FindCommonAncestor(fca) => {
                                    let length = fca.block_ids.len();
                                    for (i, id) in
                                        fca.block_ids.into_iter().enumerate()
                                    {
                                        match self
                                            .on_find_common_ancestor(&id, i, length, &out_chan_tx).await {
                                                Ok(ok) => {
                                                    if ok {
                                                        break
                                                    }
                                                },
                                                Err(err) => {
                                                    error!("{:?}, from: {}", err, self.addr);
                                                    // don't need to process more
                                                    break
                                                }
                                            }
                                    }
                                }

                                Message::GetBalance(gb) => {
                                    if let Err(err) = self.on_get_balance(gb.public_key, &out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue;

                                    }
                                }

                                Message::GetBalances(gb) => {
                                    if let Err(err) = self.on_get_balances(gb.public_keys, &out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::GetBlock(gb) => {
                                    if let Err(err) = self.on_get_block(gb.block_id, &out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::GetBlockByHeight(gbbh) => {
                                    if let Err(err) = self.on_get_block_by_height(gbbh.height, &out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::GetBlockHeader(gbh) => {
                                    if let Err(err) = self.on_get_block_header(gbh.block_id, &out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::GetBlockHeaderByHeight(gbhbh) => {
                                    if let Err(err) = self
                                        .on_get_block_header_by_height(gbhbh.height, &out_chan_tx).await {
                                            error!("{:?}, from: {}", err, self.addr);
                                            continue
                                        }
                                }

                                Message::GetFilterTransactionQueue => {
                                    if let Err(err) = self.on_get_filter_transaction_queue(&out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::GetPeerAddresses => {
                                    if let Err(err) = self.on_get_peer_addresses(&out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::GetPublicKeyTransactions(gpkt) => {
                                    if let Err(err) = self
                                        .on_get_public_key_transactions(
                                            gpkt.public_key,
                                            gpkt.start_height,
                                            gpkt.end_height,
                                            gpkt.start_index,
                                            gpkt.limit,
                                            &out_chan_tx
                                        ).await {
                                            error!("{:?}, from: {}", err, self.addr);
                                            continue
                                        }
                                }

                                Message::GetTipHeader => {
                                    if let Err(err) = self.on_get_tip_header(&out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::GetTransaction(gt) => {
                                    if let Err(err) = self.on_get_transaction(gt.transaction_id, &out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::GetTransactionResult(ptr) => {
                                    if let Some(err) = ptr.error {
                                        error!("{:?}, from: {}", err, self.addr);}
                                }

                                Message::GetTransactionRelayPolicy => {
                                    out_chan_tx.send(Message::TransactionRelayPolicy(TransactionRelayPolicyMessage {
                                        min_fee: MIN_FEE_CRUZBITS,
                                        min_amount: MIN_AMOUNT_CRUZBITS,
                                    }))?;
                                }

                                Message::GetWork(gw) => {
                                    info!("Received get_work message, from: {}", self.addr);
                                    get_work_chan.0.send(gw).await?;
                                }

                                Message::InvBlock(inv) => {
                                    let block_ids_len = inv.block_ids.len();
                                    for (i, id) in
                                        inv.block_ids.into_iter().enumerate()
                                    {
                                        if let Err(err) = self.on_inv_block(id, i, block_ids_len, &out_chan_tx).await {
                                            error!("{:?}, from: {}", err, self.addr);
                                            continue
                                        }
                                    }
                                }

                                Message::PeerAddresses(pa) => {
                                    if let Err(err) = self.on_peer_addresses(pa.addresses).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::PushTransaction(pt) => {
                                    if let Err(err) = self.on_push_transaction(pt.transaction, &out_chan_tx).await {
                                        error!("{:?}, from: {}", err, self.addr);
                                        continue
                                    }
                                }

                                Message::PushTransactionResult(ptr) => {
                                    if let Some(err) = ptr.error {
                                        error!("{}, from: {}", err, self.addr);
                                    }
                                }

                                Message::SubmitWork(sw) => {
                                    info!("Received submit_work message, from: {}", self.addr);
                                    submit_work_chan.0.send(sw).await?;
                                }

                                _ => {
                                    error!("Unknown message: {}, from: {}", message, self.addr);}
                            }
                        }

                        WsMessage::Close(_) => {
                            info!(
                                "Received close message from: {}",
                                self.addr
                            );
                            break Ok(())
                        }

                        WsMessage::Pong(_) => {
                            // handle pongs
                            if ibd {
                                // handle stalled blockchain syncs
                                (ibd, _) = PeerManager::is_initial_block_download(&self.ledger, &self.block_store)?;
                                let elapsed = last_new_block_time.elapsed();
                                if ibd && elapsed > SYNC_WAIT {
                                    break Err(PeerError::SyncStalled(self.addr))
                                }
                            } else {
                                // try processing the queue in case we've been blocked by another client
                                // and their attempt has now expired
                                self.process_download_queue(&out_chan_tx).await?;
                            }
                        }

                        // ignore other message types
                        _ => {},
                    };
                }

                msg = out_chan_rx.recv() => {
                    match msg {
                        Some(message) => {
                            let json = serde_json::to_string(&message).map_err(JsonError::Serialize)?;
                            self.send_with_timeout(&mut ws_sender, WsMessage::text(json)).await?;
                        },
                        None => {
                            // close the connection if the tx is dropped
                            self.send_with_timeout(&mut ws_sender, WsMessage::Close(None)).await?;
                        }
                    }
                }

                Some(tip) = tip_change_chan_rx.recv() => {
                    // update read limit if necessary
                    self.update_read_limit();

                    if tip.connect && !tip.more {
                        // only build off newly connected tip blocks.
                        // create and send out new work if necessary
                        self.create_new_work_block(&tip.block_id, &tip.block.header, &out_chan_tx).await?;
                    }

                    if tip.source == self.addr {
                        continue
                    }

                    if tip.connect {
                        // new tip announced, notify the peer
                        let inv = Message::InvBlock(InvBlockMessage {
                            block_ids: vec![tip.block_id]
                        });
                        // send it
                        let json = serde_json::to_string(&inv).map_err(JsonError::Serialize)?;
                        self.send_with_timeout(&mut ws_sender, WsMessage::text(json)).await?;
                    }

                    // potentially create a filter_block
                    let fb = match self.create_filter_block(tip.block_id, tip.block) {
                        Ok(Some(v)) => v,
                        Ok(None) => continue,
                        Err(err) => {
                            error!("{:?}, to: {}", err, self.addr);
                            continue
                        }
                    };

                    // send it
                    let transactions_len = fb.transactions.len();

                    let (message, r#type) = if !tip.connect {
                        (Message::FilterBlockUndo(fb), "filter_block_undo")
                    } else {
                        (Message::FilterBlock(fb), "filter_block")
                    };

                    info!("Sending {} with {} transaction(s), to: {}", r#type, transactions_len, self.addr);
                    let json = serde_json::to_string(&message).map_err(JsonError::Serialize)?;
                    self.send_with_timeout(&mut ws_sender, WsMessage::text(json)).await?;
                }

                Some(new_tx) = new_tx_chan_rx.recv() => {
                    if new_tx.source == self.addr {
                        // this is who sent it to us
                        continue
                    }

                    if !self.filter_lookup(&new_tx.transaction) {
                        continue
                    }

                    // newly verified transaction announced, relay to peer
                    let push_tx = Message::PushTransaction(PushTransactionMessage {
                        transaction: new_tx.transaction,
                    });
                    let json = serde_json::to_string(&push_tx).map_err(JsonError::Serialize)?;
                    self.send_with_timeout(&mut ws_sender, WsMessage::text(json)).await?;
                }

                Some(_) = on_connect_chan_rx.recv() => {
                    // send a new peer a request to find a common ancestor
                    self.send_find_common_ancestor(None, Some(&mut ws_sender), &out_chan_tx).await?;

                    // send a get_peer_addresses to request peers
                    info!("Sending get_peer_addresses to: {}", self.addr);
                    let message = Message::GetPeerAddresses;
                    let json = serde_json::to_string(&message).map_err(JsonError::Serialize)?;
                    self.send_with_timeout(&mut ws_sender, WsMessage::text(json)).await?;
                }

                Some(gw) = get_work_chan.1.recv() => {
                    if let Err(err) = self.on_get_work(gw, &out_chan_tx).await {
                        error!("{:?}, from: {}", err, self.addr);
                    }
                }

                Some(sw) = submit_work_chan.1.recv() => {
                    if let Err(err) = self.on_submit_work(sw, &out_chan_tx).await {
                        error!("{:?}, from: {}", err, self.addr);
                    }
                }

                _ = ticker_ping.tick() => {
                    self.send_with_timeout(&mut ws_sender, WsMessage::Ping(Bytes::new())).await?;
                }

                _ = ticker_peer_store_refresh.tick(), if self.outbound => {
                    // periodically refresh our connection time
                    if let Err(err) = self.peer_store.on_connect_success(self.addr).map_err(PeerError::PeerStorage) {
                        error!("{:?}, from: {}", err, self.addr);
                    }
                }

                _ = ticker_get_peer_addresses.tick() => {
                    // periodically send a get_peer_addresses
                    info!("Sending get_peer_addresses to: {}", self.addr);
                    let message = Message::GetPeerAddresses;
                    let json = serde_json::to_string(&message).map_err(JsonError::Serialize)?;
                    self.send_with_timeout(&mut ws_sender, WsMessage::text(json)).await?;
                }

                _ = ticker_update_work_check.tick(), if self.work.is_some() => {
                    let work = self.work.as_ref().expect("work should exist");
                    let tx_count = work.work_block.transactions.len();
                    if tx_count == MAX_TRANSACTIONS_TO_INCLUDE_PER_BLOCK as usize {
                        // already at capacity
                        continue
                    }
                    if tx_count - 1 != self.tx_queue.len() {
                        let Some((tip_id, tip_header, _tip_when)) = Processor::get_chain_tip_header(&self.ledger, &self.block_store)? else {
                            break Err(LedgerNotFoundError::ChainTip.into())
                        };
                        self.create_new_work_block(&tip_id, &tip_header, &out_chan_tx).await?;
                    }
                }

                _ = &mut self.shutdown_chan_rx => {
                    ws_sender.close().await.map_err(PeerConnectionError::Websocket)?;
                    break Ok(())
                }
            }
        }
    }

    /// Handle a message from a peer indicating block inventory available for download
    async fn on_inv_block(
        &mut self,
        id: BlockID,
        index: usize,
        length: usize,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!("Received inv_block: {}, from: {}", id, self.addr);

        if length > MAX_BLOCKS_PER_INV {
            return Err(PeerError::InvBlockMaxBlocks(length, MAX_BLOCKS_PER_INV));
        }

        // is it on the ignore list?
        if self.ignore_blocks.contains_key(&id) {
            info!("Ignoring block {}, from: {}", id, self.addr);
            return Ok(());
        }

        // do we have it queued or inflight already?
        if self.local_download_queue.exists(&id) || self.local_inflight_queue.exists(&id) {
            info!(
                "Block {} is already queued or inflight for download, from: {}",
                id, self.addr
            );
            return Ok(());
        }

        // have we processed it?
        let branch_type = self.ledger.get_branch_type(&id)?;
        if branch_type != BranchType::Unknown {
            info!("Already processed block {id}");
            if length > 1 && index + 1 == length {
                // we might be on a deep side chain. this will get us the next 500 blocks
                return self
                    .send_find_common_ancestor(Some(id), None, out_chan_tx)
                    .await;
            }
            return Ok(());
        }

        if self.local_download_queue.len() >= DOWNLOAD_QUEUE_MAX {
            info!(
                "Too many blocks in the download queue {}, max: {}, for: {}",
                self.local_download_queue.len(),
                DOWNLOAD_QUEUE_MAX,
                self.addr
            );
            // don't return an error just stop adding them to the queue
            return Ok(());
        }

        // add block to this peer's download queue
        self.local_download_queue.add(&id, &PEER_ADDR_SELF);

        // process the download queue
        self.process_download_queue(out_chan_tx).await
    }

    /// Handle a request for a block from a peer
    async fn on_get_block(
        &mut self,
        id: BlockID,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!("Received get_block: {}, from: {}", id, self.addr);
        self.get_block(id, out_chan_tx).await
    }

    /// Handle a request for a block by height from a peer
    async fn on_get_block_by_height(
        &mut self,
        height: u64,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!(
            "Received get_block_by_height: {}, from: {}",
            height, self.addr
        );
        let id = match self.ledger.get_block_id_for_height(height) {
            Ok(Some(v)) => v,
            Ok(None) => {
                // not found
                out_chan_tx.send(Message::Block(None))?;
                return Err(LedgerNotFoundError::BlockIDForHeight(height).into());
            }
            Err(err) => {
                // not found
                out_chan_tx.send(Message::Block(None))?;
                return Err(err.into());
            }
        };

        self.get_block(id, out_chan_tx).await
    }

    async fn get_block(
        &mut self,
        id: BlockID,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        // fetch the block
        let block_json = match self.block_store.get_block_bytes(&id) {
            Ok(Some(v)) => v,
            Ok(None) => {
                // not found
                out_chan_tx.send(Message::Block(Some(Box::new(BlockMessage {
                    block_id: id,
                    block: None,
                }))))?;

                return Err(BlockStorageNotFoundError::BlockBytes(id).into());
            }
            Err(err) => {
                // not found
                out_chan_tx.send(Message::Block(Some(Box::new(BlockMessage {
                    block_id: id,
                    block: None,
                }))))?;

                return Err(err.into());
            }
        };

        // send out the raw bytes
        let mut body = Vec::new();
        body.extend_from_slice(br#"{"block_id":""#);
        body.extend_from_slice(id.as_hex().as_bytes());
        body.extend_from_slice(br#"","block":"#);
        body.extend_from_slice(&block_json);
        body.extend_from_slice(br#"}"#);

        let block_message =
            serde_json::from_slice::<BlockMessage>(&body).map_err(JsonError::Deserialize)?;
        out_chan_tx.send(Message::Block(Some(Box::new(block_message))))?;

        // was this the last block in the inv we sent in response to a find common ancestor request?
        if let Some(ref continuation_block_id) = self.continuation_block_id {
            if id == *continuation_block_id {
                info!(
                    "Received get_block for continuation block {}, from: {}",
                    id, self.addr
                );

                self.continuation_block_id = None;

                // send an inv for our tip block to prompt the peer to
                // send another find common ancestor request to complete its download of the chain.
                let chain_tip = match self.ledger.get_chain_tip().map_err(PeerError::Ledger) {
                    Ok(v) => v,
                    Err(err) => {
                        error!("{err:?}");
                        return Ok(());
                    }
                };

                if let Some((tip_id, _height)) = chain_tip {
                    out_chan_tx.send(Message::InvBlock(InvBlockMessage {
                        block_ids: vec![tip_id],
                    }))?;
                }
            }
        }

        Ok(())
    }

    /// Handle receiving a block from a peer. Returns true if the block was newly processed and accepted.
    async fn on_block(
        &mut self,
        block: Block,
        ibd: bool,
        out_chan_tx: &OutChanSender,
    ) -> Result<bool, PeerError> {
        // the message has the ID in it but we can't trust that.
        // it's provided as convenience for trusted peering relationships only
        let id = block.id()?;

        info!("Received block: {}, from: {}", id, self.addr);

        match self.local_inflight_queue.peek() {
            Some(peek) => {
                if peek != id {
                    // disconnect misbehaving peer
                    return Err(PeerError::ReceivedUnrequestedBlock);
                }
            }
            None => {
                // disconnect misbehaving peer
                return Err(PeerError::ReceivedUnrequestedBlock);
            }
        };

        // don't process low difficulty blocks
        if !ibd && CHECKPOINTS_ENABLED && block.header.height < LATEST_CHECKPOINT_HEIGHT {
            // don't disconnect them. they may need us to find out about the real chain
            self.local_inflight_queue.remove(&id, &PEER_ADDR_SELF);
            self.global_inflight_queue.remove(&id, &self.addr);

            // ignore future inv_blocks for this block
            self.ignore_blocks.insert(id, true);
            if self.ignore_blocks.len() > MAX_BLOCKS_PER_INV {
                // they're intentionally sending us bad blocks
                return Err(PeerError::MaxIgnoreListSizeExceeded);
            }

            return Err(PeerError::BlockAtHeightLessThanCheckpoint(
                id,
                block.header.height,
                LATEST_CHECKPOINT_HEIGHT,
            ));
        }

        let mut accepted = false;

        // is it an orphan?
        match self.block_store.get_block_header(&block.header.previous) {
            Ok(Some((_header, _height))) => {
                // process the block
                if let Err(err) = self
                    .processor
                    .process_candidate_block(id, block, self.addr)
                    .await
                {
                    // TODO: disconnect from peer here
                    // disconnect a peer that sends us a bad block
                    return Err(err.into());
                }
                // newly accepted block
                accepted = true;

                // remove it from the inflight queues only after we process it
                self.local_inflight_queue.remove(&id, &PEER_ADDR_SELF);
                self.global_inflight_queue.remove(&id, &self.addr);
            }
            Ok(None) => {
                self.local_inflight_queue.remove(&id, &PEER_ADDR_SELF);
                self.global_inflight_queue.remove(&id, &self.addr);

                info!(
                    "Block {} is an orphan, sending find_common_ancestor to: {}",
                    id, self.addr
                );

                // send a find common ancestor request
                self.send_find_common_ancestor(None, None, out_chan_tx)
                    .await?;
            }
            Err(err) => {
                self.local_inflight_queue.remove(&id, &PEER_ADDR_SELF);
                self.global_inflight_queue.remove(&id, &self.addr);
                return Err(err.into());
            }
        };

        // see if there are any more blocks to download right now
        self.process_download_queue(out_chan_tx).await?;

        Ok(accepted)
    }

    /// Try requesting blocks that are in the download queue
    async fn process_download_queue(
        &mut self,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        // fill up as much of the inflight queue as possible
        let mut queued = 0;

        while self.local_inflight_queue.len() < INFLIGHT_QUEUE_MAX {
            // next block to download
            let Some(block_to_download) = self.local_download_queue.peek() else {
                // no more blocks in the queue
                break;
            };

            // double-check if it's been processed since we last checked
            let branch_type = self.ledger.get_branch_type(&block_to_download)?;
            if branch_type != BranchType::Unknown {
                // it's been processed. remove it and check the next one
                info!(
                    "Block {} has been processed, removing from download queue for: {}",
                    block_to_download, self.addr
                );
                self.local_download_queue
                    .remove(&block_to_download, &PEER_ADDR_SELF);
                continue;
            }

            // add block to the global inflight queue with this peer as the owner
            {
                if !self
                    .global_inflight_queue
                    .add(&block_to_download, &self.addr)
                {
                    // another peer is downloading it right now.
                    // wait to see if they succeed before trying to download any others
                    info!(
                        "Block {block_to_download} is being downloaded already from another peer"
                    );
                    break;
                }
            }

            // pop it off the local download queue
            self.local_download_queue
                .remove(&block_to_download, &PEER_ADDR_SELF);

            // mark it inflight locally
            self.local_inflight_queue
                .add(&block_to_download, &PEER_ADDR_SELF);
            queued += 1;

            // request it
            info!(
                "Sending get_block for {}, to: {}",
                block_to_download, self.addr
            );
            out_chan_tx.send(Message::GetBlock(GetBlockMessage {
                block_id: block_to_download,
            }))?;
        }

        if queued > 0 {
            info!(
                "Requested {} block(s) for download, from: {}",
                queued, self.addr
            );
            info!(
                "Queue size: {}, peer inflight: {}, global inflight: {}, for: {}",
                self.local_download_queue.len(),
                self.local_inflight_queue.len(),
                self.global_inflight_queue.len(),
                self.addr
            );
        }

        Ok(())
    }

    /// Send a message to look for a common ancestor with a peer
    /// Might be called from reader or writer context. sender means we're in the writer context
    async fn send_find_common_ancestor(
        &self,
        mut start_id: Option<BlockID>,
        ws_sender: Option<&mut WsSink>,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!("Sending find_common_ancestor to: {}", self.addr);

        let mut height = match start_id {
            Some(id) => {
                let Some((header, _when)) = self.block_store.get_block_header(&id)? else {
                    info!("No header for block {id}");
                    return Ok(());
                };
                header.height
            }
            None => {
                let Some((id_tip, height_tip)) = self.ledger.get_chain_tip()? else {
                    return Ok(());
                };
                start_id = Some(id_tip);
                height_tip
            }
        };

        let mut block_id = start_id;
        let mut ids = Vec::new();
        let mut step = 1;
        while let Some(id) = block_id {
            if id == *self.genesis_id {
                break;
            }
            ids.push(id);
            if step >= height {
                break;
            }
            let depth = height - step;
            block_id = match self
                .ledger
                .get_block_id_for_height(depth)
                .map_err(PeerError::Ledger)
            {
                Ok(v) => v,
                Err(err) => {
                    error!("{err:?}");
                    return Ok(());
                }
            };
            if ids.len() > 10 {
                step *= 2;
            }
            height = depth;
        }
        ids.push(*self.genesis_id);
        let message = Message::FindCommonAncestor(FindCommonAncestorMessage { block_ids: ids });

        // send immediately if the sender is passed in
        if let Some(ws_sender) = ws_sender {
            let json = serde_json::to_string(&message).map_err(JsonError::Serialize)?;
            self.send_with_timeout(ws_sender, WsMessage::text(json))
                .await
                .map_err(PeerError::PeerConnection)?;
            return Ok(());
        }
        out_chan_tx.send(message)?;

        Ok(())
    }

    /// Handle a find common ancestor message from a peer
    async fn on_find_common_ancestor(
        &mut self,
        id: &BlockID,
        index: usize,
        length: usize,
        out_chan_tx: &OutChanSender,
    ) -> Result<bool, PeerError> {
        info!(
            "Received find_common_ancestor: {}, index: {}, length: {}, from: {}",
            id, index, length, self.addr
        );

        let Some((header, _when)) = self.block_store.get_block_header(id)? else {
            return Ok(false);
        };

        // have we processed it?
        let branch_type = self.ledger.get_branch_type(id)?;
        if branch_type != BranchType::Main {
            // not on the main branch
            return Ok(false);
        }

        info!(
            "Common ancestor found: {}, height: {}, with: {}",
            id, header.height, self.addr
        );

        let mut ids = Vec::new();
        let mut height = header.height + 1;

        while ids.len() < MAX_BLOCKS_PER_INV {
            let Some(next_id) = self.ledger.get_block_id_for_height(height)? else {
                break;
            };
            info!(
                "Queueing inv for block {}, height: {}, to: {}",
                next_id, height, self.addr
            );
            ids.push(next_id);
            height += 1;
        }

        if !ids.is_empty() {
            // save the last ID so after the peer requests it we can trigger it to
            // send another find common ancestor request to finish downloading the rest of the chain
            let continuation_block_id = ids[ids.len() - 1];
            info!(
                "Sending inv_block with {} IDs, continuation block: {}, to: {}",
                ids.len(),
                continuation_block_id,
                self.addr
            );
            self.continuation_block_id = Some(continuation_block_id);

            out_chan_tx.send(Message::InvBlock(InvBlockMessage { block_ids: ids }))?;
        }

        Ok(true)
    }

    /// Handle a request for a block header from a peer
    async fn on_get_block_header(
        &self,
        id: BlockID,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!("Received get_block_header: {}, from: {}", id, self.addr);

        self.get_block_header(id, out_chan_tx).await
    }

    /// Handle a request for a block header by ID from a peer
    async fn on_get_block_header_by_height(
        &self,
        height: u64,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!(
            "Received get_block_header_by_height: {}, from: {}",
            height, self.addr
        );
        let id = match self.ledger.get_block_id_for_height(height) {
            Ok(Some(v)) => v,
            Ok(None) => {
                // not found
                out_chan_tx.send(Message::BlockHeader(None))?;
                return Err(LedgerNotFoundError::BlockIDForHeight(height).into());
            }
            Err(err) => {
                // not found
                out_chan_tx.send(Message::BlockHeader(None))?;
                return Err(err.into());
            }
        };

        self.get_block_header(id, out_chan_tx).await
    }

    async fn get_block_header(
        &self,
        block_id: BlockID,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        let (header, _when) = match self.block_store.get_block_header(&block_id) {
            Ok(Some(v)) => v,
            Ok(None) => {
                // not found
                out_chan_tx.send(Message::BlockHeader(Some(BlockHeaderMessage {
                    block_id,
                    block_header: None,
                })))?;

                return Err(BlockStorageNotFoundError::BlockHeader(block_id).into());
            }
            Err(err) => {
                // not found
                out_chan_tx.send(Message::BlockHeader(Some(BlockHeaderMessage {
                    block_id,
                    block_header: None,
                })))?;

                return Err(err.into());
            }
        };
        out_chan_tx.send(Message::BlockHeader(Some(BlockHeaderMessage {
            block_id,
            block_header: Some(header),
        })))?;

        Ok(())
    }

    /// Handle a request for a public key's balancep
    async fn on_get_balance(
        &self,
        pub_key: PublicKey,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!("Received get_balance from {}", self.addr);

        let (balances, tip_id, tip_height) =
            match self.ledger.get_public_key_balances(vec![pub_key]) {
                Ok(v) => v,
                Err(err) => {
                    out_chan_tx.send(Message::Balance(BalanceMessage {
                        block_id: None,
                        height: None,
                        public_key: Some(pub_key),
                        balance: None,
                        error: Some(err.to_string()),
                    }))?;

                    return Err(err.into());
                }
            };

        let mut balance = 0;
        for (_pub_key, bal) in balances {
            balance = bal;
        }

        out_chan_tx.send(Message::Balance(BalanceMessage {
            block_id: Some(tip_id),
            height: Some(tip_height),
            public_key: Some(pub_key),
            balance: Some(balance),
            error: None,
        }))?;

        Ok(())
    }

    /// Handle a request for a set of public key balances.
    async fn on_get_balances(
        &self,
        pub_keys: Vec<PublicKey>,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!(
            "Received get_balances (count: {}) from: {}",
            pub_keys.len(),
            self.addr
        );

        let max_public_keys = 64;
        if pub_keys.len() > max_public_keys {
            let err = PeerBalancesError::PublicKeysExceeded(max_public_keys);
            out_chan_tx.send(Message::Balances(BalancesMessage {
                error: Some(err.to_string()),
                block_id: None,
                height: None,
                balances: None,
            }))?;

            return Err(err.into());
        }

        let (balances, tip_id, tip_height) = match self.ledger.get_public_key_balances(pub_keys) {
            Ok(v) => v,
            Err(err) => {
                out_chan_tx.send(Message::Balances(BalancesMessage {
                    block_id: None,
                    height: None,
                    balances: None,
                    error: Some(err.to_string()),
                }))?;
                return Err(err.into());
            }
        };

        let mut pub_key_balances = Vec::with_capacity(balances.len());
        for (public_key, balance) in balances {
            pub_key_balances.push(PublicKeyBalance {
                public_key,
                balance,
            });
        }
        out_chan_tx.send(Message::Balances(BalancesMessage {
            block_id: Some(tip_id),
            height: Some(tip_height),
            balances: Some(pub_key_balances),
            error: None,
        }))?;

        Ok(())
    }

    /// Handle a request for a public key's transactions over a given height range
    async fn on_get_public_key_transactions(
        &self,
        pub_key: PublicKey,
        start_height: u64,
        end_height: u64,
        start_index: u32,
        mut limit: usize,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!("Received get_public_key_transactions from {}", self.addr);
        // enforce our limit
        if limit > 32 || limit == 0 {
            limit = 32;
        }

        // get the indices for all transactions for the given public key
        // over the given range of block heights
        let (block_ids, indices, stop_height, stop_index) =
            match self.ledger.get_public_key_transaction_indices_range(
                pub_key,
                start_height,
                end_height,
                start_index,
                limit,
            ) {
                Ok(v) => v,
                Err(err) => {
                    out_chan_tx.send(Message::PublicKeyTransactions(
                        PublicKeyTransactionsMessage {
                            public_key: None,
                            start_height: None,
                            stop_height: None,
                            stop_index: None,
                            filter_blocks: None,
                            error: Some(err.to_string()),
                        },
                    ))?;

                    return Err(err.into());
                }
            };

        // build filter blocks from the indices
        let mut fbs = Vec::new();

        for (i, block_id) in block_ids.into_iter().enumerate() {
            // fetch transaction and header
            let (tx, block_header) = match self.block_store.get_transaction(&block_id, indices[i]) {
                Ok((Some(tx), header)) => (tx, header),
                Ok((None, _header)) => {
                    let err = PeerError::PublicKeyTransactionNotFound(block_id, indices[i]);
                    error!("{err:?}");
                    continue;
                }
                Err(err) => {
                    // odd case. just log it and continue
                    let err =
                        PeerError::PublicKeyTransactionBlockStorage(block_id, indices[i], err);
                    error!("{err:?}");
                    continue;
                }
            };

            // figure out where to put it
            if fbs.is_empty() {
                // new block
                let fb = FilterBlockMessage {
                    block_id,
                    header: block_header,
                    transactions: vec![tx],
                };
                fbs.push(fb)
            } else if fbs[fbs.len() - 1].block_id != block_id {
                // new block
                let fb = FilterBlockMessage {
                    block_id,
                    header: block_header,
                    transactions: vec![tx],
                };
                fbs.push(fb);
            } else {
                // transaction is from the same block
                let last_index = fbs.len() - 1;
                let fb = &mut fbs[last_index];
                fb.transactions.push(tx);
            };
        }

        out_chan_tx.send(Message::PublicKeyTransactions(
            PublicKeyTransactionsMessage {
                public_key: Some(pub_key),
                start_height: Some(start_height),
                stop_height: Some(stop_height),
                stop_index: Some(stop_index),
                filter_blocks: Some(fbs),
                error: None,
            },
        ))?;

        Ok(())
    }

    /// Handle a request for a transaction
    async fn on_get_transaction(
        &self,
        tx_id: TransactionID,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!(
            "Received get_transaction for {}, from: {}",
            tx_id, self.addr
        );

        let (block_id, index) = match self.ledger.get_transaction_index(&tx_id) {
            Ok(Some(v)) => v,
            Ok(None) => {
                // not found
                out_chan_tx.send(Message::Transaction(TransactionMessage {
                    block_id: None,
                    height: None,
                    transaction_id: tx_id,
                    transaction: None,
                }))?;

                return Err(LedgerNotFoundError::TransactionAtIndex(tx_id).into());
            }
            Err(err) => {
                // not found
                out_chan_tx.send(Message::Transaction(TransactionMessage {
                    block_id: None,
                    height: None,
                    transaction_id: tx_id,
                    transaction: None,
                }))?;

                return Err(err.into());
            }
        };

        let (tx, block_header) = match self.block_store.get_transaction(&block_id, index) {
            Ok((Some(tx), header)) => (tx, header),
            Ok((None, header)) => {
                // odd case but send back what we know at least
                out_chan_tx.send(Message::Transaction(TransactionMessage {
                    block_id: Some(block_id),
                    height: Some(header.height),
                    transaction_id: tx_id,
                    transaction: None,
                }))?;

                return Err(
                    BlockStorageNotFoundError::TransactionAtBlockIndex(block_id, index).into(),
                );
            }
            Err(err) => {
                // odd case but send back what we know at least
                out_chan_tx.send(Message::Transaction(TransactionMessage {
                    block_id: Some(block_id),
                    height: None,
                    transaction_id: tx_id,
                    transaction: None,
                }))?;

                return Err(err.into());
            }
        };

        out_chan_tx.send(Message::Transaction(TransactionMessage {
            block_id: Some(block_id),
            height: Some(block_header.height),
            transaction_id: tx_id,
            transaction: Some(tx),
        }))?;

        Ok(())
    }

    /// Handle a request for a block header of the tip of the main chain from a peer
    async fn on_get_tip_header(&self, out_chan_tx: &OutChanSender) -> Result<(), PeerError> {
        info!("Received get_tip_header, from: {}", self.addr);
        let (tip_id, tip_header, tip_when) =
            match Processor::get_chain_tip_header(&self.ledger, &self.block_store) {
                Ok(Some(v)) => v,
                Ok(None) => {
                    // shouldn't be possible
                    out_chan_tx.send(Message::TipHeader(None))?;
                    return Err(LedgerNotFoundError::ChainTipHeader.into());
                }
                Err(err) => {
                    // shouldn't be possible
                    out_chan_tx.send(Message::TipHeader(None))?;
                    return Err(err.into());
                }
            };
        out_chan_tx.send(Message::TipHeader(Some(TipHeaderMessage {
            block_id: tip_id,
            block_header: tip_header,
            time_seen: tip_when,
        })))?;

        Ok(())
    }

    /// Handle receiving a transaction from a peer
    async fn on_push_transaction(
        &self,
        tx: Transaction,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        let id = match tx.id() {
            Ok(v) => v,
            Err(err) => {
                out_chan_tx.send(Message::PushTransactionResult(
                    PushTransactionResultMessage {
                        transaction_id: None,
                        error: Some(err.to_string()),
                    },
                ))?;
                return Err(err.into());
            }
        };

        info!("Received push_transaction: {}, from: {}", id, self.addr);

        // process the transaction if this is the first time we've seen it
        let mut err_str = None;
        if !self.tx_queue.exists(&id) {
            if let Err(err) = self
                .processor
                .process_candidate_transaction(&id, &tx, &self.addr)
                .await
            {
                err_str = Some(format!("{err:?}"));
            }
        };

        out_chan_tx.send(Message::PushTransactionResult(
            PushTransactionResultMessage {
                transaction_id: Some(id),
                error: err_str,
            },
        ))?;

        Ok(())
    }

    /// Handle a request to set a transaction filter for the connection
    async fn on_filter_load(
        &mut self,
        filter_type: String,
        exported_filter: ExportedCuckooFilter,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!(
            "Received filter_load (size: {}), from: {}",
            exported_filter.length, self.addr
        );

        // check filter type
        if filter_type != "cuckoo" {
            let err = PeerFilterError::TypeUnsupported(filter_type);
            out_chan_tx.send(Message::FilterResult(Some(FilterResultMessage {
                error: err.to_string(),
            })))?;

            return Err(err.into());
        }

        // check limit
        let max_size = 1 << 16;
        if exported_filter.length > max_size {
            let err = PeerFilterError::SizeExceeded(max_size);
            out_chan_tx.send(Message::FilterResult(Some(FilterResultMessage {
                error: err.to_string(),
            })))?;

            return Err(err.into());
        }

        // decode it
        let filter = CuckooFilter::<DefaultHasher>::from(exported_filter);
        if filter.is_empty() {
            let err = PeerFilterError::CreateFailed;
            out_chan_tx.send(Message::FilterResult(Some(FilterResultMessage {
                error: err.to_string(),
            })))?;

            return Err(err.into());
        }

        // set the filter
        self.filter = Some(filter);

        // send the empty result
        out_chan_tx.send(Message::FilterResult(None))?;

        Ok(())
    }

    /// Handle a request to add a set of public keys to the filter
    async fn on_filter_add(
        &mut self,
        pub_keys: Vec<PublicKey>,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!(
            "Received filter_add (public keys: {}), from: {}",
            pub_keys.len(),
            self.addr
        );

        // check limit
        let max_public_keys = 256;
        if pub_keys.len() > max_public_keys {
            let err = PeerFilterError::PublicKeysExceeded(max_public_keys);
            out_chan_tx.send(Message::FilterResult(Some(FilterResultMessage {
                error: err.to_string(),
            })))?;
            return Err(err.into());
        }

        // set the filter if it's not set
        let ok: Result<(), PeerFilterError> = (|| {
            if self.filter.is_none() {
                self.filter = Some(CuckooFilter::with_capacity(1 << 16));
            }
            let filter = self.filter.as_mut().expect("filter should exist");

            // perform the inserts
            for pub_key in pub_keys {
                if filter.add(&pub_key).is_err() {
                    return Err(PeerFilterError::InsertFailed);
                }
            }

            Ok(())
        })();

        // send the result
        let message = if let Err(err) = ok {
            Message::FilterResult(Some(FilterResultMessage {
                error: err.to_string(),
            }))
        } else {
            Message::FilterResult(None)
        };
        out_chan_tx.send(message)?;

        Ok(())
    }

    /// Send back a filtered view of the transaction queue
    async fn on_get_filter_transaction_queue(
        &self,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        info!("Received get_filter_transaction_queue, from: {}", self.addr);

        let message = if self.filter.is_none() {
            Message::FilterTransactionQueue(FilterTransactionQueueMessage {
                transactions: None,
                error: Some(FilterTransactionQueueError::FilterMissing.to_string()),
            })
        } else {
            let transactions = self.tx_queue.get(0);
            let mut ftq_transactions = Vec::new();
            for tx in transactions {
                if self.filter_lookup(&tx) {
                    ftq_transactions.push(tx);
                }
            }
            Message::FilterTransactionQueue(FilterTransactionQueueMessage {
                transactions: Some(ftq_transactions),
                error: None,
            })
        };
        out_chan_tx.send(message)?;

        Ok(())
    }

    /// Returns true if the transaction is of interest to the peer
    fn filter_lookup(&self, tx: &Transaction) -> bool {
        match self.filter {
            Some(ref filter) => {
                if !tx.is_coinbase()
                    && filter.contains(&tx.from.expect("transaction should have a sender"))
                {
                    return true;
                }

                filter.contains(&tx.to)
            }
            None => true,
        }
    }

    /// Called from the writer context
    fn create_filter_block(
        &self,
        id: BlockID,
        block: Block,
    ) -> Result<Option<FilterBlockMessage>, PeerError> {
        if self.filter.is_none() {
            // nothing to do
            return Ok(None);
        }

        // create a filter block
        let mut fb = FilterBlockMessage {
            block_id: id,
            header: block.header,
            transactions: Vec::new(),
        };

        // filter out transactions the peer isn't interested in
        for tx in &block.transactions {
            if self.filter_lookup(tx) {
                fb.transactions.push(tx.clone());
            }
        }

        Ok(Some(fb))
    }

    /// Received a request for peer addresses
    async fn on_get_peer_addresses(&self, out_chan_tx: &OutChanSender) -> Result<(), PeerError> {
        info!("Received get_peer_addresses message, from: {}", self.addr);

        // get up to 32 peers that have been connected to within the last 3 hours
        let time_ago = Duration::from_secs(3 * 60 * 60);
        let since = now_as_duration() - time_ago;
        let addresses = self
            .peer_store
            .get_since(32, since)?
            .into_iter()
            .map(|addr| addr.to_string())
            .collect::<Vec<_>>();

        if !addresses.is_empty() {
            out_chan_tx.send(Message::PeerAddresses(PeerAddressesMessage { addresses }))?;
        }

        Ok(())
    }

    /// Received a list of addresses
    async fn on_peer_addresses(&mut self, addresses: Vec<String>) -> Result<(), PeerError> {
        info!(
            "Received peer_addresses message with {} address(es), from: {}",
            addresses.len(),
            self.addr
        );

        let elapsed = match self.last_peer_addresses_received_time {
            Some(time) => time.elapsed(),
            None => Duration::MAX,
        };

        if elapsed < GET_PEER_ADDRESSES_PERIOD - Duration::from_secs(2 * 60) {
            // don't let a peer flood us with peer addresses
            info!(
                "Ignoring peer addresses, time since last addresses: {}",
                elapsed.as_secs()
            );
            return Ok(());
        }
        self.last_peer_addresses_received_time = Some(Instant::now());

        let limit = 32;
        for (i, addr) in addresses.into_iter().enumerate() {
            if i == limit {
                break;
            }
            // notify the peer manager
            self.addr_chan_tx.send(addr).await?;
        }

        Ok(())
    }

    /// Called when work has been received
    async fn on_get_work(
        &mut self,
        gw: GetWorkMessage,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        let ok = if self.work.is_some() {
            Err(PeerGetWorkError::WorkBlockExists)
        } else if gw.public_keys.is_empty() {
            Err(PeerGetWorkError::WorkBlockNoPublicKeys)
        } else if gw.memo.len() > MAX_MEMO_LENGTH {
            Err(PeerGetWorkError::WorkBlockMaxMemoLengthExceeded(
                MAX_MEMO_LENGTH,
                gw.memo.len(),
            ))
        } else {
            match Processor::get_chain_tip_header(&self.ledger, &self.block_store) {
                Ok(Some((tip_id, tip_header, _tip_when))) => {
                    // create and send out new work
                    self.pub_keys = gw.public_keys;
                    self.memo = Some(gw.memo);
                    self.create_new_work_block(&tip_id, &tip_header, out_chan_tx)
                        .await?;
                    Ok(())
                }
                Ok(None) => Err(LedgerNotFoundError::ChainTipHeader.into()),
                Err(err) => Err(PeerGetWorkError::Processor(self.addr, err)),
            }
        };

        if let Err(ref err) = ok {
            out_chan_tx.send(Message::Work(WorkMessage {
                work_id: None,
                header: None,
                min_time: None,
                error: Some(err.to_string()),
            }))?;
        }

        ok.map_err(Into::into)
    }

    /// Create a new work block for a mining peer.
    async fn create_new_work_block(
        &mut self,
        tip_id: &BlockID,
        tip_header: &BlockHeader,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        if self.pub_keys.is_empty() {
            // peer doesn't want work
            return Ok(());
        }

        let work_id = rand_int31();
        let peer_work = match Processor::compute_median_timestamp(tip_header, &self.block_store)
            .map_err(PeerError::ProcessorComputingMedianTimestamp)
        {
            Ok(median_timestamp) => {
                let key_index = rand::rng().random_range(0..self.pub_keys.len());
                match Miner::create_next_block(
                    tip_id,
                    tip_header,
                    &self.tx_queue,
                    &self.block_store,
                    &self.ledger,
                    self.pub_keys[key_index],
                    self.memo.clone(),
                )
                .map_err(PeerError::MinerCreateNextWorkBlock)
                {
                    Ok(work_block) => Ok(PeerWork {
                        work_id,
                        work_block,
                        median_timestamp,
                    }),
                    Err(err) => Err(err),
                }
            }
            Err(err) => Err(err),
        };

        // create a new block
        let message = match peer_work {
            Ok(ref peer_work) => Message::Work(WorkMessage {
                work_id: Some(peer_work.work_id),
                header: Some(peer_work.work_block.header.clone()),
                min_time: Some(peer_work.median_timestamp + 1),
                error: None,
            }),
            Err(ref err) => Message::Work(WorkMessage {
                work_id: Some(work_id),
                header: None,
                min_time: None,
                error: Some(err.to_string()),
            }),
        };
        out_chan_tx.send(message)?;
        self.work = peer_work.ok();

        Ok(())
    }

    /// Handle a submission of mining work.
    async fn on_submit_work(
        &mut self,
        sw: SubmitWorkMessage,
        out_chan_tx: &OutChanSender,
    ) -> Result<(), PeerError> {
        let block_id = if sw.work_id == 0 {
            Err(PeerSubmitWorkError::WorkIdMissing)
        } else {
            match sw.header.id() {
                Ok(id) => {
                    if let Some(ref mut work) = self.work {
                        if sw.work_id != work.work_id {
                            Err(PeerSubmitWorkError::WorkIdInvalid(work.work_id, sw.work_id))
                        } else {
                            work.work_block.header = sw.header;
                            match self
                                .processor
                                .process_candidate_block(id, work.work_block.clone(), self.addr)
                                .await
                            {
                                Ok(_) => Ok(id),
                                Err(err) => Err(err.into()),
                            }
                        }
                    } else {
                        Err(PeerSubmitWorkError::WorkIdPeerMissing)
                    }
                }
                Err(err) => Err(err.into()),
            }
        };

        let message = if let Err(ref err) = block_id {
            Message::SubmitWorkResult(SubmitWorkResultMessage {
                work_id: sw.work_id,
                error: Some(err.to_string()),
            })
        } else {
            Message::SubmitWorkResult(SubmitWorkResultMessage {
                work_id: sw.work_id,
                error: None,
            })
        };
        out_chan_tx.send(message)?;

        if let Err(err) = block_id {
            Err(err.into())
        } else {
            Ok(())
        }
    }

    /// Update the read limit if necessary
    fn update_read_limit(&mut self) {
        let (ok, height) = PeerManager::is_initial_block_download(&self.ledger, &self.block_store)
            .unwrap_or_else(|err| panic!("{err:?}"));

        if ok {
            // TODO: do something smarter about this
            self.read_limit = 0;
            return;
        }

        // transactions are <500 bytes so this gives us significant wiggle room
        let max_transactions = Processor::compute_max_transactions_per_block(height + 1);
        self.read_limit = max_transactions * 1024;
    }

    /// Send outgoing messages with a write timeout period
    async fn send_with_timeout(
        &self,
        ws_sender: &mut WsSink,
        message: WsMessage,
    ) -> Result<(), PeerConnectionError> {
        match timeout(WRITE_WAIT, ws_sender.send(message)).await {
            Err(err) => Err(PeerConnectionError::Timeout(self.addr, err)),
            Ok(Err(err)) => Err(err.into()),
            _ => Ok(()),
        }
    }

    /// Specifies a handler to call when the peer connection is closed.
    pub fn on_shutdown(&mut self, shutdown_fn: impl Fn() + 'static + Send + Sync) {
        self.shutdown_fns.push(Box::new(shutdown_fn));
    }
}

impl Drop for Peer {
    fn drop(&mut self) {
        // peer is shutting down
        if self.conn.is_none() {
            info!("Closed connection with: {}", self.addr);
        }

        for shutdown_fn in &self.shutdown_fns {
            shutdown_fn();
        }
    }
}

#[derive(Error)]
pub enum PeerError {
    #[error("block {0} at height {1} less than latest checkpoint height {2}")]
    BlockAtHeightLessThanCheckpoint(BlockID, u64, u64),
    #[error("received empty block, from {0}")]
    EmptyBlockReceived(SocketAddr),
    #[error("received empty transaction")]
    EmptyTransactionReceive,
    #[error("{0} blocks IDs is more than {1} maximum per inv_block")]
    InvBlockMaxBlocks(usize, usize),
    #[error("max block ignore list size exceeded")]
    MaxIgnoreListSizeExceeded,
    #[error("received invalid message, from: {0}")]
    MessageInvalid(SocketAddr, #[source] JsonError),
    #[error("received too large ({0} bytes) of a '{1}' message, from: {2}")]
    MessageLengthExceeded(usize, String, SocketAddr),
    #[error("received non-utf8 clean message, from: {0}")]
    MessageNotUtf8(SocketAddr, #[source] DataError),
    #[error("retrieving transaction history, block: {0}, index: {1} -> block storage")]
    PublicKeyTransactionBlockStorage(BlockID, u32, #[source] BlockStorageError),
    #[error("retrieving transaction history, block: {0}, index: {1}: no transaction found")]
    PublicKeyTransactionNotFound(BlockID, u32),
    #[error("received unrequested block")]
    ReceivedUnrequestedBlock,
    #[error("sync has stalled, disconnecting from from: {0}")]
    SyncStalled(SocketAddr),

    #[error("miner -> creating next block")]
    MinerCreateNextWorkBlock(#[source] MinerError),
    #[error("processor -> computing median timestamp")]
    ProcessorComputingMedianTimestamp(#[source] ProcessorError),

    #[error("peer balances")]
    PeerBalances(#[from] PeerBalancesError),
    #[error("peer connection")]
    PeerConnection(#[from] PeerConnectionError),
    #[error("peer filter")]
    PeerFilter(#[from] PeerFilterError),
    #[error("peer get work")]
    PeerGetWork(#[from] PeerGetWorkError),
    #[error("peer submit work")]
    PeerSubmitWork(#[from] PeerSubmitWorkError),

    #[error("block")]
    Block(#[from] BlockError),
    #[error("block storage")]
    BlockStorage(#[from] BlockStorageError),
    #[error("block storage not found")]
    BlockStorageNotFound(#[from] BlockStorageNotFoundError),
    #[error("channel")]
    Channel(#[from] ChannelError),
    #[error("json")]
    Json(#[from] JsonError),
    #[error("ledger")]
    Ledger(#[from] LedgerError),
    #[error("ledger not found")]
    LedgerNotFound(#[from] LedgerNotFoundError),
    #[error("peer manager")]
    PeerManager(#[from] PeerManagerError),
    #[error("peer storage")]
    PeerStorage(#[from] PeerStorageError),
    #[error("processing block")]
    ProcessBlock(#[from] ProcessBlockError),
    #[error("processor")]
    Processor(#[from] ProcessorError),
    #[error("transaction")]
    Transaction(#[from] TransactionError),
}

impl_debug_error_chain!(PeerError, "peer");

impl From<tokio::sync::mpsc::error::SendError<GetWorkMessage>> for PeerError {
    fn from(err: tokio::sync::mpsc::error::SendError<GetWorkMessage>) -> Self {
        Self::Channel(ChannelError::Send("get work", err.to_string()))
    }
}

impl From<tokio::sync::mpsc::error::SendError<Message>> for PeerError {
    fn from(err: tokio::sync::mpsc::error::SendError<Message>) -> Self {
        Self::Channel(ChannelError::Send("out", err.to_string()))
    }
}

impl From<tokio::sync::mpsc::error::SendError<String>> for PeerError {
    fn from(err: tokio::sync::mpsc::error::SendError<String>) -> Self {
        Self::Channel(ChannelError::Send("addr", err.to_string()))
    }
}

impl From<tokio::sync::mpsc::error::SendError<SubmitWorkMessage>> for PeerError {
    fn from(err: tokio::sync::mpsc::error::SendError<SubmitWorkMessage>) -> Self {
        Self::Channel(ChannelError::Send("submit work", err.to_string()))
    }
}

#[derive(Error, Debug)]
pub enum PeerConnectionError {
    #[error("failed accepting incoming from: {0}")]
    Accept(SocketAddr, #[source] tokio_tungstenite::tungstenite::Error),
    #[error("failed connecting to peer: {0}")]
    Connect(SocketAddr, #[source] tokio_tungstenite::tungstenite::Error),
    #[error("websocket connection lost, closing...")]
    Dropped(SocketAddr),
    #[error("connection timeout for peer: {0}")]
    Timeout(SocketAddr, #[source] tokio::time::error::Elapsed),

    #[error("peer storage")]
    PeerStorage(#[from] PeerStorageError),

    #[error("websocket header")]
    HttpHeaderValue(#[from] tokio_tungstenite::tungstenite::http::header::InvalidHeaderValue),
    #[error("websocket")]
    Websocket(#[from] tokio_tungstenite::tungstenite::Error),
}

/// Error type associated with cruzbit protocol messages
#[derive(Error, Debug)]
pub enum FilterTransactionQueueError {
    #[error("No filter set")]
    FilterMissing,
}

/// Error type associated with cruzbit protocol messages
#[derive(Error, Debug)]
pub enum PeerBalanceError {
    #[error("ledger")]
    Ledger(#[from] LedgerError),
}

/// Error type associated with cruzbit protocol messages
#[derive(Error, Debug)]
pub enum PeerBalancesError {
    #[error("Too many public keys, limit: {0}")]
    PublicKeysExceeded(usize),
}

/// Error type associated with cruzbit protocol messages
#[derive(Error, Debug)]
pub enum PeerFilterError {
    #[error("Failed to create filter")]
    CreateFailed,
    #[error("Unable to insert into filter")]
    InsertFailed,
    #[error("Too many public keys, limit: {0}")]
    PublicKeysExceeded(usize),
    #[error("Filter too large, max {0}")]
    SizeExceeded(usize),
    #[error("Unsupported filter type {0}")]
    TypeUnsupported(String),
}

/// Error type associated with cruzbit protocol messages
#[derive(Error, Debug)]
pub enum PeerGetWorkError {
    #[error("Work block already exists")]
    WorkBlockExists,
    #[error("Work block max memo length ({0}) exceeded: {1}")]
    WorkBlockMaxMemoLengthExceeded(usize, usize),
    #[error("Work block has no public keys")]
    WorkBlockNoPublicKeys,

    #[error("ledger not found")]
    LedgerNotFound(#[from] LedgerNotFoundError),

    #[error("getting chain tip header, for: {0}")]
    Processor(SocketAddr, #[source] ProcessorError),
}

/// Error type associated with cruzbit protocol messages
#[derive(Error, Debug)]
pub enum PeerSubmitWorkError {
    #[error("Unexpected work id {0}, found {1}")]
    WorkIdInvalid(u32, u32),
    #[error("No work id set")]
    WorkIdMissing,
    #[error("No work id set on peer")]
    WorkIdPeerMissing,

    #[error("block id")]
    Block(#[from] BlockError),
    #[error("processing work block")]
    ProcessBlock(#[from] ProcessBlockError),
}