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
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
use core::marker::PhantomData;
use std::{
    cmp::max,
    collections::VecDeque,
    fmt::{Debug, Display, Error as FmtError, Formatter},
    iter,
};

use crate::{
    session::{SessionBoundaryInfo, SessionId},
    sync::{
        data::{BranchKnowledge, NetworkData, Request, State},
        forest::{
            Error as ForestError, ExtensionRequest, Forest,
            InitializationError as ForestInitializationError, Interest,
        },
        handler::request_handler::RequestHandler,
        Block, BlockImport, BlockStatus, ChainStatus, Finalizer, Header, Justification, PeerId,
        UnverifiedHeader, UnverifiedHeaderFor, UnverifiedJustification, Verifier,
    },
    BlockId, BlockNumber, SyncOracle,
};

mod request_handler;
pub use request_handler::{block_to_response, Action, RequestHandlerError};

use crate::sync::data::{ResponseItem, ResponseItems};

/// Handles for interacting with the blockchain database.
pub struct DatabaseIO<B, J, CS, F, BI>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    CS: ChainStatus<B, J>,
    F: Finalizer<J>,
    BI: BlockImport<B>,
{
    chain_status: CS,
    finalizer: F,
    block_importer: BI,
    _phantom: PhantomData<(B, J)>,
}

impl<B, J, CS, F, BI> DatabaseIO<B, J, CS, F, BI>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    CS: ChainStatus<B, J>,
    F: Finalizer<J>,
    BI: BlockImport<B>,
{
    pub fn new(chain_status: CS, finalizer: F, block_importer: BI) -> Self {
        Self {
            chain_status,
            finalizer,
            block_importer,
            _phantom: PhantomData,
        }
    }
}

/// A handle for requesting Interest.
pub struct InterestProvider<'a, I, J>
where
    I: PeerId,
    J: Justification,
{
    forest: &'a Forest<I, J>,
}

impl<'a, I, J> InterestProvider<'a, I, J>
where
    I: PeerId,
    J: Justification,
{
    pub fn get(&self, id: &BlockId) -> Interest<I> {
        self.forest.request_interest(id)
    }
}

/// Types used by the Handler. For improved readability.
pub trait HandlerTypes {
    /// What can go wrong when handling a piece of data.
    type Error;
}

// This is only required because we don't control block imports
// and thus we can get notifications about blocks being imported that
// don't fit in the forest. This struct lets us work around this by
// manually syncing the forest after such an event.
//TODO(A0-2984): remove this after legacy sync is excised
enum MissedImportData {
    AllGood,
    MissedImports {
        highest_missed: BlockNumber,
        last_sync: BlockNumber,
    },
}

enum TrySyncError<B, J, CS>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    CS: ChainStatus<B, J>,
{
    ChainStatus(CS::Error),
    Forest(ForestError),
}

impl MissedImportData {
    pub fn new() -> Self {
        Self::AllGood
    }

    pub fn update<B, J, CS>(
        &mut self,
        missed: BlockNumber,
        chain_status: &CS,
    ) -> Result<(), CS::Error>
    where
        J: Justification,
        B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
        CS: ChainStatus<B, J>,
    {
        use MissedImportData::*;
        match self {
            AllGood => {
                *self = MissedImports {
                    highest_missed: missed,
                    last_sync: chain_status.top_finalized()?.header().id().number(),
                }
            }
            MissedImports { highest_missed, .. } => *highest_missed = max(*highest_missed, missed),
        }
        Ok(())
    }

    pub fn try_sync<B, I, J, CS>(
        &mut self,
        chain_status: &CS,
        forest: &mut Forest<I, J>,
    ) -> Result<(), TrySyncError<B, J, CS>>
    where
        J: Justification,
        B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
        I: PeerId,
        CS: ChainStatus<B, J>,
    {
        use MissedImportData::*;
        if let MissedImports {
            highest_missed,
            last_sync,
        } = self
        {
            let top_finalized = chain_status
                .top_finalized()
                .map_err(TrySyncError::ChainStatus)?
                .header()
                .id();
            // we don't want this to happen too often, but it also cannot be too close to the max forest size, thus semi-random weird looking threshold
            if top_finalized.number() - *last_sync <= 1312 {
                return Ok(());
            }
            let mut to_import = VecDeque::from(
                chain_status
                    .children(top_finalized.clone())
                    .map_err(TrySyncError::ChainStatus)?,
            );
            while let Some(header) = to_import.pop_front() {
                if header.id().number() > *highest_missed {
                    break;
                }
                // we suppress all errors except `TooNew` since we are likely trying to mark things that are already marked and they would be throwing a lot of stuff
                match forest.update_body(&header) {
                    Ok(()) => (),
                    Err(ForestError::TooNew) => {
                        *last_sync = top_finalized.number();
                        return Ok(());
                    }
                    Err(e) => return Err(TrySyncError::Forest(e)),
                }
                to_import.extend(
                    chain_status
                        .children(header.id())
                        .map_err(TrySyncError::ChainStatus)?,
                );
            }
            *self = AllGood;
        }
        Ok(())
    }
}

/// Handler for data incoming from the network.
pub struct Handler<B, I, J, CS, V, F, BI>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    I: PeerId,
    CS: ChainStatus<B, J>,
    V: Verifier<J>,
    F: Finalizer<J>,
    BI: BlockImport<B>,
{
    chain_status: CS,
    verifier: V,
    finalizer: F,
    forest: Forest<I, J>,
    session_info: SessionBoundaryInfo,
    block_importer: BI,
    missed_import_data: MissedImportData,
    sync_oracle: SyncOracle,
    phantom: PhantomData<B>,
}

/// What actions can the handler recommend as a reaction to some data.
#[derive(Clone, Debug)]
pub enum HandleStateAction<B, J>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
{
    /// A response for the peer that sent us the data.
    Response(NetworkData<B, J>),
    /// The state suggests we should try requesting a chain extension.
    ExtendChain,
    /// Do nothing.
    Noop,
}

impl<B, J> HandleStateAction<B, J>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
{
    fn response(justification: J::Unverified, other_justification: Option<J::Unverified>) -> Self {
        Self::Response(NetworkData::StateBroadcastResponse(
            justification,
            other_justification,
        ))
    }

    fn maybe_extend(new_info: bool) -> Self {
        match new_info {
            true => HandleStateAction::ExtendChain,
            false => HandleStateAction::Noop,
        }
    }
}

/// What can go wrong when handling a piece of data.
#[allow(clippy::enum_variant_names)]
#[derive(Clone, Debug)]
pub enum Error<B, J, CS, V, F>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    CS: ChainStatus<B, J>,
    V: Verifier<J>,
    F: Finalizer<J>,
{
    Verifier(V::Error),
    ChainStatus(CS::Error),
    Finalizer(F::Error),
    Forest(ForestError),
    ForestInitialization(ForestInitializationError<B, J, CS>),
    RequestHandlerError(RequestHandlerError<CS::Error>),
    MissingJustification,
    BlockNotImportable,
    HeaderNotRequired,
    MissingFavouriteBlock,
}

impl<B, J, CS, V, F> Display for Error<B, J, CS, V, F>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    CS: ChainStatus<B, J>,
    V: Verifier<J>,
    F: Finalizer<J>,
{
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
        use Error::*;
        match self {
            Verifier(e) => write!(f, "verifier error: {e}"),
            ChainStatus(e) => write!(f, "chain status error: {e}"),
            Finalizer(e) => write!(f, "finalized error: {e}"),
            Forest(e) => write!(f, "forest error: {e}"),
            ForestInitialization(e) => write!(f, "forest initialization error: {e}"),
            RequestHandlerError(e) => write!(f, "request handler error: {e}"),
            MissingJustification => write!(
                f,
                "justification for the last block of a past session missing"
            ),
            BlockNotImportable => {
                write!(f, "cannot import a block that we do not consider required")
            }
            HeaderNotRequired => write!(f, "header was not required, but it should have been"),
            MissingFavouriteBlock => {
                write!(f, "the favourite block is not present in the database")
            }
        }
    }
}

impl<B, J, CS, V, F> From<ForestError> for Error<B, J, CS, V, F>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    CS: ChainStatus<B, J>,
    V: Verifier<J>,
    F: Finalizer<J>,
{
    fn from(e: ForestError) -> Self {
        Error::Forest(e)
    }
}

impl<B, J, CS, V, F> From<TrySyncError<B, J, CS>> for Error<B, J, CS, V, F>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    CS: ChainStatus<B, J>,
    V: Verifier<J>,
    F: Finalizer<J>,
{
    fn from(e: TrySyncError<B, J, CS>) -> Self {
        use TrySyncError::*;
        match e {
            ChainStatus(e) => Error::ChainStatus(e),
            Forest(e) => Error::Forest(e),
        }
    }
}

impl<B, J, CS, V, F> From<RequestHandlerError<CS::Error>> for Error<B, J, CS, V, F>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    CS: ChainStatus<B, J>,
    V: Verifier<J>,
    F: Finalizer<J>,
{
    fn from(e: RequestHandlerError<CS::Error>) -> Self {
        Error::RequestHandlerError(e)
    }
}

impl<B, I, J, CS, V, F, BI> HandlerTypes for Handler<B, I, J, CS, V, F, BI>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    I: PeerId,
    CS: ChainStatus<B, J>,
    V: Verifier<J>,
    F: Finalizer<J>,
    BI: BlockImport<B>,
{
    type Error = Error<B, J, CS, V, F>;
}

impl<B, I, J, CS, V, F, BI> Handler<B, I, J, CS, V, F, BI>
where
    J: Justification,
    B: Block<UnverifiedHeader = UnverifiedHeaderFor<J>>,
    I: PeerId,
    CS: ChainStatus<B, J>,
    V: Verifier<J>,
    F: Finalizer<J>,
    BI: BlockImport<B>,
{
    /// New handler with the provided chain interfaces.
    pub fn new(
        database_io: DatabaseIO<B, J, CS, F, BI>,
        verifier: V,
        sync_oracle: SyncOracle,
        session_info: SessionBoundaryInfo,
    ) -> Result<Self, <Self as HandlerTypes>::Error> {
        let DatabaseIO {
            chain_status,
            finalizer,
            block_importer,
            ..
        } = database_io;
        let forest = Forest::new(&chain_status).map_err(Error::ForestInitialization)?;
        Ok(Handler {
            chain_status,
            verifier,
            finalizer,
            forest,
            session_info,
            block_importer,
            missed_import_data: MissedImportData::new(),
            sync_oracle,
            phantom: PhantomData,
        })
    }

    fn try_finalize(&mut self) -> Result<(), <Self as HandlerTypes>::Error> {
        let mut number = self
            .chain_status
            .top_finalized()
            .map_err(Error::ChainStatus)?
            .header()
            .id()
            .number()
            + 1;
        loop {
            while let Some(justification) = self.forest.try_finalize(&number) {
                self.finalizer
                    .finalize(justification)
                    .map_err(Error::Finalizer)?;
                number += 1;
            }
            number = self
                .session_info
                .last_block_of_session(self.session_info.session_id_from_block_num(number));
            match self.forest.try_finalize(&number) {
                Some(justification) => {
                    self.finalizer
                        .finalize(justification)
                        .map_err(Error::Finalizer)?;
                    number += 1;
                }
                None => {
                    self.missed_import_data
                        .try_sync(&self.chain_status, &mut self.forest)?;
                    return Ok(());
                }
            };
        }
    }

    /// Inform the handler that a block has been imported.
    pub fn block_imported(
        &mut self,
        header: J::Header,
    ) -> Result<(), <Self as HandlerTypes>::Error> {
        if let Err(e) = self.forest.update_body(&header) {
            if matches!(e, ForestError::TooNew) {
                self.missed_import_data
                    .update(header.id().number(), &self.chain_status)
                    .map_err(Error::ChainStatus)?;
            }
            return Err(e.into());
        }
        self.try_finalize()
    }

    /// Handle a request for potentially substantial amounts of data.
    ///
    /// Returns what action we should take in response to the request.
    /// We either do nothing, request new interesting block to us or send a response containing
    /// path of justifications, blocks and headers. We try to be as helpful as we can, sometimes
    /// including more information from what was requested, sometimes ignoring their requested id
    /// if we know it makes sense.
    pub fn handle_request(
        &mut self,
        request: Request<J>,
    ) -> Result<Action<B, J>, <Self as HandlerTypes>::Error> {
        let request_handler = RequestHandler::new(&self.chain_status, &self.session_info);

        Ok(match request_handler.action(request)? {
            Action::RequestBlock(id)
                if !self.forest.update_block_identifier(&id, None, true)? =>
            {
                Action::Noop
            }
            action => action,
        })
    }

    /// Handle a chain extension request.
    ///
    /// First treats it as a request for our favourite block with their favourite block
    /// as the top imported.
    /// If that fails due to our favourite block not being a descendant of theirs,
    /// it falls back to attempting to send any finalized blocks the requester doesn't have.
    pub fn handle_chain_extension_request(
        &mut self,
        state: State<J>,
    ) -> Result<Action<B, J>, <Self as HandlerTypes>::Error> {
        let request = Request::new(
            self.forest.favourite_block(),
            BranchKnowledge::TopImported(state.favourite_block().id()),
            state.clone(),
        );
        match self.handle_request(request) {
            // Either we were trying to send too far in the future
            // or our favourite is not a descendant of theirs.
            // Either way, at least try sending some justifications.
            Ok(Action::Noop)
            | Err(Error::RequestHandlerError(RequestHandlerError::RootMismatch)) => {
                let request = Request::new(
                    state.top_justification().header().id(),
                    BranchKnowledge::TopImported(state.top_justification().header().id()),
                    state,
                );
                self.handle_request(request)
            }
            result => result,
        }
    }

    /// Handle a single unverified justification.
    /// Return whether this justification was higher than the previously known highest justification.
    fn handle_justification(
        &mut self,
        justification: J::Unverified,
        maybe_peer: Option<I>,
    ) -> Result<bool, <Self as HandlerTypes>::Error> {
        let justification = self
            .verifier
            .verify_justification(justification)
            .map_err(Error::Verifier)?;
        let new_highest = self
            .forest
            .update_justification(justification, maybe_peer)?;
        self.try_finalize()?;
        self.sync_oracle
            .update_behind(self.forest.behind_finalization());
        Ok(new_highest)
    }

    /// Verify an unverified header.
    fn verify_header(
        &mut self,
        header: UnverifiedHeaderFor<J>,
    ) -> Result<J::Header, <Self as HandlerTypes>::Error> {
        self.verifier.verify_header(header).map_err(Error::Verifier)
    }

    /// Handle a justification from the user, returning whether it became the new highest justification.
    pub fn handle_justification_from_user(
        &mut self,
        justification: J::Unverified,
    ) -> Result<bool, <Self as HandlerTypes>::Error> {
        self.handle_justification(justification, None)
    }

    /// Handle a state response returning whether it resulted in a new highest justified block
    /// and possibly an error.
    ///
    /// If no error is returned, it means that the whole state response was processed
    /// correctly. Otherwise, the response might have been processed partially, or
    /// dropped. In any case, the Handler finishes in a sane state.
    pub fn handle_state_response(
        &mut self,
        justification: J::Unverified,
        maybe_justification: Option<J::Unverified>,
        peer: I,
    ) -> (bool, Option<<Self as HandlerTypes>::Error>) {
        let mut new_highest = false;

        for justification in iter::once(justification).chain(maybe_justification) {
            new_highest = match self.handle_justification(justification, Some(peer.clone())) {
                Ok(new_highest) => new_highest,
                Err(e) => return (new_highest, Some(e)),
            } || new_highest;
        }

        (new_highest, None)
    }

    /// Handle a request response returning whether it resulted in a new highest justified block
    /// and possibly an error.
    ///
    /// If no error is returned, it means that the whole request response was processed
    /// correctly. Otherwise, the response might have been processed partially, or
    /// dropped. In any case, the Handler finishes in a sane state.
    ///
    /// Note that this method does not verify nor import blocks. The received blocks
    /// are stored in a buffer, and might be silently discarded in the future
    /// if the import fails.
    pub fn handle_request_response(
        &mut self,
        response_items: ResponseItems<B, J>,
        peer: I,
    ) -> (bool, Option<<Self as HandlerTypes>::Error>) {
        let mut new_highest = false;
        // Lets us import descendands of importable blocks, useful for favourite blocks.
        let mut last_imported_block: Option<BlockId> = None;
        for item in response_items {
            match item {
                ResponseItem::Justification(j) => {
                    match self.handle_justification(j, Some(peer.clone())) {
                        Ok(highest) => new_highest = new_highest || highest,
                        Err(e) => return (new_highest, Some(e)),
                    }
                }
                ResponseItem::Header(h) => {
                    if self.forest.skippable(&h.id()) {
                        continue;
                    }
                    let h = match self.verify_header(h) {
                        Ok(h) => h,
                        Err(e) => return (new_highest, Some(e)),
                    };
                    if let Err(e) = self.forest.update_header(&h, Some(peer.clone()), false) {
                        return (new_highest, Some(Error::Forest(e)));
                    }
                    if !self.forest.importable(&h.id()) {
                        return (new_highest, Some(Error::HeaderNotRequired));
                    }
                }
                ResponseItem::Block(b) => {
                    if self.forest.skippable(&b.header().id()) {
                        continue;
                    }
                    match self.forest.importable(&b.header().id())
                        || last_imported_block
                            .map(|id| id == b.header().id())
                            .unwrap_or(false)
                    {
                        true => {
                            last_imported_block = Some(b.header().id());
                            self.block_importer.import_block(b);
                        }
                        false => return (new_highest, Some(Error::BlockNotImportable)),
                    };
                }
            }
        }

        (new_highest, None)
    }

    fn last_justification_unverified(
        &self,
        session: SessionId,
    ) -> Result<J::Unverified, <Self as HandlerTypes>::Error> {
        use Error::*;
        Ok(self
            .chain_status
            .finalized_at(self.session_info.last_block_of_session(session))
            .map_err(ChainStatus)?
            .has_justification()
            .ok_or(MissingJustification)?
            .into_unverified())
    }

    /// Handle a state broadcast returning the actions we should take in response.
    pub fn handle_state(
        &mut self,
        state: State<J>,
        peer: I,
    ) -> Result<HandleStateAction<B, J>, <Self as HandlerTypes>::Error> {
        use Error::*;
        let remote_top_number = state.top_justification().header().id().number();
        let local_top = self.chain_status.top_finalized().map_err(ChainStatus)?;
        let local_top_number = local_top.header().id().number();
        let remote_session = self
            .session_info
            .session_id_from_block_num(remote_top_number);
        let local_session = self
            .session_info
            .session_id_from_block_num(local_top_number);
        match local_session.0.checked_sub(remote_session.0) {
            // remote session number larger than ours, we can try to import the justification
            None => {
                let header = self.verify_header(state.favourite_block())?;
                Ok(HandleStateAction::maybe_extend(
                    self.handle_justification(state.top_justification(), Some(peer.clone()))?
                        || self.forest.update_header(&header, Some(peer), false)?,
                ))
            }
            // same session
            Some(0) => match remote_top_number >= local_top_number {
                // remote top justification higher than ours, we can import the justification
                true => {
                    let header = self.verify_header(state.favourite_block())?;
                    Ok(HandleStateAction::maybe_extend(
                        self.handle_justification(state.top_justification(), Some(peer.clone()))?
                            || self.forest.update_header(&header, Some(peer), false)?,
                    ))
                }
                // remote top justification lower than ours, we can send a response
                false => Ok(HandleStateAction::response(
                    local_top.into_unverified(),
                    None,
                )),
            },
            // remote lags one session behind
            Some(1) => Ok(HandleStateAction::response(
                self.last_justification_unverified(remote_session)?,
                Some(local_top.into_unverified()),
            )),
            // remote lags multiple sessions behind
            Some(2..) => Ok(HandleStateAction::response(
                self.last_justification_unverified(remote_session)?,
                Some(self.last_justification_unverified(SessionId(remote_session.0 + 1))?),
            )),
        }
    }

    /// The current state of our database.
    pub fn state(&self) -> Result<State<J>, <Self as HandlerTypes>::Error> {
        use BlockStatus::*;
        let top_justification = self
            .chain_status
            .top_finalized()
            .map_err(Error::ChainStatus)?
            .into_unverified();
        let favourite_block = match self
            .chain_status
            .status_of(self.forest.favourite_block())
            .map_err(Error::ChainStatus)?
        {
            Justified(justification) => justification.header().clone().into_unverified(),
            Present(header) => header.into_unverified(),
            Unknown => return Err(Error::MissingFavouriteBlock),
        };
        Ok(State::new(top_justification, favourite_block))
    }

    /// A handle for requesting Interest.
    pub fn interest_provider(&self) -> InterestProvider<I, J> {
        InterestProvider {
            forest: &self.forest,
        }
    }

    /// Handle an internal block request.
    /// Returns `true` if this was the first time something indicated interest in this block.
    pub fn handle_internal_request(
        &mut self,
        id: &BlockId,
    ) -> Result<bool, <Self as HandlerTypes>::Error> {
        let should_request = self.forest.update_block_identifier(id, None, true)?;

        Ok(should_request)
    }

    /// Returns the extension request we could be making right now.
    pub fn extension_request(&self) -> ExtensionRequest<I> {
        self.forest.extension_request()
    }

    /// Handle a block freshly created by this node. Imports it and returns a form of it that can be broadcast.
    pub fn handle_own_block(&mut self, block: B) -> Vec<ResponseItem<B, J>> {
        self.block_importer.import_block(block.clone());
        block_to_response(block)
    }
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::{DatabaseIO, Error, HandleStateAction, HandleStateAction::*, Handler};
    use crate::{
        session::{SessionBoundaryInfo, SessionId},
        sync::{
            data::{BranchKnowledge::*, NetworkData, Request, ResponseItem, ResponseItems, State},
            forest::{ExtensionRequest, Interest},
            handler::Action,
            mock::{Backend, MockBlock, MockHeader, MockJustification, MockPeerId},
            Block, BlockImport, ChainStatus,
            ChainStatusNotification::*,
            ChainStatusNotifier, Header, Justification,
        },
        BlockId, BlockNumber, SessionPeriod, SyncOracle,
    };

    type TestHandler =
        Handler<MockBlock, MockPeerId, MockJustification, Backend, Backend, Backend, Backend>;
    type MockResponseItems = ResponseItems<MockBlock, MockJustification>;

    const SESSION_BOUNDARY_INFO: SessionBoundaryInfo = SessionBoundaryInfo::new(SessionPeriod(20));

    fn setup() -> (
        TestHandler,
        Backend,
        impl ChainStatusNotifier<MockHeader>,
        BlockId,
    ) {
        let (backend, notifier) = Backend::setup(SESSION_BOUNDARY_INFO);
        let verifier = backend.clone();
        let database_io = DatabaseIO::new(backend.clone(), backend.clone(), backend.clone());
        let handler = Handler::new(
            database_io,
            verifier,
            SyncOracle::new(),
            SESSION_BOUNDARY_INFO,
        )
        .expect("mock backend works");
        let genesis = backend.top_finalized().expect("genesis").header().id();
        (handler, backend, notifier, genesis)
    }

    fn import_branch(backend: &mut Backend, branch_length: usize) -> Vec<MockHeader> {
        let result: Vec<_> = backend
            .top_finalized()
            .expect("mock backend works")
            .header()
            .random_branch()
            .take(branch_length)
            .collect();
        for header in &result {
            backend.import_block(MockBlock::new(header.clone(), true));
        }
        result
    }

    fn assert_dangling_branch_required(
        handler: &TestHandler,
        top: &BlockId,
        bottom: &BlockId,
        know_most: HashSet<MockPeerId>,
    ) {
        assert_eq!(
            handler.interest_provider().get(bottom),
            Interest::Uninterested,
            "should not be interested in the bottom"
        );
        assert_eq!(
            handler.interest_provider().get(top),
            Interest::Required {
                know_most,
                branch_knowledge: LowestId(bottom.clone()),
            },
            "should require the top"
        );
    }

    fn grow_light_branch_till(
        handler: &mut TestHandler,
        bottom: &BlockId,
        top: &BlockNumber,
        peer_id: MockPeerId,
    ) -> Vec<MockHeader> {
        assert!(top > &bottom.number(), "must not be empty");
        grow_light_branch(handler, bottom, (top - bottom.number()) as usize, peer_id)
    }

    fn grow_light_branch(
        handler: &mut TestHandler,
        bottom: &BlockId,
        length: usize,
        peer_id: MockPeerId,
    ) -> Vec<MockHeader> {
        let branch: Vec<_> = bottom.random_branch().take(length).collect();
        let top = branch.last().expect("branch should not be empty").id();

        assert!(
            handler.handle_internal_request(&top).expect("should work"),
            "should be newly required"
        );
        assert_eq!(
            handler.interest_provider().get(&top),
            Interest::Required {
                know_most: HashSet::new(),
                branch_knowledge: LowestId(top.clone()),
            },
            "should be required"
        );

        let (new_highest_justified, maybe_error) = handler.handle_request_response(
            branch
                .iter()
                .cloned()
                .rev()
                .map(ResponseItem::Header)
                .collect(),
            peer_id,
        );

        assert!(!new_highest_justified);
        assert!(maybe_error.is_none(), "should work");

        branch
    }

    fn create_dangling_branch(
        handler: &mut TestHandler,
        height: BlockNumber,
        length: usize,
        peer_id: MockPeerId,
    ) -> (BlockId, BlockId) {
        let bottom = BlockId::new_random(height);
        let top = grow_light_branch(handler, &bottom, length, peer_id)
            .last()
            .expect("branch should not be empty")
            .id();
        (bottom, top)
    }

    struct BranchResponseContent {
        headers: bool,
        blocks: bool,
        justifications: bool,
    }

    fn branch_response(
        branch: Vec<MockHeader>,
        content: BranchResponseContent,
    ) -> MockResponseItems {
        let mut response = vec![];
        if content.headers {
            response.extend(branch.iter().cloned().rev().map(ResponseItem::Header));
        }
        if content.blocks {
            response.extend(
                branch
                    .iter()
                    .cloned()
                    .map(|header| ResponseItem::Block(MockBlock::new(header, true))),
            );
        }
        if content.justifications {
            response.extend(
                branch
                    .into_iter()
                    .map(MockJustification::for_header)
                    .map(ResponseItem::Justification),
            );
        }
        response
    }

    async fn grow_trunk(
        handler: &mut TestHandler,
        backend: &mut Backend,
        notifier: &mut impl ChainStatusNotifier<MockHeader>,
        bottom: &BlockId,
        length: usize,
    ) -> BlockId {
        let branch: Vec<_> = bottom.random_branch().take(length).collect();
        let top = branch.last().expect("should not be empty").id();
        for header in branch.iter() {
            let block = MockBlock::new(header.clone(), true);
            let justification = MockJustification::for_header(header.clone());
            handler
                .handle_justification_from_user(justification)
                .expect("should work");
            backend.import_block(block);
            match notifier.next().await {
                Ok(BlockImported(header)) => {
                    handler.block_imported(header).expect("should work");
                }
                _ => panic!("should notify about imported block"),
            }
            match notifier.next().await {
                Ok(BlockFinalized(finalized_header)) => assert_eq!(
                    header, &finalized_header,
                    "should finalize the current header"
                ),
                _ => panic!("should notify about finalized block"),
            }
        }
        top
    }

    async fn mark_branch_imported(
        handler: &mut TestHandler,
        notifier: &mut impl ChainStatusNotifier<MockHeader>,
        branch: &Vec<MockHeader>,
    ) {
        for expected_header in branch {
            match notifier.next().await {
                Ok(BlockImported(header)) => {
                    assert_eq!(
                        &header, expected_header,
                        "should import header from the provided branch"
                    );
                    handler.block_imported(header).expect("should work");
                }
                _ => panic!("should import header from the provided branch"),
            }
        }
    }

    async fn consume_branch_finalized_notifications(
        notifier: &mut impl ChainStatusNotifier<MockHeader>,
        branch: &Vec<MockHeader>,
    ) {
        for expected_header in branch {
            match notifier.next().await {
                Ok(BlockFinalized(header)) => {
                    assert_eq!(
                        &header, expected_header,
                        "should finalize header from the provided branch"
                    );
                }
                _ => panic!("should finalize header from the provided branch"),
            }
        }
    }

    #[tokio::test]
    async fn accepts_response_twice() {
        let (mut handler, _backend, mut notifier, genesis) = setup();
        let branch = grow_light_branch(&mut handler, &genesis, 15, 4);
        let response = branch_response(
            branch.clone(),
            BranchResponseContent {
                headers: false,
                blocks: true,
                justifications: true,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(response.clone(), 7);
        assert!(new_info);
        assert!(maybe_error.is_none());
        mark_branch_imported(&mut handler, &mut notifier, &branch).await;
        let (new_info, maybe_error) = handler.handle_request_response(response, 8);
        assert!(!new_info);
        assert!(maybe_error.is_none());
    }

    #[tokio::test]
    async fn accepts_long_response_after_handling_short_one() {
        let (mut handler, _backend, mut notifier, genesis) = setup();
        let branch = grow_light_branch(&mut handler, &genesis, 35, 4);

        let short_response = branch_response(
            branch[..15].to_vec(),
            BranchResponseContent {
                headers: false,
                blocks: true,
                justifications: false,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(short_response, 2);
        assert!(!new_info);
        assert!(maybe_error.is_none());
        mark_branch_imported(&mut handler, &mut notifier, &branch[..15].to_vec()).await;

        let mid_response = branch_response(
            branch.to_vec(),
            BranchResponseContent {
                headers: false,
                blocks: true,
                justifications: false,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(mid_response, 3);
        assert!(!new_info);
        assert!(maybe_error.is_none());
        mark_branch_imported(&mut handler, &mut notifier, &branch[15..].to_vec()).await;
    }

    #[tokio::test]
    async fn handles_multiple_overlapping_responses() {
        let (mut handler, _backend, mut notifier, genesis) = setup();
        let branch = grow_light_branch(&mut handler, &genesis, 35, 4);

        // 15 blocks and justifications -> top is 15, new highest justification
        let short_response = branch_response(
            branch[..15].to_vec(),
            BranchResponseContent {
                headers: false,
                blocks: true,
                justifications: true,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(short_response, 2);
        assert!(new_info);
        assert!(maybe_error.is_none());
        mark_branch_imported(&mut handler, &mut notifier, &branch[..15].to_vec()).await;
        consume_branch_finalized_notifications(&mut notifier, &branch[..15].to_vec()).await;

        // 25 blocks -> top is 15, highest block is 25
        let mid_response = branch_response(
            branch[..25].to_vec(),
            BranchResponseContent {
                headers: false,
                blocks: true,
                justifications: false,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(mid_response, 3);
        assert!(!new_info);
        assert!(maybe_error.is_none());
        mark_branch_imported(&mut handler, &mut notifier, &branch[15..25].to_vec()).await;

        // 35 blocks -> top is 15, highest block is 35
        let long_response_blocks_only = branch_response(
            branch.clone(),
            BranchResponseContent {
                headers: false,
                blocks: true,
                justifications: false,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(long_response_blocks_only, 2);
        assert!(!new_info);
        assert!(maybe_error.is_none());
        mark_branch_imported(&mut handler, &mut notifier, &branch[25..].to_vec()).await;

        // 35 blocks, justifications, and headers (just for fun) ->
        // top is 35, new highest justification
        let full_response = branch_response(
            branch.clone(),
            BranchResponseContent {
                headers: true,
                blocks: true,
                justifications: true,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(full_response.clone(), 2);
        assert!(new_info);
        assert!(maybe_error.is_none());
        consume_branch_finalized_notifications(&mut notifier, &branch[15..].to_vec()).await;
    }

    #[tokio::test]
    async fn handles_response_with_incorrect_headers() {
        let (mut handler, _backend, _notifier, genesis) = setup();
        let branch = grow_light_branch(&mut handler, &genesis, 15, 4);
        let mut response = branch_response(
            branch,
            BranchResponseContent {
                headers: true,
                blocks: true,
                justifications: true,
            },
        );
        for item in response.iter_mut() {
            if let ResponseItem::Header(header) = item {
                header.invalidate();
            }
        }
        let (_, maybe_error) = handler.handle_request_response(response, 7);
        match maybe_error {
            Some(Error::Verifier(_)) => (),
            e => panic!("should return Verifier error, {e:?}"),
        };
    }

    #[tokio::test]
    async fn finalizes_with_justification_gaps() {
        let (mut handler, _backend, mut notifier, genesis) = setup();
        let mut bottom = genesis;
        let peer_id = 0;
        for session in 0.. {
            let top = SESSION_BOUNDARY_INFO.last_block_of_session(SessionId(session));
            let branch = grow_light_branch_till(&mut handler, &bottom, &top, peer_id);
            bottom = branch.last().expect("should not be empty").id();
            // import blocks
            let response_items = branch_response(
                branch.clone(),
                BranchResponseContent {
                    headers: true,
                    blocks: true,
                    justifications: false,
                },
            );
            let (new_info, maybe_error) = handler.handle_request_response(response_items, peer_id);
            assert!(!new_info, "should not import justification");
            assert!(maybe_error.is_none(), "should work");
            mark_branch_imported(&mut handler, &mut notifier, &branch).await;
            // increasingly larger gaps
            let partial = branch.clone()[session as usize + 1..].to_vec();
            if partial.is_empty() {
                break;
            }
            let response_items = branch_response(
                partial.clone(),
                BranchResponseContent {
                    headers: false,
                    blocks: false,
                    justifications: true,
                },
            );
            let (new_info, maybe_error) = handler.handle_request_response(response_items, peer_id);
            assert!(new_info);
            assert!(maybe_error.is_none(), "should work");
            // get notification about finalized end-of-session block
            match notifier.next().await {
                Ok(BlockFinalized(header)) => {
                    assert_eq!(header.id().number(), top, "should finalize the top block")
                }
                _ => panic!("should notify about finalized block"),
            };
        }
    }

    #[tokio::test]
    async fn skips_justification_gap_with_last_of_current_session_only() {
        let (mut handler, _backend, mut notifier, genesis) = setup();
        let last_block_of_first_session = SESSION_BOUNDARY_INFO.last_block_of_session(SessionId(0));
        let last_block_of_second_session =
            SESSION_BOUNDARY_INFO.last_block_of_session(SessionId(1));
        let peer_id = 0;
        let branch_low = grow_light_branch_till(
            &mut handler,
            &genesis,
            &last_block_of_first_session,
            peer_id,
        );
        let finalizing_justification =
            MockJustification::for_header(branch_low.last().expect("should not be empty").clone());
        let branch_high = grow_light_branch_till(
            &mut handler,
            &finalizing_justification.header().id(),
            &last_block_of_second_session,
            peer_id,
        );
        let response_items = branch_response(
            branch_low
                .iter()
                .cloned()
                .chain(branch_high.iter().cloned())
                .collect(),
            BranchResponseContent {
                headers: true,
                blocks: true,
                justifications: false,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(response_items, peer_id);
        assert!(!new_info, "should not import justification");
        assert!(maybe_error.is_none(), "should work");

        mark_branch_imported(&mut handler, &mut notifier, &branch_low).await;
        mark_branch_imported(&mut handler, &mut notifier, &branch_high).await;

        let all_but_two = branch_response(
            branch_low
                .iter()
                .rev()
                .skip(1)
                .rev()
                .skip(1)
                .cloned()
                .chain(branch_high.iter().cloned())
                .collect(),
            BranchResponseContent {
                headers: false,
                blocks: false,
                justifications: true,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(all_but_two, peer_id);
        let highest = branch_high.last().expect("should not be empty").id();
        assert!(new_info, "should import justifications");
        assert!(maybe_error.is_none(), "should work");

        assert_eq!(
            handler
                .state()
                .expect("should work")
                .top_justification()
                .header()
                .id(),
            genesis,
            "should not finalize anything yet"
        );
        handler
            .handle_justification_from_user(finalizing_justification)
            .expect("should work");
        assert_eq!(
            handler
                .state()
                .expect("should work")
                .top_justification()
                .header()
                .id(),
            highest,
            "should finalize everything"
        );
    }

    #[test]
    fn creates_dangling_branch() {
        let (mut handler, _backend, _notifier, _genesis) = setup();
        let peer_id = 0;
        let (bottom, top) = create_dangling_branch(&mut handler, 25, 10, peer_id);
        assert_dangling_branch_required(&handler, &top, &bottom, HashSet::from_iter(vec![peer_id]));
    }

    #[tokio::test]
    async fn uninterested_in_dangling_branch() {
        let (mut handler, _backend, mut notifier, genesis) = setup();

        // grow the branch that will be finalized
        let branch = grow_light_branch(&mut handler, &genesis, 15, 4);

        // grow the dangling branch that will be pruned
        let peer_id = 3;
        let (bottom, top) = create_dangling_branch(&mut handler, 10, 20, peer_id);
        assert_dangling_branch_required(&handler, &top, &bottom, HashSet::from_iter(vec![peer_id]));

        // begin finalizing the main branch
        let response = branch_response(
            branch,
            BranchResponseContent {
                headers: false,
                blocks: true,
                justifications: true,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(response, 7);
        assert!(new_info, "should create new highest justified");
        assert!(maybe_error.is_none(), "should work");

        // check that still not finalized
        assert!(
            handler.interest_provider().get(&top) != Interest::Uninterested,
            "should still be interested"
        );

        // finalize
        while let Ok(BlockImported(header)) = notifier.next().await {
            handler.block_imported(header).expect("should work");
        }

        // check if dangling branch was pruned
        assert_eq!(
            handler.interest_provider().get(&top),
            Interest::Uninterested,
            "should be uninterested"
        );
    }

    #[tokio::test]
    async fn uninterested_in_dangling_branch_when_connected_below_finalized() {
        let (mut handler, _backend, mut notifier, genesis) = setup();

        // grow the branch that will be finalized
        let branch = grow_light_branch(&mut handler, &genesis, 15, 4);

        // grow the dangling branch that will be pruned
        let fork_peer = 6;
        let fork_bottom = BlockId::new_random(15);
        let fork_child = fork_bottom.random_child();
        let fork = grow_light_branch(&mut handler, &fork_child.id(), 10, fork_peer);
        let fork_top = fork.last().expect("fork not empty").id();

        // finalize the main branch
        let response = branch_response(
            branch.clone(),
            BranchResponseContent {
                headers: false,
                blocks: true,
                justifications: true,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(response, 7);
        assert!(new_info, "should create new highest justified");
        assert!(maybe_error.is_none(), "should work");
        let mut idx = 0;
        while let Ok(BlockImported(header)) = notifier.next().await {
            assert_eq!(
                header, branch[idx],
                "should be importing the main branch in order"
            );
            handler.block_imported(header).expect("should work");
            idx += 1;
        }

        // check that the fork is still interesting
        assert_eq!(
            handler.interest_provider().get(&fork_top),
            Interest::Required {
                know_most: HashSet::from_iter(vec![fork_peer]),
                branch_knowledge: LowestId(fork_child.id()),
            },
            "should be required"
        );

        // import fork_child that connects the fork to fork_bottom,
        // which is at the same height as an already finalized block
        let response = branch_response(
            vec![fork_child],
            BranchResponseContent {
                headers: true,
                blocks: false,
                justifications: false,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(response, 12);
        assert!(!new_info, "should not create new highest justified");
        match maybe_error {
            None => panic!("should fail when it reaches the top finalized"),
            Some(_) => (),
        }

        // check that the fork is pruned
        assert_eq!(
            handler.interest_provider().get(&fork_top),
            Interest::Uninterested,
            "should be uninterested"
        );
    }

    #[tokio::test]
    async fn uninterested_in_dangling_branch_when_connected_to_composted_header() {
        let (mut handler, _backend, mut notifier, genesis) = setup();

        // grow the branch that will be finalized
        let branch = grow_light_branch(&mut handler, &genesis, 15, 4);

        // grow the branch that will be pruned in the first place
        let fork_bottom = branch[10].id();
        let fork = grow_light_branch(&mut handler, &fork_bottom, 15, 5);

        // grow the dangling branch that will be pruned
        let fork_peer = 6;
        let further_fork_bottom = fork.last().expect("branch not empty").id();
        let further_fork_child = further_fork_bottom.random_child();
        let further_fork = grow_light_branch(&mut handler, &further_fork_child.id(), 10, fork_peer);
        let fork_top = further_fork.last().expect("fork not empty").id();

        // finalize the main branch
        let response = branch_response(
            branch.clone(),
            BranchResponseContent {
                headers: false,
                blocks: true,
                justifications: true,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(response, 7);
        assert!(new_info, "should create new highest justified");
        assert!(maybe_error.is_none(), "should work");
        let mut idx = 0;
        while let Ok(BlockImported(header)) = notifier.next().await {
            assert_eq!(
                header, branch[idx],
                "should be importing the main branch in order"
            );
            handler.block_imported(header).expect("should work");
            idx += 1;
        }

        // check if the bottom part of the fork was pruned
        assert_eq!(
            handler.interest_provider().get(&further_fork_bottom),
            Interest::Uninterested,
            "should be uninterested"
        );

        // check that the fork is still interesting
        assert_eq!(
            handler.interest_provider().get(&fork_top),
            Interest::Required {
                know_most: HashSet::from_iter(vec![fork_peer]),
                branch_knowledge: LowestId(further_fork_child.id()),
            },
            "should be required"
        );

        // check that further_fork_child is higher than top finalized
        assert!(
            further_fork_child.id().number()
                > handler
                    .state()
                    .expect("should work")
                    .top_justification()
                    .header()
                    .id()
                    .number()
        );

        // import further_fork_child that connects the fork to further_fork_bottom,
        // which is composted
        let response = branch_response(
            vec![further_fork_child],
            BranchResponseContent {
                headers: true,
                blocks: false,
                justifications: false,
            },
        );
        let (new_info, maybe_error) = handler.handle_request_response(response, 12);
        assert!(!new_info, "should not create new highest justified");
        match maybe_error {
            None => panic!("should fail when it reaches the top finalized"),
            Some(_) => (),
        }

        // check that the fork is pruned
        assert_eq!(
            handler.interest_provider().get(&fork_top),
            Interest::Uninterested,
            "should be uninterested"
        );
    }

    #[tokio::test]
    async fn syncs_to_a_long_trunk() {
        let (mut handler, mut backend, mut notifier, _genesis) = setup();
        let (mut syncing_handler, _syncing_backend, mut syncing_notifier, genesis) = setup();
        let _top_main = grow_trunk(&mut handler, &mut backend, &mut notifier, &genesis, 2345).await;
        let peer_id = 0;
        let syncing_peer_id = 1;
        loop {
            // syncing peer broadcasts the state
            let state = syncing_handler.state().expect("should work");

            // peer responds
            let response = match handler
                .handle_state(state, syncing_peer_id)
                .expect("should create response")
            {
                Response(data) => data,
                ExtendChain => panic!("should not request anything from the syncing peer"),
                Noop => break,
            };
            let (justification, maybe_justification) = match response {
                NetworkData::StateBroadcastResponse(justification, maybe_justification) => {
                    (justification, maybe_justification)
                }
                _ => panic!("should create state broadcast response"),
            };

            // syncing peer processes the response and sends a request
            let mut target_id = justification.header().id();
            if let Some(justification) = &maybe_justification {
                target_id = justification.header().id();
            }
            let (new_info, maybe_error) =
                syncing_handler.handle_state_response(justification, maybe_justification, peer_id);
            assert!(maybe_error.is_none(), "should work");
            assert!(new_info, "should want to request");
            let branch_knowledge = match syncing_handler.extension_request() {
                ExtensionRequest::HighestJustified {
                    id,
                    branch_knowledge,
                    ..
                } => {
                    assert_eq!(id, target_id, "should want to request target_id");
                    branch_knowledge
                }
                _ => panic!("should want to extend"),
            };
            let state = syncing_handler.state().expect("should work");
            let request = Request::new(target_id.clone(), branch_knowledge, state);

            // peer responds
            let response_items = match handler.handle_request(request).expect("should work") {
                Action::Response(items) => items,
                _ => panic!("should prepare response"),
            };

            // syncing peer processes the response
            let (new_info, maybe_error) =
                syncing_handler.handle_request_response(response_items.clone(), peer_id);
            assert!(maybe_error.is_none(), "should work");
            assert!(!new_info, "should already know about target_id");

            // syncing peer finalizes received blocks
            let response_headers: Vec<_> = response_items
                .into_iter()
                .filter_map(|item| {
                    if let ResponseItem::Block(block) = item {
                        Some(block.header().clone())
                    } else {
                        None
                    }
                })
                .collect();
            let mut idx = 0;
            while let Ok(notification) = syncing_notifier.next().await {
                match notification {
                    BlockImported(header) => {
                        assert_eq!(header, response_headers[idx], "should import in order");
                        syncing_handler.block_imported(header).expect("should work");
                        idx += 1;
                    }
                    BlockFinalized(header) if header.id() == target_id => break,
                    _ => (),
                }
            }
        }
    }

    #[test]
    fn finalizes_imported_and_justified() {
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let header = import_branch(&mut backend, 1)[0].clone();
        handler
            .block_imported(header.clone())
            .expect("importing in order");
        let justification = MockJustification::for_header(header);
        let peer = rand::random();
        assert!(handler
            .handle_justification(justification.clone().into_unverified(), Some(peer))
            .expect("correct justification"));
        assert_eq!(
            backend.top_finalized().expect("mock backend works"),
            justification
        );
    }

    #[test]
    fn requests_missing_justifications_without_blocks() {
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let peer = rand::random();
        // skip the first justification, now every next added justification
        // should make us want to request a chain extension
        for justification in import_branch(&mut backend, 5)
            .into_iter()
            .map(MockJustification::for_header)
            .skip(1)
        {
            assert!(handler
                .handle_justification(justification.clone().into_unverified(), Some(peer))
                .expect("correct justification"));
        }
    }

    #[test]
    fn requests_missing_justifications_with_blocks() {
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let peer = rand::random();
        let justifications: Vec<MockJustification> = import_branch(&mut backend, 5)
            .into_iter()
            .map(MockJustification::for_header)
            .collect();
        for justification in justifications.iter() {
            handler
                .block_imported(justification.header().clone())
                .expect("importing in order");
        }
        // skip the first justification, now every next added justification
        // should spawn a new task
        for justification in justifications.into_iter().skip(1) {
            assert!(handler
                .handle_justification(justification.clone().into_unverified(), Some(peer))
                .expect("correct justification"));
        }
    }

    #[test]
    fn initializes_forest_properly() {
        let (mut backend, _keep) = Backend::setup(SESSION_BOUNDARY_INFO);
        let header = import_branch(&mut backend, 1)[0].clone();
        // header already imported, Handler should initialize Forest properly
        let verifier = backend.clone();
        let database_io = DatabaseIO::new(backend.clone(), backend.clone(), backend.clone());
        let mut handler = Handler::new(
            database_io,
            verifier,
            SyncOracle::new(),
            SessionBoundaryInfo::new(SessionPeriod(20)),
        )
        .expect("mock backend works");
        let justification = MockJustification::for_header(header);
        let peer: MockPeerId = rand::random();
        assert!(handler
            .handle_justification(justification.clone().into_unverified(), Some(peer))
            .expect("correct justification"));
        // should be auto-finalized, if Forest knows about imported body
        assert_eq!(
            backend.top_finalized().expect("mock backend works"),
            justification
        );
    }

    #[test]
    fn finalizes_justified_and_imported() {
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let header = import_branch(&mut backend, 1)[0].clone();
        let justification = MockJustification::for_header(header.clone());
        let peer = rand::random();
        assert!(handler
            .handle_justification(justification.clone().into_unverified(), Some(peer))
            .expect("correct justification"));
        handler.block_imported(header).expect("importing in order");
        assert_eq!(
            backend.top_finalized().expect("mock backend works"),
            justification
        );
    }

    #[test]
    fn handles_state_with_large_difference() {
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let initial_state = handler.state().expect("state works");
        let peer = rand::random();
        let justifications: Vec<MockJustification> = import_branch(&mut backend, 43)
            .into_iter()
            .map(MockJustification::for_header)
            .collect();
        let last_from_first_session = justifications[18].clone().into_unverified();
        let last_from_second_session = justifications[38].clone().into_unverified();
        for justification in justifications.into_iter() {
            handler
                .block_imported(justification.header().clone())
                .expect("importing in order");
            handler
                .handle_justification(justification.clone().into_unverified(), Some(peer))
                .expect("correct justification");
        }
        match handler
            .handle_state(initial_state, peer)
            .expect("correct justification")
        {
            HandleStateAction::Response(NetworkData::StateBroadcastResponse(
                justification,
                maybe_justification,
            )) => {
                assert_eq!(justification, last_from_first_session);
                assert_eq!(maybe_justification, Some(last_from_second_session));
            }
            other_action => panic!("expected a response with justifications, got {other_action:?}"),
        }
    }

    #[test]
    fn handles_state_with_medium_difference() {
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let initial_state = handler.state().expect("state works");
        let peer = rand::random();
        let justifications: Vec<MockJustification> = import_branch(&mut backend, 23)
            .into_iter()
            .map(MockJustification::for_header)
            .collect();
        let last_from_first_session = justifications[18].clone().into_unverified();
        let top = justifications[22].clone().into_unverified();
        for justification in justifications.into_iter() {
            handler
                .block_imported(justification.header().clone())
                .expect("importing in order");
            handler
                .handle_justification(justification.clone().into_unverified(), Some(peer))
                .expect("correct justification");
        }
        match handler
            .handle_state(initial_state, peer)
            .expect("correct justification")
        {
            HandleStateAction::Response(NetworkData::StateBroadcastResponse(
                justification,
                maybe_justification,
            )) => {
                assert_eq!(justification, last_from_first_session);
                assert_eq!(maybe_justification, Some(top));
            }
            other_action => panic!("expected a response with justifications, got {other_action:?}"),
        }
    }

    #[test]
    fn handles_state_with_small_difference() {
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let initial_state = handler.state().expect("state works");
        let peer = rand::random();
        let justifications: Vec<MockJustification> = import_branch(&mut backend, 13)
            .into_iter()
            .map(MockJustification::for_header)
            .collect();
        let top = justifications[12].clone().into_unverified();
        for justification in justifications.into_iter() {
            handler
                .block_imported(justification.header().clone())
                .expect("importing in order");
            handler
                .handle_justification(justification.clone().into_unverified(), Some(peer))
                .expect("correct justification");
        }
        match handler
            .handle_state(initial_state, peer)
            .expect("correct justification")
        {
            HandleStateAction::Response(NetworkData::StateBroadcastResponse(
                justification,
                maybe_justification,
            )) => {
                assert_eq!(justification, top);
                assert!(maybe_justification.is_none());
            }
            other_action => panic!("expected a response with justifications, got {other_action:?}"),
        }
    }

    #[test]
    fn handles_state_with_incorrect_headers() {
        let (mut handler, backend, _keep, genesis) = setup();
        let peer = rand::random();
        let mut header = genesis.random_child();
        header.invalidate();
        let state = State::new(
            MockJustification::for_header(
                backend.top_finalized().expect("genesis").header().clone(),
            ),
            header,
        );
        match handler.handle_state(state, peer) {
            Err(Error::Verifier(_)) => (),
            e => panic!("should return Verifier error, {e:?}"),
        };
        let mut header = MockHeader::random_parentless(1000).random_child();
        header.invalidate();
        let state = State::new(MockJustification::for_header(header.clone()), header);
        match handler.handle_state(state, peer) {
            Err(Error::Verifier(_)) => (),
            e => panic!("should return Verifier error, {e:?}"),
        };
    }

    fn setup_request_tests(
        handler: &mut TestHandler,
        backend: &mut Backend,
        branch_length: usize,
        finalize_up_to: usize,
    ) -> (Vec<MockJustification>, Vec<MockBlock>) {
        let peer = rand::random();
        let headers = import_branch(backend, branch_length);
        let mut justifications: Vec<_> = headers
            .clone()
            .into_iter()
            .take(finalize_up_to - 1) // 0 is already imported
            .map(MockJustification::for_header)
            .collect();
        for justification in &justifications {
            let number = justification.header().id().number();
            handler
                .block_imported(justification.header().clone())
                .expect("importing in order");
            // skip some justifications, but always keep the last of the session
            // justifications right before the last will be skipped in response
            if number % 20 < 10 || number % 20 > 14 {
                handler
                    .handle_justification(justification.clone().into_unverified(), Some(peer))
                    .expect("correct justification");
            }
        }

        for header in headers.clone().into_iter().skip(finalize_up_to - 1) {
            handler.block_imported(header).expect("importing in order");
        }

        let blocks = headers
            .into_iter()
            .map(|h| backend.block(h.id()).unwrap().unwrap())
            .collect();

        // filter justifications, these are supposed to be included in the response
        justifications.retain(|j| {
            let number = j.header().id().number();
            number % 20 < 10 || number % 20 == 19
        });

        (justifications, blocks)
    }

    #[test]
    fn handles_request_too_far_into_future() {
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let initial_state = handler.state().expect("state works");

        let (justifications, _) = setup_request_tests(&mut handler, &mut backend, 100, 100);

        let requested_id = justifications.last().unwrap().header().id();
        let request = Request::new(requested_id.clone(), LowestId(requested_id), initial_state);

        match handler.handle_request(request).expect("correct request") {
            Action::Noop => {}
            other_action => panic!("expected a response with justifications, got {other_action:?}"),
        }
    }

    #[derive(Debug, Eq, PartialEq)]
    enum SimplifiedItem {
        J(BlockNumber),
        B(BlockNumber),
        H(BlockNumber),
    }

    impl SimplifiedItem {
        pub fn from_response_items(response_items: MockResponseItems) -> Vec<SimplifiedItem> {
            response_items
                .into_iter()
                .map(|it| match it {
                    ResponseItem::Justification(j) => Self::J(j.header().id().number()),
                    ResponseItem::Header(h) => Self::H(h.id().number()),
                    ResponseItem::Block(b) => Self::B(b.id().number()),
                })
                .collect()
        }
    }

    #[test]
    fn handles_request_with_lowest_id() {
        use SimplifiedItem::*;
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let initial_state = handler.state().expect("state works");

        let (_, blocks) = setup_request_tests(&mut handler, &mut backend, 100, 20);

        let requested_id = blocks[30].clone().id();
        let lowest_id = blocks[25].clone().id();

        // request block #31, with the last known header equal to block #26
        let request = Request::new(requested_id, LowestId(lowest_id), initial_state);

        let expected_response_items = vec![
            J(1),
            B(1),
            J(2),
            B(2),
            J(3),
            B(3),
            J(4),
            B(4),
            J(5),
            B(5),
            J(6),
            B(6),
            J(7),
            B(7),
            J(8),
            B(8),
            J(9),
            B(9),
            J(19),
            H(18),
            H(17),
            H(16),
            H(15),
            H(14),
            H(13),
            H(12),
            H(11),
            H(10),
            B(10),
            B(11),
            B(12),
            B(13),
            B(14),
            B(15),
            B(16),
            B(17),
            B(18),
            B(19),
            H(26),
            H(25),
            H(24),
            H(23),
            H(22),
            H(21),
            H(20),
            B(20),
            B(21),
            B(22),
            B(23),
            B(24),
            B(25),
            B(26),
            B(27),
            B(28),
            B(29),
            B(30),
            B(31),
        ];
        match handler.handle_request(request).expect("correct request") {
            Action::Response(response_items) => {
                assert_eq!(
                    SimplifiedItem::from_response_items(response_items),
                    expected_response_items
                )
            }
            other_action => panic!("expected a response with justifications, got {other_action:?}"),
        }
    }

    #[test]
    fn handles_request_with_unknown_id() {
        let (mut handler, mut backend, _keep, _genesis) = setup();
        setup_request_tests(&mut handler, &mut backend, 100, 20);

        let header = MockHeader::random_parentless(105);
        let state = State::new(MockJustification::for_header(header.clone()), header);
        let requested_id = BlockId::new_random(120);
        let lowest_id = BlockId::new_random(110);

        let request = Request::new(requested_id.clone(), LowestId(lowest_id), state);

        match handler.handle_request(request).expect("correct request") {
            Action::RequestBlock(id) => assert_eq!(id, requested_id),
            other_action => panic!("expected a response with justifications, got {other_action:?}"),
        }
    }

    #[test]
    fn handles_request_with_top_imported() {
        use SimplifiedItem::*;
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let initial_state = handler.state().expect("state works");

        let (_, blocks) = setup_request_tests(&mut handler, &mut backend, 100, 20);

        let requested_id = blocks[30].clone().id();
        let top_imported = blocks[25].clone().id();

        // request block #31, with the top imported block equal to block #26
        let request = Request::new(requested_id, TopImported(top_imported), initial_state);

        let expected_response_items = vec![
            J(1),
            J(2),
            J(3),
            J(4),
            J(5),
            J(6),
            J(7),
            J(8),
            J(9),
            J(19),
            B(27),
            B(28),
            B(29),
            B(30),
            B(31),
        ];

        match handler.handle_request(request).expect("correct request") {
            Action::Response(response_items) => {
                assert_eq!(
                    SimplifiedItem::from_response_items(response_items),
                    expected_response_items
                )
            }
            other_action => panic!("expected a response with justifications, got {other_action:?}"),
        }
    }

    #[test]
    fn handles_chain_extension_request_for_just_blocks() {
        use SimplifiedItem::*;
        let (mut handler, mut backend, _keep, _genesis) = setup();

        let (justifications, blocks) = setup_request_tests(&mut handler, &mut backend, 30, 20);

        let remote_favourite_block = blocks[24].header().clone();
        // The justification hole means there is only 10 of 'em.
        let remote_top_justification = justifications[9].clone().into_unverified();
        let remote_state = State::new(remote_top_justification, remote_favourite_block);

        let expected_response_items = vec![B(26), B(27), B(28), B(29), B(30)];

        match handler
            .handle_chain_extension_request(remote_state)
            .expect("correct request")
        {
            Action::Response(response_items) => {
                assert_eq!(
                    SimplifiedItem::from_response_items(response_items),
                    expected_response_items
                )
            }
            other_action => panic!("expected a response, got {other_action:?}"),
        }
    }

    #[test]
    fn handles_chain_extension_request_with_justifications() {
        use SimplifiedItem::*;
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let remote_state = handler.state().expect("state should work");

        setup_request_tests(&mut handler, &mut backend, 30, 20);

        let expected_response_items = vec![
            J(1),
            B(1),
            J(2),
            B(2),
            J(3),
            B(3),
            J(4),
            B(4),
            J(5),
            B(5),
            J(6),
            B(6),
            J(7),
            B(7),
            J(8),
            B(8),
            J(9),
            B(9),
            J(19),
            B(10),
            B(11),
            B(12),
            B(13),
            B(14),
            B(15),
            B(16),
            B(17),
            B(18),
            B(19),
            B(20),
            B(21),
            B(22),
            B(23),
            B(24),
            B(25),
            B(26),
            B(27),
            B(28),
            B(29),
            B(30),
        ];

        match handler
            .handle_chain_extension_request(remote_state)
            .expect("correct request")
        {
            Action::Response(response_items) => {
                assert_eq!(
                    SimplifiedItem::from_response_items(response_items),
                    expected_response_items
                )
            }
            other_action => panic!("expected a response, got {other_action:?}"),
        }
    }

    #[test]
    fn handles_forked_chain_extension_request() {
        use SimplifiedItem::*;
        let (mut handler, mut backend, _keep, _genesis) = setup();

        let (justifications, _) = setup_request_tests(&mut handler, &mut backend, 30, 20);

        let remote_favourite_block = backend
            .top_finalized()
            .expect("backend works")
            .header()
            .random_branch()
            .nth(7)
            .expect("it's infinite");
        let remote_top_justification = justifications[0].clone().into_unverified();
        let remote_state = State::new(remote_top_justification, remote_favourite_block);

        let expected_response_items = vec![
            J(2),
            B(2),
            J(3),
            B(3),
            J(4),
            B(4),
            J(5),
            B(5),
            J(6),
            B(6),
            J(7),
            B(7),
            J(8),
            B(8),
            J(9),
            B(9),
            J(19),
            B(10),
            B(11),
            B(12),
            B(13),
            B(14),
            B(15),
            B(16),
            B(17),
            B(18),
            B(19),
            B(20),
            B(21),
            B(22),
            B(23),
            B(24),
            B(25),
            B(26),
            B(27),
            B(28),
            B(29),
            B(30),
        ];

        match handler
            .handle_chain_extension_request(remote_state)
            .expect("correct request")
        {
            Action::Response(response_items) => {
                assert_eq!(
                    SimplifiedItem::from_response_items(response_items),
                    expected_response_items
                )
            }
            other_action => panic!("expected a response, got {other_action:?}"),
        }
    }

    #[test]
    fn handles_ancient_chain_extension_request() {
        use SimplifiedItem::*;
        let (mut handler, mut backend, _keep, _genesis) = setup();

        let remote_state = handler.state().expect("state should work");

        setup_request_tests(&mut handler, &mut backend, 60, 40);

        let expected_response_items = vec![
            J(1),
            B(1),
            J(2),
            B(2),
            J(3),
            B(3),
            J(4),
            B(4),
            J(5),
            B(5),
            J(6),
            B(6),
            J(7),
            B(7),
            J(8),
            B(8),
            J(9),
            B(9),
            J(19),
            B(10),
            B(11),
            B(12),
            B(13),
            B(14),
            B(15),
            B(16),
            B(17),
            B(18),
            B(19),
            J(20),
            B(20),
            J(21),
            B(21),
            J(22),
            B(22),
            J(23),
            B(23),
            J(24),
            B(24),
            J(25),
            B(25),
            J(26),
            B(26),
            J(27),
            B(27),
            J(28),
            B(28),
            J(29),
            B(29),
            J(39),
            B(30),
            B(31),
            B(32),
            B(33),
            B(34),
            B(35),
            B(36),
            B(37),
            B(38),
            B(39),
        ];

        match handler
            .handle_chain_extension_request(remote_state)
            .expect("correct request")
        {
            Action::Response(response_items) => {
                assert_eq!(
                    SimplifiedItem::from_response_items(response_items),
                    expected_response_items
                )
            }
            other_action => panic!("expected a response, got {other_action:?}"),
        }
    }

    #[test]
    fn handles_new_internal_request() {
        let (mut handler, mut backend, _keep, _genesis) = setup();
        let _ = handler.state().expect("state works");
        let headers = import_branch(&mut backend, 2);

        assert!(handler.handle_internal_request(&headers[1].id()).unwrap());
        assert!(!handler.handle_internal_request(&headers[1].id()).unwrap());
    }

    #[test]
    fn broadcasts_own_block() {
        let (mut handler, backend, _keep, _genesis) = setup();
        let block = MockBlock::new(
            backend
                .top_finalized()
                .expect("mock backend works")
                .header()
                .random_branch()
                .next()
                .expect("branch creation succeeds"),
            true,
        );

        let result = handler.handle_own_block(block.clone());
        match result.first().expect("the header is there") {
            ResponseItem::Header(header) => assert_eq!(header, block.header()),
            other => panic!("expected header item, got {:?}", other),
        }
        match result.get(1).expect("the block is there") {
            ResponseItem::Block(block_item) => assert_eq!(block_item.header(), block.header()),
            other => panic!("expected block item, got {:?}", other),
        }
    }

    #[tokio::test]
    async fn accepts_broadcast_block() {
        let (mut handler, backend, mut notifier, _genesis) = setup();
        let block = MockBlock::new(
            backend
                .top_finalized()
                .expect("mock backend works")
                .header()
                .random_branch()
                .next()
                .expect("branch creation succeeds"),
            true,
        );

        let broadcast = handler.handle_own_block(block.clone());
        match handler.handle_request_response(broadcast, rand::random()) {
            (true, _) => panic!("block unexpectedly changed top finalized"),
            (false, Some(e)) => panic!("error handling block broadcast: {}", e),
            (false, None) => (),
        }
        assert_eq!(
            notifier.next().await.expect("should receive notification"),
            BlockImported(block.header().clone())
        );
    }

    //TODO(A0-2984): remove this after legacy sync is excised
    #[tokio::test]
    async fn works_with_overzealous_imports() {
        let (mut handler, mut backend, mut notifier, genesis) = setup();
        let branch: Vec<_> = genesis.random_branch().take(2137).collect();
        for header in branch.iter() {
            let block = MockBlock::new(header.clone(), true);
            backend.import_block(block);
            match notifier.next().await {
                Ok(BlockImported(header)) => {
                    // we ignore failures, as we expect some
                    let _ = handler.block_imported(header);
                }
                _ => panic!("should notify about imported block"),
            }
        }
        for header in branch.iter() {
            let justification = MockJustification::for_header(header.clone());
            handler
                .handle_justification_from_user(justification)
                .expect("should work");
            match notifier.next().await {
                Ok(BlockFinalized(finalized_header)) => assert_eq!(
                    header, &finalized_header,
                    "should finalize the current header"
                ),
                _ => panic!("should notify about finalized block"),
            }
        }
    }
}