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

Commit

Permalink
chore: address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Dec 3, 2020
1 parent f67ae6c commit 5565da1
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod variant;
pub use self::{hash::MessageHash, src_authority::SrcAuthority};
pub(crate) use self::{
plain_message::PlainMessage,
variant::{BootstrapResponse, JoinRequest, Proof, Variant},
variant::{BootstrapResponse, JoinRequest, ResourceProofResponse, Variant},
};
use crate::{
crypto::{self, name, Verifier},
Expand Down
26 changes: 16 additions & 10 deletions src/messages/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ pub(crate) enum Variant {
proof_share: ProofShare,
},
/// Challenge sent from existing elder nodes to the joining peer for resource proofing.
Challenge {
ResourceChallenge {
data_size: usize,
difficulty: u8,
nonce: [u8; 32],
signature: Signature,
nonce_signature: Signature,
},
}

Expand Down Expand Up @@ -208,12 +208,12 @@ impl Debug for Variant {
.field("content", content)
.field("proof_share", proof_share)
.finish(),
Self::Challenge {
Self::ResourceChallenge {
data_size,
difficulty,
..
} => f
.debug_struct("Challenge")
.debug_struct("ResourceChallenge")
.field("data_size", data_size)
.field("difficulty", difficulty)
.finish(),
Expand All @@ -237,22 +237,22 @@ pub enum BootstrapResponse {

/// Joining peer's proof of resolvement of given resource proofing challenge.
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
pub(crate) struct Proof {
pub(crate) struct ResourceProofResponse {
pub(crate) solution: u64,
pub(crate) data: VecDeque<u8>,
pub(crate) nonce: [u8; 32],
pub(crate) signature: Signature,
pub(crate) nonce_signature: Signature,
}

/// Request to join a section
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
pub(crate) struct JoinRequest {
/// The public key of the section to join
/// The public key of the section to join.
pub section_key: bls::PublicKey,
/// If the peer is being relocated, contains `RelocatePayload`. Otherwise contains `None`.
pub relocate_payload: Option<RelocatePayload>,
/// Proof of the resouce proofing
pub proof: Option<Proof>,
/// Proof of the resouce proofing.
pub resource_proof_response: Option<ResourceProofResponse>,
}

impl Debug for JoinRequest {
Expand All @@ -267,7 +267,13 @@ impl Debug for JoinRequest {
.as_ref()
.map(|payload| payload.relocate_details()),
)
.field("proof", &self.proof.as_ref().map(|proof| proof.solution))
.field(
"resource_proof_response",
&self
.resource_proof_response
.as_ref()
.map(|proof| proof.solution),
)
.finish()
}
}
26 changes: 12 additions & 14 deletions src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
message_filter::MessageFilter,
messages::{
BootstrapResponse, JoinRequest, Message, MessageHash, MessageStatus, PlainMessage,
Proof as ResourceProofingProof, Variant, VerifyStatus, PING,
ResourceProofResponse, Variant, VerifyStatus, PING,
},
network::Network,
node::Node,
Expand All @@ -43,8 +43,8 @@ use std::{cmp, net::SocketAddr, slice};
use tokio::sync::mpsc;
use xor_name::{Prefix, XorName};

pub(crate) const RESOURCE_PROOF_DATA_SIZE: usize = 128;
pub(crate) const RESOURCE_PROOF_DIFFICULTY: u8 = 4;
pub(crate) const RESOURCE_PROOF_DATA_SIZE: usize = 64;
pub(crate) const RESOURCE_PROOF_DIFFICULTY: u8 = 2;

// The approved stage - node is a full member of a section and is performing its duties according
// to its persona (adult or elder).
Expand Down Expand Up @@ -478,7 +478,7 @@ impl Approved {
}
}
}
Variant::Challenge { .. } => {
Variant::ResourceChallenge { .. } => {
// Already approved, no need to resolve challenge.
return Ok(MessageStatus::Useless);
}
Expand Down Expand Up @@ -591,7 +591,7 @@ impl Approved {

Ok(vec![])
}
Variant::Challenge { .. } => {
Variant::ResourceChallenge { .. } => {
trace!("Already got approved to join, no need to resolve challenge");
Ok(vec![])
}
Expand Down Expand Up @@ -1028,19 +1028,19 @@ impl Approved {
(MIN_AGE + 1, None, None)
};

if let Some(ResourceProofingProof {
if let Some(ResourceProofResponse {
solution,
data,
nonce,
signature,
}) = join_request.proof
nonce_signature,
}) = join_request.resource_proof_response
{
let serialized = bincode::serialize(&(*peer.name(), nonce))?;
if self
.node
.keypair
.public
.verify(&serialized, &signature)
.verify(&serialized, &nonce_signature)
.is_ok()
&& self.resource_proof.validate_all(&nonce, &data, solution)
{
Expand All @@ -1054,11 +1054,11 @@ impl Approved {

let nonce: [u8; 32] = rand::random();
let serialized = bincode::serialize(&(*peer.name(), nonce))?;
let response = Variant::Challenge {
let response = Variant::ResourceChallenge {
data_size: RESOURCE_PROOF_DATA_SIZE,
difficulty: RESOURCE_PROOF_DIFFICULTY,
nonce,
signature: crypto::sign(&serialized, &self.node.keypair),
nonce_signature: crypto::sign(&serialized, &self.node.keypair),
};
Ok(vec![self.send_direct_message(peer.addr(), response)?])
}
Expand Down Expand Up @@ -1736,9 +1736,7 @@ impl Approved {
Ok(Some(command))
}

// Setting the JoinsAllowed has two effectives:
// Trigger a round Vote::SetJoinsAllowed to update the flag once concensused. Which will
// allow/disallow new nodes joining once done.
// Setting the JoinsAllowed triggers a round Vote::SetJoinsAllowed to update the flag.
pub fn set_joins_allowed(&mut self, joins_allowed: bool) -> Result<Vec<Command>> {
let mut commands = Vec::new();
if self.is_elder() && joins_allowed != self.joins_allowed {
Expand Down
30 changes: 16 additions & 14 deletions src/routing/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use crate::{
crypto::{self, Signature},
error::{Error, Result},
location::DstLocation,
messages::{BootstrapResponse, JoinRequest, Message, Proof, Variant, VerifyStatus},
messages::{
BootstrapResponse, JoinRequest, Message, ResourceProofResponse, Variant, VerifyStatus,
},
node::Node,
peer::Peer,
relocation::{RelocatePayload, SignedRelocateDetails},
Expand All @@ -28,7 +30,7 @@ use xor_name::Prefix;

const BACKLOG_CAPACITY: usize = 100;

/// Bootstrap into the network as an adult node.
/// Bootstrap into the network as new node.
///
/// NOTE: It's not guaranteed this function ever returns. This can happen due to messages being
/// lost in transit or other reasons. It's the responsibility of the caller to handle this case,
Expand Down Expand Up @@ -240,7 +242,7 @@ impl<'a> State<'a> {
let mut join_request = JoinRequest {
section_key,
relocate_payload: relocate_payload.clone(),
proof: None,
resource_proof_response: None,
};
let mut recipients: Vec<_> = elders_info
.elders
Expand Down Expand Up @@ -285,7 +287,7 @@ impl<'a> State<'a> {
join_request = JoinRequest {
section_key,
relocate_payload: relocate_payload.clone(),
proof: None,
resource_proof_response: None,
};
recipients = elders_info
.elders
Expand All @@ -300,11 +302,11 @@ impl<'a> State<'a> {
);
}
}
JoinResponse::Challenge {
JoinResponse::ResourceChallenge {
data_size,
difficulty,
nonce,
signature,
nonce_signature,
} => {
let rp = ResourceProof::new(data_size, difficulty);
let data = rp.create_proof_data(&nonce);
Expand All @@ -313,11 +315,11 @@ impl<'a> State<'a> {
join_request = JoinRequest {
section_key,
relocate_payload: relocate_payload.clone(),
proof: Some(Proof {
resource_proof_response: Some(ResourceProofResponse {
solution,
data,
nonce,
signature,
nonce_signature,
}),
};
recipients.push(sender);
Expand Down Expand Up @@ -369,22 +371,22 @@ impl<'a> State<'a> {
sender,
));
}
Variant::Challenge {
Variant::ResourceChallenge {
data_size,
difficulty,
nonce,
signature,
nonce_signature,
} => {
if !self.verify_message(&message, None) {
continue;
}

return Ok((
JoinResponse::Challenge {
JoinResponse::ResourceChallenge {
data_size: *data_size,
difficulty: *difficulty,
nonce: *nonce,
signature: *signature,
nonce_signature: *nonce_signature,
},
sender,
));
Expand Down Expand Up @@ -467,11 +469,11 @@ enum JoinResponse {
elders_info: EldersInfo,
section_key: bls::PublicKey,
},
Challenge {
ResourceChallenge {
data_size: usize,
difficulty: u8,
nonce: [u8; 32],
signature: Signature,
nonce_signature: Signature,
},
}

Expand Down
13 changes: 6 additions & 7 deletions src/routing/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ use crate::{
location::DstLocation,
majority,
messages::{
BootstrapResponse, JoinRequest, Message, PlainMessage, Proof as ResourceProofingProof,
Variant,
BootstrapResponse, JoinRequest, Message, PlainMessage, ResourceProofResponse, Variant,
},
network::Network,
node::Node,
Expand Down Expand Up @@ -97,7 +96,7 @@ async fn receive_join_request() -> Result<()> {
Variant::JoinRequest(Box::new(JoinRequest {
section_key,
relocate_payload: None,
proof: None,
resource_proof_response: None,
})),
None,
None,
Expand All @@ -116,9 +115,9 @@ async fn receive_join_request() -> Result<()> {
);
let response_message = Message::from_bytes(&response_message)?;

let (nonce, signature) = assert_matches!(
let (nonce, nonce_signature) = assert_matches!(
response_message.variant(),
Variant::Challenge { nonce, signature, .. } => (*nonce, *signature)
Variant::ResourceChallenge { nonce, nonce_signature, .. } => (*nonce, *nonce_signature)
);

let rp = ResourceProof::new(RESOURCE_PROOF_DATA_SIZE, RESOURCE_PROOF_DIFFICULTY);
Expand All @@ -132,11 +131,11 @@ async fn receive_join_request() -> Result<()> {
Variant::JoinRequest(Box::new(JoinRequest {
section_key,
relocate_payload: None,
proof: Some(ResourceProofingProof {
resource_proof_response: Some(ResourceProofResponse {
solution,
data,
nonce,
signature,
nonce_signature,
}),
})),
None,
Expand Down

0 comments on commit 5565da1

Please sign in to comment.