Skip to content

Commit cc8607c

Browse files
authored
refactor(gossipsub): remove duplicated call to inbound_transform
May close #4369. Pull-Request: #5767.
1 parent 7c36d30 commit cc8607c

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

protocols/gossipsub/src/behaviour.rs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,8 @@ where
16841684
);
16851685
self.handle_invalid_message(
16861686
propagation_source,
1687-
raw_message,
1687+
&raw_message.topic,
1688+
Some(msg_id),
16881689
RejectReason::BlackListedSource,
16891690
);
16901691
return false;
@@ -1713,7 +1714,12 @@ where
17131714
source=%propagation_source,
17141715
"Dropping message claiming to be from self but forwarded from source"
17151716
);
1716-
self.handle_invalid_message(propagation_source, raw_message, RejectReason::SelfOrigin);
1717+
self.handle_invalid_message(
1718+
propagation_source,
1719+
&raw_message.topic,
1720+
Some(msg_id),
1721+
RejectReason::SelfOrigin,
1722+
);
17171723
return false;
17181724
}
17191725

@@ -1741,7 +1747,8 @@ where
17411747
// Reject the message and return
17421748
self.handle_invalid_message(
17431749
propagation_source,
1744-
&raw_message,
1750+
&raw_message.topic,
1751+
None,
17451752
RejectReason::ValidationError(ValidationError::TransformFailed),
17461753
);
17471754
return;
@@ -1827,42 +1834,29 @@ where
18271834
fn handle_invalid_message(
18281835
&mut self,
18291836
propagation_source: &PeerId,
1830-
raw_message: &RawMessage,
1837+
topic_hash: &TopicHash,
1838+
message_id: Option<&MessageId>,
18311839
reject_reason: RejectReason,
18321840
) {
18331841
if let Some(metrics) = self.metrics.as_mut() {
1834-
metrics.register_invalid_message(&raw_message.topic);
1842+
metrics.register_invalid_message(topic_hash);
18351843
}
1836-
1837-
let message = self.data_transform.inbound_transform(raw_message.clone());
1838-
1839-
match (&mut self.peer_score, message) {
1840-
(Some((peer_score, ..)), Ok(message)) => {
1841-
let message_id = self.config.message_id(&message);
1842-
1843-
peer_score.reject_message(
1844-
propagation_source,
1845-
&message_id,
1846-
&message.topic,
1847-
reject_reason,
1848-
);
1849-
1850-
self.gossip_promises
1851-
.reject_message(&message_id, &reject_reason);
1852-
}
1853-
(Some((peer_score, ..)), Err(_)) => {
1844+
if let Some(msg_id) = message_id {
1845+
// Valid transformation without peer scoring
1846+
self.gossip_promises.reject_message(msg_id, &reject_reason);
1847+
}
1848+
if let Some((peer_score, ..)) = &mut self.peer_score {
1849+
// The compiler will optimize this pattern-matching
1850+
if let Some(msg_id) = message_id {
1851+
// The message itself is valid, but is from a banned peer or
1852+
// claiming to be self-origin but is actually forwarded from other peers.
1853+
peer_score.reject_message(propagation_source, msg_id, topic_hash, reject_reason);
1854+
} else {
18541855
// The message is invalid, we reject it ignoring any gossip promises. If a peer is
18551856
// advertising this message via an IHAVE and it's invalid it will be double
18561857
// penalized, one for sending us an invalid and again for breaking a promise.
1857-
peer_score.reject_invalid_message(propagation_source, &raw_message.topic);
1858-
}
1859-
(None, Ok(message)) => {
1860-
// Valid transformation without peer scoring
1861-
let message_id = self.config.message_id(&message);
1862-
self.gossip_promises
1863-
.reject_message(&message_id, &reject_reason);
1858+
peer_score.reject_invalid_message(propagation_source, topic_hash);
18641859
}
1865-
(None, Err(_)) => {}
18661860
}
18671861
}
18681862

@@ -3231,7 +3225,8 @@ where
32313225
for (raw_message, validation_error) in invalid_messages {
32323226
self.handle_invalid_message(
32333227
&propagation_source,
3234-
&raw_message,
3228+
&raw_message.topic,
3229+
None,
32353230
RejectReason::ValidationError(validation_error),
32363231
)
32373232
}

0 commit comments

Comments
 (0)