Send direct message to a peer in a gossipsub network

Hi everyone,
I’m doing a simulation of a super simple bitcoin-like protocol in gossipsub in Go and I’m using libp2p to connect the peers. Every peer creates a block and then it shares the block on the network.
In my implementation I have 3 types of messages (based on the same struct, with a field messageType): data, ihave, iwant. When a peer A creates a block, it publishes an ihave message through the gossip network. Then, a peer B receives the ihave message, and sees that it doesn’t have that block. Now, the peer B has to send a message iwant directly to the peer A (not through the gossipsub network) asking for that block. After that, peer A sends a data message with the block to peer B (again on a direct connection), so that B can store it.
How can I establish the direct connection between A and B to send iwant and data messages? I would like to avoid using a streamHandler and setting different things, it would be better if I can send the message directly using gossipsub’s structures and inside its loop. Is it possible?

To develop my protocol, I followed this example go-libp2p-examples/pubsub/chat at master · libp2p/go-libp2p-examples · GitHub
If you want to check the code of my project, you can find it here GitHub - 0xfederama/bitcoin-simulation-libp2p: Simple Bitcoin protocol simulation using Go and Libp2p

You can just include a peer record in your ihave message, which the receiving peer can use to connect and then retrieve with a side protocol.
Should be straightforward.

That’s what I was thinking, but to do that I need to set a func streamHandler to handle the single connections (message iwant and data), right? Is it safe to use a stream handler and a gossipsub on the same project or will they overlap?

yes, you need to make a custom protocol to use with ita own stream handler.

1 Like