From 299afe13dfdd450ff5d33554cc43ffe6ea466c95 Mon Sep 17 00:00:00 2001 From: Henry de Valence Date: Tue, 11 Aug 2020 13:07:44 -0700 Subject: [PATCH] zebra-network tweaks. (#877) * network: move gossiped peer selection logic into address book. * network: return BoxService from init. * zebrad: add note on why we truncate thegossiped peer list Co-authored-by: Jane Lusby * Remove unused .rustfmt.toml Many of these options are never actually loaded by our CI because of a channel mismatch, where they're not applied on stable but only on nightly (see the logs from a rustfmt job). This means that we can get different settings when running `cargo fmt` on the nightly and stable channels, which was causing a CI failure on this PR. Reverting back to the default rustfmt settings avoids this problem and keeps us in line with upstream rustfmt. There's no loss to us since we were using the defaults anyways. Co-authored-by: Jane Lusby --- .rustfmt.toml | 66 ------------------------ zebra-network/src/address_book.rs | 8 +++ zebra-network/src/peer_set/initialize.rs | 14 ++--- zebrad/src/commands/seed.rs | 17 ++---- 4 files changed, 15 insertions(+), 90 deletions(-) delete mode 100644 .rustfmt.toml diff --git a/.rustfmt.toml b/.rustfmt.toml deleted file mode 100644 index ab694ba4..00000000 --- a/.rustfmt.toml +++ /dev/null @@ -1,66 +0,0 @@ -max_width = 100 -hard_tabs = false -tab_spaces = 4 -newline_style = "Auto" -use_small_heuristics = "Default" -indent_style = "Block" -wrap_comments = false -format_code_in_doc_comments = false -comment_width = 80 -normalize_comments = false -normalize_doc_attributes = false -#license_template_path = "" -format_strings = false -format_macro_matchers = false -format_macro_bodies = true -empty_item_single_line = true -struct_lit_single_line = true -fn_single_line = false -where_single_line = false -imports_indent = "Block" -imports_layout = "Mixed" -merge_imports = false -reorder_imports = true -reorder_modules = true -reorder_impl_items = false -type_punctuation_density = "Wide" -space_before_colon = false -space_after_colon = true -spaces_around_ranges = false -binop_separator = "Front" -remove_nested_parens = true -combine_control_expr = true -overflow_delimited_expr = false -struct_field_align_threshold = 0 -enum_discrim_align_threshold = 0 -match_arm_blocks = true -force_multiline_blocks = false -fn_args_layout = "Tall" -brace_style = "SameLineWhere" -control_brace_style = "AlwaysSameLine" -trailing_semicolon = true -trailing_comma = "Vertical" -match_block_trailing_comma = false -blank_lines_upper_bound = 1 -blank_lines_lower_bound = 0 -edition = "2018" -version = "One" -inline_attribute_width = 0 -merge_derives = true -use_try_shorthand = false -use_field_init_shorthand = false -force_explicit_abi = true -condense_wildcard_suffixes = false -color = "Auto" -#required_version = "1.3.0" -unstable_features = false -disable_all_formatting = false -skip_children = false -hide_parse_errors = false -error_on_line_overflow = false -error_on_unformatted = false -report_todo = "Never" -report_fixme = "Never" -ignore = [] -emit_mode = "Files" -make_backup = false diff --git a/zebra-network/src/address_book.rs b/zebra-network/src/address_book.rs index f4ed7227..0e2c10ea 100644 --- a/zebra-network/src/address_book.rs +++ b/zebra-network/src/address_book.rs @@ -35,6 +35,14 @@ impl AddressBook { } } + /// Get the contents of `self` in random order with sanitized timestamps. + pub fn sanitized(&self) -> Vec { + use rand::seq::SliceRandom; + let mut peers = self.peers().map(MetaAddr::sanitize).collect::>(); + peers.shuffle(&mut rand::thread_rng()); + peers + } + /// Check consistency of the address book invariants or panic, doing work /// quadratic in the address book size. #[cfg(test)] diff --git a/zebra-network/src/peer_set/initialize.rs b/zebra-network/src/peer_set/initialize.rs index 923e7158..3d01c43b 100644 --- a/zebra-network/src/peer_set/initialize.rs +++ b/zebra-network/src/peer_set/initialize.rs @@ -10,7 +10,7 @@ use std::{ use futures::{ channel::mpsc, - future::{self, Future, FutureExt}, + future::{self, FutureExt}, sink::SinkExt, stream::{FuturesUnordered, StreamExt}, }; @@ -19,6 +19,7 @@ use tower::{ buffer::Buffer, discover::{Change, ServiceStream}, layer::Layer, + util::BoxService, Service, ServiceExt, }; use tower_load::{peak_ewma::PeakEwmaDiscover, NoInstrument}; @@ -40,14 +41,7 @@ pub async fn init( config: Config, inbound_service: S, ) -> ( - impl Service< - Request, - Response = Response, - Error = BoxedStdError, - Future = impl Future> + Send, - > + Send - + Clone - + 'static, + Buffer, Request>, Arc>, ) where @@ -91,7 +85,7 @@ where demand_tx.clone(), handle_rx, ); - let peer_set = Buffer::new(peer_set, constants::PEERSET_BUFFER_SIZE); + let peer_set = Buffer::new(BoxService::new(peer_set), constants::PEERSET_BUFFER_SIZE); // Connect the tx end to the 3 peer sources: diff --git a/zebrad/src/commands/seed.rs b/zebrad/src/commands/seed.rs index 7a460625..10154653 100644 --- a/zebrad/src/commands/seed.rs +++ b/zebrad/src/commands/seed.rs @@ -72,21 +72,10 @@ impl Service for SeedService { let response = match req { Request::Peers => { - // Collect a list of known peers from the address book - // and sanitize their timestamps. - let mut peers = address_book - .lock() - .unwrap() - .peers() - .map(|addr| addr.sanitize()) - .collect::>(); - // The peers are still ordered by recency, so shuffle them. - use rand::seq::SliceRandom; - peers.shuffle(&mut rand::thread_rng()); - // Finally, truncate the list so that we do not trivially - // reveal our entire peer set. + debug!("selecting peers to gossip"); + let mut peers = address_book.lock().unwrap().sanitized(); + // truncate the list so that we do not trivially reveal our entire peer set. peers.truncate(50); - debug!(peers.len = peers.len()); Ok(Response::Peers(peers)) } _ => {