From 5565da11ece649330e7650c7c1e3de65ab5f5445 Mon Sep 17 00:00:00 2001 From: qima Date: Thu, 3 Dec 2020 23:59:00 +0800 Subject: [PATCH] chore: address review comments --- src/messages/mod.rs | 2 +- src/messages/variant.rs | 26 ++++++++++++++++---------- src/routing/approved.rs | 26 ++++++++++++-------------- src/routing/bootstrap.rs | 30 ++++++++++++++++-------------- src/routing/tests/mod.rs | 13 ++++++------- 5 files changed, 51 insertions(+), 46 deletions(-) diff --git a/src/messages/mod.rs b/src/messages/mod.rs index f5bb78a5ad..32ff9c8e4a 100644 --- a/src/messages/mod.rs +++ b/src/messages/mod.rs @@ -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}, diff --git a/src/messages/variant.rs b/src/messages/variant.rs index 2cdb9a8882..652159f07f 100644 --- a/src/messages/variant.rs +++ b/src/messages/variant.rs @@ -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, }, } @@ -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(), @@ -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, 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, - /// Proof of the resouce proofing - pub proof: Option, + /// Proof of the resouce proofing. + pub resource_proof_response: Option, } impl Debug for JoinRequest { @@ -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() } } diff --git a/src/routing/approved.rs b/src/routing/approved.rs index 85486a91d4..870e610fc8 100644 --- a/src/routing/approved.rs +++ b/src/routing/approved.rs @@ -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, @@ -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). @@ -478,7 +478,7 @@ impl Approved { } } } - Variant::Challenge { .. } => { + Variant::ResourceChallenge { .. } => { // Already approved, no need to resolve challenge. return Ok(MessageStatus::Useless); } @@ -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![]) } @@ -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) { @@ -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)?]) } @@ -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> { let mut commands = Vec::new(); if self.is_elder() && joins_allowed != self.joins_allowed { diff --git a/src/routing/bootstrap.rs b/src/routing/bootstrap.rs index f0d7bf1f7f..cd4e7a3875 100644 --- a/src/routing/bootstrap.rs +++ b/src/routing/bootstrap.rs @@ -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}, @@ -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, @@ -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 @@ -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 @@ -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); @@ -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); @@ -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, )); @@ -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, }, } diff --git a/src/routing/tests/mod.rs b/src/routing/tests/mod.rs index 540b7ba293..a6674d16ce 100644 --- a/src/routing/tests/mod.rs +++ b/src/routing/tests/mod.rs @@ -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, @@ -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, @@ -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); @@ -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,