Skip to content

Commit cecd765

Browse files
committed
fix(p2p): Don't broadcast gossip messages before decoding them
1 parent 5baabe6 commit cecd765

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

p2p/src/network/pubsub/p2p_network_pubsub_reducer.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ impl P2pNetworkPubsubState {
185185
}
186186

187187
broadcast(dispatcher, global_state)?;
188+
188189
if let Some((_, block)) = incoming_block {
189190
let best_tip = BlockWithHash::try_new(block)?;
190191
dispatcher.push(P2pPeerAction::BestTipUpdate { peer_id, best_tip });
@@ -453,8 +454,6 @@ impl P2pNetworkPubsubState {
453454
state.incoming_messages.clear();
454455
state.incoming_messages.shrink_to(0x20);
455456

456-
let message_id = self.mcache.put(message.clone());
457-
458457
let topic = self.topics.entry(message.topic.clone()).or_default();
459458

460459
if let Some(signature) = &message.signature {
@@ -470,27 +469,6 @@ impl P2pNetworkPubsubState {
470469
}
471470
}
472471

473-
self.clients
474-
.iter_mut()
475-
.filter(|(c, _)| {
476-
// don't send back to who sent this
477-
*c != &peer_id
478-
})
479-
.for_each(|(c, state)| {
480-
let Some(topic_state) = topic.get(c) else {
481-
return;
482-
};
483-
if topic_state.on_mesh() {
484-
state.publish(&message)
485-
} else {
486-
let ctr = state.message.control.get_or_insert_with(Default::default);
487-
ctr.ihave.push(pb::ControlIHave {
488-
topic_id: Some(message.topic.clone()),
489-
message_ids: message_id.clone().into_iter().collect(),
490-
})
491-
}
492-
});
493-
494472
if let Some(data) = &message.data {
495473
if data.len() > 8 {
496474
let mut slice = &data[8..];
@@ -517,6 +495,31 @@ impl P2pNetworkPubsubState {
517495
}
518496
}
519497

498+
let message_id = self.mcache.put(message.clone());
499+
500+
// TODO: this should only happen after the contents have been validated.
501+
// The only validation that has happened so far is that the message can be parsed.
502+
self.clients
503+
.iter_mut()
504+
.filter(|(c, _)| {
505+
// don't send back to who sent this
506+
*c != &peer_id
507+
})
508+
.for_each(|(c, state)| {
509+
let Some(topic_state) = topic.get(c) else {
510+
return;
511+
};
512+
if topic_state.on_mesh() {
513+
state.publish(&message)
514+
} else {
515+
let ctr = state.message.control.get_or_insert_with(Default::default);
516+
ctr.ihave.push(pb::ControlIHave {
517+
topic_id: Some(message.topic.clone()),
518+
message_ids: message_id.clone().into_iter().collect(),
519+
})
520+
}
521+
});
522+
520523
Ok(())
521524
}
522525

0 commit comments

Comments
 (0)