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

Commit

Permalink
Merge fb5e63c into e9cb776
Browse files Browse the repository at this point in the history
  • Loading branch information
bochaco committed Jun 7, 2021
2 parents e9cb776 + fb5e63c commit 200f577
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 124 deletions.
2 changes: 1 addition & 1 deletion src/node/agreement.rs
Expand Up @@ -76,7 +76,7 @@ pub enum Proposal {
// Previous name if relocated.
previous_name: Option<XorName>,
// The key of the destination section that the joining node knows, if any.
their_knowledge: Option<BlsPublicKey>,
destination_key: Option<BlsPublicKey>,
},

// Proposal to remove a node from our section
Expand Down
132 changes: 132 additions & 0 deletions src/node/join.rs
@@ -0,0 +1,132 @@
// Copyright 2021 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// 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::{
agreement::Proven,
relocation::RelocatePayload,
section::{MemberInfo, SectionAuthorityProvider},
};
use ed25519_dalek::Signature;
use secured_linked_list::SecuredLinkedList;
use serde::{Deserialize, Serialize};
use std::{
collections::VecDeque,
fmt::{self, Debug, Formatter},
net::SocketAddr,
};
use threshold_crypto::PublicKey as BlsPublicKey;

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

impl Debug for JoinRequest {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
formatter
.debug_struct("JoinRequest")
.field("section_key", &self.section_key)
.field(
"relocate_payload",
&self
.relocate_payload
.as_ref()
.map(|payload| &payload.details),
)
.field(
"resource_proof_response",
&self
.resource_proof_response
.as_ref()
.map(|proof| proof.solution),
)
.finish()
}
}

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

/// Response to a request to join a section
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum JoinResponse {
/// Challenge sent from existing elder nodes to the joining peer for resource proofing.
ResourceChallenge {
data_size: usize,
difficulty: u8,
nonce: [u8; 32],
nonce_signature: Signature,
},
/// Up to date section information for a joining peer to retry its join request with
Retry(SectionAuthorityProvider),
/// Response redirecting a joining peer to join a different section,
/// containing addresses of nodes that are closer (than the recipient) to the
/// requested name. The `JoinRequest` should be re-sent to these addresses.
Redirect(SectionAuthorityProvider),
/// Message sent to joining peer containing the necessary
/// info to become a member of the section.
Approval {
genesis_key: BlsPublicKey,
section_auth: Proven<SectionAuthorityProvider>,
member_info: Proven<MemberInfo>,
section_chain: SecuredLinkedList,
},
Rejected(JoinRejectionReason),
}

impl Debug for JoinResponse {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::ResourceChallenge {
data_size,
difficulty,
..
} => f
.debug_struct("ResourceChallenge")
.field("data_size", data_size)
.field("difficulty", difficulty)
.finish(),
Self::Retry(section_auth) => write!(f, "Retry({:?})", section_auth),
Self::Redirect(section_auth) => write!(f, "Redirect({:?})", section_auth),
Self::Approval {
genesis_key,
section_auth,
member_info,
section_chain,
} => f
.debug_struct("Approval")
.field("genesis_key", genesis_key)
.field("section_auth", section_auth)
.field("member_info", member_info)
.field("section_chain", section_chain)
.finish(),
Self::Rejected(reason) => write!(f, "Rejected({:?})", reason),
}
}
}

/// Reason of a join request being rejected
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum JoinRejectionReason {
/// No new peers are currently accepted for joining
JoinsDisallowed,
/// The requesting node is not externally reachable
NodeNotReachable(SocketAddr),
}
12 changes: 2 additions & 10 deletions src/node/mod.rs
Expand Up @@ -8,6 +8,7 @@
// Software.

mod agreement;
mod join;
mod network;
mod node_msg;
mod plain_message;
Expand Down Expand Up @@ -35,7 +36,7 @@ pub use section::{
pub use signature_aggregator::{Error, SignatureAggregator};
pub use signed::{Signed, SignedShare};
pub use src_authority::SrcAuthority;
pub use variant::{JoinRequest, ResourceProofResponse, Variant};
pub use variant::Variant;

use crate::{Aggregation, DstLocation, MessageId, MessageType, WireMsg};
use bytes::Bytes;
Expand Down Expand Up @@ -65,11 +66,6 @@ pub struct RoutingMsg {
}

impl RoutingMsg {
/// Gets the message ID.
pub fn id(&self) -> MessageId {
self.id
}

/// Convenience function to deserialize a 'RoutingMsg' from bytes received over the wire.
/// It returns an error if the bytes don't correspond to a node message.
pub fn from(bytes: Bytes) -> crate::Result<Self> {
Expand All @@ -87,10 +83,6 @@ impl RoutingMsg {
pub fn serialize(&self, dest: XorName, dest_section_pk: BlsPublicKey) -> crate::Result<Bytes> {
WireMsg::serialize_routing_msg(self, dest, dest_section_pk)
}

pub fn section_pk(&self) -> BlsPublicKey {
self.section_pk
}
}

impl PartialEq for RoutingMsg {
Expand Down
106 changes: 8 additions & 98 deletions src/node/variant.rs
Expand Up @@ -8,21 +8,21 @@

use super::{
agreement::{DkgFailureSigned, DkgFailureSignedSet, DkgKey, Proposal, Proven},
join::{JoinRequest, JoinResponse},
network::Network,
relocation::{RelocateDetails, RelocatePayload, RelocatePromise},
section::{ElderCandidates, MemberInfo, Section, SectionAuthorityProvider},
relocation::{RelocateDetails, RelocatePromise},
section::{ElderCandidates, Section, SectionAuthorityProvider},
signed::SignedShare,
RoutingMsg,
};
use crate::DestInfo;
use bls_dkg::key_gen::message::Message as DkgMessage;
use ed25519_dalek::Signature;
use hex_fmt::HexFmt;
use itertools::Itertools;
use secured_linked_list::SecuredLinkedList;
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeSet, VecDeque},
collections::BTreeSet,
fmt::{self, Debug, Formatter},
};
use threshold_crypto::PublicKey as BlsPublicKey;
Expand All @@ -42,14 +42,6 @@ pub enum Variant {
/// User-facing message
#[serde(with = "serde_bytes")]
UserMessage(Vec<u8>),
/// Message sent to newly joined node containing the necessary info to become a member of our
/// section.
NodeApproval {
genesis_key: BlsPublicKey,
section_auth: Proven<SectionAuthorityProvider>,
member_info: Proven<MemberInfo>,
section_chain: SecuredLinkedList,
},
/// Message sent to all members to update them about the state of our section.
Sync {
// Information about our section.
Expand All @@ -63,14 +55,10 @@ pub enum Variant {
/// - from a section to a current elder to be relocated after they are demoted.
/// - from the node to be relocated back to its section after it was demoted.
RelocatePromise(RelocatePromise),
/// Sent from a bootstrapping peer to the section that responded with a
/// `GetSectionResponse::Succcess` to its `GetSectionQuery`.
/// Sent from a bootstrapping peer to the section requesting to join as a new member
JoinRequest(Box<JoinRequest>),
/// Response to outdated JoinRequest
JoinRetry {
section_auth: SectionAuthorityProvider,
section_key: BlsPublicKey,
},
/// Response to a `JoinRequest`
JoinResponse(Box<JoinResponse>),
/// Sent from a node that can't establish the trust of the contained message to its original
/// source in order for them to provide new proof that the node would trust.
BouncedUntrustedMessage {
Expand Down Expand Up @@ -105,13 +93,6 @@ pub enum Variant {
content: Proposal,
signed_share: SignedShare,
},
/// Challenge sent from existing elder nodes to the joining peer for resource proofing.
ResourceChallenge {
data_size: usize,
difficulty: u8,
nonce: [u8; 32],
nonce_signature: Signature,
},
/// Message sent by a node to indicate it received a message from a node which was ahead in knowledge.
/// A reply is expected with a `SectionKnowledge` message.
SectionKnowledgeQuery {
Expand All @@ -125,18 +106,6 @@ impl Debug for Variant {
match self {
Self::SectionKnowledge { .. } => f.debug_struct("SectionKnowledge").finish(),
Self::UserMessage(payload) => write!(f, "UserMessage({:10})", HexFmt(payload)),
Self::NodeApproval {
genesis_key,
section_auth,
member_info,
section_chain,
} => f
.debug_struct("NodeApproval")
.field("genesis_key", genesis_key)
.field("section_auth", section_auth)
.field("member_info", member_info)
.field("section_chain", section_chain)
.finish(),
Self::Sync { section, network } => f
.debug_struct("Sync")
.field("section_auth", &section.section_auth.value)
Expand All @@ -156,14 +125,7 @@ impl Debug for Variant {
Self::Relocate(payload) => write!(f, "Relocate({:?})", payload),
Self::RelocatePromise(payload) => write!(f, "RelocatePromise({:?})", payload),
Self::JoinRequest(payload) => write!(f, "JoinRequest({:?})", payload),
Self::JoinRetry {
section_auth,
section_key,
} => f
.debug_struct("JoinRetry")
.field("section_auth", section_auth)
.field("section_key", section_key)
.finish(),
Self::JoinResponse(response) => write!(f, "JoinResponse({:?})", response),
Self::BouncedUntrustedMessage { msg, dest_info } => f
.debug_struct("BouncedUntrustedMessage")
.field("message", msg)
Expand Down Expand Up @@ -203,59 +165,7 @@ impl Debug for Variant {
.field("content", content)
.field("signed_share", signed_share)
.finish(),
Self::ResourceChallenge {
data_size,
difficulty,
..
} => f
.debug_struct("ResourceChallenge")
.field("data_size", data_size)
.field("difficulty", difficulty)
.finish(),
Self::SectionKnowledgeQuery { .. } => write!(f, "SectionKnowledgeQuery"),
}
}
}

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

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

impl Debug for JoinRequest {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
formatter
.debug_struct("JoinRequest")
.field("section_key", &self.section_key)
.field(
"relocate_payload",
&self
.relocate_payload
.as_ref()
.map(|payload| &payload.details),
)
.field(
"resource_proof_response",
&self
.resource_proof_response
.as_ref()
.map(|proof| proof.solution),
)
.finish()
}
}
7 changes: 1 addition & 6 deletions src/section_info/mod.rs
Expand Up @@ -22,10 +22,7 @@ use xor_name::{Prefix, XorName};
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum Message {
/// Message to request information about the section that matches the given name.
GetSectionQuery {
public_key: PublicKey,
is_node: bool,
},
GetSectionQuery(PublicKey),
/// Response to `GetSectionQuery`.
GetSectionResponse(GetSectionResponse),
/// Updated info related to section
Expand Down Expand Up @@ -73,8 +70,6 @@ pub enum GetSectionResponse {
/// Successful response to `GetSectionQuery`. Contains information about the requested
/// section.
Success(SectionInfo),
/// The requesting node is not externally reachable
NodeNotReachable,
/// Response to `GetSectionQuery` containing addresses of nodes that are closer to the
/// requested name than the recipient. The request should be repeated to these addresses.
Redirect(Vec<(XorName, SocketAddr)>),
Expand Down

0 comments on commit 200f577

Please sign in to comment.