signet-cold-sql 0.6.5

SQL backend for signet-cold storage
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
//! Unified SQL backend for cold storage.
//!
//! Supports both PostgreSQL and SQLite via [`sqlx::Any`]. The backend
//! auto-detects the database type at construction time and runs the
//! appropriate migration.

use crate::SqlColdError;
use crate::columns::{
    COL_ACCESS_LIST, COL_ADDRESS, COL_AMOUNT, COL_AUTHORIZATION_LIST, COL_BASE_FEE_PER_GAS,
    COL_BENEFICIARY, COL_BLOB_GAS_USED, COL_BLOB_VERSIONED_HASHES, COL_BLOCK_DATA_HASH,
    COL_BLOCK_HASH, COL_BLOCK_LOG_INDEX, COL_BLOCK_NUMBER, COL_BLOCK_TIMESTAMP, COL_CHAIN_ID,
    COL_CNT, COL_CUMULATIVE_GAS_USED, COL_DATA, COL_DIFFICULTY, COL_EVENT_TYPE,
    COL_EXCESS_BLOB_GAS, COL_EXTRA_DATA, COL_FIRST_LOG_INDEX, COL_FROM_ADDRESS, COL_GAS,
    COL_GAS_LIMIT, COL_GAS_PRICE, COL_GAS_USED, COL_HOST_BLOCK_NUMBER, COL_INPUT, COL_LOGS_BLOOM,
    COL_MAX_BN, COL_MAX_FEE_PER_BLOB_GAS, COL_MAX_FEE_PER_GAS, COL_MAX_PRIORITY_FEE_PER_GAS,
    COL_MIX_HASH, COL_NONCE, COL_OMMERS_HASH, COL_ORDER_INDEX, COL_PARENT_BEACON_BLOCK_ROOT,
    COL_PARENT_HASH, COL_PRIOR_GAS, COL_RECEIPTS_ROOT, COL_REQUESTS_HASH, COL_REWARD_ADDRESS,
    COL_ROLLUP_CHAIN_ID, COL_ROLLUP_RECIPIENT, COL_SENDER, COL_SIG_R, COL_SIG_S, COL_SIG_Y_PARITY,
    COL_STATE_ROOT, COL_SUCCESS, COL_TIMESTAMP, COL_TO_ADDRESS, COL_TOKEN, COL_TOPIC0, COL_TOPIC1,
    COL_TOPIC2, COL_TOPIC3, COL_TRANSACTIONS_ROOT, COL_TX_HASH, COL_TX_INDEX, COL_TX_TYPE,
    COL_VALUE, COL_WITHDRAWALS_ROOT,
};
use crate::convert::{
    EVENT_ENTER, EVENT_ENTER_TOKEN, EVENT_TRANSACT, build_receipt, decode_access_list_or_empty,
    decode_authorization_list, decode_b256_vec, decode_u128_required, encode_access_list,
    encode_authorization_list, encode_b256_vec, encode_u128, from_i64, to_i64,
};
use alloy::{
    consensus::{
        Header, Signed, TxEip1559, TxEip2930, TxEip4844, TxEip7702, TxLegacy, TxType,
        transaction::Recovered,
    },
    primitives::{
        Address, B256, BlockNumber, Bloom, Bytes, Log, LogData, Sealable, Signature, TxKind, U256,
    },
};
use signet_cold::{
    BlockData, ColdReceipt, ColdResult, ColdStorage, ColdStorageError, Confirmed, Filter,
    HeaderSpecifier, ReceiptSpecifier, RpcLog, SignetEventsSpecifier, TransactionSpecifier,
    ZenithHeaderSpecifier,
};
use signet_storage_types::{
    ConfirmationMeta, DbSignetEvent, DbZenithHeader, IndexedReceipt, RecoveredTx, SealedHeader,
    TransactionSigned,
};
use signet_zenith::{
    Passage::{Enter, EnterToken},
    Transactor::Transact,
    Zenith,
};
use sqlx::{AnyPool, Row};

/// SQL-based cold storage backend.
///
/// Uses [`sqlx::Any`] for database-agnostic access, supporting both
/// PostgreSQL and SQLite through a single implementation. The backend
/// is determined by the connection URL at construction time.
///
/// # Example
///
/// ```no_run
/// # async fn example() {
/// use signet_cold_sql::SqlColdBackend;
///
/// // SQLite (in-memory)
/// let backend = SqlColdBackend::connect("sqlite::memory:").await.unwrap();
///
/// // PostgreSQL
/// let backend = SqlColdBackend::connect("postgres://localhost/signet").await.unwrap();
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct SqlColdBackend {
    pool: AnyPool,
    is_postgres: bool,
}

impl SqlColdBackend {
    /// Create a new SQL cold storage backend from an existing [`AnyPool`].
    ///
    /// Auto-detects the database backend and creates all tables if they
    /// do not already exist. Callers must ensure
    /// [`sqlx::any::install_default_drivers`] has been called before
    /// constructing the pool.
    pub async fn new(pool: AnyPool) -> Result<Self, SqlColdError> {
        // Detect backend from a pooled connection.
        let conn = pool.acquire().await?;
        let backend = conn.backend_name().to_owned();
        drop(conn);

        let migration = match backend.as_str() {
            "PostgreSQL" => include_str!("../migrations/001_initial_pg.sql"),
            "SQLite" => include_str!("../migrations/001_initial.sql"),
            other => {
                return Err(SqlColdError::Convert(format!(
                    "unsupported database backend: {other}"
                )));
            }
        };
        // Execute via pool to ensure the migration uses the same
        // connection that subsequent queries will use.
        let is_postgres = backend == "PostgreSQL";
        sqlx::raw_sql(migration).execute(&pool).await?;
        Ok(Self { pool, is_postgres })
    }

    /// Connect to a database URL and create the backend.
    ///
    /// Installs the default sqlx drivers on the first call. The database
    /// type is inferred from the URL scheme (`sqlite:` or `postgres:`).
    ///
    /// For SQLite in-memory databases (`sqlite::memory:`), the pool is
    /// limited to one connection to ensure all operations share the same
    /// database.
    pub async fn connect(url: &str) -> Result<Self, SqlColdError> {
        sqlx::any::install_default_drivers();
        let pool: AnyPool = sqlx::pool::PoolOptions::new().max_connections(1).connect(url).await?;
        Self::new(pool).await
    }

    // ========================================================================
    // Specifier resolution
    // ========================================================================

    async fn resolve_header_spec(
        &self,
        spec: HeaderSpecifier,
    ) -> Result<Option<BlockNumber>, SqlColdError> {
        match spec {
            HeaderSpecifier::Number(n) => Ok(Some(n)),
            HeaderSpecifier::Hash(hash) => {
                let row = sqlx::query("SELECT block_number FROM headers WHERE block_hash = $1")
                    .bind(hash)
                    .fetch_optional(&self.pool)
                    .await?;
                Ok(row.map(|r| from_i64(r.get::<i64, _>(COL_BLOCK_NUMBER))))
            }
        }
    }

    // ========================================================================
    // Read helpers
    // ========================================================================

    async fn fetch_header_by_number(
        &self,
        block_num: BlockNumber,
    ) -> Result<Option<SealedHeader>, SqlColdError> {
        let bn = to_i64(block_num);
        let row = sqlx::query("SELECT * FROM headers WHERE block_number = $1")
            .bind(bn)
            .fetch_optional(&self.pool)
            .await?;

        row.map(|r| header_from_row(&r).map(|h| h.seal_slow())).transpose()
    }

    // ========================================================================
    // Write helpers
    // ========================================================================

    async fn insert_block(&self, data: BlockData) -> Result<(), SqlColdError> {
        let mut tx = self.pool.begin().await?;
        write_block_to_tx(&mut tx, data).await?;
        tx.commit().await?;
        Ok(())
    }

    // ========================================================================
    // Streaming helpers
    // ========================================================================

    /// Stream logs using a PostgreSQL REPEATABLE READ transaction.
    ///
    /// The transaction provides a consistent snapshot across all per-block
    /// queries, eliminating the need for anchor-hash reorg detection.
    /// Rows are streamed individually rather than materialised per block.
    #[cfg(feature = "postgres")]
    async fn produce_log_stream_pg(&self, filter: &Filter, params: signet_cold::StreamParams) {
        use tokio_stream::StreamExt;

        /// Unwrap a `Result` or send the error through the stream and return.
        macro_rules! try_stream {
            ($sender:expr, $expr:expr) => {
                match $expr {
                    Ok(v) => v,
                    Err(e) => {
                        let _ = $sender
                            .send(Err(ColdStorageError::backend(SqlColdError::from(e))))
                            .await;
                        return;
                    }
                }
            };
        }

        let signet_cold::StreamParams { from, to, max_logs, sender, deadline } = params;

        // Open a REPEATABLE READ transaction so all per-block queries see a
        // consistent snapshot. This makes reorg detection unnecessary — if a
        // reorg lands mid-stream the transaction still reads the old data.
        let mut tx = try_stream!(sender, self.pool.begin().await);
        try_stream!(
            sender,
            sqlx::query("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ").execute(&mut *tx).await
        );

        // Build the parameterised query once. $1 is the block number
        // (bound per iteration); remaining parameters are the address
        // and topic filters from the user's request.
        let (filter_clause, filter_params) = build_log_filter_clause(filter, 2);
        let data_sql = format!(
            "SELECT l.*, h.block_hash, h.timestamp AS block_timestamp, t.tx_hash, \
               (r.first_log_index + l.log_index) AS block_log_index \
             FROM logs l \
             JOIN headers h ON l.block_number = h.block_number \
             JOIN transactions t ON l.block_number = t.block_number \
               AND l.tx_index = t.tx_index \
             JOIN receipts r ON l.block_number = r.block_number \
               AND l.tx_index = r.tx_index \
             WHERE l.block_number = $1{filter_clause} \
             ORDER BY l.tx_index, l.log_index"
        );

        let mut total = 0usize;

        // Walk through blocks one at a time, streaming matching log rows
        // from each block directly to the channel.
        for block_num in from..=to {
            // Check the deadline before starting each block so we
            // don't begin a new query after the caller's timeout.
            if tokio::time::Instant::now() > deadline {
                let _ = sender.send(Err(ColdStorageError::StreamDeadlineExceeded)).await;
                return;
            }

            let mut query = sqlx::query(&data_sql).bind(to_i64(block_num));
            for param in &filter_params {
                query = query.bind(*param);
            }

            // Stream rows from this block's query. Each row is converted
            // to an RpcLog and sent over the channel immediately rather
            // than being collected into a Vec first.
            let mut stream = query.fetch(&mut *tx);
            while let Some(row_result) = stream.next().await {
                let r = try_stream!(sender, row_result);

                // Enforce the global log limit across all blocks.
                total += 1;
                if total > max_logs {
                    let _ =
                        sender.send(Err(ColdStorageError::TooManyLogs { limit: max_logs })).await;
                    return;
                }

                let log = log_from_row(&r);
                let rpc_log = RpcLog {
                    inner: log,
                    block_hash: Some(r.get(COL_BLOCK_HASH)),
                    block_number: Some(from_i64(r.get::<i64, _>(COL_BLOCK_NUMBER))),
                    block_timestamp: Some(from_i64(r.get::<i64, _>(COL_BLOCK_TIMESTAMP))),
                    transaction_hash: Some(r.get(COL_TX_HASH)),
                    transaction_index: Some(from_i64(r.get::<i64, _>(COL_TX_INDEX))),
                    log_index: Some(from_i64(r.get::<i64, _>(COL_BLOCK_LOG_INDEX))),
                    removed: false,
                };
                // Send the log to the caller. The timeout ensures we
                // stop if the deadline passes while back-pressured.
                match tokio::time::timeout_at(deadline, sender.send(Ok(rpc_log))).await {
                    Ok(Ok(())) => {}
                    Ok(Err(_)) => return, // receiver dropped
                    Err(_) => {
                        let _ = sender.send(Err(ColdStorageError::StreamDeadlineExceeded)).await;
                        return;
                    }
                }
            }

            // Early exit if we've already hit the limit — no need to
            // query the next block.
            if total >= max_logs {
                return;
            }
        }
    }
}

// ============================================================================
// Row → domain type conversion (read path)
// ============================================================================

/// Extract a required BLOB column from a row as a borrowed slice.
fn blob<'r>(r: &'r sqlx::any::AnyRow, col: &str) -> &'r [u8] {
    r.get(col)
}

/// Extract an optional BLOB column from a row as a borrowed slice.
fn opt_blob<'r>(r: &'r sqlx::any::AnyRow, col: &str) -> Option<&'r [u8]> {
    r.get(col)
}

/// Build a [`Header`] from an [`sqlx::any::AnyRow`].
fn header_from_row(r: &sqlx::any::AnyRow) -> Result<Header, SqlColdError> {
    Ok(Header {
        parent_hash: r.get(COL_PARENT_HASH),
        ommers_hash: r.get(COL_OMMERS_HASH),
        beneficiary: r.get(COL_BENEFICIARY),
        state_root: r.get(COL_STATE_ROOT),
        transactions_root: r.get(COL_TRANSACTIONS_ROOT),
        receipts_root: r.get(COL_RECEIPTS_ROOT),
        logs_bloom: Bloom::from_slice(blob(r, COL_LOGS_BLOOM)),
        difficulty: r.get(COL_DIFFICULTY),
        number: from_i64(r.get(COL_BLOCK_NUMBER)),
        gas_limit: from_i64(r.get(COL_GAS_LIMIT)),
        gas_used: from_i64(r.get(COL_GAS_USED)),
        timestamp: from_i64(r.get(COL_TIMESTAMP)),
        extra_data: r.get(COL_EXTRA_DATA),
        mix_hash: r.get(COL_MIX_HASH),
        nonce: r.get(COL_NONCE),
        base_fee_per_gas: r.get::<Option<i64>, _>(COL_BASE_FEE_PER_GAS).map(from_i64),
        withdrawals_root: r.get(COL_WITHDRAWALS_ROOT),
        blob_gas_used: r.get::<Option<i64>, _>(COL_BLOB_GAS_USED).map(from_i64),
        excess_blob_gas: r.get::<Option<i64>, _>(COL_EXCESS_BLOB_GAS).map(from_i64),
        parent_beacon_block_root: r.get(COL_PARENT_BEACON_BLOCK_ROOT),
        requests_hash: r.get(COL_REQUESTS_HASH),
    })
}

/// Build a [`TransactionSigned`] from an [`sqlx::any::AnyRow`].
fn tx_from_row(r: &sqlx::any::AnyRow) -> Result<TransactionSigned, SqlColdError> {
    use alloy::consensus::EthereumTxEnvelope;

    let sig =
        Signature::new(r.get(COL_SIG_R), r.get(COL_SIG_S), r.get::<i32, _>(COL_SIG_Y_PARITY) != 0);

    let tx_type_raw = r.get::<i32, _>(COL_TX_TYPE) as u8;
    let tx_type = TxType::try_from(tx_type_raw)
        .map_err(|_| SqlColdError::Convert(format!("invalid tx_type: {tx_type_raw}")))?;

    let chain_id: Option<i64> = r.get(COL_CHAIN_ID);
    let nonce = from_i64(r.get(COL_NONCE));
    let gas_limit = from_i64(r.get(COL_GAS_LIMIT));
    let to_addr: Option<Address> = r.get(COL_TO_ADDRESS);
    let value: U256 = r.get(COL_VALUE);
    let input: Bytes = r.get(COL_INPUT);

    match tx_type {
        TxType::Legacy => {
            let tx = TxLegacy {
                chain_id: chain_id.map(from_i64),
                nonce,
                gas_price: decode_u128_required(opt_blob(r, COL_GAS_PRICE), COL_GAS_PRICE)?,
                gas_limit,
                to: to_addr.map_or(TxKind::Create, TxKind::Call),
                value,
                input,
            };
            Ok(EthereumTxEnvelope::Legacy(Signed::new_unhashed(tx, sig)))
        }
        TxType::Eip2930 => {
            let tx = TxEip2930 {
                chain_id: from_i64(
                    chain_id
                        .ok_or_else(|| SqlColdError::Convert("EIP2930 requires chain_id".into()))?,
                ),
                nonce,
                gas_price: decode_u128_required(opt_blob(r, COL_GAS_PRICE), COL_GAS_PRICE)?,
                gas_limit,
                to: to_addr.map_or(TxKind::Create, TxKind::Call),
                value,
                input,
                access_list: decode_access_list_or_empty(opt_blob(r, COL_ACCESS_LIST))?,
            };
            Ok(EthereumTxEnvelope::Eip2930(Signed::new_unhashed(tx, sig)))
        }
        TxType::Eip1559 => {
            let tx = TxEip1559 {
                chain_id: from_i64(
                    chain_id
                        .ok_or_else(|| SqlColdError::Convert("EIP1559 requires chain_id".into()))?,
                ),
                nonce,
                gas_limit,
                max_fee_per_gas: decode_u128_required(
                    opt_blob(r, COL_MAX_FEE_PER_GAS),
                    COL_MAX_FEE_PER_GAS,
                )?,
                max_priority_fee_per_gas: decode_u128_required(
                    opt_blob(r, COL_MAX_PRIORITY_FEE_PER_GAS),
                    COL_MAX_PRIORITY_FEE_PER_GAS,
                )?,
                to: to_addr.map_or(TxKind::Create, TxKind::Call),
                value,
                input,
                access_list: decode_access_list_or_empty(opt_blob(r, COL_ACCESS_LIST))?,
            };
            Ok(EthereumTxEnvelope::Eip1559(Signed::new_unhashed(tx, sig)))
        }
        TxType::Eip4844 => {
            let tx = TxEip4844 {
                chain_id: from_i64(
                    chain_id
                        .ok_or_else(|| SqlColdError::Convert("EIP4844 requires chain_id".into()))?,
                ),
                nonce,
                gas_limit,
                max_fee_per_gas: decode_u128_required(
                    opt_blob(r, COL_MAX_FEE_PER_GAS),
                    COL_MAX_FEE_PER_GAS,
                )?,
                max_priority_fee_per_gas: decode_u128_required(
                    opt_blob(r, COL_MAX_PRIORITY_FEE_PER_GAS),
                    COL_MAX_PRIORITY_FEE_PER_GAS,
                )?,
                to: to_addr
                    .ok_or_else(|| SqlColdError::Convert("EIP4844 requires to_address".into()))?,
                value,
                input,
                access_list: decode_access_list_or_empty(opt_blob(r, COL_ACCESS_LIST))?,
                blob_versioned_hashes: decode_b256_vec(
                    opt_blob(r, COL_BLOB_VERSIONED_HASHES).ok_or_else(|| {
                        SqlColdError::Convert("EIP4844 requires blob_versioned_hashes".into())
                    })?,
                )?,
                max_fee_per_blob_gas: decode_u128_required(
                    opt_blob(r, COL_MAX_FEE_PER_BLOB_GAS),
                    COL_MAX_FEE_PER_BLOB_GAS,
                )?,
            };
            Ok(EthereumTxEnvelope::Eip4844(Signed::new_unhashed(tx, sig)))
        }
        TxType::Eip7702 => {
            let tx = TxEip7702 {
                chain_id: from_i64(
                    chain_id
                        .ok_or_else(|| SqlColdError::Convert("EIP7702 requires chain_id".into()))?,
                ),
                nonce,
                gas_limit,
                max_fee_per_gas: decode_u128_required(
                    opt_blob(r, COL_MAX_FEE_PER_GAS),
                    COL_MAX_FEE_PER_GAS,
                )?,
                max_priority_fee_per_gas: decode_u128_required(
                    opt_blob(r, COL_MAX_PRIORITY_FEE_PER_GAS),
                    COL_MAX_PRIORITY_FEE_PER_GAS,
                )?,
                to: to_addr
                    .ok_or_else(|| SqlColdError::Convert("EIP7702 requires to_address".into()))?,
                value,
                input,
                access_list: decode_access_list_or_empty(opt_blob(r, COL_ACCESS_LIST))?,
                authorization_list: decode_authorization_list(
                    opt_blob(r, COL_AUTHORIZATION_LIST).ok_or_else(|| {
                        SqlColdError::Convert("EIP7702 requires authorization_list".into())
                    })?,
                )?,
            };
            Ok(EthereumTxEnvelope::Eip7702(Signed::new_unhashed(tx, sig)))
        }
    }
}

/// Build a [`RecoveredTx`] from a row that includes `from_address`.
fn recovered_tx_from_row(r: &sqlx::any::AnyRow) -> Result<RecoveredTx, SqlColdError> {
    let sender: Address = r.get(COL_FROM_ADDRESS);
    let tx = tx_from_row(r)?;
    // SAFETY: the sender was recovered at append time and stored in from_address.
    Ok(Recovered::new_unchecked(tx, sender))
}

/// Build a [`Log`] from an [`sqlx::any::AnyRow`].
fn log_from_row(r: &sqlx::any::AnyRow) -> Log {
    let topics = [COL_TOPIC0, COL_TOPIC1, COL_TOPIC2, COL_TOPIC3]
        .into_iter()
        .filter_map(|col| r.get::<Option<B256>, _>(col))
        .collect();
    Log { address: r.get(COL_ADDRESS), data: LogData::new_unchecked(topics, r.get(COL_DATA)) }
}

/// Build a [`DbSignetEvent`] from an [`sqlx::any::AnyRow`].
fn signet_event_from_row(r: &sqlx::any::AnyRow) -> Result<DbSignetEvent, SqlColdError> {
    let event_type = r.get::<i32, _>(COL_EVENT_TYPE) as i16;
    let order = from_i64(r.get(COL_ORDER_INDEX));
    let rollup_chain_id: U256 = r.get(COL_ROLLUP_CHAIN_ID);

    match event_type {
        EVENT_TRANSACT => {
            let sender: Address = r
                .get::<Option<Address>, _>(COL_SENDER)
                .ok_or_else(|| SqlColdError::Convert("Transact requires sender".into()))?;
            let to: Address = r
                .get::<Option<Address>, _>(COL_TO_ADDRESS)
                .ok_or_else(|| SqlColdError::Convert("Transact requires to".into()))?;
            let value: U256 = r
                .get::<Option<U256>, _>(COL_VALUE)
                .ok_or_else(|| SqlColdError::Convert("Transact requires value".into()))?;
            let gas: U256 = r
                .get::<Option<U256>, _>(COL_GAS)
                .ok_or_else(|| SqlColdError::Convert("Transact requires gas".into()))?;
            let max_fee: U256 = r
                .get::<Option<U256>, _>(COL_MAX_FEE_PER_GAS)
                .ok_or_else(|| SqlColdError::Convert("Transact requires max_fee_per_gas".into()))?;
            let data: Bytes = r.get::<Option<Bytes>, _>(COL_DATA).unwrap_or_default();

            Ok(DbSignetEvent::Transact(
                order,
                Transact {
                    rollupChainId: rollup_chain_id,
                    sender,
                    to,
                    value,
                    gas,
                    maxFeePerGas: max_fee,
                    data,
                },
            ))
        }
        EVENT_ENTER => {
            let recipient: Address = r
                .get::<Option<Address>, _>(COL_ROLLUP_RECIPIENT)
                .ok_or_else(|| SqlColdError::Convert("Enter requires rollup_recipient".into()))?;
            let amount: U256 = r
                .get::<Option<U256>, _>(COL_AMOUNT)
                .ok_or_else(|| SqlColdError::Convert("Enter requires amount".into()))?;

            Ok(DbSignetEvent::Enter(
                order,
                Enter { rollupChainId: rollup_chain_id, rollupRecipient: recipient, amount },
            ))
        }
        EVENT_ENTER_TOKEN => {
            let token: Address = r
                .get::<Option<Address>, _>(COL_TOKEN)
                .ok_or_else(|| SqlColdError::Convert("EnterToken requires token".into()))?;
            let recipient: Address =
                r.get::<Option<Address>, _>(COL_ROLLUP_RECIPIENT).ok_or_else(|| {
                    SqlColdError::Convert("EnterToken requires rollup_recipient".into())
                })?;
            let amount: U256 = r
                .get::<Option<U256>, _>(COL_AMOUNT)
                .ok_or_else(|| SqlColdError::Convert("EnterToken requires amount".into()))?;

            Ok(DbSignetEvent::EnterToken(
                order,
                EnterToken {
                    rollupChainId: rollup_chain_id,
                    token,
                    rollupRecipient: recipient,
                    amount,
                },
            ))
        }
        _ => Err(SqlColdError::Convert(format!("invalid event_type: {event_type}"))),
    }
}

/// Build a [`DbZenithHeader`] from an [`sqlx::any::AnyRow`].
fn zenith_header_from_row(r: &sqlx::any::AnyRow) -> Result<DbZenithHeader, SqlColdError> {
    Ok(DbZenithHeader(Zenith::BlockHeader {
        hostBlockNumber: r.get(COL_HOST_BLOCK_NUMBER),
        rollupChainId: r.get(COL_ROLLUP_CHAIN_ID),
        gasLimit: r.get(COL_GAS_LIMIT),
        rewardAddress: r.get(COL_REWARD_ADDRESS),
        blockDataHash: r.get(COL_BLOCK_DATA_HASH),
    }))
}

// ============================================================================
// Domain type → SQL INSERT (write path)
// ============================================================================

/// Write a single block's data into an open SQL transaction.
async fn write_block_to_tx(
    tx: &mut sqlx::Transaction<'_, sqlx::Any>,
    data: BlockData,
) -> Result<(), SqlColdError> {
    let bn = to_i64(data.block_number());

    // Insert header
    let block_hash = data.header.hash_slow();
    let difficulty = &data.header.difficulty;
    sqlx::query(
        "INSERT INTO headers (
            block_number, block_hash, parent_hash, ommers_hash, beneficiary,
            state_root, transactions_root, receipts_root, logs_bloom, difficulty,
            gas_limit, gas_used, timestamp, extra_data, mix_hash, nonce,
            base_fee_per_gas, withdrawals_root, blob_gas_used, excess_blob_gas,
            parent_beacon_block_root, requests_hash
        ) VALUES (
            $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
            $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22
        )",
    )
    .bind(bn)
    .bind(block_hash)
    .bind(data.header.parent_hash)
    .bind(data.header.ommers_hash)
    .bind(data.header.beneficiary)
    .bind(data.header.state_root)
    .bind(data.header.transactions_root)
    .bind(data.header.receipts_root)
    .bind(data.header.logs_bloom)
    .bind(difficulty)
    .bind(to_i64(data.header.gas_limit))
    .bind(to_i64(data.header.gas_used))
    .bind(to_i64(data.header.timestamp))
    .bind(&data.header.extra_data)
    .bind(data.header.mix_hash)
    .bind(data.header.nonce)
    .bind(data.header.base_fee_per_gas.map(to_i64))
    .bind(data.header.withdrawals_root.as_ref())
    .bind(data.header.blob_gas_used.map(to_i64))
    .bind(data.header.excess_blob_gas.map(to_i64))
    .bind(data.header.parent_beacon_block_root.as_ref())
    .bind(data.header.requests_hash.as_ref())
    .execute(&mut **tx)
    .await?;

    // Insert transactions
    for (idx, recovered_tx) in data.transactions.iter().enumerate() {
        insert_transaction(tx, bn, to_i64(idx as u64), recovered_tx).await?;
    }

    // Insert receipts and logs, computing first_log_index as a running
    // sum of log counts (same algorithm as the MDBX IndexedReceipt path).
    let mut first_log_index = 0i64;
    for (idx, receipt) in data.receipts.iter().enumerate() {
        let tx_idx = to_i64(idx as u64);
        sqlx::query(
            "INSERT INTO receipts (block_number, tx_index, tx_type, success, cumulative_gas_used, first_log_index)
             VALUES ($1, $2, $3, $4, $5, $6)",
        )
        .bind(bn)
        .bind(tx_idx)
        .bind(receipt.tx_type as i32)
        .bind(receipt.inner.status.coerce_status() as i32)
        .bind(to_i64(receipt.inner.cumulative_gas_used))
        .bind(first_log_index)
        .execute(&mut **tx)
        .await?;
        first_log_index += receipt.inner.logs.len() as i64;

        for (log_idx, log) in receipt.inner.logs.iter().enumerate() {
            let topics = log.topics();
            sqlx::query(
                "INSERT INTO logs (block_number, tx_index, log_index, address, topic0, topic1, topic2, topic3, data)
                 VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
            )
            .bind(bn)
            .bind(tx_idx)
            .bind(to_i64(log_idx as u64))
            .bind(log.address)
            .bind(topics.first())
            .bind(topics.get(1))
            .bind(topics.get(2))
            .bind(topics.get(3))
            .bind(&log.data.data)
            .execute(&mut **tx)
            .await?;
        }
    }

    // Insert signet events
    for (idx, event) in data.signet_events.iter().enumerate() {
        insert_signet_event(tx, bn, to_i64(idx as u64), event).await?;
    }

    // Insert zenith header
    if let Some(zh) = &data.zenith_header {
        let h = &zh.0;
        sqlx::query(
            "INSERT INTO zenith_headers (
                block_number, host_block_number, rollup_chain_id,
                gas_limit, reward_address, block_data_hash
            ) VALUES ($1, $2, $3, $4, $5, $6)",
        )
        .bind(bn)
        .bind(h.hostBlockNumber)
        .bind(h.rollupChainId)
        .bind(h.gasLimit)
        .bind(h.rewardAddress)
        .bind(h.blockDataHash)
        .execute(&mut **tx)
        .await?;
    }

    Ok(())
}

/// Insert a transaction, binding directly from the source type.
async fn insert_transaction(
    conn: &mut sqlx::AnyConnection,
    bn: i64,
    tx_index: i64,
    recovered: &RecoveredTx,
) -> Result<(), SqlColdError> {
    use alloy::consensus::EthereumTxEnvelope;

    let sender = recovered.signer();
    let tx: &TransactionSigned = recovered;
    let tx_hash = tx.tx_hash();
    let tx_type = tx.tx_type() as i32;

    macro_rules! sig {
        ($s:expr) => {{
            let sig = $s.signature();
            (sig.v() as i32, sig.r(), sig.s())
        }};
    }
    let (sig_y, sig_r, sig_s) = match tx {
        EthereumTxEnvelope::Legacy(s) => sig!(s),
        EthereumTxEnvelope::Eip2930(s) => sig!(s),
        EthereumTxEnvelope::Eip1559(s) => sig!(s),
        EthereumTxEnvelope::Eip4844(s) => sig!(s),
        EthereumTxEnvelope::Eip7702(s) => sig!(s),
    };

    let (chain_id, nonce, gas_limit) = match tx {
        EthereumTxEnvelope::Legacy(s) => {
            (s.tx().chain_id.map(to_i64), to_i64(s.tx().nonce), to_i64(s.tx().gas_limit))
        }
        EthereumTxEnvelope::Eip2930(s) => {
            (Some(to_i64(s.tx().chain_id)), to_i64(s.tx().nonce), to_i64(s.tx().gas_limit))
        }
        EthereumTxEnvelope::Eip1559(s) => {
            (Some(to_i64(s.tx().chain_id)), to_i64(s.tx().nonce), to_i64(s.tx().gas_limit))
        }
        EthereumTxEnvelope::Eip4844(s) => {
            (Some(to_i64(s.tx().chain_id)), to_i64(s.tx().nonce), to_i64(s.tx().gas_limit))
        }
        EthereumTxEnvelope::Eip7702(s) => {
            (Some(to_i64(s.tx().chain_id)), to_i64(s.tx().nonce), to_i64(s.tx().gas_limit))
        }
    };

    let (value, to_addr) = match tx {
        EthereumTxEnvelope::Legacy(s) => (s.tx().value, s.tx().to.to()),
        EthereumTxEnvelope::Eip2930(s) => (s.tx().value, s.tx().to.to()),
        EthereumTxEnvelope::Eip1559(s) => (s.tx().value, s.tx().to.to()),
        EthereumTxEnvelope::Eip4844(s) => (s.tx().value, Some(&s.tx().to)),
        EthereumTxEnvelope::Eip7702(s) => (s.tx().value, Some(&s.tx().to)),
    };

    let input: &[u8] = match tx {
        EthereumTxEnvelope::Legacy(s) => s.tx().input.as_ref(),
        EthereumTxEnvelope::Eip2930(s) => s.tx().input.as_ref(),
        EthereumTxEnvelope::Eip1559(s) => s.tx().input.as_ref(),
        EthereumTxEnvelope::Eip4844(s) => s.tx().input.as_ref(),
        EthereumTxEnvelope::Eip7702(s) => s.tx().input.as_ref(),
    };

    let (gas_price, max_fee, max_priority_fee, max_blob_fee) = match tx {
        EthereumTxEnvelope::Legacy(s) => (Some(encode_u128(s.tx().gas_price)), None, None, None),
        EthereumTxEnvelope::Eip2930(s) => (Some(encode_u128(s.tx().gas_price)), None, None, None),
        EthereumTxEnvelope::Eip1559(s) => (
            None,
            Some(encode_u128(s.tx().max_fee_per_gas)),
            Some(encode_u128(s.tx().max_priority_fee_per_gas)),
            None,
        ),
        EthereumTxEnvelope::Eip4844(s) => (
            None,
            Some(encode_u128(s.tx().max_fee_per_gas)),
            Some(encode_u128(s.tx().max_priority_fee_per_gas)),
            Some(encode_u128(s.tx().max_fee_per_blob_gas)),
        ),
        EthereumTxEnvelope::Eip7702(s) => (
            None,
            Some(encode_u128(s.tx().max_fee_per_gas)),
            Some(encode_u128(s.tx().max_priority_fee_per_gas)),
            None,
        ),
    };

    let (access_list, blob_hashes, auth_list) = match tx {
        EthereumTxEnvelope::Legacy(_) => (None, None, None),
        EthereumTxEnvelope::Eip2930(s) => {
            (Some(encode_access_list(&s.tx().access_list)), None, None)
        }
        EthereumTxEnvelope::Eip1559(s) => {
            (Some(encode_access_list(&s.tx().access_list)), None, None)
        }
        EthereumTxEnvelope::Eip4844(s) => (
            Some(encode_access_list(&s.tx().access_list)),
            Some(encode_b256_vec(&s.tx().blob_versioned_hashes)),
            None,
        ),
        EthereumTxEnvelope::Eip7702(s) => (
            Some(encode_access_list(&s.tx().access_list)),
            None,
            Some(encode_authorization_list(&s.tx().authorization_list)),
        ),
    };

    sqlx::query(
        "INSERT INTO transactions (
            block_number, tx_index, tx_hash, tx_type,
            sig_y_parity, sig_r, sig_s,
            chain_id, nonce, gas_limit, to_address, value, input,
            gas_price, max_fee_per_gas, max_priority_fee_per_gas,
            max_fee_per_blob_gas, blob_versioned_hashes,
            access_list, authorization_list, from_address
        ) VALUES (
            $1, $2, $3, $4, $5, $6, $7, $8, $9, $10,
            $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21
        )",
    )
    .bind(bn)
    .bind(tx_index)
    .bind(tx_hash)
    .bind(tx_type)
    .bind(sig_y)
    .bind(sig_r)
    .bind(sig_s)
    .bind(chain_id)
    .bind(nonce)
    .bind(gas_limit)
    .bind(to_addr)
    .bind(value)
    .bind(input)
    .bind(gas_price.as_ref().map(|v| v.as_slice()))
    .bind(max_fee.as_ref().map(|v| v.as_slice()))
    .bind(max_priority_fee.as_ref().map(|v| v.as_slice()))
    .bind(max_blob_fee.as_ref().map(|v| v.as_slice()))
    .bind(blob_hashes.as_deref())
    .bind(access_list.as_deref())
    .bind(auth_list.as_deref())
    .bind(sender)
    .execute(&mut *conn)
    .await?;

    Ok(())
}

/// Insert a signet event, binding directly from the source type.
async fn insert_signet_event(
    conn: &mut sqlx::AnyConnection,
    block_number: i64,
    event_index: i64,
    event: &DbSignetEvent,
) -> Result<(), SqlColdError> {
    let (event_type, order, chain_id) = match event {
        DbSignetEvent::Transact(o, t) => (0i32, to_i64(*o), &t.rollupChainId),
        DbSignetEvent::Enter(o, e) => (1i32, to_i64(*o), &e.rollupChainId),
        DbSignetEvent::EnterToken(o, e) => (2i32, to_i64(*o), &e.rollupChainId),
    };

    let (value, gas, max_fee, amount) = match event {
        DbSignetEvent::Transact(_, t) => {
            (Some(&t.value), Some(&t.gas), Some(&t.maxFeePerGas), None)
        }
        DbSignetEvent::Enter(_, e) => (None, None, None, Some(&e.amount)),
        DbSignetEvent::EnterToken(_, e) => (None, None, None, Some(&e.amount)),
    };

    sqlx::query(
        "INSERT INTO signet_events (
            block_number, event_index, event_type, order_index,
            rollup_chain_id, sender, to_address, value, gas,
            max_fee_per_gas, data, rollup_recipient, amount, token
        ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)",
    )
    .bind(block_number)
    .bind(event_index)
    .bind(event_type)
    .bind(order)
    .bind(chain_id)
    .bind(match event {
        DbSignetEvent::Transact(_, t) => Some(&t.sender),
        _ => None,
    })
    .bind(match event {
        DbSignetEvent::Transact(_, t) => Some(&t.to),
        _ => None,
    })
    .bind(value)
    .bind(gas)
    .bind(max_fee)
    .bind(match event {
        DbSignetEvent::Transact(_, t) => Some(&t.data),
        _ => None,
    })
    .bind(match event {
        DbSignetEvent::Enter(_, e) => Some(&e.rollupRecipient),
        DbSignetEvent::EnterToken(_, e) => Some(&e.rollupRecipient),
        _ => None,
    })
    .bind(amount)
    .bind(match event {
        DbSignetEvent::EnterToken(_, e) => Some(&e.token),
        _ => None,
    })
    .execute(&mut *conn)
    .await?;

    Ok(())
}

// ============================================================================
// Log filter helpers
// ============================================================================

/// Append a SQL filter clause for a set of byte-encoded values.
///
/// For a single value, generates ` AND {column} = ${idx}`.
/// For multiple values, generates ` AND {column} IN (${idx}, ...)`.
/// Returns the next available parameter index.
fn append_filter_clause<'a>(
    clause: &mut String,
    params: &mut Vec<&'a [u8]>,
    mut idx: u32,
    column: &str,
    values: impl ExactSizeIterator<Item = &'a [u8]>,
) -> u32 {
    use std::fmt::Write;

    let len = values.len();
    if len == 1 {
        write!(clause, " AND {column} = ${idx}").unwrap();
        values.for_each(|v| params.push(v));
        return idx + 1;
    }
    write!(clause, " AND {column} IN (").unwrap();
    for (i, v) in values.enumerate() {
        if i > 0 {
            clause.push_str(", ");
        }
        write!(clause, "${idx}").unwrap();
        params.push(v);
        idx += 1;
    }
    clause.push(')');
    idx
}

fn build_log_filter_clause(filter: &Filter, start_idx: u32) -> (String, Vec<&[u8]>) {
    let mut clause = String::new();
    let mut params: Vec<&[u8]> = Vec::new();
    let mut idx = start_idx;

    if !filter.address.is_empty() {
        idx = append_filter_clause(
            &mut clause,
            &mut params,
            idx,
            "l.address",
            filter.address.iter().map(|a| a.as_slice()),
        );
    }

    let topic_cols = ["l.topic0", "l.topic1", "l.topic2", "l.topic3"];
    for (i, topic_filter) in filter.topics.iter().enumerate() {
        if topic_filter.is_empty() {
            continue;
        }
        idx = append_filter_clause(
            &mut clause,
            &mut params,
            idx,
            topic_cols[i],
            topic_filter.iter().map(|v| v.as_slice()),
        );
    }

    (clause, params)
}

// ============================================================================
// ColdStorage implementation
// ============================================================================

impl ColdStorage for SqlColdBackend {
    async fn get_header(&self, spec: HeaderSpecifier) -> ColdResult<Option<SealedHeader>> {
        let Some(block_num) = self.resolve_header_spec(spec).await? else {
            return Ok(None);
        };
        self.fetch_header_by_number(block_num).await.map_err(ColdStorageError::from)
    }

    async fn get_headers(
        &self,
        specs: Vec<HeaderSpecifier>,
    ) -> ColdResult<Vec<Option<SealedHeader>>> {
        let mut results = Vec::with_capacity(specs.len());
        for spec in specs {
            let header = self.get_header(spec).await?;
            results.push(header);
        }
        Ok(results)
    }

    async fn get_transaction(
        &self,
        spec: TransactionSpecifier,
    ) -> ColdResult<Option<Confirmed<RecoveredTx>>> {
        let row = match spec {
            TransactionSpecifier::Hash(hash) => sqlx::query(
                "SELECT t.*, h.block_hash
                     FROM transactions t
                     JOIN headers h ON t.block_number = h.block_number
                     WHERE t.tx_hash = $1",
            )
            .bind(hash)
            .fetch_optional(&self.pool)
            .await
            .map_err(SqlColdError::from)?,
            TransactionSpecifier::BlockAndIndex { block, index } => sqlx::query(
                "SELECT t.*, h.block_hash
                     FROM transactions t
                     JOIN headers h ON t.block_number = h.block_number
                     WHERE t.block_number = $1 AND t.tx_index = $2",
            )
            .bind(to_i64(block))
            .bind(to_i64(index))
            .fetch_optional(&self.pool)
            .await
            .map_err(SqlColdError::from)?,
            TransactionSpecifier::BlockHashAndIndex { block_hash, index } => sqlx::query(
                "SELECT t.*, h.block_hash
                     FROM transactions t
                     JOIN headers h ON t.block_number = h.block_number
                     WHERE h.block_hash = $1 AND t.tx_index = $2",
            )
            .bind(block_hash)
            .bind(to_i64(index))
            .fetch_optional(&self.pool)
            .await
            .map_err(SqlColdError::from)?,
        };

        let Some(r) = row else {
            return Ok(None);
        };

        let block = from_i64(r.get::<i64, _>(COL_BLOCK_NUMBER));
        let index = from_i64(r.get::<i64, _>(COL_TX_INDEX));
        let block_hash = r.get(COL_BLOCK_HASH);
        let recovered = recovered_tx_from_row(&r).map_err(ColdStorageError::from)?;
        let meta = ConfirmationMeta::new(block, block_hash, index);
        Ok(Some(Confirmed::new(recovered, meta)))
    }

    async fn get_transactions_in_block(&self, block: BlockNumber) -> ColdResult<Vec<RecoveredTx>> {
        let bn = to_i64(block);
        let rows =
            sqlx::query("SELECT * FROM transactions WHERE block_number = $1 ORDER BY tx_index")
                .bind(bn)
                .fetch_all(&self.pool)
                .await
                .map_err(SqlColdError::from)?;

        rows.iter().map(|r| recovered_tx_from_row(r).map_err(ColdStorageError::from)).collect()
    }

    async fn get_transaction_count(&self, block: BlockNumber) -> ColdResult<u64> {
        let bn = to_i64(block);
        let row = sqlx::query("SELECT COUNT(*) as cnt FROM transactions WHERE block_number = $1")
            .bind(bn)
            .fetch_one(&self.pool)
            .await
            .map_err(SqlColdError::from)?;

        Ok(from_i64(row.get::<i64, _>(COL_CNT)))
    }

    async fn get_receipt(&self, spec: ReceiptSpecifier) -> ColdResult<Option<ColdReceipt>> {
        // Resolve to (block, index)
        let (block, index) = match spec {
            ReceiptSpecifier::TxHash(hash) => {
                let row = sqlx::query(
                    "SELECT block_number, tx_index FROM transactions WHERE tx_hash = $1",
                )
                .bind(hash)
                .fetch_optional(&self.pool)
                .await
                .map_err(SqlColdError::from)?;
                let Some(r) = row else { return Ok(None) };
                (
                    from_i64(r.get::<i64, _>(COL_BLOCK_NUMBER)),
                    from_i64(r.get::<i64, _>(COL_TX_INDEX)),
                )
            }
            ReceiptSpecifier::BlockAndIndex { block, index } => (block, index),
        };

        let Some(header) = self.fetch_header_by_number(block).await? else {
            return Ok(None);
        };

        // Fetch receipt + tx_hash + from_address
        let receipt_row = sqlx::query(
            "SELECT r.*, t.tx_hash, t.from_address
             FROM receipts r
             JOIN transactions t ON r.block_number = t.block_number AND r.tx_index = t.tx_index
             WHERE r.block_number = $1 AND r.tx_index = $2",
        )
        .bind(to_i64(block))
        .bind(to_i64(index))
        .fetch_optional(&self.pool)
        .await
        .map_err(SqlColdError::from)?;

        let Some(rr) = receipt_row else {
            return Ok(None);
        };

        let bn: i64 = rr.get(COL_BLOCK_NUMBER);
        let tx_idx: i64 = rr.get(COL_TX_INDEX);
        let tx_hash = rr.get(COL_TX_HASH);
        let sender = rr.get(COL_FROM_ADDRESS);
        let tx_type = rr.get::<i32, _>(COL_TX_TYPE) as i16;
        let success = rr.get::<i32, _>(COL_SUCCESS) != 0;
        let cumulative_gas_used: i64 = rr.get(COL_CUMULATIVE_GAS_USED);

        let log_rows = sqlx::query(
            "SELECT * FROM logs WHERE block_number = $1 AND tx_index = $2 ORDER BY log_index",
        )
        .bind(bn)
        .bind(tx_idx)
        .fetch_all(&self.pool)
        .await
        .map_err(SqlColdError::from)?;

        let logs = log_rows.iter().map(log_from_row).collect();
        let built = build_receipt(tx_type, success, cumulative_gas_used, logs)
            .map_err(ColdStorageError::from)?;

        // Read first_log_index directly from the receipt row; compute
        // gas_used from the prior receipt's cumulative gas.
        let first_log_index: u64 = from_i64(rr.get::<i64, _>(COL_FIRST_LOG_INDEX));
        let prior = sqlx::query(
            "SELECT CAST(MAX(r.cumulative_gas_used) AS bigint) as prior_gas
             FROM receipts r WHERE r.block_number = $1 AND r.tx_index < $2",
        )
        .bind(to_i64(block))
        .bind(to_i64(index))
        .fetch_one(&self.pool)
        .await
        .map_err(SqlColdError::from)?;
        let prior_cumulative_gas: u64 =
            prior.get::<Option<i64>, _>(COL_PRIOR_GAS).unwrap_or(0) as u64;
        let gas_used = built.inner.cumulative_gas_used - prior_cumulative_gas;

        let ir = IndexedReceipt { receipt: built, tx_hash, first_log_index, gas_used, sender };
        Ok(Some(ColdReceipt::new(ir, &header, index)))
    }

    async fn get_receipts_in_block(&self, block: BlockNumber) -> ColdResult<Vec<ColdReceipt>> {
        let Some(header) =
            self.fetch_header_by_number(block).await.map_err(ColdStorageError::from)?
        else {
            return Ok(Vec::new());
        };

        let bn = to_i64(block);

        // Fetch receipts joined with tx_hash and from_address
        let receipt_rows = sqlx::query(
            "SELECT r.*, t.tx_hash, t.from_address
             FROM receipts r
             JOIN transactions t ON r.block_number = t.block_number AND r.tx_index = t.tx_index
             WHERE r.block_number = $1
             ORDER BY r.tx_index",
        )
        .bind(bn)
        .fetch_all(&self.pool)
        .await
        .map_err(SqlColdError::from)?;

        let all_log_rows =
            sqlx::query("SELECT * FROM logs WHERE block_number = $1 ORDER BY tx_index, log_index")
                .bind(bn)
                .fetch_all(&self.pool)
                .await
                .map_err(SqlColdError::from)?;

        // Group logs by tx_index
        let mut logs_by_tx: std::collections::BTreeMap<i64, Vec<Log>> =
            std::collections::BTreeMap::new();
        for r in &all_log_rows {
            let tx_idx: i64 = r.get(COL_TX_INDEX);
            logs_by_tx.entry(tx_idx).or_default().push(log_from_row(r));
        }

        let mut first_log_index = 0u64;
        let mut prior_cumulative_gas = 0u64;
        receipt_rows
            .into_iter()
            .enumerate()
            .map(|(idx, rr)| {
                let tx_idx: i64 = rr.get(COL_TX_INDEX);
                let tx_hash = rr.get(COL_TX_HASH);
                let sender = rr.get(COL_FROM_ADDRESS);
                let tx_type = rr.get::<i32, _>(COL_TX_TYPE) as i16;
                let success = rr.get::<i32, _>(COL_SUCCESS) != 0;
                let cumulative_gas_used: i64 = rr.get(COL_CUMULATIVE_GAS_USED);
                let logs = logs_by_tx.remove(&tx_idx).unwrap_or_default();
                let receipt = build_receipt(tx_type, success, cumulative_gas_used, logs)
                    .map_err(ColdStorageError::from)?;
                let gas_used = receipt.inner.cumulative_gas_used - prior_cumulative_gas;
                prior_cumulative_gas = receipt.inner.cumulative_gas_used;
                let ir = IndexedReceipt { receipt, tx_hash, first_log_index, gas_used, sender };
                first_log_index += ir.receipt.inner.logs.len() as u64;
                Ok(ColdReceipt::new(ir, &header, idx as u64))
            })
            .collect()
    }

    async fn get_signet_events(
        &self,
        spec: SignetEventsSpecifier,
    ) -> ColdResult<Vec<DbSignetEvent>> {
        let rows = match spec {
            SignetEventsSpecifier::Block(block) => {
                let bn = to_i64(block);
                sqlx::query(
                    "SELECT * FROM signet_events WHERE block_number = $1 ORDER BY event_index",
                )
                .bind(bn)
                .fetch_all(&self.pool)
                .await
                .map_err(SqlColdError::from)?
            }
            SignetEventsSpecifier::BlockRange { start, end } => {
                let s = to_i64(start);
                let e = to_i64(end);
                sqlx::query(
                    "SELECT * FROM signet_events WHERE block_number >= $1 AND block_number <= $2
                     ORDER BY block_number, event_index",
                )
                .bind(s)
                .bind(e)
                .fetch_all(&self.pool)
                .await
                .map_err(SqlColdError::from)?
            }
        };

        rows.iter().map(|r| signet_event_from_row(r).map_err(ColdStorageError::from)).collect()
    }

    async fn get_zenith_header(
        &self,
        spec: ZenithHeaderSpecifier,
    ) -> ColdResult<Option<DbZenithHeader>> {
        let block = match spec {
            ZenithHeaderSpecifier::Number(n) => n,
            ZenithHeaderSpecifier::Range { start, .. } => start,
        };
        let bn = to_i64(block);
        let row = sqlx::query("SELECT * FROM zenith_headers WHERE block_number = $1")
            .bind(bn)
            .fetch_optional(&self.pool)
            .await
            .map_err(SqlColdError::from)?;

        row.map(|r| zenith_header_from_row(&r)).transpose().map_err(ColdStorageError::from)
    }

    async fn get_zenith_headers(
        &self,
        spec: ZenithHeaderSpecifier,
    ) -> ColdResult<Vec<DbZenithHeader>> {
        let rows = match spec {
            ZenithHeaderSpecifier::Number(n) => {
                let bn = to_i64(n);
                sqlx::query("SELECT * FROM zenith_headers WHERE block_number = $1")
                    .bind(bn)
                    .fetch_all(&self.pool)
                    .await
                    .map_err(SqlColdError::from)?
            }
            ZenithHeaderSpecifier::Range { start, end } => {
                let s = to_i64(start);
                let e = to_i64(end);
                sqlx::query(
                    "SELECT * FROM zenith_headers WHERE block_number >= $1 AND block_number <= $2
                     ORDER BY block_number",
                )
                .bind(s)
                .bind(e)
                .fetch_all(&self.pool)
                .await
                .map_err(SqlColdError::from)?
            }
        };

        rows.iter().map(|r| zenith_header_from_row(r).map_err(ColdStorageError::from)).collect()
    }

    async fn get_logs(&self, filter: &Filter, max_logs: usize) -> ColdResult<Vec<RpcLog>> {
        let from = filter.get_from_block().unwrap_or(0);
        let to = filter.get_to_block().unwrap_or(u64::MAX);

        // Build WHERE clause: block range ($1, $2) + address/topic filters.
        let (filter_clause, params) = build_log_filter_clause(filter, 3);
        let where_clause = format!("l.block_number >= $1 AND l.block_number <= $2{filter_clause}");

        // Run a cheap COUNT(*) query first to reject queries that exceed
        // the limit without loading any row data.
        let count_sql = format!("SELECT COUNT(*) as cnt FROM logs l WHERE {where_clause}");
        let mut count_query = sqlx::query(&count_sql).bind(to_i64(from)).bind(to_i64(to));
        for param in &params {
            count_query = count_query.bind(*param);
        }
        let count_row = count_query.fetch_one(&self.pool).await.map_err(SqlColdError::from)?;
        let count = from_i64(count_row.get::<i64, _>(COL_CNT)) as usize;
        if count > max_logs {
            return Err(ColdStorageError::TooManyLogs { limit: max_logs });
        }

        // Fetch the actual log data with JOINs and the correlated subquery
        // for block_log_index (absolute position within block).
        let data_sql = format!(
            "SELECT l.*, h.block_hash, h.timestamp AS block_timestamp, t.tx_hash, \
               (r.first_log_index + l.log_index) AS block_log_index \
             FROM logs l \
             JOIN headers h ON l.block_number = h.block_number \
             JOIN transactions t ON l.block_number = t.block_number \
               AND l.tx_index = t.tx_index \
             JOIN receipts r ON l.block_number = r.block_number \
               AND l.tx_index = r.tx_index \
             WHERE {where_clause} \
             ORDER BY l.block_number, l.tx_index, l.log_index"
        );
        let mut query = sqlx::query(&data_sql).bind(to_i64(from)).bind(to_i64(to));
        for param in &params {
            query = query.bind(*param);
        }

        let rows = query.fetch_all(&self.pool).await.map_err(SqlColdError::from)?;

        rows.into_iter()
            .map(|r| {
                let log = log_from_row(&r);
                Ok(RpcLog {
                    inner: log,
                    block_hash: Some(r.get(COL_BLOCK_HASH)),
                    block_number: Some(from_i64(r.get::<i64, _>(COL_BLOCK_NUMBER))),
                    block_timestamp: Some(from_i64(r.get::<i64, _>(COL_BLOCK_TIMESTAMP))),
                    transaction_hash: Some(r.get(COL_TX_HASH)),
                    transaction_index: Some(from_i64(r.get::<i64, _>(COL_TX_INDEX))),
                    log_index: Some(from_i64(r.get::<i64, _>(COL_BLOCK_LOG_INDEX))),
                    removed: false,
                })
            })
            .collect::<ColdResult<Vec<_>>>()
    }

    async fn produce_log_stream(&self, filter: &Filter, params: signet_cold::StreamParams) {
        #[cfg(feature = "postgres")]
        if self.is_postgres {
            return self.produce_log_stream_pg(filter, params).await;
        }
        signet_cold::produce_log_stream_default(self, filter, params).await;
    }

    async fn get_latest_block(&self) -> ColdResult<Option<BlockNumber>> {
        let row = sqlx::query("SELECT MAX(block_number) as max_bn FROM headers")
            .fetch_one(&self.pool)
            .await
            .map_err(SqlColdError::from)?;
        Ok(row.get::<Option<i64>, _>(COL_MAX_BN).map(from_i64))
    }

    async fn append_block(&self, data: BlockData) -> ColdResult<()> {
        self.insert_block(data).await.map_err(ColdStorageError::from)
    }

    async fn append_blocks(&self, data: Vec<BlockData>) -> ColdResult<()> {
        let mut tx = self.pool.begin().await.map_err(SqlColdError::from)?;
        for block_data in data {
            write_block_to_tx(&mut tx, block_data).await.map_err(ColdStorageError::from)?;
        }
        tx.commit().await.map_err(SqlColdError::from)?;
        Ok(())
    }

    async fn truncate_above(&self, block: BlockNumber) -> ColdResult<()> {
        let bn = to_i64(block);
        let mut tx = self.pool.begin().await.map_err(SqlColdError::from)?;

        // Delete child tables first, then headers (preserves FK ordering).
        for table in
            ["logs", "transactions", "receipts", "signet_events", "zenith_headers", "headers"]
        {
            sqlx::query(&format!("DELETE FROM {table} WHERE block_number > $1"))
                .bind(bn)
                .execute(&mut *tx)
                .await
                .map_err(SqlColdError::from)?;
        }

        tx.commit().await.map_err(SqlColdError::from)?;
        Ok(())
    }
}

#[cfg(all(test, feature = "test-utils"))]
mod tests {
    use super::*;
    use signet_cold::conformance::conformance;

    #[tokio::test]
    async fn sqlite_conformance() {
        let backend = SqlColdBackend::connect("sqlite::memory:").await.unwrap();
        conformance(backend).await.unwrap();
    }

    #[tokio::test]
    async fn pg_conformance() {
        let Ok(url) = std::env::var("DATABASE_URL") else {
            eprintln!("skipping pg conformance: DATABASE_URL not set");
            return;
        };
        let backend = SqlColdBackend::connect(&url).await.unwrap();
        conformance(backend).await.unwrap();
    }
}