Skip to content

Commit 2e40551

Browse files
committed
Add port-alloc utility.
1 parent 0c4b46b commit 2e40551

File tree

22 files changed

+337
-119
lines changed

22 files changed

+337
-119
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ generic-array = { version = "0.14.7", features = ["serde", "zeroize"] }
7070
http = "1.3.1"
7171
jiff = { version = "0.2", default-features = false, features = ["serde", "std"] }
7272
minicbor = { version = "2.1.1", features = ["full"] }
73+
minicbor-io = { version = "0.23.0", features = ["async-io"] }
7374
nohash-hasher = "0.2"
7475
parking_lot = "0.12.3"
75-
portpicker = "0.1.1"
7676
prometheus = "0.14"
7777
prost = "0.14.1"
7878
quickcheck = "1.0.3"
@@ -94,7 +94,7 @@ thiserror = "2.0"
9494
tokio = { version = "1", default-features = false, features = ["full"] }
9595
tokio-stream = "0.1.17"
9696
tokio-tungstenite = { version = "0.27.0", features = ["rustls-tls-webpki-roots", "url"] }
97-
tokio-util = "0.7.15"
97+
tokio-util = { version = "0.7.15", features = ["compat"] }
9898
toml = "0.9"
9999
toml_edit = "0.22"
100100
tonic = "0.14.1"

cliquenet/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@ turmoil = { workspace = true, optional = true }
2424

2525
[dev-dependencies]
2626
criterion = { workspace = true }
27-
portpicker = { workspace = true }
2827
quickcheck = { workspace = true }
2928

3029
[[bench]]
3130
name = "network_bytes"
3231
harness = false
33-

cliquenet/tests/frame-handling.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use std::net::{Ipv4Addr, SocketAddr};
33
use bytes::BytesMut;
44
use cliquenet::{Network, NetworkMetrics, Overlay, overlay::Data};
55
use multisig::{Keypair, PublicKey, x25519};
6-
use portpicker::pick_unused_port;
76
use rand::{Rng, RngCore};
87
use tokio::time::{Duration, timeout};
98

@@ -20,12 +19,12 @@ async fn multiple_frames() {
2019
(
2120
party_a_sign.public_key(),
2221
party_a_dh.public_key(),
23-
(Ipv4Addr::LOCALHOST, pick_unused_port().unwrap()).into(),
22+
(Ipv4Addr::LOCALHOST, 50000).into(),
2423
),
2524
(
2625
party_b_sign.public_key(),
2726
party_b_dh.public_key(),
28-
(Ipv4Addr::LOCALHOST, pick_unused_port().unwrap()).into(),
27+
(Ipv4Addr::LOCALHOST, 51000).into(),
2928
),
3029
];
3130

justfile

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ build_docker:
2121
build-contracts:
2222
forge build
2323

24+
[private]
25+
build-tools:
26+
cargo build --release --features bin --bin run --bin port-alloc
27+
2428
####################
2529
###CHECK COMMANDS###
2630
####################
@@ -127,21 +131,25 @@ verify_blocks *ARGS:
127131
####################
128132
####TEST COMMANDS###
129133
####################
130-
test *ARGS:
131-
cargo nextest run {{ARGS}}
134+
test *ARGS: build-tools
135+
target/release/run --with target/release/port-alloc cargo nextest run {{ARGS}}
132136
@if [ "{{ARGS}}" == "" ]; then cargo test --doc; fi
133137

134138
test-contracts: build-contracts
135139
forge test
136140

137-
test_ci *ARGS:
138-
env {{LOG_LEVELS}} NO_COLOR=1 cargo nextest run --workspace {{ARGS}}
141+
test_ci *ARGS: build-tools
142+
env {{LOG_LEVELS}} NO_COLOR=1 target/release/run \
143+
--with target/release/port-alloc \
144+
-- cargo nextest run --workspace {{ARGS}}
139145
env {{LOG_LEVELS}} NO_COLOR=1 cargo test --doc {{ARGS}}
140146

141-
test-individually:
147+
test-individually: build-tools
142148
@for pkg in $(cargo metadata --no-deps --format-version 1 | jq -r '.packages[].name'); do \
143149
echo "Testing $pkg"; \
144-
cargo nextest run --no-tests=pass -p $pkg || exit 1; \
150+
target/release/run \
151+
--with target/release/port-alloc \
152+
-- cargo nextest run --no-tests=pass -p $pkg || exit 1; \
145153
done
146154

147155
test-contract-deploy *ARGS:

tests/Cargo.toml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ futures = { workspace = true }
1717
metrics = { path = "../metrics" }
1818
multisig = { path = "../multisig" }
1919
parking_lot = { workspace = true }
20-
portpicker = { workspace = true }
2120
rand = { workspace = true }
2221
sailfish = { path = "../sailfish", features = ["test"] }
2322
sailfish-types = { path = "../sailfish-types" }
@@ -33,11 +32,22 @@ url = { workspace = true }
3332
# optional:
3433
anyhow = { workspace = true, optional = true }
3534
clap = { workspace = true, optional = true }
35+
minicbor-io = { workspace = true, optional = true }
3636

3737
[features]
38-
bin = ["dep:anyhow", "dep:clap"]
38+
bin = ["dep:anyhow", "dep:clap", "dep:minicbor-io"]
3939

4040
[[bin]]
4141
name = "block-verifier"
4242
path = "src/binaries/block-verifier.rs"
4343
required-features = ["bin"]
44+
45+
[[bin]]
46+
name = "run"
47+
path = "src/binaries/run.rs"
48+
required-features = ["bin"]
49+
50+
[[bin]]
51+
name = "port-alloc"
52+
path = "src/binaries/port-alloc.rs"
53+
required-features = ["bin"]

tests/src/binaries/port-alloc.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use std::{
2+
net::Ipv4Addr,
3+
sync::{
4+
Arc,
5+
atomic::{AtomicU16, Ordering},
6+
},
7+
};
8+
9+
use anyhow::{Context, Result};
10+
use minicbor_io::{AsyncReader, AsyncWriter};
11+
use timeboost_utils::ports::{ALLOCATOR_PORT, Request, Response};
12+
use tokio::net::{TcpListener, TcpStream};
13+
use tokio_util::{
14+
compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt},
15+
task::TaskTracker,
16+
};
17+
18+
#[tokio::main]
19+
async fn main() -> Result<()> {
20+
let tasks = TaskTracker::new();
21+
let counter = Arc::new(AtomicU16::new(2048));
22+
23+
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, ALLOCATOR_PORT))
24+
.await
25+
.context("allocator port is in use")?;
26+
27+
loop {
28+
let Ok((stream, _)) = listener.accept().await else {
29+
continue;
30+
};
31+
tasks.spawn(alloc(stream, counter.clone()));
32+
}
33+
}
34+
35+
async fn alloc(mut stream: TcpStream, ctr: Arc<AtomicU16>) -> Result<()> {
36+
let mut reader = AsyncReader::new((&mut stream).compat());
37+
let Some(request) = reader.read().await? else {
38+
return Ok(());
39+
};
40+
let mut writer = AsyncWriter::new(stream.compat_write());
41+
match request {
42+
Request::Alloc(n) => {
43+
let mut ports = Vec::new();
44+
for _ in 0..n {
45+
loop {
46+
let port = ctr.fetch_add(1, Ordering::Relaxed);
47+
if TcpListener::bind((Ipv4Addr::LOCALHOST, port)).await.is_ok() {
48+
ports.push(port);
49+
break;
50+
}
51+
}
52+
}
53+
writer.write(Response::Ports(ports)).await?;
54+
}
55+
}
56+
Ok(())
57+
}

0 commit comments

Comments
 (0)