use crate::connection::{
create_interaction_context, ConnectionConfig, InteractionContext, InteractionContextOptions,
InteractionType,
};
use crate::error::Result;
use crate::schema::{MempoolSizeAndCapacity, Slot, Transaction, TransactionId};
use std::sync::Arc;
use super::{
acquire_mempool, has_transaction, next_transaction, next_transaction_id, release_mempool,
size_of_mempool,
};
pub struct MempoolMonitoringClient {
context: Arc<InteractionContext>,
}
impl MempoolMonitoringClient {
pub fn new(context: InteractionContext) -> Self {
Self {
context: Arc::new(context),
}
}
pub async fn connect(connection: ConnectionConfig) -> Result<Self> {
let context = create_interaction_context(InteractionContextOptions {
connection,
interaction_type: InteractionType::LongRunning,
..Default::default()
})
.await?;
Ok(Self::new(context))
}
pub fn context(&self) -> &InteractionContext {
&self.context
}
pub async fn acquire_mempool(&self) -> Result<Slot> {
acquire_mempool(&self.context).await
}
pub async fn has_transaction(&self, id: &str) -> Result<bool> {
has_transaction(&self.context, id).await
}
pub async fn next_transaction_id(&self) -> Result<Option<TransactionId>> {
next_transaction_id(&self.context).await
}
pub async fn next_transaction(&self) -> Result<Option<Transaction>> {
next_transaction(&self.context).await
}
pub async fn size_of_mempool(&self) -> Result<MempoolSizeAndCapacity> {
size_of_mempool(&self.context).await
}
pub async fn release_mempool(&self) -> Result<()> {
release_mempool(&self.context).await
}
pub async fn shutdown(&self) -> Result<()> {
self.context.shutdown().await
}
}
pub async fn create_mempool_monitoring_client(
connection: ConnectionConfig,
) -> Result<MempoolMonitoringClient> {
MempoolMonitoringClient::connect(connection).await
}
pub struct MempoolTransactionIterator<'a> {
client: &'a MempoolMonitoringClient,
exhausted: bool,
}
impl<'a> MempoolTransactionIterator<'a> {
pub fn new(client: &'a MempoolMonitoringClient) -> Self {
Self {
client,
exhausted: false,
}
}
pub async fn next(&mut self) -> Result<Option<Transaction>> {
if self.exhausted {
return Ok(None);
}
match self.client.next_transaction().await? {
Some(tx) => Ok(Some(tx)),
None => {
self.exhausted = true;
Ok(None)
}
}
}
}