Skip to content

Commit 49c4d24

Browse files
committed
feat(client): add get_mempool_stats
1 parent 1d6bc57 commit 49c4d24

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-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, MerkleProof,
31-
OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo, BASE_BACKOFF_MILLIS,
32-
RETRYABLE_ERROR_CODES,
30+
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MempoolStats,
31+
MerkleProof, OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo,
32+
BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
3333
};
3434

3535
#[derive(Debug, Clone)]
@@ -453,6 +453,11 @@ impl<S: Sleeper> AsyncClient<S> {
453453
self.get_response_json(&path).await
454454
}
455455

456+
/// Get statistics about the mempool.
457+
pub async fn get_mempool_stats(&self) -> Result<MempoolStats, Error> {
458+
self.get_response_json("/mempool").await
459+
}
460+
456461
/// Get an map where the key is the confirmation target (in number of
457462
/// blocks) and the value is the estimated feerate (in sat/vB).
458463
pub async fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {

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, MerkleProof,
32-
OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo, BASE_BACKOFF_MILLIS,
33-
RETRYABLE_ERROR_CODES,
31+
AddressStats, BlockInformation, BlockStatus, BlockSummary, Builder, Error, MempoolStats,
32+
MerkleProof, OutputSpendStatus, OutputStatus, ScriptHashStats, Tx, TxStatus, Utxo,
33+
BASE_BACKOFF_MILLIS, RETRYABLE_ERROR_CODES,
3434
};
3535

3636
#[derive(Debug, Clone)]
@@ -316,6 +316,11 @@ impl BlockingClient {
316316
.map(|s| BlockHash::from_str(s.as_str()).map_err(Error::HexToArray))?
317317
}
318318

319+
/// Get statistics about the mempool.
320+
pub fn get_mempool_stats(&self) -> Result<MempoolStats, Error> {
321+
self.get_response_json("/mempool")
322+
}
323+
319324
/// Get an map where the key is the confirmation target (in number of
320325
/// blocks) and the value is the estimated feerate (in sat/vB).
321326
pub fn get_fee_estimates(&self) -> Result<HashMap<u16, f64>, Error> {

src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,39 @@ mod test {
875875
assert_eq!(txid_at_block_index, txid_at_block_index_async);
876876
}
877877

878+
#[cfg(all(feature = "blocking", feature = "async"))]
879+
#[tokio::test]
880+
async fn test_get_mempool_stats() {
881+
let (blocking_client, async_client) = setup_clients().await;
882+
let address = BITCOIND
883+
.client
884+
.new_address_with_type(AddressType::Legacy)
885+
.unwrap();
886+
887+
for _ in 0..5 {
888+
BITCOIND
889+
.client
890+
.send_to_address(&address, Amount::from_sat(1000))
891+
.unwrap();
892+
}
893+
894+
// Sleep for 5 seconds so the transaction has time to propagate to electrs' mempool.
895+
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
896+
897+
let stats_blocking = blocking_client.get_mempool_stats().unwrap();
898+
let stats_async = async_client.get_mempool_stats().await.unwrap();
899+
900+
assert_eq!(stats_blocking, stats_async);
901+
902+
assert!(stats_blocking.count >= 5);
903+
assert!(stats_blocking.vsize > 0);
904+
assert!(stats_blocking.total_fee > 0);
905+
906+
if stats_blocking.count > 0 {
907+
assert!(!stats_blocking.fee_histogram.is_empty());
908+
}
909+
}
910+
878911
#[cfg(all(feature = "blocking", feature = "async"))]
879912
#[tokio::test]
880913
async fn test_get_fee_estimates() {

0 commit comments

Comments
 (0)