Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iqlusioninc abscissa/v0.5 #169

Merged
merged 4 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,302 changes: 631 additions & 671 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions zebrad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ edition = "2018"
[dependencies]
rand = "0.7"
chrono = "0.4"
abscissa_core = "0.3.0"
failure = "0.1"
gumdrop = "0.6"
lazy_static = "1"
abscissa_core = "0.5"
gumdrop = "0.7"
serde = { version = "1", features = ["serde_derive"] }
toml = "0.5"
thiserror = "1"

tokio = { version = "0.2", features = ["time", "rt-threaded", "stream"] }
futures = "0.3"
Expand All @@ -30,6 +29,6 @@ tower = "0.3"
zebra-chain = { path = "../zebra-chain" }
zebra-network = { path = "../zebra-network" }

[dev-dependencies.abscissa_core]
version = "0.3.0"
features = ["testing"]
[dev-dependencies]
abscissa_core = { version = "0.5", features = ["testing"] }
once_cell = "1.2"
30 changes: 7 additions & 23 deletions zebrad/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

use crate::{commands::ZebradCmd, config::ZebradConfig};
use abscissa_core::{
application, config, logging, Application, Component, EntryPoint, FrameworkError, StandardPaths,
application::{self, AppCell},
config, trace, Application, EntryPoint, FrameworkError, StandardPaths,
};
use lazy_static::lazy_static;

lazy_static! {
/// Application state
pub static ref APPLICATION: application::Lock<ZebradApp> = application::Lock::default();
}
/// Application state
pub static APPLICATION: AppCell<ZebradApp> = AppCell::new();

/// Obtain a read-only (multi-reader) lock on the application state.
///
Expand Down Expand Up @@ -78,20 +76,6 @@ impl Application for ZebradApp {
&mut self.state
}

/// Override the provided impl to skip the default logging component.
///
/// We want to use tracing as the log subscriber in our tracing component,
/// so only initialize the abscissa Terminal component.
fn framework_components(
&mut self,
_command: &Self::Cmd,
) -> Result<Vec<Box<dyn Component<Self>>>, FrameworkError> {
use abscissa_core::terminal::{component::Terminal, ColorChoice};
// XXX abscissa uses self.term_colors(command), check if we should match
let terminal = Terminal::new(ColorChoice::Auto);
Ok(vec![Box::new(terminal)])
}

/// Register all components used by this application.
///
/// If you would like to add additional components to your application
Expand Down Expand Up @@ -120,11 +104,11 @@ impl Application for ZebradApp {
}

/// Get logging configuration from command-line options
fn logging_config(&self, command: &EntryPoint<ZebradCmd>) -> logging::Config {
fn tracing_config(&self, command: &EntryPoint<ZebradCmd>) -> trace::Config {
if command.verbose {
logging::Config::verbose()
trace::Config::verbose()
} else {
logging::Config::default()
trace::Config::default()
}
}
}
35 changes: 23 additions & 12 deletions zebrad/src/commands/connect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
//! `connect` subcommand - test stub for talking to zcashd

use crate::prelude::*;
use crate::{
error::{Error, ErrorKind},
prelude::*,
};

use abscissa_core::{Command, Options, Runnable};

Expand All @@ -23,29 +26,32 @@ impl Runnable for ConnectCmd {
info!(connect.addr = ?self.addr);

use crate::components::tokio::TokioComponent;
let _ = app_writer()
let rt = app_writer()
.state_mut()
.components
.get_downcast_mut::<TokioComponent>()
.expect("TokioComponent should be available")
.rt
.block_on(self.connect());
.take();

rt.expect("runtime should not already be taken")
.block_on(self.connect())
// Surface any error that occurred executing the future.
.unwrap();
}
}

impl ConnectCmd {
async fn connect(&self) -> Result<(), failure::Error> {
async fn connect(&self) -> Result<(), Error> {
use zebra_network::{AddressBook, Request, Response};

info!("begin tower-based peer handling test stub");
use tower::{buffer::Buffer, service_fn, Service, ServiceExt};

let node = Buffer::new(
service_fn(|req| {
async move {
info!(?req);
Ok::<Response, failure::Error>(Response::Ok)
}
service_fn(|req| async move {
info!(?req);
Ok::<Response, Error>(Response::Ok)
}),
1,
);
Expand All @@ -59,17 +65,22 @@ impl ConnectCmd {
let (mut peer_set, _address_book) = zebra_network::init(config, node).await;

info!("waiting for peer_set ready");
peer_set.ready().await.map_err(Error::from_boxed_compat)?;
peer_set
.ready()
.await
.map_err(|e| Error::from(ErrorKind::Io.context(e)))?;

info!("peer_set became ready, constructing addr requests");

use failure::Error;
use futures::stream::{FuturesUnordered, StreamExt};

let mut addr_reqs = FuturesUnordered::new();
for i in 0..10usize {
info!(i, "awaiting peer_set ready");
peer_set.ready().await.map_err(Error::from_boxed_compat)?;
peer_set
.ready()
.await
.map_err(|e| Error::from(ErrorKind::Io.context(e)))?;
info!(i, "calling peer_set");
addr_reqs.push(peer_set.call(Request::GetPeers));
}
Expand Down
23 changes: 16 additions & 7 deletions zebrad/src/commands/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use tower::{buffer::Buffer, Service, ServiceExt};

use zebra_network::{AddressBook, BoxedStdError, Request, Response};

use crate::prelude::*;
use crate::{
error::{Error, ErrorKind},
prelude::*,
};

/// Whether our `SeedService` is poll_ready or not.
#[derive(Debug)]
Expand Down Expand Up @@ -109,20 +112,23 @@ impl Runnable for SeedCmd {
fn run(&self) {
use crate::components::tokio::TokioComponent;

let _ = app_writer()
let rt = app_writer()
.state_mut()
.components
.get_downcast_mut::<TokioComponent>()
.expect("TokioComponent should be available")
.rt
.block_on(self.seed());
.take();

rt.expect("runtime should not already be taken")
.block_on(self.seed())
// Surface any error that occurred executing the future.
.unwrap();
}
}

impl SeedCmd {
async fn seed(&self) -> Result<(), failure::Error> {
use failure::Error;

async fn seed(&self) -> Result<(), Error> {
info!("begin tower-based peer handling test stub");

let (addressbook_tx, addressbook_rx) = oneshot::channel();
Expand All @@ -138,7 +144,10 @@ impl SeedCmd {
let _ = addressbook_tx.send(address_book);

info!("waiting for peer_set ready");
peer_set.ready().await.map_err(Error::from_boxed_compat)?;
peer_set
.ready()
.await
.map_err(|e| Error::from(ErrorKind::Io.context(e)))?;

info!("peer_set became ready");

Expand Down
5 changes: 4 additions & 1 deletion zebrad/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ impl Runnable for StartCmd {

use crate::components::tokio::TokioComponent;

app_writer()
let rt = app_writer()
.state_mut()
.components
.get_downcast_mut::<TokioComponent>()
.expect("TokioComponent should be available")
.rt
.take();

rt.expect("runtime should not already be taken")
.block_on(future::pending::<()>());
}
}
Expand Down
10 changes: 8 additions & 2 deletions zebrad/src/components/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ use abscissa_core::{Component, FrameworkError};
use tokio::runtime::Runtime;

/// An Abscissa component which owns a Tokio runtime.
///
/// The runtime is stored as an `Option` so that when it's time to enter an async
/// context by calling `block_on` with a "root future", the runtime can be taken
/// independently of Abscissa's component locking system. Otherwise whatever
/// calls `block_on` holds an application lock for the entire lifetime of the
/// async context.
#[derive(Component, Debug)]
pub struct TokioComponent {
pub rt: Runtime,
pub rt: Option<Runtime>,
}

impl TokioComponent {
pub fn new() -> Result<Self, FrameworkError> {
Ok(Self {
rt: Runtime::new().unwrap(),
rt: Some(Runtime::new().unwrap()),
})
}
}
Loading