Changeset View
Changeset View
Standalone View
Standalone View
chronik/chronik-indexer/src/query/txs.rs
// Copyright (c) 2023 The Bitcoin developers | // Copyright (c) 2023 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
//! Module for [`QueryTxs`], to query txs from mempool/db. | //! Module for [`QueryTxs`], to query txs from mempool/db. | ||||
use abc_rust_error::{Result, WrapErr}; | use abc_rust_error::{Result, WrapErr}; | ||||
use bitcoinsuite_core::tx::{Tx, TxId}; | use bitcoinsuite_core::{ | ||||
ser::BitcoinSer, | |||||
tx::{Tx, TxId}, | |||||
}; | |||||
use chronik_bridge::ffi; | use chronik_bridge::ffi; | ||||
use chronik_db::{ | use chronik_db::{ | ||||
db::Db, | db::Db, | ||||
io::{BlockReader, SpentByReader, TxReader}, | io::{BlockReader, SpentByReader, TxReader}, | ||||
mem::Mempool, | mem::Mempool, | ||||
}; | }; | ||||
use chronik_proto::proto; | use chronik_proto::proto; | ||||
use thiserror::Error; | use thiserror::Error; | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | pub fn tx_by_id(&self, txid: TxId) -> Result<proto::Tx> { | ||||
tx_entry.time_first_seen, | tx_entry.time_first_seen, | ||||
tx_entry.is_coinbase, | tx_entry.is_coinbase, | ||||
Some(&block), | Some(&block), | ||||
self.avalanche, | self.avalanche, | ||||
)) | )) | ||||
} | } | ||||
} | } | ||||
} | } | ||||
/// Query the raw serialized tx by txid. | |||||
/// | |||||
/// Serializes the tx if it's in the mempool, or reads the tx data from the | |||||
/// node's storage otherwise. | |||||
pub fn raw_tx_by_id(&self, txid: &TxId) -> Result<proto::RawTx> { | |||||
let raw_tx = match self.mempool.tx(txid) { | |||||
Some(mempool_tx) => mempool_tx.tx.ser().to_vec(), | |||||
None => { | |||||
let tx_reader = TxReader::new(self.db)?; | |||||
let block_reader = BlockReader::new(self.db)?; | |||||
let block_tx = | |||||
tx_reader.tx_by_txid(txid)?.ok_or(TxNotFound(*txid))?; | |||||
let block = block_reader | |||||
.by_height(block_tx.block_height)? | |||||
.ok_or(DbTxHasNoBlock(*txid))?; | |||||
ffi::load_raw_tx(block.file_num, block_tx.entry.data_pos) | |||||
.wrap_err(ReadFailure(*txid))? | |||||
} | |||||
}; | |||||
Ok(proto::RawTx { raw_tx }) | |||||
} | |||||
} | } |