Skip to content

Commit c69b5d5

Browse files
authored
refactor: implement builder pattern for TransportManager (#453)
This PR implements the builder pattern for `TransportManager`. resolves #343
1 parent 3f13b10 commit c69b5d5

File tree

9 files changed

+192
-411
lines changed

9 files changed

+192
-411
lines changed

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl From<Role> for crate::yamux::Mode {
6868

6969
/// Configuration builder for [`Litep2p`](`crate::Litep2p`).
7070
pub struct ConfigBuilder {
71-
// TCP transport configuration.
71+
/// TCP transport configuration.
7272
tcp: Option<TcpConfig>,
7373

7474
/// QUIC transport config.

src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use crate::{
4040
request_response::RequestResponseProtocol,
4141
},
4242
transport::{
43-
manager::{SupportedTransport, TransportManager},
43+
manager::{SupportedTransport, TransportManager, TransportManagerBuilder},
4444
tcp::TcpTransport,
4545
TransportBuilder, TransportEvent,
4646
},
@@ -170,14 +170,15 @@ impl Litep2p {
170170
);
171171

172172
let supported_transports = Self::supported_transports(&litep2p_config);
173-
let (mut transport_manager, transport_handle) = TransportManager::new(
174-
litep2p_config.keypair.clone(),
175-
supported_transports,
176-
bandwidth_sink.clone(),
177-
litep2p_config.max_parallel_dials,
178-
litep2p_config.connection_limits,
179-
);
173+
let mut transport_manager = TransportManagerBuilder::new()
174+
.with_keypair(litep2p_config.keypair.clone())
175+
.with_supported_transports(supported_transports)
176+
.with_bandwidth_sink(bandwidth_sink.clone())
177+
.with_max_parallel_dials(litep2p_config.max_parallel_dials)
178+
.with_connection_limits_config(litep2p_config.connection_limits)
179+
.build();
180180

181+
let transport_handle = transport_manager.transport_manager_handle();
181182
// add known addresses to `TransportManager`, if any exist
182183
if !litep2p_config.known_addresses.is_empty() {
183184
for (peer, addresses) in litep2p_config.known_addresses {

src/protocol/libp2p/kademlia/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,18 +1250,15 @@ impl Kademlia {
12501250

12511251
#[cfg(test)]
12521252
mod tests {
1253-
use std::collections::HashSet;
1254-
12551253
use super::*;
12561254
use crate::{
12571255
codec::ProtocolCodec,
1258-
crypto::ed25519::Keypair,
12591256
transport::{
1260-
manager::{limits::ConnectionLimitsConfig, TransportManager},
1257+
manager::{TransportManager, TransportManagerBuilder},
12611258
KEEP_ALIVE_TIMEOUT,
12621259
},
12631260
types::protocol::ProtocolName,
1264-
BandwidthSink, ConnectionId,
1261+
ConnectionId,
12651262
};
12661263
use multiaddr::Protocol;
12671264
use multihash::Multihash;
@@ -1275,21 +1272,15 @@ mod tests {
12751272
}
12761273

12771274
fn make_kademlia() -> (Kademlia, Context, TransportManager) {
1278-
let (manager, handle) = TransportManager::new(
1279-
Keypair::generate(),
1280-
HashSet::new(),
1281-
BandwidthSink::new(),
1282-
8usize,
1283-
ConnectionLimitsConfig::default(),
1284-
);
1275+
let manager = TransportManagerBuilder::new().build();
12851276

12861277
let peer = PeerId::random();
12871278
let (transport_service, _tx) = TransportService::new(
12881279
peer,
12891280
ProtocolName::from("/kad/1"),
12901281
Vec::new(),
12911282
Default::default(),
1292-
handle,
1283+
manager.transport_manager_handle(),
12931284
KEEP_ALIVE_TIMEOUT,
12941285
);
12951286
let (event_tx, event_rx) = channel(64);

src/protocol/mdns.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,7 @@ impl Mdns {
363363
#[cfg(test)]
364364
mod tests {
365365
use super::*;
366-
use crate::{
367-
crypto::ed25519::Keypair,
368-
transport::manager::{limits::ConnectionLimitsConfig, TransportManager},
369-
BandwidthSink,
370-
};
366+
use crate::transport::manager::TransportManagerBuilder;
371367
use futures::StreamExt;
372368
use multiaddr::Protocol;
373369

@@ -378,16 +374,10 @@ mod tests {
378374
.try_init();
379375

380376
let (config1, mut stream1) = Config::new(Duration::from_secs(5));
381-
let (_manager1, handle1) = TransportManager::new(
382-
Keypair::generate(),
383-
HashSet::new(),
384-
BandwidthSink::new(),
385-
8usize,
386-
ConnectionLimitsConfig::default(),
387-
);
377+
let manager1 = TransportManagerBuilder::new().build();
388378

389379
let mdns1 = Mdns::new(
390-
handle1,
380+
manager1.transport_manager_handle(),
391381
config1,
392382
vec![
393383
"/ip6/::1/tcp/8888/p2p/12D3KooWNP463TyS3vUpmekjjZ2dg7xy1WHNMM7MqfsMevMTaaaa"
@@ -400,16 +390,10 @@ mod tests {
400390
);
401391

402392
let (config2, mut stream2) = Config::new(Duration::from_secs(5));
403-
let (_manager1, handle2) = TransportManager::new(
404-
Keypair::generate(),
405-
HashSet::new(),
406-
BandwidthSink::new(),
407-
8usize,
408-
ConnectionLimitsConfig::default(),
409-
);
393+
let manager2 = TransportManagerBuilder::new().build();
410394

411395
let mdns2 = Mdns::new(
412-
handle2,
396+
manager2.transport_manager_handle(),
413397
config2,
414398
vec![
415399
"/ip6/::1/tcp/9999/p2p/12D3KooWNP463TyS3vUpmekjjZ2dg7xy1WHNMM7MqfsMevMTbbbb"

src/protocol/notification/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const LOG_TARGET: &str = "litep2p::notification";
6868
/// Connection state.
6969
///
7070
/// Used to track transport level connectivity state when there is a pending validation.
71-
/// See [`PeerState::PendingValidation.`] for more details.
71+
/// See [`PeerState::ValidationPending`] for more details.
7272
#[derive(Debug, PartialEq, Eq)]
7373
enum ConnectionState {
7474
/// There is a active, transport-level connection open to the peer.

src/protocol/notification/tests/mod.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use std::collections::HashSet;
22-
2321
use crate::{
24-
crypto::ed25519::Keypair,
2522
executor::DefaultExecutor,
2623
protocol::{
2724
notification::{
@@ -30,11 +27,11 @@ use crate::{
3027
InnerTransportEvent, ProtocolCommand, TransportService,
3128
},
3229
transport::{
33-
manager::{limits::ConnectionLimitsConfig, TransportManager},
30+
manager::{TransportManager, TransportManagerBuilder},
3431
KEEP_ALIVE_TIMEOUT,
3532
},
3633
types::protocol::ProtocolName,
37-
BandwidthSink, PeerId,
34+
PeerId,
3835
};
3936

4037
use tokio::sync::mpsc::{channel, Receiver, Sender};
@@ -51,21 +48,15 @@ fn make_notification_protocol() -> (
5148
TransportManager,
5249
Sender<InnerTransportEvent>,
5350
) {
54-
let (manager, handle) = TransportManager::new(
55-
Keypair::generate(),
56-
HashSet::new(),
57-
BandwidthSink::new(),
58-
8usize,
59-
ConnectionLimitsConfig::default(),
60-
);
51+
let manager = TransportManagerBuilder::new().build();
6152

6253
let peer = PeerId::random();
6354
let (transport_service, tx) = TransportService::new(
6455
peer,
6556
ProtocolName::from("/notif/1"),
6657
Vec::new(),
6758
std::sync::Arc::new(Default::default()),
68-
handle,
59+
manager.transport_manager_handle(),
6960
KEEP_ALIVE_TIMEOUT,
7061
);
7162
let (config, handle) = NotificationConfig::new(

src/protocol/request_response/tests.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
// DEALINGS IN THE SOFTWARE.
2020

2121
use crate::{
22-
crypto::ed25519::Keypair,
2322
mock::substream::{DummySubstream, MockSubstream},
2423
protocol::{
2524
request_response::{
@@ -30,17 +29,17 @@ use crate::{
3029
},
3130
substream::Substream,
3231
transport::{
33-
manager::{limits::ConnectionLimitsConfig, TransportManager},
32+
manager::{TransportManager, TransportManagerBuilder},
3433
KEEP_ALIVE_TIMEOUT,
3534
},
3635
types::{RequestId, SubstreamId},
37-
BandwidthSink, Error, PeerId, ProtocolName,
36+
Error, PeerId, ProtocolName,
3837
};
3938

4039
use futures::StreamExt;
4140
use tokio::sync::mpsc::Sender;
4241

43-
use std::{collections::HashSet, task::Poll};
42+
use std::task::Poll;
4443

4544
// create new protocol for testing
4645
fn protocol() -> (
@@ -49,21 +48,15 @@ fn protocol() -> (
4948
TransportManager,
5049
Sender<InnerTransportEvent>,
5150
) {
52-
let (manager, handle) = TransportManager::new(
53-
Keypair::generate(),
54-
HashSet::new(),
55-
BandwidthSink::new(),
56-
8usize,
57-
ConnectionLimitsConfig::default(),
58-
);
51+
let manager = TransportManagerBuilder::new().build();
5952

6053
let peer = PeerId::random();
6154
let (transport_service, tx) = TransportService::new(
6255
peer,
6356
ProtocolName::from("/notif/1"),
6457
Vec::new(),
6558
std::sync::Arc::new(Default::default()),
66-
handle,
59+
manager.transport_manager_handle(),
6760
KEEP_ALIVE_TIMEOUT,
6861
);
6962
let (config, handle) =

0 commit comments

Comments
 (0)