2021-11-18 17:55:38 -08:00
|
|
|
//! The inbound service handles requests from Zebra's peers.
|
|
|
|
//!
|
|
|
|
//! It downloads and verifies gossiped blocks and mempool transactions,
|
|
|
|
//! when Zebra is close to the chain tip.
|
|
|
|
//!
|
|
|
|
//! It also responds to peer requests for blocks, transactions, and peer addresses.
|
|
|
|
|
2020-09-18 12:18:22 -07:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
2021-04-18 23:04:24 -07:00
|
|
|
sync::Arc,
|
2020-09-18 12:18:22 -07:00
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
2021-12-03 10:09:43 -08:00
|
|
|
use chrono::Utc;
|
2020-09-18 13:47:31 -07:00
|
|
|
use futures::{
|
|
|
|
future::{FutureExt, TryFutureExt},
|
2021-01-24 20:55:36 -08:00
|
|
|
stream::Stream,
|
2020-09-18 13:47:31 -07:00
|
|
|
};
|
2021-11-23 09:42:44 -08:00
|
|
|
use tokio::sync::oneshot::{self, error::TryRecvError};
|
2021-01-13 01:39:33 -08:00
|
|
|
use tower::{buffer::Buffer, timeout::Timeout, util::BoxService, Service, ServiceExt};
|
2020-09-18 12:18:22 -07:00
|
|
|
|
|
|
|
use zebra_network as zn;
|
2020-09-18 13:47:31 -07:00
|
|
|
use zebra_state as zs;
|
2020-09-18 12:18:22 -07:00
|
|
|
|
2020-11-24 14:53:57 -08:00
|
|
|
use zebra_chain::block::{self, Block};
|
2021-09-13 13:28:07 -07:00
|
|
|
use zebra_consensus::chain::VerifyChainError;
|
2021-11-11 16:25:23 -08:00
|
|
|
use zebra_network::{constants::MAX_ADDRS_IN_MESSAGE, AddressBook};
|
2020-11-24 14:53:57 -08:00
|
|
|
|
2021-01-13 01:39:33 -08:00
|
|
|
// Re-use the syncer timeouts for consistency.
|
2021-09-02 06:42:31 -07:00
|
|
|
use super::{
|
2021-09-13 13:28:07 -07:00
|
|
|
mempool, mempool as mp,
|
2021-09-02 06:42:31 -07:00
|
|
|
sync::{BLOCK_DOWNLOAD_TIMEOUT, BLOCK_VERIFY_TIMEOUT},
|
|
|
|
};
|
2021-01-13 01:39:33 -08:00
|
|
|
|
2020-11-19 09:55:06 -08:00
|
|
|
mod downloads;
|
2021-09-02 06:42:31 -07:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2021-09-01 17:06:20 -07:00
|
|
|
use downloads::Downloads as BlockDownloads;
|
2020-11-19 09:55:06 -08:00
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
/// A security parameter to return only 1/3 of available addresses as a
|
|
|
|
/// response to a `Peers` request.
|
|
|
|
const FRAC_OF_AVAILABLE_ADDRESS: f64 = 1. / 3.;
|
|
|
|
|
|
|
|
type BlockDownloadPeerSet =
|
|
|
|
Buffer<BoxService<zn::Request, zn::Response, zn::BoxError>, zn::Request>;
|
2020-09-18 13:47:31 -07:00
|
|
|
type State = Buffer<BoxService<zs::Request, zs::Response, zs::BoxError>, zs::Request>;
|
2021-09-08 11:51:17 -07:00
|
|
|
type Mempool = Buffer<BoxService<mp::Request, mp::Response, mp::BoxError>, mp::Request>;
|
2021-09-01 17:06:20 -07:00
|
|
|
type BlockVerifier = Buffer<BoxService<Arc<Block>, block::Hash, VerifyChainError>, Arc<Block>>;
|
2021-11-23 09:42:44 -08:00
|
|
|
type GossipedBlockDownloads =
|
|
|
|
BlockDownloads<Timeout<BlockDownloadPeerSet>, Timeout<BlockVerifier>, State>;
|
2020-09-18 12:18:22 -07:00
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
/// The services used by the [`Inbound`] service.
|
|
|
|
pub struct InboundSetupData {
|
|
|
|
/// A shared list of peer addresses.
|
|
|
|
pub address_book: Arc<std::sync::Mutex<AddressBook>>,
|
2021-01-24 22:11:19 -08:00
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
/// A service that can be used to download gossiped blocks.
|
|
|
|
pub block_download_peer_set: BlockDownloadPeerSet,
|
|
|
|
|
|
|
|
/// A service that verifies downloaded blocks.
|
|
|
|
///
|
|
|
|
/// Given to `Inbound.block_downloads` after the required services are set up.
|
|
|
|
pub block_verifier: BlockVerifier,
|
|
|
|
|
|
|
|
/// A service that manages transactions in the memory pool.
|
|
|
|
pub mempool: Mempool,
|
|
|
|
|
|
|
|
/// A service that manages cached blockchain state.
|
|
|
|
pub state: State,
|
|
|
|
}
|
2021-11-02 15:21:54 -07:00
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
/// Tracks the internal state of the [`Inbound`] service during setup.
|
2021-01-24 22:11:19 -08:00
|
|
|
pub enum Setup {
|
2021-11-23 09:42:44 -08:00
|
|
|
/// Waiting for service setup to complete.
|
2021-01-24 22:11:19 -08:00
|
|
|
///
|
2021-11-23 09:42:44 -08:00
|
|
|
/// All requests are ignored.
|
|
|
|
Pending {
|
|
|
|
/// A oneshot channel used to receive required services,
|
|
|
|
/// after they are set up.
|
|
|
|
setup: oneshot::Receiver<InboundSetupData>,
|
2021-01-24 22:11:19 -08:00
|
|
|
},
|
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
/// Setup is complete.
|
2021-01-24 22:11:19 -08:00
|
|
|
///
|
|
|
|
/// All requests are answered.
|
|
|
|
Initialized {
|
|
|
|
/// A shared list of peer addresses.
|
2021-04-18 23:04:24 -07:00
|
|
|
address_book: Arc<std::sync::Mutex<zn::AddressBook>>,
|
2021-01-24 22:11:19 -08:00
|
|
|
|
2021-05-07 04:01:11 -07:00
|
|
|
/// A `futures::Stream` that downloads and verifies gossiped blocks.
|
2021-11-23 09:42:44 -08:00
|
|
|
block_downloads: Pin<Box<GossipedBlockDownloads>>,
|
2021-09-01 17:06:20 -07:00
|
|
|
|
2021-09-13 13:28:07 -07:00
|
|
|
/// A service that manages transactions in the memory pool.
|
|
|
|
mempool: Mempool,
|
2021-11-23 09:42:44 -08:00
|
|
|
|
|
|
|
/// A service that manages cached blockchain state.
|
|
|
|
state: State,
|
2021-01-24 22:11:19 -08:00
|
|
|
},
|
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
/// Temporary state used in the inbound service's internal initialization code.
|
2021-01-24 22:11:19 -08:00
|
|
|
///
|
2021-11-23 09:42:44 -08:00
|
|
|
/// If this state occurs outside the service initialization code, the service panics.
|
2021-01-24 22:11:19 -08:00
|
|
|
FailedInit,
|
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
/// Setup failed, because the setup channel permanently failed.
|
2021-01-24 22:11:19 -08:00
|
|
|
/// The service keeps returning readiness errors for every request.
|
2021-11-23 09:42:44 -08:00
|
|
|
FailedRecv {
|
|
|
|
/// The original channel error.
|
|
|
|
error: SharedRecvError,
|
|
|
|
},
|
2021-01-27 14:18:26 -08:00
|
|
|
}
|
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
/// A wrapper around `Arc<TryRecvError>` that implements `Error`.
|
2021-01-27 14:18:26 -08:00
|
|
|
#[derive(thiserror::Error, Debug, Clone)]
|
|
|
|
#[error(transparent)]
|
|
|
|
pub struct SharedRecvError(Arc<TryRecvError>);
|
|
|
|
|
|
|
|
impl From<TryRecvError> for SharedRecvError {
|
|
|
|
fn from(source: TryRecvError) -> Self {
|
|
|
|
Self(Arc::new(source))
|
|
|
|
}
|
2021-01-24 22:11:19 -08:00
|
|
|
}
|
2020-09-18 12:18:22 -07:00
|
|
|
|
2020-09-18 16:13:57 -07:00
|
|
|
/// Uses the node state to respond to inbound peer requests.
|
|
|
|
///
|
|
|
|
/// This service, wrapped in appropriate middleware, is passed to
|
|
|
|
/// `zebra_network::init` to respond to inbound peer requests.
|
|
|
|
///
|
|
|
|
/// The `Inbound` service is responsible for:
|
|
|
|
///
|
|
|
|
/// - supplying network data like peer addresses to other nodes;
|
|
|
|
/// - supplying chain data like blocks to other nodes;
|
2021-11-23 09:42:44 -08:00
|
|
|
/// - supplying mempool transactions to other nodes;
|
|
|
|
/// - receiving gossiped transactions; and
|
|
|
|
/// - receiving gossiped blocks.
|
2020-09-18 16:13:57 -07:00
|
|
|
///
|
|
|
|
/// Because the `Inbound` service is responsible for participating in the gossip
|
|
|
|
/// protocols used for transaction and block diffusion, there is a potential
|
2021-11-23 09:42:44 -08:00
|
|
|
/// overlap with the `ChainSync` and `Mempool` components.
|
2020-09-18 16:13:57 -07:00
|
|
|
///
|
2021-11-23 09:42:44 -08:00
|
|
|
/// The division of responsibility is that:
|
|
|
|
///
|
|
|
|
/// The `ChainSync` and `Mempool` components are *internally driven*,
|
|
|
|
/// periodically polling the network to check for new blocks or transactions.
|
|
|
|
///
|
|
|
|
/// The `Inbound` service is *externally driven*, responding to block gossip
|
|
|
|
/// by attempting to download and validate advertised blocks.
|
|
|
|
///
|
|
|
|
/// Gossiped transactions are forwarded to the mempool downloader,
|
|
|
|
/// which unifies polled and gossiped transactions into a single download list.
|
2020-09-18 12:18:22 -07:00
|
|
|
pub struct Inbound {
|
2021-11-23 09:42:44 -08:00
|
|
|
/// Provides service dependencies, if they are available.
|
2021-01-13 02:26:50 -08:00
|
|
|
///
|
2021-11-23 09:42:44 -08:00
|
|
|
/// Some services are unavailable until Zebra has completed setup.
|
|
|
|
setup: Setup,
|
2020-09-18 12:18:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Inbound {
|
2021-11-18 17:55:38 -08:00
|
|
|
/// Create a new inbound service.
|
|
|
|
///
|
2021-11-23 09:42:44 -08:00
|
|
|
/// Dependent services are sent via the `setup` channel after initialization.
|
|
|
|
pub fn new(setup: oneshot::Receiver<InboundSetupData>) -> Inbound {
|
|
|
|
Inbound {
|
|
|
|
setup: Setup::Pending { setup },
|
2020-09-18 12:18:22 -07:00
|
|
|
}
|
|
|
|
}
|
2021-01-27 15:09:40 -08:00
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
/// Remove `self.setup`, temporarily replacing it with an invalid state.
|
2021-01-27 15:09:40 -08:00
|
|
|
fn take_setup(&mut self) -> Setup {
|
2021-11-23 09:42:44 -08:00
|
|
|
let mut setup = Setup::FailedInit;
|
|
|
|
std::mem::swap(&mut self.setup, &mut setup);
|
|
|
|
setup
|
2021-01-27 15:09:40 -08:00
|
|
|
}
|
2020-09-18 12:18:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Service<zn::Request> for Inbound {
|
|
|
|
type Response = zn::Response;
|
|
|
|
type Error = zn::BoxError;
|
|
|
|
type Future =
|
|
|
|
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
|
|
|
|
|
2020-09-18 14:11:29 -07:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2021-11-23 09:42:44 -08:00
|
|
|
// Check whether the setup is finished, but don't wait for it to
|
2020-09-19 23:50:51 -07:00
|
|
|
// become ready before reporting readiness. We expect to get it "soon",
|
|
|
|
// and reporting unreadiness might cause unwanted load-shedding, since
|
|
|
|
// the load-shed middleware is unable to distinguish being unready due
|
|
|
|
// to load from being unready while waiting on setup.
|
2021-01-28 19:06:59 -08:00
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
// Every setup variant handler must provide a result
|
2021-01-28 19:06:59 -08:00
|
|
|
let result;
|
2021-01-27 15:09:40 -08:00
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
self.setup = match self.take_setup() {
|
|
|
|
Setup::Pending { mut setup } => match setup.try_recv() {
|
|
|
|
Ok(setup_data) => {
|
|
|
|
let InboundSetupData {
|
|
|
|
address_book,
|
|
|
|
block_download_peer_set,
|
|
|
|
block_verifier,
|
|
|
|
mempool,
|
|
|
|
state,
|
|
|
|
} = setup_data;
|
|
|
|
|
2021-09-01 17:06:20 -07:00
|
|
|
let block_downloads = Box::pin(BlockDownloads::new(
|
2021-11-23 09:42:44 -08:00
|
|
|
Timeout::new(block_download_peer_set.clone(), BLOCK_DOWNLOAD_TIMEOUT),
|
2021-09-01 17:06:20 -07:00
|
|
|
Timeout::new(block_verifier, BLOCK_VERIFY_TIMEOUT),
|
2021-11-23 09:42:44 -08:00
|
|
|
state.clone(),
|
2021-09-01 17:06:20 -07:00
|
|
|
));
|
2021-09-13 13:28:07 -07:00
|
|
|
|
2021-01-28 19:06:59 -08:00
|
|
|
result = Ok(());
|
2021-01-27 15:09:40 -08:00
|
|
|
Setup::Initialized {
|
|
|
|
address_book,
|
2021-09-01 17:06:20 -07:00
|
|
|
block_downloads,
|
2021-09-13 13:28:07 -07:00
|
|
|
mempool,
|
2021-11-23 09:42:44 -08:00
|
|
|
state,
|
2021-01-24 22:11:19 -08:00
|
|
|
}
|
2021-01-27 15:09:40 -08:00
|
|
|
}
|
|
|
|
Err(TryRecvError::Empty) => {
|
|
|
|
// There's no setup data yet, so keep waiting for it
|
2021-01-28 19:06:59 -08:00
|
|
|
result = Ok(());
|
2021-11-23 09:42:44 -08:00
|
|
|
Setup::Pending { setup }
|
2021-01-24 22:11:19 -08:00
|
|
|
}
|
2021-01-27 15:09:40 -08:00
|
|
|
Err(error @ TryRecvError::Closed) => {
|
2021-11-23 09:42:44 -08:00
|
|
|
// Mark the service as failed, because setup failed
|
|
|
|
error!(?error, "inbound setup failed");
|
2021-01-27 15:09:40 -08:00
|
|
|
let error: SharedRecvError = error.into();
|
|
|
|
result = Err(error.clone().into());
|
|
|
|
Setup::FailedRecv { error }
|
|
|
|
}
|
|
|
|
},
|
2021-11-23 09:42:44 -08:00
|
|
|
// Make sure previous setups were left in a valid state
|
2021-01-28 19:06:24 -08:00
|
|
|
Setup::FailedInit => unreachable!("incomplete previous Inbound initialization"),
|
2021-11-23 09:42:44 -08:00
|
|
|
// If setup failed, report service failure
|
2021-01-27 15:09:40 -08:00
|
|
|
Setup::FailedRecv { error } => {
|
|
|
|
result = Err(error.clone().into());
|
|
|
|
Setup::FailedRecv { error }
|
2021-01-24 22:11:19 -08:00
|
|
|
}
|
2021-01-27 15:09:40 -08:00
|
|
|
// Clean up completed download tasks, ignoring their results
|
|
|
|
Setup::Initialized {
|
|
|
|
address_book,
|
2021-09-01 17:06:20 -07:00
|
|
|
mut block_downloads,
|
2021-09-13 13:28:07 -07:00
|
|
|
mempool,
|
2021-11-23 09:42:44 -08:00
|
|
|
state,
|
2021-01-27 15:09:40 -08:00
|
|
|
} => {
|
2021-09-01 17:06:20 -07:00
|
|
|
while let Poll::Ready(Some(_)) = block_downloads.as_mut().poll_next(cx) {}
|
2021-01-28 19:06:59 -08:00
|
|
|
|
|
|
|
result = Ok(());
|
2021-01-27 15:09:40 -08:00
|
|
|
Setup::Initialized {
|
|
|
|
address_book,
|
2021-09-01 17:06:20 -07:00
|
|
|
block_downloads,
|
2021-09-13 13:28:07 -07:00
|
|
|
mempool,
|
2021-11-23 09:42:44 -08:00
|
|
|
state,
|
2021-01-27 15:09:40 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-11-19 09:55:06 -08:00
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
// Make sure we're leaving the setup in a valid state
|
|
|
|
if matches!(self.setup, Setup::FailedInit) {
|
2021-01-28 19:06:24 -08:00
|
|
|
unreachable!("incomplete Inbound initialization after poll_ready state handling");
|
|
|
|
}
|
|
|
|
|
2021-01-22 05:03:33 -08:00
|
|
|
// TODO:
|
2021-01-24 20:55:36 -08:00
|
|
|
// * do we want to propagate backpressure from the download queue or its outbound network?
|
|
|
|
// currently, the download queue waits for the outbound network in the download future,
|
|
|
|
// and drops new requests after it reaches a hard-coded limit. This is the
|
|
|
|
// "load shed directly" pattern from #1618.
|
|
|
|
// * currently, the state service is always ready, unless its buffer is full.
|
|
|
|
// So we might also want to propagate backpressure from its buffer.
|
2021-02-15 12:14:40 -08:00
|
|
|
// * poll_ready needs to be implemented carefully, to avoid hangs or deadlocks.
|
|
|
|
// See #1593 for details.
|
2021-01-27 15:09:40 -08:00
|
|
|
Poll::Ready(result)
|
2020-09-18 12:18:22 -07:00
|
|
|
}
|
|
|
|
|
2021-09-28 16:06:40 -07:00
|
|
|
/// Call the inbound service.
|
|
|
|
///
|
|
|
|
/// Errors indicate that the peer has done something wrong or unexpected,
|
|
|
|
/// and will cause callers to disconnect from the remote peer.
|
2020-11-24 16:05:11 -08:00
|
|
|
#[instrument(name = "inbound", skip(self, req))]
|
2020-09-18 12:18:22 -07:00
|
|
|
fn call(&mut self, req: zn::Request) -> Self::Future {
|
2021-11-23 09:42:44 -08:00
|
|
|
let (address_book, block_downloads, mempool, state) = match &mut self.setup {
|
|
|
|
Setup::Initialized {
|
|
|
|
address_book,
|
|
|
|
block_downloads,
|
|
|
|
mempool,
|
|
|
|
state,
|
|
|
|
} => (address_book, block_downloads, mempool, state),
|
|
|
|
_ => {
|
|
|
|
debug!("ignoring request from remote peer during setup");
|
|
|
|
return async { Ok(zn::Response::Nil) }.boxed();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-09-18 12:18:22 -07:00
|
|
|
match req {
|
2021-01-24 22:11:19 -08:00
|
|
|
zn::Request::Peers => {
|
2021-11-23 09:42:44 -08:00
|
|
|
// # Security
|
|
|
|
//
|
|
|
|
// We truncate the list to not reveal our entire peer set in one call.
|
|
|
|
// But we don't monitor repeated requests and the results are shuffled,
|
|
|
|
// a crawler could just send repeated queries and get the full list.
|
|
|
|
//
|
|
|
|
// # Correctness
|
|
|
|
//
|
|
|
|
// Briefly hold the address book threaded mutex while
|
|
|
|
// cloning the address book. Then sanitize after releasing
|
|
|
|
// the lock.
|
|
|
|
let peers = address_book.lock().unwrap().clone();
|
|
|
|
|
2021-12-03 10:09:43 -08:00
|
|
|
// Correctness: get the current time after acquiring the address book lock.
|
|
|
|
let now = Utc::now();
|
|
|
|
|
2021-11-23 09:42:44 -08:00
|
|
|
// Send a sanitized response
|
2021-12-03 10:09:43 -08:00
|
|
|
let mut peers = peers.sanitized(now);
|
2021-11-23 09:42:44 -08:00
|
|
|
|
|
|
|
// Truncate the list
|
|
|
|
let truncate_at = MAX_ADDRS_IN_MESSAGE
|
|
|
|
.min((peers.len() as f64 * FRAC_OF_AVAILABLE_ADDRESS).ceil() as usize);
|
|
|
|
peers.truncate(truncate_at);
|
|
|
|
|
|
|
|
if !peers.is_empty() {
|
|
|
|
async { Ok(zn::Response::Peers(peers)) }.boxed()
|
2021-01-24 22:11:19 -08:00
|
|
|
} else {
|
2021-11-23 09:42:44 -08:00
|
|
|
info!("ignoring `Peers` request from remote peer because our address book is empty");
|
2021-01-24 21:16:04 -08:00
|
|
|
async { Ok(zn::Response::Nil) }.boxed()
|
|
|
|
}
|
2021-01-24 22:11:19 -08:00
|
|
|
}
|
2020-09-18 13:47:31 -07:00
|
|
|
zn::Request::BlocksByHash(hashes) => {
|
2021-01-22 05:03:33 -08:00
|
|
|
// Correctness:
|
|
|
|
//
|
2021-02-15 12:14:16 -08:00
|
|
|
// We can't use `call_all` here, because it can hold one buffer slot per concurrent
|
|
|
|
// future, until the `CallAll` struct is dropped. We can't hold those slots in this
|
|
|
|
// future because:
|
|
|
|
// * we're not sure when the returned future will complete, and
|
|
|
|
// * we don't limit how many returned futures can be concurrently running
|
2021-01-22 05:03:33 -08:00
|
|
|
// https://github.com/tower-rs/tower/blob/master/tower/src/util/call_all/common.rs#L112
|
2021-01-27 14:44:22 -08:00
|
|
|
use futures::stream::TryStreamExt;
|
|
|
|
hashes
|
|
|
|
.into_iter()
|
|
|
|
.map(|hash| zs::Request::Block(hash.into()))
|
2021-11-23 09:42:44 -08:00
|
|
|
.map(|request| state.clone().oneshot(request))
|
2021-01-27 14:44:22 -08:00
|
|
|
.collect::<futures::stream::FuturesOrdered<_>>()
|
|
|
|
.try_filter_map(|response| async move {
|
|
|
|
Ok(match response {
|
|
|
|
zs::Response::Block(Some(block)) => Some(block),
|
2021-01-03 19:25:35 -08:00
|
|
|
// `zcashd` ignores missing blocks in GetData responses,
|
|
|
|
// rather than including them in a trailing `NotFound`
|
|
|
|
// message
|
2021-01-27 14:44:22 -08:00
|
|
|
zs::Response::Block(None) => None,
|
2020-09-18 13:47:31 -07:00
|
|
|
_ => unreachable!("wrong response from state"),
|
2021-01-27 14:44:22 -08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
.try_collect::<Vec<_>>()
|
2021-09-23 12:58:00 -07:00
|
|
|
.map_ok(|blocks| {
|
|
|
|
if blocks.is_empty() {
|
|
|
|
zn::Response::Nil
|
|
|
|
} else {
|
|
|
|
zn::Response::Blocks(blocks)
|
|
|
|
}
|
|
|
|
})
|
2021-01-27 14:44:22 -08:00
|
|
|
.boxed()
|
2020-09-18 13:47:31 -07:00
|
|
|
}
|
2021-09-06 15:55:17 -07:00
|
|
|
zn::Request::TransactionsById(transactions) => {
|
2021-11-23 09:42:44 -08:00
|
|
|
let request = mempool::Request::TransactionsById(transactions);
|
|
|
|
mempool.clone().oneshot(request).map_ok(|resp| match resp {
|
|
|
|
mempool::Response::Transactions(transactions) if transactions.is_empty() => zn::Response::Nil,
|
|
|
|
mempool::Response::Transactions(transactions) => zn::Response::Transactions(transactions),
|
|
|
|
_ => unreachable!("Mempool component should always respond to a `TransactionsById` request with a `Transactions` response"),
|
|
|
|
})
|
2021-09-13 13:28:07 -07:00
|
|
|
.boxed()
|
2020-09-18 13:47:31 -07:00
|
|
|
}
|
2020-11-30 13:30:37 -08:00
|
|
|
zn::Request::FindBlocks { known_blocks, stop } => {
|
|
|
|
let request = zs::Request::FindBlockHashes { known_blocks, stop };
|
2021-11-23 09:42:44 -08:00
|
|
|
state.clone().oneshot(request).map_ok(|resp| match resp {
|
|
|
|
zs::Response::BlockHashes(hashes) if hashes.is_empty() => zn::Response::Nil,
|
|
|
|
zs::Response::BlockHashes(hashes) => zn::Response::BlockHashes(hashes),
|
|
|
|
_ => unreachable!("zebra-state should always respond to a `FindBlockHashes` request with a `BlockHashes` response"),
|
|
|
|
})
|
|
|
|
.boxed()
|
2020-09-18 13:47:31 -07:00
|
|
|
}
|
2020-11-30 13:30:37 -08:00
|
|
|
zn::Request::FindHeaders { known_blocks, stop } => {
|
|
|
|
let request = zs::Request::FindBlockHeaders { known_blocks, stop };
|
2021-11-23 09:42:44 -08:00
|
|
|
state.clone().oneshot(request).map_ok(|resp| match resp {
|
|
|
|
zs::Response::BlockHeaders(headers) if headers.is_empty() => zn::Response::Nil,
|
|
|
|
zs::Response::BlockHeaders(headers) => zn::Response::BlockHeaders(headers),
|
|
|
|
_ => unreachable!("zebra-state should always respond to a `FindBlockHeaders` request with a `BlockHeaders` response"),
|
|
|
|
})
|
|
|
|
.boxed()
|
2020-09-18 22:16:59 -07:00
|
|
|
}
|
2021-09-09 06:04:44 -07:00
|
|
|
zn::Request::PushTransaction(transaction) => {
|
2021-11-23 09:42:44 -08:00
|
|
|
mempool
|
|
|
|
.clone()
|
|
|
|
.oneshot(mempool::Request::Queue(vec![transaction.into()]))
|
|
|
|
// The response just indicates if processing was queued or not; ignore it
|
|
|
|
.map_ok(|_resp| zn::Response::Nil)
|
|
|
|
.boxed()
|
2020-09-18 13:47:31 -07:00
|
|
|
}
|
2021-09-01 17:06:20 -07:00
|
|
|
zn::Request::AdvertiseTransactionIds(transactions) => {
|
2021-11-23 09:42:44 -08:00
|
|
|
let transactions = transactions.into_iter().map(Into::into).collect();
|
|
|
|
mempool
|
|
|
|
.clone()
|
|
|
|
.oneshot(mempool::Request::Queue(transactions))
|
|
|
|
// The response just indicates if processing was queued or not; ignore it
|
|
|
|
.map_ok(|_resp| zn::Response::Nil)
|
|
|
|
.boxed()
|
2020-09-18 12:18:22 -07:00
|
|
|
}
|
2020-11-19 09:55:06 -08:00
|
|
|
zn::Request::AdvertiseBlock(hash) => {
|
2021-11-23 09:42:44 -08:00
|
|
|
block_downloads.download_and_verify(hash);
|
2020-11-24 14:20:34 -08:00
|
|
|
async { Ok(zn::Response::Nil) }.boxed()
|
2020-09-18 13:47:31 -07:00
|
|
|
}
|
2021-08-18 15:55:24 -07:00
|
|
|
zn::Request::MempoolTransactionIds => {
|
2021-11-23 09:42:44 -08:00
|
|
|
mempool.clone().oneshot(mempool::Request::TransactionIds).map_ok(|resp| match resp {
|
|
|
|
mempool::Response::TransactionIds(transaction_ids) if transaction_ids.is_empty() => zn::Response::Nil,
|
|
|
|
mempool::Response::TransactionIds(transaction_ids) => zn::Response::TransactionIds(transaction_ids.into_iter().collect()),
|
|
|
|
_ => unreachable!("Mempool component should always respond to a `TransactionIds` request with a `TransactionIds` response"),
|
|
|
|
})
|
2021-09-13 13:28:07 -07:00
|
|
|
.boxed()
|
2020-09-18 22:16:59 -07:00
|
|
|
}
|
2020-09-18 13:47:31 -07:00
|
|
|
zn::Request::Ping(_) => {
|
|
|
|
unreachable!("ping requests are handled internally");
|
|
|
|
}
|
2020-09-18 12:18:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|