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

Commit

Permalink
Simplify state conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Apr 29, 2019
1 parent 665a842 commit 0e97a30
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 205 deletions.
69 changes: 28 additions & 41 deletions src/states/bootstrapping.rs
Expand Up @@ -49,19 +49,18 @@ pub enum TargetState {

// State of Client or Node while bootstrapping.
pub struct Bootstrapping {
action_sender: RoutingActionSender,
pub(super) action_sender: RoutingActionSender,
pub(super) cache: Box<Cache>,
pub(super) crust_service: Service,
pub(super) full_id: FullId,
pub(super) min_section_size: usize,
pub(super) timer: Timer,
bootstrap_blacklist: HashSet<SocketAddr>,
bootstrap_connection: Option<(PublicId, u64)>,
cache: Box<Cache>,
target_state: TargetState,
crust_service: Service,
full_id: FullId,
min_section_size: usize,
timer: Timer,
}

impl Bootstrapping {
#[allow(clippy::new_ret_no_self)]
pub fn new(
action_sender: RoutingActionSender,
cache: Box<Cache>,
Expand All @@ -83,61 +82,49 @@ impl Bootstrapping {
}
}
Some(Bootstrapping {
action_sender: action_sender,
bootstrap_blacklist: HashSet::new(),
bootstrap_connection: None,
action_sender,
cache: cache,
target_state: target_state,
crust_service: crust_service,
full_id: full_id,
min_section_size: min_section_size,
crust_service,
full_id,
min_section_size,
timer: timer,
bootstrap_blacklist: HashSet::new(),
bootstrap_connection: None,
target_state,
})
}

pub fn into_target_state(self, proxy_public_id: PublicId, outbox: &mut EventBox) -> State {
match self.target_state {
TargetState::Client { msg_expiry_dur } => State::Client(Client::from_bootstrapping(
self.crust_service,
self.full_id,
self.min_section_size,
self,
proxy_public_id,
self.timer,
msg_expiry_dur,
outbox,
)),
TargetState::RelocatingNode => {
if let Some(node) = RelocatingNode::from_bootstrapping(
self.action_sender,
self.cache,
self.crust_service,
self.full_id,
self.min_section_size,
proxy_public_id,
self.timer,
) {
if let Some(node) = RelocatingNode::from_bootstrapping(self, proxy_public_id) {
State::RelocatingNode(node)
} else {
outbox.send_event(Event::RestartRequired);
State::Terminated
}
}
TargetState::ProvingNode {
old_full_id,
our_section,
ref old_full_id,
ref our_section,
..
} => State::ProvingNode(ProvingNode::from_bootstrapping(
our_section,
self.action_sender,
self.cache,
self.crust_service,
old_full_id,
self.full_id,
self.min_section_size,
proxy_public_id,
self.timer,
outbox,
)),
} => {
let old_full_id = old_full_id.clone();
let our_section = our_section.clone();
State::ProvingNode(ProvingNode::from_bootstrapping(
self,
proxy_public_id,
old_full_id,
our_section,
outbox,
))
}
}
}

Expand Down
23 changes: 11 additions & 12 deletions src/states/client.rs
Expand Up @@ -6,7 +6,10 @@
// 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 super::common::{proxied, Base, Bootstrapped, NotEstablished, USER_MSG_CACHE_EXPIRY_DURATION};
use super::{
bootstrapping::Bootstrapping,
common::{proxied, Base, Bootstrapped, NotEstablished, USER_MSG_CACHE_EXPIRY_DURATION},
};
use crate::{
ack_manager::{Ack, AckManager, UnacknowledgedMessage},
chain::SectionInfo,
Expand Down Expand Up @@ -51,27 +54,23 @@ pub struct Client {
}

impl Client {
#[allow(clippy::too_many_arguments)]
pub fn from_bootstrapping(
crust_service: Service,
full_id: FullId,
min_section_size: usize,
source: Bootstrapping,
proxy_pub_id: PublicId,
timer: Timer,
msg_expiry_dur: Duration,
outbox: &mut EventBox,
) -> Self {
let client = Client {
ack_mgr: AckManager::new(),
crust_service: crust_service,
full_id: full_id,
min_section_size: min_section_size,
proxy_pub_id: proxy_pub_id,
crust_service: source.crust_service,
full_id: source.full_id,
min_section_size: source.min_section_size,
proxy_pub_id,
routing_msg_filter: RoutingMessageFilter::new(),
timer: timer,
timer: source.timer,
user_msg_cache: UserMessageCache::with_expiry_duration(USER_MSG_CACHE_EXPIRY_DURATION),
resend_buf: Default::default(),
msg_expiry_dur: msg_expiry_dur,
msg_expiry_dur,
};

debug!("{} State changed to Client.", client);
Expand Down
83 changes: 28 additions & 55 deletions src/states/establishing_node.rs
Expand Up @@ -11,6 +11,7 @@ use super::{
proxied, Approved, Base, Bootstrapped, NotEstablished, Relocated, RelocatedNotEstablished,
},
node::Node,
proving_node::ProvingNode,
};
use crate::{
ack_manager::AckManager,
Expand Down Expand Up @@ -39,61 +40,51 @@ use std::{
const POKE_TIMEOUT: Duration = Duration::from_secs(60);

pub struct EstablishingNode {
ack_mgr: AckManager,
cache: Box<Cache>,
chain: Chain,
crust_service: Service,
full_id: FullId,
gen_pfx_info: GenesisPfxInfo,
pub(super) ack_mgr: AckManager,
pub(super) cache: Box<Cache>,
pub(super) chain: Chain,
pub(super) crust_service: Service,
pub(super) full_id: FullId,
pub(super) gen_pfx_info: GenesisPfxInfo,
/// Routing messages addressed to us that we cannot handle until we are established.
msg_backlog: Vec<RoutingMessage>,
notified_nodes: BTreeSet<PublicId>,
parsec_map: ParsecMap,
peer_mgr: PeerManager,
pub(super) msg_backlog: Vec<RoutingMessage>,
pub(super) notified_nodes: BTreeSet<PublicId>,
pub(super) parsec_map: ParsecMap,
pub(super) peer_mgr: PeerManager,
pub(super) routing_msg_filter: RoutingMessageFilter,
pub(super) timer: Timer,
poke_timer_token: u64,
routing_msg_filter: RoutingMessageFilter,
timer: Timer,
}

impl EstablishingNode {
#[allow(clippy::too_many_arguments)]
pub fn from_proving_node(
ack_mgr: AckManager,
cache: Box<Cache>,
crust_service: Service,
full_id: FullId,
source: ProvingNode,
gen_pfx_info: GenesisPfxInfo,
min_section_size: usize,
msg_backlog: Vec<RoutingMessage>,
notified_nodes: BTreeSet<PublicId>,
peer_mgr: PeerManager,
routing_msg_filter: RoutingMessageFilter,
timer: Timer,
outbox: &mut EventBox,
) -> Result<Self, RoutingError> {
let public_id = *full_id.public_id();
let poke_timer_token = timer.schedule(POKE_TIMEOUT);
let public_id = *source.full_id.public_id();
let poke_timer_token = source.timer.schedule(POKE_TIMEOUT);

let parsec_map = ParsecMap::new(full_id.clone(), &gen_pfx_info);
let chain = Chain::new(min_section_size, public_id, gen_pfx_info.clone());
let parsec_map = ParsecMap::new(source.full_id.clone(), &gen_pfx_info);
let chain = Chain::new(source.min_section_size, public_id, gen_pfx_info.clone());

let mut node = Self {
ack_mgr,
cache,
ack_mgr: source.ack_mgr,
cache: source.cache,
chain,
crust_service,
full_id,
crust_service: source.crust_service,
full_id: source.full_id,
gen_pfx_info,
msg_backlog: vec![],
notified_nodes,
notified_nodes: source.notified_nodes,
parsec_map,
peer_mgr,
peer_mgr: source.peer_mgr,
routing_msg_filter: source.routing_msg_filter,
timer: source.timer,
poke_timer_token,
routing_msg_filter,
timer,
};

node.init(msg_backlog, outbox)?;
node.init(source.msg_backlog, outbox)?;
Ok(node)
}

Expand All @@ -117,25 +108,7 @@ impl EstablishingNode {
old_pfx: Prefix<XorName>,
outbox: &mut EventBox,
) -> State {
let node = Node::from_establishing_node(
self.ack_mgr,
self.cache,
self.chain,
self.crust_service,
self.full_id,
self.gen_pfx_info,
self.msg_backlog.into_iter().collect(),
self.notified_nodes,
old_pfx,
self.parsec_map,
self.peer_mgr,
self.routing_msg_filter,
sec_info,
self.timer,
outbox,
);

match node {
match Node::from_establishing_node(self, sec_info, old_pfx, outbox) {
Ok(node) => State::Node(node),
Err(_) => State::Terminated,
}
Expand Down
47 changes: 19 additions & 28 deletions src/states/node.rs
Expand Up @@ -6,7 +6,10 @@
// 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 super::common::{Approved, Base, Bootstrapped, Relocated, USER_MSG_CACHE_EXPIRY_DURATION};
use super::{
common::{Approved, Base, Bootstrapped, Relocated, USER_MSG_CACHE_EXPIRY_DURATION},
establishing_node::EstablishingNode,
};
use crate::{
ack_manager::{Ack, AckManager},
cache::Cache,
Expand Down Expand Up @@ -168,38 +171,26 @@ impl Node {
}
}

#[allow(clippy::too_many_arguments)]
pub fn from_establishing_node(
ack_mgr: AckManager,
cache: Box<Cache>,
chain: Chain,
crust_service: Service,
full_id: FullId,
gen_pfx_info: GenesisPfxInfo,
msg_queue: VecDeque<RoutingMessage>,
notified_nodes: BTreeSet<PublicId>,
old_pfx: Prefix<XorName>,
parsec_map: ParsecMap,
peer_mgr: PeerManager,
routing_msg_filter: RoutingMessageFilter,
source: EstablishingNode,
sec_info: SectionInfo,
timer: Timer,
old_pfx: Prefix<XorName>,
outbox: &mut EventBox,
) -> Result<Self, RoutingError> {
let mut node = Self::new(
ack_mgr,
cache,
chain,
crust_service,
full_id,
gen_pfx_info,
source.ack_mgr,
source.cache,
source.chain,
source.crust_service,
source.full_id,
source.gen_pfx_info,
false,
msg_queue,
notified_nodes,
parsec_map,
peer_mgr,
routing_msg_filter,
timer,
source.msg_backlog.into_iter().collect(),
source.notified_nodes,
source.parsec_map,
source.peer_mgr,
source.routing_msg_filter,
source.timer,
);
node.init(sec_info, old_pfx, outbox)?;
Ok(node)
Expand Down Expand Up @@ -254,7 +245,7 @@ impl Node {
proxy_load_amount: 0,
disable_resource_proof: dev_config.disable_resource_proof,
parsec_map,
gen_pfx_info: gen_pfx_info.clone(),
gen_pfx_info,
gossip_timer_token,
chain,
reconnect_peers: Default::default(),
Expand Down

0 comments on commit 0e97a30

Please sign in to comment.