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

Commit

Permalink
feat: replace (old) SectionProofChain with (new) SectionChain
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Mar 2, 2021
1 parent a3d786f commit 03fb82c
Show file tree
Hide file tree
Showing 16 changed files with 596 additions and 874 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ edition = "2018"
bincode = "1.2.1"
bls_dkg = "~0.3.1"
bls_signature_aggregator = "~0.2.0"
bytes = "~1.0.1"
futures = "~0.3.6"
bytes = "1.0.1"
futures = "~0.3.12"
hex_fmt = "~0.3.0"
itertools = "~0.9.0"
lru_time_cache = "~0.11.0"
Expand Down Expand Up @@ -46,7 +46,7 @@ sn_data_types = "~0.15.0"

[dependencies.tokio]
version = "~0.2.24"
features = [ "sync", "time", "rt-util" ]
features = [ "rt-util", "sync", "time" ]

[dependencies.tracing]
version = "~0.1.22"
Expand Down
6 changes: 3 additions & 3 deletions src/consensus/proven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// 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::{consensus::Proof, section::SectionProofChain};
use crate::{consensus::Proof, section::SectionChain};
use serde::{Deserialize, Serialize};
use std::{borrow::Borrow, fmt::Debug};
use xor_name::Prefix;
Expand All @@ -23,8 +23,8 @@ impl<T: Serialize> Proven<T> {
Self { value, proof }
}

pub fn verify(&self, history: &SectionProofChain) -> bool {
history.has_key(&self.proof.public_key) && self.self_verify()
pub fn verify(&self, section_chain: &SectionChain) -> bool {
section_chain.has_key(&self.proof.public_key) && self.self_verify()
}

pub fn self_verify(&self) -> bool {
Expand Down
14 changes: 7 additions & 7 deletions src/consensus/vote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{Proof, ProofShare, Proven, SignatureAggregator};
use crate::{
error::Result,
messages::PlainMessage,
section::{EldersInfo, MemberInfo, SectionProofChain},
section::{EldersInfo, MemberInfo, SectionChain},
};
use serde::{Deserialize, Serialize, Serializer};
use thiserror::Error;
Expand Down Expand Up @@ -55,13 +55,13 @@ pub(crate) enum Vote {
// Voted to update their knowledge of our section.
TheirKnowledge {
prefix: Prefix,
key_index: u64,
key: bls::PublicKey,
},

// Voted to send an user message whose source is our section.
SendMessage {
message: Box<PlainMessage>,
proof_chain: SectionProofChain,
proof_chain: SectionChain,
},

// Voted to concensus whether new node shall be allowed to join
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<'a> Serialize for SignableView<'a> {
Vote::SectionInfo(info) => info.serialize(serializer),
Vote::OurElders(info) => info.proof.public_key.serialize(serializer),
Vote::TheirKey { prefix, key } => (prefix, key).serialize(serializer),
Vote::TheirKnowledge { prefix, key_index } => (prefix, key_index).serialize(serializer),
Vote::TheirKnowledge { prefix, key } => (prefix, key).serialize(serializer),
Vote::SendMessage { message, .. } => message.as_signable().serialize(serializer),
Vote::JoinsAllowed(joins_allowed) => joins_allowed.serialize(serializer),
}
Expand Down Expand Up @@ -171,9 +171,9 @@ mod tests {

// Vote::TheirKnowledge
let prefix = gen_prefix();
let key_index = rand::random();
let vote = Vote::TheirKnowledge { prefix, key_index };
verify_serialize_for_signing(&vote, &(prefix, key_index));
let key = bls::SecretKey::random().public_key();
let vote = Vote::TheirKnowledge { prefix, key };
verify_serialize_for_signing(&vote, &(prefix, key));

Ok(())
}
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// 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::section::SectionChainError;
use thiserror::Error;

/// The type returned by the sn_routing message handling methods.
Expand Down Expand Up @@ -39,6 +40,8 @@ pub enum Error {
FailedSend,
#[error("Invalid vote.")]
InvalidVote,
#[error("Invalid section chain: {0}")]
InvalidSectionChain(#[from] SectionChainError),
#[error("Messaging protocol error: {0}")]
Messaging(#[from] sn_messaging::Error),
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub use self::{
error::{Error, Result},
event::{Event, NodeElderChange, SendStream},
routing::{Config, EventStream, Routing},
section::{SectionProofChain, MIN_AGE},
section::{SectionChain, SectionChainError, MIN_AGE},
};
pub use qp2p::Config as TransportConfig;

Expand Down
42 changes: 21 additions & 21 deletions src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
crypto::{self, name, Verifier},
error::{Error, Result},
node::Node,
section::{ExtendError, SectionKeyShare, SectionProofChain, TrustStatus},
section::{SectionChain, SectionChainError, SectionKeyShare},
};
use bls_signature_aggregator::ProofShare;
use bytes::Bytes;
Expand All @@ -45,7 +45,7 @@ pub(crate) struct Message {
/// The body of the message.
variant: Variant,
/// Proof chain to verify the message trust. Does not need to be signed.
proof_chain: Option<SectionProofChain>,
proof_chain: Option<SectionChain>,
/// Source's knowledge of the destination section key. If present, the destination can use it
/// to determine the length of the proof of messages sent to the source so the source would
/// trust it (the proof needs to start at this key).
Expand Down Expand Up @@ -112,7 +112,7 @@ impl Message {
src: SrcAuthority,
dst: DstLocation,
variant: Variant,
proof_chain: Option<SectionProofChain>,
proof_chain: Option<SectionChain>,
dst_key: Option<bls::PublicKey>,
) -> Result<Message, CreateError> {
let mut msg = Message {
Expand All @@ -138,7 +138,7 @@ impl Message {
key_share: &SectionKeyShare,
dst: DstLocation,
user_msg: Bytes,
proof_chain: SectionProofChain,
proof_chain: SectionChain,
dst_key: Option<bls::PublicKey>,
src_section: XorName,
) -> Result<Self, CreateError> {
Expand Down Expand Up @@ -177,7 +177,7 @@ impl Message {
node: &Node,
dst: DstLocation,
variant: Variant,
proof_chain: Option<SectionProofChain>,
proof_chain: Option<SectionChain>,
dst_key: Option<bls::PublicKey>,
) -> Result<Self, CreateError> {
let serialized = bincode::serialize(&SignableView {
Expand All @@ -200,7 +200,7 @@ impl Message {
pub(crate) fn section_src(
plain: PlainMessage,
signature: bls::Signature,
proof_chain: SectionProofChain,
proof_chain: SectionChain,
) -> Result<Self, CreateError> {
Self::new_signed(
SrcAuthority::Section {
Expand Down Expand Up @@ -262,7 +262,7 @@ impl Message {
.filter(|(known_prefix, _)| known_prefix.matches(&name(public_key)))
.map(|(_, key)| key);

proof_chain.check_trust(trusted_keys).into()
VerifyStatus::from_section_chain_result(proof_chain.verify(trusted_keys))
}
SrcAuthority::Section { prefix, signature } => {
// Proof chain is required for section-src messages.
Expand All @@ -281,7 +281,7 @@ impl Message {
.filter(|(known_prefix, _)| prefix.is_compatible(known_prefix))
.map(|(_, key)| key);

proof_chain.check_trust(trusted_keys).into()
VerifyStatus::from_section_chain_result(proof_chain.verify(trusted_keys))
}
}
}
Expand Down Expand Up @@ -321,7 +321,7 @@ impl Message {
}

/// Returns the attached proof chain, if any.
pub(crate) fn proof_chain(&self) -> Result<&SectionProofChain> {
pub(crate) fn proof_chain(&self) -> Result<&SectionChain> {
self.proof_chain.as_ref().ok_or(Error::InvalidMessage)
}

Expand All @@ -335,12 +335,12 @@ impl Message {
pub(crate) fn extend_proof_chain(
mut self,
new_first_key: &bls::PublicKey,
full_chain: &SectionProofChain,
full_chain: &SectionChain,
) -> Result<Self, ExtendProofChainError> {
if let Variant::Sync { section, .. } = &mut self.variant {
section.extend_chain(new_first_key, full_chain)?
} else if let Some(proof_chain) = &mut self.proof_chain {
proof_chain.extend(new_first_key, full_chain)?
*proof_chain = proof_chain.extend(new_first_key, full_chain)?
} else {
return Err(ExtendProofChainError::NoProofChain);
}
Expand Down Expand Up @@ -388,12 +388,12 @@ pub enum VerifyStatus {
Unknown,
}

impl Into<Result<VerifyStatus>> for TrustStatus {
fn into(self) -> Result<VerifyStatus> {
match self {
Self::Trusted => Ok(VerifyStatus::Full),
Self::Unknown => Ok(VerifyStatus::Unknown),
Self::Invalid => Err(Error::InvalidMessage),
impl VerifyStatus {
pub(crate) fn from_section_chain_result(result: Result<(), SectionChainError>) -> Result<Self> {
match result {
Ok(()) => Ok(VerifyStatus::Full),
Err(SectionChainError::Untrusted) => Ok(VerifyStatus::Unknown),
Err(_) => Err(Error::InvalidMessage),
}
}
}
Expand Down Expand Up @@ -435,7 +435,7 @@ pub enum ExtendProofChainError {
#[error("message has no proof chain")]
NoProofChain,
#[error("failed to extend proof chain: {}", .0)]
Extend(#[from] ExtendError),
Extend(#[from] SectionChainError),
#[error("failed to re-create message: {}", .0)]
Create(#[from] CreateError),
}
Expand Down Expand Up @@ -471,9 +471,9 @@ mod tests {
let sk1 = bls::SecretKey::random();
let pk1 = sk1.public_key();

let mut full_proof_chain = SectionProofChain::new(sk0.public_key());
let mut full_proof_chain = SectionChain::new(pk0);
let pk1_sig = sk0.sign(&bincode::serialize(&pk1)?);
let _ = full_proof_chain.push(pk1, pk1_sig);
let _ = full_proof_chain.insert(&pk0, pk1, pk1_sig);

let (elders_info, _) = section::test_utils::gen_elders_info(Default::default(), 3);
let elders_info = consensus::test_utils::proven(&sk1, elders_info)?;
Expand All @@ -490,7 +490,7 @@ mod tests {
&node,
DstLocation::Direct,
variant,
Some(full_proof_chain.slice(1..)),
Some(full_proof_chain.truncate(1)),
Some(pk1),
)?;

Expand Down
22 changes: 13 additions & 9 deletions src/messages/variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
error::{Error, Result},
network::Network,
relocation::{RelocateDetails, RelocatePayload, RelocatePromise},
section::{EldersInfo, MemberInfo, Section, SectionProofChain},
section::{EldersInfo, MemberInfo, Section, SectionChain},
};
use bls_dkg::key_gen::message::Message as DkgMessage;
use bytes::Bytes;
Expand Down Expand Up @@ -83,8 +83,10 @@ pub(crate) enum Variant {
dkg_key: DkgKey,
/// The DKG particpants.
elders_info: EldersInfo,
/// The section chain index of the key to be generated.
key_index: u64,
/// A DKG "generation". A DKG with higher generation supersedes a DKG with lower but DKGs
/// with the same generation need to executed in parallel as we can't tell which one "wins"
/// before they complete.
generation: u64,
},
/// Message exchanged for DKG process.
DKGMessage {
Expand Down Expand Up @@ -121,7 +123,7 @@ pub(crate) enum Variant {
impl Variant {
pub(crate) fn verify<'a, I>(
&self,
proof_chain: Option<&SectionProofChain>,
proof_chain: Option<&SectionChain>,
trusted_keys: I,
) -> Result<VerifyStatus>
where
Expand All @@ -142,17 +144,19 @@ impl Variant {
return Err(Error::InvalidMessage);
}

proof_chain.check_trust(trusted_keys).into()
VerifyStatus::from_section_chain_result(proof_chain.verify(trusted_keys))
}
Self::Sync { section, .. } => {
VerifyStatus::from_section_chain_result(section.chain().verify(trusted_keys))
}
Self::Sync { section, .. } => section.chain().check_trust(trusted_keys).into(),
Self::NeighbourInfo { elders_info, .. } => {
let proof_chain = proof_chain.ok_or(Error::InvalidMessage)?;

if !elders_info.verify(proof_chain) {
return Err(Error::InvalidMessage);
}

proof_chain.check_trust(trusted_keys).into()
VerifyStatus::from_section_chain_result(proof_chain.verify(trusted_keys))
}
_ => Ok(VerifyStatus::Full),
}
Expand Down Expand Up @@ -204,12 +208,12 @@ impl Debug for Variant {
Self::DKGStart {
dkg_key,
elders_info,
key_index,
generation,
} => f
.debug_struct("DKGStart")
.field("dkg_key", dkg_key)
.field("elders_info", elders_info)
.field("key_index", key_index)
.field("generation", generation)
.finish(),
Self::DKGMessage { dkg_key, message } => f
.debug_struct("DKGMessage")
Expand Down

0 comments on commit 03fb82c

Please sign in to comment.