riftenlabs-indexer/src/db/cauldron/mempool.rs

49 lines
1.5 KiB
Rust
Raw Normal View History

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;
use rusqlite::Connection;
2024-03-04 16:40:50 +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 {
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)
}
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(", ");
2025-07-16 09:31:14 +02:00
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))?;
2024-03-04 16:40:50 +01:00
Ok(rows_deleted != 0)
}