Skip to content

Commit

Permalink
add debug logs for invalid messages
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed Aug 28, 2024
1 parent ecf9cea commit 1d9d874
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
16 changes: 7 additions & 9 deletions mm2src/mm2_main/src/lp_healthcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ use std::str::FromStr;

use crate::lp_network::broadcast_p2p_msg;

pub const PEER_HEALTHCHECK_PREFIX: TopicPrefix = "hcheck";
pub(crate) const PEER_HEALTHCHECK_PREFIX: TopicPrefix = "hcheck";

#[derive(Deserialize, Serialize)]
pub(crate) struct HealthCheckMessage {
#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct HealthcheckMessage {
signature: Vec<u8>,
data: HealthCheckData,
}

impl HealthCheckMessage {
pub fn generate_message(
impl HealthcheckMessage {
pub(crate) fn generate_message(
ctx: &MmArc,
target_peer: PeerId,
is_a_reply: bool,
Expand Down Expand Up @@ -73,11 +73,9 @@ impl HealthCheckMessage {
pub(crate) fn should_reply(&self) -> bool { !self.data.is_a_reply }

pub(crate) fn sender_peer(&self) -> &str { &self.data.sender_peer }

pub(crate) fn target_peer(&self) -> &str { &self.data.target_peer }
}

#[derive(Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize)]
struct HealthCheckData {
sender_peer: String,
sender_public_key: Vec<u8>,
Expand Down Expand Up @@ -116,7 +114,7 @@ pub async fn peer_connection_healthcheck_rpc(ctx: MmArc, req: RequestPayload) ->
const RESULT_CHANNEL_TIMEOUT: Duration = Duration::from_secs(10);

let target_peer_id = PeerId::from_str(&req.peer_id).unwrap();
let msg = HealthCheckMessage::generate_message(&ctx, target_peer_id, false, 10).unwrap();
let msg = HealthcheckMessage::generate_message(&ctx, target_peer_id, false, 10).unwrap();

let (tx, rx): (Sender<()>, Receiver<()>) = oneshot::channel();
{
Expand Down
13 changes: 9 additions & 4 deletions mm2src/mm2_main/src/lp_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use serde::de;
use std::net::ToSocketAddrs;
use std::str::FromStr;

use crate::lp_healthcheck::{peer_healthcheck_topic, HealthCheckMessage};
use crate::lp_healthcheck::{peer_healthcheck_topic, HealthcheckMessage};
use crate::{lp_healthcheck, lp_ordermatch, lp_stats, lp_swap};

pub type P2PRequestResult<T> = Result<T, MmError<P2PRequestError>>;
Expand Down Expand Up @@ -220,14 +220,19 @@ async fn process_p2p_message(
Some(lp_healthcheck::PEER_HEALTHCHECK_PREFIX) => {
let p2p_ctx = P2PContext::fetch_from_mm_arc(&ctx);

let data = HealthCheckMessage::decode(&message.data).expect("!!TODO");
assert!(data.is_received_message_valid(p2p_ctx.peer_id()), "must ve valid, TODO");
let data = HealthcheckMessage::decode(&message.data).expect("!!TODO");

if !data.is_received_message_valid(p2p_ctx.peer_id()) {
log::error!("Received an invalid healthcheck message.");
log::debug!("Message context: {:?}", data);
return;
};

if data.should_reply() {
// Reply the message so they know we are healthy.
let target_peer_id = PeerId::from_str(data.sender_peer()).expect("!!!! TODO");
let topic = peer_healthcheck_topic(&target_peer_id);
let msg = HealthCheckMessage::generate_message(&ctx, target_peer_id, true, 10).unwrap();
let msg = HealthcheckMessage::generate_message(&ctx, target_peer_id, true, 10).unwrap();
broadcast_p2p_msg(&ctx, topic, msg.encode().unwrap(), None);
} else {
// The requested peer is healthy; signal the result channel.
Expand Down

0 comments on commit 1d9d874

Please sign in to comment.