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

Commit

Permalink
feat: return TryJoinLater error when network disallow join
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi authored and S-Coyle committed Apr 8, 2021
1 parent 10243a9 commit a5e4d4b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ qp2p = "~0.11.3"
rand = "~0.7.3"
rand_chacha = "~0.2.2"
resource_proof = "0.8.0"
sn_messaging = "~13.0.0"
sn_messaging = "~13.1.0"
sn_data_types = "~0.18.0"
thiserror = "1.0.23"
tokio = "1.3.0"
Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,6 @@ pub enum Error {
InvalidSectionChain(#[from] SectionChainError),
#[error("Messaging protocol error: {0}")]
Messaging(#[from] sn_messaging::Error),
#[error("Routing is set to not allow taking any new node")]
TryJoinLater,
}
77 changes: 75 additions & 2 deletions src/routing/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,14 @@ impl<'a> State<'a> {
prefix,
pk_set,
elders,
joins_allowed,
}) => {
if !joins_allowed {
error!(
"Network is set to not taking any new joining node, try join later."
);
return Err(Error::TryJoinLater);
}
let key = pk_set.public_key();
info!(
"Joining a section ({:b}), key: {:?}, elders: {:?} (given by {:?})",
Expand Down Expand Up @@ -609,8 +616,8 @@ async fn send_messages(mut rx: mpsc::Receiver<(MessageType, Vec<SocketAddr>)>, c
mod tests {
use super::*;
use crate::{
agreement::test_utils::*, routing::tests::SecretKeySet, section::test_utils::*,
section::MemberInfo, ELDER_SIZE, MIN_ADULT_AGE, MIN_AGE,
agreement::test_utils::*, error::Error as RoutingError, routing::tests::SecretKeySet,
section::test_utils::*, section::MemberInfo, ELDER_SIZE, MIN_ADULT_AGE, MIN_AGE,
};
use anyhow::{anyhow, Error, Result};
use assert_matches::assert_matches;
Expand Down Expand Up @@ -674,6 +681,7 @@ mod tests {
.peers()
.map(|peer| (*peer.name(), *peer.addr()))
.collect(),
joins_allowed: true,
};
// Send GetSectionResponse::Success
let message = SectionInfoMsg::GetSectionResponse(GetSectionResponse::Success(
Expand Down Expand Up @@ -855,6 +863,69 @@ mod tests {
}
}

#[tokio::test]
async fn joins_disallowed_get_section_response_success() -> Result<()> {
let (send_tx, mut send_rx) = mpsc::channel(1);
let (recv_tx, recv_rx) = mpsc::channel(1);
let recv_rx = MessageReceiver::Deserialized(recv_rx);

let (elders_info, mut nodes) = gen_elders_info(Default::default(), ELDER_SIZE);
let bootstrap_node = nodes.remove(0);
let bootstrap_addr = bootstrap_node.addr;

let sk_set = SecretKeySet::random();
let pk_set = sk_set.public_keys();

let node = Node::new(
crypto::gen_keypair(&Prefix::default().range_inclusive(), MIN_ADULT_AGE),
gen_addr(),
);

let mut state = State::new(node, send_tx, recv_rx);

let bootstrap_task = state.bootstrap(vec![bootstrap_addr], None);

// Send an valid `BootstrapResponse::Join` followed by a valid one. The invalid one is
// ignored and the valid one processed normally.
let test_task = async {
let (message, _) = send_rx
.recv()
.await
.ok_or_else(|| anyhow!("GetSectionQuery was not received"))?;

assert_matches!(
message,
MessageType::SectionInfo(SectionInfoMsg::GetSectionQuery(_))
);

let infrastructure_info = SectionInfo {
prefix: elders_info.prefix,
pk_set,
elders: elders_info
.peers()
.map(|peer| (*peer.name(), *peer.addr()))
.collect(),
joins_allowed: false,
};
// Send GetSectionResponse::Success with the flag of joins_allowed set to false.
let message = SectionInfoMsg::GetSectionResponse(GetSectionResponse::Success(
infrastructure_info,
));
recv_tx.try_send((MessageType::SectionInfo(message), bootstrap_addr))?;

Ok(())
};

let (bootstrap_result, test_result) = future::join(bootstrap_task, test_task).await;

if let Err(RoutingError::TryJoinLater) = bootstrap_result {
} else {
return Err(anyhow!("Not getting an execpted network rejection."));
}

test_result
}

#[tokio::test]
async fn invalid_get_section_response_success() -> Result<()> {
let (send_tx, mut send_rx) = mpsc::channel(1);
Expand Down Expand Up @@ -904,6 +975,7 @@ mod tests {
elders: (0..ELDER_SIZE)
.map(|_| (bad_prefix.substituted_in(rand::random()), gen_addr()))
.collect(),
joins_allowed: true,
};

let message = SectionInfoMsg::GetSectionResponse(GetSectionResponse::Success(
Expand All @@ -919,6 +991,7 @@ mod tests {
elders: (0..ELDER_SIZE)
.map(|_| (good_prefix.substituted_in(rand::random()), gen_addr()))
.collect(),
joins_allowed: true,
};

let message = SectionInfoMsg::GetSectionResponse(GetSectionResponse::Success(
Expand Down
2 changes: 2 additions & 0 deletions src/routing/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ impl Core {
.peers()
.map(|peer| (*peer.name(), *peer.addr()))
.collect(),
joins_allowed: self.joins_allowed,
})
} else {
// If we are elder, we should know a section that is closer to `name` that us.
Expand Down Expand Up @@ -2033,6 +2034,7 @@ impl Core {
.peers()
.map(|peer| (*peer.name(), *peer.addr()))
.collect(),
joins_allowed: self.joins_allowed,
}));
} else {
return Err(TargetSectionError::DkgInProgress);
Expand Down

0 comments on commit a5e4d4b

Please sign in to comment.