@@ -48,6 +48,8 @@ impl DriaP2PClient {
48
48
///
49
49
/// The `version` is used to create the protocol strings for the client, and its very important that
50
50
/// they match with the clients existing within the network.
51
+ ///
52
+ /// If for any reason the given `listen_addr` is not available, it will try to listen on a random port on `localhost`.
51
53
#[ allow( clippy:: type_complexity) ]
52
54
pub fn new (
53
55
keypair : Keypair ,
@@ -62,7 +64,6 @@ impl DriaP2PClient {
62
64
) > {
63
65
// this is our peerId
64
66
let peer_id = keypair. public ( ) . to_peer_id ( ) ;
65
- log:: info!( "Compute node peer address: {}" , peer_id) ;
66
67
67
68
let mut swarm = SwarmBuilder :: with_existing_identity ( keypair)
68
69
. with_tokio ( )
@@ -123,19 +124,23 @@ impl DriaP2PClient {
123
124
124
125
// listen on all interfaces for incoming connections
125
126
log:: info!( "Listening p2p network on: {}" , listen_addr) ;
126
- swarm. listen_on ( listen_addr) ?;
127
+ if let Err ( e) = swarm. listen_on ( listen_addr) {
128
+ log:: error!( "Could not listen on address: {:?}" , e) ;
129
+ log:: warn!( "Trying fallback address with localhost random port" ) ;
130
+ swarm. listen_on ( "/ip4/127.0.0.1/tcp/0" . parse ( ) . unwrap ( ) ) ?;
131
+ }
127
132
128
133
// listen on relay addresses with p2p circuit
129
- for addr in & nodes. relay_nodes {
134
+ for addr in nodes. relay_nodes . iter ( ) . cloned ( ) {
130
135
log:: info!( "Listening to relay: {}" , addr) ;
131
- swarm. listen_on ( addr. clone ( ) . with ( Protocol :: P2pCircuit ) ) ?;
136
+ swarm. listen_on ( addr. with ( Protocol :: P2pCircuit ) ) ?;
132
137
}
133
138
134
139
// dial rpc nodes
135
- for rpc_addr in & nodes. rpc_nodes {
140
+ for rpc_addr in nodes. rpc_nodes . iter ( ) . cloned ( ) {
136
141
log:: info!( "Dialing RPC node: {}" , rpc_addr) ;
137
- if let Err ( e) = swarm. dial ( rpc_addr. clone ( ) ) {
138
- log:: error!( "Error dialing RPC node: {:?}" , e) ;
142
+ if let Err ( e) = swarm. dial ( rpc_addr) {
143
+ log:: error!( "Could not dial RPC node: {:?}" , e) ;
139
144
} ;
140
145
}
141
146
0 commit comments