Skip to content

Commit f010183

Browse files
committed
feat: add get_mempool_recent_txs
1 parent 1c9c1b4 commit f010183

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
@@ -949,6 +949,56 @@ mod test {
949949
}
950950
}
951951

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

0 commit comments

Comments
 (0)