Skip to content
This repository was archived by the owner on Mar 23, 2021. It is now read-only.

Commit 05bf1aa

Browse files
Replace port-check with custom async free_local_port function
Now that the `new` function is async, we should not use blocking calls to std. Since we will eventually transition to async-std anyway, we can start depending on it here.
1 parent e56aa3f commit 05bf1aa

File tree

7 files changed

+156
-34
lines changed

7 files changed

+156
-34
lines changed

Cargo.lock

Lines changed: 130 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ edition = "2018"
66

77
[dependencies]
88
anyhow = "1.0.25"
9+
async-std = "1.2.0"
910
derive_more = "0.99.2"
1011
dirs = "2"
1112
emerald-vault-core = { git = "http://github.com/thomaseizinger/emerald-vault.git", branch = "create-comit-app-compatible", default-features = false }
@@ -17,7 +18,6 @@ hex = "0.4.0"
1718
hex-literal = "0.2.1"
1819
hex-serde = "0.1.0"
1920
lazy_static = "1.4"
20-
port_check = "0.1"
2121
reqwest = { version = "0.9", default-features = false }
2222
rust_bitcoin = { version = "0.19.1", package = "bitcoin" }
2323
secp256k1 = { version = "0.12", features = ["rand"] }

src/docker/bitcoin.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::docker::{self, DockerImage, LogMessage, DOCKER_NETWORK};
1+
use crate::docker::{
2+
self, free_local_port::free_local_port, DockerImage, LogMessage, DOCKER_NETWORK,
3+
};
24
use anyhow::Context;
35
use futures::compat::Future01CompatExt;
46
use reqwest::r#async::Client;
@@ -56,14 +58,12 @@ pub async fn new_bitcoind_instance() -> anyhow::Result<BitcoindInstance> {
5658
"-txindex",
5759
]);
5860

59-
let p2p_port =
60-
port_check::free_local_port().ok_or(anyhow::anyhow!("failed to grab a free local port"))?;
61+
let p2p_port = free_local_port().await?;
6162
options_builder.expose(18444, "tcp", p2p_port as u32);
6263

6364
let p2p_uri = BitcoindP2PUri { port: p2p_port };
6465

65-
let http_port =
66-
port_check::free_local_port().ok_or(anyhow::anyhow!("failed to grab a free local port"))?;
66+
let http_port = free_local_port().await?;
6767
options_builder.expose(18443, "tcp", http_port as u32);
6868

6969
let http_endpoint = BitcoindHttpEndpoint { port: http_port };

src/docker/cnd.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
docker::{self, DockerImage, LogMessage, DOCKER_NETWORK},
2+
docker::{self, free_local_port::free_local_port, DockerImage, LogMessage, DOCKER_NETWORK},
33
temp_fs,
44
};
55
use futures::compat::Future01CompatExt;
@@ -44,8 +44,7 @@ pub async fn new_instance(index: u32) -> anyhow::Result<CndInstance> {
4444
options_builder.cmd(vec!["--", "cnd", "--config=/config/cnd.toml"]);
4545
options_builder.volumes(vec![&format!("{}:/config", config_folder.display())]);
4646

47-
let http_port =
48-
port_check::free_local_port().ok_or(anyhow::anyhow!("failed to grab a free local port"))?;
47+
let http_port = free_local_port().await?;
4948
options_builder.expose(8080, "tcp", http_port as u32);
5049

5150
let options = options_builder.build();

src/docker/ethereum.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::docker::{self, DockerImage, LogMessage, DOCKER_NETWORK};
1+
use crate::docker::{
2+
self, free_local_port::free_local_port, DockerImage, LogMessage, DOCKER_NETWORK,
3+
};
24
use emerald_rs::PrivateKey;
35
use futures::compat::Future01CompatExt;
46
use lazy_static::lazy_static;
@@ -44,8 +46,7 @@ pub async fn new_parity_instance() -> anyhow::Result<ParityInstance> {
4446
options_builder.name("ethereum");
4547
options_builder.network_mode(DOCKER_NETWORK);
4648

47-
let http_port =
48-
port_check::free_local_port().ok_or(anyhow::anyhow!("failed to grab a free local port"))?;
49+
let http_port = free_local_port().await?;
4950
options_builder.expose(8545, "tcp", http_port as u32);
5051

5152
let http_endpoint = ParityHttpEndpoint { port: http_port };

src/docker/free_local_port.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use anyhow::Context;
2+
use async_std::net::TcpListener;
3+
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
4+
5+
pub async fn free_local_port() -> anyhow::Result<u16> {
6+
let socket = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 0));
7+
let listener = TcpListener::bind(&socket)
8+
.await
9+
.with_context(|| format!("unable to bind to {}", socket))?;
10+
let socket_addr = listener.local_addr()?;
11+
12+
Ok(socket_addr.port())
13+
}

src/docker/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use tokio::prelude::{stream::Stream, Future};
88
pub mod bitcoin;
99
pub mod cnd;
1010
pub mod ethereum;
11+
mod free_local_port;
1112

1213
pub const DOCKER_NETWORK: &str = "create-comit-app";
1314

0 commit comments

Comments
 (0)