network: add PeerError::WrongMessage

This lets us distinguish between cases where the message was unsupported
(e.g., BIP11 messages), and cases where the message was uninterpretable
in context (e.g., unsolicited messages).
This commit is contained in:
Henry de Valence 2020-09-18 20:43:24 -07:00
parent 430176dd0d
commit 3c993f33b1
2 changed files with 16 additions and 20 deletions

View File

@ -504,36 +504,32 @@ where
// could be a response, so if we see them here, they were either
// sent unsolicited, or we've failed to handle messages correctly.
Message::Reject { .. } => {
trace!("rejecting unsolicited reject message");
self.fail_with(PeerError::UnsupportedMessage);
self.fail_with(PeerError::WrongMessage("unsolicited reject message"));
return;
}
Message::NotFound { .. } => {
trace!("rejecting unsolicited notfound message");
self.fail_with(PeerError::UnsupportedMessage);
self.fail_with(PeerError::WrongMessage("unsolicited notfound message"));
return;
}
Message::Pong(_) => {
trace!("rejecting unsolicited pong message");
self.fail_with(PeerError::UnsupportedMessage);
self.fail_with(PeerError::WrongMessage("unsolicited pong message"));
return;
}
Message::Block(_) => {
trace!("rejecting unsolicited block message");
self.fail_with(PeerError::UnsupportedMessage);
self.fail_with(PeerError::WrongMessage("unsolicited block message"));
return;
}
Message::Headers(_) => {
trace!("rejecting unsolicited headers message");
self.fail_with(PeerError::UnsupportedMessage);
self.fail_with(PeerError::WrongMessage("unsolicited headers message"));
return;
}
// These messages should never be sent by peers.
Message::FilterLoad { .. }
| Message::FilterAdd { .. }
| Message::FilterClear { .. } => {
trace!("got BIP11 message without NODE_BLOOM");
self.fail_with(PeerError::UnsupportedMessage);
self.fail_with(PeerError::UnsupportedMessage(
"got BIP11 message without advertising NODE_BLOOM",
));
return;
}
// Zebra crawls the network proactively, to prevent
@ -553,8 +549,7 @@ where
Request::TransactionsByHash(transaction_hashes(&items))
}
_ => {
debug!(?items, "ignoring unrecognized inv message");
self.fail_with(PeerError::UnsupportedMessage);
self.fail_with(PeerError::WrongMessage("inv with mixed item types"));
return;
}
},
@ -572,8 +567,7 @@ where
Request::TransactionsByHash(transaction_hashes(&items))
}
_ => {
trace!(?items, "ignoring getdata with mixed item types");
self.fail_with(PeerError::UnsupportedMessage);
self.fail_with(PeerError::WrongMessage("getdata with mixed item types"));
return;
}
},

View File

@ -40,10 +40,12 @@ pub enum PeerError {
/// to shed load.
#[error("Internal services over capacity")]
Overloaded,
/// A peer sent us a message we don't support or instructed to
/// disconnect from upon receipt.
#[error("Remote peer sent an unsupported message type.")]
UnsupportedMessage,
/// A peer sent us a message we don't support.
#[error("Remote peer sent an unsupported message type: {0}")]
UnsupportedMessage(&'static str),
/// A peer sent us a message we couldn't interpret in context.
#[error("Remote peer sent an uninterpretable message: {0}")]
WrongMessage(&'static str),
/// We got a `Reject` message. This does not necessarily mean that
/// the peer connection is in a bad state, but for the time being
/// we are considering it a PeerError.