Skip to content

Commit 2dd1d47

Browse files
committed
tweak(p2p): Use thiserror when defining pubsub errors
1 parent 48fcaec commit 2dd1d47

File tree

1 file changed

+7
-29
lines changed

1 file changed

+7
-29
lines changed

p2p/src/network/pubsub/pubsub_effectful/p2p_network_pubsub_effectful_effects.rs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use openmina_core::bug_condition;
2-
31
use libp2p_identity::{DecodingError, PublicKey};
42

53
use super::super::pb;
@@ -10,17 +8,17 @@ use crate::{
108

119
use super::P2pNetworkPubsubEffectfulAction;
1210

13-
#[derive(Debug)]
11+
#[derive(Debug, thiserror::Error)]
1412
pub enum PubSubError {
15-
/// The message does not contain a signature.
13+
#[error("Message does not contain a signature.")]
1614
MissingSignature,
17-
/// The message does not contain a verifying key.
15+
#[error("Message does not contain a verifying key.")]
1816
MissingVerifyingKey,
19-
/// Failed to retrieve the originator's public key.
17+
#[error("Failed to retrieve the originator's public key.")]
2018
OriginatorFailed,
21-
/// Serialization of the message without signature and key failed.
19+
#[error("Message serialization failed.")]
2220
SerializationError,
23-
/// The message's signature is invalid.
21+
#[error("Message's signature is invalid.")]
2422
InvalidSignature,
2523
}
2624

@@ -56,29 +54,9 @@ impl P2pNetworkPubsubEffectfulAction {
5654
match validate_message(message, store) {
5755
Ok(valid_msg) => valid_messages.push(valid_msg),
5856
Err(error) => {
59-
let error_msg = match error {
60-
PubSubError::MissingSignature => {
61-
"message doesn't contain signature"
62-
}
63-
PubSubError::MissingVerifyingKey => {
64-
"message doesn't contain verifying key"
65-
}
66-
PubSubError::OriginatorFailed => "originator function failed",
67-
PubSubError::InvalidSignature => "invalid signature",
68-
PubSubError::SerializationError => {
69-
// Should never happen;
70-
// We just decoded this message, so it should encode
71-
// without errors.
72-
bug_condition!("serialization error");
73-
"serialization error"
74-
}
75-
};
76-
7757
store.dispatch(P2pNetworkSchedulerAction::Error {
7858
addr,
79-
error: P2pNetworkConnectionError::PubSubError(
80-
error_msg.to_string(),
81-
),
59+
error: P2pNetworkConnectionError::PubSubError(error.to_string()),
8260
});
8361

8462
return; // Early exit, no need to process the rest

0 commit comments

Comments
 (0)