@@ -6,6 +6,7 @@ use libp2p::identity::{Keypair, PeerId, PublicKey};
6
6
use libp2p:: kad:: store:: MemoryStore ;
7
7
use libp2p:: StreamProtocol ;
8
8
use libp2p:: { autonat, dcutr, gossipsub, identify, kad, relay} ;
9
+ use eyre:: { eyre, Result } ;
9
10
10
11
#[ derive( libp2p:: swarm:: NetworkBehaviour ) ]
11
12
pub struct DriaBehaviour {
@@ -23,17 +24,17 @@ impl DriaBehaviour {
23
24
relay_behavior : relay:: client:: Behaviour ,
24
25
identity_protocol : String ,
25
26
kademlia_protocol : StreamProtocol ,
26
- ) -> Self {
27
+ ) -> Result < Self > {
27
28
let public_key = key. public ( ) ;
28
29
let peer_id = public_key. to_peer_id ( ) ;
29
- Self {
30
+ Ok ( Self {
30
31
relay : relay_behavior,
31
- gossipsub : create_gossipsub_behavior ( peer_id) ,
32
+ gossipsub : create_gossipsub_behavior ( peer_id) ? ,
32
33
kademlia : create_kademlia_behavior ( peer_id, kademlia_protocol) ,
33
34
autonat : create_autonat_behavior ( peer_id) ,
34
35
dcutr : create_dcutr_behavior ( peer_id) ,
35
36
identify : create_identify_behavior ( public_key, identity_protocol) ,
36
- }
37
+ } )
37
38
}
38
39
}
39
40
@@ -42,7 +43,7 @@ impl DriaBehaviour {
42
43
fn create_kademlia_behavior (
43
44
local_peer_id : PeerId ,
44
45
protocol_name : StreamProtocol ,
45
- ) -> kad:: Behaviour < MemoryStore > {
46
+ ) -> kad:: Behaviour < MemoryStore > {
46
47
use kad:: { Behaviour , Config } ;
47
48
48
49
const QUERY_TIMEOUT_SECS : u64 = 5 * 60 ;
@@ -94,7 +95,7 @@ fn create_autonat_behavior(local_peer_id: PeerId) -> autonat::Behaviour {
94
95
95
96
/// Configures the Gossipsub behavior for pub/sub messaging across peers.
96
97
#[ inline]
97
- fn create_gossipsub_behavior ( author : PeerId ) -> gossipsub:: Behaviour {
98
+ fn create_gossipsub_behavior ( author : PeerId ) -> Result < gossipsub:: Behaviour > {
98
99
use gossipsub:: {
99
100
Behaviour , ConfigBuilder , Message , MessageAuthenticity , MessageId , ValidationMode ,
100
101
} ;
@@ -138,23 +139,30 @@ fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
138
139
} ;
139
140
140
141
// 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
+ } ;
141
160
142
- Behaviour :: new (
161
+ match Behaviour :: new (
143
162
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
+ }
160
168
}
0 commit comments