diff --git a/codechain/main.rs b/codechain/main.rs index f3fc26d88f..7f77a077dd 100644 --- a/codechain/main.rs +++ b/codechain/main.rs @@ -273,6 +273,10 @@ impl NetworkControl for DummyNetworkService { fn is_connected(&self, _addr: &SocketAddr) -> Result { Err(NetworkControlError::Disabled) } + + fn get_port(&self) -> Result { + Err(NetworkControlError::Disabled) + } } fn run_node(matches: ArgMatches) -> Result<(), String> { diff --git a/network/src/control.rs b/network/src/control.rs index 3454c7774d..23d639bf73 100644 --- a/network/src/control.rs +++ b/network/src/control.rs @@ -25,6 +25,7 @@ pub trait Control: Send + Sync { fn connect(&self, addr: SocketAddr) -> Result<(), Error>; fn disconnect(&self, addr: SocketAddr) -> Result<(), Error>; fn is_connected(&self, addr: &SocketAddr) -> Result; + fn get_port(&self) -> Result; } #[derive(Clone, Debug)] diff --git a/network/src/service.rs b/network/src/service.rs index 31f4c545cb..4baaf72cd2 100644 --- a/network/src/service.rs +++ b/network/src/service.rs @@ -35,6 +35,7 @@ pub struct Service { timer: IoService, client: Arc, routing_table: Arc, + socket_address: SocketAddr, } impl Service { @@ -68,6 +69,7 @@ impl Service { timer, client, routing_table, + socket_address: address, })) } @@ -126,6 +128,10 @@ impl Control for Service { fn is_connected(&self, addr: &SocketAddr) -> Result { Ok(self.routing_table.is_connected(addr)) } + + fn get_port(&self) -> Result { + Ok(self.socket_address.port()) + } } #[derive(Debug)] diff --git a/rpc/src/v1/impls/net.rs b/rpc/src/v1/impls/net.rs index 86db07fe17..8ee85c91da 100644 --- a/rpc/src/v1/impls/net.rs +++ b/rpc/src/v1/impls/net.rs @@ -54,4 +54,8 @@ impl Net for NetClient { fn is_connected(&self, address: ::std::net::IpAddr, port: u16) -> Result { Ok(self.network_control.is_connected(&SocketAddr::new(address, port)).map_err(errors::network_control)?) } + + fn get_port(&self) -> Result { + Ok(self.network_control.get_port().map_err(errors::network_control)?) + } } diff --git a/rpc/src/v1/traits/net.rs b/rpc/src/v1/traits/net.rs index 03efeef10c..450d282866 100644 --- a/rpc/src/v1/traits/net.rs +++ b/rpc/src/v1/traits/net.rs @@ -30,5 +30,8 @@ build_rpc_trait! { # [rpc(name = "net_isConnected")] fn is_connected(&self, ::std::net::IpAddr, u16) -> Result; + + # [rpc(name = "net_getPort")] + fn get_port(&self) -> Result; } }