feat(examples): added txpoolExt_clearTxpool to existing example (#18175)

This commit is contained in:
TMOT
2025-09-01 10:07:52 +00:00
committed by GitHub
parent e9a57a72c8
commit d69fda1a2b

View File

@@ -79,6 +79,10 @@ pub trait TxpoolExtApi {
#[method(name = "transactionCount")]
fn transaction_count(&self) -> RpcResult<usize>;
/// Clears the transaction pool.
#[method(name = "clearTxpool")]
fn clear_txpool(&self) -> RpcResult<()>;
/// Creates a subscription that returns the number of transactions in the pool every 10s.
#[subscription(name = "subscribeTransactionCount", item = usize)]
fn subscribe_transaction_count(
@@ -101,6 +105,12 @@ where
Ok(self.pool.pool_size().total)
}
fn clear_txpool(&self) -> RpcResult<()> {
let all_tx_hashes = self.pool.all_transaction_hashes();
self.pool.remove_transactions(all_tx_hashes);
Ok(())
}
fn subscribe_transaction_count(
&self,
pending_subscription_sink: PendingSubscriptionSink,
@@ -148,6 +158,12 @@ mod tests {
Ok(self.pool.pool_size().total)
}
fn clear_txpool(&self) -> RpcResult<()> {
let all_tx_hashes = self.pool.all_transaction_hashes();
self.pool.remove_transactions(all_tx_hashes);
Ok(())
}
fn subscribe_transaction_count(
&self,
pending: PendingSubscriptionSink,
@@ -190,6 +206,16 @@ mod tests {
assert_eq!(count, 0);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_call_clear_txpool_http() {
let server_addr = start_server().await;
let uri = format!("http://{server_addr}");
let client = HttpClientBuilder::default().build(&uri).unwrap();
TxpoolExtApiClient::clear_txpool(&client).await.unwrap();
let count = TxpoolExtApiClient::transaction_count(&client).await.unwrap();
assert_eq!(count, 0);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_subscribe_transaction_count_ws() {
let server_addr = start_server().await;