Changeset View
Changeset View
Standalone View
Standalone View
chronik/chronik-indexer/src/query/blocks.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 [`QueryBlocks`], to query blocks. | //! Module for [`QueryBlocks`], to query blocks. | ||||
use abc_rust_error::{Result, WrapErr}; | use abc_rust_error::{Result, WrapErr}; | ||||
use bitcoinsuite_core::{ | use bitcoinsuite_core::{ | ||||
block::BlockHash, | block::BlockHash, | ||||
tx::{Tx, TxId}, | tx::{Tx, TxId}, | ||||
}; | }; | ||||
use chronik_bridge::ffi; | |||||
use chronik_db::{ | use chronik_db::{ | ||||
db::Db, | db::Db, | ||||
io::{ | io::{ | ||||
BlockHeight, BlockReader, BlockStats, BlockStatsReader, DbBlock, | BlockHeight, BlockReader, BlockStats, BlockStatsReader, DbBlock, | ||||
SpentByReader, TxNum, TxReader, | SpentByReader, TxNum, TxReader, | ||||
}, | }, | ||||
mem::Mempool, | mem::Mempool, | ||||
}; | }; | ||||
use chronik_proto::proto; | use chronik_proto::proto; | ||||
use thiserror::Error; | use thiserror::Error; | ||||
use crate::{ | use crate::{ | ||||
avalanche::Avalanche, | avalanche::Avalanche, | ||||
indexer::Node, | |||||
query::{make_tx_proto, HashOrHeight, OutputsSpent, TxTokenData}, | query::{make_tx_proto, HashOrHeight, OutputsSpent, TxTokenData}, | ||||
}; | }; | ||||
const MAX_BLOCKS_PAGE_SIZE: usize = 500; | const MAX_BLOCKS_PAGE_SIZE: usize = 500; | ||||
/// Smallest allowed page size | /// Smallest allowed page size | ||||
pub const MIN_BLOCK_TXS_PAGE_SIZE: usize = 1; | pub const MIN_BLOCK_TXS_PAGE_SIZE: usize = 1; | ||||
/// Largest allowed page size | /// Largest allowed page size | ||||
pub const MAX_BLOCK_TXS_PAGE_SIZE: usize = 200; | pub const MAX_BLOCK_TXS_PAGE_SIZE: usize = 200; | ||||
/// Struct for querying blocks from the DB. | /// Struct for querying blocks from the DB. | ||||
#[derive(Debug)] | #[derive(Debug)] | ||||
pub struct QueryBlocks<'a> { | pub struct QueryBlocks<'a> { | ||||
/// Db. | /// Db. | ||||
pub db: &'a Db, | pub db: &'a Db, | ||||
/// Avalanche. | /// Avalanche. | ||||
pub avalanche: &'a Avalanche, | pub avalanche: &'a Avalanche, | ||||
/// Mempool | /// Mempool | ||||
pub mempool: &'a Mempool, | pub mempool: &'a Mempool, | ||||
/// Access to bitcoind to read txs | |||||
pub node: &'a Node, | |||||
/// Whether the SLP/ALP token index is enabled | /// Whether the SLP/ALP token index is enabled | ||||
pub is_token_index_enabled: bool, | pub is_token_index_enabled: bool, | ||||
} | } | ||||
/// Errors indicating something went wrong with querying blocks. | /// Errors indicating something went wrong with querying blocks. | ||||
#[derive(Debug, Error, PartialEq)] | #[derive(Debug, Error, PartialEq)] | ||||
pub enum QueryBlockError { | pub enum QueryBlockError { | ||||
/// Block not found in DB | /// Block not found in DB | ||||
▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | ) -> Result<proto::TxHistoryPage> { | ||||
.min(tx_range.end); | .min(tx_range.end); | ||||
let num_page_txs = (page_tx_num_end - page_tx_num_start) as usize; | let num_page_txs = (page_tx_num_end - page_tx_num_start) as usize; | ||||
let mut txs = Vec::with_capacity(num_page_txs); | let mut txs = Vec::with_capacity(num_page_txs); | ||||
for tx_num in page_tx_num_start..page_tx_num_end { | for tx_num in page_tx_num_start..page_tx_num_end { | ||||
let db_tx = tx_reader | let db_tx = tx_reader | ||||
.tx_by_tx_num(tx_num)? | .tx_by_tx_num(tx_num)? | ||||
.ok_or(BlockHasMissingTx(db_block.hash.clone(), tx_num))?; | .ok_or(BlockHasMissingTx(db_block.hash.clone(), tx_num))?; | ||||
let tx = Tx::from( | let tx = Tx::from( | ||||
ffi::load_tx( | self.node | ||||
.bridge | |||||
.load_tx( | |||||
db_block.file_num, | db_block.file_num, | ||||
db_tx.entry.data_pos, | db_tx.entry.data_pos, | ||||
db_tx.entry.undo_pos, | db_tx.entry.undo_pos, | ||||
) | ) | ||||
.wrap_err(ReadFailure(db_tx.entry.txid))?, | .wrap_err(ReadFailure(db_tx.entry.txid))?, | ||||
); | ); | ||||
let outputs_spent = OutputsSpent::query( | let outputs_spent = OutputsSpent::query( | ||||
&spent_by_reader, | &spent_by_reader, | ||||
&tx_reader, | &tx_reader, | ||||
self.mempool.spent_by().outputs_spent(&db_tx.entry.txid), | self.mempool.spent_by().outputs_spent(&db_tx.entry.txid), | ||||
tx_num, | tx_num, | ||||
)?; | )?; | ||||
let token = TxTokenData::from_db( | let token = TxTokenData::from_db( | ||||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |