smoldot 1.1.0

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

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

//! Warp syncing.
//!
//! Warp syncing makes it possible to very quickly reach the currently finalized block of a chain.
//! No attempt is made at verifying blocks. The algorithm assumes that the currently finalized
//! block is valid, as the chain is likely bricked if this is not the case.
//!
//! # Long range attack vulnerability
//!
//! Warp syncing is particularly vulnerable to what is called long range attacks.
//!
//! The authorities allowed to finalize blocks can generate multiple proofs of finality for
//! multiple different blocks of the same height. In other words, they can finalize more than one
//! chain at a time.
//!
//! Finalizing multiple chains at the same time is called an equivocation. Client implementations
//! detect equivocations and report them to the runtime. While this is out of scope of the client,
//! the runtime then typically provides a mechanism that punishes the validators that have
//! equivocated.
//! However, this defense mechanism is flawed in case when a long time passes between the
//! multiple finality proofs generated by the same validators. Clients cannot hold an unlimited
//! amount of information in memory, so they might not detect the equivocation, and even if it is
//! detected, the punishment might not be enforceable because validators have moved all their
//! funds.
//!
//! In other words, it is possible for two thirds of the validators that were active at a certain
//! past block N to collude and decide to finalize a different block N, even when N has been
//! finalized for the first time several weeks or months in the past. When a client then warp
//! syncs, it can be tricked to consider this alternative block N as the finalized one.
//!
//! There is no fool-proof defense against this attack. However, consider the extremely high
//! investment and high risk for the malicious validators, and the difficulty of pulling off this
//! attack, it is extremely unlikely to happen in reality.
//! The aforementioned punishment system is the only defense against this attack, and in order to
//! be effective, the starting point of the warp syncing shouldn't be too far in the past. How
//! far exactly depends on the logic of the runtime of the chain.
//!
//! # Overview
//!
//! The warp syncing algorithm works only if the chain uses Grandpa for its finality.
//! It consists in the following steps:
//!
//! - Downloading a warp sync proof from a source. This proof contains a list of *fragments*. Each
//! fragment represents a change in the list of Grandpa authorities, and a list of signatures of
//! the previous authorities that certify that this change is correct.
//! - Verifying the fragments. Each fragment that is successfully verified progresses towards
//! the head of the chain. Even if one fragment is invalid, all the previously-verified
//! fragments can still be kept, and the warp syncing can resume from there.
//! - Downloading from a source the runtime code of the final block of the proof.
//! - Performing some runtime calls in order to obtain the current consensus-related parameters
//! of the chain. This might require obtaining some storage items, in which case they must also
//! be downloaded from a source.
//!
//! At the end of the syncing, a [`ValidChainInformation`] corresponding to the head of the chain
//! is yielded.
//!
//! # Usage
//!
//! Use the [`start_warp_sync()`] function to start a Grandpa warp syncing state machine.
//!
//! At any given moment, this state machine holds a list of *sources* that it might use to
//! download the warp sync proof or the runtime code. Sources must be added and removed by the API
//! user by calling one of the various `add_source` and `remove_source` functions.
//!
//! Sources are identified through a [`SourceId`]. Each source has an opaque so-called "user data"
//! of type `TSrc` associated to it. The content of this "user data" is at the discretion of the
//! API user.
//!
//! Similarly, at any given moment, this state machine holds a list of requests that concern these
//! sources. Use [`WarpSync::desired_requests`] to determine which requests will be useful to the
//! progress of the warp syncing, then use [`WarpSync::add_request`] to update the state machine
//! with a newly-started request.
//!
//! Use [`WarpSync::process_one`] in order to run verifications of the payloads that have
//! previously been downloaded.
//!

use crate::{
    chain::chain_information::{
        self, ChainInformationConsensusRef, ChainInformationFinality, ChainInformationFinalityRef,
        ValidChainInformation, ValidChainInformationRef,
    },
    executor::{
        self,
        host::{self, HostVmPrototype},
        vm::ExecHint,
    },
    finality::{decode, verify},
    header,
    informant::HashDisplay,
    trie::{self, proof_decode},
};

use alloc::{
    borrow::{Cow, ToOwned as _},
    collections::{BTreeSet, VecDeque},
    vec,
    vec::Vec,
};
use core::{cmp, fmt, iter, mem, ops};

pub use trie::Nibble;

/// The configuration for [`start_warp_sync()`].
#[derive(Debug)]
pub struct Config {
    /// The chain information of the starting point of the warp syncing.
    pub start_chain_information: ValidChainInformation,

    /// Number of bytes used when encoding/decoding the block number. Influences how various data
    /// structures should be parsed.
    pub block_number_bytes: usize,

    /// The initial capacity of the list of sources.
    pub sources_capacity: usize,

    /// The initial capacity of the list of requests.
    pub requests_capacity: usize,

    /// Known valid Merkle value and storage value combination for the `:code` key.
    ///
    /// If provided, the warp syncing algorithm will first fetch the Merkle value of `:code`, and
    /// if it matches the Merkle value provided in the hint, use the storage value in the hint
    /// instead of downloading it. If the hint doesn't match, an extra round-trip will be needed,
    /// but if the hint matches it saves a big download.
    pub code_trie_node_hint: Option<ConfigCodeTrieNodeHint>,

    /// Number of warp sync fragments after which the state machine will pause downloading new
    /// ones until the ones that have been downloaded are verified.
    ///
    /// A too low value will cause stalls, while a high value will use more memory and runs the
    /// risk of wasting more bandwidth in case the downloaded fragments need to be thrown away.
    pub num_download_ahead_fragments: usize,

    /// If the height of the current local finalized block is `N`, the warp sync state machine
    /// will not attempt to warp sync to blocks whose height inferior or equal to `N + k` where
    /// `k` is the value in this field.
    ///
    /// Because warp syncing is a relatively expensive process, it is not worth performing it
    /// between two blocks that are too close to each other.
    ///
    /// The ideal value of this field depends on the block production rate and the time it takes
    /// to answer requests.
    pub warp_sync_minimum_gap: usize,

    /// If `true`, the body of the warp sync target will be downloaded before the warp sync
    /// finishes.
    /// Determines whether [`RuntimeInformation::finalized_body`] is `Some`.
    pub download_block_body: bool,

    /// If `true`, all the storage proofs and call proofs necessary in order to compute the chain
    /// information of the warp synced block will be downloaded. If `false`, the finality
    /// information of the warp synced block is inferred from the warp sync fragments instead.
    pub download_all_chain_information_storage_proofs: bool,
}

/// See [`Config::code_trie_node_hint`].
#[derive(Debug)]
pub struct ConfigCodeTrieNodeHint {
    /// Potential Merkle value of the `:code` key.
    pub merkle_value: Vec<u8>,

    /// Storage value corresponding to [`ConfigCodeTrieNodeHint::merkle_value`].
    pub storage_value: Vec<u8>,

    /// Closest ancestor of the `:code` key except for `:code` itself.
    pub closest_ancestor_excluding: Vec<Nibble>,
}

/// Initializes the warp sync state machine.
///
/// On error, returns the [`ValidChainInformation`] that was provided in the configuration.
pub fn start_warp_sync<TSrc, TRq>(
    config: Config,
) -> Result<WarpSync<TSrc, TRq>, (ValidChainInformation, WarpSyncInitError)> {
    match config.start_chain_information.as_ref().finality {
        // TODO: we make sure that `finalized_scheduled_change` is `None` because it seems complicated to support, but ideally it would be supported
        ChainInformationFinalityRef::Grandpa {
            finalized_scheduled_change: None,
            ..
        } => {}
        _ => {
            return Err((
                config.start_chain_information,
                WarpSyncInitError::NotGrandpa,
            ));
        }
    }

    match config.start_chain_information.as_ref().consensus {
        ChainInformationConsensusRef::Babe { .. } | ChainInformationConsensusRef::Aura { .. } => {}
        ChainInformationConsensusRef::Unknown => {
            return Err((
                config.start_chain_information,
                WarpSyncInitError::UnknownConsensus,
            ));
        }
    }

    let warped_header = config
        .start_chain_information
        .as_ref()
        .finalized_block_header
        .scale_encoding_vec(config.block_number_bytes);

    Ok(WarpSync {
        warped_header_number: config
            .start_chain_information
            .as_ref()
            .finalized_block_header
            .number,
        warped_header_state_root: *config
            .start_chain_information
            .as_ref()
            .finalized_block_header
            .state_root,
        warped_header_extrinsics_root: *config
            .start_chain_information
            .as_ref()
            .finalized_block_header
            .extrinsics_root,
        warped_header_hash: header::hash_from_scale_encoded_header(&warped_header),
        warped_header,
        warped_finality: config.start_chain_information.as_ref().finality.into(),
        warped_block_ty: WarpedBlockTy::AlreadyVerified,
        runtime_calls: runtime_calls_default_value(
            config.start_chain_information.as_ref().consensus,
        ),
        verified_chain_information: config.start_chain_information,
        code_trie_node_hint: config.code_trie_node_hint,
        num_download_ahead_fragments: config.num_download_ahead_fragments,
        warp_sync_minimum_gap: config.warp_sync_minimum_gap,
        block_number_bytes: config.block_number_bytes,
        download_all_chain_information_storage_proofs: config
            .download_all_chain_information_storage_proofs,
        sources: slab::Slab::with_capacity(config.sources_capacity),
        sources_by_finalized_height: BTreeSet::new(),
        in_progress_requests: slab::Slab::with_capacity(config.requests_capacity),
        in_progress_requests_by_source: BTreeSet::new(),
        warp_sync_fragments_download: None,
        verify_queue: VecDeque::new(),
        runtime_download: RuntimeDownload::NotStarted {
            hint_doesnt_match: false,
        },
        body_download: if config.download_block_body {
            BodyDownload::NotStarted
        } else {
            BodyDownload::NotNeeded
        },
    })
}

/// Error potentially returned by [`start_warp_sync()`].
#[derive(Debug, derive_more::Display, derive_more::Error, Clone)]
pub enum WarpSyncInitError {
    /// Chain doesn't use the Grandpa finality algorithm.
    NotGrandpa,
    /// Chain uses an unrecognized consensus mechanism.
    UnknownConsensus,
}

/// Identifier for a source in the [`WarpSync`].
//
// Implementation note: this represents the index within the `Slab` used for the list of sources.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct SourceId(usize);

impl SourceId {
    /// Smallest possible [`SourceId`]. It is always inferior or equal to any other.
    pub const MIN: Self = SourceId(usize::MIN);
}

// TODO: consider removing this entirely
pub struct RuntimeInformation {
    /// The runtime constructed in `VirtualMachineParamsGet`. Corresponds to the runtime of the
    /// finalized block of [`WarpSync::as_chain_information`].
    pub finalized_runtime: HostVmPrototype,

    /// List of SCALE-encoded extrinsics of the body of the finalized block.
    /// `Some` if and only if [`Config::download_block_body`] was `true`.
    pub finalized_body: Option<Vec<Vec<u8>>>,

    /// Storage value at the `:code` key of the finalized block.
    pub finalized_storage_code: Option<Vec<u8>>,

    /// Storage value at the `:heappages` key of the finalized block.
    pub finalized_storage_heap_pages: Option<Vec<u8>>,

    /// Merkle value of the `:code` trie node of the finalized block.
    pub finalized_storage_code_merkle_value: Option<Vec<u8>>,

    /// Closest ancestor of the `:code` trie node of the finalized block excluding `:code` itself.
    pub finalized_storage_code_closest_ancestor_excluding: Option<Vec<Nibble>>,
}

/// Fragment to be verified.
#[derive(Debug)]
pub struct WarpSyncFragment {
    /// Header of a block in the chain.
    pub scale_encoded_header: Vec<u8>,

    /// Justification that proves the finality of [`WarpSyncFragment::scale_encoded_header`].
    pub scale_encoded_justification: Vec<u8>,
}

/// Warp syncing process state machine.
pub struct WarpSync<TSrc, TRq> {
    /// SCALE-encoded header of the finalized block of the chain we warp synced to. Initially
    /// identical to the value in [`WarpSync::verified_chain_information`].
    warped_header: Vec<u8>,
    /// Hash of the block in [`WarpSync::warped_header`].
    warped_header_hash: [u8; 32],
    /// State trie root hash of the block in [`WarpSync::warped_header`].
    warped_header_state_root: [u8; 32],
    /// Extrinsics trie root hash of the block in [`WarpSync::warped_header`].
    warped_header_extrinsics_root: [u8; 32],
    /// Number of the block in [`WarpSync::warped_header`].
    warped_header_number: u64,
    /// Information about the finality of the chain at the point where we warp synced to.
    /// Initially identical to the value in [`WarpSync::verified_chain_information`].
    warped_finality: ChainInformationFinality,
    /// Information about the block described by [`WarpSync::warped_header`] and
    /// [`WarpSync::warped_finality`].
    warped_block_ty: WarpedBlockTy,
    /// See [`Config::code_trie_node_hint`].
    code_trie_node_hint: Option<ConfigCodeTrieNodeHint>,
    /// Starting point of the warp syncing as provided to [`start_warp_sync`], or latest chain
    /// information that was warp synced to.
    verified_chain_information: ValidChainInformation,
    /// See [`Config::num_download_ahead_fragments`].
    num_download_ahead_fragments: usize,
    /// See [`Config::warp_sync_minimum_gap`].
    warp_sync_minimum_gap: usize,
    /// See [`Config::block_number_bytes`].
    block_number_bytes: usize,
    /// See [`Config::download_all_chain_information_storage_proofs`].
    download_all_chain_information_storage_proofs: bool,
    /// List of requests that have been added using [`WarpSync::add_source`].
    sources: slab::Slab<Source<TSrc>>,
    /// Subset of the entries as [`WarpSync::sources`] whose [`Source::finalized_block_height`]
    /// is `Ok`. Indexed by [`Source::finalized_block_height`].
    sources_by_finalized_height: BTreeSet<(u64, SourceId)>,
    /// List of requests that have been added using [`WarpSync::add_request`].
    in_progress_requests: slab::Slab<(SourceId, TRq, RequestDetail)>,
    /// Identical to [`WarpSync::in_progress_requests`], but indexed differently.
    in_progress_requests_by_source: BTreeSet<(SourceId, RequestId)>,
    /// Request that is downloading warp sync fragments, if any has been started yet.
    warp_sync_fragments_download: Option<RequestId>,
    /// Queue of fragments that have been downloaded and need to be verified.
    verify_queue: VecDeque<PendingVerify>,
    /// State of the download of the runtime and chain information call proofs.
    runtime_download: RuntimeDownload,
    /// State of the download of the body of the block.
    body_download: BodyDownload,
    /// For each call required by the chain information builder, whether it has been downloaded yet.
    runtime_calls:
        hashbrown::HashMap<chain_information::build::RuntimeCall, CallProof, fnv::FnvBuildHasher>,
}

/// See [`WarpSync::sources`].
#[derive(Debug, Copy, Clone)]
struct Source<TSrc> {
    /// User data chosen by the API user.
    user_data: TSrc,
    /// Height of the finalized block of the source, as reported by the source.
    finalized_block_height: u64,
}

/// See [`WarpSync::warped_block_ty`].
enum WarpedBlockTy {
    /// Block is equal to the finalized block in [`WarpSync::verified_chain_information`].
    AlreadyVerified,
    /// Block is known to not be warp-syncable due to an incompatibility between smoldot and
    /// the chain.
    KnownBad,
    /// Block is expected to be warp syncable.
    Normal,
}

/// See [`WarpSync::runtime_download`].
enum RuntimeDownload {
    NotStarted {
        hint_doesnt_match: bool,
    },
    Downloading {
        hint_doesnt_match: bool,
        request_id: RequestId,
    },
    NotVerified {
        /// Source the runtime has been obtained from. `None` if the source has been removed.
        downloaded_source: Option<SourceId>,
        hint_doesnt_match: bool,
        trie_proof: Vec<u8>,
    },
    Verified {
        downloaded_runtime: DownloadedRuntime,
        chain_info_builder: chain_information::build::ChainInformationBuild,
    },
}

/// See [`WarpSync::body_download`].
enum BodyDownload {
    NotNeeded,
    NotStarted,
    Downloading {
        request_id: RequestId,
    },
    Downloaded {
        /// Source the body has been obtained from. `None` if the source has been removed.
        downloaded_source: Option<SourceId>,
        body: Vec<Vec<u8>>,
    },
}

/// See [`WarpSync::verify_queue`].
struct PendingVerify {
    /// Source the fragments have been obtained from. `None` if the source has been removed.
    downloaded_source: Option<SourceId>,
    /// `true` if the source has indicated that there is no more fragment afterwards, in other
    /// words that the last fragment corresponds to the current finalized block of the chain.
    final_set_of_fragments: bool,
    /// List of fragments to verify. Can be empty.
    fragments: Vec<WarpSyncFragment>,
    /// Number of fragments at the start of [`PendingVerify::fragments`] that have already been
    /// verified. Must always be strictly inferior to `fragments.len()`, unless the list of
    /// fragments is empty.
    next_fragment_to_verify_index: usize,
}

/// See [`RuntimeDownload::Verified`].
struct DownloadedRuntime {
    /// Storage item at the `:code` key. `None` if there is no entry at that key.
    storage_code: Option<Vec<u8>>,
    /// Storage item at the `:heappages` key. `None` if there is no entry at that key.
    storage_heap_pages: Option<Vec<u8>>,
    /// Merkle value of the `:code` trie node. `None` if there is no entry at that key.
    code_merkle_value: Option<Vec<u8>>,
    /// Closest ancestor of the `:code` key except for `:code` itself.
    closest_ancestor_excluding: Option<Vec<Nibble>>,
}

/// See [`WarpSync::runtime_calls`].
enum CallProof {
    NotStarted,
    Downloading(RequestId),
    Downloaded {
        /// Source the proof has been obtained from. `None` if the source has been removed.
        downloaded_source: Option<SourceId>,
        proof: Vec<u8>,
    },
}

/// Returns the default value for [`WarpSync::runtime_calls`].
///
/// Contains the list of calls that we anticipate the chain information builder will make. This
/// assumes that the runtime is the latest version available.
fn runtime_calls_default_value(
    verified_chain_information_consensus: chain_information::ChainInformationConsensusRef,
) -> hashbrown::HashMap<chain_information::build::RuntimeCall, CallProof, fnv::FnvBuildHasher> {
    let mut list = hashbrown::HashMap::with_capacity_and_hasher(8, Default::default());
    match verified_chain_information_consensus {
        ChainInformationConsensusRef::Aura { .. } => {
            list.insert(
                chain_information::build::RuntimeCall::AuraApiAuthorities,
                CallProof::NotStarted,
            );
            list.insert(
                chain_information::build::RuntimeCall::AuraApiSlotDuration,
                CallProof::NotStarted,
            );
        }
        ChainInformationConsensusRef::Babe { .. } => {
            list.insert(
                chain_information::build::RuntimeCall::BabeApiCurrentEpoch,
                CallProof::NotStarted,
            );
            list.insert(
                chain_information::build::RuntimeCall::BabeApiNextEpoch,
                CallProof::NotStarted,
            );
            list.insert(
                chain_information::build::RuntimeCall::BabeApiConfiguration,
                CallProof::NotStarted,
            );
        }
        ChainInformationConsensusRef::Unknown => {}
    }
    list
}

/// See [`WarpSync::status`].
#[derive(Debug)]
pub enum Status<'a, TSrc> {
    /// Warp syncing algorithm is downloading Grandpa warp sync fragments containing a finality
    /// proof.
    Fragments {
        /// Source from which the fragments are currently being downloaded, if any.
        source: Option<(SourceId, &'a TSrc)>,
        /// Hash of the highest block that is proven to be finalized.
        ///
        /// This isn't necessarily the same block as returned by
        /// [`WarpSync::as_chain_information`], as this function first has to download
        /// extra information compared to just the finalized block.
        finalized_block_hash: [u8; 32],
        /// Height of the block indicated by [`Status::ChainInformation::finalized_block_hash`].
        finalized_block_number: u64,
    },
    /// Warp syncing algorithm has reached the head of the finalized chain and is downloading and
    /// building the chain information.
    ChainInformation {
        /// Hash of the highest block that is proven to be finalized.
        ///
        /// This isn't necessarily the same block as returned by
        /// [`WarpSync::as_chain_information`], as this function first has to download
        /// extra information compared to just the finalized block.
        finalized_block_hash: [u8; 32],
        /// Height of the block indicated by [`Status::ChainInformation::finalized_block_hash`].
        finalized_block_number: u64,
    },
}

impl<TSrc, TRq> WarpSync<TSrc, TRq> {
    /// Returns the value that was initially passed in [`Config::block_number_bytes`].
    pub fn block_number_bytes(&self) -> usize {
        self.block_number_bytes
    }

    /// Returns the chain information that is considered verified.
    pub fn as_chain_information(&'_ self) -> ValidChainInformationRef<'_> {
        // Note: after verifying a warp sync fragment, we are certain that the header targeted by
        // this fragment is indeed part of the chain. However, this is not enough in order to
        // produce a full chain information struct. Such struct can only be produced after the
        // entire warp syncing has succeeded. If if it still in progress, all we can return is
        // the starting point.
        (&self.verified_chain_information).into()
    }

    /// Modifies the chain information known to be valid.
    pub fn set_chain_information(&mut self, chain_information: ValidChainInformationRef) {
        // The implementation simply resets the state machine, apart from the fragment download
        // and verification queue.
        // TODO: what if the new chain doesn't support grandpa?
        if self.warped_header_number <= chain_information.as_ref().finalized_block_header.number {
            self.warped_header = chain_information
                .as_ref()
                .finalized_block_header
                .scale_encoding_vec(self.block_number_bytes);
            self.warped_header_hash = chain_information
                .as_ref()
                .finalized_block_header
                .hash(self.block_number_bytes);
            self.warped_header_state_root =
                *chain_information.as_ref().finalized_block_header.state_root;
            self.warped_header_extrinsics_root = *chain_information
                .as_ref()
                .finalized_block_header
                .extrinsics_root;
            self.warped_header_number = chain_information.as_ref().finalized_block_header.number;
            self.warped_finality = chain_information.as_ref().finality.into();
            self.warped_block_ty = WarpedBlockTy::AlreadyVerified;

            self.verified_chain_information = chain_information.into();
            self.runtime_calls =
                runtime_calls_default_value(self.verified_chain_information.as_ref().consensus);

            self.runtime_download = RuntimeDownload::NotStarted {
                hint_doesnt_match: false,
            };

            if !matches!(self.body_download, BodyDownload::NotNeeded) {
                self.body_download = BodyDownload::NotStarted;
            }
        }
    }

    /// Returns the current status of the warp syncing.
    pub fn status(&'_ self) -> Status<'_, TSrc> {
        match &self.runtime_download {
            RuntimeDownload::NotStarted { .. } => {
                let finalized_block_hash = self.warped_header_hash;

                let source_id =
                    if let Some(warp_sync_fragments_download) = self.warp_sync_fragments_download {
                        Some(
                            self.in_progress_requests
                                .get(warp_sync_fragments_download.0)
                                .unwrap()
                                .0,
                        )
                    } else {
                        self.verify_queue.back().and_then(|f| f.downloaded_source)
                    };

                Status::Fragments {
                    source: source_id.map(|id| (id, &self.sources[id.0].user_data)),
                    finalized_block_hash,
                    finalized_block_number: self.warped_header_number,
                }
            }
            _ => Status::ChainInformation {
                finalized_block_hash: self.warped_header_hash,
                finalized_block_number: self.warped_header_number,
            },
        }
    }

    /// Returns a list of all known sources stored in the state machine.
    pub fn sources(&self) -> impl Iterator<Item = SourceId> {
        self.sources.iter().map(|(id, _)| SourceId(id))
    }

    /// Returns the number of ongoing requests that concern this source.
    ///
    /// # Panic
    ///
    /// Panics if the [`SourceId`] is invalid.
    ///
    pub fn source_num_ongoing_requests(&self, source_id: SourceId) -> usize {
        assert!(self.sources.contains(source_id.0));
        self.in_progress_requests_by_source
            .range((source_id, RequestId(usize::MIN))..=(source_id, RequestId(usize::MAX)))
            .count()
    }

    /// Add a source to the list of sources.
    ///
    /// The source has a finalized block height of 0, which should later be updated using
    /// [`WarpSync::set_source_finality_state`].
    pub fn add_source(&mut self, user_data: TSrc) -> SourceId {
        let source_id = SourceId(self.sources.insert(Source {
            user_data,
            finalized_block_height: 0,
        }));

        let _inserted = self.sources_by_finalized_height.insert((0, source_id));
        debug_assert!(_inserted);
        debug_assert!(self.sources.len() >= self.sources_by_finalized_height.len());

        source_id
    }

    /// Removes a source from the list of sources. In addition to the user data associated to this
    /// source, also returns a list of requests that were in progress concerning this source. These
    /// requests are now considered obsolete.
    ///
    /// # Panic
    ///
    /// Panics if the [`SourceId`] is invalid.
    ///
    pub fn remove_source(
        &mut self,
        to_remove: SourceId,
    ) -> (TSrc, impl Iterator<Item = (RequestId, TRq)>) {
        debug_assert!(self.sources.contains(to_remove.0));
        let removed = self.sources.remove(to_remove.0);
        let _was_in = self
            .sources_by_finalized_height
            .remove(&(removed.finalized_block_height, to_remove));
        debug_assert!(_was_in);
        debug_assert!(self.sources.len() >= self.sources_by_finalized_height.len());

        // We make sure to not leave invalid source IDs in the state of `self`.
        // TODO: O(n)
        for item in &mut self.verify_queue {
            if item.downloaded_source == Some(to_remove) {
                item.downloaded_source = None;
            }
        }
        if let RuntimeDownload::NotVerified {
            downloaded_source, ..
        } = &mut self.runtime_download
        {
            if *downloaded_source == Some(to_remove) {
                *downloaded_source = None;
            }
        }
        if let BodyDownload::Downloaded {
            downloaded_source, ..
        } = &mut self.body_download
        {
            if *downloaded_source == Some(to_remove) {
                *downloaded_source = None;
            }
        }
        for (_, call_proof) in &mut self.runtime_calls {
            if let CallProof::Downloaded {
                downloaded_source, ..
            } = call_proof
            {
                if *downloaded_source == Some(to_remove) {
                    *downloaded_source = None;
                }
            }
        }

        let obsolete_requests_indices = self
            .in_progress_requests_by_source
            .range((to_remove, RequestId(usize::MIN))..=(to_remove, RequestId(usize::MAX)))
            .map(|(_, rq_id)| rq_id.0)
            .collect::<Vec<_>>();
        let mut obsolete_requests = Vec::with_capacity(obsolete_requests_indices.len());
        for index in obsolete_requests_indices {
            let (_, user_data, _) = self.in_progress_requests.remove(index);
            self.in_progress_requests_by_source
                .remove(&(to_remove, RequestId(index)));
            if self.warp_sync_fragments_download == Some(RequestId(index)) {
                self.warp_sync_fragments_download = None;
            }
            for call in self.runtime_calls.values_mut() {
                if matches!(call, CallProof::Downloading(rq_id) if *rq_id == RequestId(index)) {
                    *call = CallProof::NotStarted;
                }
            }
            if let RuntimeDownload::Downloading {
                request_id,
                hint_doesnt_match,
            } = &mut self.runtime_download
            {
                if *request_id == RequestId(index) {
                    self.runtime_download = RuntimeDownload::NotStarted {
                        hint_doesnt_match: *hint_doesnt_match,
                    };
                }
            }
            if let BodyDownload::Downloading { request_id } = &mut self.body_download {
                if *request_id == RequestId(index) {
                    self.body_download = BodyDownload::NotStarted;
                }
            }
            obsolete_requests.push((RequestId(index), user_data));
        }

        (removed.user_data, obsolete_requests.into_iter())
    }

    /// Sets the finalized block height of the given source.
    ///
    /// # Panic
    ///
    /// Panics if `source_id` is invalid.
    ///
    pub fn set_source_finality_state(&mut self, source_id: SourceId, finalized_block_height: u64) {
        let stored_height = &mut self.sources[source_id.0].finalized_block_height;

        // Small optimization. No need to do anything more if the block doesn't actuall change.
        if *stored_height == finalized_block_height {
            return;
        }

        // Note that if the new finalized block is below the former one (which is not something
        // that is ever supposed to happen), we should in principle cancel the requests
        // targeting that source that require a specific block height. In practice, however,
        // we don't care as again this isn't supposed to ever happen. While ongoing requests
        // might fail as a result, this is handled the same way as a regular request failure.

        let _was_in = self
            .sources_by_finalized_height
            .remove(&(*stored_height, source_id));
        debug_assert!(_was_in);
        let _inserted = self
            .sources_by_finalized_height
            .insert((finalized_block_height, source_id));
        debug_assert!(_inserted);

        *stored_height = finalized_block_height;
    }

    /// Gets the finalized block height of the given source.
    ///
    /// Equal to 0 if [`WarpSync::set_source_finality_state`] hasn't been called.
    ///
    /// # Panic
    ///
    /// Panics if `source_id` is invalid.
    ///
    pub fn source_finality_state(&self, source_id: SourceId) -> u64 {
        self.sources[source_id.0].finalized_block_height
    }

    /// Returns a list of requests that should be started in order to drive the warp syncing
    /// process to completion.
    ///
    /// Once a request that matches a desired request is added through
    /// [`WarpSync::add_request`], it is no longer returned by this function.
    pub fn desired_requests(&self) -> impl Iterator<Item = (SourceId, &TSrc, DesiredRequest)> {
        // If we are in the fragments download phase, return a fragments download request.
        let mut desired_warp_sync_request = if self.warp_sync_fragments_download.is_none() {
            if self.verify_queue.iter().fold(0, |sum, entry| {
                sum + entry.fragments.len() - entry.next_fragment_to_verify_index
            }) < self.num_download_ahead_fragments
            {
                // Block hash to request.
                let start_block_hash = self
                    .verify_queue
                    .back()
                    .and_then(|entry| entry.fragments.last())
                    .map(|fragment| {
                        header::hash_from_scale_encoded_header(&fragment.scale_encoded_header)
                    })
                    .unwrap_or(self.warped_header_hash);

                // Calculate the block number at the tail of the verify queue.
                // Contains `None` if the verify queue has a problem such as an indecodable header.
                // In that situation, we don't start any new request and wait for the verify
                // queue to empty itself.
                let verify_queue_tail_block_number = self
                    .verify_queue
                    .back()
                    .map(|entry| {
                        entry
                            .fragments
                            .last()
                            .and_then(|fragment| {
                                header::decode(
                                    &fragment.scale_encoded_header,
                                    self.block_number_bytes,
                                )
                                .ok()
                            })
                            .map(|header| header.number)
                    })
                    .unwrap_or(Some(self.warped_header_number));
                let warp_sync_minimum_gap = self.warp_sync_minimum_gap;

                if let Some(verify_queue_tail_block_number) = verify_queue_tail_block_number {
                    // Combine the request with every single available source.
                    either::Left(self.sources.iter().filter_map(move |(src_id, src)| {
                        if src.finalized_block_height
                            <= verify_queue_tail_block_number.saturating_add(
                                u64::try_from(warp_sync_minimum_gap).unwrap_or(u64::MAX),
                            )
                        {
                            return None;
                        }

                        Some((
                            SourceId(src_id),
                            &src.user_data,
                            DesiredRequest::WarpSyncRequest {
                                block_hash: start_block_hash,
                            },
                        ))
                    }))
                } else {
                    either::Right(iter::empty())
                }
            } else {
                either::Right(iter::empty())
            }
        } else {
            either::Right(iter::empty())
        }
        .peekable();

        // If we are in the appropriate phase, and we are not currently downloading the runtime,
        // return a runtime download request.
        let desired_runtime_parameters_get = if let (
            WarpedBlockTy::Normal,
            RuntimeDownload::NotStarted { hint_doesnt_match },
            None,
            true,
            None,
        ) = (
            &self.warped_block_ty,
            &self.runtime_download,
            self.warp_sync_fragments_download,
            self.verify_queue.is_empty(),
            desired_warp_sync_request.peek(),
        ) {
            let code_key_to_request = if let (false, Some(hint)) =
                (*hint_doesnt_match, self.code_trie_node_hint.as_ref())
            {
                Cow::Owned(
                    trie::nibbles_to_bytes_truncate(
                        hint.closest_ancestor_excluding.iter().copied(),
                    )
                    .collect::<Vec<_>>(),
                )
            } else {
                Cow::Borrowed(&b":code"[..])
            };

            // Sources are ordered by increasing finalized block height, in order to
            // have the highest chance for the block to not be pruned.
            let sources_with_block = self
                .sources_by_finalized_height
                .range((self.warped_header_number, SourceId(usize::MIN))..)
                .map(|(_, src_id)| src_id);

            either::Left(sources_with_block.map(move |source_id| {
                (
                    *source_id,
                    &self.sources[source_id.0].user_data,
                    DesiredRequest::StorageGetMerkleProof {
                        block_hash: self.warped_header_hash,
                        state_trie_root: self.warped_header_state_root,
                        keys: vec![code_key_to_request.to_vec(), b":heappages".to_vec()],
                    },
                )
            }))
        } else {
            either::Right(iter::empty())
        };

        // If we are in the appropriate phase, and we are not currently downloading the body of
        // the block, return a runtime download request.
        let desired_body_download =
            if let (WarpedBlockTy::Normal, BodyDownload::NotStarted, None, true, None) = (
                &self.warped_block_ty,
                &self.body_download,
                self.warp_sync_fragments_download,
                self.verify_queue.is_empty(),
                desired_warp_sync_request.peek(),
            ) {
                // Sources are ordered by increasing finalized block height, in order to
                // have the highest chance for the block to not be pruned.
                let sources_with_block = self
                    .sources_by_finalized_height
                    .range((self.warped_header_number, SourceId(usize::MIN))..)
                    .map(|(_, src_id)| src_id);

                either::Left(sources_with_block.map(move |source_id| {
                    (
                        *source_id,
                        &self.sources[source_id.0].user_data,
                        DesiredRequest::BlockBodyDownload {
                            block_hash: self.warped_header_hash,
                            block_number: self.warped_header_number,
                            extrinsics_root: self.warped_header_extrinsics_root,
                        },
                    )
                }))
            } else {
                either::Right(iter::empty())
            };

        // Return the list of runtime calls indicated by the chain information builder state
        // machine.
        let desired_call_proofs = if matches!(self.warped_block_ty, WarpedBlockTy::Normal)
            && self.warp_sync_fragments_download.is_none()
            && self.verify_queue.is_empty()
            && desired_warp_sync_request.peek().is_none()
        {
            either::Left(
                self.runtime_calls
                    .iter()
                    .filter(|(_, v)| matches!(v, CallProof::NotStarted))
                    .map(|(call, _)| DesiredRequest::RuntimeCallMerkleProof {
                        block_hash: self.warped_header_hash,
                        function_name: call.function_name().into(),
                        parameter_vectored: Cow::Owned(call.parameter_vectored_vec()),
                    })
                    .flat_map(move |request_detail| {
                        // Sources are ordered by increasing finalized block height, in order to
                        // have the highest chance for the block to not be pruned.
                        let sources_with_block = self
                            .sources_by_finalized_height
                            .range((self.warped_header_number, SourceId(usize::MIN))..)
                            .map(|(_, src_id)| src_id);

                        sources_with_block.map(move |source_id| {
                            (
                                *source_id,
                                &self.sources[source_id.0].user_data,
                                request_detail.clone(),
                            )
                        })
                    }),
            )
        } else {
            either::Right(iter::empty())
        };

        // Chain all these demanded requests together.
        desired_warp_sync_request
            .chain(desired_runtime_parameters_get)
            .chain(desired_body_download)
            .chain(desired_call_proofs)
    }

    /// Inserts a new request in the data structure.
    ///
    /// > **Note**: The request doesn't necessarily have to match a request returned by
    /// >           [`WarpSync::desired_requests`].
    ///
    /// # Panic
    ///
    /// Panics if the [`SourceId`] is out of range.
    ///
    pub fn add_request(
        &mut self,
        source_id: SourceId,
        user_data: TRq,
        detail: RequestDetail,
    ) -> RequestId {
        assert!(self.sources.contains(source_id.0));

        let request_slot = self.in_progress_requests.vacant_entry();
        let request_id = RequestId(request_slot.key());

        match (&detail, &mut self.runtime_download, &mut self.body_download) {
            (RequestDetail::WarpSyncRequest { block_hash }, _, _)
                if self.warp_sync_fragments_download.is_none()
                    && *block_hash
                        == self
                            .verify_queue
                            .back()
                            .and_then(|entry| entry.fragments.last())
                            .map(|fragment| {
                                header::hash_from_scale_encoded_header(
                                    &fragment.scale_encoded_header,
                                )
                            })
                            .unwrap_or(self.warped_header_hash) =>
            {
                self.warp_sync_fragments_download = Some(request_id);
            }
            (
                RequestDetail::BlockBodyDownload {
                    block_hash,
                    block_number,
                },
                _,
                BodyDownload::NotStarted,
            ) => {
                if self.sources[source_id.0].finalized_block_height >= self.warped_header_number
                    && *block_number == self.warped_header_number
                    && *block_hash == self.warped_header_hash
                {
                    self.body_download = BodyDownload::Downloading { request_id };
                }
            }
            (
                RequestDetail::StorageGetMerkleProof { block_hash, keys },
                RuntimeDownload::NotStarted { hint_doesnt_match },
                _,
            ) => {
                let code_key_to_request = if let (false, Some(hint)) =
                    (*hint_doesnt_match, self.code_trie_node_hint.as_ref())
                {
                    Cow::Owned(
                        trie::nibbles_to_bytes_truncate(
                            hint.closest_ancestor_excluding.iter().copied(),
                        )
                        .collect::<Vec<_>>(),
                    )
                } else {
                    Cow::Borrowed(&b":code"[..])
                };

                if self.sources[source_id.0].finalized_block_height >= self.warped_header_number
                    && *block_hash == self.warped_header_hash
                    && keys.iter().any(|k| *k == *code_key_to_request)
                    && keys.iter().any(|k| k == b":heappages")
                {
                    self.runtime_download = RuntimeDownload::Downloading {
                        hint_doesnt_match: *hint_doesnt_match,
                        request_id,
                    };
                }
            }
            (
                RequestDetail::RuntimeCallMerkleProof {
                    block_hash,
                    function_name,
                    parameter_vectored,
                },
                _,
                _,
            ) => {
                for (info, status) in &mut self.runtime_calls {
                    if matches!(status, CallProof::NotStarted)
                        && self.sources[source_id.0].finalized_block_height
                            >= self.warped_header_number
                        && *block_hash == self.warped_header_hash
                        && function_name == info.function_name()
                        && parameters_equal(parameter_vectored, info.parameter_vectored())
                    {
                        *status = CallProof::Downloading(request_id);
                        break;
                    }
                }
            }
            _ => {}
        }

        request_slot.insert((source_id, user_data, detail));
        let _was_inserted = self
            .in_progress_requests_by_source
            .insert((source_id, request_id));
        debug_assert!(_was_inserted);
        request_id
    }

    /// Removes the given request from the state machine. Returns the user data that was associated
    /// to it.
    ///
    /// > **Note**: The state machine might want to re-start the same request again. It is out of
    /// >           the scope of this module to keep track of requests that don't succeed.
    ///
    /// # Panic
    ///
    /// Panics if the [`RequestId`] is invalid.
    ///
    pub fn remove_request(&mut self, id: RequestId) -> TRq {
        if self.warp_sync_fragments_download == Some(id) {
            self.warp_sync_fragments_download = None;
        }

        for call in self.runtime_calls.values_mut() {
            if matches!(call, CallProof::Downloading(rq_id) if *rq_id == id) {
                *call = CallProof::NotStarted;
            }
        }

        if let RuntimeDownload::Downloading {
            request_id,
            hint_doesnt_match,
        } = &mut self.runtime_download
        {
            if *request_id == id {
                self.runtime_download = RuntimeDownload::NotStarted {
                    hint_doesnt_match: *hint_doesnt_match,
                }
            }
        }

        if let BodyDownload::Downloading { request_id } = &mut self.body_download {
            if *request_id == id {
                self.body_download = BodyDownload::NotStarted;
            }
        }

        let (source_id, user_data, _) = self.in_progress_requests.remove(id.0);
        let _was_removed = self.in_progress_requests_by_source.remove(&(source_id, id));
        debug_assert!(_was_removed);
        user_data
    }

    /// Returns the [`SourceId`] that is expected to fulfill the given request.
    ///
    /// # Panic
    ///
    /// Panics if the [`RequestId`] is invalid.
    ///
    pub fn request_source_id(&self, request_id: RequestId) -> SourceId {
        self.in_progress_requests[request_id.0].0
    }

    /// Injects a body download and removes the given request from the state machine.
    /// Returns the user data that was associated to it.
    ///
    /// # Panic
    ///
    /// Panics if the [`RequestId`] is invalid.
    /// Panics if the [`RequestId`] doesn't correspond to a body download request.
    ///
    pub fn body_download_response(&mut self, id: RequestId, body: Vec<Vec<u8>>) -> TRq {
        // Remove the request from the list, obtaining its user data.
        // If the request corresponds to the runtime parameters we're looking for, the function
        // continues below, otherwise we return early.
        let (source_id, user_data) =
            match (self.in_progress_requests.remove(id.0), &self.body_download) {
                ((source_id, user_data, _), BodyDownload::Downloading { request_id })
                    if *request_id == id =>
                {
                    (source_id, user_data)
                }
                ((source_id, user_data, RequestDetail::BlockBodyDownload { .. }), _) => {
                    let _was_removed = self.in_progress_requests_by_source.remove(&(source_id, id));
                    debug_assert!(_was_removed);
                    return user_data;
                }
                (
                    (
                        _,
                        _,
                        RequestDetail::RuntimeCallMerkleProof { .. }
                        | RequestDetail::WarpSyncRequest { .. }
                        | RequestDetail::StorageGetMerkleProof { .. },
                    ),
                    _,
                ) => panic!(),
            };

        self.body_download = BodyDownload::Downloaded {
            downloaded_source: Some(source_id),
            body,
        };

        let _was_removed = self.in_progress_requests_by_source.remove(&(source_id, id));
        debug_assert!(_was_removed);

        user_data
    }

    /// Injects a Merkle proof and removes the given request from the state machine.
    /// Returns the user data that was associated to it.
    ///
    /// # Panic
    ///
    /// Panics if the [`RequestId`] is invalid.
    /// Panics if the [`RequestId`] doesn't correspond to a storage get request.
    ///
    pub fn storage_get_response(&mut self, id: RequestId, merkle_proof: Vec<u8>) -> TRq {
        // Remove the request from the list, obtaining its user data.
        // If the request corresponds to the runtime parameters we're looking for, the function
        // continues below, otherwise we return early.
        let (source_id, hint_doesnt_match, user_data) = match (
            self.in_progress_requests.remove(id.0),
            &self.runtime_download,
        ) {
            (
                (source_id, user_data, _),
                RuntimeDownload::Downloading {
                    request_id,
                    hint_doesnt_match,
                },
            ) if *request_id == id => (source_id, *hint_doesnt_match, user_data),
            ((source_id, user_data, RequestDetail::StorageGetMerkleProof { .. }), _) => {
                let _was_removed = self.in_progress_requests_by_source.remove(&(source_id, id));
                debug_assert!(_was_removed);
                return user_data;
            }
            (
                (
                    _,
                    _,
                    RequestDetail::RuntimeCallMerkleProof { .. }
                    | RequestDetail::WarpSyncRequest { .. }
                    | RequestDetail::BlockBodyDownload { .. },
                ),
                _,
            ) => panic!(),
        };

        self.runtime_download = RuntimeDownload::NotVerified {
            downloaded_source: Some(source_id),
            hint_doesnt_match,
            trie_proof: merkle_proof,
        };

        let _was_removed = self.in_progress_requests_by_source.remove(&(source_id, id));
        debug_assert!(_was_removed);

        user_data
    }

    /// Injects a response and removes the given request from the state machine. Returns
    /// the user data that was associated to it.
    ///
    /// # Panic
    ///
    /// Panics if the [`RequestId`] is invalid.
    /// Panics if the [`RequestId`] doesn't correspond to a runtime Merkle call proof request.
    ///
    pub fn runtime_call_merkle_proof_response(
        &mut self,
        request_id: RequestId,
        response: Vec<u8>,
    ) -> TRq {
        let (source_id, user_data, RequestDetail::RuntimeCallMerkleProof { .. }) =
            self.in_progress_requests.remove(request_id.0)
        else {
            // Wrong request type.
            panic!()
        };

        for call in self.runtime_calls.values_mut() {
            if matches!(call, CallProof::Downloading(rq_id) if *rq_id == request_id) {
                *call = CallProof::Downloaded {
                    downloaded_source: Some(source_id),
                    proof: response,
                };
                break;
            }
        }

        let _was_removed = self
            .in_progress_requests_by_source
            .remove(&(source_id, request_id));
        debug_assert!(_was_removed);

        user_data
    }

    /// Injects a response and removes the given request from the state machine. Returns
    /// the user data that was associated to it.
    ///
    /// If the header of the last fragment of the response is decodable, this function updates
    /// the finalized block of the source.
    ///
    /// # Panic
    ///
    /// Panics if the [`RequestId`] is invalid.
    /// Panics if the [`RequestId`] doesn't correspond to a warp sync request.
    ///
    // TODO: more zero cost API w.r.t. the fragments
    pub fn warp_sync_request_response(
        &mut self,
        request_id: RequestId,
        fragments: Vec<WarpSyncFragment>,
        final_set_of_fragments: bool,
    ) -> TRq {
        let (rq_source_id, user_data) = match self.in_progress_requests.remove(request_id.0) {
            (rq_source_id, user_data, RequestDetail::WarpSyncRequest { .. }) => {
                (rq_source_id, user_data)
            }
            (_, _, _) => panic!(),
        };

        debug_assert!(self.sources.contains(rq_source_id.0));

        // Since we send requests only to sources with an appropriate finalized block, we make
        // sure that the finalized block of the source that sent the response matches the
        // fragments that it sent.
        // If we didn't do that, it would be possible for example to warp sync to block 200 while
        // believing that the source is only at block 199, and thus the warp syncing would stall.
        if let Some(last_header) = fragments
            .last()
            .and_then(|h| header::decode(&h.scale_encoded_header, self.block_number_bytes).ok())
        {
            let src_finalized_height = &mut self.sources[rq_source_id.0].finalized_block_height;

            let new_height = if final_set_of_fragments {
                // If the source indicated that this is the last fragment, then we know that
                // it's also equal to their finalized block.
                last_header.number
            } else {
                // If this is not the last fragment, we know that the finalized block of the
                // source is *at least* the one provided.
                // TODO: could maybe do + gap or something?
                cmp::max(*src_finalized_height, last_header.number.saturating_add(1))
            };

            if *src_finalized_height != new_height {
                let _was_in = self
                    .sources_by_finalized_height
                    .remove(&(*src_finalized_height, rq_source_id));
                debug_assert!(_was_in);

                *src_finalized_height = new_height;

                let _inserted = self
                    .sources_by_finalized_height
                    .insert((*src_finalized_height, rq_source_id));
                debug_assert!(_inserted);
            }
        }

        if self.warp_sync_fragments_download == Some(request_id) {
            self.warp_sync_fragments_download = None;

            self.verify_queue.push_back(PendingVerify {
                final_set_of_fragments,
                downloaded_source: Some(rq_source_id),
                fragments,
                next_fragment_to_verify_index: 0,
            });
        }

        let _was_removed = self
            .in_progress_requests_by_source
            .remove(&(rq_source_id, request_id));
        debug_assert!(_was_removed);

        user_data
    }

    /// Start processing one CPU operation.
    ///
    /// This function takes ownership of `self` and yields it back after the operation is finished.
    // TODO: take a `&mut self` instead of `self` ; requires many changes in all.rs
    pub fn process_one(self) -> ProcessOne<TSrc, TRq> {
        // If we've downloaded everything that was needed, switch to "build chain information"
        // mode.
        if matches!(self.runtime_download, RuntimeDownload::Verified { .. })
            && matches!(
                self.body_download,
                BodyDownload::NotNeeded | BodyDownload::Downloaded { .. }
            )
            && self
                .runtime_calls
                .values()
                .all(|c| matches!(c, CallProof::Downloaded { .. }))
        {
            return ProcessOne::BuildChainInformation(BuildChainInformation { inner: self });
        }

        if let RuntimeDownload::NotVerified { .. } = &self.runtime_download {
            return ProcessOne::BuildRuntime(BuildRuntime { inner: self });
        }

        if !self.verify_queue.is_empty() {
            return ProcessOne::VerifyWarpSyncFragment(VerifyWarpSyncFragment { inner: self });
        }

        ProcessOne::Idle(self)
    }
}

impl<TSrc, TRq> ops::Index<SourceId> for WarpSync<TSrc, TRq> {
    type Output = TSrc;

    #[track_caller]
    fn index(&self, source_id: SourceId) -> &TSrc {
        debug_assert!(self.sources.contains(source_id.0));
        &self.sources[source_id.0].user_data
    }
}

impl<TSrc, TRq> ops::IndexMut<SourceId> for WarpSync<TSrc, TRq> {
    #[track_caller]
    fn index_mut(&mut self, source_id: SourceId) -> &mut TSrc {
        debug_assert!(self.sources.contains(source_id.0));
        &mut self.sources[source_id.0].user_data
    }
}

impl<TSrc, TRq> ops::Index<RequestId> for WarpSync<TSrc, TRq> {
    type Output = TRq;

    #[track_caller]
    fn index(&self, request_id: RequestId) -> &TRq {
        debug_assert!(self.in_progress_requests.contains(request_id.0));
        &self.in_progress_requests[request_id.0].1
    }
}

impl<TSrc, TRq> ops::IndexMut<RequestId> for WarpSync<TSrc, TRq> {
    #[track_caller]
    fn index_mut(&mut self, request_id: RequestId) -> &mut TRq {
        debug_assert!(self.in_progress_requests.contains(request_id.0));
        &mut self.in_progress_requests[request_id.0].1
    }
}

/// Information about a request that the warp sync state machine would like to start.
///
/// See [`WarpSync::desired_requests`].
#[derive(Debug, Clone)]
pub enum DesiredRequest {
    /// A warp sync request should be started.
    WarpSyncRequest {
        /// Starting point of the warp syncing. The first fragment of the response should be the
        /// of the epoch that the starting point is in.
        block_hash: [u8; 32],
    },
    /// A block body download should be started.
    BlockBodyDownload {
        /// Hash of the block whose body to download.
        block_hash: [u8; 32],
        /// Height of the block whose body to download.
        block_number: u64,
        /// Extrinsics trie root hash found in the header of the block.
        extrinsics_root: [u8; 32],
    },
    /// A storage request of the runtime code and heap pages should be started.
    StorageGetMerkleProof {
        /// Hash of the block to request the parameters against.
        block_hash: [u8; 32],
        /// State trie root hash found in the header of the block.
        state_trie_root: [u8; 32],
        /// Keys whose values are requested.
        // TODO: consider Cow<'static, [u8]> instead
        keys: Vec<Vec<u8>>,
    },
    /// A call proof should be started.
    RuntimeCallMerkleProof {
        /// Hash of the header of the block the call should be made against.
        block_hash: [u8; 32],
        /// Name of the function of the call proof.
        function_name: Cow<'static, str>,
        /// Parameters of the call.
        parameter_vectored: Cow<'static, [u8]>,
    },
}

/// Information about a request to add to the state machine.
///
/// See [`WarpSync::add_request`].
#[derive(Debug, Clone)]
pub enum RequestDetail {
    /// See [`DesiredRequest::WarpSyncRequest`].
    WarpSyncRequest {
        /// See [`DesiredRequest::WarpSyncRequest::block_hash`].
        block_hash: [u8; 32],
    },
    /// See [`DesiredRequest::BlockBodyDownload`].
    BlockBodyDownload {
        /// See [`DesiredRequest::BlockBodyDownload::block_hash`].
        block_hash: [u8; 32],
        /// See [`DesiredRequest::BlockBodyDownload::block_number`].
        // TODO: remove this field as it's inappropriate, but this causes issues in all.rs
        block_number: u64,
    },
    /// See [`DesiredRequest::StorageGetMerkleProof`].
    StorageGetMerkleProof {
        /// See [`DesiredRequest::StorageGetMerkleProof::block_hash`].
        block_hash: [u8; 32],
        /// See [`DesiredRequest::StorageGetMerkleProof::keys`].
        keys: Vec<Vec<u8>>,
    },
    /// See [`DesiredRequest::RuntimeCallMerkleProof`].
    RuntimeCallMerkleProof {
        /// See [`DesiredRequest::RuntimeCallMerkleProof::block_hash`].
        block_hash: [u8; 32],
        /// See [`DesiredRequest::RuntimeCallMerkleProof::function_name`].
        function_name: Cow<'static, str>,
        /// See [`DesiredRequest::RuntimeCallMerkleProof::parameter_vectored`].
        parameter_vectored: Cow<'static, [u8]>,
    },
}

/// Identifier for a request in the warp sync state machine.
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct RequestId(usize);

/// Return value of [`WarpSync::process_one`].
pub enum ProcessOne<TSrc, TRq> {
    /// Nothing to verify at the moment. The state machine is yielded back.
    Idle(WarpSync<TSrc, TRq>),
    /// Ready to verify a warp sync fragment.
    ///
    /// > **Note**: In case where a source has sent an empty list of fragment, which is invalid,
    /// >           this variant will "verify" the list and produce an error.
    VerifyWarpSyncFragment(VerifyWarpSyncFragment<TSrc, TRq>),
    /// Ready to build the runtime of the chain..
    BuildRuntime(BuildRuntime<TSrc, TRq>),
    /// Ready to verify the parameters of the chain against the finalized block.
    BuildChainInformation(BuildChainInformation<TSrc, TRq>),
}

/// Ready to verify a warp sync fragment.
///
/// > **Note**: In case where a source has sent an empty list of fragment, which is invalid,
/// >           this variant will "verify" the list and produce an error.
pub struct VerifyWarpSyncFragment<TSrc, TRq> {
    inner: WarpSync<TSrc, TRq>,
}

impl<TSrc, TRq> VerifyWarpSyncFragment<TSrc, TRq> {
    /// Returns the source that has sent the fragments that we are about to verify, and its user
    /// data.
    ///
    /// Returns `None` if the source has been removed since the fragments have been downloaded.
    pub fn proof_sender(&self) -> Option<(SourceId, &TSrc)> {
        let entry_to_verify = self.inner.verify_queue.front().unwrap();
        let source_id = entry_to_verify.downloaded_source?;
        Some((source_id, &self.inner.sources[source_id.0].user_data))
    }

    /// Verify one warp sync fragment.
    ///
    /// Must be passed a randomly-generated value that is used by the verification process. Note
    /// that the verification is still deterministic.
    ///
    /// On success, returns the block hash and height that have been verified as being part of
    /// the chain.
    /// On error, returns why the verification has failed. The warp syncing process still
    /// continues.
    pub fn verify(
        mut self,
        randomness_seed: [u8; 32],
    ) -> (
        WarpSync<TSrc, TRq>,
        Result<([u8; 32], u64), VerifyFragmentError>,
    ) {
        // A `VerifyWarpSyncFragment` is only ever created if `verify_queue` is non-empty.
        debug_assert!(!self.inner.verify_queue.is_empty());
        let fragments_to_verify = self
            .inner
            .verify_queue
            .front_mut()
            .unwrap_or_else(|| unreachable!());

        // The source has sent an empty list of fragments. This is invalid.
        if fragments_to_verify.fragments.is_empty() {
            self.inner.verify_queue.pop_front().unwrap();
            return (self.inner, Err(VerifyFragmentError::EmptyProof));
        }

        // Given that the list of fragments is non-empty, we are assuming that there are still
        // fragments to verify, otherwise this entry should have been removed in a previous
        // iteration.
        let fragment_to_verify = fragments_to_verify
            .fragments
            .get(fragments_to_verify.next_fragment_to_verify_index)
            .unwrap_or_else(|| unreachable!());

        // It has been checked at the warp sync initialization that the finality algorithm is
        // indeed Grandpa.
        let chain_information::ChainInformationFinality::Grandpa {
            after_finalized_block_authorities_set_id,
            finalized_triggered_authorities,
            .. // TODO: support finalized_scheduled_change? difficult to implement
        } = &mut self.inner.warped_finality
        else {
            unreachable!()
        };

        // Decode the header and justification of the fragment.
        let fragment_header_hash =
            header::hash_from_scale_encoded_header(&fragment_to_verify.scale_encoded_header);
        let fragment_decoded_header = match header::decode(
            &fragment_to_verify.scale_encoded_header,
            self.inner.block_number_bytes,
        ) {
            Ok(j) => j,
            Err(err) => {
                self.inner.verify_queue.clear();
                self.inner.warp_sync_fragments_download = None;
                return (self.inner, Err(VerifyFragmentError::InvalidHeader(err)));
            }
        };
        let fragment_decoded_justification = match decode::decode_grandpa_justification(
            &fragment_to_verify.scale_encoded_justification,
            self.inner.block_number_bytes,
        ) {
            Ok(j) => j,
            Err(err) => {
                self.inner.verify_queue.clear();
                self.inner.warp_sync_fragments_download = None;
                return (
                    self.inner,
                    Err(VerifyFragmentError::InvalidJustification(err)),
                );
            }
        };

        // Make sure that the header would actually advance the warp sync process forward.
        if fragment_decoded_header.number <= self.inner.warped_header_number {
            self.inner.verify_queue.clear();
            self.inner.warp_sync_fragments_download = None;
            return (
                self.inner,
                Err(VerifyFragmentError::BlockNumberNotIncrementing),
            );
        }

        // Make sure that the justification indeed corresponds to the header.
        if *fragment_decoded_justification.target_hash != fragment_header_hash
            || fragment_decoded_justification.target_number != fragment_decoded_header.number
        {
            let error = VerifyFragmentError::TargetHashMismatch {
                justification_target_hash: *fragment_decoded_justification.target_hash,
                justification_target_height: fragment_decoded_justification.target_number,
                header_hash: fragment_header_hash,
                header_height: fragment_decoded_header.number,
            };
            self.inner.verify_queue.clear();
            self.inner.warp_sync_fragments_download = None;
            return (self.inner, Err(error));
        }

        // Check whether the justification is valid.
        if let Err(err) = verify::verify_justification(verify::JustificationVerifyConfig {
            justification: &fragment_to_verify.scale_encoded_justification,
            block_number_bytes: self.inner.block_number_bytes,
            authorities_list: finalized_triggered_authorities
                .iter()
                .map(|a| &a.public_key[..]),
            authorities_set_id: *after_finalized_block_authorities_set_id,
            randomness_seed,
        }) {
            self.inner.verify_queue.clear();
            self.inner.warp_sync_fragments_download = None;
            return (
                self.inner,
                Err(VerifyFragmentError::JustificationVerify(err)),
            );
        }

        // Try to grab the new list of authorities from the header.
        let new_authorities_list = fragment_decoded_header
            .digest
            .logs()
            .find_map(|log_item| match log_item {
                header::DigestItemRef::GrandpaConsensus(grandpa_log_item) => match grandpa_log_item
                {
                    header::GrandpaConsensusLogRef::ScheduledChange(change)
                    | header::GrandpaConsensusLogRef::ForcedChange { change, .. } => {
                        Some(change.next_authorities)
                    }
                    _ => None,
                },
                _ => None,
            })
            .map(|next_authorities| {
                next_authorities
                    .map(header::GrandpaAuthority::from)
                    .collect()
            });

        // Fragments must only include headers containing an update to the list of authorities,
        // unless it's the very head of the chain.
        if new_authorities_list.is_none()
            && (!fragments_to_verify.final_set_of_fragments
                || fragments_to_verify.next_fragment_to_verify_index
                    != fragments_to_verify.fragments.len() - 1)
        {
            self.inner.verify_queue.clear();
            self.inner.warp_sync_fragments_download = None;
            return (self.inner, Err(VerifyFragmentError::NonMinimalProof));
        }

        // Verification of the fragment has succeeded 🎉. We can now update `self`.
        fragments_to_verify.next_fragment_to_verify_index += 1;
        self.inner.warped_header_number = fragment_decoded_header.number;
        self.inner.warped_header_state_root = *fragment_decoded_header.state_root;
        self.inner.warped_header_extrinsics_root = *fragment_decoded_header.extrinsics_root;
        self.inner.warped_header_hash = fragment_header_hash;
        self.inner.warped_header = fragment_to_verify.scale_encoded_header.clone(); // TODO: figure out how to remove this clone()
        self.inner.warped_block_ty = WarpedBlockTy::Normal;
        self.inner.runtime_download = RuntimeDownload::NotStarted {
            hint_doesnt_match: false,
        };
        if !matches!(self.inner.body_download, BodyDownload::NotNeeded) {
            self.inner.body_download = BodyDownload::NotStarted;
        }
        self.inner.runtime_calls =
            runtime_calls_default_value(self.inner.verified_chain_information.as_ref().consensus);
        if let Some(new_authorities_list) = new_authorities_list {
            *finalized_triggered_authorities = new_authorities_list;
            *after_finalized_block_authorities_set_id += 1;
        }
        if fragments_to_verify.next_fragment_to_verify_index == fragments_to_verify.fragments.len()
        {
            self.inner.verify_queue.pop_front().unwrap();
        }

        // Returning.
        let result = Ok((
            self.inner.warped_header_hash,
            self.inner.warped_header_number,
        ));
        (self.inner, result)
    }
}

/// Error potentially returned by [`VerifyWarpSyncFragment::verify`].
#[derive(Debug)]
pub enum VerifyFragmentError {
    /// Justification found within the fragment is invalid.
    JustificationVerify(verify::JustificationVerifyError),
    /// Mismatch between the block targeted by the justification and the header.
    TargetHashMismatch {
        /// Hash of the block the justification targets.
        justification_target_hash: [u8; 32],
        /// Height of the block the justification targets.
        justification_target_height: u64,
        /// Hash of the header.
        header_hash: [u8; 32],
        /// Height of the header.
        header_height: u64,
    },
    /// Warp sync fragment doesn't contain an authorities list change when it should.
    NonMinimalProof,
    /// Header does not actually advance the warp syncing process. This means that a source has
    /// sent a header below the requested hash.
    BlockNumberNotIncrementing,
    /// Warp sync proof is empty.
    EmptyProof,
    /// Failed to decode header.
    InvalidHeader(header::Error),
    /// Failed to decode justification.
    InvalidJustification(decode::JustificationDecodeError),
}

impl fmt::Display for VerifyFragmentError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            VerifyFragmentError::JustificationVerify(err) => fmt::Display::fmt(err, f),
            VerifyFragmentError::TargetHashMismatch {
                justification_target_hash,
                justification_target_height,
                header_hash,
                header_height,
            } => {
                write!(
                    f,
                    "Justification target (hash: {}, height: {}) doesn't match the associated header (hash: {}, height: {})",
                    HashDisplay(justification_target_hash),
                    justification_target_height,
                    HashDisplay(header_hash),
                    header_height,
                )
            }
            VerifyFragmentError::NonMinimalProof => write!(
                f,
                "Warp sync proof fragment doesn't contain an authorities list change"
            ),
            VerifyFragmentError::BlockNumberNotIncrementing => write!(
                f,
                "Warp sync proof header doesn't advance the warp syncing process"
            ),
            VerifyFragmentError::EmptyProof => write!(f, "Warp sync proof is empty"),
            VerifyFragmentError::InvalidHeader(err) => write!(f, "Failed to decode header: {err}"),
            VerifyFragmentError::InvalidJustification(err) => {
                write!(f, "Failed to decode justification: {err}")
            }
        }
    }
}

/// Problem encountered during a call to [`BuildRuntime::build`] or
/// [`BuildChainInformation::build`] that can be attributed to the source sending invalid data.
#[derive(Debug, derive_more::Display, derive_more::Error)]
#[display("{error}")]
pub struct SourceMisbehavior {
    /// Source that committed the felony. `None` if the source has been removed between the moment
    /// when the request has succeeded and when it has been verified.
    pub source_id: Option<SourceId>,
    /// Error that the source made.
    #[error(source)]
    pub error: SourceMisbehaviorTy,
}

/// See [`SourceMisbehavior::error`].
#[derive(Debug, derive_more::Display, derive_more::Error)]
pub enum SourceMisbehaviorTy {
    /// Failed to verify Merkle proof.
    InvalidMerkleProof(proof_decode::Error),
    /// Merkle proof is missing the necessary entries.
    MerkleProofEntriesMissing,
    /// Downloaded block body doesn't match the expected extrinsics root.
    BlockBodyExtrinsicsRootMismatch,
}

/// Problem encountered during a call to [`BuildRuntime::build`].
#[derive(Debug, derive_more::Display, derive_more::Error)]
pub enum BuildRuntimeError {
    /// The chain doesn't include any storage item at `:code`.
    #[display("The chain doesn't include any storage item at `:code`")]
    MissingCode,
    /// The storage item at `:heappages` is in an incorrect format.
    #[display("Invalid heap pages value: {_0}")]
    InvalidHeapPages(executor::InvalidHeapPagesError),
    /// Error building the runtime of the chain.
    #[display("Error building the runtime: {_0}")]
    RuntimeBuild(executor::host::NewErr),
    /// Source that has sent a proof didn't behave properly.
    SourceMisbehavior(SourceMisbehavior),
}

/// Ready to build the runtime of the finalized chain.
pub struct BuildRuntime<TSrc, TRq> {
    inner: WarpSync<TSrc, TRq>,
}

impl<TSrc, TRq> BuildRuntime<TSrc, TRq> {
    /// Build the runtime of the chain.
    ///
    /// Must be passed parameters used for the construction of the runtime: a hint as to whether
    /// the runtime is trusted and/or will be executed again, and whether unresolved function
    /// imports are allowed.
    pub fn build(
        mut self,
        exec_hint: ExecHint,
        allow_unresolved_imports: bool,
    ) -> (WarpSync<TSrc, TRq>, Result<(), BuildRuntimeError>) {
        let RuntimeDownload::NotVerified {
            downloaded_source,
            hint_doesnt_match,
            trie_proof,
        } = &mut self.inner.runtime_download
        else {
            unreachable!()
        };

        let downloaded_runtime = mem::take(trie_proof);
        let decoded_downloaded_runtime =
            match proof_decode::decode_and_verify_proof(proof_decode::Config {
                proof: &downloaded_runtime[..],
            }) {
                Ok(p) => p,
                Err(err) => {
                    let downloaded_source = *downloaded_source;
                    self.inner.runtime_download = RuntimeDownload::NotStarted {
                        hint_doesnt_match: *hint_doesnt_match,
                    };
                    return (
                        self.inner,
                        Err(BuildRuntimeError::SourceMisbehavior(SourceMisbehavior {
                            source_id: downloaded_source,
                            error: SourceMisbehaviorTy::InvalidMerkleProof(err),
                        })),
                    );
                }
            };

        let (
            finalized_storage_code_merkle_value,
            finalized_storage_code_closest_ancestor_excluding,
        ) = {
            let code_nibbles = trie::bytes_to_nibbles(b":code".iter().copied()).collect::<Vec<_>>();
            match decoded_downloaded_runtime.closest_ancestor_in_proof(
                &self.inner.warped_header_state_root,
                code_nibbles.iter().take(code_nibbles.len() - 1).copied(),
            ) {
                Ok(Some(closest_ancestor_key)) => {
                    let closest_ancestor_key = closest_ancestor_key.collect::<Vec<_>>();
                    let next_nibble = code_nibbles[closest_ancestor_key.len()];
                    let merkle_value = decoded_downloaded_runtime
                        .trie_node_info(
                            &self.inner.warped_header_state_root,
                            closest_ancestor_key.iter().copied(),
                        )
                        .unwrap()
                        .children
                        .child(next_nibble)
                        .merkle_value();

                    match merkle_value {
                        Some(mv) => (mv.to_owned(), closest_ancestor_key),
                        None => {
                            self.inner.warped_block_ty = WarpedBlockTy::KnownBad;
                            self.inner.runtime_download = RuntimeDownload::NotStarted {
                                hint_doesnt_match: *hint_doesnt_match,
                            };
                            if !matches!(self.inner.body_download, BodyDownload::NotNeeded) {
                                self.inner.body_download = BodyDownload::NotStarted;
                            }
                            return (self.inner, Err(BuildRuntimeError::MissingCode));
                        }
                    }
                }
                Ok(None) => {
                    self.inner.warped_block_ty = WarpedBlockTy::KnownBad;
                    self.inner.runtime_download = RuntimeDownload::NotStarted {
                        hint_doesnt_match: *hint_doesnt_match,
                    };
                    if !matches!(self.inner.body_download, BodyDownload::NotNeeded) {
                        self.inner.body_download = BodyDownload::NotStarted;
                    }
                    return (self.inner, Err(BuildRuntimeError::MissingCode));
                }
                Err(proof_decode::IncompleteProofError { .. }) => {
                    let downloaded_source = *downloaded_source;
                    self.inner.runtime_download = RuntimeDownload::NotStarted {
                        hint_doesnt_match: *hint_doesnt_match,
                    };
                    if !matches!(self.inner.body_download, BodyDownload::NotNeeded) {
                        self.inner.body_download = BodyDownload::NotStarted;
                    }
                    return (
                        self.inner,
                        Err(BuildRuntimeError::SourceMisbehavior(SourceMisbehavior {
                            source_id: downloaded_source,
                            error: SourceMisbehaviorTy::MerkleProofEntriesMissing,
                        })),
                    );
                }
            }
        };

        let finalized_storage_code = if let (false, Some(hint)) =
            (*hint_doesnt_match, self.inner.code_trie_node_hint.as_ref())
        {
            if hint.merkle_value == finalized_storage_code_merkle_value {
                &hint.storage_value
            } else {
                self.inner.runtime_download = RuntimeDownload::NotStarted {
                    hint_doesnt_match: true,
                };
                return (self.inner, Ok(()));
            }
        } else {
            match decoded_downloaded_runtime
                .storage_value(&self.inner.warped_header_state_root, b":code")
            {
                Ok(Some((code, _))) => code,
                Ok(None) => {
                    self.inner.warped_block_ty = WarpedBlockTy::KnownBad;
                    self.inner.runtime_download = RuntimeDownload::NotStarted {
                        hint_doesnt_match: *hint_doesnt_match,
                    };
                    if !matches!(self.inner.body_download, BodyDownload::NotNeeded) {
                        self.inner.body_download = BodyDownload::NotStarted;
                    }
                    return (self.inner, Err(BuildRuntimeError::MissingCode));
                }
                Err(proof_decode::IncompleteProofError { .. }) => {
                    let downloaded_source = *downloaded_source;
                    return (
                        self.inner,
                        Err(BuildRuntimeError::SourceMisbehavior(SourceMisbehavior {
                            source_id: downloaded_source,
                            error: SourceMisbehaviorTy::MerkleProofEntriesMissing,
                        })),
                    );
                }
            }
        };

        let finalized_storage_heappages = match decoded_downloaded_runtime
            .storage_value(&self.inner.warped_header_state_root, b":heappages")
        {
            Ok(val) => val.map(|(v, _)| v),
            Err(proof_decode::IncompleteProofError { .. }) => {
                let downloaded_source = *downloaded_source;
                return (
                    self.inner,
                    Err(BuildRuntimeError::SourceMisbehavior(SourceMisbehavior {
                        source_id: downloaded_source,
                        error: SourceMisbehaviorTy::MerkleProofEntriesMissing,
                    })),
                );
            }
        };

        let decoded_heap_pages =
            match executor::storage_heap_pages_to_value(finalized_storage_heappages) {
                Ok(hp) => hp,
                Err(err) => {
                    self.inner.warped_block_ty = WarpedBlockTy::KnownBad;
                    self.inner.runtime_download = RuntimeDownload::NotStarted {
                        hint_doesnt_match: *hint_doesnt_match,
                    };
                    if !matches!(self.inner.body_download, BodyDownload::NotNeeded) {
                        self.inner.body_download = BodyDownload::NotStarted;
                    }
                    return (self.inner, Err(BuildRuntimeError::InvalidHeapPages(err)));
                }
            };

        let runtime = match HostVmPrototype::new(host::Config {
            module: &finalized_storage_code,
            heap_pages: decoded_heap_pages,
            exec_hint,
            allow_unresolved_imports,
        }) {
            Ok(runtime) => runtime,
            Err(err) => {
                self.inner.warped_block_ty = WarpedBlockTy::KnownBad;
                self.inner.runtime_download = RuntimeDownload::NotStarted {
                    hint_doesnt_match: *hint_doesnt_match,
                };
                if !matches!(self.inner.body_download, BodyDownload::NotNeeded) {
                    self.inner.body_download = BodyDownload::NotStarted;
                }
                return (self.inner, Err(BuildRuntimeError::RuntimeBuild(err)));
            }
        };

        let chain_info_builder = chain_information::build::ChainInformationBuild::new(
            chain_information::build::Config {
                finalized_block_header: chain_information::build::ConfigFinalizedBlockHeader::Any {
                    scale_encoded_header: self.inner.warped_header.clone(),
                    known_finality: if self.inner.download_all_chain_information_storage_proofs {
                        None
                    } else {
                        Some(self.inner.warped_finality.clone())
                    },
                },
                block_number_bytes: self.inner.block_number_bytes,
                runtime,
            },
        );

        if let chain_information::build::ChainInformationBuild::InProgress(in_progress) =
            &chain_info_builder
        {
            for call in in_progress.remaining_calls() {
                if let hashbrown::hash_map::Entry::Vacant(entry) =
                    self.inner.runtime_calls.entry(call)
                {
                    entry.insert(CallProof::NotStarted);
                }
            }
        }

        self.inner.runtime_download = RuntimeDownload::Verified {
            downloaded_runtime: DownloadedRuntime {
                storage_code: Some(finalized_storage_code.to_vec()),
                storage_heap_pages: finalized_storage_heappages.map(|v| v.to_vec()),
                code_merkle_value: Some(finalized_storage_code_merkle_value),
                closest_ancestor_excluding: Some(finalized_storage_code_closest_ancestor_excluding),
            },
            chain_info_builder,
        };

        (self.inner, Ok(()))
    }
}

/// Problem encountered during a call to [`BuildChainInformation::build`].
#[derive(Debug, derive_more::Display, derive_more::Error)]
pub enum BuildChainInformationError {
    /// Error building the chain information.
    #[display("Error building the chain information: {_0}")]
    ChainInformationBuild(chain_information::build::Error),
    /// Source that has sent a proof didn't behave properly.
    SourceMisbehavior(SourceMisbehavior),
}

/// Ready to verify the parameters of the chain against the finalized block.
pub struct BuildChainInformation<TSrc, TRq> {
    inner: WarpSync<TSrc, TRq>,
}

impl<TSrc, TRq> BuildChainInformation<TSrc, TRq> {
    /// Build the information about the chain.
    pub fn build(
        mut self,
    ) -> (
        WarpSync<TSrc, TRq>,
        Result<RuntimeInformation, BuildChainInformationError>,
    ) {
        let downloaded_body = match &mut self.inner.body_download {
            BodyDownload::NotNeeded => None,
            BodyDownload::Downloaded {
                downloaded_source,
                body,
            } => {
                if header::extrinsics_root(body) != self.inner.warped_header_extrinsics_root {
                    let source_id = *downloaded_source;
                    self.inner.body_download = BodyDownload::NotStarted;
                    return (
                        self.inner,
                        Err(BuildChainInformationError::SourceMisbehavior(
                            SourceMisbehavior {
                                source_id,
                                error: SourceMisbehaviorTy::BlockBodyExtrinsicsRootMismatch,
                            },
                        )),
                    );
                }

                Some(body)
            }
            _ => unreachable!(),
        };

        let RuntimeDownload::Verified {
            mut chain_info_builder,
            downloaded_runtime,
            ..
        } = mem::replace(
            &mut self.inner.runtime_download,
            RuntimeDownload::NotStarted {
                hint_doesnt_match: false,
            },
        )
        else {
            unreachable!()
        };

        let runtime_calls = mem::take(&mut self.inner.runtime_calls);

        debug_assert!(
            runtime_calls
                .values()
                .all(|c| matches!(c, CallProof::Downloaded { .. }))
        );

        // Decode all the Merkle proofs that have been received.
        let calls = {
            let mut decoded_proofs = hashbrown::HashMap::with_capacity_and_hasher(
                runtime_calls.len(),
                fnv::FnvBuildHasher::default(),
            );

            for (call, proof) in runtime_calls {
                let CallProof::Downloaded {
                    proof,
                    downloaded_source,
                } = proof
                else {
                    unreachable!()
                };

                let decoded_proof =
                    match proof_decode::decode_and_verify_proof(proof_decode::Config {
                        proof: proof.into_iter(),
                    }) {
                        Ok(d) => d,
                        Err(err) => {
                            return (
                                self.inner,
                                Err(BuildChainInformationError::SourceMisbehavior(
                                    SourceMisbehavior {
                                        source_id: downloaded_source,
                                        error: SourceMisbehaviorTy::InvalidMerkleProof(err),
                                    },
                                )),
                            );
                        }
                    };

                decoded_proofs.insert(call, (decoded_proof, downloaded_source));
            }

            decoded_proofs
        };

        loop {
            let in_progress = match chain_info_builder {
                chain_information::build::ChainInformationBuild::Finished {
                    result: Ok(chain_information),
                    virtual_machine,
                } => {
                    // This `if` is necessary as in principle we might have continued warp syncing
                    // after downloading everything needed but before building the chain
                    // information.
                    if self.inner.warped_header_number
                        == chain_information.as_ref().finalized_block_header.number
                    {
                        self.inner.warped_block_ty = WarpedBlockTy::AlreadyVerified;
                    }

                    let finalized_body = downloaded_body.map(mem::take);
                    if !matches!(self.inner.body_download, BodyDownload::NotNeeded) {
                        self.inner.body_download = BodyDownload::NotStarted;
                    }

                    self.inner.verified_chain_information = chain_information;
                    self.inner.runtime_calls = runtime_calls_default_value(
                        self.inner.verified_chain_information.as_ref().consensus,
                    );
                    return (
                        self.inner,
                        Ok(RuntimeInformation {
                            finalized_runtime: virtual_machine,
                            finalized_body,
                            finalized_storage_code: downloaded_runtime.storage_code,
                            finalized_storage_heap_pages: downloaded_runtime.storage_heap_pages,
                            finalized_storage_code_merkle_value: downloaded_runtime
                                .code_merkle_value,
                            finalized_storage_code_closest_ancestor_excluding: downloaded_runtime
                                .closest_ancestor_excluding,
                        }),
                    );
                }
                chain_information::build::ChainInformationBuild::Finished {
                    result: Err(err),
                    ..
                } => {
                    self.inner.warped_block_ty = WarpedBlockTy::KnownBad;
                    return (
                        self.inner,
                        Err(BuildChainInformationError::ChainInformationBuild(err)),
                    );
                }
                chain_information::build::ChainInformationBuild::InProgress(in_progress) => {
                    in_progress
                }
            };

            chain_info_builder = match in_progress {
                chain_information::build::InProgress::StorageGet(get) => {
                    // TODO: child tries not supported
                    let (proof, downloaded_source) = calls.get(&get.call_in_progress()).unwrap();
                    let value = match proof
                        .storage_value(&self.inner.warped_header_state_root, get.key().as_ref())
                    {
                        Ok(v) => v,
                        Err(proof_decode::IncompleteProofError { .. }) => {
                            return (
                                self.inner,
                                Err(BuildChainInformationError::SourceMisbehavior(
                                    SourceMisbehavior {
                                        source_id: *downloaded_source,
                                        error: SourceMisbehaviorTy::MerkleProofEntriesMissing,
                                    },
                                )),
                            );
                        }
                    };

                    get.inject_value(value.map(|(val, ver)| (iter::once(val), ver)))
                }
                chain_information::build::InProgress::NextKey(nk) => {
                    // TODO: child tries not supported
                    let (proof, downloaded_source) = calls.get(&nk.call_in_progress()).unwrap();
                    let value = match proof.next_key(
                        &self.inner.warped_header_state_root,
                        nk.key(),
                        nk.or_equal(),
                        nk.prefix(),
                        nk.branch_nodes(),
                    ) {
                        Ok(v) => v,
                        Err(proof_decode::IncompleteProofError { .. }) => {
                            return (
                                self.inner,
                                Err(BuildChainInformationError::SourceMisbehavior(
                                    SourceMisbehavior {
                                        source_id: *downloaded_source,
                                        error: SourceMisbehaviorTy::MerkleProofEntriesMissing,
                                    },
                                )),
                            );
                        }
                    };
                    nk.inject_key(value)
                }
                chain_information::build::InProgress::ClosestDescendantMerkleValue(mv) => {
                    // TODO: child tries not supported
                    let (proof, downloaded_source) = calls.get(&mv.call_in_progress()).unwrap();
                    let value = match proof.closest_descendant_merkle_value(
                        &self.inner.warped_header_state_root,
                        mv.key(),
                    ) {
                        Ok(v) => v,
                        Err(proof_decode::IncompleteProofError { .. }) => {
                            return (
                                self.inner,
                                Err(BuildChainInformationError::SourceMisbehavior(
                                    SourceMisbehavior {
                                        source_id: *downloaded_source,
                                        error: SourceMisbehaviorTy::MerkleProofEntriesMissing,
                                    },
                                )),
                            );
                        }
                    };
                    mv.inject_merkle_value(value)
                }
            };
        }
    }
}

/// Returns `true` if `a` and `b` are equal.
fn parameters_equal(mut a: &[u8], b: impl Iterator<Item = impl AsRef<[u8]>>) -> bool {
    for slice in b {
        let slice = slice.as_ref();

        if a.len() < slice.len() {
            return false;
        }

        if &a[..slice.len()] != slice {
            return false;
        }

        a = &a[slice.len()..];
    }

    true
}