pop-fork 0.13.0

Library for forking live Substrate chains.
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
// SPDX-License-Identifier: GPL-3.0

//! SQLite-based storage cache for fork operations.
//!
//! Provides persistent caching of storage values fetched from live chains,
//! enabling fast restarts and reducing RPC calls.

use crate::{
	error::cache::CacheError,
	models::{
		BlockRow, LocalKeyRow, NewBlockRow, NewLocalKeyRow, NewLocalValueRow, NewPrefixScanRow,
		NewStorageRow,
	},
	schema::{blocks, local_keys, local_values, prefix_scans, storage},
	strings::cache::{errors, lock_patterns, pragmas, urls},
};
use bb8::CustomizeConnection;
use diesel::{
	OptionalExtension, prelude::*, result::Error as DieselError, sqlite::SqliteConnection,
};
use diesel_async::{
	AsyncConnection, AsyncMigrationHarness, RunQueryDsl,
	pooled_connection::{
		AsyncDieselConnectionManager, PoolError,
		bb8::{Pool, PooledConnection},
	},
	sync_connection_wrapper::SyncConnectionWrapper,
};
use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations};
use std::{
	collections::{HashMap, HashSet},
	future::Future,
	ops::{Deref, DerefMut},
	path::Path,
	pin::Pin,
	sync::Arc,
	time::Duration,
};
use subxt::config::substrate::H256;
use tokio::sync::{Mutex, MutexGuard};

/// Maximum number of connections in the SQLite connection pool.
///
/// Since Pop is the only process accessing the database, this is for internal
/// async task concurrency. 5 provides comfortable headroom for parallel operations
/// while remaining lightweight on end-user devices.
const MAX_POOL_CONNECTIONS: u32 = 5;
/// Maximum retries for transient SQLite lock/busy errors on write paths.
const MAX_LOCK_RETRIES: u32 = 30;

/// Progress information for a prefix scan operation.
///
/// Tracks the state of an incremental prefix scan, enabling resumable
/// operations that can be interrupted and continued later.
///
/// # Lifecycle
///
/// 1. **Not started**: `get_prefix_scan_progress()` returns `None`
/// 2. **In progress**: `is_complete = false`, `last_scanned_key` holds the resume point
/// 3. **Completed**: `is_complete = true`, all keys for the prefix have been scanned
#[derive(Debug, Clone)]
pub struct PrefixScanProgress {
	/// The last key that was successfully scanned.
	/// Used as the starting point when resuming an interrupted scan.
	pub last_scanned_key: Option<Vec<u8>>,
	/// Whether the scan has processed all keys matching the prefix.
	pub is_complete: bool,
}

/// SQLite-backed persistent cache for storage values.
///
/// Enables fast restarts without re-fetching all data from live chains
/// and reduces load on public RPC endpoints.
#[derive(Clone, Debug)]
pub struct StorageCache {
	inner: StorageConn,
}

/// Internal connection wrapper for the storage cache.
#[derive(Clone)]
enum StorageConn {
	/// For file-based databases, uses a connection pool to enable concurrent access
	/// from multiple async tasks. This is more efficient for persistent storage where multiple
	/// operations may run in parallel.
	Pool(Pool<SyncConnectionWrapper<SqliteConnection>>),
	/// For in-memory databases, uses a single shared connection protected by a mutex.
	/// In-memory databases don't benefit from connection pools since all connections share the
	/// same memory state.
	Single(Arc<Mutex<SyncConnectionWrapper<SqliteConnection>>>),
}

impl std::fmt::Debug for StorageConn {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		match self {
			StorageConn::Pool(_) => f.debug_tuple("Pool").field(&"...").finish(),
			StorageConn::Single(_) => f.debug_tuple("Single").field(&"...").finish(),
		}
	}
}

/// Connection guard that handles both pool and single connection types.
///
/// Automatically returns the connection to the pool or unlocks the mutex when dropped.
pub(crate) enum ConnectionGuard<'a> {
	Pool(PooledConnection<'a, SyncConnectionWrapper<SqliteConnection>>),
	Single(MutexGuard<'a, SyncConnectionWrapper<SqliteConnection>>),
}

impl<'a> Deref for ConnectionGuard<'a> {
	type Target = SyncConnectionWrapper<SqliteConnection>;

	fn deref(&self) -> &Self::Target {
		match self {
			ConnectionGuard::Pool(conn) => conn,
			ConnectionGuard::Single(guard) => guard,
		}
	}
}

impl<'a> DerefMut for ConnectionGuard<'a> {
	fn deref_mut(&mut self) -> &mut Self::Target {
		match self {
			ConnectionGuard::Pool(conn) => conn,
			ConnectionGuard::Single(guard) => guard,
		}
	}
}

/// Increments the attempt counter and sleeps with linear backoff.
///
/// Uses a simple linear backoff strategy: delay = 10ms * attempt_number.
/// This gives the database time to release locks while avoiding excessive delays.
async fn retry_conn(attempts: &mut u32) {
	*attempts += 1;
	let delay_ms = 10u64.saturating_mul(*attempts as u64);
	tokio::time::sleep(Duration::from_millis(delay_ms)).await;
}

/// Connection customizer that sets SQLite pragmas on each pooled connection.
#[derive(Debug, Clone, Copy)]
struct SqliteConnectionCustomizer;

impl CustomizeConnection<SyncConnectionWrapper<SqliteConnection>, PoolError>
	for SqliteConnectionCustomizer
{
	fn on_acquire<'a>(
		&'a self,
		conn: &'a mut SyncConnectionWrapper<SqliteConnection>,
	) -> Pin<Box<dyn Future<Output = Result<(), PoolError>> + Send + 'a>> {
		Box::pin(async move {
			// Set busy timeout to reduce lock errors under contention
			diesel::sql_query(pragmas::BUSY_TIMEOUT)
				.execute(conn)
				.await
				.map_err(PoolError::QueryError)?;
			Ok(())
		})
	}
}

impl StorageCache {
	/// Open or create a cache database at the specified path.
	///
	/// Creates the parent directory if it doesn't exist.
	pub async fn open(maybe_path: Option<&Path>) -> Result<Self, CacheError> {
		// For in-memory open a single dedicated connection; for file path use a pool.
		if let Some(path) = maybe_path {
			// Ensure parent directory exists
			if let Some(parent) = path.parent() {
				std::fs::create_dir_all(parent)?;
			}
			let url = path.display().to_string();

			// Run migrations on a temporary async connection first
			{
				let mut conn = SyncConnectionWrapper::<SqliteConnection>::establish(&url).await?;
				// Apply pragmas for better concurrency on file databases
				// WAL mode: Persists to the database file itself
				diesel::sql_query(pragmas::JOURNAL_MODE_WAL).execute(&mut conn).await?;
				// Busy timeout: For this migration connection
				diesel::sql_query(pragmas::BUSY_TIMEOUT).execute(&mut conn).await?;
				let mut harness = AsyncMigrationHarness::new(conn);
				harness.run_pending_migrations(MIGRATIONS)?;
				let _ = harness.into_inner();
			}

			// Build the pool with connection customizer
			let manager =
				AsyncDieselConnectionManager::<SyncConnectionWrapper<SqliteConnection>>::new(url);
			let pool = Pool::builder()
				.max_size(MAX_POOL_CONNECTIONS)
				.connection_customizer(Box::new(SqliteConnectionCustomizer))
				.build(manager)
				.await?;
			Ok(Self { inner: StorageConn::Pool(pool) })
		} else {
			// Single in-memory connection
			let mut conn =
				SyncConnectionWrapper::<SqliteConnection>::establish(urls::IN_MEMORY).await?;
			// Run migrations on this single connection
			// Set busy timeout to reduce lock errors under contention
			diesel::sql_query(pragmas::BUSY_TIMEOUT).execute(&mut conn).await?;
			let mut harness = AsyncMigrationHarness::new(conn);
			harness.run_pending_migrations(MIGRATIONS)?;
			let conn = harness.into_inner();
			Ok(Self {
				inner: StorageConn::Single(std::sync::Arc::new(tokio::sync::Mutex::new(conn))),
			})
		}
	}

	/// Open an in-memory cache.
	///
	/// Creates a fresh in-memory SQLite database and runs all migrations
	/// to set up the storage and blocks tables.
	pub async fn in_memory() -> Result<Self, CacheError> {
		Self::open(None).await
	}

	/// Get a database connection.
	///
	/// Handles acquiring the connection from either the pool or single mutex.
	/// The connection is automatically returned to the pool or unlocks the mutex when dropped.
	pub(crate) async fn get_conn(&self) -> Result<ConnectionGuard<'_>, CacheError> {
		match &self.inner {
			StorageConn::Pool(pool) => {
				let conn = pool.get().await.map_err(|e| {
					CacheError::Connection(ConnectionError::BadConnection(e.to_string()))
				})?;
				Ok(ConnectionGuard::Pool(conn))
			},
			StorageConn::Single(m) => {
				let conn = m.lock().await;
				Ok(ConnectionGuard::Single(conn))
			},
		}
	}

	/// Get a cached storage value.
	///
	/// # Returns
	/// * `Ok(Some(Some(value)))` - Cached with a value.
	/// * `Ok(Some(None))` - Cached as empty (storage key exists but has no value).
	/// * `Ok(None)` - Not in cache (unknown).
	pub async fn get_storage(
		&self,
		block_hash: H256,
		key: &[u8],
	) -> Result<Option<Option<Vec<u8>>>, CacheError> {
		// Retrieve the cached value and its empty flag for the given block and key.
		// We need both `value` and `is_empty` to distinguish between:
		// - Key not in cache (no row returned)
		// - Key cached as empty (row exists, is_empty = true)
		// - Key cached with value (row exists, is_empty = false)
		use crate::schema::storage::columns as sc;

		let mut conn = self.get_conn().await?;

		let row: Option<(Option<Vec<u8>>, bool)> = storage::table
			.filter(sc::block_hash.eq(block_hash.as_bytes()))
			.filter(sc::key.eq(key))
			.select((sc::value, sc::is_empty))
			.first::<(Option<Vec<u8>>, bool)>(&mut conn)
			.await
			.optional()?;

		Ok(row.map(|(val, empty)| if empty { None } else { val }))
	}

	/// Cache a storage value.
	///
	/// # Arguments
	/// * `block_hash` - The block hash this storage is from
	/// * `key` - The storage key
	/// * `value` - The storage value, or None if the key has no value (empty)
	pub async fn set_storage(
		&self,
		block_hash: H256,
		key: &[u8],
		value: Option<&[u8]>,
	) -> Result<(), CacheError> {
		// Insert or update the cached storage entry with simple retry on lock contention.
		use crate::schema::storage::columns as sc;

		// Retry loop for transient SQLite lock/busy errors.
		// SQLite may return SQLITE_BUSY when another connection holds a lock.
		// We retry up to MAX_LOCK_RETRIES times with increasing backoff delays.
		let mut attempts = 0;
		loop {
			let mut conn = self.get_conn().await?;

			let row = NewStorageRow {
				block_hash: block_hash.as_bytes(),
				key,
				value,
				is_empty: value.is_none(),
			};

			let res = diesel::insert_into(storage::table)
				.values(&row)
				.on_conflict((sc::block_hash, sc::key))
				.do_update()
				.set((sc::value.eq(value), sc::is_empty.eq(row.is_empty)))
				.execute(&mut conn)
				.await;

			match res {
				Ok(_) => return Ok(()),
				Err(e) if is_locked_error(&e) && attempts < MAX_LOCK_RETRIES => {
					retry_conn(&mut attempts).await;
					continue;
				},
				Err(e) => return Err(e.into()),
			}
		}
	}

	/// Get multiple cached storage values in a batch.
	///
	/// Returns results in the same order as the input keys.
	pub async fn get_storage_batch(
		&self,
		block_hash: H256,
		keys: &[&[u8]],
	) -> Result<Vec<Option<Option<Vec<u8>>>>, CacheError> {
		if keys.is_empty() {
			return Ok(vec![]);
		}

		let mut seen = HashSet::with_capacity(keys.len());
		if keys.iter().any(|key| !seen.insert(key)) {
			return Err(CacheError::DuplicatedKeys);
		}

		use crate::schema::storage::columns as sc;
		let mut conn = self.get_conn().await?;

		let rows: Vec<(Vec<u8>, Option<Vec<u8>>, bool)> = storage::table
			.filter(sc::block_hash.eq(block_hash.as_bytes()))
			.filter(sc::key.eq_any(keys))
			.select((sc::key, sc::value, sc::is_empty))
			.load::<(Vec<u8>, Option<Vec<u8>>, bool)>(&mut conn)
			.await?;

		// Build a map from the results. SQLite doesn't guarantee result order matches
		// the IN clause order, so we use a HashMap to look up values by key.
		let mut cache_map = HashMap::new();
		for (key, value, empty) in rows {
			let value = if empty { None } else { value };
			cache_map.insert(key, value);
		}

		// Return values in the same order as input keys.
		// Keys not found in cache_map (not in DB) return None (not cached).
		// Keys found return Some(value) where value is None for empty or Some(bytes) for data.
		Ok(keys.iter().map(|key| cache_map.remove(*key)).collect())
	}

	/// Cache multiple storage values in a batch.
	///
	/// Uses a transaction for efficiency.
	pub async fn set_storage_batch(
		&self,
		block_hash: H256,
		entries: &[(&[u8], Option<&[u8]>)],
	) -> Result<(), CacheError> {
		if entries.is_empty() {
			return Ok(());
		}

		let mut seen = HashSet::with_capacity(entries.len());
		if entries.iter().any(|(key, _)| !seen.insert(key)) {
			return Err(CacheError::DuplicatedKeys);
		}

		// Use a transaction to batch all inserts together.
		// This is significantly faster than individual inserts because:
		// 1. SQLite commits are expensive (fsync to disk)
		// 2. A transaction groups all inserts into a single commit
		// 3. If any insert fails, the entire batch is rolled back
		use crate::schema::storage::columns as sc;
		let entries = Arc::new(entries);
		let block_hash = Arc::new(block_hash);

		// Retry loop for transient SQLite lock/busy errors.
		// SQLite may return SQLITE_BUSY when another connection holds a lock.
		// We retry up to MAX_LOCK_RETRIES (30) times with increasing backoff delays.
		let mut attempts = 0;
		loop {
			let entries = Arc::clone(&entries);
			let block_hash = Arc::clone(&block_hash);
			let mut conn = self.get_conn().await?;
			let res = conn
				.transaction::<_, DieselError, _>(move |conn| {
					Box::pin(async move {
						let new_rows: Vec<NewStorageRow> = entries
							.iter()
							.map(|(key, value)| NewStorageRow {
								block_hash: block_hash.as_bytes(),
								key,
								value: *value,
								is_empty: value.is_none(),
							})
							.collect();
						for row in new_rows {
							diesel::insert_into(storage::table)
								.values(&row)
								.on_conflict((sc::block_hash, sc::key))
								.do_update()
								.set((sc::value.eq(row.value), sc::is_empty.eq(row.is_empty)))
								.execute(conn)
								.await?;
						}
						Ok(())
					})
				})
				.await;

			match res {
				Ok(_) => return Ok(()),
				Err(e) if is_locked_error(&e) && attempts < MAX_LOCK_RETRIES => {
					retry_conn(&mut attempts).await;
					continue;
				},
				Err(e) => return Err(e.into()),
			}
		}
	}

	/// Get a local key's ID from the local_keys table.
	///
	/// # Returns
	/// * `Ok(Some(key_row))` - Key exists with its ID
	/// * `Ok(None)` - Key not in local_keys table
	pub async fn get_local_key(&self, key: &[u8]) -> Result<Option<LocalKeyRow>, CacheError> {
		use crate::schema::local_keys::columns as lkc;

		let mut conn = self.get_conn().await?;

		let row = local_keys::table
			.filter(lkc::key.eq(key))
			.select(LocalKeyRow::as_select())
			.first(&mut conn)
			.await
			.optional()?;

		Ok(row)
	}

	/// Insert a new key into local_keys and return its ID.
	///
	/// If the key already exists, returns the existing ID.
	pub async fn insert_local_key(&self, key: &[u8]) -> Result<i32, CacheError> {
		use crate::schema::local_keys::columns as lkc;

		let mut attempts = 0;
		loop {
			let mut conn = self.get_conn().await?;

			// Try to insert, ignore conflict (key already exists)
			let res = diesel::insert_into(local_keys::table)
				.values(NewLocalKeyRow { key })
				.on_conflict(lkc::key)
				.do_nothing()
				.execute(&mut conn)
				.await;

			match res {
				Ok(_) => {
					// Fetch the ID (either just inserted or already existed)
					let key_id: i32 = local_keys::table
						.filter(lkc::key.eq(key))
						.select(lkc::id)
						.first(&mut conn)
						.await?;
					return Ok(key_id);
				},
				Err(e) if is_locked_error(&e) && attempts < MAX_LOCK_RETRIES => {
					retry_conn(&mut attempts).await;
					continue;
				},
				Err(e) => return Err(e.into()),
			}
		}
	}

	/// Get a local storage value valid at a specific block number.
	///
	/// Queries the local_values table for a value that is valid at the given block:
	/// valid_from <= block_number AND (valid_until IS NULL OR valid_until > block_number)
	///
	/// # Returns
	/// * `Ok(Some(Some(value)))` - Value found with data
	/// * `Ok(Some(None))` - Value found but explicitly deleted (NULL in DB)
	/// * `Ok(None)` - No value valid at this block (no row found)
	pub async fn get_local_value_at_block(
		&self,
		key: &[u8],
		block_number: u32,
	) -> Result<Option<Option<Vec<u8>>>, CacheError> {
		use crate::schema::{local_keys::columns as lkc, local_values::columns as lvc};

		let mut conn = self.get_conn().await?;
		let block_num = block_number as i64;

		// First get the key_id
		let key_id: i32 = match local_keys::table
			.filter(lkc::key.eq(key))
			.select(lkc::id)
			.first(&mut conn)
			.await
			.optional()?
		{
			Some(id) => id,
			_ => return Ok(None),
		};

		// Query for value valid at block_number (value can be NULL for deletions)
		let value: Option<Option<Vec<u8>>> = local_values::table
			.filter(lvc::key_id.eq(key_id))
			.filter(lvc::valid_from.le(block_num))
			.filter(lvc::valid_until.is_null().or(lvc::valid_until.gt(block_num)))
			.select(lvc::value)
			.first(&mut conn)
			.await
			.optional()?;

		Ok(value)
	}

	/// Get multiple local storage values valid at a specific block number.
	///
	/// Returns results in the same order as the input keys.
	/// * `Some(Some(value))` - Value found with data
	/// * `Some(None)` - Value found but explicitly deleted (NULL in DB)
	/// * `None` - No value valid at this block (no row found)
	pub async fn get_local_values_at_block_batch(
		&self,
		keys: &[&[u8]],
		block_number: u32,
	) -> Result<Vec<Option<Option<Vec<u8>>>>, CacheError> {
		if keys.is_empty() {
			return Ok(vec![]);
		}

		let mut seen = HashSet::with_capacity(keys.len());
		if keys.iter().any(|key| !seen.insert(key)) {
			return Err(CacheError::DuplicatedKeys);
		}

		use crate::schema::{local_keys::columns as lkc, local_values::columns as lvc};

		let mut conn = self.get_conn().await?;
		let block_num = block_number as i64;

		// Get all key_ids for the requested keys
		let key_rows: Vec<LocalKeyRow> = local_keys::table
			.filter(lkc::key.eq_any(keys))
			.select(LocalKeyRow::as_select())
			.load(&mut conn)
			.await?;

		// Build a map from key bytes to key_id
		let key_to_id: HashMap<Vec<u8>, i32> =
			key_rows.iter().map(|r| (r.key.clone(), r.id)).collect();

		// Get all key_ids that exist
		let key_ids: Vec<i32> = key_to_id.values().copied().collect();

		if key_ids.is_empty() {
			return Ok(vec![None; keys.len()]);
		}

		// Query for values valid at block_number for all key_ids (value can be NULL for deletions)
		let value_rows: Vec<(i32, Option<Vec<u8>>)> = local_values::table
			.filter(lvc::key_id.eq_any(&key_ids))
			.filter(lvc::valid_from.le(block_num))
			.filter(lvc::valid_until.is_null().or(lvc::valid_until.gt(block_num)))
			.select((lvc::key_id, lvc::value))
			.load(&mut conn)
			.await?;

		// Build a map from key_id to value (Option<Vec<u8>> to track deletions)
		let mut id_to_value: HashMap<i32, Option<Vec<u8>>> = HashMap::new();
		for (key_id, value) in value_rows {
			id_to_value.insert(key_id, value);
		}

		// Build result in same order as input keys
		// None = key not found in local storage
		// Some(None) = key was deleted
		// Some(Some(value)) = key has a value
		Ok(keys
			.iter()
			.map(|key| key_to_id.get(*key).and_then(|key_id| id_to_value.remove(key_id)))
			.collect())
	}

	/// Get all locally-modified keys matching a prefix that existed at a specific block.
	///
	/// Joins `local_keys` with `local_values` to find keys where:
	/// - The key starts with `prefix`
	/// - A value entry is valid at `block_number` (valid_from <= block AND (valid_until IS NULL OR
	///   valid_until > block))
	/// - The value is not NULL (i.e., the key was not deleted at that block)
	///
	/// Returns a sorted list of matching keys.
	pub async fn get_local_keys_at_block(
		&self,
		prefix: &[u8],
		block_number: u32,
	) -> Result<Vec<Vec<u8>>, CacheError> {
		use crate::schema::{local_keys::columns as lkc, local_values::columns as lvc};

		let mut conn = self.get_conn().await?;
		let block_num = block_number as i64;
		let prefix_vec = prefix.to_vec();

		let mut query = local_keys::table
			.inner_join(local_values::table)
			.filter(lkc::key.ge(&prefix_vec))
			.filter(lvc::valid_from.le(block_num))
			.filter(lvc::valid_until.is_null().or(lvc::valid_until.gt(block_num)))
			.filter(lvc::value.is_not_null())
			.select(lkc::key)
			.distinct()
			.order(lkc::key.asc())
			.into_boxed();

		if let Some(upper) = Self::prefix_upper_bound(prefix) {
			query = query.filter(lkc::key.lt(upper));
		}

		let keys: Vec<Vec<u8>> = query.load(&mut conn).await?;
		Ok(keys)
	}

	/// Get all locally-deleted keys matching a prefix at a specific block.
	///
	/// Returns keys where the value entry valid at `block_number` has a NULL value
	/// (explicitly deleted). This is needed to exclude deleted keys from merged
	/// remote + local key enumeration.
	pub async fn get_local_deleted_keys_at_block(
		&self,
		prefix: &[u8],
		block_number: u32,
	) -> Result<Vec<Vec<u8>>, CacheError> {
		use crate::schema::{local_keys::columns as lkc, local_values::columns as lvc};

		let mut conn = self.get_conn().await?;
		let block_num = block_number as i64;
		let prefix_vec = prefix.to_vec();

		let mut query = local_keys::table
			.inner_join(local_values::table)
			.filter(lkc::key.ge(&prefix_vec))
			.filter(lvc::valid_from.le(block_num))
			.filter(lvc::valid_until.is_null().or(lvc::valid_until.gt(block_num)))
			.filter(lvc::value.is_null())
			.select(lkc::key)
			.distinct()
			.order(lkc::key.asc())
			.into_boxed();

		if let Some(upper) = Self::prefix_upper_bound(prefix) {
			query = query.filter(lkc::key.lt(upper));
		}

		let keys: Vec<Vec<u8>> = query.load(&mut conn).await?;
		Ok(keys)
	}

	/// Compute the exclusive upper bound for a binary prefix range query.
	///
	/// Increments the prefix to produce the first byte sequence that does NOT
	/// start with `prefix`. Returns `None` if the prefix is all `0xFF` bytes
	/// (no upper bound needed, `>=` is sufficient).
	fn prefix_upper_bound(prefix: &[u8]) -> Option<Vec<u8>> {
		let mut upper = prefix.to_vec();
		// Walk backwards, incrementing the last non-0xFF byte.
		while let Some(last) = upper.last_mut() {
			if *last < 0xFF {
				*last += 1;
				return Some(upper);
			}
			upper.pop();
		}
		// All bytes were 0xFF, no upper bound possible.
		None
	}

	/// Insert a new local value entry.
	///
	/// # Arguments
	/// * `key_id` - The key ID from local_keys table
	/// * `value` - The value bytes, or None to record a deletion
	/// * `valid_from` - Block number when this value becomes valid
	pub async fn insert_local_value(
		&self,
		key_id: i32,
		value: Option<&[u8]>,
		valid_from: u32,
	) -> Result<(), CacheError> {
		let mut attempts = 0;
		loop {
			let mut conn = self.get_conn().await?;

			let row = NewLocalValueRow {
				key_id,
				value: value.map(|v| v.to_vec()),
				valid_from: valid_from as i64,
				valid_until: None,
			};

			let res =
				diesel::insert_into(local_values::table).values(&row).execute(&mut conn).await;

			match res {
				Ok(_) => return Ok(()),
				Err(e) if is_locked_error(&e) && attempts < MAX_LOCK_RETRIES => {
					retry_conn(&mut attempts).await;
					continue;
				},
				Err(e) => return Err(e.into()),
			}
		}
	}

	/// Close the currently open local value entry (set valid_until).
	///
	/// Finds the entry for this key_id where valid_until IS NULL and sets it.
	pub async fn close_local_value(&self, key_id: i32, valid_until: u32) -> Result<(), CacheError> {
		use crate::schema::local_values::columns as lvc;

		let mut attempts = 0;
		loop {
			let mut conn = self.get_conn().await?;

			let res = diesel::update(
				local_values::table
					.filter(lvc::key_id.eq(key_id))
					.filter(lvc::valid_until.is_null()),
			)
			.set(lvc::valid_until.eq(Some(valid_until as i64)))
			.execute(&mut conn)
			.await;

			match res {
				Ok(_) => return Ok(()),
				Err(e) if is_locked_error(&e) && attempts < MAX_LOCK_RETRIES => {
					retry_conn(&mut attempts).await;
					continue;
				},
				Err(e) => return Err(e.into()),
			}
		}
	}

	/// Commit a batch of local storage changes in a single transaction.
	///
	/// For each entry: upserts the key into `local_keys`, closes the previous value
	/// (sets `valid_until`), and inserts the new value. Wrapping everything in one
	/// transaction avoids per-operation fsync overhead, reducing commit time from
	/// tens of seconds to sub-second.
	pub async fn commit_local_changes(
		&self,
		entries: &[(&[u8], Option<&[u8]>)],
		block_number: u32,
	) -> Result<(), CacheError> {
		use crate::schema::{local_keys::columns as lkc, local_values::columns as lvc};

		if entries.is_empty() {
			return Ok(());
		}

		// Clone entries into owned data so they can move into the async closure.
		let owned: Vec<(Vec<u8>, Option<Vec<u8>>)> =
			entries.iter().map(|(k, v)| (k.to_vec(), v.map(|val| val.to_vec()))).collect();

		let mut attempts = 0;
		loop {
			let owned = owned.clone();
			let mut conn = self.get_conn().await?;

			let res = conn
				.transaction::<_, DieselError, _>(move |conn| {
					Box::pin(async move {
						for (key, value) in &owned {
							// Upsert key
							diesel::insert_into(local_keys::table)
								.values(NewLocalKeyRow { key })
								.on_conflict(lkc::key)
								.do_nothing()
								.execute(conn)
								.await?;

							let key_id: i32 = local_keys::table
								.filter(lkc::key.eq(key.as_slice()))
								.select(lkc::id)
								.first(conn)
								.await?;

							// Close previous value
							diesel::update(
								local_values::table
									.filter(lvc::key_id.eq(key_id))
									.filter(lvc::valid_until.is_null()),
							)
							.set(lvc::valid_until.eq(Some(block_number as i64)))
							.execute(conn)
							.await?;

							// Insert new value
							let row = NewLocalValueRow {
								key_id,
								value: value.clone(),
								valid_from: block_number as i64,
								valid_until: None,
							};
							diesel::insert_into(local_values::table)
								.values(&row)
								.execute(conn)
								.await?;
						}
						Ok(())
					})
				})
				.await;

			match res {
				Ok(_) => return Ok(()),
				Err(e) if is_locked_error(&e) && attempts < MAX_LOCK_RETRIES => {
					retry_conn(&mut attempts).await;
					continue;
				},
				Err(e) => return Err(e.into()),
			}
		}
	}

	/// Clear all local storage data (both local_keys and local_values tables).
	///
	/// This removes all locally tracked key-value pairs and their validity history.
	/// Uses a transaction to ensure both tables are cleared atomically.
	pub async fn clear_local_storage(&self) -> Result<(), CacheError> {
		let mut attempts = 0;
		loop {
			let mut conn = self.get_conn().await?;

			let res = conn
				.transaction::<_, DieselError, _>(|conn| {
					Box::pin(async move {
						// Delete local_values first (has foreign key to local_keys)
						diesel::delete(local_values::table).execute(conn).await?;
						// Then delete local_keys
						diesel::delete(local_keys::table).execute(conn).await?;
						Ok(())
					})
				})
				.await;

			match res {
				Ok(_) => return Ok(()),
				Err(e) if is_locked_error(&e) && attempts < MAX_LOCK_RETRIES => {
					retry_conn(&mut attempts).await;
					continue;
				},
				Err(e) => return Err(e.into()),
			}
		}
	}

	/// Cache block metadata.
	pub async fn cache_block(
		&self,
		hash: H256,
		number: u32,
		parent_hash: H256,
		header: &[u8],
	) -> Result<(), CacheError> {
		// Store block metadata for quick lookup without hitting the remote RPC.
		use crate::schema::blocks::columns as bc;

		// Retry loop for transient SQLite lock/busy errors.
		// SQLite may return SQLITE_BUSY when another connection holds a lock.
		// We retry up to MAX_LOCK_RETRIES (30) times with increasing backoff delays.
		let mut attempts = 0;
		let parent_hash_bytes = parent_hash.as_bytes();
		loop {
			let mut conn = self.get_conn().await?;

			let block = NewBlockRow {
				hash: hash.as_bytes(),
				number: number as i64,
				parent_hash: parent_hash_bytes,
				header,
			};

			let res = diesel::insert_into(blocks::table)
				.values(&block)
				.on_conflict(bc::hash)
				.do_update()
				.set((
					bc::number.eq(number as i64),
					bc::parent_hash.eq(parent_hash_bytes),
					bc::header.eq(header),
				))
				.execute(&mut conn)
				.await;

			match res {
				Ok(_) => return Ok(()),
				Err(e) if is_locked_error(&e) && attempts < MAX_LOCK_RETRIES => {
					retry_conn(&mut attempts).await;
					continue;
				},
				Err(e) => return Err(e.into()),
			}
		}
	}

	/// Get cached block metadata.
	pub async fn get_block(&self, hash: H256) -> Result<Option<BlockRow>, CacheError> {
		// Retrieve all block metadata fields by the block's hash (primary key).
		// Returns None if the block hasn't been cached yet.
		use crate::schema::blocks::columns as bc;

		let mut conn = self.get_conn().await?;

		let row = blocks::table
			.filter(bc::hash.eq(hash.as_bytes()))
			.select(BlockRow::as_select())
			.first(&mut conn)
			.await
			.optional()?;

		match row {
			// Sanity check on the block number, as we use i64 to represent them in SQLite but
			// Substrate blocks are u32
			Some(BlockRow { number, .. }) if number < 0 || number > u32::MAX.into() =>
				Err(CacheError::DataCorruption(errors::BLOCK_NUMBER_OUT_OF_U32_RANGE.into())),
			row @ Some(_) => Ok(row),
			None => Ok(None),
		}
	}

	/// Get cached block metadata by block number.
	pub async fn get_block_by_number(
		&self,
		block_number: u32,
	) -> Result<Option<BlockRow>, CacheError> {
		// Retrieve block metadata by block number.
		// Returns None if the block hasn't been cached yet.
		use crate::schema::blocks::columns as bc;

		let mut conn = self.get_conn().await?;

		let row = blocks::table
			.filter(bc::number.eq(block_number as i64))
			.select(BlockRow::as_select())
			.first(&mut conn)
			.await
			.optional()?;

		match row {
			// Sanity check on the block number
			Some(BlockRow { number, .. }) if number < 0 || number > u32::MAX.into() =>
				Err(CacheError::DataCorruption(errors::BLOCK_NUMBER_OUT_OF_U32_RANGE.into())),
			row @ Some(_) => Ok(row),
			None => Ok(None),
		}
	}

	/// Clear all cached data for a specific block.
	pub async fn clear_block(&self, hash: H256) -> Result<(), CacheError> {
		// Use a transaction to ensure both deletes succeed or fail together.
		// This maintains consistency: we never have orphaned storage entries
		// without their parent block, or vice versa.
		use crate::schema::{
			blocks::columns as bc, prefix_scans::columns as psc, storage::columns as sc,
		};
		let block_hash = Arc::new(hash.as_bytes());

		// Retry loop for transient SQLite lock/busy errors.
		// SQLite may return SQLITE_BUSY when another connection holds a lock.
		// We retry up to MAX_LOCK_RETRIES (30) times with increasing backoff delays.
		let mut attempts = 0;
		loop {
			let block_hash = Arc::clone(&block_hash);
			let mut conn = self.get_conn().await?;

			let res = conn
				.transaction::<_, DieselError, _>(move |conn| {
					Box::pin(async move {
						diesel::delete(storage::table.filter(sc::block_hash.eq(*block_hash)))
							.execute(conn)
							.await?;
						diesel::delete(blocks::table.filter(bc::hash.eq(*block_hash)))
							.execute(conn)
							.await?;
						diesel::delete(prefix_scans::table.filter(psc::block_hash.eq(*block_hash)))
							.execute(conn)
							.await?;
						Ok(())
					})
				})
				.await;

			match res {
				Ok(_) => return Ok(()),
				Err(e) if is_locked_error(&e) && attempts < MAX_LOCK_RETRIES => {
					retry_conn(&mut attempts).await;
					continue;
				},
				Err(e) => return Err(e.into()),
			}
		}
	}

	/// Get the progress of a prefix scan operation.
	///
	/// # Returns
	/// * `Ok(Some(progress))` - Scan has been started, returns progress info
	/// * `Ok(None)` - No scan has been started for this prefix
	pub async fn get_prefix_scan_progress(
		&self,
		block_hash: H256,
		prefix: &[u8],
	) -> Result<Option<PrefixScanProgress>, CacheError> {
		use crate::schema::prefix_scans::columns as psc;

		let mut conn = self.get_conn().await?;

		let row: Option<(Option<Vec<u8>>, bool)> = prefix_scans::table
			.filter(psc::block_hash.eq(block_hash.as_bytes()))
			.filter(psc::prefix.eq(prefix))
			.select((psc::last_scanned_key, psc::is_complete))
			.first::<(Option<Vec<u8>>, bool)>(&mut conn)
			.await
			.optional()?;

		Ok(row.map(|(last_key, complete)| PrefixScanProgress {
			last_scanned_key: last_key,
			is_complete: complete,
		}))
	}

	/// Update the progress of a prefix scan operation (upsert).
	///
	/// Creates a new progress record or updates an existing one. Uses SQLite's
	/// `ON CONFLICT DO UPDATE` for atomic upsert semantics.
	///
	/// # Arguments
	/// * `block_hash` - The block hash being scanned
	/// * `prefix` - The storage prefix being scanned
	/// * `last_key` - The last key that was processed
	/// * `is_complete` - Whether the scan has finished
	pub async fn update_prefix_scan(
		&self,
		block_hash: H256,
		prefix: &[u8],
		last_key: &[u8],
		is_complete: bool,
	) -> Result<(), CacheError> {
		use crate::schema::prefix_scans::columns as psc;
		use diesel::upsert::excluded;

		let new_row = NewPrefixScanRow {
			block_hash: block_hash.as_bytes(),
			prefix,
			last_scanned_key: Some(last_key),
			is_complete,
		};

		let mut attempts = 0;
		loop {
			let mut conn = self.get_conn().await?;
			let res = diesel::insert_into(prefix_scans::table)
				.values(&new_row)
				.on_conflict((psc::block_hash, psc::prefix))
				.do_update()
				.set((
					psc::last_scanned_key.eq(excluded(psc::last_scanned_key)),
					psc::is_complete.eq(excluded(psc::is_complete)),
				))
				.execute(&mut conn)
				.await;

			match res {
				Ok(_) => return Ok(()),
				Err(e) if is_locked_error(&e) && attempts < MAX_LOCK_RETRIES => {
					retry_conn(&mut attempts).await;
					continue;
				},
				Err(e) => return Err(e.into()),
			}
		}
	}

	/// Get all cached keys matching a prefix.
	///
	/// Uses a range query (`key >= prefix AND key < prefix+1`) for efficient
	/// prefix matching on SQLite's B-tree index. This is more performant than
	/// `LIKE` or `GLOB` patterns for binary key prefixes.
	pub async fn get_keys_by_prefix(
		&self,
		block_hash: H256,
		prefix: &[u8],
	) -> Result<Vec<Vec<u8>>, CacheError> {
		use crate::schema::storage::columns as sc;

		let mut conn = self.get_conn().await?;

		// SQLite BLOB comparison with >= and < for prefix range
		let prefix_end = increment_prefix(prefix);

		let mut query = storage::table
			.filter(sc::block_hash.eq(block_hash.as_bytes()))
			.filter(sc::key.ge(prefix))
			.select(sc::key)
			.into_boxed();

		if let Some(ref end) = prefix_end {
			query = query.filter(sc::key.lt(end));
		}

		Ok(query.load::<Vec<u8>>(&mut conn).await?)
	}

	/// Find the next cached key after `key` that matches `prefix`.
	///
	/// Uses a range query (`key > current AND key >= prefix AND key < prefix+1`)
	/// for efficient lookup on SQLite's B-tree index.
	///
	/// # Returns
	/// * `Ok(Some(next_key))` - The next key after `key` matching the prefix
	/// * `Ok(None)` - No more keys with this prefix after `key`
	pub async fn next_key_from_cache(
		&self,
		block_hash: H256,
		prefix: &[u8],
		key: &[u8],
	) -> Result<Option<Vec<u8>>, CacheError> {
		use crate::schema::storage::columns as sc;

		let mut conn = self.get_conn().await?;
		let prefix_end = increment_prefix(prefix);

		let mut query = storage::table
			.filter(sc::block_hash.eq(block_hash.as_bytes()))
			.filter(sc::key.gt(key))
			.filter(sc::key.ge(prefix))
			.filter(sc::is_empty.eq(false))
			.select(sc::key)
			.order(sc::key.asc())
			.limit(1)
			.into_boxed();

		if let Some(ref end) = prefix_end {
			query = query.filter(sc::key.lt(end));
		}

		Ok(query.first::<Vec<u8>>(&mut conn).await.optional()?)
	}

	/// Count cached keys matching a prefix.
	///
	/// Uses the same range query strategy as [`Self::get_keys_by_prefix`] for
	/// efficient counting without loading key data.
	pub async fn count_keys_by_prefix(
		&self,
		block_hash: H256,
		prefix: &[u8],
	) -> Result<usize, CacheError> {
		use crate::schema::storage::columns as sc;

		let mut conn = self.get_conn().await?;
		let prefix_end = increment_prefix(prefix);

		let mut query = storage::table
			.filter(sc::block_hash.eq(block_hash.as_bytes()))
			.filter(sc::key.ge(prefix))
			.into_boxed();

		if let Some(ref end) = prefix_end {
			query = query.filter(sc::key.lt(end));
		}

		let count: i64 = query.count().get_result(&mut conn).await?;

		Ok(count as usize)
	}
}

/// Increment a byte slice to get the exclusive upper bound for prefix queries.
/// Returns None if the prefix is all 0xFF bytes (no upper bound needed).
fn increment_prefix(prefix: &[u8]) -> Option<Vec<u8>> {
	let mut result = prefix.to_vec();
	// Find the rightmost byte that isn't 0xFF and increment it
	for i in (0..result.len()).rev() {
		if result[i] < 0xFF {
			result[i] += 1;
			result.truncate(i + 1);
			return Some(result);
		}
	}
	// All bytes were 0xFF, no upper bound
	None
}

fn is_locked_error(e: &DieselError) -> bool {
	match e {
		DieselError::DatabaseError(_, info) => {
			let msg = info.message().to_ascii_lowercase();
			msg.contains(lock_patterns::DATABASE_IS_LOCKED) || msg.contains(lock_patterns::BUSY)
		},
		_ => false,
	}
}

// Embed Diesel migrations located at `crates/pop-fork/migrations`
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");

#[cfg(test)]
mod tests {
	use super::*;

	#[tokio::test(flavor = "multi_thread")]
	async fn in_memory_cache_works() {
		let cache = StorageCache::in_memory().await.unwrap();

		let block_hash = H256::from([1u8; 32]);
		let key = b"test_key";
		let value = b"test_value";

		// Initially not cached
		assert!(cache.get_storage(block_hash, key).await.unwrap().is_none());

		// Set a value
		cache.set_storage(block_hash, key, Some(value)).await.unwrap();

		// Now cached with value
		let cached = cache.get_storage(block_hash, key).await.unwrap();
		assert_eq!(cached, Some(Some(value.to_vec())));
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn cache_empty_value() {
		let cache = StorageCache::in_memory().await.unwrap();

		let block_hash = H256::from([2u8; 32]);
		let key = b"empty_key";

		// Set as empty (key exists but no value)
		cache.set_storage(block_hash, key, None).await.unwrap();

		// Cached as empty
		let cached = cache.get_storage(block_hash, key).await.unwrap();
		assert_eq!(cached, Some(None));
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn batch_operations() {
		let cache = StorageCache::in_memory().await.unwrap();

		let block_hash = H256::from([3u8; 32]);
		let entries: Vec<(&[u8], Option<&[u8]>)> = vec![
			(b"key1", Some(b"value1")),
			(b"key2", Some(b"value2")),
			(b"key3", None), // empty
		];

		// Batch set
		cache.set_storage_batch(block_hash, &entries).await.unwrap();

		// Batch get
		let keys: Vec<&[u8]> = vec![b"key1", b"key2", b"key3", b"key4"];
		let results = cache.get_storage_batch(block_hash, &keys).await.unwrap();

		assert_eq!(results.len(), 4);
		assert_eq!(results[0], Some(Some(b"value1".to_vec())));
		assert_eq!(results[1], Some(Some(b"value2".to_vec())));
		assert_eq!(results[2], Some(None)); // empty
		assert_eq!(results[3], None); // not cached
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn block_caching() {
		let cache = StorageCache::in_memory().await.unwrap();

		let hash = H256::from([4u8; 32]);
		let parent_hash = H256::from([3u8; 32]);
		let header = b"mock_header_data";

		// Cache block
		cache.cache_block(hash, 100, parent_hash, header).await.unwrap();

		// Get block
		let block = cache.get_block(hash).await.unwrap().unwrap();
		assert_eq!(block.hash, hash.as_bytes().to_vec());
		assert_eq!(block.number, 100i64);
		assert_eq!(block.parent_hash, parent_hash.as_bytes().to_vec());
		assert_eq!(block.header, header.to_vec());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_block_with_non_cached_block() {
		let cache = StorageCache::in_memory().await.unwrap();

		let hash = H256::from([4u8; 32]);

		// Get block
		let block = cache.get_block(hash).await.unwrap();

		assert!(block.is_none());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_block_number_corrupted_block_number_fails() {
		let cache = StorageCache::in_memory().await.unwrap();

		let hash1 = H256::from([4u8; 32]);
		let hash2 = H256::from([5u8; 32]);
		let parent_hash = H256::from([3u8; 32]);
		let header = b"mock_header_data";

		// Manually insert invalid block with negative number directly into database
		let invalid_block1 = NewBlockRow {
			hash: hash1.as_bytes(),
			number: -1, // Invalid: below 0
			parent_hash: parent_hash.as_bytes(),
			header,
		};

		// Manually insert invalid block with number above the u32 maximum directly into database
		let invalid_block2 = NewBlockRow {
			hash: hash2.as_bytes(),
			number: u32::MAX as i64 + 1,
			parent_hash: parent_hash.as_bytes(),
			header,
		};

		// Insert directly into the database bypassing validation
		match &cache.inner {
			StorageConn::Single(m) => {
				let mut conn = m.lock().await;
				for block in [invalid_block1, invalid_block2] {
					diesel::insert_into(blocks::table)
						.values(&block)
						.execute(&mut *conn)
						.await
						.unwrap();
				}
			},
			_ => unreachable!("Test single connection; qed;"),
		}

		// Get block should fail with DataCorruption error
		assert!(
			matches!(cache.get_block(hash1).await, Err(CacheError::DataCorruption(msg)) if msg == errors::BLOCK_NUMBER_OUT_OF_U32_RANGE)
		);
		assert!(
			matches!(cache.get_block(hash2).await, Err(CacheError::DataCorruption(msg)) if msg == errors::BLOCK_NUMBER_OUT_OF_U32_RANGE)
		);
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn different_blocks_have_separate_storage() {
		let cache = StorageCache::in_memory().await.unwrap();

		let block1 = H256::from([5u8; 32]);
		let block2 = H256::from([6u8; 32]);
		let key = b"same_key";

		cache.set_storage(block1, key, Some(b"value1")).await.unwrap();
		cache.set_storage(block2, key, Some(b"value2")).await.unwrap();

		let cached1 = cache.get_storage(block1, key).await.unwrap();
		let cached2 = cache.get_storage(block2, key).await.unwrap();

		assert_eq!(cached1, Some(Some(b"value1".to_vec())));
		assert_eq!(cached2, Some(Some(b"value2".to_vec())));
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn clear_block_removes_data() {
		let cache = StorageCache::in_memory().await.unwrap();

		let hash = H256::from([7u8; 32]);
		let parent_hash = H256::from([6u8; 32]);
		let key = b"test_key";

		cache.set_storage(hash, key, Some(b"value")).await.unwrap();
		cache.cache_block(hash, 50, parent_hash, b"header").await.unwrap();

		// Data exists
		assert!(cache.get_storage(hash, key).await.unwrap().is_some());
		assert!(cache.get_block(hash).await.unwrap().is_some());

		// Clear
		cache.clear_block(hash).await.unwrap();

		// Data removed
		assert!(cache.get_storage(hash, key).await.unwrap().is_none());
		assert!(cache.get_block(hash).await.unwrap().is_none());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn file_persistence() {
		let temp_dir = tempfile::tempdir().unwrap();
		let db_path = temp_dir.path().join("test_cache.db");

		let block_hash = H256::from([8u8; 32]);
		let key = b"persistent_key";
		let value = b"persistent_value";

		// Write and close
		{
			let cache = StorageCache::open(Some(&db_path)).await.unwrap();
			cache.set_storage(block_hash, key, Some(value)).await.unwrap();
		}

		// Reopen and verify
		{
			let cache = StorageCache::open(Some(&db_path)).await.unwrap();
			let cached = cache.get_storage(block_hash, key).await.unwrap();
			assert_eq!(cached, Some(Some(value.to_vec())));
		}
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn concurrent_access() {
		let temp_dir = tempfile::tempdir().unwrap();
		let db_path = temp_dir.path().join("concurrent_test.db");
		let cache = StorageCache::open(Some(&db_path)).await.unwrap();

		let block_hash = H256::from([9u8; 32]);

		// Spawn multiple concurrent write tasks
		// StorageCache is cheap to clone (just increments pool's reference count)
		let mut handles = vec![];
		for i in 0..10u8 {
			let cache = cache.clone();
			let handle = tokio::spawn(async move {
				let key = format!("key_{}", i);
				let value = format!("value_{}", i);
				cache.set_storage(block_hash, key.as_bytes(), Some(value.as_bytes())).await
			});
			handles.push(handle);
		}

		// Wait for all writes to complete
		for handle in handles {
			handle.await.unwrap().unwrap();
		}

		// Spawn concurrent read tasks
		let mut read_handles = vec![];
		for i in 0..10u8 {
			let cache = cache.clone();
			let handle = tokio::spawn(async move {
				let key = format!("key_{}", i);
				cache.get_storage(block_hash, key.as_bytes()).await
			});
			read_handles.push((i, handle));
		}

		// Verify all reads return correct values
		for (i, handle) in read_handles {
			let result = handle.await.unwrap().unwrap();
			let expected_value = format!("value_{}", i);
			assert_eq!(result, Some(Some(expected_value.into_bytes())));
		}

		// Test concurrent batch operations
		let cache1 = cache.clone();
		let cache2 = cache.clone();
		let block_hash2 = H256::from([10u8; 32]);

		let batch_handle1 = tokio::spawn(async move {
			let keys: Vec<Vec<u8>> = (0..5).map(|i| format!("batch1_{}", i).into_bytes()).collect();
			let values: Vec<Vec<u8>> = (0..5).map(|i| vec![i]).collect();
			let entries: Vec<(&[u8], Option<&[u8]>)> = keys
				.iter()
				.zip(values.iter())
				.map(|(k, v)| (k.as_slice(), Some(v.as_slice())))
				.collect();
			cache1.set_storage_batch(block_hash2, &entries).await
		});

		let batch_handle2 = tokio::spawn(async move {
			let keys: Vec<Vec<u8>> =
				(5..10).map(|i| format!("batch2_{}", i).into_bytes()).collect();
			let values: Vec<Vec<u8>> = (5..10).map(|i| vec![i]).collect();
			let entries: Vec<(&[u8], Option<&[u8]>)> = keys
				.iter()
				.zip(values.iter())
				.map(|(k, v)| (k.as_slice(), Some(v.as_slice())))
				.collect();
			cache2.set_storage_batch(block_hash2, &entries).await
		});

		batch_handle1.await.unwrap().unwrap();
		batch_handle2.await.unwrap().unwrap();

		// Verify batch results
		let keys: Vec<Vec<u8>> = (0..5).map(|i| format!("batch1_{}", i).into_bytes()).collect();
		let key_refs: Vec<&[u8]> = keys.iter().map(|k| k.as_slice()).collect();
		let results = cache.get_storage_batch(block_hash2, &key_refs).await.unwrap();
		for (i, result) in results.iter().enumerate() {
			assert_eq!(*result, Some(Some(vec![i as u8])));
		}
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_storage_batch_with_duplicate_keys() {
		let cache = StorageCache::in_memory().await.unwrap();

		let block_hash = H256::from([11u8; 32]);
		let entries: Vec<(&[u8], Option<&[u8]>)> = vec![
			(b"key1", Some(b"value1")),
			(b"key2", Some(b"value2")),
			(b"key3", Some(b"value3")),
		];

		// Set up some values
		cache.set_storage_batch(block_hash, &entries).await.unwrap();

		// Query with duplicate keys - key1 appears twice, key2 appears three times
		let keys: Vec<&[u8]> = vec![b"key1", b"key2", b"key1", b"key3", b"key2", b"key2"];
		let results = cache.get_storage_batch(block_hash, &keys).await;

		assert!(matches!(results, Err(CacheError::DuplicatedKeys)));
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn set_storage_batch_with_duplicate_keys() {
		let cache = StorageCache::in_memory().await.unwrap();

		let block_hash = H256::from([12u8; 32]);

		// Set batch with duplicate keys - last value should win
		let entries: Vec<(&[u8], Option<&[u8]>)> = vec![
			(b"key1", Some(b"first_value")),
			(b"key2", Some(b"value2")),
			(b"key1", Some(b"second_value")), // duplicate key1
			(b"key3", Some(b"value3")),
			(b"key1", Some(b"final_value")), // another duplicate key1
		];

		let result = cache.set_storage_batch(block_hash, &entries).await;
		assert!(matches!(result, Err(CacheError::DuplicatedKeys)));
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn prefix_scan_progress_tracking() {
		let cache = StorageCache::in_memory().await.unwrap();
		let block_hash = H256::from([11u8; 32]);
		let prefix = b"balances:";

		// Initially no progress
		let progress = cache.get_prefix_scan_progress(block_hash, prefix).await.unwrap();
		assert!(progress.is_none());

		// Update progress with a partial scan
		let last_key = b"balances:account123";
		cache.update_prefix_scan(block_hash, prefix, last_key, false).await.unwrap();

		// Progress should now exist
		let progress = cache.get_prefix_scan_progress(block_hash, prefix).await.unwrap();
		assert!(progress.is_some());
		let p = progress.unwrap();
		assert_eq!(p.last_scanned_key, Some(last_key.to_vec()));
		assert!(!p.is_complete);

		// Update to complete
		let final_key = b"balances:zzz";
		cache.update_prefix_scan(block_hash, prefix, final_key, true).await.unwrap();

		let progress = cache.get_prefix_scan_progress(block_hash, prefix).await.unwrap();
		let p = progress.unwrap();
		assert_eq!(p.last_scanned_key, Some(final_key.to_vec()));
		assert!(p.is_complete);
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn prefix_scan_different_blocks_separate() {
		let cache = StorageCache::in_memory().await.unwrap();
		let block1 = H256::from([12u8; 32]);
		let block2 = H256::from([13u8; 32]);
		let prefix = b"system:";

		// Set progress on block1 only
		cache.update_prefix_scan(block1, prefix, b"system:key1", true).await.unwrap();

		// Block1 has progress
		let p1 = cache.get_prefix_scan_progress(block1, prefix).await.unwrap();
		assert!(p1.is_some());
		assert!(p1.unwrap().is_complete);

		// Block2 has no progress
		let p2 = cache.get_prefix_scan_progress(block2, prefix).await.unwrap();
		assert!(p2.is_none());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_keys_by_prefix_works() {
		let cache = StorageCache::in_memory().await.unwrap();
		let block_hash = H256::from([14u8; 32]);

		// Insert keys with different prefixes
		let entries: Vec<(&[u8], Option<&[u8]>)> = vec![
			(b"tokens:alice", Some(b"100")),
			(b"tokens:bob", Some(b"200")),
			(b"tokens:charlie", Some(b"300")),
			(b"balances:alice", Some(b"50")),
			(b"balances:bob", Some(b"75")),
		];
		cache.set_storage_batch(block_hash, &entries).await.unwrap();

		// Get keys with "tokens:" prefix
		let token_keys = cache.get_keys_by_prefix(block_hash, b"tokens:").await.unwrap();
		assert_eq!(token_keys.len(), 3);
		assert!(token_keys.contains(&b"tokens:alice".to_vec()));
		assert!(token_keys.contains(&b"tokens:bob".to_vec()));
		assert!(token_keys.contains(&b"tokens:charlie".to_vec()));

		// Get keys with "balances:" prefix
		let balance_keys = cache.get_keys_by_prefix(block_hash, b"balances:").await.unwrap();
		assert_eq!(balance_keys.len(), 2);
		assert!(balance_keys.contains(&b"balances:alice".to_vec()));
		assert!(balance_keys.contains(&b"balances:bob".to_vec()));

		// Get keys with non-existent prefix
		let empty_keys = cache.get_keys_by_prefix(block_hash, b"nonexistent:").await.unwrap();
		assert!(empty_keys.is_empty());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn count_keys_by_prefix_works() {
		let cache = StorageCache::in_memory().await.unwrap();
		let block_hash = H256::from([15u8; 32]);

		// Insert keys with different prefixes
		let entries: Vec<(&[u8], Option<&[u8]>)> = vec![
			(b"prefix_a:1", Some(b"v1")),
			(b"prefix_a:2", Some(b"v2")),
			(b"prefix_a:3", Some(b"v3")),
			(b"prefix_b:1", Some(b"v4")),
		];
		cache.set_storage_batch(block_hash, &entries).await.unwrap();

		assert_eq!(cache.count_keys_by_prefix(block_hash, b"prefix_a:").await.unwrap(), 3);
		assert_eq!(cache.count_keys_by_prefix(block_hash, b"prefix_b:").await.unwrap(), 1);
		assert_eq!(cache.count_keys_by_prefix(block_hash, b"prefix_c:").await.unwrap(), 0);
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn next_key_from_cache_works() {
		let cache = StorageCache::in_memory().await.unwrap();
		let block_hash = H256::from([20u8; 32]);

		// Insert keys with a prefix
		let entries: Vec<(&[u8], Option<&[u8]>)> = vec![
			(b"prefix:aaa", Some(b"v1")),
			(b"prefix:bbb", Some(b"v2")),
			(b"prefix:ccc", Some(b"v3")),
			(b"other:ddd", Some(b"v4")),
		];
		cache.set_storage_batch(block_hash, &entries).await.unwrap();

		// Next key after "prefix:aaa" with prefix "prefix:" should be "prefix:bbb"
		let next = cache.next_key_from_cache(block_hash, b"prefix:", b"prefix:aaa").await.unwrap();
		assert_eq!(next, Some(b"prefix:bbb".to_vec()));

		// Next key after "prefix:bbb" should be "prefix:ccc"
		let next = cache.next_key_from_cache(block_hash, b"prefix:", b"prefix:bbb").await.unwrap();
		assert_eq!(next, Some(b"prefix:ccc".to_vec()));

		// Next key after "prefix:ccc" should be None (no more keys)
		let next = cache.next_key_from_cache(block_hash, b"prefix:", b"prefix:ccc").await.unwrap();
		assert!(next.is_none());

		// Next key from the very start with prefix "prefix:" should be "prefix:aaa"
		let next = cache.next_key_from_cache(block_hash, b"prefix:", b"prefix:").await.unwrap();
		assert_eq!(next, Some(b"prefix:aaa".to_vec()));
	}

	#[test]
	fn increment_prefix_works() {
		// Normal case
		assert_eq!(increment_prefix(b"abc"), Some(b"abd".to_vec()));

		// Increment last byte
		assert_eq!(increment_prefix(b"ab\xff"), Some(b"ac".to_vec()));

		// Multiple 0xff bytes
		assert_eq!(increment_prefix(b"a\xff\xff"), Some(b"b".to_vec()));

		// All 0xff - no valid increment possible
		assert_eq!(increment_prefix(b"\xff\xff\xff"), None);

		// Empty prefix - no increment possible
		assert_eq!(increment_prefix(b""), None);

		// Single byte
		assert_eq!(increment_prefix(b"a"), Some(b"b".to_vec()));
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn clear_block_removes_prefix_scans() {
		let cache = StorageCache::in_memory().await.unwrap();
		let hash = H256::from([16u8; 32]);
		let prefix = b"test:";

		// Set up prefix scan progress
		cache.update_prefix_scan(hash, prefix, b"test:key", true).await.unwrap();
		assert!(cache.get_prefix_scan_progress(hash, prefix).await.unwrap().is_some());

		// Clear block
		cache.clear_block(hash).await.unwrap();

		// Prefix scan progress should be removed
		assert!(cache.get_prefix_scan_progress(hash, prefix).await.unwrap().is_none());
	}

	// Tests for local storage with validity

	#[tokio::test(flavor = "multi_thread")]
	async fn get_local_key_returns_none_for_nonexistent_key() {
		let cache = StorageCache::in_memory().await.unwrap();

		let result = cache.get_local_key(b"nonexistent_key").await.unwrap();
		assert!(result.is_none());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn insert_local_key_creates_new_key() {
		let cache = StorageCache::in_memory().await.unwrap();
		let key = b"new_key";

		// Insert new key
		let key_id = cache.insert_local_key(key).await.unwrap();
		assert_eq!(key_id, 1);

		// Verify it exists
		let result = cache.get_local_key(key).await.unwrap();
		assert!(result.is_some());
		assert_eq!(result.unwrap().id, key_id);
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn insert_local_key_returns_existing_id() {
		let cache = StorageCache::in_memory().await.unwrap();
		let key = b"duplicate_key";

		// Insert key twice
		let key_id1 = cache.insert_local_key(key).await.unwrap();
		let key_id2 = cache.insert_local_key(key).await.unwrap();

		// Should return the same ID
		assert_eq!(key_id1, key_id2);
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn insert_and_get_local_value_at_block() {
		let cache = StorageCache::in_memory().await.unwrap();
		let key = b"test_key";
		let value = b"test_value";

		// Insert key and value
		let key_id = cache.insert_local_key(key).await.unwrap();
		cache.insert_local_value(key_id, Some(value), 100).await.unwrap();

		// Query at block 100 - should find it (valid_from = 100, valid_until = NULL)
		let result = cache.get_local_value_at_block(key, 100).await.unwrap();
		assert_eq!(result, Some(Some(value.to_vec())));

		// Query at block 150 - should still find it (valid_until = NULL means still valid)
		let result = cache.get_local_value_at_block(key, 150).await.unwrap();
		assert_eq!(result, Some(Some(value.to_vec())));

		// Query at block 99 - should not find it (before valid_from)
		let result = cache.get_local_value_at_block(key, 99).await.unwrap();
		assert!(result.is_none());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_local_value_at_block_nonexistent_key() {
		let cache = StorageCache::in_memory().await.unwrap();

		let result = cache.get_local_value_at_block(b"nonexistent", 100).await.unwrap();
		assert!(result.is_none());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn close_local_value_sets_valid_until() {
		let cache = StorageCache::in_memory().await.unwrap();
		let key = b"closing_key";
		let value1 = b"value1";
		let value2 = b"value2";

		// Insert key and first value at block 100
		let key_id = cache.insert_local_key(key).await.unwrap();
		cache.insert_local_value(key_id, Some(value1), 100).await.unwrap();

		// Close at block 150 and insert new value
		cache.close_local_value(key_id, 150).await.unwrap();
		cache.insert_local_value(key_id, Some(value2), 150).await.unwrap();

		// Query at block 120 - should get value1
		let result = cache.get_local_value_at_block(key, 120).await.unwrap();
		assert_eq!(result, Some(Some(value1.to_vec())));

		// Query at block 150 - should get value2
		let result = cache.get_local_value_at_block(key, 150).await.unwrap();
		assert_eq!(result, Some(Some(value2.to_vec())));

		// Query at block 200 - should get value2 (still valid)
		let result = cache.get_local_value_at_block(key, 200).await.unwrap();
		assert_eq!(result, Some(Some(value2.to_vec())));
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_local_values_at_block_batch_works() {
		let cache = StorageCache::in_memory().await.unwrap();

		let key1 = b"batch_key1";
		let key2 = b"batch_key2";
		let key3 = b"batch_key3";
		let value1 = b"batch_value1";
		let value2 = b"batch_value2";

		// Insert keys and values
		let key_id1 = cache.insert_local_key(key1).await.unwrap();
		let key_id2 = cache.insert_local_key(key2).await.unwrap();
		cache.insert_local_value(key_id1, Some(value1), 100).await.unwrap();
		cache.insert_local_value(key_id2, Some(value2), 100).await.unwrap();

		// Batch query
		let keys: Vec<&[u8]> = vec![key1, key2, key3];
		let results = cache.get_local_values_at_block_batch(&keys, 100).await.unwrap();

		assert_eq!(results.len(), 3);
		assert_eq!(results[0], Some(Some(value1.to_vec())));
		assert_eq!(results[1], Some(Some(value2.to_vec())));
		assert!(results[2].is_none()); // key3 doesn't exist
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_local_values_at_block_batch_respects_validity() {
		let cache = StorageCache::in_memory().await.unwrap();

		let key = b"validity_key";
		let value1 = b"value_v1";
		let value2 = b"value_v2";

		// Insert key and values with validity ranges
		let key_id = cache.insert_local_key(key).await.unwrap();
		cache.insert_local_value(key_id, Some(value1), 100).await.unwrap();
		cache.close_local_value(key_id, 200).await.unwrap();
		cache.insert_local_value(key_id, Some(value2), 200).await.unwrap();

		// Query at different blocks
		let keys: Vec<&[u8]> = vec![key];

		let results = cache.get_local_values_at_block_batch(&keys, 150).await.unwrap();
		assert_eq!(results[0], Some(Some(value1.to_vec())));

		let results = cache.get_local_values_at_block_batch(&keys, 200).await.unwrap();
		assert_eq!(results[0], Some(Some(value2.to_vec())));

		let results = cache.get_local_values_at_block_batch(&keys, 99).await.unwrap();
		assert!(results[0].is_none());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_local_values_at_block_batch_with_duplicate_keys() {
		let cache = StorageCache::in_memory().await.unwrap();

		let key = b"dup_key";
		let keys: Vec<&[u8]> = vec![key, key]; // duplicate

		let result = cache.get_local_values_at_block_batch(&keys, 100).await;
		assert!(matches!(result, Err(CacheError::DuplicatedKeys)));
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn clear_local_storage_removes_all_data() {
		let cache = StorageCache::in_memory().await.unwrap();

		let key1 = b"clear_key1";
		let key2 = b"clear_key2";
		let value = b"some_value";

		// Insert some data
		let key_id1 = cache.insert_local_key(key1).await.unwrap();
		let key_id2 = cache.insert_local_key(key2).await.unwrap();
		cache.insert_local_value(key_id1, Some(value), 100).await.unwrap();
		cache.insert_local_value(key_id2, Some(value), 100).await.unwrap();

		// Verify data exists
		assert!(cache.get_local_key(key1).await.unwrap().is_some());
		assert!(cache.get_local_key(key2).await.unwrap().is_some());
		assert!(cache.get_local_value_at_block(key1, 100).await.unwrap().is_some());

		// Clear all local storage
		cache.clear_local_storage().await.unwrap();

		// Verify data is gone
		assert!(cache.get_local_key(key1).await.unwrap().is_none());
		assert!(cache.get_local_key(key2).await.unwrap().is_none());
		assert!(cache.get_local_value_at_block(key1, 100).await.unwrap().is_none());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_local_keys_at_block_returns_live_keys() {
		let cache = StorageCache::in_memory().await.unwrap();

		// Insert keys with prefix "pallet:" at different blocks.
		let k1 = b"pallet:alice";
		let k2 = b"pallet:bob";
		let k3 = b"other:charlie";

		let id1 = cache.insert_local_key(k1).await.unwrap();
		let id2 = cache.insert_local_key(k2).await.unwrap();
		let id3 = cache.insert_local_key(k3).await.unwrap();

		// k1: valid from block 100
		cache.insert_local_value(id1, Some(b"v1"), 100).await.unwrap();
		// k2: valid from block 200
		cache.insert_local_value(id2, Some(b"v2"), 200).await.unwrap();
		// k3: valid from block 100 (different prefix)
		cache.insert_local_value(id3, Some(b"v3"), 100).await.unwrap();

		// At block 150: only k1 matches "pallet:" prefix
		let keys = cache.get_local_keys_at_block(b"pallet:", 150).await.unwrap();
		assert_eq!(keys, vec![k1.to_vec()]);

		// At block 200: both k1 and k2 match
		let keys = cache.get_local_keys_at_block(b"pallet:", 200).await.unwrap();
		assert_eq!(keys, vec![k1.to_vec(), k2.to_vec()]);

		// At block 99: nothing matches (before any inserts)
		let keys = cache.get_local_keys_at_block(b"pallet:", 99).await.unwrap();
		assert!(keys.is_empty());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_local_keys_at_block_excludes_deleted() {
		let cache = StorageCache::in_memory().await.unwrap();

		let k1 = b"pallet:alice";
		let id1 = cache.insert_local_key(k1).await.unwrap();

		// k1: value at block 100, deleted at block 200
		cache.insert_local_value(id1, Some(b"v1"), 100).await.unwrap();
		cache.close_local_value(id1, 200).await.unwrap();
		cache.insert_local_value(id1, None, 200).await.unwrap();

		// At block 150: key exists (has value)
		let keys = cache.get_local_keys_at_block(b"pallet:", 150).await.unwrap();
		assert_eq!(keys, vec![k1.to_vec()]);

		// At block 200: key was deleted (value is NULL)
		let keys = cache.get_local_keys_at_block(b"pallet:", 200).await.unwrap();
		assert!(keys.is_empty());
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn get_local_deleted_keys_at_block_works() {
		let cache = StorageCache::in_memory().await.unwrap();

		let k1 = b"pallet:alice";
		let id1 = cache.insert_local_key(k1).await.unwrap();

		// k1: value at block 100, deleted at block 200
		cache.insert_local_value(id1, Some(b"v1"), 100).await.unwrap();
		cache.close_local_value(id1, 200).await.unwrap();
		cache.insert_local_value(id1, None, 200).await.unwrap();

		// At block 150: no deleted keys
		let deleted = cache.get_local_deleted_keys_at_block(b"pallet:", 150).await.unwrap();
		assert!(deleted.is_empty());

		// At block 200: k1 is deleted
		let deleted = cache.get_local_deleted_keys_at_block(b"pallet:", 200).await.unwrap();
		assert_eq!(deleted, vec![k1.to_vec()]);
	}

	#[tokio::test(flavor = "multi_thread")]
	async fn prefix_upper_bound_works() {
		// Normal case
		assert_eq!(StorageCache::prefix_upper_bound(b"abc"), Some(b"abd".to_vec()));
		// Trailing 0xFF
		assert_eq!(StorageCache::prefix_upper_bound(b"ab\xff"), Some(b"ac".to_vec()));
		// All 0xFF
		assert_eq!(StorageCache::prefix_upper_bound(b"\xff\xff"), None);
		// Empty prefix
		assert_eq!(StorageCache::prefix_upper_bound(b""), None);
	}
}