Add a `Config` struct to zebra-network.

This struct is pulled into the main abscissa config as a subsection.
This commit is contained in:
Henry de Valence 2019-10-08 13:57:24 -07:00 committed by Deirdre Connolly
parent 1266653be2
commit fb2b502eb9
6 changed files with 47 additions and 19 deletions

View File

@ -14,6 +14,7 @@ rand = "0.7"
byteorder = "1.3"
chrono = "0.4"
failure = "0.1"
serde = { version = "1", features = ["serde_derive"] }
tokio = "=0.2.0-alpha.6"
tower = { git = "https://github.com/tower-rs/tower", branch = "v0.3.x" }
tracing = { git = "https://github.com/tokio-rs/tracing" }

View File

@ -0,0 +1,15 @@
/// Configuration for networking code.
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct Config {
/// The user-agent to advertise.
pub user_agent: String,
}
impl Default for Config {
fn default() -> Config {
Config {
user_agent: crate::constants::USER_AGENT.to_owned(),
}
}
}

View File

@ -2,6 +2,8 @@
#![deny(missing_docs)]
#[macro_use]
extern crate serde;
#[macro_use]
extern crate failure;
#[macro_use]
@ -19,6 +21,9 @@ pub(crate) type BoxedStdError = Box<dyn std::error::Error + Send + Sync + 'stati
mod network;
pub use network::Network;
mod config;
pub use config::Config;
pub mod protocol;
// XXX revisit privacy once we finish encapsulation.

View File

@ -18,7 +18,7 @@ use crate::{
constants,
protocol::{codec::*, internal::*, message::*, types::*},
timestamp_collector::{PeerLastSeen, TimestampCollector},
BoxedStdError, Network,
BoxedStdError, Config, Network,
};
use super::{
@ -28,6 +28,7 @@ use super::{
/// A [`Service`] that connects to a remote peer and constructs a client/server pair.
pub struct PeerConnector<S> {
config: Config,
network: Network,
internal_service: S,
sender: mpsc::Sender<PeerLastSeen>,
@ -39,10 +40,21 @@ where
S::Future: Send,
S::Error: Into<BoxedStdError>,
{
/// XXX replace with a builder
pub fn new(network: Network, internal_service: S, collector: &TimestampCollector) -> Self {
/// Construct a new `PeerConnector`.
pub fn new(
config: Config,
network: Network,
internal_service: S,
collector: &TimestampCollector,
) -> Self {
// XXX this function has too many parameters, but it's not clear how to
// do a nice builder as all fields are mandatory. Could have Builder1,
// Builder2, ..., with Builder1::with_config() -> Builder2;
// Builder2::with_internal_service() -> ... or use Options in a single
// Builder type or use the derive_builder crate.
let sender = collector.sender_handle();
PeerConnector {
config,
network,
internal_service,
sender,
@ -74,6 +86,7 @@ where
let network = self.network.clone();
let internal_service = self.internal_service.clone();
let sender = self.sender.clone();
let user_agent = self.config.user_agent.clone();
let fut = async move {
info!("beginning connection");
@ -93,7 +106,9 @@ where
"127.0.0.1:9000".parse().unwrap(),
),
nonce: Nonce::default(),
user_agent: "Zebra Peer".to_owned(),
user_agent,
// XXX eventually the `PeerConnector` will need to have a handle
// for a service that gets the current block height.
start_height: BlockHeight(0),
relay: false,
};

View File

@ -72,9 +72,9 @@ impl ConnectCmd {
1,
);
let config = app_config().network.clone();
let collector = TimestampCollector::new();
let mut pc = PeerConnector::new(Network::Mainnet, node, &collector);
let mut pc = PeerConnector::new(config, Network::Mainnet, node, &collector);
// no need to call ready because pc is always ready
let mut client = pc.call(self.addr.clone()).await?;

View File

@ -7,24 +7,16 @@
use abscissa_core::Config;
use serde::{Deserialize, Serialize};
use zebra_network::Config as NetworkSection;
/// Zebrad Configuration
#[derive(Clone, Config, Debug, Deserialize, Serialize)]
#[derive(Clone, Config, Default, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ZebradConfig {
/// Tracing configuration
pub tracing: TracingSection,
}
/// Default configuration settings.
///
/// Note: if your needs are as simple as below, you can
/// use `#[derive(Default)]` on ZebradConfig instead.
impl Default for ZebradConfig {
fn default() -> Self {
Self {
tracing: TracingSection::default(),
}
}
/// Networking configuration,
pub network: NetworkSection,
}
/// Tracing configuration section.