Skip to content

Commit 16a6fb0

Browse files
committed
feat: add get_mempool_txids
1 parent 31e744c commit 16a6fb0

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/async.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,13 @@ impl<S: Sleeper> AsyncClient<S> {
464464
self.get_response_json("/mempool").await
465465
}
466466

467+
/// Get the full list of transaction IDs in the mempool.
468+
///
469+
/// The order of the txids is arbitrary and does not match bitcoind's.
470+
pub async fn get_mempool_txids(&self) -> Result<Vec<Txid>, Error> {
471+
self.get_response_json("/mempool/txids").await
472+
}
473+
467474
/// Get an map where the key is the confirmation target (in number of
468475
/// blocks) and the value is the estimated feerate (in sat/vB).
469476
pub async fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {

src/blocking.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,13 @@ impl BlockingClient {
321321
self.get_response_json("/mempool")
322322
}
323323

324+
/// Get the full list of transaction IDs in the mempool.
325+
///
326+
/// The order of the txids is arbitrary and does not match bitcoind's.
327+
pub fn get_mempool_txids(&self) -> Result<Vec<Txid>, Error> {
328+
self.get_response_json("/mempool/txids")
329+
}
330+
324331
/// Get an map where the key is the confirmation target (in number of
325332
/// blocks) and the value is the estimated feerate (in sat/vB).
326333
pub fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {

src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,47 @@ mod test {
906906
}
907907
}
908908

909+
#[cfg(all(feature = "blocking", feature = "async"))]
910+
#[tokio::test]
911+
async fn test_get_mempool_txids() {
912+
let (blocking_client, async_client) = setup_clients().await;
913+
914+
let address = BITCOIND
915+
.client
916+
.new_address_with_type(AddressType::Legacy)
917+
.unwrap();
918+
919+
let _miner = MINER.lock().await;
920+
921+
let mut expected_txids = Vec::new();
922+
for _ in 0..5 {
923+
let txid = BITCOIND
924+
.client
925+
.send_to_address(&address, Amount::from_sat(1000))
926+
.unwrap()
927+
.txid()
928+
.unwrap();
929+
expected_txids.push(txid);
930+
}
931+
932+
// Sleep for 5 seconds so the transaction has time to propagate to electrs' mempool.
933+
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
934+
935+
let txids_blocking = blocking_client.get_mempool_txids().unwrap();
936+
let txids_async = async_client.get_mempool_txids().await.unwrap();
937+
938+
assert_eq!(txids_blocking, txids_async);
939+
assert!(txids_blocking.len() >= 5);
940+
941+
for expected_txid in &expected_txids {
942+
assert!(
943+
txids_blocking.contains(expected_txid),
944+
"Txid {} not found in mempool",
945+
expected_txid
946+
);
947+
}
948+
}
949+
909950
#[cfg(all(feature = "blocking", feature = "async"))]
910951
#[tokio::test]
911952
async fn test_get_fee_estimates() {

0 commit comments

Comments
 (0)