Skip to content

Commit dcc7334

Browse files
committed
used eyre:Result instead of expect
1 parent 63ba970 commit dcc7334

File tree

2 files changed

+36
-30
lines changed

2 files changed

+36
-30
lines changed

p2p/src/behaviour.rs

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use libp2p::identity::{Keypair, PeerId, PublicKey};
66
use libp2p::kad::store::MemoryStore;
77
use libp2p::StreamProtocol;
88
use libp2p::{autonat, dcutr, gossipsub, identify, kad, relay};
9+
use eyre::{eyre, Result};
910

1011
#[derive(libp2p::swarm::NetworkBehaviour)]
1112
pub struct DriaBehaviour {
@@ -23,17 +24,17 @@ impl DriaBehaviour {
2324
relay_behavior: 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+
Ok(Self {
3031
relay: relay_behavior,
31-
gossipsub: create_gossipsub_behavior(peer_id),
32+
gossipsub: create_gossipsub_behavior(peer_id)?,
3233
kademlia: create_kademlia_behavior(peer_id, kademlia_protocol),
3334
autonat: create_autonat_behavior(peer_id),
3435
dcutr: create_dcutr_behavior(peer_id),
3536
identify: create_identify_behavior(public_key, identity_protocol),
36-
}
37+
})
3738
}
3839
}
3940

@@ -42,7 +43,7 @@ impl DriaBehaviour {
4243
fn create_kademlia_behavior(
4344
local_peer_id: PeerId,
4445
protocol_name: StreamProtocol,
45-
) -> kad::Behaviour<MemoryStore> {
46+
) -> kad::Behaviour<MemoryStore>{
4647
use kad::{Behaviour, Config};
4748

4849
const QUERY_TIMEOUT_SECS: u64 = 5 * 60;
@@ -94,7 +95,7 @@ fn create_autonat_behavior(local_peer_id: PeerId) -> autonat::Behaviour {
9495

9596
/// Configures the Gossipsub behavior for pub/sub messaging across peers.
9697
#[inline]
97-
fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
98+
fn create_gossipsub_behavior(author: PeerId) -> Result<gossipsub::Behaviour> {
9899
use gossipsub::{
99100
Behaviour, ConfigBuilder, Message, MessageAuthenticity, MessageId, ValidationMode,
100101
};
@@ -138,23 +139,30 @@ fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
138139
};
139140

140141
// TODO: add data transform here later
142+
let config = match ConfigBuilder::default()
143+
.heartbeat_interval(Duration::from_secs(HEARTBEAT_INTERVAL_SECS))
144+
.max_transmit_size(MAX_TRANSMIT_SIZE)
145+
.message_id_fn(message_id_fn)
146+
.message_capacity(MESSAGE_CAPACITY)
147+
.message_ttl(Duration::from_secs(MESSAGE_TTL_SECS))
148+
.gossip_ttl(Duration::from_secs(GOSSIP_TTL_SECS))
149+
.duplicate_cache_time(Duration::from_secs(DUPLICATE_CACHE_TIME_SECS))
150+
.max_ihave_length(MAX_IHAVE_LENGTH)
151+
.send_queue_size(MAX_SEND_QUEUE_SIZE)
152+
.validation_mode(VALIDATION_MODE)
153+
.validate_messages()
154+
.build() {
155+
Ok(config) => config,
156+
Err(e) => {
157+
return Err(eyre!("Failed to create gossipsub config: {}", e));
158+
}
159+
};
141160

142-
Behaviour::new(
161+
match Behaviour::new(
143162
MessageAuthenticity::Author(author),
144-
ConfigBuilder::default()
145-
.heartbeat_interval(Duration::from_secs(HEARTBEAT_INTERVAL_SECS))
146-
.max_transmit_size(MAX_TRANSMIT_SIZE)
147-
.message_id_fn(message_id_fn)
148-
.message_capacity(MESSAGE_CAPACITY)
149-
.message_ttl(Duration::from_secs(MESSAGE_TTL_SECS))
150-
.gossip_ttl(Duration::from_secs(GOSSIP_TTL_SECS))
151-
.duplicate_cache_time(Duration::from_secs(DUPLICATE_CACHE_TIME_SECS))
152-
.max_ihave_length(MAX_IHAVE_LENGTH)
153-
.send_queue_size(MAX_SEND_QUEUE_SIZE)
154-
.validation_mode(VALIDATION_MODE)
155-
.validate_messages()
156-
.build()
157-
.expect("Valid config"), // TODO: better error handling
158-
)
159-
.expect("Valid behaviour") // TODO: better error handling
163+
config
164+
) {
165+
Ok(behaviour) => Ok(behaviour),
166+
Err(e) => Err(eyre!("Failed to create gossipsub behaviour: {}", e)),
167+
}
160168
}

p2p/src/client.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::*;
2-
use eyre::Result;
2+
use eyre::{eyre, Result};
33
use libp2p::futures::StreamExt;
44
use libp2p::gossipsub::{
55
Message, MessageAcceptance, MessageId, PublishError, SubscriptionError, TopicHash,
@@ -72,13 +72,13 @@ impl DriaP2PClient {
7272
)?
7373
.with_quic()
7474
.with_relay_client(noise::Config::new, yamux::Config::default)?
75-
.with_behaviour(|key, relay_behavior| {
76-
Ok(DriaBehaviour::new(
75+
.with_behaviour(|key, relay_behavior: libp2p::relay::client::Behaviour| {
76+
DriaBehaviour::new(
7777
key,
7878
relay_behavior,
7979
identity_protocol.clone(),
8080
kademlia_protocol.clone(),
81-
))
81+
).unwrap()
8282
})?
8383
.with_swarm_config(|c| {
8484
c.with_idle_connection_timeout(Duration::from_secs(IDLE_CONNECTION_TIMEOUT_SECS))
@@ -135,9 +135,7 @@ impl DriaP2PClient {
135135
identity_protocol,
136136
kademlia_protocol,
137137
})
138-
}
139-
140-
/// Subscribe to a topic.
138+
} /// Subscribe to a topic.
141139
pub fn subscribe(&mut self, topic_name: &str) -> Result<bool, SubscriptionError> {
142140
log::debug!("Subscribing to {}", topic_name);
143141

0 commit comments

Comments
 (0)