Skip to content

Commit 31e744c

Browse files
committed
feat: add get_mempool_stats
1 parent 4538381 commit 31e744c

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)]
@@ -459,6 +459,11 @@ impl<S: Sleeper> AsyncClient<S> {
459459
self.get_response_json(&path).await
460460
}
461461

462+
/// Get statistics about the mempool.
463+
pub async fn get_mempool_stats(&self) -> Result<MempoolStats, Error> {
464+
self.get_response_json("/mempool").await
465+
}
466+
462467
/// Get an map where the key is the confirmation target (in number of
463468
/// blocks) and the value is the estimated feerate (in sat/vB).
464469
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
@@ -873,6 +873,39 @@ mod test {
873873
assert_eq!(txid_at_block_index, txid_at_block_index_async);
874874
}
875875

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

0 commit comments

Comments
 (0)