@@ -2,6 +2,7 @@ use std::time::Duration;
2
2
3
3
use libp2p:: kad:: { Addresses , KBucketKey , KBucketRef } ;
4
4
use libp2p:: request_response:: { OutboundRequestId , ResponseChannel } ;
5
+ use libp2p:: swarm:: behaviour:: toggle:: Toggle ;
5
6
use libp2p:: swarm:: NetworkBehaviour ;
6
7
use libp2p:: { gossipsub, identify, ping} ;
7
8
use libp2p_broadcast as broadcast;
@@ -66,10 +67,10 @@ impl From<discovery::NetworkEvent> for NetworkEvent {
66
67
pub struct Behaviour {
67
68
pub identify : identify:: Behaviour ,
68
69
pub ping : ping:: Behaviour ,
69
- pub gossipsub : gossipsub:: Behaviour ,
70
- pub broadcast : broadcast:: Behaviour ,
71
- pub sync : sync:: Behaviour ,
72
- pub discovery : discovery:: Behaviour ,
70
+ pub gossipsub : Toggle < gossipsub:: Behaviour > ,
71
+ pub broadcast : Toggle < broadcast:: Behaviour > ,
72
+ pub sync : Toggle < sync:: Behaviour > ,
73
+ pub discovery : Toggle < discovery:: Behaviour > ,
73
74
}
74
75
75
76
/// Dummy implementation of Debug for Behaviour.
@@ -82,6 +83,8 @@ impl std::fmt::Debug for Behaviour {
82
83
impl discovery:: DiscoveryClient for Behaviour {
83
84
fn add_address ( & mut self , peer : & PeerId , address : Multiaddr ) -> libp2p:: kad:: RoutingUpdate {
84
85
self . discovery
86
+ . as_mut ( )
87
+ . expect ( "Discovery behaviour should be available" )
85
88
. kademlia
86
89
. as_mut ( )
87
90
. expect ( "Kademlia behaviour should be available" )
@@ -90,22 +93,32 @@ impl discovery::DiscoveryClient for Behaviour {
90
93
91
94
fn kbuckets ( & mut self ) -> impl Iterator < Item = KBucketRef < ' _ , KBucketKey < PeerId > , Addresses > > {
92
95
self . discovery
96
+ . as_mut ( )
97
+ . expect ( "Discovery behaviour should be available" )
93
98
. kademlia
94
99
. as_mut ( )
95
100
. expect ( "Kademlia behaviour should be available" )
96
101
. kbuckets ( )
97
102
}
98
103
99
104
fn send_request ( & mut self , peer_id : & PeerId , req : discovery:: Request ) -> OutboundRequestId {
100
- self . discovery . request_response . send_request ( peer_id, req)
105
+ self . discovery
106
+ . as_mut ( )
107
+ . expect ( "Discovery behaviour should be available" )
108
+ . request_response
109
+ . send_request ( peer_id, req)
101
110
}
102
111
103
112
fn send_response (
104
113
& mut self ,
105
114
ch : ResponseChannel < discovery:: Response > ,
106
115
rs : discovery:: Response ,
107
116
) -> Result < ( ) , discovery:: Response > {
108
- self . discovery . request_response . send_response ( ch, rs)
117
+ self . discovery
118
+ . as_mut ( )
119
+ . expect ( "Discovery behaviour should be available" )
120
+ . request_response
121
+ . send_response ( ch, rs)
109
122
}
110
123
}
111
124
@@ -144,35 +157,45 @@ impl Behaviour {
144
157
145
158
let ping = ping:: Behaviour :: new ( ping:: Config :: new ( ) . with_interval ( Duration :: from_secs ( 5 ) ) ) ;
146
159
147
- let gossipsub = gossipsub:: Behaviour :: new_with_metrics (
148
- gossipsub:: MessageAuthenticity :: Signed ( keypair. clone ( ) ) ,
149
- gossipsub_config ( config. gossipsub , config. pubsub_max_size ) ,
150
- registry. sub_registry_with_prefix ( "gossipsub" ) ,
151
- Default :: default ( ) ,
152
- )
153
- . unwrap ( ) ;
154
-
155
- let broadcast = broadcast:: Behaviour :: new_with_metrics (
156
- broadcast:: Config {
157
- max_buf_size : config. pubsub_max_size ,
158
- } ,
159
- registry. sub_registry_with_prefix ( "broadcast" ) ,
160
- ) ;
161
-
162
- let sync = sync:: Behaviour :: new_with_metrics (
163
- sync:: Config :: default ( ) . with_max_response_size ( config. rpc_max_size ) ,
164
- registry. sub_registry_with_prefix ( "sync" ) ,
165
- ) ;
166
-
167
- let discovery = discovery:: Behaviour :: new ( keypair, config. discovery ) ;
160
+ let gossipsub = config. pubsub_protocol . is_gossipsub ( ) . then ( || {
161
+ gossipsub:: Behaviour :: new_with_metrics (
162
+ gossipsub:: MessageAuthenticity :: Signed ( keypair. clone ( ) ) ,
163
+ gossipsub_config ( config. gossipsub , config. pubsub_max_size ) ,
164
+ registry. sub_registry_with_prefix ( "gossipsub" ) ,
165
+ Default :: default ( ) ,
166
+ )
167
+ . unwrap ( )
168
+ } ) ;
169
+
170
+ let enable_broadcast = config. pubsub_protocol . is_broadcast ( ) || config. enable_sync ;
171
+ let broadcast = enable_broadcast. then ( || {
172
+ broadcast:: Behaviour :: new_with_metrics (
173
+ broadcast:: Config {
174
+ max_buf_size : config. pubsub_max_size ,
175
+ } ,
176
+ registry. sub_registry_with_prefix ( "broadcast" ) ,
177
+ )
178
+ } ) ;
179
+
180
+ let sync = config. enable_sync . then ( || {
181
+ sync:: Behaviour :: new_with_metrics (
182
+ sync:: Config :: default ( ) . with_max_response_size ( config. rpc_max_size ) ,
183
+ registry. sub_registry_with_prefix ( "sync" ) ,
184
+ )
185
+ } ) ;
186
+
187
+ let discovery = config
188
+ . discovery
189
+ . enabled
190
+ . then ( || discovery:: Behaviour :: new ( keypair, config. discovery ) ) ;
168
191
169
192
Self {
170
193
identify,
171
194
ping,
172
- gossipsub ,
173
- broadcast ,
174
- sync ,
175
- discovery,
195
+ sync : Toggle :: from ( sync ) ,
196
+ gossipsub : Toggle :: from ( gossipsub ) ,
197
+ broadcast : Toggle :: from ( broadcast ) ,
198
+ discovery : Toggle :: from ( discovery ) ,
176
199
}
177
200
}
178
201
}
0 commit comments