Skip to content

Commit 3560553

Browse files
akundazSozinM
andauthored
fix flashblock timing (#34)
* fix clippy * fix port allocation in tests * unit test flashblock timing * write tests, get them working, introduce new step to stop flashblocks * zero remaining time unit test * add log message back in and move around some state access * respect first flashblock interval with better state management * revert pipeline organization * clippy * fmt * change rblib --------- Co-authored-by: Solar Mithril <[email protected]>
1 parent a65cdb1 commit 3560553

File tree

11 files changed

+797
-327
lines changed

11 files changed

+797
-327
lines changed

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jemalloc = ["rblib/jemalloc"]
3434
debug = ["tokio/full", "tokio/tracing", "dep:console-subscriber"]
3535

3636
[dependencies]
37-
rblib = { git = "https://github.com/flashbots/rblib", rev = "989b942d0facdde135f05bf66a376e30ce21ebc7" }
37+
rblib = { git = "https://github.com/flashbots/rblib", rev = "198cbe65e59f7467c756d5ba6422155c98402291" }
3838

3939
futures = "0.3"
4040
tokio = "1.46"
@@ -72,9 +72,10 @@ reth-provider = { git = "https://github.com/paradigmxyz/reth", tag = "v1.8.2" }
7272

7373
# debug flag
7474
console-subscriber = { version = "0.4", optional = true }
75+
tracing-subscriber = "0.3.20"
7576

7677
[dev-dependencies]
77-
rblib = { git = "https://github.com/flashbots/rblib", rev = "989b942d0facdde135f05bf66a376e30ce21ebc7", features = [
78+
rblib = { git = "https://github.com/flashbots/rblib", rev = "198cbe65e59f7467c756d5ba6422155c98402291", features = [
7879
"test-utils",
7980
] }
8081

src/args.rs

Lines changed: 23 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -215,40 +215,15 @@ fn expand_path(s: &str) -> Result<PathBuf> {
215215
impl FlashblocksArgs {
216216
/// Configures flashblocks for tests. Handles WS port assignment.
217217
pub fn default_on_for_tests() -> Self {
218-
use {
219-
core::net::{Ipv4Addr, SocketAddrV4},
220-
std::{
221-
collections::HashSet,
222-
net::TcpListener,
223-
sync::{Mutex, OnceLock},
224-
},
225-
};
226-
227-
static RESERVED_PORTS: OnceLock<Mutex<HashSet<u16>>> = OnceLock::new();
228-
let reserved = RESERVED_PORTS.get_or_init(|| Mutex::new(HashSet::new()));
229-
230-
let port = (12000..19000)
231-
.find(|port| {
232-
let addr = format!("0.0.0.0:{port}");
233-
if let Ok(listener) = TcpListener::bind(&addr) {
234-
drop(listener);
235-
let mut set = reserved.lock().unwrap();
236-
if set.contains(port) {
237-
false
238-
} else {
239-
set.insert(*port);
240-
true
241-
}
242-
} else {
243-
false
244-
}
245-
})
246-
.expect("No available ports found for test");
218+
use core::net::{Ipv4Addr, SocketAddrV4};
247219

248220
Self {
249221
interval: Duration::from_millis(250),
250222
leeway_time: Duration::from_millis(75),
251-
enabled: Some(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port).into()),
223+
enabled: Some(
224+
SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, Self::get_available_port())
225+
.into(),
226+
),
252227
calculate_state_root: true,
253228
}
254229
}
@@ -257,41 +232,29 @@ impl FlashblocksArgs {
257232
leeway_time: Duration,
258233
interval: Duration,
259234
) -> Self {
260-
use {
261-
core::net::{Ipv4Addr, SocketAddrV4},
262-
std::{
263-
collections::HashSet,
264-
net::TcpListener,
265-
sync::{Mutex, OnceLock},
266-
},
267-
};
268-
269-
static RESERVED_PORTS: OnceLock<Mutex<HashSet<u16>>> = OnceLock::new();
270-
let reserved = RESERVED_PORTS.get_or_init(|| Mutex::new(HashSet::new()));
271-
272-
let port = (12000..19000)
273-
.find(|port| {
274-
let addr = format!("0.0.0.0:{port}");
275-
if let Ok(listener) = TcpListener::bind(&addr) {
276-
drop(listener);
277-
let mut set = reserved.lock().unwrap();
278-
if set.contains(port) {
279-
false
280-
} else {
281-
set.insert(*port);
282-
true
283-
}
284-
} else {
285-
false
286-
}
287-
})
288-
.expect("No available ports found for test");
235+
use core::net::{Ipv4Addr, SocketAddrV4};
289236

290237
Self {
291238
interval,
292239
leeway_time,
293-
enabled: Some(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port).into()),
240+
enabled: Some(
241+
SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, Self::get_available_port())
242+
.into(),
243+
),
294244
calculate_state_root: true,
295245
}
296246
}
247+
248+
/// Gets an available port by first binding to port 0 -- instructing the OS to
249+
/// find and assign one. Then the listener is dropped when this goes out of
250+
/// scope, freeing the port for the next time this function is called.
251+
fn get_available_port() -> u16 {
252+
use std::net::TcpListener;
253+
254+
TcpListener::bind("127.0.0.1:0")
255+
.expect("Failed to bind to random port")
256+
.local_addr()
257+
.expect("Failed to get local address")
258+
.port()
259+
}
297260
}

0 commit comments

Comments
 (0)