Skip to content

Commit 8043106

Browse files
committed
better error handling in Behaviour, consistent names
1 parent 9ebf4a1 commit 8043106

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

p2p/src/behaviour.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,27 @@ pub struct DriaBehaviour {
2121
impl DriaBehaviour {
2222
pub fn new(
2323
key: &Keypair,
24-
relay_behavior: relay::client::Behaviour,
24+
relay_behaviour: relay::client::Behaviour,
2525
identity_protocol: String,
2626
kademlia_protocol: StreamProtocol,
2727
) -> Result<Self> {
2828
let public_key = key.public();
2929
let peer_id = public_key.to_peer_id();
30+
3031
Ok(Self {
31-
relay: relay_behavior,
32-
gossipsub: create_gossipsub_behavior(peer_id)?,
33-
kademlia: create_kademlia_behavior(peer_id, kademlia_protocol),
34-
autonat: create_autonat_behavior(peer_id),
35-
dcutr: create_dcutr_behavior(peer_id),
36-
identify: create_identify_behavior(public_key, identity_protocol),
32+
relay: relay_behaviour,
33+
gossipsub: create_gossipsub_behaviour(peer_id)?,
34+
kademlia: create_kademlia_behaviour(peer_id, kademlia_protocol),
35+
autonat: create_autonat_behaviour(peer_id),
36+
dcutr: create_dcutr_behaviour(peer_id),
37+
identify: create_identify_behaviour(public_key, identity_protocol),
3738
})
3839
}
3940
}
4041

4142
/// Configures the Kademlia DHT behavior for the node.
4243
#[inline]
43-
fn create_kademlia_behavior(
44+
fn create_kademlia_behaviour(
4445
local_peer_id: PeerId,
4546
protocol_name: StreamProtocol,
4647
) -> kad::Behaviour<MemoryStore> {
@@ -58,7 +59,7 @@ fn create_kademlia_behavior(
5859

5960
/// Configures the Identify behavior to allow nodes to exchange information like supported protocols.
6061
#[inline]
61-
fn create_identify_behavior(
62+
fn create_identify_behaviour(
6263
local_public_key: PublicKey,
6364
protocol_version: String,
6465
) -> identify::Behaviour {
@@ -73,15 +74,15 @@ fn create_identify_behavior(
7374
/// It uses a Relay for the hole-punching process, and if it succeeds the peers are
7475
/// connected directly without the need for the relay; otherwise, they keep using the relay.
7576
#[inline]
76-
fn create_dcutr_behavior(local_peer_id: PeerId) -> dcutr::Behaviour {
77+
fn create_dcutr_behaviour(local_peer_id: PeerId) -> dcutr::Behaviour {
7778
use dcutr::Behaviour;
7879

7980
Behaviour::new(local_peer_id)
8081
}
8182

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

8788
Behaviour::new(
@@ -95,7 +96,7 @@ fn create_autonat_behavior(local_peer_id: PeerId) -> autonat::Behaviour {
9596

9697
/// Configures the Gossipsub behavior for pub/sub messaging across peers.
9798
#[inline]
98-
fn create_gossipsub_behavior(author: PeerId) -> Result<gossipsub::Behaviour> {
99+
fn create_gossipsub_behaviour(author: PeerId) -> Result<gossipsub::Behaviour> {
99100
use gossipsub::{
100101
Behaviour, ConfigBuilder, Message, MessageAuthenticity, MessageId, ValidationMode,
101102
};

p2p/src/client.rs

Lines changed: 3 additions & 7 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,17 +71,14 @@ impl DriaP2PClient {
7271
)?
7372
.with_quic()
7473
.with_relay_client(noise::Config::new, yamux::Config::default)?
75-
.with_behaviour(|key, relay_behavior| {
74+
.with_behaviour(|key, relay_behaviour| {
7675
DriaBehaviour::new(
7776
key,
78-
relay_behavior,
77+
relay_behaviour,
7978
identity_protocol.clone(),
8079
kademlia_protocol.clone(),
8180
)
82-
.unwrap_or_else(|e| {
83-
log::error!("Error creating Dria Behaviour: {:?}", e);
84-
panic!("Failed to create Dria Behaviour: {:?}", e);
85-
})
81+
.map_err(Into::into)
8682
})?
8783
.with_swarm_config(|c| {
8884
c.with_idle_connection_timeout(Duration::from_secs(IDLE_CONNECTION_TIMEOUT_SECS))

0 commit comments

Comments
 (0)