1
0
mirror of https://github.com/romanz/electrs.git synced 2024-11-19 09:54:09 +01:00

Improve error logging in case of p2p parsing error

Following https://github.com/rootzoll/raspiblitz/issues/3447
This commit is contained in:
Roman Zeyde 2022-11-19 21:03:40 +02:00
parent 83dd3dd509
commit 3cdff3dcb2
No known key found for this signature in database
GPG Key ID: 87CAE5FA46917CBB

View File

@ -4,7 +4,7 @@ use bitcoin::{
encode::{self, ReadExt, VarInt}, encode::{self, ReadExt, VarInt},
Decodable, Decodable,
}, },
hashes::Hash, hashes::{hex::ToHex, Hash},
network::{ network::{
address, constants, address, constants,
message::{self, CommandString, NetworkMessage}, message::{self, CommandString, NetworkMessage},
@ -248,8 +248,10 @@ impl Connection {
}; };
let label = format!("parse_{}", raw_msg.cmd.as_ref()); let label = format!("parse_{}", raw_msg.cmd.as_ref());
let msg = parse_duration let msg = match parse_duration.observe_duration(&label, || raw_msg.parse()) {
.observe_duration(&label, || raw_msg.parse().expect("invalid message")); Ok(msg) => msg,
Err(err) => bail!("failed to parse '{}({})': {}", raw_msg.cmd, raw_msg.raw.to_hex(), err),
};
trace!("recv: {:?}", msg); trace!("recv: {:?}", msg);
match msg { match msg {
@ -338,7 +340,7 @@ struct RawNetworkMessage {
} }
impl RawNetworkMessage { impl RawNetworkMessage {
fn parse(self) -> Result<NetworkMessage, encode::Error> { fn parse(&self) -> Result<NetworkMessage> {
let mut raw: &[u8] = &self.raw; let mut raw: &[u8] = &self.raw;
let payload = match self.cmd.as_ref() { let payload = match self.cmd.as_ref() {
"version" => NetworkMessage::Version(Decodable::consensus_decode(&mut raw)?), "version" => NetworkMessage::Version(Decodable::consensus_decode(&mut raw)?),
@ -359,10 +361,11 @@ impl RawNetworkMessage {
"reject" => NetworkMessage::Reject(Decodable::consensus_decode(&mut raw)?), "reject" => NetworkMessage::Reject(Decodable::consensus_decode(&mut raw)?),
"alert" => NetworkMessage::Alert(Decodable::consensus_decode(&mut raw)?), "alert" => NetworkMessage::Alert(Decodable::consensus_decode(&mut raw)?),
"addr" => NetworkMessage::Addr(Decodable::consensus_decode(&mut raw)?), "addr" => NetworkMessage::Addr(Decodable::consensus_decode(&mut raw)?),
_ => NetworkMessage::Unknown { _ => bail!(
command: self.cmd, "unsupported message: command={}, payload={}",
payload: self.raw, self.cmd,
}, self.raw.to_hex()
),
}; };
Ok(payload) Ok(payload)
} }