From fd5e6217464d65eda247cd1cdd2085f7d1dcb55f Mon Sep 17 00:00:00 2001 From: Mononaut Date: Wed, 2 Aug 2023 13:10:11 +0900 Subject: [PATCH 1/3] Add a POST /mempool/txs bulk query-by-txid endpoint --- src/rest.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/rest.rs b/src/rest.rs index 9e92246e..6e04af67 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -12,7 +12,7 @@ use crate::util::{ use {bitcoin::consensus::encode, std::str::FromStr}; use bitcoin::blockdata::opcodes; -use bitcoin::hashes::hex::{FromHex, ToHex}; +use bitcoin::hashes::hex::{Error as HashHexError, FromHex, ToHex}; use bitcoin::hashes::Error as HashError; use hex::{self, FromHexError}; use hyper::service::{make_service_fn, service_fn}; @@ -1128,6 +1128,26 @@ fn handle_request( json_response(prepare_txs(txs, query, config), TTL_SHORT) } + (&Method::POST, Some(&"mempool"), Some(&"txs"), None, None, None) => { + let txid_strings: Vec = + serde_json::from_slice(&body).map_err(|err| HttpError::from(err.to_string()))?; + + match txid_strings + .into_iter() + .map(|txid| Txid::from_hex(&txid)) + .collect::, HashHexError>>() + { + Ok(txids) => { + let txs: Vec<(Transaction, Option)> = txids + .iter() + .filter_map(|txid| query.mempool().lookup_txn(txid).map(|tx| (tx, None))) + .collect(); + + json_response(prepare_txs(txs, query, config), TTL_SHORT) + } + Err(err) => http_message(StatusCode::BAD_REQUEST, err.to_string(), 0), + } + } (&Method::GET, Some(&"mempool"), Some(&"txs"), last_seen_txid, None, None) => { let last_seen_txid = last_seen_txid.and_then(|txid| Txid::from_hex(txid).ok()); let txs = query From e1e6775706cd1e532bcee2ece8672087d29ae677 Mon Sep 17 00:00:00 2001 From: mononaut <83316221+mononaut@users.noreply.github.com> Date: Thu, 3 Aug 2023 15:33:26 +0900 Subject: [PATCH 2/3] move query.mempool() out of filter_map closure Co-authored-by: Jonathan Underwood --- src/rest.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/rest.rs b/src/rest.rs index 6e04af67..dfe7823c 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -1138,10 +1138,13 @@ fn handle_request( .collect::, HashHexError>>() { Ok(txids) => { - let txs: Vec<(Transaction, Option)> = txids - .iter() - .filter_map(|txid| query.mempool().lookup_txn(txid).map(|tx| (tx, None))) - .collect(); + let txs: Vec<(Transaction, Option)> = { + let mempool = query.mempool(); + txids + .iter() + .filter_map(|txid| mempool.lookup_txn(txid).map(|tx| (tx, None))) + .collect() + }; json_response(prepare_txs(txs, query, config), TTL_SHORT) } From 96f01e6a35e093776ddf72aaefdecf867f492b6f Mon Sep 17 00:00:00 2001 From: Mononaut Date: Thu, 3 Aug 2023 15:34:49 +0900 Subject: [PATCH 3/3] implement /mempool/txs suggestions --- src/rest.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/rest.rs b/src/rest.rs index dfe7823c..625f7643 100644 --- a/src/rest.rs +++ b/src/rest.rs @@ -12,7 +12,7 @@ use crate::util::{ use {bitcoin::consensus::encode, std::str::FromStr}; use bitcoin::blockdata::opcodes; -use bitcoin::hashes::hex::{Error as HashHexError, FromHex, ToHex}; +use bitcoin::hashes::hex::{FromHex, ToHex}; use bitcoin::hashes::Error as HashError; use hex::{self, FromHexError}; use hyper::service::{make_service_fn, service_fn}; @@ -1135,7 +1135,7 @@ fn handle_request( match txid_strings .into_iter() .map(|txid| Txid::from_hex(&txid)) - .collect::, HashHexError>>() + .collect::, _>>() { Ok(txids) => { let txs: Vec<(Transaction, Option)> = { @@ -1146,7 +1146,7 @@ fn handle_request( .collect() }; - json_response(prepare_txs(txs, query, config), TTL_SHORT) + json_response(prepare_txs(txs, query, config), 0) } Err(err) => http_message(StatusCode::BAD_REQUEST, err.to_string(), 0), }