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

Commit

Permalink
chore(deps): update sn_messaging, sn_data_types
Browse files Browse the repository at this point in the history
BREAKING CHANGE: removes send_message_to_client api,
  • Loading branch information
oetyng committed Feb 23, 2021
1 parent 84c53d8 commit 367b673
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 79 deletions.
11 changes: 4 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ rand_chacha = "~0.2.2"
thiserror = "1.0.23"
xor_name = "1.1.0"
resource_proof = "0.8.0"
# sn_messaging = "~4.0.0"
sn_messaging = { git = "https://github.com/oetyng/sn_messaging", branch = "all-acc-in-routing" }
# sn_messaging = { path = "../sn_messaging" }
# sn_data_types = "~0.14.3"
sn_data_types = { git = "https://github.com/oetyng/sn_data_types", branch = "remove-msgenvelope" }
# sn_data_types = { path = "../sn_data_types" }
sn_messaging = "~5.0.0"
sn_data_types = "~0.15.0"

[dependencies.bls]
package = "threshold_crypto"
Expand Down Expand Up @@ -72,4 +68,5 @@ yansi = "~0.5.0"

[dev-dependencies.tokio]
version = "~0.2.24"
features = [ "stream", "udp" ]
features = [ "stream", "udp" ]

77 changes: 6 additions & 71 deletions src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
// 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::{Command, SplitBarrier};
use super::{
enduser_registry::{EndUserRegistry, SocketId},
Command, SplitBarrier,
};
use crate::{
consensus::{
DkgCommands, DkgFailureProof, DkgFailureProofSet, DkgKey, DkgVoter, Proof, ProofShare,
Expand Down Expand Up @@ -38,83 +41,21 @@ use bytes::Bytes;
use ed25519_dalek::Verifier;
use itertools::Itertools;
use resource_proof::ResourceProof;
use sn_data_types::{PublicKey as EndUserPK, Signature as EndUserSig};
use sn_messaging::{
node::NodeMessage,
section_info::{
Error as TargetSectionError, GetSectionResponse, Message as SectionInfoMsg, SectionInfo,
},
DstLocation, EndUser, MessageType, SrcLocation,
};
use std::{
cmp,
collections::{btree_map::Entry, BTreeMap},
net::SocketAddr,
slice,
};
use std::{cmp, net::SocketAddr, slice};
use tokio::sync::mpsc;
use xor_name::{Prefix, XorName};

pub(crate) const RESOURCE_PROOF_DATA_SIZE: usize = 64;
pub(crate) const RESOURCE_PROOF_DIFFICULTY: u8 = 2;
const KEY_CACHE_SIZE: u8 = 5;

type SocketId = XorName;

struct EndUserRegistry {
clients: BTreeMap<SocketAddr, EndUser>,
socket_id_mapping: BTreeMap<SocketId, SocketAddr>,
}

impl EndUserRegistry {
pub fn new() -> Self {
Self {
clients: BTreeMap::default(),
socket_id_mapping: BTreeMap::default(),
}
}

pub fn get_enduser_by_addr(&self, socketaddr: &SocketAddr) -> Option<EndUser> {
self.clients.get(socketaddr).copied()
}

pub fn get_socket_addr(&self, socket_id: &SocketId) -> Option<&SocketAddr> {
self.socket_id_mapping.get(socket_id)
}

pub fn try_add(
&mut self,
sender: SocketAddr,
end_user_pk: EndUserPK,
socketaddr_sig: EndUserSig,
) -> Result<()> {
if let Ok(data) = &bincode::serialize(&sender) {
end_user_pk
.verify(&socketaddr_sig, data)
.map_err(|_e| Error::InvalidState)?;
} else {
return Err(Error::InvalidState);
}
let socket_id = if let Ok(socket_id_src) = &bincode::serialize(&socketaddr_sig) {
XorName::from_content(&[socket_id_src])
} else {
return Err(Error::InvalidState);
};
let end_user = EndUser::Client {
public_key: end_user_pk,
socket_id,
};
match self.socket_id_mapping.entry(socket_id) {
Entry::Vacant(_) => {
let _ = self.clients.insert(sender, end_user);
let _ = self.socket_id_mapping.insert(socket_id, sender);
}
Entry::Occupied(_) => (),
}
Ok(())
}
}

// The approved stage - node is a full member of a section and is performing its duties according
// to its persona (adult or elder).
pub(crate) struct Approved {
Expand Down Expand Up @@ -781,13 +722,7 @@ impl Approved {
// If elder, always handle UserMessage, otherwise handle it only if addressed directly to us
// as a node.
fn should_handle_user_message(&self, dst: &DstLocation) -> bool {
let is_elder = self.is_elder();
let are_we_dst = if let DstLocation::Node(name) = dst {
name == &self.node.name()
} else {
false
};
is_elder || are_we_dst
self.is_elder() || dst == &DstLocation::Node(self.node.name())
}

// Decide how to handle a `Vote` message.
Expand Down
71 changes: 71 additions & 0 deletions src/routing/enduser_registry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 crate::error::{Error, Result};
use sn_data_types::{PublicKey as EndUserPK, Signature as EndUserSig};
use sn_messaging::EndUser;
use std::{
collections::{btree_map::Entry, BTreeMap},
net::SocketAddr,
};
use xor_name::XorName;

pub type SocketId = XorName;
pub(crate) struct EndUserRegistry {
clients: BTreeMap<SocketAddr, EndUser>,
socket_id_mapping: BTreeMap<SocketId, SocketAddr>,
}

impl EndUserRegistry {
pub fn new() -> Self {
Self {
clients: BTreeMap::default(),
socket_id_mapping: BTreeMap::default(),
}
}

pub fn get_enduser_by_addr(&self, socketaddr: &SocketAddr) -> Option<EndUser> {
self.clients.get(socketaddr).copied()
}

pub fn get_socket_addr(&self, socket_id: &SocketId) -> Option<&SocketAddr> {
self.socket_id_mapping.get(socket_id)
}

pub fn try_add(
&mut self,
sender: SocketAddr,
end_user_pk: EndUserPK,
socketaddr_sig: EndUserSig,
) -> Result<()> {
if let Ok(data) = &bincode::serialize(&sender) {
end_user_pk
.verify(&socketaddr_sig, data)
.map_err(|_e| Error::InvalidState)?;
} else {
return Err(Error::InvalidState);
}
let socket_id = if let Ok(socket_id_src) = &bincode::serialize(&socketaddr_sig) {
XorName::from_content(&[socket_id_src])
} else {
return Err(Error::InvalidState);
};
let end_user = EndUser::Client {
public_key: end_user_pk,
socket_id,
};
match self.socket_id_mapping.entry(socket_id) {
Entry::Vacant(_) => {
let _ = self.clients.insert(sender, end_user);
let _ = self.socket_id_mapping.insert(socket_id, sender);
}
Entry::Occupied(_) => (),
}
Ok(())
}
}
3 changes: 2 additions & 1 deletion src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub(crate) mod command;
mod approved;
mod bootstrap;
mod comm;
mod enduser_registry;
mod event_stream;
mod split_barrier;
mod stage;
Expand Down Expand Up @@ -320,7 +321,7 @@ impl Routing {
{
if let Some(socket_addr) = self.stage.state.lock().await.get_socket_addr(&socket_id)
{
socket = Some(socket_addr.clone());
socket = Some(*socket_addr);
}
}

Expand Down

0 comments on commit 367b673

Please sign in to comment.