Skip to content

Commit e9f3c3c

Browse files
committed
refactoring
1 parent 41d6206 commit e9f3c3c

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

p2p/src/behaviour.rs

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

5-
use eyre::{eyre, Result};
5+
use eyre::{eyre, Context, Result};
66
use libp2p::identity::{Keypair, PeerId, PublicKey};
77
use libp2p::kad::store::MemoryStore;
88
use libp2p::StreamProtocol;
@@ -139,28 +139,22 @@ fn create_gossipsub_behavior(author: PeerId) -> Result<gossipsub::Behaviour> {
139139
};
140140

141141
// 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-
{
156-
Ok(config) => config,
157-
Err(e) => {
158-
return Err(eyre!("Failed to create gossipsub config: {}", e));
159-
}
160-
};
161-
162-
match Behaviour::new(MessageAuthenticity::Author(author), config) {
163-
Ok(behaviour) => Ok(behaviour),
164-
Err(e) => Err(eyre!("Failed to create gossipsub behaviour: {}", e)),
165-
}
142+
Behaviour::new(
143+
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+
.wrap_err(eyre!("Failed to create config"))?,
158+
)
159+
.map_err(|e| eyre!(e))
166160
}

p2p/src/client.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,17 @@ impl DriaP2PClient {
7272
)?
7373
.with_quic()
7474
.with_relay_client(noise::Config::new, yamux::Config::default)?
75-
.with_behaviour(|key, relay_behavior: libp2p::relay::client::Behaviour| {
75+
.with_behaviour(|key, relay_behavior| {
7676
DriaBehaviour::new(
7777
key,
7878
relay_behavior,
7979
identity_protocol.clone(),
8080
kademlia_protocol.clone(),
8181
)
82-
.unwrap()
82+
.unwrap_or_else(|e| {
83+
log::error!("Error creating Dria Behaviour: {:?}", e);
84+
panic!("Failed to create Dria Behaviour: {:?}", e);
85+
})
8386
})?
8487
.with_swarm_config(|c| {
8588
c.with_idle_connection_timeout(Duration::from_secs(IDLE_CONNECTION_TIMEOUT_SECS))
@@ -137,6 +140,7 @@ impl DriaP2PClient {
137140
kademlia_protocol,
138141
})
139142
}
143+
140144
/// Subscribe to a topic.
141145
pub fn subscribe(&mut self, topic_name: &str) -> Result<bool, SubscriptionError> {
142146
log::debug!("Subscribing to {}", topic_name);

0 commit comments

Comments
 (0)