Skip to content

Commit

Permalink
Add chunked mempool txids endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mononaut committed Jan 25, 2024
1 parent 13e5023 commit 5e28629
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/new_index/mempool.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bitcoin::hashes::Hash;
use bounded_vec_deque::BoundedVecDeque;
use itertools::Itertools;

Expand Down Expand Up @@ -288,6 +289,23 @@ impl Mempool {
self.txstore.keys().collect()
}

// Get all txids in the mempool with the given prefix
pub fn txids_by_prefix(&self, prefix: &str) -> Result<Vec<&Txid>> {
let _timer = self.latency.with_label_values(&["txids_by_prefix"]).start_timer();

// get Txid range bounds for the given prefix
let start_bytes = hex::decode(format!("{:0<64}", prefix)).chain_err(|| "invalid hash prefix")?;
let end_bytes = hex::decode(format!("{:f<64}", prefix)).chain_err(|| "invalid hash prefix")?;
let start_txid = Txid::from_hash(Hash::from_slice(&start_bytes).chain_err(|| "invalid hash prefix")?);
let end_txid = Txid::from_hash(Hash::from_slice(&end_bytes).chain_err(|| "invalid hash prefix")?);

Ok(self
.txstore
.range(start_txid..=end_txid)
.map(|(k, _v)| k)
.collect())
}

// Get all txs in the mempool
pub fn txs(&self) -> Vec<Transaction> {
let _timer = self.latency.with_label_values(&["txs"]).start_timer();
Expand Down
6 changes: 6 additions & 0 deletions src/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1256,6 +1256,12 @@ fn handle_request(
(&Method::GET, Some(&"mempool"), Some(&"txids"), None, None, None) => {
json_response(query.mempool().txids(), TTL_SHORT)
}
(&Method::GET, Some(&"mempool"), Some(&"txids"), Some(prefix), None, None) => {
match query.mempool().txids_by_prefix(prefix) {
Ok(txids) => json_response(txids, TTL_SHORT),
Err(err) => http_message(StatusCode::BAD_REQUEST, err.to_string(), 0),
}
}
(
&Method::GET,
Some(&INTERNAL_PREFIX),
Expand Down

0 comments on commit 5e28629

Please sign in to comment.