ogmios_client/mempool_monitoring/client.rs
1//! Mempool Monitoring client implementation.
2
3use crate::connection::{
4 create_interaction_context, ConnectionConfig, InteractionContext, InteractionContextOptions,
5 InteractionType,
6};
7use crate::error::Result;
8use crate::schema::{MempoolSizeAndCapacity, Slot, Transaction, TransactionId};
9use std::sync::Arc;
10
11use super::{
12 acquire_mempool, has_transaction, next_transaction, next_transaction_id, release_mempool,
13 size_of_mempool,
14};
15
16/// A mempool monitoring client for observing pending transactions.
17///
18/// This client provides methods for acquiring a snapshot of the mempool
19/// and iterating through pending transactions.
20///
21/// # Example
22///
23/// ```rust,no_run
24/// use ogmios_client::mempool_monitoring::MempoolMonitoringClient;
25/// use ogmios_client::connection::ConnectionConfig;
26///
27/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
28/// let client = MempoolMonitoringClient::connect(ConnectionConfig::default()).await?;
29///
30/// // Acquire mempool snapshot
31/// let slot = client.acquire_mempool().await?;
32/// println!("Acquired mempool at slot {}", slot);
33///
34/// // Get mempool size
35/// let size = client.size_of_mempool().await?;
36/// println!("Mempool has {} transactions ({} bytes)",
37/// size.transactions, size.bytes);
38///
39/// // Iterate through transactions
40/// while let Some(tx) = client.next_transaction().await? {
41/// println!("Transaction: {}", tx.id);
42/// }
43///
44/// // Release when done
45/// client.release_mempool().await?;
46/// client.shutdown().await?;
47/// # Ok(())
48/// # }
49/// ```
50pub struct MempoolMonitoringClient {
51 /// The interaction context.
52 context: Arc<InteractionContext>,
53}
54
55impl MempoolMonitoringClient {
56 /// Create a new mempool monitoring client from an existing context.
57 pub fn new(context: InteractionContext) -> Self {
58 Self {
59 context: Arc::new(context),
60 }
61 }
62
63 /// Connect to Ogmios and create a new mempool monitoring client.
64 ///
65 /// # Arguments
66 ///
67 /// * `connection` - Connection configuration.
68 pub async fn connect(connection: ConnectionConfig) -> Result<Self> {
69 let context = create_interaction_context(InteractionContextOptions {
70 connection,
71 interaction_type: InteractionType::LongRunning,
72 ..Default::default()
73 })
74 .await?;
75
76 Ok(Self::new(context))
77 }
78
79 /// Get a reference to the interaction context.
80 pub fn context(&self) -> &InteractionContext {
81 &self.context
82 }
83
84 /// Acquire a snapshot of the mempool.
85 ///
86 /// # Returns
87 ///
88 /// The slot number at which the mempool was acquired.
89 pub async fn acquire_mempool(&self) -> Result<Slot> {
90 acquire_mempool(&self.context).await
91 }
92
93 /// Check if a transaction is in the mempool.
94 ///
95 /// # Arguments
96 ///
97 /// * `id` - The transaction ID to check.
98 pub async fn has_transaction(&self, id: &str) -> Result<bool> {
99 has_transaction(&self.context, id).await
100 }
101
102 /// Get the next transaction ID from the mempool.
103 ///
104 /// # Returns
105 ///
106 /// The next transaction ID, or `None` if the mempool has been exhausted.
107 pub async fn next_transaction_id(&self) -> Result<Option<TransactionId>> {
108 next_transaction_id(&self.context).await
109 }
110
111 /// Get the next full transaction from the mempool.
112 ///
113 /// # Returns
114 ///
115 /// The full transaction, or `None` if the mempool has been exhausted.
116 pub async fn next_transaction(&self) -> Result<Option<Transaction>> {
117 next_transaction(&self.context).await
118 }
119
120 /// Get the size and capacity of the mempool.
121 pub async fn size_of_mempool(&self) -> Result<MempoolSizeAndCapacity> {
122 size_of_mempool(&self.context).await
123 }
124
125 /// Release the acquired mempool snapshot.
126 pub async fn release_mempool(&self) -> Result<()> {
127 release_mempool(&self.context).await
128 }
129
130 /// Shutdown the client.
131 pub async fn shutdown(&self) -> Result<()> {
132 self.context.shutdown().await
133 }
134}
135
136/// Create a mempool monitoring client.
137///
138/// This is a convenience function that creates a connection and client in one step.
139pub async fn create_mempool_monitoring_client(
140 connection: ConnectionConfig,
141) -> Result<MempoolMonitoringClient> {
142 MempoolMonitoringClient::connect(connection).await
143}
144
145/// Iterator over mempool transactions.
146///
147/// This struct provides an async iterator interface for mempool transactions.
148pub struct MempoolTransactionIterator<'a> {
149 client: &'a MempoolMonitoringClient,
150 exhausted: bool,
151}
152
153impl<'a> MempoolTransactionIterator<'a> {
154 /// Create a new mempool transaction iterator.
155 pub fn new(client: &'a MempoolMonitoringClient) -> Self {
156 Self {
157 client,
158 exhausted: false,
159 }
160 }
161
162 /// Get the next transaction.
163 pub async fn next(&mut self) -> Result<Option<Transaction>> {
164 if self.exhausted {
165 return Ok(None);
166 }
167
168 match self.client.next_transaction().await? {
169 Some(tx) => Ok(Some(tx)),
170 None => {
171 self.exhausted = true;
172 Ok(None)
173 }
174 }
175 }
176}