Skip to content

Commit

Permalink
Merge pull request #134 from firstbatchxyz/caglacelik/behaviour-error…
Browse files Browse the repository at this point in the history
…-handling

Behaviour Error Handling
  • Loading branch information
erhant authored Oct 17, 2024
2 parents 63ba970 + b80a856 commit 7ea642f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
38 changes: 20 additions & 18 deletions p2p/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::hash_map;
use std::hash::{Hash, Hasher};
use std::time::Duration;

use eyre::{eyre, Context, Result};
use libp2p::identity::{Keypair, PeerId, PublicKey};
use libp2p::kad::store::MemoryStore;
use libp2p::StreamProtocol;
Expand All @@ -20,26 +21,28 @@ pub struct DriaBehaviour {
impl DriaBehaviour {
pub fn new(
key: &Keypair,
relay_behavior: relay::client::Behaviour,
relay_behaviour: relay::client::Behaviour,
identity_protocol: String,
kademlia_protocol: StreamProtocol,
) -> Self {
) -> Result<Self> {
let public_key = key.public();
let peer_id = public_key.to_peer_id();
Self {
relay: relay_behavior,
gossipsub: create_gossipsub_behavior(peer_id),
kademlia: create_kademlia_behavior(peer_id, kademlia_protocol),
autonat: create_autonat_behavior(peer_id),
dcutr: create_dcutr_behavior(peer_id),
identify: create_identify_behavior(public_key, identity_protocol),
}

Ok(Self {
relay: relay_behaviour,
gossipsub: create_gossipsub_behaviour(peer_id)
.wrap_err("could not create Gossipsub behaviour")?,
kademlia: create_kademlia_behaviour(peer_id, kademlia_protocol),
autonat: create_autonat_behaviour(peer_id),
dcutr: create_dcutr_behaviour(peer_id),
identify: create_identify_behaviour(public_key, identity_protocol),
})
}
}

/// Configures the Kademlia DHT behavior for the node.
#[inline]
fn create_kademlia_behavior(
fn create_kademlia_behaviour(
local_peer_id: PeerId,
protocol_name: StreamProtocol,
) -> kad::Behaviour<MemoryStore> {
Expand All @@ -57,7 +60,7 @@ fn create_kademlia_behavior(

/// Configures the Identify behavior to allow nodes to exchange information like supported protocols.
#[inline]
fn create_identify_behavior(
fn create_identify_behaviour(
local_public_key: PublicKey,
protocol_version: String,
) -> identify::Behaviour {
Expand All @@ -72,15 +75,15 @@ fn create_identify_behavior(
/// It uses a Relay for the hole-punching process, and if it succeeds the peers are
/// connected directly without the need for the relay; otherwise, they keep using the relay.
#[inline]
fn create_dcutr_behavior(local_peer_id: PeerId) -> dcutr::Behaviour {
fn create_dcutr_behaviour(local_peer_id: PeerId) -> dcutr::Behaviour {
use dcutr::Behaviour;

Behaviour::new(local_peer_id)
}

/// Configures the Autonat behavior to assist in network address translation detection.
#[inline]
fn create_autonat_behavior(local_peer_id: PeerId) -> autonat::Behaviour {
fn create_autonat_behaviour(local_peer_id: PeerId) -> autonat::Behaviour {
use autonat::{Behaviour, Config};

Behaviour::new(
Expand All @@ -94,7 +97,7 @@ fn create_autonat_behavior(local_peer_id: PeerId) -> autonat::Behaviour {

/// Configures the Gossipsub behavior for pub/sub messaging across peers.
#[inline]
fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
fn create_gossipsub_behaviour(author: PeerId) -> Result<gossipsub::Behaviour> {
use gossipsub::{
Behaviour, ConfigBuilder, Message, MessageAuthenticity, MessageId, ValidationMode,
};
Expand Down Expand Up @@ -138,7 +141,6 @@ fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
};

// TODO: add data transform here later

Behaviour::new(
MessageAuthenticity::Author(author),
ConfigBuilder::default()
Expand All @@ -154,7 +156,7 @@ fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
.validation_mode(VALIDATION_MODE)
.validate_messages()
.build()
.expect("Valid config"), // TODO: better error handling
.wrap_err(eyre!("could not create Gossipsub config"))?,
)
.expect("Valid behaviour") // TODO: better error handling
.map_err(|e| eyre!(e))
}
10 changes: 5 additions & 5 deletions p2p/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ pub struct DriaP2PClient {
}

/// Number of seconds before an idle connection is closed.
/// TODO: default is 0, is 60 a good value?
const IDLE_CONNECTION_TIMEOUT_SECS: u64 = 60;

/// Number of seconds between refreshing the Kademlia DHT.
Expand Down Expand Up @@ -72,13 +71,14 @@ impl DriaP2PClient {
)?
.with_quic()
.with_relay_client(noise::Config::new, yamux::Config::default)?
.with_behaviour(|key, relay_behavior| {
Ok(DriaBehaviour::new(
.with_behaviour(|key, relay_behaviour| {
DriaBehaviour::new(
key,
relay_behavior,
relay_behaviour,
identity_protocol.clone(),
kademlia_protocol.clone(),
))
)
.map_err(Into::into)
})?
.with_swarm_config(|c| {
c.with_idle_connection_timeout(Duration::from_secs(IDLE_CONNECTION_TIMEOUT_SECS))
Expand Down

0 comments on commit 7ea642f

Please sign in to comment.