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

Commit

Permalink
Merge ec7e77b into d060cca
Browse files Browse the repository at this point in the history
  • Loading branch information
bochaco committed Jun 4, 2021
2 parents d060cca + ec7e77b commit 8ae758f
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 99 deletions.
127 changes: 127 additions & 0 deletions src/node/join.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
// 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;
use xor_name::XorName;

/// 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, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum JoinResponse {
/// Challenge sent from existing elder nodes to the joining peer for resource proofing.
ResourceChallenge(JoinChallenge),
/// Response to an outdated JoinRequest.
Retry(JoinRetry),
/// Response redirecting a bootstrapping node 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(Vec<(XorName, SocketAddr)>),
/// Message sent to joining node containing the necessary
/// info to become a member of the section.
Approval(Box<JoinApproval>),
}

/// Challenge sent from existing elder nodes to the joining peer for resource proofing.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct JoinChallenge {
data_size: usize,
difficulty: u8,
nonce: [u8; 32],
nonce_signature: Signature,
}

#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
/// Information provided to a bootstrapping peer which is approved to join a section
pub struct JoinApproval {
genesis_key: BlsPublicKey,
section_auth: Proven<SectionAuthorityProvider>,
member_info: Proven<MemberInfo>,
section_chain: SecuredLinkedList,
}

impl Debug for JoinApproval {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("JoinApproval")
.field("genesis_key", &self.genesis_key)
.field("section_auth", &self.section_auth)
.field("member_info", &self.member_info)
.field("section_chain", &self.section_chain)
.finish()
}
}

#[derive(Clone, Eq, PartialEq, Serialize, Deserialize)]
/// Up to date section information for a joining peer to retry its join request with
pub struct JoinRetry {
section_auth: SectionAuthorityProvider,
section_key: BlsPublicKey,
}

impl Debug for JoinRetry {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.debug_struct("JoinRetry")
.field("section_auth", &self.section_auth)
.field("section_key", &self.section_key)
.finish()
}
}
4 changes: 3 additions & 1 deletion src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// Software.

mod agreement;
mod join;
mod network;
mod node_msg;
mod plain_message;
Expand All @@ -18,6 +19,7 @@ mod src_authority;
mod variant;

pub use agreement::{DkgFailureProof, DkgFailureProofSet, DkgKey, Proposal, Proven};
pub use join::{JoinRequest, ResourceProofResponse};
pub use network::{Network, OtherSection};
pub use node_msg::{
NodeCmd, NodeCmdError, NodeDataError, NodeDataQueryResponse, NodeEvent, NodeMsg, NodeQuery,
Expand All @@ -31,7 +33,7 @@ pub use section::{
ElderCandidates, MemberInfo, Peer, PeerState, Section, SectionAuthorityProvider, SectionPeers,
};
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
106 changes: 8 additions & 98 deletions src/node/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@

use super::{
agreement::{DkgFailureProof, DkgFailureProofSet, DkgKey, Proposal, Proven},
join::{JoinRequest, JoinResponse},
network::Network,
relocation::{RelocateDetails, RelocatePayload, RelocatePromise},
section::{ElderCandidates, MemberInfo, Section, SectionAuthorityProvider},
relocation::{RelocateDetails, RelocatePromise},
section::{ElderCandidates, Section, SectionAuthorityProvider},
RoutingMsg,
};
use crate::DestInfo;
use bls_dkg::key_gen::message::Message as DkgMessage;
use bls_signature_aggregator::ProofShare;
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,
proof_share: ProofShare,
},
/// 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("proof_share", proof_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()
}
}

0 comments on commit 8ae758f

Please sign in to comment.