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

Commit

Permalink
feat: notify client of incorrect section_key
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Feb 16, 2021
1 parent fab1a93 commit c54f034
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ lru_time_cache = "~0.11.0"
qp2p = "~0.9.16"
rand = "~0.7.3"
rand_chacha = "~0.2.2"
sn_messaging = "3.0.0"
sn_messaging = { path= "../sn_messaging" }
thiserror = "1.0.23"
xor_name = "1.1.0"
resource_proof = "0.8.0"
Expand Down
11 changes: 11 additions & 0 deletions src/consensus/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ impl DkgVoter {
}
}

// Checks whether there is any on-going DKG session.
pub fn has_ongoing_dkg(&self) -> bool {
self.sessions
.values()
.any(|session| !session.is_finalized())
}

// Handle a received DkgMessage.
pub fn process_message(
&mut self,
Expand Down Expand Up @@ -370,6 +377,10 @@ impl Session {
}
}

fn is_finalized(&self) -> bool {
self.key_gen.is_finalized()
}

fn report_failure(&mut self, dkg_key: &DkgKey, keypair: &Keypair) -> Vec<DkgCommand> {
let proof = DkgFailureProof::new(keypair, dkg_key);

Expand Down
22 changes: 22 additions & 0 deletions src/routing/approved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use ed25519_dalek::Verifier;
use itertools::Itertools;
use resource_proof::ResourceProof;
use sn_messaging::{
client::Error as ClientError,
infrastructure::{GetSectionResponse, Query},
node::NodeMessage,
MessageType,
Expand Down Expand Up @@ -246,6 +247,10 @@ impl Approved {

vec![]
}
Query::SectionKeyResponse(_) => {
error!("Shall not receive an error response to client");
vec![]
}
}
}

Expand Down Expand Up @@ -1834,6 +1839,23 @@ impl Approved {
Ok(Some(command))
}

pub fn check_key_status(&self, bls_pk: &bls::PublicKey) -> Result<(), ClientError> {
if self.dkg_voter.has_ongoing_dkg() {
return Err(ClientError::DkgInProgress);
}
if !self.section.chain().has_key(bls_pk) {
return Err(ClientError::UnrecognizedSectionKey);
}
if bls_pk != self.section.chain().last_key() {
if let Ok(public_key_set) = self.public_key_set() {
return Err(ClientError::TargetSectionKeyIsNotCurrent(public_key_set));
} else {
return Err(ClientError::DkgInProgress);
}
}
Ok(())
}

// Setting the JoinsAllowed triggers a round Vote::SetJoinsAllowed to update the flag.
pub fn set_joins_allowed(&mut self, joins_allowed: bool) -> Result<Vec<Command>> {
let mut commands = Vec::new();
Expand Down
20 changes: 19 additions & 1 deletion src/routing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ use crate::{
use bytes::Bytes;
use ed25519_dalek::{Keypair, PublicKey, Signature, Signer};
use itertools::Itertools;
use sn_messaging::{client::MsgEnvelope, node::NodeMessage, MessageType, WireMsg};
use sn_messaging::{
client::MsgEnvelope, infrastructure::Query, node::NodeMessage, MessageType, WireMsg,
};
use std::{net::SocketAddr, sync::Arc};
use tokio::{sync::mpsc, task};
use xor_name::{Prefix, XorName};
Expand Down Expand Up @@ -408,6 +410,22 @@ async fn handle_message(stage: Arc<Stage>, bytes: Bytes, sender: SocketAddr) {
}
}
MessageType::ClientMessage(msg_envelope) => {
if let Some(client_pk) = msg_envelope.message.target_section_pk() {
if let Some(bls_pk) = client_pk.bls() {
if let Err(error) = stage.check_key_status(&bls_pk).await {
let command = Command::SendMessage {
recipients: vec![sender],
delivery_group_size: 1,
message: MessageType::InfrastructureQuery(Query::SectionKeyResponse(
error,
)),
};
let _ = task::spawn(stage.handle_commands(command));
return;
}
}
}

let event = Event::ClientMessageReceived {
content: Box::new(msg_envelope),
src: sender,
Expand Down
6 changes: 5 additions & 1 deletion src/routing/stage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use super::{bootstrap, Approved, Comm, Command};
use crate::{error::Result, event::Event, relocation::SignedRelocateDetails};
use sn_messaging::MessageType;
use sn_messaging::{client::Error as ClientError, MessageType};
use std::{net::SocketAddr, sync::Arc, time::Duration};
use tokio::{
sync::{mpsc, watch, Mutex},
Expand Down Expand Up @@ -175,6 +175,10 @@ impl Stage {
let _ = tokio::spawn(self.handle_commands(command));
}

pub async fn check_key_status(&self, bls_pk: &bls::PublicKey) -> Result<(), ClientError> {
self.state.lock().await.check_key_status(bls_pk)
}

async fn send_message(
&self,
recipients: &[SocketAddr],
Expand Down
1 change: 1 addition & 0 deletions tests/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async fn test_messages_client_node() -> Result<()> {
let message = Message::Query {
query: Query::Transfer(TransferQuery::GetBalance(pk)),
id,
target_section_pk: None,
};

let msg_envelope = MsgEnvelope {
Expand Down

0 comments on commit c54f034

Please sign in to comment.