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

Commit

Permalink
api!: for aggregate_at_src message, notify sn_node with proof as well
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi authored and dirvine committed Apr 21, 2021
1 parent 4e86a20 commit 8a39aaa
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// permissions and limitations relating to use of the SAFE Network Software.

use crate::section::SectionChain;
use bls_signature_aggregator::Proof;
use bytes::Bytes;
use ed25519_dalek::Keypair;
use hex_fmt::HexFmt;
Expand Down Expand Up @@ -49,6 +50,8 @@ pub enum Event {
src: SrcLocation,
/// The destination location that receives the message.
dst: DstLocation,
/// The proof if the message was set to be aggregated at source.
proof: Option<Proof>,
/// The proof chain for the message, if any.
proof_chain: Option<SectionChain>,
},
Expand Down
31 changes: 20 additions & 11 deletions src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ impl Message {
return Err(CreateError::PublicKeyMismatch);
}
}
SrcAuthority::Section { signature, .. } => {
SrcAuthority::Section { proof, .. } => {
if let Some(proof_chain) = msg.proof_chain.as_ref() {
if !proof_chain.last_key().verify(signature, &signed_bytes) {
if !proof_chain
.last_key()
.verify(&proof.signature, &signed_bytes)
{
error!(
"Failed signature: {:?} (proof chain: {:?})",
msg, proof_chain
Expand Down Expand Up @@ -201,10 +204,7 @@ impl Message {
return Err(Error::FailedSignature);
}

self.src = SrcAuthority::Section {
signature: proof.signature,
src_name,
};
self.src = SrcAuthority::Section { proof, src_name };

Ok(self)
}
Expand Down Expand Up @@ -240,16 +240,16 @@ impl Message {
}

/// Creates a signed message from a section.
/// Note: `signature` isn't verified and is assumed valid.
/// Note: `proof` isn't verified and is assumed valid.
pub(crate) fn section_src(
plain: PlainMessage,
signature: bls::Signature,
proof: Proof,
proof_chain: SectionChain,
) -> Result<Self, CreateError> {
Self::new_signed(
SrcAuthority::Section {
src_name: plain.src,
signature,
proof,
},
plain.dst,
plain.variant,
Expand Down Expand Up @@ -304,15 +304,15 @@ impl Message {
Ok(VerifyStatus::Unknown)
}
}
SrcAuthority::Section { signature, .. } => {
SrcAuthority::Section { proof, .. } => {
// Proof chain is required for section-src messages.
let proof_chain = if let Some(proof_chain) = self.proof_chain.as_ref() {
proof_chain
} else {
return Err(Error::InvalidMessage);
};

if !proof_chain.last_key().verify(signature, &bytes) {
if !proof_chain.last_key().verify(&proof.signature, &bytes) {
return Err(Error::FailedSignature);
}

Expand All @@ -325,6 +325,15 @@ impl Message {
}
}

/// Getter
pub fn proof(&self) -> Option<Proof> {
if let SrcAuthority::Section { proof, .. } = &self.src {
Some(proof.clone())
} else {
None
}
}

/// Getter
pub fn dst(&self) -> &DstLocation {
&self.dst
Expand Down
6 changes: 3 additions & 3 deletions src/messages/src_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
error::{Error, Result},
peer::Peer,
};
use bls_signature_aggregator::ProofShare;
use bls_signature_aggregator::{Proof, ProofShare};
use serde::{Deserialize, Serialize};
use sn_messaging::SrcLocation;
use std::net::SocketAddr;
Expand Down Expand Up @@ -43,8 +43,8 @@ pub enum SrcAuthority {
Section {
/// Name in the source section.
src_name: XorName,
/// BLS signature of the message corresponding to the source section public key.
signature: bls::Signature,
/// BLS proof of the message corresponding to the source section.
proof: Proof,
},
}

Expand Down
3 changes: 2 additions & 1 deletion src/routing/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,7 @@ impl Core {
content,
src: msg.src().src_location(),
dst: *msg.dst(),
proof: msg.proof(),
proof_chain: msg.proof_chain().ok().cloned(),
});
Ok(vec![])
Expand Down Expand Up @@ -1802,7 +1803,7 @@ impl Core {
proof_chain: SectionChain,
proof: Proof,
) -> Result<Command> {
let message = Message::section_src(message, proof.signature, proof_chain)?;
let message = Message::section_src(message, proof, proof_chain)?;

Ok(Command::HandleMessage {
message,
Expand Down
27 changes: 24 additions & 3 deletions src/routing/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,14 @@ async fn receive_join_request_from_relocated_node() -> Result<()> {
.secret_key()
.sign(&bincode::serialize(&relocate_message.as_signable())?);
let proof_chain = SectionChain::new(section_key);
let relocate_message = Message::section_src(relocate_message, signature, proof_chain)?;
let relocate_message = Message::section_src(
relocate_message,
Proof {
public_key: section_key,
signature,
},
proof_chain,
)?;
let relocate_details = SignedRelocateDetails::new(relocate_message)?;
let relocate_payload = RelocatePayload::new(
relocate_details,
Expand Down Expand Up @@ -976,7 +983,14 @@ async fn handle_untrusted_message(source: UntrustedMessageSource) -> Result<()>
variant: Variant::UserMessage(Bytes::from_static(b"hello")),
};
let signature = sk1.sign(&bincode::serialize(&message.as_signable())?);
let original_message = Message::section_src(message, signature, SectionChain::new(pk1))?;
let original_message = Message::section_src(
message,
Proof {
public_key: pk1,
signature,
},
SectionChain::new(pk1),
)?;

let commands = dispatcher
.handle_command(Command::HandleMessage {
Expand Down Expand Up @@ -1148,7 +1162,14 @@ async fn handle_bounced_untrusted_message() -> Result<()> {
.secret_key()
.sign(&bincode::serialize(&original_message.as_signable())?);
let proof_chain = chain.truncate(1);
let original_message = Message::section_src(original_message, signature, proof_chain)?;
let original_message = Message::section_src(
original_message,
Proof {
public_key: pk1,
signature,
},
proof_chain,
)?;

// Create our node.
let state = Core::new(
Expand Down

0 comments on commit 8a39aaa

Please sign in to comment.