Skip to content

Commit 7ea642f

Browse files
authored
Merge pull request #134 from firstbatchxyz/caglacelik/behaviour-error-handling
Behaviour Error Handling
2 parents 63ba970 + b80a856 commit 7ea642f

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

p2p/src/behaviour.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::collections::hash_map;
22
use std::hash::{Hash, Hasher};
33
use std::time::Duration;
44

5+
use eyre::{eyre, Context, Result};
56
use libp2p::identity::{Keypair, PeerId, PublicKey};
67
use libp2p::kad::store::MemoryStore;
78
use libp2p::StreamProtocol;
@@ -20,26 +21,28 @@ pub struct DriaBehaviour {
2021
impl DriaBehaviour {
2122
pub fn new(
2223
key: &Keypair,
23-
relay_behavior: relay::client::Behaviour,
24+
relay_behaviour: relay::client::Behaviour,
2425
identity_protocol: String,
2526
kademlia_protocol: StreamProtocol,
26-
) -> Self {
27+
) -> Result<Self> {
2728
let public_key = key.public();
2829
let peer_id = public_key.to_peer_id();
29-
Self {
30-
relay: relay_behavior,
31-
gossipsub: create_gossipsub_behavior(peer_id),
32-
kademlia: create_kademlia_behavior(peer_id, kademlia_protocol),
33-
autonat: create_autonat_behavior(peer_id),
34-
dcutr: create_dcutr_behavior(peer_id),
35-
identify: create_identify_behavior(public_key, identity_protocol),
36-
}
30+
31+
Ok(Self {
32+
relay: relay_behaviour,
33+
gossipsub: create_gossipsub_behaviour(peer_id)
34+
.wrap_err("could not create Gossipsub behaviour")?,
35+
kademlia: create_kademlia_behaviour(peer_id, kademlia_protocol),
36+
autonat: create_autonat_behaviour(peer_id),
37+
dcutr: create_dcutr_behaviour(peer_id),
38+
identify: create_identify_behaviour(public_key, identity_protocol),
39+
})
3740
}
3841
}
3942

4043
/// Configures the Kademlia DHT behavior for the node.
4144
#[inline]
42-
fn create_kademlia_behavior(
45+
fn create_kademlia_behaviour(
4346
local_peer_id: PeerId,
4447
protocol_name: StreamProtocol,
4548
) -> kad::Behaviour<MemoryStore> {
@@ -57,7 +60,7 @@ fn create_kademlia_behavior(
5760

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

7881
Behaviour::new(local_peer_id)
7982
}
8083

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

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

9598
/// Configures the Gossipsub behavior for pub/sub messaging across peers.
9699
#[inline]
97-
fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
100+
fn create_gossipsub_behaviour(author: PeerId) -> Result<gossipsub::Behaviour> {
98101
use gossipsub::{
99102
Behaviour, ConfigBuilder, Message, MessageAuthenticity, MessageId, ValidationMode,
100103
};
@@ -138,7 +141,6 @@ fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
138141
};
139142

140143
// TODO: add data transform here later
141-
142144
Behaviour::new(
143145
MessageAuthenticity::Author(author),
144146
ConfigBuilder::default()
@@ -154,7 +156,7 @@ fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
154156
.validation_mode(VALIDATION_MODE)
155157
.validate_messages()
156158
.build()
157-
.expect("Valid config"), // TODO: better error handling
159+
.wrap_err(eyre!("could not create Gossipsub config"))?,
158160
)
159-
.expect("Valid behaviour") // TODO: better error handling
161+
.map_err(|e| eyre!(e))
160162
}

p2p/src/client.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub struct DriaP2PClient {
3535
}
3636

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

4140
/// Number of seconds between refreshing the Kademlia DHT.
@@ -72,13 +71,14 @@ impl DriaP2PClient {
7271
)?
7372
.with_quic()
7473
.with_relay_client(noise::Config::new, yamux::Config::default)?
75-
.with_behaviour(|key, relay_behavior| {
76-
Ok(DriaBehaviour::new(
74+
.with_behaviour(|key, relay_behaviour| {
75+
DriaBehaviour::new(
7776
key,
78-
relay_behavior,
77+
relay_behaviour,
7978
identity_protocol.clone(),
8079
kademlia_protocol.clone(),
81-
))
80+
)
81+
.map_err(Into::into)
8282
})?
8383
.with_swarm_config(|c| {
8484
c.with_idle_connection_timeout(Duration::from_secs(IDLE_CONNECTION_TIMEOUT_SECS))

0 commit comments

Comments
 (0)