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

Commit

Permalink
feat!: use XorName instead of Prefix for section message src
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `Routing::match_section` renamed to `Routing::matching_section`
  • Loading branch information
madadam committed Mar 18, 2021
1 parent 5c925ad commit d2347ee
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 100 deletions.
6 changes: 3 additions & 3 deletions src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Message {
user_msg: Bytes,
proof_chain: SectionChain,
dst_key: Option<bls::PublicKey>,
src_section: XorName,
src_name: XorName,
) -> Result<Self, CreateError> {
let variant = Variant::UserMessage(user_msg);
let serialized = bincode::serialize(&SignableView {
Expand All @@ -158,7 +158,7 @@ impl Message {
signature_share,
};
let src = SrcAuthority::BlsShare {
src_section,
src_name,
proof_share,
public_key: node.keypair.public,
age: node.age,
Expand Down Expand Up @@ -207,7 +207,7 @@ impl Message {
) -> Result<Self, CreateError> {
Self::new_signed(
SrcAuthority::Section {
prefix: plain.src,
src_name: plain.src,
signature,
},
plain.dst,
Expand Down
6 changes: 3 additions & 3 deletions src/messages/plain_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
use super::{SignableView, Variant};
use serde::{Deserialize, Serialize};
use sn_messaging::DstLocation;
use xor_name::Prefix;
use xor_name::XorName;

/// Section-source message without signature and proof.
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, Debug)]
pub(crate) struct PlainMessage {
/// Prefix of the source section.
pub src: Prefix,
/// Name in the source section.
pub src: XorName,
/// Destination location.
pub dst: DstLocation,
/// The latest key of the destination section according to the sender's knowledge.
Expand Down
30 changes: 11 additions & 19 deletions src/messages/src_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use bls_signature_aggregator::ProofShare;
use serde::{Deserialize, Serialize};
use sn_messaging::SrcLocation;
use std::net::SocketAddr;
use xor_name::{Prefix, XorName};
use xor_name::XorName;

/// Source authority of a message.
/// Src of message and authority to send it. Authority is validated by the signature.
Expand All @@ -36,8 +36,8 @@ pub enum SrcAuthority {
},
/// Authority of a single peer that uses it's BLS Keyshare to sign the message.
BlsShare {
/// Section name at the time
src_section: XorName,
/// Name in the source section
src_name: XorName,
/// Public key of the source peer.
public_key: PublicKey,
/// Age of the source peer.
Expand All @@ -47,8 +47,8 @@ pub enum SrcAuthority {
},
/// Authority of a whole section.
Section {
/// Prefix of the source section.
prefix: Prefix,
/// Name in the source section.
src_name: XorName,
/// BLS signature of the message corresponding to the source section public key.
signature: bls::Signature,
},
Expand All @@ -59,24 +59,24 @@ impl SrcAuthority {
match self {
Self::Node { public_key, .. } => SrcLocation::Node(name(public_key)),
Self::BlsShare { public_key, .. } => SrcLocation::Node(name(public_key)),
Self::Section { prefix, .. } => SrcLocation::Section(prefix.name()),
Self::Section { src_name, .. } => SrcLocation::Section(*src_name),
}
}

pub(crate) fn is_section(&self) -> bool {
matches!(self, Self::Section { .. })
}

pub(crate) fn to_node_name(&self) -> Result<XorName> {
pub(crate) fn name(&self) -> XorName {
match self {
Self::Node { public_key, .. } => Ok(name(public_key)),
Self::BlsShare { public_key, .. } => Ok(name(public_key)),
Self::Section { .. } => Err(Error::InvalidSrcLocation),
Self::Node { public_key, .. } => name(public_key),
Self::BlsShare { public_key, .. } => name(public_key),
Self::Section { src_name, .. } => *src_name,
}
}

// If this location is `Node`, returns the corresponding `Peer` with `addr`. Otherwise error.
pub(crate) fn to_node_peer(&self, addr: SocketAddr) -> Result<Peer> {
pub(crate) fn peer(&self, addr: SocketAddr) -> Result<Peer> {
match self {
Self::Section { .. } => Err(Error::InvalidSrcLocation),
Self::Node {
Expand All @@ -87,12 +87,4 @@ impl SrcAuthority {
} => Ok(Peer::new(name(public_key), addr, *age)),
}
}

// If this is `Section`, returns the prefix.
pub(crate) fn as_section_prefix(&self) -> Result<&Prefix> {
match self {
Self::Section { prefix, .. } => Ok(prefix),
Self::Node { .. } | Self::BlsShare { .. } => Err(Error::InvalidSrcLocation),
}
}
}
27 changes: 10 additions & 17 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::{
};

use serde::{Deserialize, Serialize};
use sn_messaging::DstLocation;
use std::{borrow::Borrow, collections::HashSet, iter};
use xor_name::{Prefix, XorName};

Expand Down Expand Up @@ -205,27 +204,22 @@ impl Network {

/// Returns the elders_info and the latest known key for the prefix that matches `name`,
/// excluding self section.
pub fn section_by_name(&self, name: &XorName) -> (Option<bls::PublicKey>, Option<EldersInfo>) {
pub fn section_by_name(
&self,
name: &XorName,
) -> (Option<&bls::PublicKey>, Option<&EldersInfo>) {
(
self.keys.get_matching(name).map(|entry| entry.value.1),
self.keys.get_matching(name).map(|entry| &entry.value.1),
self.neighbours
.get_matching(name)
.map(|entry| entry.elders_info.value.clone()),
.map(|entry| &entry.elders_info.value),
)
}

/// Returns the public key in our chain that will be trusted by the given section.
/// If `None` is returned, the only key guaranteed to be trusted is the root key.
pub fn knowledge_by_section(&self, prefix: &Prefix) -> Option<&bls::PublicKey> {
/// Returns the public key in our chain that will be trusted by the given name.
pub fn knowledge_by_name(&self, name: &XorName) -> Option<&bls::PublicKey> {
self.knowledge
.get_equal_or_ancestor(prefix)
.map(|entry| &entry.value.1)
}

/// Returns the public key in our chain that will be trusted by the given location.
pub fn knowledge_by_location(&self, dst: &DstLocation) -> Option<&bls::PublicKey> {
self.knowledge
.get_matching(&dst.name()?)
.get_matching(name)
.map(|entry| &entry.value.1)
}

Expand Down Expand Up @@ -571,9 +565,8 @@ mod tests {
for (dst_name_prefix_str, expected_key) in expected_trusted_keys {
let dst_name_prefix: Prefix = dst_name_prefix_str.parse().unwrap();
let dst_name = dst_name_prefix.substituted_in(rand::random());
let dst = DstLocation::Section(dst_name);

assert_eq!(map.knowledge_by_location(&dst), Some(&expected_key));
assert_eq!(map.knowledge_by_name(&dst_name), Some(&expected_key));
}
}

Expand Down

0 comments on commit d2347ee

Please sign in to comment.