Skip to content
This repository has been archived by the owner on Jun 25, 2021. It is now read-only.

Commit

Permalink
api!: remove NetworkParams
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Nov 3, 2020
1 parent 98995f0 commit 686c248
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 158 deletions.
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand All @@ -95,7 +94,6 @@ mod location;
mod message_filter;
mod messages;
mod network;
mod network_params;
mod node;
mod peer;
mod relocation;
Expand Down
27 changes: 0 additions & 27 deletions src/network_params.rs

This file was deleted.

32 changes: 6 additions & 26 deletions src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -53,33 +53,21 @@ pub(crate) struct Approved {
dkg_voter: DkgVoter,
relocate_state: Option<RelocateState>,
msg_filter: MessageFilter,
pub(super) network_params: NetworkParams,
pub(super) event_tx: mpsc::UnboundedSender<Event>,
}

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<Event>,
) -> Result<Self> {
pub fn first_node(node: Node, event_tx: mpsc::UnboundedSender<Event>) -> Result<Self> {
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.
pub fn new(
node: Node,
section: Section,
section_key_share: Option<SectionKeyShare>,
network_params: NetworkParams,
event_tx: mpsc::UnboundedSender<Event>,
) -> Self {
let section_keys_provider = SectionKeysProvider::new(section_key_share);
Expand All @@ -94,7 +82,6 @@ impl Approved {
dkg_voter: Default::default(),
relocate_state: None,
msg_filter: MessageFilter::new(),
network_params,
event_tx,
}
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1092,10 +1076,7 @@ impl Approved {
fn promote_and_demote_elders(&mut self) -> Result<Vec<Command>> {
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)?);
}

Expand Down Expand Up @@ -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(
Expand Down
15 changes: 9 additions & 6 deletions src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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));

Expand Down
2 changes: 1 addition & 1 deletion src/routing/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));

Expand Down
65 changes: 9 additions & 56 deletions src/routing/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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()?);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()?);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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()?);
Expand Down Expand Up @@ -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()?);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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()?);
Expand Down Expand Up @@ -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();
Expand All @@ -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()?);
Expand Down
Loading

0 comments on commit 686c248

Please sign in to comment.