From c06df2f9a38e9382e644b241adf0e96e5ca00955 Mon Sep 17 00:00:00 2001 From: "Yahya Hassanzadeh, Ph.D." <19204398+yhassanzadeh13@users.noreply.github.com> Date: Fri, 18 Oct 2024 13:28:24 -0700 Subject: [PATCH] Add Function to Enable Application Layer to Send Direct Control Messages (#562) ### PR Description This PR addresses https://github.com/libp2p/go-libp2p-pubsub/issues/561; i.e., adding a new `SendControl` function to the `GossipSubRouter`. This will allow the application layer to send direct control messages to peers, facilitating finer-grained testing. --- gossipsub.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/gossipsub.go b/gossipsub.go index dcc5d19..222c71a 100644 --- a/gossipsub.go +++ b/gossipsub.go @@ -2121,6 +2121,23 @@ func (gs *GossipSubRouter) WithDefaultTagTracer() Option { return WithRawTracer(gs.tagTracer) } +// SendControl dispatches the given set of control messages to the given peer. +// The control messages are sent as a single RPC, with the given (optional) messages. +// Args: +// +// p: the peer to send the control messages to. +// ctl: the control messages to send. +// msgs: the messages to send in the same RPC (optional). +// The control messages are piggybacked on the messages. +// +// Returns: +// +// nothing. +func (gs *GossipSubRouter) SendControl(p peer.ID, ctl *pb.ControlMessage, msgs ...*pb.Message) { + out := rpcWithControl(msgs, ctl.Ihave, ctl.Iwant, ctl.Graft, ctl.Prune, ctl.Idontwant) + gs.sendRPC(p, out, false) +} + func peerListToMap(peers []peer.ID) map[peer.ID]struct{} { pmap := make(map[peer.ID]struct{}) for _, p := range peers {