2021-08-24 07:23:53 -07:00
|
|
|
//! Zebra mempool.
|
2021-11-01 07:32:48 -07:00
|
|
|
//!
|
|
|
|
//! A zebrad application component that manages the active collection, reception,
|
|
|
|
//! gossip, verification, in-memory storage, eviction, and rejection of unmined Zcash
|
|
|
|
//! transactions (those that have not been confirmed in a mined block on the
|
|
|
|
//! blockchain).
|
|
|
|
//!
|
|
|
|
//! Major parts of the mempool include:
|
|
|
|
//! * [Mempool Service][`Mempool`]
|
|
|
|
//! * activates when the syncer is near the chain tip
|
|
|
|
//! * spawns [download and verify tasks][`downloads::Downloads`] for each crawled or gossiped transaction
|
|
|
|
//! * handles in-memory [storage][`storage::Storage`] of unmined transactions
|
|
|
|
//! * [Crawler][`crawler::Crawler`]
|
|
|
|
//! * runs in the background to periodically poll peers for fresh unmined transactions
|
|
|
|
//! * [Queue Checker][`queue_checker::QueueChecker`]
|
|
|
|
//! * runs in the background, polling the mempool to store newly verified transactions
|
|
|
|
//! * [Transaction Gossip Task][`gossip::gossip_mempool_transaction_id`]
|
|
|
|
//! * runs in the background and gossips newly added mempool transactions
|
|
|
|
//! to peers
|
2021-08-24 07:23:53 -07:00
|
|
|
|
2021-08-27 07:36:17 -07:00
|
|
|
use std::{
|
|
|
|
collections::HashSet,
|
|
|
|
future::Future,
|
2021-09-28 16:06:40 -07:00
|
|
|
iter,
|
2023-11-02 08:00:18 -07:00
|
|
|
pin::{pin, Pin},
|
2021-08-27 07:36:17 -07:00
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
2021-09-13 13:28:07 -07:00
|
|
|
use futures::{future::FutureExt, stream::Stream};
|
2023-05-15 14:10:28 -07:00
|
|
|
use tokio::sync::broadcast;
|
2023-11-02 08:00:18 -07:00
|
|
|
use tokio_stream::StreamExt;
|
2021-09-13 13:28:07 -07:00
|
|
|
use tower::{buffer::Buffer, timeout::Timeout, util::BoxService, Service};
|
2021-08-27 07:36:17 -07:00
|
|
|
|
2022-12-02 03:21:23 -08:00
|
|
|
use zebra_chain::{
|
2023-04-03 16:22:07 -07:00
|
|
|
block::{self, Height},
|
|
|
|
chain_sync_status::ChainSyncStatus,
|
|
|
|
chain_tip::ChainTip,
|
2022-12-02 03:21:23 -08:00
|
|
|
transaction::UnminedTxId,
|
|
|
|
};
|
2021-09-13 13:28:07 -07:00
|
|
|
use zebra_consensus::{error::TransactionError, transaction};
|
|
|
|
use zebra_network as zn;
|
2022-12-12 15:19:45 -08:00
|
|
|
use zebra_node_services::mempool::{Gossip, Request, Response};
|
2021-09-13 13:28:07 -07:00
|
|
|
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
|
|
|
|
2023-11-02 08:00:18 -07:00
|
|
|
use crate::components::{mempool::crawler::RATE_LIMIT_DELAY, sync::SyncStatus};
|
2021-08-25 11:39:27 -07:00
|
|
|
|
2021-11-18 17:55:38 -08:00
|
|
|
pub mod config;
|
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;
|
2021-10-08 04:59:46 -07:00
|
|
|
pub mod gossip;
|
2021-10-18 12:23:21 -07:00
|
|
|
mod queue_checker;
|
2021-08-27 07:36:17 -07:00
|
|
|
mod storage;
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
2021-08-24 07:23:53 -07:00
|
|
|
|
2021-10-12 10:31:54 -07:00
|
|
|
pub use crate::BoxError;
|
|
|
|
|
|
|
|
pub use config::Config;
|
|
|
|
pub use crawler::Crawler;
|
|
|
|
pub use error::MempoolError;
|
|
|
|
pub use gossip::gossip_mempool_transaction_id;
|
2021-10-18 12:23:21 -07:00
|
|
|
pub use queue_checker::QueueChecker;
|
2021-10-12 10:31:54 -07:00
|
|
|
pub use storage::{
|
2023-04-13 01:42:17 -07:00
|
|
|
ExactTipRejectionError, SameEffectsChainRejectionError, SameEffectsTipRejectionError, Storage,
|
2021-10-07 12:58:42 -07:00
|
|
|
};
|
2021-10-06 18:20:38 -07:00
|
|
|
|
2021-09-02 06:42:31 -07:00
|
|
|
#[cfg(test)]
|
2022-02-25 13:43:21 -08:00
|
|
|
pub use self::{storage::tests::unmined_transactions_in_blocks, tests::UnboxMempoolError};
|
2021-08-27 07:36:17 -07:00
|
|
|
|
2021-10-12 10:31:54 -07:00
|
|
|
use downloads::{
|
2022-02-25 13:43:21 -08:00
|
|
|
Downloads as TxDownloads, TRANSACTION_DOWNLOAD_TIMEOUT, TRANSACTION_VERIFY_TIMEOUT,
|
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-09-28 16:06:40 -07:00
|
|
|
/// The state of the mempool.
|
|
|
|
///
|
2021-11-12 11:30:22 -08:00
|
|
|
/// Indicates whether it is enabled or disabled and, if enabled, contains
|
2021-09-28 16:06:40 -07:00
|
|
|
/// the necessary data to run it.
|
2022-12-07 22:11:33 -08:00
|
|
|
//
|
|
|
|
// Zebra only has one mempool, so the enum variant size difference doesn't matter.
|
Refactor mempool spend conflict checks to increase performance (#2826)
* Add `HashSet`s to help spend conflict detection
Keep track of the spent transparent outpoints and the revealed
nullifiers.
Clippy complained that the `ActiveState` had variants with large size
differences, but that was expected, so I disabled that lint on that
`enum`.
* Clear the `HashSet`s when clearing the mempool
Clear them so that they remain consistent with the set of verified
transactions.
* Use `HashSet`s to check for spend conflicts
Store new outputs into its respective `HashSet`, and abort if a
duplicate output is found.
* Remove inserted outputs when aborting
Restore the `HashSet` to its previous state.
* Remove tracked outputs when removing a transaction
Keep the mempool storage in a consistent state when a transaction is
removed.
* Remove tracked outputs when evicting from mempool
Ensure eviction also keeps the tracked outputs consistent with the
verified transactions.
* Refactor to create a `VerifiedSet` helper type
Move the code to handle the output caches into the new type. Also move
the eviction code to make things a little simpler.
* Refactor to have a single `remove` method
Centralize the code that handles the removal of a transaction to avoid
mistakes.
* Move mempool size limiting back to `Storage`
Because the evicted transactions must be added to the rejected list.
* Remove leftover `dbg!` statement
Leftover from some temporary testing code.
Co-authored-by: teor <teor@riseup.net>
* Remove unnecessary `TODO`
It is more speculation than planning, so it doesn't add much value.
Co-authored-by: teor <teor@riseup.net>
* Fix typo in documentation
The verb should match the subject "transactions" which is plural.
Co-authored-by: teor <teor@riseup.net>
* Add a comment to warn about correctness
There's a subtle but important detail in the implementation that should
be made more visible to avoid mistakes in the future.
Co-authored-by: teor <teor@riseup.net>
* Remove outdated comment
Left-over from the attempt to move the eviction into the `VerifiedSet`.
* Improve comment explaining lint removal
Rewrite the comment explaining why the Clippy lint was ignored.
* Check for spend conflicts in `VerifiedSet`
Refactor to avoid API misuse.
* Test rejected transaction rollback
Using two transactions, perform the same test adding a conflict to both
of them to check if the second inserted transaction is properly
rejected. Then remove any conflicts from the second transaction and add
it again. That should work, because if it doesn't it means that when the
second transaction was rejected it left things it shouldn't in the
cache.
* Test removal of multiple transactions
When removing multiple transactions from the mempool storage, all of the
ones requested should be removed and any other transaction should be
still be there afterwards.
* Increase mempool size to 4, so that spend conflict tests work
If the mempool size is smaller than 4,
these tests don't fail on a trivial removal bug.
Because we need a minimum number of transactions in the mempool
to trigger the bug.
Also commit a proptest seed that fails on a trivial removal bug.
(This seed fails if we remove indexes in order,
because every index past the first removes the wrong transaction.)
* Summarise transaction data in proptest error output
* Summarise spend conflict field data in proptest error output
* Summarise multiple removal field data in proptest error output
And replace the very large proptest debug output with the new summary.
Co-authored-by: teor <teor@riseup.net>
2021-10-10 16:54:46 -07:00
|
|
|
#[allow(clippy::large_enum_variant)]
|
2023-01-17 16:27:42 -08:00
|
|
|
#[derive(Default)]
|
2021-09-28 16:06:40 -07:00
|
|
|
enum ActiveState {
|
|
|
|
/// The Mempool is disabled.
|
2023-01-17 16:27:42 -08:00
|
|
|
#[default]
|
2021-09-28 16:06:40 -07:00
|
|
|
Disabled,
|
2023-01-17 16:27:42 -08:00
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
/// The Mempool is enabled.
|
|
|
|
Enabled {
|
|
|
|
/// The Mempool storage itself.
|
|
|
|
///
|
2023-01-17 16:27:42 -08:00
|
|
|
/// # Correctness
|
|
|
|
///
|
|
|
|
/// Only components internal to the [`Mempool`] struct are allowed to
|
2021-09-28 16:06:40 -07:00
|
|
|
/// inject transactions into `storage`, as transactions must be verified beforehand.
|
2023-04-13 01:42:17 -07:00
|
|
|
storage: Storage,
|
2023-01-17 16:27:42 -08:00
|
|
|
|
2021-09-29 19:09:08 -07:00
|
|
|
/// The transaction download and verify stream.
|
2021-09-28 16:06:40 -07:00
|
|
|
tx_downloads: Pin<Box<InboundTxDownloads>>,
|
2023-04-03 16:22:07 -07:00
|
|
|
|
|
|
|
/// Last seen chain tip hash that mempool transactions have been verified against.
|
|
|
|
///
|
|
|
|
/// In some tests, this is initialized to the latest chain tip, then updated in `poll_ready()` before each request.
|
|
|
|
last_seen_tip_hash: block::Hash,
|
2021-09-28 16:06:40 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-18 10:39:56 -07:00
|
|
|
impl ActiveState {
|
2022-06-14 20:57:19 -07:00
|
|
|
/// Returns the current state, leaving [`Self::Disabled`] in its place.
|
2021-10-18 10:39:56 -07:00
|
|
|
fn take(&mut self) -> Self {
|
|
|
|
std::mem::take(self)
|
|
|
|
}
|
2022-12-12 15:19:45 -08:00
|
|
|
|
|
|
|
/// Returns a list of requests that will retry every stored and pending transaction.
|
|
|
|
fn transaction_retry_requests(&self) -> Vec<Gossip> {
|
|
|
|
match self {
|
|
|
|
ActiveState::Disabled => Vec::new(),
|
|
|
|
ActiveState::Enabled {
|
|
|
|
storage,
|
|
|
|
tx_downloads,
|
2023-04-03 16:22:07 -07:00
|
|
|
..
|
2022-12-12 15:19:45 -08:00
|
|
|
} => {
|
|
|
|
let mut transactions = Vec::new();
|
|
|
|
|
|
|
|
let storage = storage.transactions().map(|tx| tx.clone().into());
|
|
|
|
transactions.extend(storage);
|
|
|
|
|
|
|
|
let pending = tx_downloads.transaction_requests().cloned();
|
|
|
|
transactions.extend(pending);
|
|
|
|
|
|
|
|
transactions
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 01:42:17 -07:00
|
|
|
|
|
|
|
/// Returns the number of pending transactions waiting for download or verify,
|
|
|
|
/// or zero if the mempool is disabled.
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
fn queued_transaction_count(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
ActiveState::Disabled => 0,
|
|
|
|
ActiveState::Enabled { tx_downloads, .. } => tx_downloads.in_flight(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of transactions in storage, or zero if the mempool is disabled.
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
fn transaction_count(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
ActiveState::Disabled => 0,
|
|
|
|
ActiveState::Enabled { storage, .. } => storage.transaction_count(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the cost of the transactions in the mempool, according to ZIP-401.
|
|
|
|
/// Returns zero if the mempool is disabled.
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
fn total_cost(&self) -> u64 {
|
|
|
|
match self {
|
|
|
|
ActiveState::Disabled => 0,
|
|
|
|
ActiveState::Enabled { storage, .. } => storage.total_cost(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the total serialized size of the verified transactions in the set,
|
|
|
|
/// or zero if the mempool is disabled.
|
|
|
|
///
|
|
|
|
/// See [`Storage::total_serialized_size()`] for details.
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
pub fn total_serialized_size(&self) -> usize {
|
|
|
|
match self {
|
|
|
|
ActiveState::Disabled => 0,
|
|
|
|
ActiveState::Enabled { storage, .. } => storage.total_serialized_size(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the number of rejected transaction hashes in storage,
|
|
|
|
/// or zero if the mempool is disabled.
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
fn rejected_transaction_count(&mut self) -> usize {
|
|
|
|
match self {
|
|
|
|
ActiveState::Disabled => 0,
|
|
|
|
ActiveState::Enabled { storage, .. } => storage.rejected_transaction_count(),
|
|
|
|
}
|
|
|
|
}
|
2021-10-18 10:39:56 -07:00
|
|
|
}
|
|
|
|
|
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 {
|
2021-10-26 17:21:19 -07:00
|
|
|
/// The configurable options for the mempool, persisted between states.
|
|
|
|
config: Config,
|
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
/// The state of the mempool.
|
|
|
|
active_state: ActiveState,
|
2021-09-15 15:13:29 -07:00
|
|
|
|
|
|
|
/// Allows checking if we are near the tip to enable/disable the mempool.
|
|
|
|
sync_status: SyncStatus,
|
2021-09-21 10:06:52 -07:00
|
|
|
|
2021-10-13 08:04:49 -07:00
|
|
|
/// If the state's best chain tip has reached this height, always enable the mempool.
|
|
|
|
debug_enable_at_height: Option<Height>,
|
|
|
|
|
2021-12-17 08:31:51 -08:00
|
|
|
/// Allows efficient access to the best tip of the blockchain.
|
2021-09-29 09:52:44 -07:00
|
|
|
latest_chain_tip: zs::LatestChainTip,
|
2021-10-13 08:04:49 -07:00
|
|
|
|
|
|
|
/// Allows the detection of newly added chain tip blocks,
|
|
|
|
/// and chain tip resets.
|
2021-09-21 10:06:52 -07:00
|
|
|
chain_tip_change: ChainTipChange,
|
2021-09-28 16:06:40 -07:00
|
|
|
|
|
|
|
/// Handle to the outbound service.
|
|
|
|
/// Used to construct the transaction downloader.
|
|
|
|
outbound: Outbound,
|
|
|
|
|
|
|
|
/// Handle to the state service.
|
|
|
|
/// Used to construct the transaction downloader.
|
|
|
|
state: State,
|
|
|
|
|
|
|
|
/// Handle to the transaction verifier service.
|
|
|
|
/// Used to construct the transaction downloader.
|
|
|
|
tx_verifier: TxVerifier,
|
2021-10-08 04:59:46 -07:00
|
|
|
|
|
|
|
/// Sender part of a gossip transactions channel.
|
|
|
|
/// Used to broadcast transaction ids to peers.
|
2023-05-15 14:10:28 -07:00
|
|
|
transaction_sender: broadcast::Sender<HashSet<UnminedTxId>>,
|
2023-04-13 01:42:17 -07:00
|
|
|
|
|
|
|
// Diagnostics
|
|
|
|
//
|
|
|
|
/// Queued transactions pending download or verification transmitter.
|
|
|
|
/// Only displayed after the mempool's first activation.
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
queued_count_bar: Option<howudoin::Tx>,
|
|
|
|
|
|
|
|
/// Number of mempool transactions transmitter.
|
|
|
|
/// Only displayed after the mempool's first activation.
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
transaction_count_bar: Option<howudoin::Tx>,
|
|
|
|
|
|
|
|
/// Mempool transaction cost transmitter.
|
|
|
|
/// Only displayed after the mempool's first activation.
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
transaction_cost_bar: Option<howudoin::Tx>,
|
|
|
|
|
|
|
|
/// Rejected transactions transmitter.
|
|
|
|
/// Only displayed after the mempool's first activation.
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
rejected_count_bar: Option<howudoin::Tx>,
|
2021-08-27 07:36:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Mempool {
|
2021-09-13 13:28:07 -07:00
|
|
|
pub(crate) fn new(
|
2021-10-13 08:04:49 -07:00
|
|
|
config: &Config,
|
2021-09-13 13:28:07 -07:00
|
|
|
outbound: Outbound,
|
|
|
|
state: State,
|
|
|
|
tx_verifier: TxVerifier,
|
2021-09-15 15:13:29 -07:00
|
|
|
sync_status: SyncStatus,
|
2021-09-29 09:52:44 -07:00
|
|
|
latest_chain_tip: zs::LatestChainTip,
|
2021-09-21 10:06:52 -07:00
|
|
|
chain_tip_change: ChainTipChange,
|
2023-05-15 14:10:28 -07:00
|
|
|
) -> (Self, broadcast::Receiver<HashSet<UnminedTxId>>) {
|
2021-10-12 10:31:54 -07:00
|
|
|
let (transaction_sender, transaction_receiver) =
|
2023-05-15 14:10:28 -07:00
|
|
|
tokio::sync::broadcast::channel(gossip::MAX_CHANGES_BEFORE_SEND * 2);
|
2021-10-12 10:31:54 -07:00
|
|
|
|
|
|
|
let mut service = Mempool {
|
2021-10-26 17:21:19 -07:00
|
|
|
config: config.clone(),
|
2021-09-28 16:06:40 -07:00
|
|
|
active_state: ActiveState::Disabled,
|
2021-09-15 15:13:29 -07:00
|
|
|
sync_status,
|
2021-10-13 08:04:49 -07:00
|
|
|
debug_enable_at_height: config.debug_enable_at_height.map(Height),
|
2021-09-29 09:52:44 -07:00
|
|
|
latest_chain_tip,
|
2021-09-21 10:06:52 -07:00
|
|
|
chain_tip_change,
|
2021-09-28 16:06:40 -07:00
|
|
|
outbound,
|
|
|
|
state,
|
|
|
|
tx_verifier,
|
2021-10-08 04:59:46 -07:00
|
|
|
transaction_sender,
|
2023-04-13 01:42:17 -07:00
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
queued_count_bar: None,
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
transaction_count_bar: None,
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
transaction_cost_bar: None,
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
rejected_count_bar: None,
|
2021-10-12 10:31:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Make sure `is_enabled` is accurate.
|
|
|
|
// Otherwise, it is only updated in `poll_ready`, right before each service call.
|
2023-04-03 16:22:07 -07:00
|
|
|
service.update_state(None);
|
2021-10-12 10:31:54 -07:00
|
|
|
|
|
|
|
(service, transaction_receiver)
|
2021-09-28 16:06:40 -07:00
|
|
|
}
|
|
|
|
|
2021-10-13 08:04:49 -07:00
|
|
|
/// Is the mempool enabled by a debug config option?
|
|
|
|
fn is_enabled_by_debug(&self) -> bool {
|
|
|
|
let mut is_debug_enabled = false;
|
|
|
|
|
|
|
|
// optimise non-debug performance
|
|
|
|
if self.debug_enable_at_height.is_none() {
|
|
|
|
return is_debug_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
let enable_at_height = self
|
|
|
|
.debug_enable_at_height
|
|
|
|
.expect("unexpected debug_enable_at_height: just checked for None");
|
|
|
|
|
|
|
|
if let Some(best_tip_height) = self.latest_chain_tip.best_tip_height() {
|
|
|
|
is_debug_enabled = best_tip_height >= enable_at_height;
|
|
|
|
|
|
|
|
if is_debug_enabled && !self.is_enabled() {
|
|
|
|
info!(
|
|
|
|
?best_tip_height,
|
|
|
|
?enable_at_height,
|
|
|
|
"enabling mempool for debugging"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
is_debug_enabled
|
|
|
|
}
|
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
/// Update the mempool state (enabled / disabled) depending on how close to
|
|
|
|
/// the tip is the synchronization, including side effects to state changes.
|
2021-11-30 13:04:32 -08:00
|
|
|
///
|
2023-04-03 16:22:07 -07:00
|
|
|
/// Accepts an optional [`TipAction`] for setting the `last_seen_tip_hash` field
|
|
|
|
/// when enabling the mempool state, it will not enable the mempool if this is None.
|
|
|
|
///
|
2021-11-30 13:04:32 -08:00
|
|
|
/// Returns `true` if the state changed.
|
2023-04-03 16:22:07 -07:00
|
|
|
fn update_state(&mut self, tip_action: Option<&TipAction>) -> bool {
|
2021-10-13 08:04:49 -07:00
|
|
|
let is_close_to_tip = self.sync_status.is_close_to_tip() || self.is_enabled_by_debug();
|
|
|
|
|
2023-04-03 16:22:07 -07:00
|
|
|
match (is_close_to_tip, self.is_enabled(), tip_action) {
|
|
|
|
// the active state is up to date, or there is no tip action to activate the mempool
|
|
|
|
(false, false, _) | (true, true, _) | (true, false, None) => return false,
|
|
|
|
|
|
|
|
// Enable state - there should be a chain tip when Zebra is close to the network tip
|
|
|
|
(true, false, Some(tip_action)) => {
|
|
|
|
let (last_seen_tip_hash, tip_height) = tip_action.best_tip_hash_and_height();
|
|
|
|
|
|
|
|
info!(?tip_height, "activating mempool: Zebra is close to the tip");
|
|
|
|
|
|
|
|
let tx_downloads = Box::pin(TxDownloads::new(
|
|
|
|
Timeout::new(self.outbound.clone(), TRANSACTION_DOWNLOAD_TIMEOUT),
|
|
|
|
Timeout::new(self.tx_verifier.clone(), TRANSACTION_VERIFY_TIMEOUT),
|
|
|
|
self.state.clone(),
|
|
|
|
));
|
|
|
|
self.active_state = ActiveState::Enabled {
|
|
|
|
storage: storage::Storage::new(&self.config),
|
|
|
|
tx_downloads,
|
|
|
|
last_seen_tip_hash,
|
|
|
|
};
|
|
|
|
}
|
2021-10-13 08:04:49 -07:00
|
|
|
|
2023-04-03 16:22:07 -07:00
|
|
|
// Disable state
|
|
|
|
(false, true, _) => {
|
|
|
|
info!(
|
|
|
|
tip_height = ?self.latest_chain_tip.best_tip_height(),
|
|
|
|
"deactivating mempool: Zebra is syncing lots of blocks"
|
|
|
|
);
|
2021-10-13 08:04:49 -07:00
|
|
|
|
2023-04-03 16:22:07 -07:00
|
|
|
// This drops the previous ActiveState::Enabled, cancelling its download tasks.
|
|
|
|
// We don't preserve the previous transactions, because we are syncing lots of blocks.
|
|
|
|
self.active_state = ActiveState::Disabled;
|
|
|
|
}
|
|
|
|
};
|
2021-11-30 13:04:32 -08:00
|
|
|
|
|
|
|
true
|
2021-09-28 16:06:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return whether the mempool is enabled or not.
|
|
|
|
pub fn is_enabled(&self) -> bool {
|
|
|
|
match self.active_state {
|
|
|
|
ActiveState::Disabled => false,
|
|
|
|
ActiveState::Enabled { .. } => true,
|
2021-08-27 07:36:17 -07:00
|
|
|
}
|
|
|
|
}
|
2021-09-02 06:42:31 -07:00
|
|
|
|
2021-10-15 11:03:13 -07:00
|
|
|
/// Remove expired transaction ids from a given list of inserted ones.
|
|
|
|
fn remove_expired_from_peer_list(
|
|
|
|
send_to_peers_ids: &HashSet<UnminedTxId>,
|
|
|
|
expired_transactions: &HashSet<UnminedTxId>,
|
|
|
|
) -> HashSet<UnminedTxId> {
|
|
|
|
send_to_peers_ids
|
|
|
|
.difference(expired_transactions)
|
|
|
|
.copied()
|
|
|
|
.collect()
|
2021-09-13 13:28:07 -07:00
|
|
|
}
|
2023-04-13 01:42:17 -07:00
|
|
|
|
|
|
|
/// Update metrics for the mempool.
|
|
|
|
fn update_metrics(&mut self) {
|
|
|
|
// Shutdown if needed
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
if matches!(howudoin::cancelled(), Some(true)) {
|
|
|
|
self.disable_metrics();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize if just activated
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
if self.is_enabled()
|
|
|
|
&& (self.queued_count_bar.is_none()
|
|
|
|
|| self.transaction_count_bar.is_none()
|
|
|
|
|| self.transaction_cost_bar.is_none()
|
|
|
|
|| self.rejected_count_bar.is_none())
|
|
|
|
{
|
2023-06-14 12:07:02 -07:00
|
|
|
let _max_transaction_count = self.config.tx_cost_limit
|
2023-04-13 01:42:17 -07:00
|
|
|
/ zebra_chain::transaction::MEMPOOL_TRANSACTION_COST_THRESHOLD;
|
|
|
|
|
2023-07-05 00:08:59 -07:00
|
|
|
let transaction_count_bar = *howudoin::new_root()
|
|
|
|
.label("Mempool Transactions")
|
|
|
|
.set_pos(0u64);
|
|
|
|
// .set_len(max_transaction_count);
|
|
|
|
|
|
|
|
let transaction_cost_bar = howudoin::new_with_parent(transaction_count_bar.id())
|
|
|
|
.label("Mempool Cost")
|
|
|
|
.set_pos(0u64)
|
|
|
|
// .set_len(self.config.tx_cost_limit)
|
|
|
|
.fmt_as_bytes(true);
|
|
|
|
|
|
|
|
let queued_count_bar = *howudoin::new_with_parent(transaction_cost_bar.id())
|
|
|
|
.label("Mempool Queue")
|
|
|
|
.set_pos(0u64);
|
2023-06-14 12:07:02 -07:00
|
|
|
// .set_len(
|
|
|
|
// u64::try_from(downloads::MAX_INBOUND_CONCURRENCY).expect("fits in u64"),
|
2023-07-05 00:08:59 -07:00
|
|
|
// );
|
2023-04-13 01:42:17 -07:00
|
|
|
|
2023-07-05 00:08:59 -07:00
|
|
|
let rejected_count_bar = *howudoin::new_with_parent(queued_count_bar.id())
|
|
|
|
.label("Mempool Rejects")
|
|
|
|
.set_pos(0u64);
|
2023-06-14 12:07:02 -07:00
|
|
|
// .set_len(
|
|
|
|
// u64::try_from(storage::MAX_EVICTION_MEMORY_ENTRIES).expect("fits in u64"),
|
2023-07-05 00:08:59 -07:00
|
|
|
// );
|
|
|
|
|
|
|
|
self.transaction_count_bar = Some(transaction_count_bar);
|
|
|
|
self.transaction_cost_bar = Some(transaction_cost_bar);
|
|
|
|
self.queued_count_bar = Some(queued_count_bar);
|
|
|
|
self.rejected_count_bar = Some(rejected_count_bar);
|
2023-04-13 01:42:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update if the mempool has ever been active
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
if let (
|
|
|
|
Some(queued_count_bar),
|
|
|
|
Some(transaction_count_bar),
|
|
|
|
Some(transaction_cost_bar),
|
|
|
|
Some(rejected_count_bar),
|
|
|
|
) = (
|
|
|
|
self.queued_count_bar,
|
|
|
|
self.transaction_count_bar,
|
|
|
|
self.transaction_cost_bar,
|
|
|
|
self.rejected_count_bar,
|
|
|
|
) {
|
|
|
|
let queued_count = self.active_state.queued_transaction_count();
|
|
|
|
let transaction_count = self.active_state.transaction_count();
|
|
|
|
|
|
|
|
let transaction_cost = self.active_state.total_cost();
|
|
|
|
let transaction_size = self.active_state.total_serialized_size();
|
|
|
|
let transaction_size =
|
|
|
|
indicatif::HumanBytes(transaction_size.try_into().expect("fits in u64"));
|
|
|
|
|
|
|
|
let rejected_count = self.active_state.rejected_transaction_count();
|
|
|
|
|
|
|
|
queued_count_bar.set_pos(u64::try_from(queued_count).expect("fits in u64"));
|
|
|
|
|
|
|
|
transaction_count_bar.set_pos(u64::try_from(transaction_count).expect("fits in u64"));
|
|
|
|
|
|
|
|
// Display the cost and cost limit, with the actual size as a description.
|
|
|
|
//
|
|
|
|
// Costs can be much higher than the transaction size due to the
|
|
|
|
// MEMPOOL_TRANSACTION_COST_THRESHOLD minimum cost.
|
|
|
|
transaction_cost_bar
|
|
|
|
.set_pos(transaction_cost)
|
|
|
|
.desc(format!("Actual size {transaction_size}"));
|
|
|
|
|
|
|
|
rejected_count_bar.set_pos(u64::try_from(rejected_count).expect("fits in u64"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Disable metrics for the mempool.
|
|
|
|
fn disable_metrics(&self) {
|
|
|
|
#[cfg(feature = "progress-bar")]
|
|
|
|
{
|
|
|
|
if let Some(bar) = self.queued_count_bar {
|
|
|
|
bar.close()
|
|
|
|
}
|
|
|
|
if let Some(bar) = self.transaction_count_bar {
|
|
|
|
bar.close()
|
|
|
|
}
|
|
|
|
if let Some(bar) = self.transaction_cost_bar {
|
|
|
|
bar.close()
|
|
|
|
}
|
|
|
|
if let Some(bar) = self.rejected_count_bar {
|
|
|
|
bar.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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>> {
|
2023-04-03 16:22:07 -07:00
|
|
|
let tip_action = self.chain_tip_change.last_tip_change();
|
|
|
|
let is_state_changed = self.update_state(tip_action.as_ref());
|
2021-09-23 12:09:44 -07:00
|
|
|
|
2022-09-06 06:32:33 -07:00
|
|
|
tracing::trace!(is_enabled = ?self.is_enabled(), ?is_state_changed, "started polling the mempool...");
|
|
|
|
|
2021-10-18 10:39:56 -07:00
|
|
|
// When the mempool is disabled we still return that the service is ready.
|
2021-11-30 13:04:32 -08:00
|
|
|
// Otherwise, callers could block waiting for the mempool to be enabled.
|
2021-10-18 10:39:56 -07:00
|
|
|
if !self.is_enabled() {
|
2023-04-13 01:42:17 -07:00
|
|
|
self.update_metrics();
|
|
|
|
|
2021-10-18 10:39:56 -07:00
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
2021-09-29 09:52:44 -07:00
|
|
|
|
2021-10-18 10:39:56 -07:00
|
|
|
// Clear the mempool and cancel downloads if there has been a chain tip reset.
|
2023-04-03 10:57:56 -07:00
|
|
|
//
|
|
|
|
// But if the mempool was just freshly enabled,
|
|
|
|
// skip resetting and removing mined transactions for this tip.
|
|
|
|
if !is_state_changed && matches!(tip_action, Some(TipAction::Reset { .. })) {
|
2021-10-18 10:39:56 -07:00
|
|
|
info!(
|
|
|
|
tip_height = ?tip_action.as_ref().unwrap().best_tip_height(),
|
|
|
|
"resetting mempool: switched best chain, skipped blocks, or activated network upgrade"
|
|
|
|
);
|
|
|
|
|
2022-12-12 15:19:45 -08:00
|
|
|
let previous_state = self.active_state.take();
|
|
|
|
let tx_retries = previous_state.transaction_retry_requests();
|
|
|
|
|
2021-10-18 10:39:56 -07:00
|
|
|
// Use the same code for dropping and resetting the mempool,
|
|
|
|
// to avoid subtle bugs.
|
2022-12-12 15:19:45 -08:00
|
|
|
//
|
2021-10-18 10:39:56 -07:00
|
|
|
// Drop the current contents of the state,
|
|
|
|
// cancelling any pending download tasks,
|
|
|
|
// and dropping completed verification results.
|
2022-12-12 15:19:45 -08:00
|
|
|
std::mem::drop(previous_state);
|
2021-10-18 10:39:56 -07:00
|
|
|
|
|
|
|
// Re-initialise an empty state.
|
2023-04-03 16:22:07 -07:00
|
|
|
self.update_state(tip_action.as_ref());
|
2021-10-18 10:39:56 -07:00
|
|
|
|
2022-12-12 15:19:45 -08:00
|
|
|
// Re-verify the transactions that were pending or valid at the previous tip.
|
|
|
|
// This saves us the time and data needed to re-download them.
|
|
|
|
if let ActiveState::Enabled { tx_downloads, .. } = &mut self.active_state {
|
|
|
|
info!(
|
|
|
|
transactions = tx_retries.len(),
|
|
|
|
"re-verifying mempool transactions after a chain fork"
|
|
|
|
);
|
|
|
|
|
|
|
|
for tx in tx_retries {
|
|
|
|
// This is just an efficiency optimisation, so we don't care if queueing
|
|
|
|
// transaction requests fails.
|
|
|
|
let _result = tx_downloads.download_if_needed_and_verify(tx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 01:42:17 -07:00
|
|
|
self.update_metrics();
|
|
|
|
|
2021-10-18 10:39:56 -07:00
|
|
|
return Poll::Ready(Ok(()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let ActiveState::Enabled {
|
|
|
|
storage,
|
|
|
|
tx_downloads,
|
2023-04-03 16:22:07 -07:00
|
|
|
last_seen_tip_hash,
|
2021-10-18 10:39:56 -07:00
|
|
|
} = &mut self.active_state
|
|
|
|
{
|
|
|
|
// Collect inserted transaction ids.
|
|
|
|
let mut send_to_peers_ids = HashSet::<_>::new();
|
|
|
|
|
2023-02-16 12:06:42 -08:00
|
|
|
let best_tip_height = self.latest_chain_tip.best_tip_height();
|
|
|
|
|
2021-10-18 10:39:56 -07:00
|
|
|
// Clean up completed download tasks and add to mempool if successful.
|
2023-11-02 08:00:18 -07:00
|
|
|
while let Poll::Ready(Some(r)) =
|
|
|
|
pin!(tx_downloads.timeout(RATE_LIMIT_DELAY)).poll_next(cx)
|
|
|
|
{
|
2021-10-18 10:39:56 -07:00
|
|
|
match r {
|
2023-11-02 08:00:18 -07:00
|
|
|
Ok(Ok((tx, expected_tip_height))) => {
|
2023-02-16 12:06:42 -08:00
|
|
|
// # Correctness:
|
|
|
|
//
|
|
|
|
// It's okay to use tip height here instead of the tip hash since
|
|
|
|
// chain_tip_change.last_tip_change() returns a `TipAction::Reset` when
|
|
|
|
// the best chain changes (which is the only way to stay at the same height), and the
|
|
|
|
// mempool re-verifies all pending tx_downloads when there's a `TipAction::Reset`.
|
|
|
|
if best_tip_height == expected_tip_height {
|
|
|
|
let insert_result = storage.insert(tx.clone());
|
|
|
|
|
|
|
|
tracing::trace!(
|
|
|
|
?insert_result,
|
|
|
|
"got Ok(_) transaction verify, tried to store",
|
|
|
|
);
|
|
|
|
|
|
|
|
if let Ok(inserted_id) = insert_result {
|
|
|
|
// Save transaction ids that we will send to peers
|
|
|
|
send_to_peers_ids.insert(inserted_id);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tracing::trace!("chain grew during tx verification, retrying ..",);
|
|
|
|
|
|
|
|
// We don't care if re-queueing the transaction request fails.
|
|
|
|
let _result =
|
|
|
|
tx_downloads.download_if_needed_and_verify(tx.transaction.into());
|
2021-10-14 06:19:21 -07:00
|
|
|
}
|
|
|
|
}
|
2023-11-02 08:00:18 -07:00
|
|
|
Ok(Err((txid, error))) => {
|
2022-09-06 06:32:33 -07:00
|
|
|
tracing::debug!(?txid, ?error, "mempool transaction failed to verify");
|
|
|
|
|
2024-01-01 17:26:54 -08:00
|
|
|
metrics::counter!("mempool.failed.verify.tasks.total", "reason" => error.to_string()).increment(1);
|
2022-09-06 06:32:33 -07:00
|
|
|
storage.reject_if_needed(txid, error);
|
2021-10-18 10:39:56 -07:00
|
|
|
}
|
2023-11-02 08:00:18 -07:00
|
|
|
Err(_elapsed) => {
|
|
|
|
// A timeout happens when the stream hangs waiting for another service,
|
|
|
|
// so there is no specific transaction ID.
|
|
|
|
|
|
|
|
tracing::info!("mempool transaction failed to verify due to timeout");
|
|
|
|
|
2024-01-01 17:26:54 -08:00
|
|
|
metrics::counter!("mempool.failed.verify.tasks.total", "reason" => "timeout").increment(1);
|
2023-11-02 08:00:18 -07:00
|
|
|
}
|
2021-10-18 10:39:56 -07:00
|
|
|
};
|
|
|
|
}
|
2021-10-14 06:19:21 -07:00
|
|
|
|
2021-10-18 10:39:56 -07:00
|
|
|
// Handle best chain tip changes
|
|
|
|
if let Some(TipAction::Grow { block }) = tip_action {
|
2022-09-06 06:32:33 -07:00
|
|
|
tracing::trace!(block_height = ?block.height, "handling blocks added to tip");
|
2023-04-03 16:22:07 -07:00
|
|
|
*last_seen_tip_hash = block.hash;
|
2022-09-06 06:32:33 -07:00
|
|
|
|
2021-10-18 10:39:56 -07:00
|
|
|
// Cancel downloads/verifications/storage of transactions
|
|
|
|
// with the same mined IDs as recently mined transactions.
|
|
|
|
let mined_ids = block.transaction_hashes.iter().cloned().collect();
|
|
|
|
tx_downloads.cancel(&mined_ids);
|
2022-10-24 12:48:45 -07:00
|
|
|
storage.reject_and_remove_same_effects(&mined_ids, block.transactions);
|
2023-01-27 13:46:51 -08:00
|
|
|
|
|
|
|
// Clear any transaction rejections if they might have become valid after
|
|
|
|
// the new block was added to the tip.
|
2021-10-18 10:39:56 -07:00
|
|
|
storage.clear_tip_rejections();
|
|
|
|
}
|
2021-10-12 17:50:35 -07:00
|
|
|
|
2021-10-18 10:39:56 -07:00
|
|
|
// Remove expired transactions from the mempool.
|
2023-01-27 13:46:51 -08:00
|
|
|
//
|
|
|
|
// Lock times never expire, because block times are strictly increasing.
|
|
|
|
// So we don't need to check them here.
|
2023-02-16 12:06:42 -08:00
|
|
|
if let Some(tip_height) = best_tip_height {
|
2021-10-18 10:39:56 -07:00
|
|
|
let expired_transactions = storage.remove_expired_transactions(tip_height);
|
|
|
|
// Remove transactions that are expired from the peers list
|
|
|
|
send_to_peers_ids =
|
|
|
|
Self::remove_expired_from_peer_list(&send_to_peers_ids, &expired_transactions);
|
2022-09-06 06:32:33 -07:00
|
|
|
|
|
|
|
if !expired_transactions.is_empty() {
|
|
|
|
tracing::debug!(
|
|
|
|
?expired_transactions,
|
|
|
|
"removed expired transactions from the mempool",
|
|
|
|
);
|
|
|
|
}
|
2021-09-28 16:06:40 -07:00
|
|
|
}
|
2021-10-18 10:39:56 -07:00
|
|
|
|
|
|
|
// Send transactions that were not rejected nor expired to peers
|
|
|
|
if !send_to_peers_ids.is_empty() {
|
2022-09-06 06:32:33 -07:00
|
|
|
tracing::trace!(?send_to_peers_ids, "sending new transactions to peers");
|
|
|
|
|
2022-05-09 20:41:51 -07:00
|
|
|
self.transaction_sender.send(send_to_peers_ids)?;
|
2021-09-13 13:28:07 -07:00
|
|
|
}
|
|
|
|
}
|
2021-09-29 09:52:44 -07:00
|
|
|
|
2023-04-13 01:42:17 -07:00
|
|
|
self.update_metrics();
|
|
|
|
|
2021-08-27 07:36:17 -07:00
|
|
|
Poll::Ready(Ok(()))
|
|
|
|
}
|
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
/// Call the mempool service.
|
|
|
|
///
|
|
|
|
/// Errors indicate that the peer has done something wrong or unexpected,
|
|
|
|
/// and will cause callers to disconnect from the remote peer.
|
2021-08-27 07:36:17 -07:00
|
|
|
#[instrument(name = "mempool", skip(self, req))]
|
|
|
|
fn call(&mut self, req: Request) -> Self::Future {
|
2021-09-28 16:06:40 -07:00
|
|
|
match &mut self.active_state {
|
|
|
|
ActiveState::Enabled {
|
|
|
|
storage,
|
|
|
|
tx_downloads,
|
2023-04-03 16:22:07 -07:00
|
|
|
#[cfg(feature = "getblocktemplate-rpcs")]
|
|
|
|
last_seen_tip_hash,
|
|
|
|
#[cfg(not(feature = "getblocktemplate-rpcs"))]
|
|
|
|
last_seen_tip_hash: _,
|
2021-09-28 16:06:40 -07:00
|
|
|
} => match req {
|
2021-10-18 12:23:21 -07:00
|
|
|
// Queries
|
2021-09-28 16:06:40 -07:00
|
|
|
Request::TransactionIds => {
|
2022-09-06 06:32:33 -07:00
|
|
|
trace!(?req, "got mempool request");
|
|
|
|
|
|
|
|
let res: HashSet<_> = storage.tx_ids().collect();
|
|
|
|
|
|
|
|
// This log line is checked by tests,
|
|
|
|
// because lightwalletd doesn't return mempool transactions at the moment.
|
|
|
|
//
|
|
|
|
// TODO: downgrade to trace level when we can check transactions via gRPC
|
|
|
|
info!(?req, res_count = ?res.len(), "answered mempool request");
|
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
async move { Ok(Response::TransactionIds(res)) }.boxed()
|
|
|
|
}
|
2022-11-02 20:25:01 -07:00
|
|
|
|
2022-09-06 06:32:33 -07:00
|
|
|
Request::TransactionsById(ref ids) => {
|
|
|
|
trace!(?req, "got mempool request");
|
|
|
|
|
|
|
|
let res: Vec<_> = storage.transactions_exact(ids.clone()).cloned().collect();
|
|
|
|
|
|
|
|
trace!(?req, res_count = ?res.len(), "answered mempool request");
|
|
|
|
|
2021-10-06 15:26:36 -07:00
|
|
|
async move { Ok(Response::Transactions(res)) }.boxed()
|
2021-09-28 16:06:40 -07:00
|
|
|
}
|
2022-09-06 06:32:33 -07:00
|
|
|
Request::TransactionsByMinedId(ref ids) => {
|
|
|
|
trace!(?req, "got mempool request");
|
|
|
|
|
|
|
|
let res: Vec<_> = storage
|
|
|
|
.transactions_same_effects(ids.clone())
|
|
|
|
.cloned()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
trace!(?req, res_count = ?res.len(), "answered mempool request");
|
|
|
|
|
2022-03-18 16:00:03 -07:00
|
|
|
async move { Ok(Response::Transactions(res)) }.boxed()
|
|
|
|
}
|
2022-11-03 10:03:41 -07:00
|
|
|
|
2022-11-02 20:25:01 -07:00
|
|
|
#[cfg(feature = "getblocktemplate-rpcs")]
|
2022-11-03 10:03:41 -07:00
|
|
|
Request::FullTransactions => {
|
2022-11-02 20:25:01 -07:00
|
|
|
trace!(?req, "got mempool request");
|
|
|
|
|
2023-04-03 16:22:07 -07:00
|
|
|
let transactions: Vec<_> = storage.full_transactions().cloned().collect();
|
2022-11-02 20:25:01 -07:00
|
|
|
|
2023-04-03 16:22:07 -07:00
|
|
|
trace!(?req, transactions_count = ?transactions.len(), "answered mempool request");
|
|
|
|
|
|
|
|
let response = Response::FullTransactions {
|
|
|
|
transactions,
|
|
|
|
last_seen_tip_hash: *last_seen_tip_hash,
|
|
|
|
};
|
2022-11-02 20:25:01 -07:00
|
|
|
|
2023-04-03 16:22:07 -07:00
|
|
|
async move { Ok(response) }.boxed()
|
2022-11-02 20:25:01 -07:00
|
|
|
}
|
|
|
|
|
2022-09-06 06:32:33 -07:00
|
|
|
Request::RejectedTransactionIds(ref ids) => {
|
|
|
|
trace!(?req, "got mempool request");
|
|
|
|
|
|
|
|
let res = storage.rejected_transactions(ids.clone()).collect();
|
|
|
|
|
|
|
|
trace!(?req, ?res, "answered mempool request");
|
|
|
|
|
2021-10-06 15:45:15 -07:00
|
|
|
async move { Ok(Response::RejectedTransactionIds(res)) }.boxed()
|
2021-09-28 16:06:40 -07:00
|
|
|
}
|
2021-10-18 12:23:21 -07:00
|
|
|
|
|
|
|
// Queue mempool candidates
|
2021-09-28 16:06:40 -07:00
|
|
|
Request::Queue(gossiped_txs) => {
|
2022-09-06 06:32:33 -07:00
|
|
|
trace!(req_count = ?gossiped_txs.len(), "got mempool Queue request");
|
|
|
|
|
2022-02-25 13:43:21 -08:00
|
|
|
let rsp: Vec<Result<(), BoxError>> = gossiped_txs
|
2021-09-28 16:06:40 -07:00
|
|
|
.into_iter()
|
2022-02-25 13:43:21 -08:00
|
|
|
.map(|gossiped_tx| -> Result<(), MempoolError> {
|
2021-10-15 11:03:13 -07:00
|
|
|
storage.should_download_or_verify(gossiped_tx.id())?;
|
2021-09-28 16:06:40 -07:00
|
|
|
tx_downloads.download_if_needed_and_verify(gossiped_tx)?;
|
2022-09-06 06:32:33 -07:00
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
Ok(())
|
|
|
|
})
|
2022-02-25 13:43:21 -08:00
|
|
|
.map(|result| result.map_err(BoxError::from))
|
2021-09-28 16:06:40 -07:00
|
|
|
.collect();
|
2023-04-13 01:42:17 -07:00
|
|
|
|
|
|
|
// We've added transactions to the queue
|
|
|
|
self.update_metrics();
|
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
async move { Ok(Response::Queued(rsp)) }.boxed()
|
|
|
|
}
|
2021-10-18 12:23:21 -07:00
|
|
|
|
|
|
|
// Store successfully downloaded and verified transactions in the mempool
|
|
|
|
Request::CheckForVerifiedTransactions => {
|
2022-09-06 06:32:33 -07:00
|
|
|
trace!(?req, "got mempool request");
|
|
|
|
|
2021-10-18 12:23:21 -07:00
|
|
|
// all the work for this request is done in poll_ready
|
|
|
|
async move { Ok(Response::CheckedForVerifiedTransactions) }.boxed()
|
|
|
|
}
|
2021-09-28 16:06:40 -07:00
|
|
|
},
|
|
|
|
ActiveState::Disabled => {
|
2022-09-06 06:32:33 -07:00
|
|
|
// TODO: add the name of the request, but not the content,
|
|
|
|
// like the command() or Display impls of network requests
|
|
|
|
trace!("got mempool request while mempool is disabled");
|
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
// We can't return an error since that will cause a disconnection
|
|
|
|
// by the peer connection handler. Therefore, return successful
|
|
|
|
// empty responses.
|
2023-04-03 16:22:07 -07:00
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
let resp = match req {
|
2022-11-03 10:03:41 -07:00
|
|
|
// Return empty responses for queries.
|
2021-09-28 16:06:40 -07:00
|
|
|
Request::TransactionIds => Response::TransactionIds(Default::default()),
|
2022-11-02 20:25:01 -07:00
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
Request::TransactionsById(_) => Response::Transactions(Default::default()),
|
2022-03-18 16:00:03 -07:00
|
|
|
Request::TransactionsByMinedId(_) => Response::Transactions(Default::default()),
|
2022-11-02 20:25:01 -07:00
|
|
|
#[cfg(feature = "getblocktemplate-rpcs")]
|
2023-04-03 16:22:07 -07:00
|
|
|
Request::FullTransactions => {
|
|
|
|
return async move {
|
|
|
|
Err("mempool is not active: wait for Zebra to sync to the tip".into())
|
|
|
|
}
|
|
|
|
.boxed()
|
|
|
|
}
|
2022-11-02 20:25:01 -07:00
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
Request::RejectedTransactionIds(_) => {
|
|
|
|
Response::RejectedTransactionIds(Default::default())
|
|
|
|
}
|
2021-10-18 12:23:21 -07:00
|
|
|
|
2022-11-03 10:03:41 -07:00
|
|
|
// Don't queue mempool candidates, because there is no queue.
|
2021-09-28 16:06:40 -07:00
|
|
|
Request::Queue(gossiped_txs) => Response::Queued(
|
2021-10-18 12:23:21 -07:00
|
|
|
// Special case; we can signal the error inside the response,
|
|
|
|
// because the inbound service ignores inner errors.
|
2022-02-25 13:43:21 -08:00
|
|
|
iter::repeat(MempoolError::Disabled)
|
2021-09-28 16:06:40 -07:00
|
|
|
.take(gossiped_txs.len())
|
2022-02-25 13:43:21 -08:00
|
|
|
.map(BoxError::from)
|
|
|
|
.map(Err)
|
2021-09-28 16:06:40 -07:00
|
|
|
.collect(),
|
|
|
|
),
|
2021-10-18 12:23:21 -07:00
|
|
|
|
|
|
|
// Check if the mempool should be enabled.
|
|
|
|
// This request makes sure mempools are debug-enabled in the acceptance tests.
|
|
|
|
Request::CheckForVerifiedTransactions => {
|
|
|
|
// all the work for this request is done in poll_ready
|
|
|
|
Response::CheckedForVerifiedTransactions
|
|
|
|
}
|
2021-09-28 16:06:40 -07:00
|
|
|
};
|
2023-04-03 16:22:07 -07:00
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
async move { Ok(resp) }.boxed()
|
2021-09-13 13:28:07 -07:00
|
|
|
}
|
2021-08-27 07:36:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 01:42:17 -07:00
|
|
|
|
|
|
|
impl Drop for Mempool {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
self.disable_metrics();
|
|
|
|
}
|
|
|
|
}
|