Skip to content

Commit

Permalink
refactor(gossipsub): remove duplicated call to inbound_transform
Browse files Browse the repository at this point in the history
May close #4369.

Pull-Request: #5767.
  • Loading branch information
drHuangMHT authored Jan 16, 2025
1 parent 7c36d30 commit cc8607c
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,8 @@ where
);
self.handle_invalid_message(
propagation_source,
raw_message,
&raw_message.topic,
Some(msg_id),
RejectReason::BlackListedSource,
);
return false;
Expand Down Expand Up @@ -1713,7 +1714,12 @@ where
source=%propagation_source,
"Dropping message claiming to be from self but forwarded from source"
);
self.handle_invalid_message(propagation_source, raw_message, RejectReason::SelfOrigin);
self.handle_invalid_message(
propagation_source,
&raw_message.topic,
Some(msg_id),
RejectReason::SelfOrigin,
);
return false;
}

Expand Down Expand Up @@ -1741,7 +1747,8 @@ where
// Reject the message and return
self.handle_invalid_message(
propagation_source,
&raw_message,
&raw_message.topic,
None,
RejectReason::ValidationError(ValidationError::TransformFailed),
);
return;
Expand Down Expand Up @@ -1827,42 +1834,29 @@ where
fn handle_invalid_message(
&mut self,
propagation_source: &PeerId,
raw_message: &RawMessage,
topic_hash: &TopicHash,
message_id: Option<&MessageId>,
reject_reason: RejectReason,
) {
if let Some(metrics) = self.metrics.as_mut() {
metrics.register_invalid_message(&raw_message.topic);
metrics.register_invalid_message(topic_hash);
}

let message = self.data_transform.inbound_transform(raw_message.clone());

match (&mut self.peer_score, message) {
(Some((peer_score, ..)), Ok(message)) => {
let message_id = self.config.message_id(&message);

peer_score.reject_message(
propagation_source,
&message_id,
&message.topic,
reject_reason,
);

self.gossip_promises
.reject_message(&message_id, &reject_reason);
}
(Some((peer_score, ..)), Err(_)) => {
if let Some(msg_id) = message_id {
// Valid transformation without peer scoring
self.gossip_promises.reject_message(msg_id, &reject_reason);
}
if let Some((peer_score, ..)) = &mut self.peer_score {
// The compiler will optimize this pattern-matching
if let Some(msg_id) = message_id {
// The message itself is valid, but is from a banned peer or
// claiming to be self-origin but is actually forwarded from other peers.
peer_score.reject_message(propagation_source, msg_id, topic_hash, reject_reason);
} else {
// The message is invalid, we reject it ignoring any gossip promises. If a peer is
// advertising this message via an IHAVE and it's invalid it will be double
// penalized, one for sending us an invalid and again for breaking a promise.
peer_score.reject_invalid_message(propagation_source, &raw_message.topic);
}
(None, Ok(message)) => {
// Valid transformation without peer scoring
let message_id = self.config.message_id(&message);
self.gossip_promises
.reject_message(&message_id, &reject_reason);
peer_score.reject_invalid_message(propagation_source, topic_hash);
}
(None, Err(_)) => {}
}
}

Expand Down Expand Up @@ -3231,7 +3225,8 @@ where
for (raw_message, validation_error) in invalid_messages {
self.handle_invalid_message(
&propagation_source,
&raw_message,
&raw_message.topic,
None,
RejectReason::ValidationError(validation_error),
)
}
Expand Down

0 comments on commit cc8607c

Please sign in to comment.