2019-10-24 13:28:42 -07:00
|
|
|
//! `seed` subcommand - test stub for talking to zcashd
|
|
|
|
|
2019-11-05 16:40:18 -08:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
|
|
|
|
2019-10-24 13:28:42 -07:00
|
|
|
use crate::{config::ZebradConfig, prelude::*};
|
|
|
|
|
|
|
|
use abscissa_core::{config, Command, FrameworkError, Options, Runnable};
|
|
|
|
|
|
|
|
/// `seed` subcommand
|
|
|
|
///
|
|
|
|
/// A DNS seeder command to spider and collect as many valid peer
|
|
|
|
/// addresses as we can.
|
|
|
|
#[derive(Command, Debug, Options)]
|
|
|
|
pub struct SeedCmd {
|
|
|
|
/// Filter strings
|
|
|
|
#[options(free)]
|
|
|
|
filters: Vec<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl config::Override<ZebradConfig> for SeedCmd {
|
|
|
|
// Process the given command line options, overriding settings
|
|
|
|
// from a configuration file using explicit flags taken from
|
|
|
|
// command-line arguments.
|
|
|
|
fn override_config(&self, mut config: ZebradConfig) -> Result<ZebradConfig, FrameworkError> {
|
|
|
|
if !self.filters.is_empty() {
|
|
|
|
config.tracing.filter = self.filters.join(",");
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Runnable for SeedCmd {
|
|
|
|
/// Start the application.
|
|
|
|
fn run(&self) {
|
|
|
|
use crate::components::tokio::TokioComponent;
|
|
|
|
|
|
|
|
let wait = tokio::future::pending::<()>();
|
|
|
|
// Combine the seed future with an infinite wait
|
|
|
|
// so that the program has to be explicitly killed and
|
|
|
|
// won't die before all tracing messages are written.
|
|
|
|
let fut = futures::future::join(
|
|
|
|
async {
|
|
|
|
match self.seed().await {
|
|
|
|
Ok(()) => {}
|
|
|
|
Err(e) => {
|
|
|
|
// Print any error that occurs.
|
|
|
|
error!(?e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
wait,
|
|
|
|
);
|
|
|
|
|
|
|
|
let _ = app_reader()
|
|
|
|
.state()
|
|
|
|
.components
|
|
|
|
.get_downcast_ref::<TokioComponent>()
|
|
|
|
.expect("TokioComponent should be available")
|
|
|
|
.rt
|
|
|
|
.block_on(fut);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SeedCmd {
|
|
|
|
async fn seed(&self) -> Result<(), failure::Error> {
|
|
|
|
use failure::Error;
|
2019-11-05 16:40:18 -08:00
|
|
|
use futures::stream::StreamExt;
|
|
|
|
use tower::{buffer::Buffer, Service, ServiceExt};
|
2019-10-24 13:28:42 -07:00
|
|
|
use zebra_network::{AddressBook, Request, Response};
|
|
|
|
|
|
|
|
info!("begin tower-based peer handling test stub");
|
|
|
|
|
2019-11-05 16:40:18 -08:00
|
|
|
struct SeedService {
|
|
|
|
address_book: Option<AddressBook>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SeedService {
|
|
|
|
fn set_address_book(&mut self, address_book: AddressBook) {
|
|
|
|
self.address_book = Some(address_book);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Service<Request> for SeedService {
|
|
|
|
type Response = Response;
|
|
|
|
type Error = Error;
|
|
|
|
type Future =
|
|
|
|
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
|
|
|
Ok(()).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: Request) -> Self::Future {
|
|
|
|
let response = match req {
|
|
|
|
Request::GetPeers => match &self.address_book {
|
|
|
|
Some(address_book) => Ok::<Response, failure::Error>(Response::Peers(
|
|
|
|
address_book.peers().collect(),
|
|
|
|
)),
|
|
|
|
_ => Ok::<Response, failure::Error>(Response::Ok),
|
|
|
|
},
|
|
|
|
_ => Ok::<Response, failure::Error>(Response::Ok),
|
|
|
|
};
|
|
|
|
|
|
|
|
return Box::pin(futures::future::ready(response));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let node = Buffer::new(SeedService { address_book: None }, 1);
|
2019-10-24 13:28:42 -07:00
|
|
|
|
|
|
|
let config = app_config().network.clone();
|
|
|
|
|
|
|
|
// XXX How do I create a service above that answers questions
|
|
|
|
// about this specific address book?
|
|
|
|
let (mut peer_set, address_book) = zebra_network::init(config, node).await;
|
|
|
|
|
|
|
|
// XXX Do not tell our DNS seed queries about gossiped addrs
|
|
|
|
// that we have not connected to before?
|
|
|
|
info!("waiting for peer_set ready");
|
|
|
|
peer_set.ready().await.map_err(Error::from_boxed_compat)?;
|
|
|
|
|
|
|
|
info!("peer_set became ready");
|
|
|
|
|
2019-11-05 16:40:18 -08:00
|
|
|
let eternity = tokio::future::pending::<()>();
|
|
|
|
eternity.await;
|
2019-10-24 13:28:42 -07:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|