riftenlabs-indexer/src/db/cauldron/mempool.rs
Dagur Valberg Johannsson 58fd9595d7
Convert hash columns from TEXT to BLOB storage
Migrate all 32-byte hash fields from TEXT (64-char hex) to BLOB
(32 bytes) for ~50% storage savings on hash columns.

Changes:
- Add src/db/blob.rs with ToBlob/FromBlob traits for hash types
- Update all table schemas to use BLOB for hash columns
- Replace .to_hex() with .to_blob() for DB inserts
- Replace ::from_hex() with ::from_blob() for DB reads
- Use SQL hex() function where API responses need hex strings
- Bump DB_VERSION from 4 to 5

Affected tables: tx, pool, pool_history_entry, utxo_spending,
utxo_funding, user_action, auth_chain_entry, bcmr_data,
bcmr_failure, bcmr_well_known, crc20, crc20_candidates,
delphi_entry, cached_token_metrics

Fixes #1
2026-01-22 08:30:17 +01:00

48 lines
1.5 KiB
Rust

// Copyright (C) 2024-2026 Whiterun LLC
//
// This software is licensed under the GNU Affero General Public License (AGPL), version 3.0 or later.
// A copy of the license can be found in the LICENSE file or at https://www.gnu.org/licenses/agpl-3.0.html
use std::collections::HashSet;
use anyhow::Result;
use bitcoincash::Txid;
use rusqlite::Connection;
use crate::db::blob::{FromBlob, ToBlob};
pub fn load_mempool(conn: &Connection) -> Result<HashSet<Txid>> {
let mut stmt = conn.prepare("SELECT txid FROM tx WHERE blockhash is NULL")?;
let txid_iter = stmt.query_map([], |row| row.get(0))?;
let mut txids: HashSet<Txid> = HashSet::new();
for txid_res in txid_iter {
let txid_blob: Vec<u8> = txid_res?;
let txid = Txid::from_blob(&txid_blob).expect("invalid txid in db");
txids.insert(txid);
}
Ok(txids)
}
pub fn delete_mempool_txs<'a, I>(db_tx: &Connection, txids: I) -> Result<bool>
where
I: IntoIterator<Item = &'a Txid>,
{
let txid_blobs: Vec<Vec<u8>> = txids.into_iter().map(|txid| txid.to_blob()).collect();
let placeholders = txid_blobs
.iter()
.map(|_| "?")
.collect::<Vec<_>>()
.join(", ");
let query = format!("DELETE FROM tx WHERE txid IN ({placeholders}) AND blockhash is NULL");
let params: Vec<&dyn rusqlite::ToSql> = txid_blobs
.iter()
.map(|s| s as &dyn rusqlite::ToSql)
.collect();
let mut stmt = db_tx.prepare(&query)?;
let rows_deleted = stmt.execute(rusqlite::params_from_iter(params))?;
Ok(rows_deleted != 0)
}