@@ -2,6 +2,7 @@ use std::collections::hash_map;
2
2
use std:: hash:: { Hash , Hasher } ;
3
3
use std:: time:: Duration ;
4
4
5
+ use eyre:: { eyre, Context , Result } ;
5
6
use libp2p:: identity:: { Keypair , PeerId , PublicKey } ;
6
7
use libp2p:: kad:: store:: MemoryStore ;
7
8
use libp2p:: StreamProtocol ;
@@ -20,26 +21,28 @@ pub struct DriaBehaviour {
20
21
impl DriaBehaviour {
21
22
pub fn new (
22
23
key : & Keypair ,
23
- relay_behavior : relay:: client:: Behaviour ,
24
+ relay_behaviour : 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
- 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
+ } )
37
40
}
38
41
}
39
42
40
43
/// Configures the Kademlia DHT behavior for the node.
41
44
#[ inline]
42
- fn create_kademlia_behavior (
45
+ fn create_kademlia_behaviour (
43
46
local_peer_id : PeerId ,
44
47
protocol_name : StreamProtocol ,
45
48
) -> kad:: Behaviour < MemoryStore > {
@@ -57,7 +60,7 @@ fn create_kademlia_behavior(
57
60
58
61
/// Configures the Identify behavior to allow nodes to exchange information like supported protocols.
59
62
#[ inline]
60
- fn create_identify_behavior (
63
+ fn create_identify_behaviour (
61
64
local_public_key : PublicKey ,
62
65
protocol_version : String ,
63
66
) -> identify:: Behaviour {
@@ -72,15 +75,15 @@ fn create_identify_behavior(
72
75
/// It uses a Relay for the hole-punching process, and if it succeeds the peers are
73
76
/// connected directly without the need for the relay; otherwise, they keep using the relay.
74
77
#[ inline]
75
- fn create_dcutr_behavior ( local_peer_id : PeerId ) -> dcutr:: Behaviour {
78
+ fn create_dcutr_behaviour ( local_peer_id : PeerId ) -> dcutr:: Behaviour {
76
79
use dcutr:: Behaviour ;
77
80
78
81
Behaviour :: new ( local_peer_id)
79
82
}
80
83
81
84
/// Configures the Autonat behavior to assist in network address translation detection.
82
85
#[ inline]
83
- fn create_autonat_behavior ( local_peer_id : PeerId ) -> autonat:: Behaviour {
86
+ fn create_autonat_behaviour ( local_peer_id : PeerId ) -> autonat:: Behaviour {
84
87
use autonat:: { Behaviour , Config } ;
85
88
86
89
Behaviour :: new (
@@ -94,7 +97,7 @@ fn create_autonat_behavior(local_peer_id: PeerId) -> autonat::Behaviour {
94
97
95
98
/// Configures the Gossipsub behavior for pub/sub messaging across peers.
96
99
#[ inline]
97
- fn create_gossipsub_behavior ( author : PeerId ) -> gossipsub:: Behaviour {
100
+ fn create_gossipsub_behaviour ( author : PeerId ) -> Result < gossipsub:: Behaviour > {
98
101
use gossipsub:: {
99
102
Behaviour , ConfigBuilder , Message , MessageAuthenticity , MessageId , ValidationMode ,
100
103
} ;
@@ -138,7 +141,6 @@ fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
138
141
} ;
139
142
140
143
// TODO: add data transform here later
141
-
142
144
Behaviour :: new (
143
145
MessageAuthenticity :: Author ( author) ,
144
146
ConfigBuilder :: default ( )
@@ -154,7 +156,7 @@ fn create_gossipsub_behavior(author: PeerId) -> gossipsub::Behaviour {
154
156
. validation_mode ( VALIDATION_MODE )
155
157
. validate_messages ( )
156
158
. build ( )
157
- . expect ( "Valid config") , // TODO: better error handling
159
+ . wrap_err ( eyre ! ( "could not create Gossipsub config") ) ? ,
158
160
)
159
- . expect ( "Valid behaviour" ) // TODO: better error handling
161
+ . map_err ( |e| eyre ! ( e ) )
160
162
}
0 commit comments