Repository navigation
feat(libp2p/drand): connect to drand relays for beacon gossip #7755
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,7 @@ use super::{ | |
| ForestBehaviour, ForestBehaviourEvent, Libp2pConfig, | ||
| chain_exchange::{ChainExchangeRequest, ChainExchangeResponse, make_chain_exchange_response}, | ||
| connection_manager::{ConnectionManager, PeerValue}, | ||
| discovery::{DerivedDiscoveryBehaviourEvent, PeerInfo}, | ||
| discovery::{DerivedDiscoveryBehaviourEvent, PeerInfo, resolve_relays}, | ||
| }; | ||
| use crate::beacon::gossipsub::DrandGossipMessage; | ||
| use crate::libp2p::{ | ||
|
|
@@ -196,6 +196,7 @@ pub(in crate::libp2p) struct DrandGossipQueue { | |
| pub struct Libp2pService { | ||
| swarm: Swarm<ForestBehaviour>, | ||
| bootstrap_peers: HashMap<PeerId, Multiaddr>, | ||
| drand_relays: HashMap<PeerId, Multiaddr>, | ||
| connection_manager: ConnectionManager, | ||
| cs: ChainStore, | ||
| peer_manager: Arc<PeerManager>, | ||
|
|
@@ -209,6 +210,7 @@ pub struct Libp2pService { | |
| } | ||
|
|
||
| impl Libp2pService { | ||
| #[allow(clippy::too_many_arguments)] | ||
| pub async fn new( | ||
| config: Libp2pConfig, | ||
| cs: ChainStore, | ||
|
|
@@ -217,6 +219,7 @@ impl Libp2pService { | |
| network_name: GenesisNetworkName, | ||
| genesis_cid: Cid, | ||
| drand_topic: Option<TopicHash>, | ||
| drand_relays: &[Multiaddr], | ||
| ) -> anyhow::Result<Self> { | ||
| let drand_topic = drand_topic.map(|topic| IdentTopic::new(topic.into_string())); | ||
| let behaviour = ForestBehaviour::new( | ||
|
|
@@ -304,6 +307,7 @@ impl Libp2pService { | |
| Ok(Libp2pService { | ||
| swarm, | ||
| bootstrap_peers, | ||
| drand_relays: resolve_relays(drand_relays).await, | ||
| connection_manager: ConnectionManager::new(config.target_peer_count), | ||
| cs, | ||
| peer_manager, | ||
|
|
@@ -327,6 +331,13 @@ impl Libp2pService { | |
| warn!("Failed to bootstrap with Kademlia: {e:#}"); | ||
| } | ||
|
|
||
| // Relays never speak hello and must survive pruning: protect them, and connect now | ||
| // rather than on the first re-dial tick. | ||
| for peer in self.drand_relays.keys() { | ||
| self.peer_manager.protect_peer(*peer); | ||
| } | ||
| dial_peers_if_needed(&mut self.swarm, &self.drand_relays, "drand relay"); | ||
|
|
||
| let bitswap_request_manager = self.swarm.behaviour().bitswap.request_manager(); | ||
| let mut swarm_stream = self.swarm.fuse(); | ||
| let mut network_stream = self.network_receiver_in.stream().fuse(); | ||
|
|
@@ -377,7 +388,7 @@ impl Libp2pService { | |
| cx_response_tx.clone(), | ||
| &pubsub_block_str, | ||
| &pubsub_msg_str, | ||
| self.drand_gossip.as_ref()).await; | ||
| self.drand_gossip.as_ref(), &self.drand_relays).await; | ||
| }, | ||
| Some(SwarmEvent::ConnectionEstablished { peer_id, endpoint, .. }) => { | ||
| self.connection_manager.on_connection_established(peer_id, endpoint.is_dialer(), Instant::now()); | ||
|
|
@@ -432,7 +443,8 @@ impl Libp2pService { | |
| } | ||
| }, | ||
| _ = bootstrap_peer_dialer_interval_stream.next() => { | ||
| dial_to_bootstrap_peers_if_needed(swarm_stream.get_mut(), &self.bootstrap_peers); | ||
| dial_peers_if_needed(swarm_stream.get_mut(), &self.bootstrap_peers, "bootstrap"); | ||
| dial_peers_if_needed(swarm_stream.get_mut(), &self.drand_relays, "drand relay"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| }; | ||
| } | ||
|
|
@@ -462,13 +474,14 @@ impl Libp2pService { | |
| } | ||
| } | ||
|
|
||
| fn dial_to_bootstrap_peers_if_needed( | ||
| fn dial_peers_if_needed( | ||
| swarm: &mut Swarm<ForestBehaviour>, | ||
| bootstrap_peers: &HashMap<PeerId, Multiaddr>, | ||
| peers: &HashMap<PeerId, Multiaddr>, | ||
| kind: &str, | ||
| ) { | ||
| for (peer, ma) in bootstrap_peers { | ||
| for (peer, ma) in peers { | ||
| if !swarm.behaviour().peers().contains(peer) { | ||
| info!("Re-dialing to bootstrap peer at {ma}"); | ||
| info!("Dialing {kind} peer at {ma}"); | ||
| if let Err(e) = swarm.dial(ma.clone()) { | ||
| warn!("{e}"); | ||
| } | ||
|
|
@@ -694,22 +707,33 @@ async fn handle_discovery_event( | |
| discovery_out: DiscoveryEvent, | ||
| network_sender_out: &Sender<NetworkEvent>, | ||
| peer_manager: &PeerManager, | ||
| drand_relays: &HashMap<PeerId, Multiaddr>, | ||
| ) { | ||
| // `drand` relays are not Filecoin peers: no hello, no chain sync, no protocol bans. | ||
| let is_drand_relay = |peer: &PeerId| drand_relays.contains_key(peer); | ||
| match discovery_out { | ||
| DiscoveryEvent::PeerConnected(peer_id) => { | ||
| trace!("Peer connected, {peer_id}"); | ||
| emit_event(network_sender_out, NetworkEvent::PeerConnected(peer_id)).await; | ||
| if !is_drand_relay(&peer_id) { | ||
| emit_event(network_sender_out, NetworkEvent::PeerConnected(peer_id)).await; | ||
| } | ||
| } | ||
| DiscoveryEvent::PeerDisconnected(peer_id) => { | ||
| trace!("Peer disconnected, {peer_id}"); | ||
| emit_event(network_sender_out, NetworkEvent::PeerDisconnected(peer_id)).await; | ||
| if !is_drand_relay(&peer_id) { | ||
| emit_event(network_sender_out, NetworkEvent::PeerDisconnected(peer_id)).await; | ||
| } | ||
| } | ||
| DiscoveryEvent::Discovery(discovery_event) => match &*discovery_event { | ||
| DerivedDiscoveryBehaviourEvent::Identify(identify::Event::Received { | ||
| peer_id, | ||
| info, | ||
| .. | ||
| }) => { | ||
| // Relays only speak gossipsub, so the Filecoin protocol checks don't apply. | ||
| if is_drand_relay(peer_id) { | ||
| return; | ||
| } | ||
| let protocols = HashSet::from_iter(info.protocols.iter().map(|p| p.to_string())); | ||
| if !protocols.contains(super::hello::HELLO_PROTOCOL_NAME) { | ||
| peer_manager | ||
|
|
@@ -1040,6 +1064,7 @@ async fn handle_forest_behaviour_event( | |
| pubsub_block_str: &str, | ||
| pubsub_msg_str: &str, | ||
| drand_gossip: Option<&DrandGossipQueue>, | ||
| drand_relays: &HashMap<PeerId, Multiaddr>, | ||
| ) { | ||
| match event { | ||
| ForestBehaviourEvent::Discovery(discovery_out) => { | ||
|
|
@@ -1048,6 +1073,7 @@ async fn handle_forest_behaviour_event( | |
| discovery_out, | ||
| network_sender_out, | ||
| peer_manager, | ||
| drand_relays, | ||
| ) | ||
| .await | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.