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

Commit

Permalink
refactor: remove unnecessary Command::SendBootstrapRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Oct 7, 2020
1 parent 2e8a1fd commit 48a802e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 46 deletions.
8 changes: 1 addition & 7 deletions src/node/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ pub(crate) enum Command {
dst: DstLocation,
content: Bytes,
},
/// Send `BootstrapRequest` to the given recipients.
SendBootstrapRequest(Vec<SocketAddr>),
/// Schedule a timeout after the given duration. When the timeout expires, a `HandleTimeout`
/// command is pushed into the command queue. The token is used to identify the timeout.
/// command is raised. The token is used to identify the timeout.
ScheduleTimeout { duration: Duration, token: u64 },
/// Transition into the given state.
Transition(Box<State>),
Expand Down Expand Up @@ -116,10 +114,6 @@ impl Debug for Command {
.field("dst", dst)
.field("content", &format_args!("{:10}", hex_fmt::HexFmt(content)))
.finish(),
Self::SendBootstrapRequest(recipients) => f
.debug_tuple("SendBootstrapRequest")
.field(recipients)
.finish(),
Self::ScheduleTimeout { duration, token } => f
.debug_struct("ScheduleTimeout")
.field("duration", duration)
Expand Down
2 changes: 1 addition & 1 deletion src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Node {
} else {
info!("{} Bootstrapping a new node.", node_name);
let (comm, bootstrap_addr) = Comm::from_bootstrapping(config.transport_config).await?;
let (state, command) = Bootstrapping::new(None, vec![bootstrap_addr], node_info);
let (state, command) = Bootstrapping::new(None, vec![bootstrap_addr], node_info)?;

(state.into(), comm, Some(command))
};
Expand Down
2 changes: 1 addition & 1 deletion src/node/stage/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl Approved {
}) => {
// Transition from Approved to Bootstrapping on relocation
let (state, command) =
Bootstrapping::new(Some(details), conn_infos, self.node_info.clone());
Bootstrapping::new(Some(details), conn_infos, self.node_info.clone())?;
let state = State::Bootstrapping(state);
Ok(vec![Command::Transition(Box::new(state)), command])
}
Expand Down
16 changes: 8 additions & 8 deletions src/node/stage/bootstrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ impl Bootstrapping {
relocate_details: Option<SignedRelocateDetails>,
bootstrap_contacts: Vec<SocketAddr>,
node_info: NodeInfo,
) -> (Self, Command) {
(
Self {
node_info,
relocate_details,
},
Command::SendBootstrapRequest(bootstrap_contacts),
)
) -> Result<(Self, Command)> {
let state = Self {
node_info,
relocate_details,
};
let command = state.send_bootstrap_request(&bootstrap_contacts)?;

Ok((state, command))
}

pub fn handle_message(
Expand Down
10 changes: 0 additions & 10 deletions src/node/stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,6 @@ impl Stage {
Command::SendUserMessage { src, dst, content } => {
self.send_user_message(src, dst, content).await
}
Command::SendBootstrapRequest(recipients) => {
Ok(vec![self.send_bootstrap_request(recipients).await?])
}
Command::ScheduleTimeout { duration, token } => {
Ok(vec![self.handle_schedule_timeout(duration, token).await])
}
Expand Down Expand Up @@ -413,13 +410,6 @@ impl Stage {
}
}

async fn send_bootstrap_request(&self, recipients: Vec<SocketAddr>) -> Result<Command> {
match &*self.state.lock().await {
State::Bootstrapping(state) => state.send_bootstrap_request(&recipients),
_ => Err(Error::InvalidState),
}
}

async fn handle_schedule_timeout(&self, duration: Duration, token: u64) -> Command {
time::delay_for(duration).await;
Command::HandleTimeout(token)
Expand Down
25 changes: 6 additions & 19 deletions src/node/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,26 +50,15 @@ use xor_name::Prefix;
async fn send_bootstrap_request() -> Result<()> {
let bootstrap_addr = create_addr();
let (node_info, _) = create_node_info();
let (state, command) = Bootstrapping::new(None, vec![bootstrap_addr], node_info);
let (state, command) = Bootstrapping::new(None, vec![bootstrap_addr], node_info)?;
let node = Stage::new(state.into(), create_comm()?);

let recipients = assert_matches!(
command,
Command::SendBootstrapRequest(recipients) => recipients
);
assert_eq!(recipients, [bootstrap_addr]);

let mut commands = node
.handle_command(Command::SendBootstrapRequest(recipients))
.await?
.into_iter();

let (recipients, message) = assert_matches!(
commands.next(),
Some(Command::SendMessage {
command,
Command::SendMessage {
recipients,
message, ..
}) => (recipients, message)
} => (recipients, message)
);
assert_eq!(recipients, [bootstrap_addr]);

Expand All @@ -79,8 +68,6 @@ async fn send_bootstrap_request() -> Result<()> {
Variant::BootstrapRequest(name) => assert_eq!(*name, node.name().await)
);

assert!(commands.next().is_none());

Ok(())
}

Expand Down Expand Up @@ -133,7 +120,7 @@ async fn receive_bootstrap_request() -> Result<()> {
#[tokio::test]
async fn receive_bootstrap_response_join() -> Result<()> {
let (node_info, _) = create_node_info();
let (state, _) = Bootstrapping::new(None, vec![create_addr()], node_info);
let (state, _) = Bootstrapping::new(None, vec![create_addr()], node_info)?;
let node = Stage::new(state.into(), create_comm()?);

let (elders_info, elder_keypairs) = create_elders_info();
Expand Down Expand Up @@ -193,7 +180,7 @@ async fn receive_bootstrap_response_join() -> Result<()> {
async fn receive_bootstrap_response_rebootstrap() -> Result<()> {
let (node_info, _) = create_node_info();
let node_name = node_info.name();
let (state, _) = Bootstrapping::new(None, vec![create_addr()], node_info);
let (state, _) = Bootstrapping::new(None, vec![create_addr()], node_info)?;
let node = Stage::new(state.into(), create_comm()?);

let old_keypair = create_keypair();
Expand Down

0 comments on commit 48a802e

Please sign in to comment.