From 686c2482358e03b94779c0cde9a61af2b83b6575 Mon Sep 17 00:00:00 2001 From: qima Date: Mon, 2 Nov 2020 20:07:55 +0800 Subject: [PATCH] api!: remove NetworkParams --- src/lib.rs | 2 -- src/network_params.rs | 27 ----------------- src/routing/approved.rs | 32 ++++---------------- src/routing/mod.rs | 15 ++++++---- src/routing/stage.rs | 2 +- src/routing/tests/mod.rs | 65 ++++++---------------------------------- src/section/mod.rs | 26 +++++----------- tests/bootstrap.rs | 5 ++-- tests/drop.rs | 4 +-- tests/utils/mod.rs | 21 +++---------- 10 files changed, 41 insertions(+), 158 deletions(-) delete mode 100644 src/network_params.rs diff --git a/src/lib.rs b/src/lib.rs index 9b5a86ca92..168a48b76f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,6 @@ extern crate log; pub use self::{ error::{Error, Result}, location::{DstLocation, SrcLocation}, - network_params::NetworkParams, routing::{Config, EventStream, Routing}, section::{SectionProofChain, MIN_AGE}, }; @@ -95,7 +94,6 @@ mod location; mod message_filter; mod messages; mod network; -mod network_params; mod node; mod peer; mod relocation; diff --git a/src/network_params.rs b/src/network_params.rs deleted file mode 100644 index a3ece1a5b7..0000000000 --- a/src/network_params.rs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2019 MaidSafe.net limited. -// -// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3. -// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed -// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. Please review the Licences for the specific language governing -// permissions and limitations relating to use of the SAFE Network Software. - -use crate::{ELDER_SIZE, RECOMMENDED_SECTION_SIZE}; - -/// Network parameters: number of elders, recommended section size -#[derive(Clone, Copy, Debug)] -pub struct NetworkParams { - /// The number of elders per section - pub elder_size: usize, - /// Recommended number of nodes in a section. - pub recommended_section_size: usize, -} - -impl Default for NetworkParams { - fn default() -> Self { - Self { - elder_size: ELDER_SIZE, - recommended_section_size: RECOMMENDED_SECTION_SIZE, - } - } -} diff --git a/src/routing/approved.rs b/src/routing/approved.rs index fd64a05f12..81ada3cf8c 100644 --- a/src/routing/approved.rs +++ b/src/routing/approved.rs @@ -31,7 +31,7 @@ use crate::{ EldersInfo, MemberInfo, Section, SectionKeyShare, SectionKeysProvider, SectionProofChain, MIN_AGE, }, - NetworkParams, + RECOMMENDED_SECTION_SIZE, }; use bls_dkg::key_gen::outcome::Outcome as DkgOutcome; use bytes::Bytes; @@ -53,25 +53,14 @@ pub(crate) struct Approved { dkg_voter: DkgVoter, relocate_state: Option, msg_filter: MessageFilter, - pub(super) network_params: NetworkParams, pub(super) event_tx: mpsc::UnboundedSender, } impl Approved { // Creates the approved state for the first node in the network - pub fn first_node( - node: Node, - network_params: NetworkParams, - event_tx: mpsc::UnboundedSender, - ) -> Result { + pub fn first_node(node: Node, event_tx: mpsc::UnboundedSender) -> Result { let (section, section_key_share) = Section::first_node(node.peer())?; - Ok(Self::new( - node, - section, - Some(section_key_share), - network_params, - event_tx, - )) + Ok(Self::new(node, section, Some(section_key_share), event_tx)) } // Creates the approved state for a regular node. @@ -79,7 +68,6 @@ impl Approved { node: Node, section: Section, section_key_share: Option, - network_params: NetworkParams, event_tx: mpsc::UnboundedSender, ) -> Self { let section_keys_provider = SectionKeysProvider::new(section_key_share); @@ -94,7 +82,6 @@ impl Approved { dkg_voter: Default::default(), relocate_state: None, msg_filter: MessageFilter::new(), - network_params, event_tx, } } @@ -269,10 +256,7 @@ impl Approved { let mut commands = vec![]; - for new_elders_info in self - .section - .promote_and_demote_elders(&self.network_params, &self.node.name()) - { + for new_elders_info in self.section.promote_and_demote_elders(&self.node.name()) { // Check whether the result still corresponds to the current elder candidates. if new_elders_info == dkg_elders_info { debug!("handle DKG result for {}: {:?}", new_elders_info, result); @@ -1092,10 +1076,7 @@ impl Approved { fn promote_and_demote_elders(&mut self) -> Result> { let mut commands = vec![]; - for info in self - .section - .promote_and_demote_elders(&self.network_params, &self.node.name()) - { + for info in self.section.promote_and_demote_elders(&self.node.name()) { commands.extend(self.send_dkg_start(info)?); } @@ -1165,8 +1146,7 @@ impl Approved { // and it has no more than `recommended_section_size` members. fn is_in_startup_phase(&self) -> bool { self.section.prefix().is_empty() - && self.section.members().joined().count() - <= self.network_params.recommended_section_size + && self.section.members().joined().count() <= RECOMMENDED_SECTION_SIZE } fn handle_online_event( diff --git a/src/routing/mod.rs b/src/routing/mod.rs index e813003316..a55357858f 100644 --- a/src/routing/mod.rs +++ b/src/routing/mod.rs @@ -28,11 +28,10 @@ use crate::{ error::{Error, Result}, event::{Connected, Event}, location::{DstLocation, SrcLocation}, - network_params::NetworkParams, node::Node, peer::Peer, section::{EldersInfo, SectionProofChain}, - TransportConfig, + TransportConfig, ELDER_SIZE, RECOMMENDED_SECTION_SIZE, }; use bytes::Bytes; use ed25519_dalek::{Keypair, PublicKey, Signature, Signer}; @@ -50,7 +49,10 @@ pub struct Config { /// Configuration for the underlying network transport. pub transport_config: TransportConfig, /// Global network parameters. Must be identical for all nodes in the network. - pub network_params: NetworkParams, + /// The number of elders per section + pub elder_size: usize, + /// Recommended number of nodes in a section. + pub recommended_section_size: usize, } impl Default for Config { @@ -59,7 +61,8 @@ impl Default for Config { first: false, keypair: None, transport_config: TransportConfig::default(), - network_params: NetworkParams::default(), + elder_size: ELDER_SIZE, + recommended_section_size: RECOMMENDED_SECTION_SIZE, } } } @@ -98,7 +101,7 @@ impl Routing { let incoming_msgs = comm.listen()?; let node = Node::new(keypair, comm.our_connection_info()?); - let state = Approved::first_node(node, config.network_params, event_tx)?; + let state = Approved::first_node(node, event_tx)?; state.send_event(Event::Connected(Connected::First)); state.send_event(Event::PromotedToElder); @@ -112,7 +115,7 @@ impl Routing { let node = Node::new(keypair, comm.our_connection_info()?); let (node, section) = bootstrap::infant(node, &comm, &mut incoming_msgs, bootstrap_addr).await?; - let state = Approved::new(node, section, None, config.network_params, event_tx); + let state = Approved::new(node, section, None, event_tx); state.send_event(Event::Connected(Connected::First)); diff --git a/src/routing/stage.rs b/src/routing/stage.rs index 42e58c8721..d951620d15 100644 --- a/src/routing/stage.rs +++ b/src/routing/stage.rs @@ -164,7 +164,7 @@ impl Stage { let mut state = self.state.lock().await; let event_tx = state.event_tx.clone(); - *state = Approved::new(node, section, None, state.network_params, event_tx); + *state = Approved::new(node, section, None, event_tx); state.send_event(Event::Connected(Connected::Relocate { previous_name })); diff --git a/src/routing/tests/mod.rs b/src/routing/tests/mod.rs index 2100300dd2..c1d48477e3 100644 --- a/src/routing/tests/mod.rs +++ b/src/routing/tests/mod.rs @@ -22,7 +22,7 @@ use crate::{ test_utils::*, EldersInfo, MemberInfo, PeerState, Section, SectionKeyShare, SectionProofChain, MIN_AGE, }, - Error, NetworkParams, ELDER_SIZE, + Error, ELDER_SIZE, }; use anyhow::Result; use assert_matches::assert_matches; @@ -35,7 +35,7 @@ use xor_name::{Prefix, XorName}; #[tokio::test] async fn receive_bootstrap_request() -> Result<()> { let node = create_node(); - let state = Approved::first_node(node, NetworkParams::default(), mpsc::unbounded_channel().0)?; + let state = Approved::first_node(node, mpsc::unbounded_channel().0)?; let stage = Stage::new(state, create_comm()?); let new_node = Node::new(crypto::gen_keypair(), gen_addr()); @@ -78,7 +78,7 @@ async fn receive_bootstrap_request() -> Result<()> { #[tokio::test] async fn receive_join_request() -> Result<()> { let node = create_node(); - let state = Approved::first_node(node, NetworkParams::default(), mpsc::unbounded_channel().0)?; + let state = Approved::first_node(node, mpsc::unbounded_channel().0)?; let stage = Stage::new(state, create_comm()?); let new_node = Node::new(crypto::gen_keypair(), gen_addr()); @@ -132,7 +132,6 @@ async fn accumulate_votes() -> Result<()> { node, section, Some(section_key_share), - NetworkParams::default(), mpsc::unbounded_channel().0, ); let stage = Stage::new(state, create_comm()?); @@ -186,13 +185,7 @@ async fn handle_consensus_on_online_of_infant() -> Result<()> { let sk_set = SecretKeySet::random(); let (section, section_key_share) = create_section(&sk_set, &elders_info)?; let node = nodes.remove(0); - let state = Approved::new( - node, - section, - Some(section_key_share), - NetworkParams::default(), - event_tx, - ); + let state = Approved::new(node, section, Some(section_key_share), event_tx); let stage = Stage::new(state, create_comm()?); let new_peer = create_peer(); @@ -274,7 +267,6 @@ async fn handle_consensus_on_online_of_elder_candidate() -> Result<()> { node, section, Some(section_key_share), - NetworkParams::default(), mpsc::unbounded_channel().0, ); let stage = Stage::new(state, create_comm()?); @@ -360,13 +352,7 @@ async fn handle_consensus_on_offline_of_non_elder() -> Result<()> { let (event_tx, mut event_rx) = mpsc::unbounded_channel(); let node = nodes.remove(0); - let state = Approved::new( - node, - section, - Some(section_key_share), - NetworkParams::default(), - event_tx, - ); + let state = Approved::new(node, section, Some(section_key_share), event_tx); let stage = Stage::new(state, create_comm()?); let member_info = MemberInfo { @@ -417,13 +403,7 @@ async fn handle_consensus_on_offline_of_elder() -> Result<()> { let (event_tx, mut event_rx) = mpsc::unbounded_channel(); let node = nodes.remove(0); let node_name = node.name(); - let state = Approved::new( - node, - section, - Some(section_key_share), - NetworkParams::default(), - event_tx, - ); + let state = Approved::new(node, section, Some(section_key_share), event_tx); let stage = Stage::new(state, create_comm()?); // Handle the consensus on the Offline vote @@ -548,13 +528,7 @@ async fn handle_unknown_message(source: UnknownMessageSource) -> Result<()> { let section = Section::new(chain, proven_elders_info)?; let node = create_node(); - let state = Approved::new( - node, - section, - None, - NetworkParams::default(), - mpsc::unbounded_channel().0, - ); + let state = Approved::new(node, section, None, mpsc::unbounded_channel().0); let stage = Stage::new(state, create_comm()?); // non-elders can't handle messages addressed to sections. @@ -664,13 +638,7 @@ async fn handle_untrusted_message(source: UntrustedMessageSource) -> Result<()> let node = create_node(); let node_name = node.name(); - let state = Approved::new( - node, - section, - None, - NetworkParams::default(), - mpsc::unbounded_channel().0, - ); + let state = Approved::new(node, section, None, mpsc::unbounded_channel().0); let stage = Stage::new(state, create_comm()?); let sk1 = bls::SecretKey::random(); @@ -760,7 +728,6 @@ async fn handle_bounced_unknown_message() -> Result<()> { node, section, Some(section_key_share), - NetworkParams::default(), mpsc::unbounded_channel().0, ); let stage = Stage::new(state, create_comm()?); @@ -862,7 +829,6 @@ async fn handle_bounced_untrusted_message() -> Result<()> { node, section, Some(section_key_share), - NetworkParams::default(), mpsc::unbounded_channel().0, ); let stage = Stage::new(state, create_comm()?); @@ -934,13 +900,7 @@ async fn handle_sync() -> Result<()> { let (event_tx, mut event_rx) = mpsc::unbounded_channel(); let section_key_share = create_section_key_share(&sk1_set, 0); let node = nodes.remove(0); - let state = Approved::new( - node, - old_section, - Some(section_key_share), - NetworkParams::default(), - event_tx, - ); + let state = Approved::new(node, old_section, Some(section_key_share), event_tx); let stage = Stage::new(state, create_comm()?); // Create new `Section` as a successor to the previous one. @@ -1018,7 +978,6 @@ async fn receive_message_with_invalid_proof_chain() -> Result<()> { node, section, Some(section_key_share), - NetworkParams::default(), mpsc::unbounded_channel().0, ); let stage = Stage::new(state, create_comm()?); @@ -1068,11 +1027,6 @@ enum RelocatedPeerRole { } async fn relocation(relocated_peer_role: RelocatedPeerRole) -> Result<()> { - let network_params = NetworkParams { - recommended_section_size: ELDER_SIZE + 1, - ..Default::default() - }; - let sk_set = SecretKeySet::random(); let prefix: Prefix = "0".parse().unwrap(); @@ -1089,7 +1043,6 @@ async fn relocation(relocated_peer_role: RelocatedPeerRole) -> Result<()> { node, section, Some(section_key_share), - network_params, mpsc::unbounded_channel().0, ); let stage = Stage::new(state, create_comm()?); diff --git a/src/section/mod.rs b/src/section/mod.rs index a2fe1c28f4..0d39822920 100644 --- a/src/section/mod.rs +++ b/src/section/mod.rs @@ -26,7 +26,7 @@ use crate::{ consensus::Proven, error::{Error, Result}, peer::Peer, - NetworkParams, + ELDER_SIZE, RECOMMENDED_SECTION_SIZE, }; use bls_signature_aggregator::Proof; use serde::{Deserialize, Serialize}; @@ -194,16 +194,12 @@ impl Section { /// Generate a new section info(s) based on the current set of members. /// Returns a set of EldersInfos to vote for. - pub fn promote_and_demote_elders( - &self, - network_params: &NetworkParams, - our_name: &XorName, - ) -> Vec { - if let Some((our_info, other_info)) = self.try_split(network_params, our_name) { + pub fn promote_and_demote_elders(&self, our_name: &XorName) -> Vec { + if let Some((our_info, other_info)) = self.try_split(our_name) { return vec![our_info, other_info]; } - let expected_peers = self.elder_candidates(network_params.elder_size); + let expected_peers = self.elder_candidates(ELDER_SIZE); let expected_names: BTreeSet<_> = expected_peers.iter().map(Peer::name).collect(); let current_names: BTreeSet<_> = self.elders_info().elders.keys().collect(); @@ -269,11 +265,7 @@ impl Section { // Tries to split our section. // If we have enough mature nodes for both subsections, returns the elders infos of the two // subsections. Otherwise returns `None`. - fn try_split( - &self, - network_params: &NetworkParams, - our_name: &XorName, - ) -> Option<(EldersInfo, EldersInfo)> { + fn try_split(&self, our_name: &XorName) -> Option<(EldersInfo, EldersInfo)> { let next_bit_index = if let Ok(index) = self.prefix().bit_count().try_into() { index } else { @@ -296,9 +288,7 @@ impl Section { }); // If none of the two new sections would contain enough entries, return `None`. - if our_new_size < network_params.recommended_section_size - || sibling_new_size < network_params.recommended_section_size - { + if our_new_size < RECOMMENDED_SECTION_SIZE || sibling_new_size < RECOMMENDED_SECTION_SIZE { return None; } @@ -307,12 +297,12 @@ impl Section { let our_elders = self.members.elder_candidates_matching_prefix( &our_prefix, - network_params.elder_size, + ELDER_SIZE, self.elders_info(), ); let other_elders = self.members.elder_candidates_matching_prefix( &other_prefix, - network_params.elder_size, + ELDER_SIZE, self.elders_info(), ); diff --git a/tests/bootstrap.rs b/tests/bootstrap.rs index fe4a77e507..1aadab7d1f 100644 --- a/tests/bootstrap.rs +++ b/tests/bootstrap.rs @@ -13,7 +13,7 @@ use ed25519_dalek::Keypair; use futures::future; use sn_routing::{ event::{Connected, Event}, - EventStream, NetworkParams, Routing, + EventStream, Routing, }; use tokio::time; use utils::*; @@ -140,11 +140,10 @@ async fn test_section_bootstrapping() -> Result<()> { // Test that the first `ELDER_SIZE` nodes in the network are promoted to elders. #[tokio::test] async fn test_startup_elders() -> Result<()> { - let network_params = NetworkParams::default(); // FIXME: using only 3 nodes for now because with 4 or more the test takes too long (but still // succeeds). Needs further investigation. let network_size = 3; - let mut nodes = create_connected_nodes(network_size, network_params).await?; + let mut nodes = create_connected_nodes(network_size).await?; async fn expect_promote_event(stream: &mut EventStream) { while let Some(event) = stream.next().await { diff --git a/tests/drop.rs b/tests/drop.rs index a6ab5a9c99..7ba1cf11b4 100644 --- a/tests/drop.rs +++ b/tests/drop.rs @@ -11,12 +11,12 @@ mod utils; use self::utils::*; use anyhow::{format_err, Result}; use bytes::Bytes; -use sn_routing::{event::Event, DstLocation, NetworkParams, SrcLocation}; +use sn_routing::{event::Event, DstLocation, SrcLocation}; use tokio::time; #[tokio::test] async fn test_node_drop() -> Result<()> { - let mut nodes = create_connected_nodes(2, NetworkParams::default()).await?; + let mut nodes = create_connected_nodes(2).await?; // Drop one node let dropped_name = nodes.remove(1).0.name().await; diff --git a/tests/utils/mod.rs b/tests/utils/mod.rs index 0014abc6a9..4300b3da2f 100644 --- a/tests/utils/mod.rs +++ b/tests/utils/mod.rs @@ -16,7 +16,7 @@ use futures::future; use itertools::Itertools; use sn_routing::{ event::{Connected, Event}, - Config, EventStream, NetworkParams, Routing, TransportConfig, MIN_AGE, + Config, EventStream, Routing, TransportConfig, MIN_AGE, }; use std::{ collections::{BTreeSet, HashSet}, @@ -71,13 +71,8 @@ impl<'a> RoutingBuilder { self } - pub fn network_params(mut self, params: NetworkParams) -> Self { - self.config.network_params = params; - self - } - pub fn elder_size(mut self, size: usize) -> Self { - self.config.network_params.elder_size = size; + self.config.elder_size = size; self } @@ -118,18 +113,11 @@ macro_rules! assert_next_event { } /// Create the given number of nodes and wait until they all connect. -pub async fn create_connected_nodes( - count: usize, - network_params: NetworkParams, -) -> Result> { +pub async fn create_connected_nodes(count: usize) -> Result> { let mut nodes = vec![]; // Create the first node - let (node, mut event_stream) = RoutingBuilder::new(None) - .first() - .network_params(network_params) - .create() - .await?; + let (node, mut event_stream) = RoutingBuilder::new(None).first().create().await?; assert_next_event!(event_stream, Event::Connected(Connected::First)); assert_next_event!(event_stream, Event::PromotedToElder); @@ -140,7 +128,6 @@ pub async fn create_connected_nodes( // Create the other nodes bootstrapping off the first node. let other_nodes = (1..count).map(|_| async { let (node, mut event_stream) = RoutingBuilder::new(None) - .network_params(network_params) .with_contact(bootstrap_contact) .create() .await?;