JoshOrndorff / recipes

A Hands-On Cookbook for Aspiring Blockchain Chefs
GNU General Public License v3.0
379 stars 187 forks source link

Recipe about gossiping with other nodes over the network #420

Open JoshOrndorff opened 3 years ago

JoshOrndorff commented 3 years ago

image

nuke-web3 commented 3 years ago

Hi, I have few question regarding gossip. I try to use sc_network_gossip::GossipEngine. I hope this is the right way to go. Do we get the messages sent to us using Receiver returned by messages_for? How does this relate to the ImportQueue in case of gossiping blocks, though I would like to transmit other messages, too)? If I register a message with register_gossip_message and send it selectively to a single peer (using send_topic), does only the recipient receive it or other connected peers, too, after they poll the receiver of messages_for? How do I receive messages sent by send_message, where no topic is specified? Thank you.


I'd recommend to directly use the NetworkService in sc_network instead. It's more simple to use Step 1: add an entry in NetworkConfiguration::extra_sets: https://docs.rs/sc-network/0.9.0/sc_network/config/struct.NetworkConfiguration.html#structfield.extra_sets See example config for what we use for GrandPa: https://github.com/paritytech/substrate/blob/6ac86d545f6da8e4afc373dc0876c3e7ba79e51b/client/finality-grandpa/src/lib.rs#L690-L700 Step 2: call events_stream at initialization: https://docs.rs/sc-network/0.9.0/sc_network/struct.NetworkService.html#method.event_stream Step 3: if you want to gossip to the same peers you're connected to for syncing (which is the easy option), call add_set_reserved and remove_set_reserved when you receive SyncConnected and SyncDisconnected (example) Adding/removing reserved peers sets the list of peers to have an open gossiping channel with Step 4: you will receive events whenever a gossiping channel is open or closed or a message received (https://docs.rs/sc-network/0.9.0/sc_network/enum.Event.html) Step 5: use write_notification to send a message to someone: https://docs.rs/sc-network/0.9.0/sc_network/struct.NetworkService.html#method.write_notification Step 7: profit