2026-01-21 12:34:59 +01:00
|
|
|
// Copyright (C) 2024-2026 Whiterun LLC
|
2024-03-04 16:40:50 +01:00
|
|
|
//
|
|
|
|
|
// 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;
|
2024-10-25 14:21:02 +02:00
|
|
|
use rusqlite::Connection;
|
2024-03-04 16:40:50 +01:00
|
|
|
|
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-21 13:25:35 +01:00
|
|
|
use crate::db::blob::{FromBlob, ToBlob};
|
|
|
|
|
|
2024-03-04 16:40:50 +01:00
|
|
|
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 {
|
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-21 13:25:35 +01:00
|
|
|
let txid_blob: Vec<u8> = txid_res?;
|
|
|
|
|
let txid = Txid::from_blob(&txid_blob).expect("invalid txid in db");
|
2024-03-04 16:40:50 +01:00
|
|
|
txids.insert(txid);
|
|
|
|
|
}
|
|
|
|
|
Ok(txids)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-25 14:21:02 +02:00
|
|
|
pub fn delete_mempool_txs<'a, I>(db_tx: &Connection, txids: I) -> Result<bool>
|
|
|
|
|
where
|
|
|
|
|
I: IntoIterator<Item = &'a Txid>,
|
|
|
|
|
{
|
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-21 13:25:35 +01:00
|
|
|
let txid_blobs: Vec<Vec<u8>> = txids.into_iter().map(|txid| txid.to_blob()).collect();
|
|
|
|
|
let placeholders = txid_blobs
|
2024-10-25 14:21:02 +02:00
|
|
|
.iter()
|
|
|
|
|
.map(|_| "?")
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.join(", ");
|
2025-07-16 09:31:14 +02:00
|
|
|
let query = format!("DELETE FROM tx WHERE txid IN ({placeholders}) AND blockhash is NULL");
|
2024-10-25 14:21:02 +02:00
|
|
|
|
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-21 13:25:35 +01:00
|
|
|
let params: Vec<&dyn rusqlite::ToSql> = txid_blobs
|
2024-10-25 14:21:02 +02:00
|
|
|
.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))?;
|
2024-03-04 16:40:50 +01:00
|
|
|
|
|
|
|
|
Ok(rows_deleted != 0)
|
|
|
|
|
}
|