use std::{
collections::HashSet,
future::Future,
pin::Pin,
sync::Arc,
task::{Context, Poll},
time::Duration,
};
use futures::{
future::{FutureExt, TryFutureExt},
stream::Stream,
};
use tokio::sync::oneshot::{self, error::TryRecvError};
use tower::{buffer::Buffer, timeout::Timeout, util::BoxService, Service, ServiceExt};
use zebra_network::{self as zn, PeerSocketAddr};
use zebra_state::{self as zs};
use zebra_chain::{
block::{self, Block},
serialization::ZcashSerialize,
transaction::UnminedTxId,
};
use zebra_consensus::{router::RouterError, VerifyBlockError};
use zebra_network::{AddressBook, InventoryResponse};
use zebra_node_services::mempool;
use crate::BoxError;
use super::sync::{BLOCK_DOWNLOAD_TIMEOUT, BLOCK_VERIFY_TIMEOUT};
use InventoryResponse::*;
mod cached_peer_addr_response;
pub(crate) mod downloads;
use cached_peer_addr_response::CachedPeerAddrResponse;
#[cfg(test)]
mod tests;
use downloads::Downloads as BlockDownloads;
pub const MAX_INBOUND_RESPONSE_TIME: Duration = Duration::from_secs(5);
pub const GETDATA_SENT_BYTES_LIMIT: usize = 1_000_000;
pub const GETDATA_MAX_BLOCK_COUNT: usize = 16;
type BlockDownloadPeerSet =
Buffer<BoxService<zn::Request, zn::Response, zn::BoxError>, zn::Request>;
type State = Buffer<BoxService<zs::Request, zs::Response, zs::BoxError>, zs::Request>;
type Mempool = Buffer<BoxService<mempool::Request, mempool::Response, BoxError>, mempool::Request>;
type SemanticBlockVerifier = Buffer<
BoxService<zebra_consensus::Request, block::Hash, RouterError>,
zebra_consensus::Request,
>;
type GossipedBlockDownloads =
BlockDownloads<Timeout<BlockDownloadPeerSet>, Timeout<SemanticBlockVerifier>, State>;
pub struct InboundSetupData {
pub address_book: Arc<std::sync::Mutex<AddressBook>>,
pub block_download_peer_set: BlockDownloadPeerSet,
pub block_verifier: SemanticBlockVerifier,
pub mempool: Mempool,
pub state: State,
pub latest_chain_tip: zs::LatestChainTip,
pub misbehavior_sender: tokio::sync::mpsc::Sender<(PeerSocketAddr, u32)>,
}
pub enum Setup {
Pending {
full_verify_concurrency_limit: usize,
setup: oneshot::Receiver<InboundSetupData>,
},
Initialized {
cached_peer_addr_response: CachedPeerAddrResponse,
block_downloads: Pin<Box<GossipedBlockDownloads>>,
mempool: Mempool,
state: State,
misbehavior_sender: tokio::sync::mpsc::Sender<(PeerSocketAddr, u32)>,
},
FailedInit,
FailedRecv {
error: SharedRecvError,
},
}
#[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))
}
}
pub struct Inbound {
setup: Setup,
}
impl Inbound {
pub fn new(
full_verify_concurrency_limit: usize,
setup: oneshot::Receiver<InboundSetupData>,
) -> Inbound {
Inbound {
setup: Setup::Pending {
full_verify_concurrency_limit,
setup,
},
}
}
fn take_setup(&mut self) -> Setup {
let mut setup = Setup::FailedInit;
std::mem::swap(&mut self.setup, &mut setup);
setup
}
}
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>>;
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let result;
self.setup = match self.take_setup() {
Setup::Pending {
full_verify_concurrency_limit,
mut setup,
} => match setup.try_recv() {
Ok(setup_data) => {
let InboundSetupData {
address_book,
block_download_peer_set,
block_verifier,
mempool,
state,
latest_chain_tip,
misbehavior_sender,
} = setup_data;
let cached_peer_addr_response = CachedPeerAddrResponse::new(address_book);
let block_downloads = Box::pin(BlockDownloads::new(
full_verify_concurrency_limit,
Timeout::new(block_download_peer_set, BLOCK_DOWNLOAD_TIMEOUT),
Timeout::new(block_verifier, BLOCK_VERIFY_TIMEOUT),
state.clone(),
latest_chain_tip,
));
result = Ok(());
Setup::Initialized {
cached_peer_addr_response,
block_downloads,
mempool,
state,
misbehavior_sender,
}
}
Err(TryRecvError::Empty) => {
result = Ok(());
Setup::Pending {
full_verify_concurrency_limit,
setup,
}
}
Err(error @ TryRecvError::Closed) => {
error!(?error, "inbound setup failed");
let error: SharedRecvError = error.into();
result = Err(error.clone().into());
Setup::FailedRecv { error }
}
},
Setup::FailedInit => unreachable!("incomplete previous Inbound initialization"),
Setup::FailedRecv { error } => {
result = Err(error.clone().into());
Setup::FailedRecv { error }
}
Setup::Initialized {
cached_peer_addr_response,
mut block_downloads,
mempool,
state,
misbehavior_sender,
} => {
while let Poll::Ready(Some(result)) = block_downloads.as_mut().poll_next(cx) {
let Err((err, Some(advertiser_addr))) = result else {
continue;
};
let Ok(err) = err.downcast::<VerifyBlockError>() else {
continue;
};
if err.misbehavior_score() != 0 {
let _ =
misbehavior_sender.try_send((advertiser_addr, err.misbehavior_score()));
}
}
result = Ok(());
Setup::Initialized {
cached_peer_addr_response,
block_downloads,
mempool,
state,
misbehavior_sender,
}
}
};
if matches!(self.setup, Setup::FailedInit) {
unreachable!("incomplete Inbound initialization after poll_ready state handling");
}
Poll::Ready(result)
}
#[instrument(name = "inbound", skip(self, req))]
fn call(&mut self, req: zn::Request) -> Self::Future {
let (cached_peer_addr_response, block_downloads, mempool, state) = match &mut self.setup {
Setup::Initialized {
cached_peer_addr_response,
block_downloads,
mempool,
state,
misbehavior_sender: _,
} => (cached_peer_addr_response, block_downloads, mempool, state),
_ => {
debug!("ignoring request from remote peer during setup");
return async { Ok(zn::Response::Nil) }.boxed();
}
};
match req {
zn::Request::Peers => {
cached_peer_addr_response.try_refresh();
let response = cached_peer_addr_response.value();
async move {
Ok(response)
}.boxed()
}
zn::Request::BlocksByHash(hashes) => {
if hashes.is_empty() {
return async { Ok(zn::Response::Nil) }.boxed();
}
let state = state.clone();
async move {
let mut blocks: Vec<InventoryResponse<(Arc<Block>, Option<PeerSocketAddr>), block::Hash>> = Vec::new();
let mut total_size = 0;
for &hash in hashes.iter().take(GETDATA_MAX_BLOCK_COUNT) {
if total_size >= GETDATA_SENT_BYTES_LIMIT {
break;
}
let response = state.clone().ready().await?.call(zs::Request::Block(hash.into())).await?;
match response {
zs::Response::Block(Some(block)) => {
total_size += block.zcash_serialized_size();
blocks.push(Available((block, None)))
},
zs::Response::Block(None) => blocks.push(Missing(hash)),
_ => unreachable!("wrong response from state"),
}
}
Ok(zn::Response::Blocks(blocks))
}.boxed()
}
zn::Request::TransactionsById(req_tx_ids) => {
if req_tx_ids.is_empty() {
return async { Ok(zn::Response::Nil) }.boxed();
}
let request = mempool::Request::TransactionsById(req_tx_ids.clone());
mempool.clone().oneshot(request).map_ok(move |resp| {
let mut total_size = 0;
let transactions = match resp {
mempool::Response::Transactions(transactions) => transactions,
_ => unreachable!("Mempool component should always respond to a `TransactionsById` request with a `Transactions` response"),
};
let available_tx_ids: HashSet<UnminedTxId> = transactions.iter().map(|tx| tx.id).collect();
let missing = req_tx_ids.into_iter().filter(|tx_id| !available_tx_ids.contains(tx_id)).map(Missing);
let available = transactions.into_iter().take_while(|tx| {
let within_limit = total_size < GETDATA_SENT_BYTES_LIMIT;
total_size += tx.size;
within_limit
}).map(|tx| Available((tx, None)));
zn::Response::Transactions(available.chain(missing).collect())
}).boxed()
}
zn::Request::FindBlocks { known_blocks, stop } => {
let request = zs::Request::FindBlockHashes { known_blocks, stop };
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()
}
zn::Request::FindHeaders { known_blocks, stop } => {
let request = zs::Request::FindBlockHeaders { known_blocks, stop };
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()
}
zn::Request::PushTransaction(transaction) => {
mempool
.clone()
.oneshot(mempool::Request::Queue(vec![transaction.into()]))
.map_ok(|_resp| zn::Response::Nil)
.boxed()
}
zn::Request::AdvertiseTransactionIds(transactions) => {
let transactions = transactions.into_iter().map(Into::into).collect();
mempool
.clone()
.oneshot(mempool::Request::Queue(transactions))
.map_ok(|_resp| zn::Response::Nil)
.boxed()
}
zn::Request::AdvertiseBlock(hash) => {
block_downloads.download_and_verify(hash);
async { Ok(zn::Response::Nil) }.boxed()
}
zn::Request::MempoolTransactionIds => {
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"),
})
.boxed()
}
zn::Request::Ping(_) => {
unreachable!("ping requests are handled internally");
}
zn::Request::AdvertiseBlockToAll(_) => unreachable!("should always be decoded as `AdvertiseBlock` request")
}
}
}