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

Commit

Permalink
refactor(bootstrap): changes to new messaging flow for peers joining …
Browse files Browse the repository at this point in the history
…the network

BREAKING CHANGE: new node Join messaging is not backward compatible.
  • Loading branch information
bochaco authored and Yoga07 committed Jun 9, 2021
1 parent 7961391 commit 0ebb7c0
Show file tree
Hide file tree
Showing 21 changed files with 607 additions and 924 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ qp2p = "~0.12.0"
rand = "~0.7.3"
rand_chacha = "~0.2.2"
resource_proof = "0.8.0"
sn_messaging = { git = "https://github.com/maqi/sn_messaging.git", branch = "refactory_sap" }
sn_messaging = "33.0.0"
sn_data_types = "~0.18.3"
thiserror = "1.0.23"
tokio = "1.3.0"
Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,6 @@ pub enum Error {
NoMatchingSection,
#[error("No matching Elder")]
NoMatchingElder,
#[error("Cannot start node since it is not externally reachable")]
NodeNotReachable,
#[error("Node cannot join the network since it is not externally reachable: {0}")]
NodeNotReachable(SocketAddr),
}
3 changes: 1 addition & 2 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ use bytes::Bytes;
use ed25519_dalek::Keypair;
use hex_fmt::HexFmt;
pub use qp2p::{RecvStream, SendStream};
use sn_messaging::Signed;
use sn_messaging::{client::ClientMsg, DstLocation, EndUser, SrcLocation};
use sn_messaging::{client::ClientMsg, node::Signed, DstLocation, EndUser, SrcLocation};
use std::{
collections::BTreeSet,
fmt::{self, Debug, Formatter},
Expand Down
7 changes: 3 additions & 4 deletions src/message_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::cache::Cache;
use crate::messages::RoutingMsgUtils;
use sn_messaging::{node::RoutingMsg, DstLocation, MessageId};
use std::time::Duration;
use xor_name::XorName;
Expand Down Expand Up @@ -59,17 +58,17 @@ impl MessageFilter {
//
pub async fn filter_outgoing(&self, msg: &RoutingMsg, pub_id: &XorName) -> FilteringResult {
// Not filtering direct messages.
if let DstLocation::DirectAndUnrouted = msg.dst() {
if let DstLocation::DirectAndUnrouted = msg.dst {
return FilteringResult::NewMessage;
}

if self
.outgoing
.set((msg.id(), *pub_id), (), None)
.set((msg.id, *pub_id), (), None)
.await
.is_some()
{
trace!("Outgoing message filtered: {:?}", msg.id());
trace!("Outgoing message filtered: {:?}", msg.id);
FilteringResult::KnownMessage
} else {
FilteringResult::NewMessage
Expand Down
72 changes: 23 additions & 49 deletions src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use crate::{
};
use secured_linked_list::{error::Error as SecuredLinkedListError, SecuredLinkedList};
use serde::Serialize;
use sn_messaging::node::{Signed, SignedShare};
use sn_messaging::{
node::{PlainMessage, RoutingMsg, SrcAuthority, Variant},
node::{JoinResponse, PlainMessage, RoutingMsg, SrcAuthority, Variant},
Aggregation, DstLocation, MessageId,
};
use sn_messaging::{Signed, SignedShare};
use std::fmt::Debug;
use thiserror::Error;
use xor_name::XorName;
Expand Down Expand Up @@ -81,17 +81,6 @@ pub trait RoutingMsgUtils {
/// Getter
fn signed(&self) -> Option<Signed>;

/// Getter
fn dst(&self) -> &DstLocation;

fn id(&self) -> &MessageId;

/// Getter
fn variant(&self) -> &Variant;

/// Getter
fn src(&self) -> &SrcAuthority;

fn verify_variant<'a, I: IntoIterator<Item = &'a bls::PublicKey>>(
&self,
trusted_keys: I,
Expand Down Expand Up @@ -127,7 +116,7 @@ impl RoutingMsgUtils for RoutingMsg {
return Err(Error::CreateError(CreateError::FailedSignature));
}

if signed_share.public_key_set.public_key() != msg.section_pk() {
if signed_share.public_key_set.public_key() != msg.section_pk {
error!(
"Signed share public key doesn't match signed chain last key: {:?}",
msg
Expand Down Expand Up @@ -368,49 +357,34 @@ impl RoutingMsgUtils for RoutingMsg {
}
}

/// Getter
fn dst(&self) -> &DstLocation {
&self.dst
}

/// Get the MessageId
fn id(&self) -> &MessageId {
&self.id
}

/// Getter
fn variant(&self) -> &Variant {
&self.variant
}

/// Getter
fn src(&self) -> &SrcAuthority {
&self.src
}

fn verify_variant<'a, I>(&self, trusted_keys: I) -> Result<VerifyStatus>
where
I: IntoIterator<Item = &'a bls::PublicKey>,
{
let proof_chain = match &self.variant {
Variant::NodeApproval {
section_auth,
member_info,
section_chain,
..
} => {
if !section_auth.verify(section_chain) {
return Err(Error::InvalidMessage);
}

if !member_info.verify(section_chain) {
return Err(Error::InvalidMessage);
Variant::JoinResponse(resp) => {
if let JoinResponse::Approval {
ref section_auth,
ref member_info,
ref section_chain,
..
} = **resp
{
if !section_auth.verify(section_chain) {
return Err(Error::InvalidMessage);
}

if !member_info.verify(section_chain) {
return Err(Error::InvalidMessage);
}

section_chain
} else {
return Ok(VerifyStatus::Full);
}

section_chain
}
Variant::SectionKnowledge {
src_info: (_, chain),
src_info: (_, ref chain),
..
} => chain,
Variant::Sync { section, .. } => section.chain(),
Expand Down
2 changes: 1 addition & 1 deletion src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use xor_name::{XorName, XOR_NAME_LEN};
/// Information and state of our node
#[derive(Clone)]
pub struct Node {
// Keep the secret key in Box to allow Clone while also preventing multiple copies to exist in
// Keep the secret key in Arc to allow Clone while also preventing multiple copies to exist in
// memory which might be insecure.
// TODO: find a way to not require `Clone`.
pub keypair: Arc<Keypair>,
Expand Down
7 changes: 3 additions & 4 deletions src/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use crate::{
crypto::{self, Keypair, Verifier},
error::Error,
messages::RoutingMsgUtils,
network::NetworkUtils,
peer::PeerUtils,
section::{SectionPeersUtils, SectionUtils},
Expand Down Expand Up @@ -120,16 +119,16 @@ pub trait SignedRelocateDetailsUtils {

impl SignedRelocateDetailsUtils for SignedRelocateDetails {
fn new(signed_msg: RoutingMsg) -> Result<Self, Error> {
if let Variant::Relocate(_) = signed_msg.variant() {
if let Variant::Relocate(_) = signed_msg.variant {
Ok(Self { signed_msg })
} else {
Err(Error::InvalidMessage)
}
}

fn relocate_details(&self) -> Result<&RelocateDetails, Error> {
if let Variant::Relocate(details) = &self.signed_msg.variant() {
Ok(&details)
if let Variant::Relocate(details) = &self.signed_msg.variant {
Ok(details)
} else {
error!("SignedRelocateDetails does not contain Variant::Relocate");
Err(Error::InvalidMessage)
Expand Down

0 comments on commit 0ebb7c0

Please sign in to comment.