Skip to content

Commit 30e3b95

Browse files
committed
feat: add get_mempool_recent_txs
1 parent bbb3859 commit 30e3b95

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

src/async.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ use log::{debug, error, info, trace};
2727
use reqwest::{header, Client, Response};
2828

2929
use crate::{
30-
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MempoolStats,
31-
MerkleProof, OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo,
32-
BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
30+
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MempoolRecentTxs,
31+
MempoolStats, MerkleProof, OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus,
32+
Utxo, BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
3333
};
3434

3535
#[derive(Debug, Clone)]
@@ -464,6 +464,11 @@ impl<S: Sleeper> AsyncClient<S> {
464464
self.get_response_json("/mempool").await
465465
}
466466

467+
// Get a list of the last 10 transactions to enter the mempool.
468+
pub async fn get_mempool_recent_txs(&self) -> Result<Vec<MempoolRecentTxs>, Error> {
469+
self.get_response_json("/mempool/recent").await
470+
}
471+
467472
/// Get the full list of transaction IDs in the mempool.
468473
///
469474
/// The order of the txids is arbitrary and does not match bitcoind's.

src/blocking.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ use bitcoin::hex::{DisplayHex, FromHex};
2828
use bitcoin::{Address, Block, BlockHash, MerkleBlock, Script, Transaction, Txid};
2929

3030
use crate::{
31-
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MempoolStats,
32-
MerkleProof, OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo,
33-
BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
31+
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MempoolRecentTxs,
32+
MempoolStats, MerkleProof, OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus,
33+
Utxo, BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
3434
};
3535

3636
#[derive(Debug, Clone)]
@@ -321,6 +321,11 @@ impl BlockingClient {
321321
self.get_response_json("/mempool")
322322
}
323323

324+
// Get a list of the last 10 transactions to enter the mempool.
325+
pub fn get_mempool_recent_txs(&self) -> Result<Vec<MempoolRecentTxs>, Error> {
326+
self.get_response_json("/mempool/recent")
327+
}
328+
324329
/// Get the full list of transaction IDs in the mempool.
325330
///
326331
/// The order of the txids is arbitrary and does not match bitcoind's.

src/lib.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,56 @@ mod test {
947947
}
948948
}
949949

950+
#[cfg(all(feature = "blocking", feature = "async"))]
951+
#[tokio::test]
952+
async fn test_get_mempool_recent_txs() {
953+
let (blocking_client, async_client) = setup_clients().await;
954+
let address = BITCOIND
955+
.client
956+
.new_address_with_type(AddressType::Legacy)
957+
.unwrap();
958+
959+
let _miner = MINER.lock().await;
960+
961+
let mut expected_txids = Vec::new();
962+
for _ in 0..5 {
963+
let txid = BITCOIND
964+
.client
965+
.send_to_address(&address, Amount::from_sat(1000))
966+
.unwrap()
967+
.txid()
968+
.unwrap();
969+
expected_txids.push(txid);
970+
}
971+
972+
// Sleep for 5 seconds so transactions have time to propagate
973+
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
974+
975+
let recent_blocking = blocking_client.get_mempool_recent_txs().unwrap();
976+
let recent_async = async_client.get_mempool_recent_txs().await.unwrap();
977+
978+
assert_eq!(recent_blocking, recent_async);
979+
980+
// Should return up to 10 transactions
981+
assert!(recent_blocking.len() <= 10);
982+
assert!(!recent_blocking.is_empty());
983+
984+
// Our most recent transactions should be in there
985+
let returned_txids: Vec<Txid> = recent_blocking.iter().map(|tx| tx.txid).collect();
986+
for expected_txid in &expected_txids {
987+
assert!(
988+
returned_txids.contains(expected_txid),
989+
"Txid {} not found in recent mempool txs",
990+
expected_txid
991+
);
992+
}
993+
994+
for tx in &recent_blocking {
995+
assert!(tx.vsize > 0);
996+
assert!(tx.value > 0);
997+
}
998+
}
999+
9501000
#[cfg(all(feature = "blocking", feature = "async"))]
9511001
#[tokio::test]
9521002
async fn test_get_fee_estimates() {

0 commit comments

Comments
 (0)