Compare commits

...

1 Commits

Author SHA1 Message Date
Mononaut
d5f4c4cf52
Add chunked mempool txids endpoint 2024-01-25 20:52:07 +00:00
2 changed files with 31 additions and 0 deletions

View File

@ -1,3 +1,4 @@
use bitcoin::hashes::Hash;
use bounded_vec_deque::BoundedVecDeque;
use itertools::Itertools;
@ -288,6 +289,30 @@ 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();

View File

@ -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),