Skip to content

Commit

Permalink
Improve error logging in case of p2p parsing error
Browse files Browse the repository at this point in the history
  • Loading branch information
romanz committed Nov 19, 2022
1 parent 83dd3dd commit 3cdff3d
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/p2p.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bitcoin::{
encode::{self, ReadExt, VarInt},
Decodable,
},
hashes::Hash,
hashes::{hex::ToHex, Hash},
network::{
address, constants,
message::{self, CommandString, NetworkMessage},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -338,7 +340,7 @@ struct RawNetworkMessage {
}

impl RawNetworkMessage {
fn parse(self) -> Result<NetworkMessage, encode::Error> {
fn parse(&self) -> Result<NetworkMessage> {
let mut raw: &[u8] = &self.raw;
let payload = match self.cmd.as_ref() {
"version" => NetworkMessage::Version(Decodable::consensus_decode(&mut raw)?),
Expand All @@ -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)
}
Expand Down

0 comments on commit 3cdff3d

Please sign in to comment.