Skip to content

Commit 838b14f

Browse files
committed
send RpcOut to connection handler
1 parent f5739b8 commit 838b14f

File tree

3 files changed

+86
-137
lines changed

3 files changed

+86
-137
lines changed

protocols/gossipsub/src/behaviour.rs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,7 @@ where
539539
let event = RpcOut::Subscriptions(vec![Subscription {
540540
topic_hash: topic_hash.clone(),
541541
action: SubscriptionAction::Subscribe,
542-
}])
543-
.into_protobuf();
542+
}]);
544543

545544
for peer in peer_list {
546545
tracing::debug!(%peer, "Sending SUBSCRIBE to peer");
@@ -575,8 +574,7 @@ where
575574
let event = RpcOut::Subscriptions(vec![Subscription {
576575
topic_hash: topic_hash.clone(),
577576
action: SubscriptionAction::Unsubscribe,
578-
}])
579-
.into_protobuf();
577+
}]);
580578

581579
for peer in peer_list {
582580
tracing::debug!(%peer, "Sending UNSUBSCRIBE to peer");
@@ -616,13 +614,13 @@ where
616614
topic: raw_message.topic.clone(),
617615
});
618616

619-
let event = RpcOut::Publish(raw_message.clone()).into_protobuf();
620-
621617
// check that the size doesn't exceed the max transmission size
622-
if event.get_size() > self.config.max_transmit_size() {
618+
if raw_message.raw_protobuf_len() > self.config.max_transmit_size() {
623619
return Err(PublishError::MessageTooLarge);
624620
}
625621

622+
let event = RpcOut::Publish(raw_message.clone());
623+
626624
// Check the if the message has been published before
627625
if self.duplicate_cache.contains(&msg_id) {
628626
// This message has already been seen. We don't re-publish messages that have already
@@ -638,10 +636,6 @@ where
638636

639637
let topic_hash = raw_message.topic.clone();
640638

641-
// If we are not flood publishing forward the message to mesh peers.
642-
let mesh_peers_sent = !self.config.flood_publish()
643-
&& self.forward_msg(&msg_id, raw_message.clone(), None, HashSet::new())?;
644-
645639
let mut recipient_peers = HashSet::new();
646640
if let Some(set) = self.topic_peers.get(&topic_hash) {
647641
if self.config.flood_publish() {
@@ -655,6 +649,13 @@ where
655649
.cloned(),
656650
);
657651
} else {
652+
// Mesh peers
653+
if let Some(mesh_peers) = self.mesh.get(&raw_message.topic) {
654+
for peer_id in mesh_peers {
655+
recipient_peers.insert(*peer_id);
656+
}
657+
}
658+
658659
// Explicit peers
659660
for peer in &self.explicit_peers {
660661
if set.contains(peer) {
@@ -712,13 +713,14 @@ where
712713
}
713714
}
714715

715-
if recipient_peers.is_empty() && !mesh_peers_sent {
716+
if recipient_peers.is_empty() {
716717
return Err(PublishError::InsufficientPeers);
717718
}
718719

719720
// If the message isn't a duplicate and we have sent it to some peers add it to the
720721
// duplicate cache and memcache.
721722
self.duplicate_cache.insert(msg_id.clone());
723+
let msg_bytes = raw_message.raw_protobuf_len();
722724
self.mcache.put(&msg_id, raw_message);
723725

724726
// If the message is anonymous or has a random author add it to the published message ids
@@ -730,7 +732,6 @@ where
730732
}
731733

732734
// Send to peers we know are subscribed to the topic.
733-
let msg_bytes = event.get_size();
734735
for peer_id in recipient_peers.iter() {
735736
tracing::trace!(peer=%peer_id, "Sending message to peer");
736737
self.send_message(*peer_id, event.clone())?;
@@ -1347,9 +1348,11 @@ where
13471348
.map(|message| message.topic.clone())
13481349
.collect::<HashSet<TopicHash>>();
13491350

1350-
let message = RpcOut::Forward(message_list).into_protobuf();
1351+
let msg_bytes = message_list
1352+
.iter()
1353+
.fold(0, |acc, m| acc + m.raw_protobuf_len());
13511354

1352-
let msg_bytes = message.get_size();
1355+
let message = RpcOut::Forward(message_list);
13531356

13541357
if self.send_message(*peer_id, message).is_err() {
13551358
tracing::error!("Failed to send cached messages. Messages too large");
@@ -1519,9 +1522,7 @@ where
15191522
"GRAFT: Not subscribed to topics - Sending PRUNE to peer"
15201523
);
15211524

1522-
if let Err(e) =
1523-
self.send_message(*peer_id, RpcOut::Control(prune_messages).into_protobuf())
1524-
{
1525+
if let Err(e) = self.send_message(*peer_id, RpcOut::Control(prune_messages)) {
15251526
tracing::error!("Failed to send PRUNE: {:?}", e);
15261527
}
15271528
}
@@ -2024,8 +2025,7 @@ where
20242025
.into_iter()
20252026
.map(|topic_hash| ControlAction::Graft { topic_hash })
20262027
.collect(),
2027-
)
2028-
.into_protobuf(),
2028+
),
20292029
)
20302030
.is_err()
20312031
{
@@ -2586,7 +2586,7 @@ where
25862586

25872587
// send the control messages
25882588
if self
2589-
.send_message(peer, RpcOut::Control(control_msgs).into_protobuf())
2589+
.send_message(peer, RpcOut::Control(control_msgs))
25902590
.is_err()
25912591
{
25922592
tracing::error!("Failed to send control messages. Message too large");
@@ -2618,7 +2618,7 @@ where
26182618
}
26192619

26202620
if self
2621-
.send_message(*peer, RpcOut::Control(remaining_prunes).into_protobuf())
2621+
.send_message(*peer, RpcOut::Control(remaining_prunes))
26222622
.is_err()
26232623
{
26242624
tracing::error!("Failed to send prune messages. Message too large");
@@ -2679,9 +2679,9 @@ where
26792679

26802680
// forward the message to peers
26812681
if !recipient_peers.is_empty() {
2682-
let event = RpcOut::Forward(vec![message.clone()]).into_protobuf();
2682+
let msg_bytes = message.raw_protobuf_len();
2683+
let event = RpcOut::Forward(vec![message.clone()]);
26832684

2684-
let msg_bytes = event.get_size();
26852685
for peer in recipient_peers.iter() {
26862686
tracing::debug!(%peer, message=%msg_id, "Sending message to peer");
26872687
self.send_message(*peer, event.clone())?;
@@ -2800,10 +2800,7 @@ where
28002800
/// Takes each control action mapping and turns it into a message
28012801
fn flush_control_pool(&mut self) {
28022802
for (peer, controls) in self.control_pool.drain().collect::<Vec<_>>() {
2803-
if self
2804-
.send_message(peer, RpcOut::Control(controls).into_protobuf())
2805-
.is_err()
2806-
{
2803+
if self.send_message(peer, RpcOut::Control(controls)).is_err() {
28072804
tracing::error!("Failed to flush control pool. Message too large");
28082805
}
28092806
}
@@ -2812,9 +2809,9 @@ where
28122809
self.pending_iwant_msgs.clear();
28132810
}
28142811

2815-
/// Send a [`Rpc`] message to a peer. This will wrap the message in an arc if it
2812+
/// Send a [`RpcOut`] message to a peer. This will wrap the message in an arc if it
28162813
/// is not already an arc.
2817-
fn send_message(&mut self, peer_id: PeerId, message: proto::RPC) -> Result<(), PublishError> {
2814+
fn send_message(&mut self, peer_id: PeerId, message: RpcOut) -> Result<(), PublishError> {
28182815
// If the message is oversized, try and fragment it. If it cannot be fragmented, log an
28192816
// error and drop the message (all individual messages should be small enough to fit in the
28202817
// max_transmit_size)
@@ -2891,10 +2888,7 @@ where
28912888
if !subscriptions.is_empty() {
28922889
// send our subscriptions to the peer
28932890
if self
2894-
.send_message(
2895-
peer_id,
2896-
RpcOut::Subscriptions(subscriptions).into_protobuf(),
2897-
)
2891+
.send_message(peer_id, RpcOut::Subscriptions(subscriptions))
28982892
.is_err()
28992893
{
29002894
tracing::error!("Failed to send subscriptions, message too large");
@@ -3195,9 +3189,7 @@ where
31953189
// Handle messages
31963190
for (count, raw_message) in rpc.messages.into_iter().enumerate() {
31973191
// Only process the amount of messages the configuration allows.
3198-
if self.config.max_messages_per_rpc().is_some()
3199-
&& Some(count) >= self.config.max_messages_per_rpc()
3200-
{
3192+
if Some(count) >= self.config.max_messages_per_rpc() {
32013193
tracing::warn!("Received more messages than permitted. Ignoring further messages. Processed: {}", count);
32023194
break;
32033195
}

0 commit comments

Comments
 (0)