2021-08-24 07:23:53 -07:00
|
|
|
//! Zebra mempool.
|
|
|
|
|
2021-08-27 07:36:17 -07:00
|
|
|
use std::{
|
|
|
|
collections::HashSet,
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
2021-09-13 13:28:07 -07:00
|
|
|
use futures::{future::FutureExt, stream::Stream};
|
|
|
|
use tower::{buffer::Buffer, timeout::Timeout, util::BoxService, Service};
|
2021-08-27 07:36:17 -07:00
|
|
|
|
|
|
|
use zebra_chain::{
|
|
|
|
parameters::Network,
|
|
|
|
transaction::{UnminedTx, UnminedTxId},
|
|
|
|
};
|
2021-09-13 13:28:07 -07:00
|
|
|
use zebra_consensus::{error::TransactionError, transaction};
|
|
|
|
use zebra_network as zn;
|
|
|
|
use zebra_state as zs;
|
2021-09-23 12:09:44 -07:00
|
|
|
use zebra_state::{ChainTipChange, TipAction};
|
2021-08-27 07:36:17 -07:00
|
|
|
|
2021-09-08 11:51:17 -07:00
|
|
|
pub use crate::BoxError;
|
2021-08-25 11:39:27 -07:00
|
|
|
|
2021-08-24 07:23:53 -07:00
|
|
|
mod crawler;
|
2021-09-01 17:06:20 -07:00
|
|
|
pub mod downloads;
|
2021-08-27 07:36:17 -07:00
|
|
|
mod error;
|
|
|
|
mod storage;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2021-08-24 07:23:53 -07:00
|
|
|
|
|
|
|
pub use self::crawler::Crawler;
|
2021-08-27 07:36:17 -07:00
|
|
|
pub use self::error::MempoolError;
|
2021-09-02 06:42:31 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
pub use self::storage::tests::unmined_transactions_in_blocks;
|
2021-08-27 07:36:17 -07:00
|
|
|
|
2021-09-13 13:28:07 -07:00
|
|
|
use self::downloads::{
|
|
|
|
Downloads as TxDownloads, Gossip, TRANSACTION_DOWNLOAD_TIMEOUT, TRANSACTION_VERIFY_TIMEOUT,
|
|
|
|
};
|
|
|
|
|
2021-09-15 15:13:29 -07:00
|
|
|
use super::sync::SyncStatus;
|
|
|
|
|
2021-09-13 13:28:07 -07:00
|
|
|
type Outbound = Buffer<BoxService<zn::Request, zn::Response, zn::BoxError>, zn::Request>;
|
|
|
|
type State = Buffer<BoxService<zs::Request, zs::Response, zs::BoxError>, zs::Request>;
|
|
|
|
type TxVerifier = Buffer<
|
|
|
|
BoxService<transaction::Request, transaction::Response, TransactionError>,
|
|
|
|
transaction::Request,
|
|
|
|
>;
|
|
|
|
type InboundTxDownloads = TxDownloads<Timeout<Outbound>, Timeout<TxVerifier>, State>;
|
|
|
|
|
2021-08-27 07:36:17 -07:00
|
|
|
#[derive(Debug)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
pub enum Request {
|
|
|
|
TransactionIds,
|
|
|
|
TransactionsById(HashSet<UnminedTxId>),
|
2021-09-08 11:51:17 -07:00
|
|
|
RejectedTransactionIds(HashSet<UnminedTxId>),
|
2021-09-13 13:28:07 -07:00
|
|
|
Queue(Vec<Gossip>),
|
2021-08-27 07:36:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Response {
|
|
|
|
Transactions(Vec<UnminedTx>),
|
|
|
|
TransactionIds(Vec<UnminedTxId>),
|
2021-09-08 11:51:17 -07:00
|
|
|
RejectedTransactionIds(Vec<UnminedTxId>),
|
2021-09-13 13:28:07 -07:00
|
|
|
Queued(Vec<Result<(), MempoolError>>),
|
2021-08-27 07:36:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Mempool async management and query service.
|
|
|
|
///
|
|
|
|
/// The mempool is the set of all verified transactions that this node is aware
|
|
|
|
/// of that have yet to be confirmed by the Zcash network. A transaction is
|
|
|
|
/// confirmed when it has been included in a block ('mined').
|
|
|
|
pub struct Mempool {
|
|
|
|
/// The Mempool storage itself.
|
|
|
|
///
|
|
|
|
/// ##: Correctness: only components internal to the [`Mempool`] struct are allowed to
|
|
|
|
/// inject transactions into `storage`, as transactions must be verified beforehand.
|
|
|
|
storage: storage::Storage,
|
2021-09-13 13:28:07 -07:00
|
|
|
|
|
|
|
/// The transaction dowload and verify stream.
|
|
|
|
tx_downloads: Pin<Box<InboundTxDownloads>>,
|
2021-09-15 15:13:29 -07:00
|
|
|
|
|
|
|
/// Allows checking if we are near the tip to enable/disable the mempool.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
sync_status: SyncStatus,
|
2021-09-21 10:06:52 -07:00
|
|
|
|
|
|
|
/// Allows the detection of chain tip resets.
|
|
|
|
#[allow(dead_code)]
|
|
|
|
chain_tip_change: ChainTipChange,
|
2021-08-27 07:36:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Mempool {
|
|
|
|
#[allow(dead_code)]
|
2021-09-13 13:28:07 -07:00
|
|
|
pub(crate) fn new(
|
|
|
|
_network: Network,
|
|
|
|
outbound: Outbound,
|
|
|
|
state: State,
|
|
|
|
tx_verifier: TxVerifier,
|
2021-09-15 15:13:29 -07:00
|
|
|
sync_status: SyncStatus,
|
2021-09-21 10:06:52 -07:00
|
|
|
chain_tip_change: ChainTipChange,
|
2021-09-13 13:28:07 -07:00
|
|
|
) -> Self {
|
|
|
|
let tx_downloads = Box::pin(TxDownloads::new(
|
|
|
|
Timeout::new(outbound, TRANSACTION_DOWNLOAD_TIMEOUT),
|
|
|
|
Timeout::new(tx_verifier, TRANSACTION_VERIFY_TIMEOUT),
|
|
|
|
state,
|
|
|
|
));
|
2021-09-21 10:06:52 -07:00
|
|
|
|
2021-08-27 07:36:17 -07:00
|
|
|
Mempool {
|
|
|
|
storage: Default::default(),
|
2021-09-13 13:28:07 -07:00
|
|
|
tx_downloads,
|
2021-09-15 15:13:29 -07:00
|
|
|
sync_status,
|
2021-09-21 10:06:52 -07:00
|
|
|
chain_tip_change,
|
2021-08-27 07:36:17 -07:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 06:42:31 -07:00
|
|
|
|
2021-09-21 10:06:52 -07:00
|
|
|
/// Get the storage field of the mempool for testing purposes.
|
2021-09-02 06:42:31 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
pub fn storage(&mut self) -> &mut storage::Storage {
|
|
|
|
&mut self.storage
|
|
|
|
}
|
2021-09-13 13:28:07 -07:00
|
|
|
|
|
|
|
/// Check if transaction should be downloaded and/or verified.
|
|
|
|
///
|
|
|
|
/// If it is already in the mempool (or in its rejected list)
|
|
|
|
/// then it shouldn't be downloaded/verified.
|
|
|
|
fn should_download_or_verify(&mut self, txid: UnminedTxId) -> Result<(), MempoolError> {
|
|
|
|
// Check if the transaction is already in the mempool.
|
2021-09-09 10:28:52 -07:00
|
|
|
if self.storage.contains(&txid) {
|
2021-09-13 13:28:07 -07:00
|
|
|
return Err(MempoolError::InMempool);
|
|
|
|
}
|
2021-09-09 10:28:52 -07:00
|
|
|
if self.storage.contains_rejected(&txid) {
|
2021-09-13 13:28:07 -07:00
|
|
|
return Err(MempoolError::Rejected);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-08-27 07:36:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Service<Request> for Mempool {
|
|
|
|
type Response = Response;
|
|
|
|
type Error = BoxError;
|
|
|
|
type Future =
|
|
|
|
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
|
|
|
|
|
2021-09-13 13:28:07 -07:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2021-09-23 12:09:44 -07:00
|
|
|
// Clear the mempool if there has been a chain tip reset.
|
|
|
|
if let Some(TipAction::Reset { .. }) = self.chain_tip_change.last_tip_change() {
|
|
|
|
self.storage.clear();
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:28:07 -07:00
|
|
|
// Clean up completed download tasks and add to mempool if successful
|
|
|
|
while let Poll::Ready(Some(r)) = self.tx_downloads.as_mut().poll_next(cx) {
|
|
|
|
if let Ok(tx) = r {
|
|
|
|
// TODO: should we do something with the result?
|
|
|
|
let _ = self.storage.insert(tx);
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 07:36:17 -07:00
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[instrument(name = "mempool", skip(self, req))]
|
|
|
|
fn call(&mut self, req: Request) -> Self::Future {
|
|
|
|
match req {
|
|
|
|
Request::TransactionIds => {
|
2021-09-09 10:28:52 -07:00
|
|
|
let res = self.storage.tx_ids();
|
2021-08-27 07:36:17 -07:00
|
|
|
async move { Ok(Response::TransactionIds(res)) }.boxed()
|
|
|
|
}
|
|
|
|
Request::TransactionsById(ids) => {
|
2021-09-09 10:28:52 -07:00
|
|
|
let rsp = Ok(self.storage.transactions(ids)).map(Response::Transactions);
|
2021-08-27 07:36:17 -07:00
|
|
|
async move { rsp }.boxed()
|
|
|
|
}
|
2021-09-08 11:51:17 -07:00
|
|
|
Request::RejectedTransactionIds(ids) => {
|
2021-09-09 10:28:52 -07:00
|
|
|
let rsp = Ok(self.storage.rejected_transactions(ids))
|
2021-09-08 11:51:17 -07:00
|
|
|
.map(Response::RejectedTransactionIds);
|
|
|
|
async move { rsp }.boxed()
|
|
|
|
}
|
2021-09-13 13:28:07 -07:00
|
|
|
Request::Queue(gossiped_txs) => {
|
|
|
|
let rsp: Vec<Result<(), MempoolError>> = gossiped_txs
|
|
|
|
.into_iter()
|
|
|
|
.map(|gossiped_tx| {
|
|
|
|
self.should_download_or_verify(gossiped_tx.id())?;
|
|
|
|
self.tx_downloads
|
|
|
|
.download_if_needed_and_verify(gossiped_tx)?;
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
async move { Ok(Response::Queued(rsp)) }.boxed()
|
|
|
|
}
|
2021-08-27 07:36:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|