Skip to content

Commit

Permalink
Use txid_limit only in get_oldest_tx()
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Mar 25, 2024
1 parent 4f3e08b commit cf0b40f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,15 @@ impl Query {
fn confirmed_status(
&self,
script_hash: &[u8],
current_block_index: usize
current_block_index: usize,
use_txid_limit: bool,
) -> Result<(Vec<Txo>, Vec<SpendingInput>)> {
let mut funding = vec![];
let mut spending = vec![];
let read_store = self.app.read_store();

let txos = self.find_funding_outputs(read_store, script_hash, current_block_index)?;
if self.txid_limit > 0 && txos.len() > self.txid_limit {
if use_txid_limit && self.txid_limit > 0 && txos.len() > self.txid_limit {
bail!(
"{}+ transactions found, query may take a long time",
txos.len()
Expand All @@ -287,14 +288,15 @@ impl Query {
&self,
script_hash: &[u8],
confirmed_funding: &[Txo],
use_txid_limit: bool,
) -> Result<(Vec<Txo>, Vec<SpendingInput>)> {
let mut funding = vec![];
let mut spending = vec![];

let tracker = self.tracker.read().unwrap();

let txos = self.find_funding_outputs(tracker.index(), script_hash, 9999999999)?;
if self.txid_limit > 0 && txos.len() > self.txid_limit {
if use_txid_limit && self.txid_limit > 0 && txos.len() > self.txid_limit {
bail!(
"{}+ transactions found, query may take a long time",
txos.len()
Expand All @@ -311,20 +313,20 @@ impl Query {
Ok((funding, spending))
}

pub fn status(&self, script_hash: &[u8], current_block_index: usize) -> Result<Status> {
pub fn status(&self, script_hash: &[u8], current_block_index: usize, use_txid_limit: bool) -> Result<Status> {
let confirmed = self
.confirmed_status(script_hash, current_block_index)
.confirmed_status(script_hash, current_block_index, use_txid_limit)
.chain_err(|| "failed to get confirmed status")?;

let mempool = self
.mempool_status(script_hash, &confirmed.0)
.mempool_status(script_hash, &confirmed.0, use_txid_limit)
.chain_err(|| "failed to get mempool status")?;

Ok(Status { confirmed, mempool })
}

pub fn oldest_tx(&self, script_hash: &[u8], current_block_index: usize) -> Result<TxBlockIndex> {
let all_status = self.status(script_hash, current_block_index).unwrap();
let all_status = self.status(script_hash, current_block_index, true).unwrap();

all_status.oldest().chain_err(|| "no txs for address")
}
Expand Down
4 changes: 2 additions & 2 deletions src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Connection {

fn blockchain_scripthash_get_history(&self, params: &[Value]) -> Result<Value> {
let script_hash = hash_from_value(params.get(0)).chain_err(|| "bad script_hash")?;
let status = self.query.status(&script_hash[..], 9999999999)?;
let status = self.query.status(&script_hash[..], 9999999999, false)?;
Ok(json!(Value::Array(
status
.history()
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Connection {

fn blockchain_scripthash_get_utxos(&self, params: &[Value]) -> Result<Value> {
let script_hash = hash_from_value(params.get(0)).chain_err(|| "bad script_hash")?;
let status = self.query.status(&script_hash[..], 9999999999)?;
let status = self.query.status(&script_hash[..], 9999999999, false)?;

let mut dict = HashMap::new();
for item in status.funding().into_iter() {
Expand Down

0 comments on commit cf0b40f

Please sign in to comment.