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

Commit

Permalink
Extract EstablishingNode from Node
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Apr 25, 2019
1 parent 4c87f6f commit ca95759
Show file tree
Hide file tree
Showing 7 changed files with 652 additions and 272 deletions.
35 changes: 26 additions & 9 deletions src/parsec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ pub struct ParsecMap {
}

impl ParsecMap {
pub fn new(full_id: FullId, gen_pfx_info: &GenesisPfxInfo) -> Self {
let parsec = create(full_id, gen_pfx_info);
pub fn new(version: u64, parsec: Parsec) -> Self {
let mut map = BTreeMap::new();
let _ = map.insert(*gen_pfx_info.first_info.version(), parsec);
let _ = map.insert(version, parsec);

Self { map }
}
Expand Down Expand Up @@ -68,7 +67,7 @@ impl ParsecMap {

let response = parsec
.handle_request(&pub_id, request)
.map(|response| Message::Direct(DirectMessage::ParsecResponse(msg_version, response)))
.map(|response| response.into_message(msg_version))
.map_err(|err| {
debug!("{} - Error handling parsec request: {:?}", log_ident, err);
err
Expand Down Expand Up @@ -100,11 +99,13 @@ impl ParsecMap {
}

pub fn create_gossip(&mut self, version: u64, target: &id::PublicId) -> Option<Message> {
let parsec = self.map.get_mut(&version)?;
parsec
.create_gossip(target)
.ok()
.map(|request| Message::Direct(DirectMessage::ParsecRequest(version, request)))
Some(
self.map
.get_mut(&version)?
.create_gossip(target)
.ok()?
.into_message(version),
)
}

pub fn vote_for(&mut self, event: chain::NetworkEvent, log_ident: &str) {
Expand Down Expand Up @@ -167,6 +168,22 @@ impl ParsecMap {
}
}

pub trait GossipMessage {
fn into_message(self, version: u64) -> Message;
}

impl GossipMessage for Request {
fn into_message(self, version: u64) -> Message {
Message::Direct(DirectMessage::ParsecRequest(version, self))
}
}

impl GossipMessage for Response {
fn into_message(self, version: u64) -> Message {
Message::Direct(DirectMessage::ParsecResponse(version, self))
}
}

/// Create Parsec instance.
pub fn create(full_id: FullId, gen_pfx_info: &GenesisPfxInfo) -> Parsec {
if gen_pfx_info
Expand Down
18 changes: 0 additions & 18 deletions src/peer_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,6 @@ pub struct PeerManager {
our_public_id: PublicId,
candidate: Candidate,
disable_client_rate_limiter: bool,
established: bool,
}

impl PeerManager {
Expand All @@ -330,7 +329,6 @@ impl PeerManager {
our_public_id: our_public_id,
candidate: Candidate::None,
disable_client_rate_limiter: disable_client_rate_limiter,
established: false,
}
}

Expand Down Expand Up @@ -653,12 +651,7 @@ impl PeerManager {
}

/// Remove and return `PublicId`s of expired peers.
/// Will only be active once we are established.
pub fn remove_expired_peers(&mut self) -> Vec<PublicId> {
if !self.established {
return vec![];
}

let remove_candidate = if self.candidate.is_expired() {
match self.candidate {
Candidate::None => None,
Expand Down Expand Up @@ -1001,17 +994,6 @@ impl PeerManager {

self.peers.remove(pub_id).is_some() || remove_candidate
}

/// Sets this peer as established.
/// Expired peers will be purged once established.
pub fn set_established(&mut self) {
self.established = true;
}

/// Returns whether this peer is established.
pub fn is_established(&self) -> bool {
self.established
}
}

impl fmt::Display for PeerManager {
Expand Down
35 changes: 29 additions & 6 deletions src/state_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

use crate::{
action::Action,
chain::GenesisPfxInfo,
chain::{GenesisPfxInfo, SectionInfo},
id::{FullId, PublicId},
outbox::EventBox,
routing_table::Prefix,
states::common::Base,
states::{Bootstrapping, Client, Node, ProvingNode, RelocatingNode},
states::{Bootstrapping, Client, EstablishingNode, Node, ProvingNode, RelocatingNode},
timer::Timer,
types::RoutingActionSender,
xor_name::XorName,
Expand Down Expand Up @@ -41,6 +41,7 @@ macro_rules! state_dispatch {
State::Client($state) => $expr,
State::RelocatingNode($state) => $expr,
State::ProvingNode($state) => $expr,
State::EstablishingNode($state) => $expr,
State::Node($state) => $expr,
State::Terminated => $term_expr,
}
Expand All @@ -67,6 +68,7 @@ pub enum State {
Client(Client),
RelocatingNode(RelocatingNode),
ProvingNode(ProvingNode),
EstablishingNode(EstablishingNode),
Node(Node),
Terminated,
}
Expand Down Expand Up @@ -123,6 +125,7 @@ impl State {
#[cfg(feature = "mock")]
fn chain(&self) -> Option<&Chain> {
match *self {
State::EstablishingNode(ref state) => Some(state.chain()),
State::Node(ref state) => Some(state.chain()),
_ => None,
}
Expand Down Expand Up @@ -188,6 +191,7 @@ impl State {
State::Client(ref mut state) => state.get_timed_out_tokens(),
State::RelocatingNode(ref mut state) => state.get_timed_out_tokens(),
State::ProvingNode(ref mut state) => state.get_timed_out_tokens(),
State::EstablishingNode(ref mut state) => state.get_timed_out_tokens(),
State::Node(ref mut state) => state.get_timed_out_tokens(),
}
}
Expand All @@ -207,6 +211,7 @@ impl State {
| State::Client(_)
| State::RelocatingNode(_)
| State::ProvingNode(_) => false,
State::EstablishingNode(ref state) => state.has_unconsensused_observations(),
State::Node(ref state) => state.has_unconsensused_observations(),
}
}
Expand All @@ -225,6 +230,7 @@ impl State {
State::Client(ref state) => state.in_authority(auth),
State::RelocatingNode(ref state) => state.in_authority(auth),
State::ProvingNode(ref state) => state.in_authority(auth),
State::EstablishingNode(ref state) => state.in_authority(auth),
State::Node(ref state) => state.in_authority(auth),
}
}
Expand All @@ -235,6 +241,7 @@ impl State {
State::Client(ref state) => state.ack_mgr().has_unacked_msg(),
State::RelocatingNode(ref state) => state.ack_mgr().has_unacked_msg(),
State::ProvingNode(ref state) => state.ack_mgr().has_unacked_msg(),
State::EstablishingNode(ref state) => state.ack_mgr().has_unacked_msg(),
State::Node(ref state) => state.ack_mgr().has_unacked_msg(),
}
}
Expand All @@ -254,10 +261,15 @@ pub enum Transition {
new_id: FullId,
our_section: (Prefix<XorName>, BTreeSet<PublicId>),
},
// `ProvingNode` state transitioning to `Node`.
IntoNode {
// `ProvingNode` state transitioning to `EstablishingNode`.
IntoEstablishingNode {
gen_pfx_info: GenesisPfxInfo,
},
// `EstablishingNode` state transition to `Node`.
IntoNode {
sec_info: SectionInfo,
old_pfx: Prefix<XorName>,
},
Terminate,
}

Expand Down Expand Up @@ -415,9 +427,20 @@ impl StateMachine {
};
self.state = new_state;
}
IntoNode { gen_pfx_info } => {
IntoEstablishingNode { gen_pfx_info } => {
let new_state = match mem::replace(&mut self.state, State::Terminated) {
State::ProvingNode(proving_node) => {
proving_node.into_establishing_node(gen_pfx_info)
}
_ => unreachable!(),
};
self.state = new_state
}
IntoNode { sec_info, old_pfx } => {
let new_state = match mem::replace(&mut self.state, State::Terminated) {
State::ProvingNode(proving_node) => proving_node.into_node(gen_pfx_info),
State::EstablishingNode(establishing_node) => {
establishing_node.into_node(sec_info, old_pfx, outbox)
}
_ => unreachable!(),
};
self.state = new_state
Expand Down
Loading

0 comments on commit ca95759

Please sign in to comment.