use crate::{
mempool::{
service::{MempoolRequest, MempoolResponse},
MempoolServiceError,
StateResponse,
StatsResponse,
TxStorageResponse,
},
transactions::{transaction::Transaction, types::Signature},
};
use tari_service_framework::{reply_channel::TrySenderService, Service};
#[derive(Clone)]
pub struct MempoolHandle {
inner: TrySenderService<MempoolRequest, MempoolResponse, MempoolServiceError>,
}
impl MempoolHandle {
pub(crate) fn new(request_sender: TrySenderService<MempoolRequest, MempoolResponse, MempoolServiceError>) -> Self {
Self { inner: request_sender }
}
pub async fn get_stats(&mut self) -> Result<StatsResponse, MempoolServiceError> {
match self.inner.call(MempoolRequest::GetStats).await?? {
MempoolResponse::Stats(resp) => Ok(resp),
_ => panic!("Incorrect response"),
}
}
pub async fn get_state(&mut self) -> Result<StateResponse, MempoolServiceError> {
match self.inner.call(MempoolRequest::GetState).await?? {
MempoolResponse::State(resp) => Ok(resp),
_ => panic!("Incorrect response"),
}
}
pub async fn get_tx_state_by_excess_sig(
&mut self,
sig: Signature,
) -> Result<TxStorageResponse, MempoolServiceError>
{
match self.inner.call(MempoolRequest::GetTxStateByExcessSig(sig)).await?? {
MempoolResponse::TxStorage(resp) => Ok(resp),
_ => panic!("Incorrect response"),
}
}
pub async fn submit_transaction(
&mut self,
transaction: Transaction,
) -> Result<TxStorageResponse, MempoolServiceError>
{
match self
.inner
.call(MempoolRequest::SubmitTransaction(transaction))
.await??
{
MempoolResponse::TxStorage(resp) => Ok(resp),
_ => panic!("Incorrect response"),
}
}
}