2020-09-18 12:18:22 -07:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
2020-09-18 13:47:31 -07:00
|
|
|
use futures::{
|
|
|
|
future::{FutureExt, TryFutureExt},
|
2020-11-24 14:06:30 -08:00
|
|
|
stream::{Stream, TryStreamExt},
|
2020-09-18 13:47:31 -07:00
|
|
|
};
|
2020-09-18 12:18:22 -07:00
|
|
|
use tokio::sync::oneshot;
|
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};
|
|
|
|
use zebra_consensus::chain::VerifyChainError;
|
|
|
|
use zebra_network::AddressBook;
|
|
|
|
|
2021-01-13 01:39:33 -08:00
|
|
|
// Re-use the syncer timeouts for consistency.
|
|
|
|
use super::sync::{BLOCK_DOWNLOAD_TIMEOUT, BLOCK_VERIFY_TIMEOUT};
|
|
|
|
|
2020-11-19 09:55:06 -08:00
|
|
|
mod downloads;
|
|
|
|
use downloads::Downloads;
|
|
|
|
|
2020-09-18 12:18:22 -07:00
|
|
|
type Outbound = 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>;
|
2020-11-24 14:53:57 -08:00
|
|
|
type Verifier = Buffer<BoxService<Arc<Block>, block::Hash, VerifyChainError>, Arc<Block>>;
|
2020-09-18 12:18:22 -07:00
|
|
|
|
|
|
|
pub type SetupData = (Outbound, Arc<Mutex<AddressBook>>);
|
|
|
|
|
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;
|
|
|
|
/// - performing transaction diffusion;
|
|
|
|
/// - performing block diffusion.
|
|
|
|
///
|
|
|
|
/// Because the `Inbound` service is responsible for participating in the gossip
|
|
|
|
/// protocols used for transaction and block diffusion, there is a potential
|
|
|
|
/// overlap with the `ChainSync` component.
|
|
|
|
///
|
|
|
|
/// The division of responsibility is that the `ChainSync` component is
|
|
|
|
/// *internally driven*, periodically polling the network to check whether it is
|
|
|
|
/// behind the current tip, while the `Inbound` service is *externally driven*,
|
|
|
|
/// responding to block gossip by attempting to download and validate advertised
|
|
|
|
/// blocks.
|
2020-09-18 12:18:22 -07:00
|
|
|
pub struct Inbound {
|
2021-01-22 05:02:30 -08:00
|
|
|
// invariants:
|
|
|
|
// * Before setup: address_book and downloads are None, and the *_setup members are Some
|
|
|
|
// * After setup: address_book and downloads are Some, and the *_setup members are None
|
2020-09-18 12:18:22 -07:00
|
|
|
//
|
|
|
|
// why not use an enum for the inbound state? because it would mean
|
|
|
|
// match-wrapping the body of Service::call rather than just expect()ing
|
|
|
|
// some Options.
|
2021-01-13 02:26:50 -08:00
|
|
|
|
|
|
|
// Setup
|
|
|
|
/// A oneshot channel used to receive the address_book and outbound services
|
|
|
|
/// after the network is set up.
|
2021-01-22 02:41:05 -08:00
|
|
|
///
|
|
|
|
/// `None` after the network is set up.
|
2020-09-18 12:18:22 -07:00
|
|
|
network_setup: Option<oneshot::Receiver<SetupData>>,
|
2021-01-13 02:26:50 -08:00
|
|
|
|
2021-01-22 02:41:05 -08:00
|
|
|
/// A service that verifies downloaded blocks. Given to `downloads`
|
|
|
|
/// after the network is set up.
|
|
|
|
///
|
|
|
|
/// `None` after the network is set up and `downloads` is created.
|
2021-01-22 05:02:30 -08:00
|
|
|
verifier_setup: Option<Verifier>,
|
2021-01-22 02:41:05 -08:00
|
|
|
|
2021-01-22 05:02:30 -08:00
|
|
|
// Services and Data Stores
|
|
|
|
/// A shared list of peer addresses.
|
2021-01-22 02:41:05 -08:00
|
|
|
///
|
|
|
|
/// `None` until the network is set up.
|
2020-09-18 12:18:22 -07:00
|
|
|
address_book: Option<Arc<Mutex<zn::AddressBook>>>,
|
2021-01-13 02:26:50 -08:00
|
|
|
|
2021-01-22 02:41:05 -08:00
|
|
|
/// A stream that downloads and verifies gossipped blocks.
|
2021-01-13 02:26:50 -08:00
|
|
|
///
|
2021-01-22 02:41:05 -08:00
|
|
|
/// `None` until the network is set up.
|
|
|
|
downloads: Option<Pin<Box<Downloads<Timeout<Outbound>, Timeout<Verifier>, State>>>>,
|
2021-01-13 02:26:50 -08:00
|
|
|
|
|
|
|
/// A service that manages cached blockchain state.
|
2020-09-18 13:47:31 -07:00
|
|
|
state: State,
|
2020-09-18 12:18:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Inbound {
|
2020-11-19 09:55:06 -08:00
|
|
|
pub fn new(
|
|
|
|
network_setup: oneshot::Receiver<SetupData>,
|
|
|
|
state: State,
|
|
|
|
verifier: Verifier,
|
|
|
|
) -> Self {
|
2020-09-18 12:18:22 -07:00
|
|
|
Self {
|
|
|
|
network_setup: Some(network_setup),
|
2021-01-22 05:02:30 -08:00
|
|
|
verifier_setup: Some(verifier),
|
2020-09-18 12:18:22 -07:00
|
|
|
address_book: None,
|
2021-01-13 02:26:50 -08:00
|
|
|
downloads: None,
|
2020-09-18 13:47:31 -07:00
|
|
|
state,
|
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>> {
|
2020-09-19 23:50:51 -07:00
|
|
|
// Check whether the network setup is finished, but don't wait for it to
|
|
|
|
// 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.
|
|
|
|
if let Some(mut rx) = self.network_setup.take() {
|
|
|
|
use oneshot::error::TryRecvError;
|
|
|
|
match rx.try_recv() {
|
2020-09-18 12:18:22 -07:00
|
|
|
Ok((outbound, address_book)) => {
|
2021-01-22 05:02:30 -08:00
|
|
|
let verifier = self
|
|
|
|
.verifier_setup
|
|
|
|
.take()
|
|
|
|
.expect("unexpected missing verifier during inbound network setup");
|
|
|
|
|
2020-09-18 12:18:22 -07:00
|
|
|
self.address_book = Some(address_book);
|
2020-11-24 14:50:02 -08:00
|
|
|
self.downloads = Some(Box::pin(Downloads::new(
|
2021-01-13 01:39:33 -08:00
|
|
|
Timeout::new(outbound, BLOCK_DOWNLOAD_TIMEOUT),
|
2021-01-22 05:02:30 -08:00
|
|
|
Timeout::new(verifier, BLOCK_VERIFY_TIMEOUT),
|
2020-11-19 09:55:06 -08:00
|
|
|
self.state.clone(),
|
2020-11-24 14:50:02 -08:00
|
|
|
)));
|
2021-01-22 05:02:30 -08:00
|
|
|
|
2021-01-22 02:41:05 -08:00
|
|
|
self.network_setup = None;
|
2020-09-18 12:18:22 -07:00
|
|
|
}
|
|
|
|
Err(TryRecvError::Empty) => {
|
|
|
|
self.network_setup = Some(rx);
|
|
|
|
}
|
2020-09-19 23:50:51 -07:00
|
|
|
Err(e @ TryRecvError::Closed) => {
|
|
|
|
// In this case, report that the service failed, and put the
|
|
|
|
// failed oneshot back so we'll fail again in case
|
|
|
|
// poll_ready is called after failure.
|
|
|
|
self.network_setup = Some(rx);
|
|
|
|
return Poll::Ready(Err(e.into()));
|
2020-09-18 14:11:29 -07:00
|
|
|
}
|
2020-09-19 23:50:51 -07:00
|
|
|
};
|
|
|
|
}
|
2020-11-19 09:55:06 -08:00
|
|
|
|
2021-01-22 02:41:05 -08:00
|
|
|
// Clean up completed download tasks, ignoring their results
|
2020-11-24 13:21:11 -08:00
|
|
|
if let Some(downloads) = self.downloads.as_mut() {
|
2020-11-24 14:50:02 -08:00
|
|
|
while let Poll::Ready(Some(_)) = downloads.as_mut().poll_next(cx) {}
|
2020-11-24 13:21:11 -08:00
|
|
|
}
|
2020-11-19 09:55:06 -08:00
|
|
|
|
2020-09-19 23:50:51 -07:00
|
|
|
// Now report readiness based on readiness of the inner services, if they're available.
|
2021-01-22 02:41:05 -08:00
|
|
|
//
|
|
|
|
// TODO: do we want to propagate backpressure from the download queue or its outbound network here?
|
|
|
|
// 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.
|
|
|
|
match self.state.poll_ready(cx) {
|
|
|
|
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
Poll::Ready(Ok(())) => Poll::Ready(Ok(())),
|
2020-09-18 12:18:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
match req {
|
2020-09-19 23:50:51 -07:00
|
|
|
zn::Request::Peers => match self.address_book.as_ref() {
|
|
|
|
Some(addrs) => {
|
|
|
|
// We could truncate the list to try to not reveal our entire
|
|
|
|
// peer set. But because we don't monitor repeated requests,
|
|
|
|
// this wouldn't actually achieve anything, because a crawler
|
|
|
|
// could just repeatedly query it.
|
|
|
|
let mut peers = addrs.lock().unwrap().sanitized();
|
|
|
|
const MAX_ADDR: usize = 1000; // bitcoin protocol constant
|
|
|
|
peers.truncate(MAX_ADDR);
|
|
|
|
async { Ok(zn::Response::Peers(peers)) }.boxed()
|
|
|
|
}
|
|
|
|
None => async { Err("not ready to serve addresses".into()) }.boxed(),
|
|
|
|
},
|
2020-09-18 13:47:31 -07:00
|
|
|
zn::Request::BlocksByHash(hashes) => {
|
|
|
|
let state = self.state.clone();
|
|
|
|
let requests = futures::stream::iter(
|
|
|
|
hashes
|
|
|
|
.into_iter()
|
|
|
|
.map(|hash| zs::Request::Block(hash.into())),
|
|
|
|
);
|
|
|
|
|
|
|
|
state
|
|
|
|
.call_all(requests)
|
|
|
|
.try_filter_map(|rsp| {
|
|
|
|
futures::future::ready(match rsp {
|
|
|
|
zs::Response::Block(Some(block)) => Ok(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
|
|
|
|
zs::Response::Block(None) => Ok(None),
|
2020-09-18 13:47:31 -07:00
|
|
|
_ => unreachable!("wrong response from state"),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.try_collect::<Vec<_>>()
|
|
|
|
.map_ok(zn::Response::Blocks)
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
zn::Request::TransactionsByHash(_transactions) => {
|
2021-01-03 19:25:35 -08:00
|
|
|
// `zcashd` returns a list of found transactions, followed by a
|
|
|
|
// `NotFound` message if any transactions are missing. `zcashd`
|
|
|
|
// says that Simplified Payment Verification (SPV) clients rely on
|
|
|
|
// this behaviour - are there any of them on the Zcash network?
|
|
|
|
// https://github.com/zcash/zcash/blob/e7b425298f6d9a54810cb7183f00be547e4d9415/src/main.cpp#L5632
|
|
|
|
// We'll implement this request once we have a mempool:
|
|
|
|
// https://en.bitcoin.it/wiki/Protocol_documentation#getdata
|
2020-09-18 13:47:31 -07:00
|
|
|
debug!("ignoring unimplemented request");
|
|
|
|
async { Ok(zn::Response::Nil) }.boxed()
|
|
|
|
}
|
2020-11-30 13:30:37 -08:00
|
|
|
zn::Request::FindBlocks { known_blocks, stop } => {
|
|
|
|
let request = zs::Request::FindBlockHashes { known_blocks, stop };
|
|
|
|
self.state.call(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 };
|
|
|
|
self.state.call(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
|
|
|
}
|
2020-09-18 13:47:31 -07:00
|
|
|
zn::Request::PushTransaction(_transaction) => {
|
|
|
|
debug!("ignoring unimplemented request");
|
|
|
|
async { Ok(zn::Response::Nil) }.boxed()
|
|
|
|
}
|
|
|
|
zn::Request::AdvertiseTransactions(_transactions) => {
|
2020-09-18 12:18:22 -07:00
|
|
|
debug!("ignoring unimplemented request");
|
|
|
|
async { Ok(zn::Response::Nil) }.boxed()
|
|
|
|
}
|
2020-11-19 09:55:06 -08:00
|
|
|
zn::Request::AdvertiseBlock(hash) => {
|
2020-11-24 14:20:34 -08:00
|
|
|
if let Some(downloads) = self.downloads.as_mut() {
|
|
|
|
downloads.download_and_verify(hash);
|
2020-11-19 09:55:06 -08:00
|
|
|
}
|
2020-11-24 14:20:34 -08:00
|
|
|
async { Ok(zn::Response::Nil) }.boxed()
|
2020-09-18 13:47:31 -07:00
|
|
|
}
|
2020-09-18 22:16:59 -07:00
|
|
|
zn::Request::MempoolTransactions => {
|
|
|
|
debug!("ignoring unimplemented request");
|
|
|
|
async { Ok(zn::Response::Nil) }.boxed()
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|