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

Commit

Permalink
chore: make Peer contains the age info
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Oct 7, 2020
1 parent c63331f commit 783961f
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 31 deletions.
7 changes: 6 additions & 1 deletion src/messages/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl Message {
SrcAuthority::Node {
public_key,
signature,
..
} => {
if public_key.verify(&signed_bytes, signature).is_err() {
error!("Failed signature: {:?}", msg);
Expand Down Expand Up @@ -123,6 +124,7 @@ impl Message {
/// Creates a signed message from single node.
pub(crate) fn single_src(
keypair: &Keypair,
age: u8,
dst: DstLocation,
variant: Variant,
proof_chain: Option<SectionProofChain>,
Expand All @@ -136,6 +138,7 @@ impl Message {
let signature = crypto::sign(&serialized, keypair);
let src = SrcAuthority::Node {
public_key: keypair.public,
age,
signature,
};

Expand Down Expand Up @@ -179,6 +182,7 @@ impl Message {
SrcAuthority::Node {
public_key,
signature,
..
} => {
if public_key.verify(&bytes, signature).is_err() {
return Err(Error::FailedSignature);
Expand Down Expand Up @@ -357,7 +361,7 @@ pub(crate) struct SignableView<'a> {
#[cfg(test)]
mod tests {
use super::*;
use crate::{consensus, crypto::Keypair, rng, section};
use crate::{consensus, crypto::Keypair, rng, section, MIN_AGE};
use std::iter;

#[test]
Expand All @@ -382,6 +386,7 @@ mod tests {
let variant = Variant::NodeApproval(elders_info);
let message = Message::single_src(
&keypair,
MIN_AGE,
DstLocation::Direct,
variant,
Some(full_proof_chain.slice(1..)),
Expand Down
12 changes: 8 additions & 4 deletions src/messages/src_authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub enum SrcAuthority {
Node {
/// Public key of the source peer.
public_key: PublicKey,
/// Age of the source peer.
age: u8,
/// ed-25519 signature of the message corresponding to the public key of the source peer.
signature: SimpleSignature,
},
Expand Down Expand Up @@ -60,9 +62,11 @@ impl SrcAuthority {
matches!(self, Self::Section { .. })
}

pub(crate) fn as_node(&self) -> Result<XorName> {
pub(crate) fn as_node(&self) -> Result<(XorName, u8)> {
match self {
Self::Node { public_key, .. } => Ok(name(public_key)),
Self::Node {
public_key, age, ..
} => Ok((name(public_key), *age)),
Self::Section { .. } => Err(Error::BadLocation),
}
}
Expand All @@ -76,8 +80,8 @@ impl SrcAuthority {
}

pub(crate) fn to_sender_node(&self, sender: Option<SocketAddr>) -> Result<Peer> {
let name = self.as_node()?;
let (name, age) = self.as_node()?;
let conn_info = sender.ok_or(Error::InvalidSource)?;
Ok(Peer::new(name, conn_info))
Ok(Peer::new(name, conn_info, age))
}
}
37 changes: 31 additions & 6 deletions src/node/stage/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl Approved {
let proof_chain = self.shared_state.create_proof_chain_for_our_info(None);
let message = Message::single_src(
&self.node_info.keypair,
self.age(),
DstLocation::Direct,
variant,
Some(proof_chain),
Expand Down Expand Up @@ -453,12 +454,12 @@ impl Approved {
Ok(None)
}
Variant::DKGMessage { dkg_key, message } => {
self.handle_dkg_message(*dkg_key, message.clone(), msg.src().as_node()?)
self.handle_dkg_message(*dkg_key, message.clone(), msg.src().as_node()?.0)
.await?;
Ok(None)
}
Variant::DKGResult { dkg_key, result } => {
self.handle_dkg_result(*dkg_key, *result, msg.src().as_node()?)
self.handle_dkg_result(*dkg_key, *result, msg.src().as_node()?.0)
.await?;

Ok(None)
Expand Down Expand Up @@ -523,6 +524,7 @@ impl Approved {

let bounce_msg = Message::single_src(
&self.node_info.keypair,
self.age(),
bounce_dst,
Variant::BouncedUntrustedMessage(Box::new(msg)),
None,
Expand All @@ -547,6 +549,7 @@ impl Approved {
) -> Result<()> {
let bounce_msg = Message::single_src(
&self.node_info.keypair,
self.age(),
DstLocation::Direct,
Variant::BouncedUnknownMessage {
src_key: *self.shared_state.our_history.last_key(),
Expand Down Expand Up @@ -871,8 +874,11 @@ impl Approved {
(MIN_AGE, None, None)
};

let mut updated_peer = peer;
updated_peer.age = age;

self.vote(Vote::Online {
member_info: MemberInfo::joined(peer, age),
member_info: MemberInfo::joined(updated_peer),
previous_name,
their_knowledge,
})
Expand Down Expand Up @@ -1155,7 +1161,7 @@ impl Approved {
proof: Proof,
) -> Result<()> {
let peer = member_info.peer;
let age = member_info.age;
let age = peer.age;
let signature = proof.signature.clone();

if !self.shared_state.update_member(member_info, proof) {
Expand Down Expand Up @@ -1187,7 +1193,7 @@ impl Approved {

async fn handle_offline_event(&mut self, member_info: MemberInfo, proof: Proof) -> Result<()> {
let peer = member_info.peer;
let age = member_info.age;
let age = peer.age;
let signature = proof.signature.clone();

if !self.shared_state.update_member(member_info, proof) {
Expand Down Expand Up @@ -1496,6 +1502,7 @@ impl Approved {
trace!("Send {:?} to {:?}", variant, peer);
let message = Message::single_src(
&self.node_info.keypair,
self.age(),
DstLocation::Direct,
variant,
Some(proof_chain),
Expand All @@ -1522,6 +1529,7 @@ impl Approved {
trace!("Send {:?} to {:?}", variant, peer);
let message = Message::single_src(
&self.node_info.keypair,
self.age(),
DstLocation::Direct,
variant,
None,
Expand Down Expand Up @@ -1621,6 +1629,7 @@ impl Approved {
let recipients: Vec<_> = recipients.map(Peer::addr).copied().collect();
let message = Message::single_src(
&self.node_info.keypair,
self.age(),
DstLocation::Direct,
variant,
None,
Expand All @@ -1646,6 +1655,7 @@ impl Approved {
};
let message = Message::single_src(
&self.node_info.keypair,
self.age(),
DstLocation::Direct,
variant,
None,
Expand Down Expand Up @@ -1722,7 +1732,14 @@ impl Approved {
match src {
SrcLocation::Node(_) => {
// If the source is a single node, we don't even need to vote, so let's cut this short.
let msg = Message::single_src(&self.node_info.keypair, dst, variant, None, None)?;
let msg = Message::single_src(
&self.node_info.keypair,
self.age(),
dst,
variant,
None,
None,
)?;
self.relay_message(&msg).await
}
SrcLocation::Section(_) => {
Expand Down Expand Up @@ -1790,6 +1807,7 @@ impl Approved {
) -> Result<()> {
let message = Message::single_src(
&self.node_info.keypair,
self.age(),
DstLocation::Direct,
variant,
None,
Expand Down Expand Up @@ -1912,6 +1930,7 @@ impl Approved {
trace!("sending NeighbourInfo {:?}", variant);
let msg = Message::single_src(
&self.node_info.keypair,
self.age(),
DstLocation::Section(dst.name()),
variant,
Some(proof_chain),
Expand All @@ -1924,6 +1943,12 @@ impl Approved {
fn print_network_stats(&self) {
self.shared_state.sections.network_stats().print()
}

fn age(&self) -> u8 {
self.shared_state
.find_age_for_peer(&self.node_info.name())
.unwrap_or(MIN_AGE)
}
}

pub(crate) struct RelocateParams {
Expand Down
3 changes: 2 additions & 1 deletion src/node/stage/bootstrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
relocation::{RelocatePayload, SignedRelocateDetails},
section::EldersInfo,
timer::Timer,
DstLocation,
DstLocation, MIN_AGE,
};
use futures::future;
use std::{iter, net::SocketAddr, sync::Arc};
Expand Down Expand Up @@ -162,6 +162,7 @@ impl Bootstrapping {

let message = Message::single_src(
&self.node_info.keypair,
MIN_AGE,
DstLocation::Direct,
Variant::BootstrapRequest(destination),
None,
Expand Down
3 changes: 2 additions & 1 deletion src/node/stage/joining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::{
relocation::RelocatePayload,
section::{EldersInfo, SharedState},
timer::Timer,
DstLocation,
DstLocation, MIN_AGE,
};
use std::{net::SocketAddr, time::Duration};
use xor_name::Prefix;
Expand Down Expand Up @@ -235,6 +235,7 @@ impl Joining {
let variant = Variant::JoinRequest(Box::new(join_request));
let message = Message::single_src(
&self.node_info.keypair,
MIN_AGE,
DstLocation::Direct,
variant,
None,
Expand Down
4 changes: 2 additions & 2 deletions src/node/stage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Stage {
)> {
let comm = Comm::new(transport_config)?;
let connection_info = comm.our_connection_info()?;
let peer = Peer::new(name(&keypair.public), connection_info);
let peer = Peer::new(name(&keypair.public), connection_info, MIN_AGE);

let mut rng = MainRng::default();
let secret_key_set = consensus::generate_secret_key_set(&mut rng, 1);
Expand Down Expand Up @@ -384,7 +384,7 @@ fn create_first_shared_state(
);

for peer in shared_state.sections.our().elders.values() {
let member_info = MemberInfo::joined(*peer, MIN_AGE);
let member_info = MemberInfo::joined(*peer);
let proof = create_first_proof(pk_set, sk_share, &member_info)?;
let _ = shared_state
.our_members
Expand Down
19 changes: 14 additions & 5 deletions src/peer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ use xor_name::XorName;
/// from being connected at the network layer, which currently is handled by quic-p2p.
#[derive(Clone, Copy, Debug, Hash, PartialEq, PartialOrd, Ord, Eq, Serialize, Deserialize)]
pub struct Peer {
pub name: XorName,
pub addr: SocketAddr,
name: XorName,
addr: SocketAddr,
pub age: u8,
}

impl Peer {
/// Creates a new `Peer` given a `Name` and a `ConnectionInfo`.
pub fn new(name: XorName, addr: SocketAddr) -> Self {
Self { name, addr }
/// Creates a new `Peer` given `Name`, `ConnectionInfo` and `age`.
pub fn new(name: XorName, addr: SocketAddr, age: u8) -> Self {
Self { name, addr, age }
}

/// Returns the `XorName` of the peer.
Expand All @@ -34,4 +35,12 @@ impl Peer {
pub fn addr(&self) -> &SocketAddr {
&self.addr
}

// Converts this info into one with the age increased by one.
pub fn increment_age(self) -> Self {
Self {
age: self.age.saturating_add(1),
..self
}
}
}
4 changes: 2 additions & 2 deletions src/section/elders_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub(crate) fn gen_elders_info(
prefix: Prefix,
count: usize,
) -> (EldersInfo, Vec<Keypair>) {
use crate::crypto::name;
use crate::{crypto::name, MIN_AGE};
use rand::Rng;
use std::net::SocketAddr;

Expand All @@ -106,7 +106,7 @@ pub(crate) fn gen_elders_info(
.iter()
.map(|keypair| {
let addr = gen_socket_addr(rng);
let peer = Peer::new(name(&keypair.public), addr);
let peer = Peer::new(name(&keypair.public), addr, MIN_AGE);
(*peer.name(), peer)
})
.collect();
Expand Down
8 changes: 3 additions & 5 deletions src/section/member_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,19 @@ pub const MIN_AGE: u8 = 4;
pub struct MemberInfo {
pub peer: Peer,
pub state: PeerState,
pub age: u8,
}

impl MemberInfo {
// Creates a `MemberInfo` in the `Joined` state.
pub fn joined(peer: Peer, age: u8) -> Self {
pub fn joined(peer: Peer) -> Self {
Self {
peer,
state: PeerState::Joined,
age,
}
}

pub fn is_adult(&self) -> bool {
self.age > MIN_AGE
self.peer.age > MIN_AGE
}

pub fn leave(self) -> Self {
Expand All @@ -53,7 +51,7 @@ impl MemberInfo {
// Converts this info into one with the age increased by one.
pub fn increment_age(self) -> Self {
Self {
age: self.age.saturating_add(1),
peer: self.peer.increment_age(),
..self
}
}
Expand Down

0 comments on commit 783961f

Please sign in to comment.