zebra/zebra-node-services/src/mempool.rs

128 lines
4.8 KiB
Rust
Raw Permalink Normal View History

Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
//! The Zebra mempool.
//!
//! A service that manages known unmined Zcash transactions.
use std::collections::HashSet;
use zebra_chain::transaction::{self, UnminedTx, UnminedTxId};
#[cfg(feature = "getblocktemplate-rpcs")]
use zebra_chain::transaction::VerifiedUnminedTx;
Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
use crate::BoxError;
mod gossip;
Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
pub use self::gossip::Gossip;
/// A mempool service request.
///
/// Requests can query the current set of mempool transactions,
/// queue transactions to be downloaded and verified, or
/// run the mempool to check for newly verified transactions.
///
/// Requests can't modify the mempool directly,
/// because all mempool transactions must be verified.
#[derive(Debug, Eq, PartialEq)]
pub enum Request {
/// Query all [`UnminedTxId`]s in the mempool.
Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
TransactionIds,
/// Query matching [`UnminedTx`] in the mempool,
Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
/// using a unique set of [`UnminedTxId`]s.
TransactionsById(HashSet<UnminedTxId>),
/// Query matching [`UnminedTx`] in the mempool,
/// using a unique set of [`transaction::Hash`]es. Pre-V5 transactions are matched
/// directly; V5 transaction are matched just by the Hash, disregarding
/// the [`AuthDigest`](zebra_chain::transaction::AuthDigest).
TransactionsByMinedId(HashSet<transaction::Hash>),
/// Get all the [`VerifiedUnminedTx`] in the mempool.
///
/// Equivalent to `TransactionsById(TransactionIds)`,
/// but each transaction also includes the `miner_fee` and `legacy_sigop_count` fields.
//
// TODO: make the Transactions response return VerifiedUnminedTx,
// and remove the FullTransactions variant
#[cfg(feature = "getblocktemplate-rpcs")]
FullTransactions,
Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
/// Query matching cached rejected transaction IDs in the mempool,
/// using a unique set of [`UnminedTxId`]s.
RejectedTransactionIds(HashSet<UnminedTxId>),
/// Queue a list of gossiped transactions or transaction IDs, or
/// crawled transaction IDs.
///
/// The transaction downloader checks for duplicates across IDs and transactions.
Queue(Vec<Gossip>),
/// Check for newly verified transactions.
///
/// The transaction downloader does not push transactions into the mempool.
/// So a task should send this request regularly (every 5-10 seconds).
///
/// These checks also happen for other request variants,
/// but we can't rely on peers to send queries regularly,
/// and crawler queue requests depend on peer responses.
/// Also, crawler requests aren't frequent enough for transaction propagation.
///
/// # Correctness
///
/// This request is required to avoid hangs in the mempool.
///
/// The queue checker task can't call `poll_ready` directly on the mempool
/// service, because the service is wrapped in a `Buffer`. Calling
/// `Buffer::poll_ready` reserves a buffer slot, which can cause hangs
/// when too many slots are reserved but unused:
Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
/// <https://docs.rs/tower/0.4.10/tower/buffer/struct.Buffer.html#a-note-on-choosing-a-bound>
CheckForVerifiedTransactions,
}
/// A response to a mempool service request.
///
/// Responses can read the current set of mempool transactions,
/// check the queued status of transactions to be downloaded and verified, or
/// confirm that the mempool has been checked for newly verified transactions.
#[derive(Debug)]
pub enum Response {
/// Returns all [`UnminedTxId`]s from the mempool.
Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
TransactionIds(HashSet<UnminedTxId>),
/// Returns matching [`UnminedTx`] from the mempool.
Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
///
/// Since the [`Request::TransactionsById`] request is unique,
/// the response transactions are also unique. The same applies to
/// [`Request::TransactionsByMinedId`] requests, since the mempool does not allow
/// different transactions with different mined IDs.
Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
Transactions(Vec<UnminedTx>),
/// Returns all [`VerifiedUnminedTx`] in the mempool.
//
// TODO: make the Transactions response return VerifiedUnminedTx,
// and remove the FullTransactions variant
#[cfg(feature = "getblocktemplate-rpcs")]
FullTransactions {
/// All [`VerifiedUnminedTx`]s in the mempool
transactions: Vec<VerifiedUnminedTx>,
/// Last seen chain tip hash by mempool service
last_seen_tip_hash: zebra_chain::block::Hash,
},
/// Returns matching cached rejected [`UnminedTxId`]s from the mempool,
Refactor to create a new `zebra-node-services` crate (#3648) * Create new empty `zebra-node-services` crate The goal is to store the mempool `Request` and `Response` types so that the `zebra-rpc` crate can interface with the mempool service without having to import `zebrad`. * Move `Gossip` mempool type into new crate It is required by the `Request` type, which will be moved next. * Add documentation to `Gossip` variants Avoid having to add an exception to allow undocumented code. * Move `mempool::Request` type to new crate The first part of the service interface definition. Usages have been changed to refer to the new crate directly, and since this refactor is still incomplete, some `mp` aliases are used in a few places to refer to the old module. * Create an `UnboxMempoolError` helper trait Centralize some common code to extract and downcast boxed mempool errors. The `mempool::Response` will need to contain a `BoxError` instead of a `MempoolError` when it is moved to the `zebra-node-services` crate, so this prepares the tests to be updated with less changes. * Use `UnboxMempoolError` in tests Make the necessary changes so that the tests are ready to support a `BoxError` in the `mempool::Response` type. * Use `BoxError` in `mempool::Response` Prepare it to be moved to the `zebra-node-services` crate. * Move `mempool::Response` to `zebra-node-services` Update usages to import from the new crate directly. * Remove `mp` aliases for mempool component module Use any internal types directly instead. * Replace `tower::BoxService` with custom alias Remove the dependency of `zebra-node-services` on `tower`. * Move `Gossip` into a separate `sub-module` Keep only the main `Request` and `Response` types in the `mempool` module. * Use `crate::BoxError` instead of `tower::BoxError` Follow the existing convention. * Add missing `gossip.rs` module file It was missing from a previous refactor commit.
2022-02-25 13:43:21 -08:00
RejectedTransactionIds(HashSet<UnminedTxId>),
/// Returns a list of queue results.
///
/// These are the results of the initial queue checks.
/// The transaction may also fail download or verification later.
///
/// Each result matches the request at the corresponding vector index.
Queued(Vec<Result<(), BoxError>>),
/// Confirms that the mempool has checked for recently verified transactions.
CheckedForVerifiedTransactions,
}