From 3cdff3dcb27ccb33192c83d60248377e9c3cb794 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sat, 19 Nov 2022 21:03:40 +0200 Subject: [PATCH] Improve error logging in case of p2p parsing error Following https://github.com/rootzoll/raspiblitz/issues/3447 --- src/p2p.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/p2p.rs b/src/p2p.rs index 12dcc25..19e3af7 100644 --- a/src/p2p.rs +++ b/src/p2p.rs @@ -4,7 +4,7 @@ use bitcoin::{ encode::{self, ReadExt, VarInt}, Decodable, }, - hashes::Hash, + hashes::{hex::ToHex, Hash}, network::{ address, constants, message::{self, CommandString, NetworkMessage}, @@ -248,8 +248,10 @@ impl Connection { }; let label = format!("parse_{}", raw_msg.cmd.as_ref()); - let msg = parse_duration - .observe_duration(&label, || raw_msg.parse().expect("invalid message")); + let msg = match parse_duration.observe_duration(&label, || raw_msg.parse()) { + Ok(msg) => msg, + Err(err) => bail!("failed to parse '{}({})': {}", raw_msg.cmd, raw_msg.raw.to_hex(), err), + }; trace!("recv: {:?}", msg); match msg { @@ -338,7 +340,7 @@ struct RawNetworkMessage { } impl RawNetworkMessage { - fn parse(self) -> Result { + fn parse(&self) -> Result { let mut raw: &[u8] = &self.raw; let payload = match self.cmd.as_ref() { "version" => NetworkMessage::Version(Decodable::consensus_decode(&mut raw)?), @@ -359,10 +361,11 @@ impl RawNetworkMessage { "reject" => NetworkMessage::Reject(Decodable::consensus_decode(&mut raw)?), "alert" => NetworkMessage::Alert(Decodable::consensus_decode(&mut raw)?), "addr" => NetworkMessage::Addr(Decodable::consensus_decode(&mut raw)?), - _ => NetworkMessage::Unknown { - command: self.cmd, - payload: self.raw, - }, + _ => bail!( + "unsupported message: command={}, payload={}", + self.cmd, + self.raw.to_hex() + ), }; Ok(payload) }