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

Commit

Permalink
Reduce coupling between states
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed May 7, 2019
1 parent 56837ad commit f5ed47a
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 167 deletions.
66 changes: 45 additions & 21 deletions src/states/bootstrapping.rs
Expand Up @@ -6,7 +6,12 @@
// 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::Base, Client, ProvingNode, RelocatingNode};
use super::{
client::{Client, ClientDetails},
common::Base,
proving_node::{ProvingNode, ProvingNodeDetails},
relocating_node::{RelocatingNode, RelocatingNodeDetails},
};
use crate::{
cache::Cache,
crust::CrustUser,
Expand Down Expand Up @@ -49,15 +54,15 @@ pub enum TargetState {

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

impl Bootstrapping {
Expand Down Expand Up @@ -94,36 +99,55 @@ impl Bootstrapping {
})
}

pub fn into_target_state(self, proxy_public_id: PublicId, outbox: &mut EventBox) -> State {
pub fn into_target_state(self, proxy_pub_id: PublicId, outbox: &mut EventBox) -> State {
match self.target_state {
TargetState::Client { msg_expiry_dur } => State::Client(Client::from_bootstrapping(
self,
proxy_public_id,
msg_expiry_dur,
ClientDetails {
crust_service: self.crust_service,
full_id: self.full_id,
min_section_size: self.min_section_size,
msg_expiry_dur,
proxy_pub_id,
timer: self.timer,
},
outbox,
)),
TargetState::RelocatingNode => {
if let Some(node) = RelocatingNode::from_bootstrapping(self, proxy_public_id) {
let details = RelocatingNodeDetails {
action_sender: self.action_sender,
cache: self.cache,
crust_service: self.crust_service,
full_id: self.full_id,
min_section_size: self.min_section_size,
proxy_pub_id,
timer: self.timer,
};

if let Some(node) = RelocatingNode::from_bootstrapping(details) {
State::RelocatingNode(node)
} else {
outbox.send_event(Event::RestartRequired);
State::Terminated
}
}
TargetState::ProvingNode {
ref old_full_id,
ref our_section,
old_full_id,
our_section,
..
} => {
let old_full_id = old_full_id.clone();
let our_section = our_section.clone();
State::ProvingNode(ProvingNode::from_bootstrapping(
self,
proxy_public_id,
let details = ProvingNodeDetails {
action_sender: self.action_sender,
cache: self.cache,
crust_service: self.crust_service,
full_id: self.full_id,
min_section_size: self.min_section_size,
old_full_id,
our_section,
outbox,
))
proxy_pub_id,
timer: self.timer,
};

State::ProvingNode(ProvingNode::from_bootstrapping(details, outbox))
}
}
}
Expand Down
33 changes: 17 additions & 16 deletions src/states/client.rs
Expand Up @@ -6,10 +6,7 @@
// 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::{
bootstrapping::Bootstrapping,
common::{proxied, Base, Bootstrapped, NotEstablished, USER_MSG_CACHE_EXPIRY_DURATION},
};
use super::common::{proxied, Base, Bootstrapped, NotEstablished, USER_MSG_CACHE_EXPIRY_DURATION};
use crate::{
ack_manager::{Ack, AckManager, UnacknowledgedMessage},
chain::SectionInfo,
Expand Down Expand Up @@ -37,6 +34,15 @@ use std::{
/// Duration to wait before sending rate limit exceeded messages.
pub const RATE_EXCEED_RETRY: Duration = Duration::from_millis(800);

pub struct ClientDetails {
pub crust_service: Service,
pub full_id: FullId,
pub min_section_size: usize,
pub msg_expiry_dur: Duration,
pub proxy_pub_id: PublicId,
pub timer: Timer,
}

/// A node connecting a user to the network, as opposed to a routing / data storage node.
///
/// Each client has a _proxy_: a node through which all requests are routed.
Expand All @@ -54,23 +60,18 @@ pub struct Client {
}

impl Client {
pub fn from_bootstrapping(
source: Bootstrapping,
proxy_pub_id: PublicId,
msg_expiry_dur: Duration,
outbox: &mut EventBox,
) -> Self {
pub fn from_bootstrapping(details: ClientDetails, outbox: &mut EventBox) -> Self {
let client = Client {
ack_mgr: AckManager::new(),
crust_service: source.crust_service,
full_id: source.full_id,
min_section_size: source.min_section_size,
proxy_pub_id,
crust_service: details.crust_service,
full_id: details.full_id,
min_section_size: details.min_section_size,
proxy_pub_id: details.proxy_pub_id,
routing_msg_filter: RoutingMessageFilter::new(),
timer: source.timer,
timer: details.timer,
user_msg_cache: UserMessageCache::with_expiry_duration(USER_MSG_CACHE_EXPIRY_DURATION),
resend_buf: Default::default(),
msg_expiry_dur,
msg_expiry_dur: details.msg_expiry_dur,
};

debug!("{} State changed to Client.", client);
Expand Down
93 changes: 62 additions & 31 deletions src/states/establishing_node.rs
Expand Up @@ -10,8 +10,7 @@ use super::{
common::{
proxied, Approved, Base, Bootstrapped, NotEstablished, Relocated, RelocatedNotEstablished,
},
node::Node,
proving_node::ProvingNode,
node::{Node, NodeDetails},
};
use crate::{
ack_manager::AckManager,
Expand Down Expand Up @@ -39,52 +38,69 @@ use std::{

const POKE_TIMEOUT: Duration = Duration::from_secs(60);

pub struct EstablishingNodeDetails {
pub ack_mgr: AckManager,
pub cache: Box<Cache>,
pub crust_service: Service,
pub full_id: FullId,
pub gen_pfx_info: GenesisPfxInfo,
pub min_section_size: usize,
pub msg_backlog: Vec<RoutingMessage>,
pub notified_nodes: BTreeSet<PublicId>,
pub peer_mgr: PeerManager,
pub routing_msg_filter: RoutingMessageFilter,
pub timer: Timer,
}

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

impl EstablishingNode {
pub fn from_proving_node(
source: ProvingNode,
gen_pfx_info: GenesisPfxInfo,
details: EstablishingNodeDetails,
outbox: &mut EventBox,
) -> Result<Self, RoutingError> {
let public_id = *source.full_id.public_id();
let poke_timer_token = source.timer.schedule(POKE_TIMEOUT);
let public_id = *details.full_id.public_id();
let poke_timer_token = details.timer.schedule(POKE_TIMEOUT);

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 parsec_map = ParsecMap::new(details.full_id.clone(), &details.gen_pfx_info);
let chain = Chain::new(
details.min_section_size,
public_id,
details.gen_pfx_info.clone(),
);

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

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

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

match Node::from_establishing_node(details, sec_info, old_pfx, outbox) {
Ok(node) => State::Node(node),
Err(_) => State::Terminated,
}
Expand Down

0 comments on commit f5ed47a

Please sign in to comment.