Skip to content

Commit 20368f0

Browse files
committed
Remove use of std::thread::sleep in async context.
1 parent 808865f commit 20368f0

File tree

4 files changed

+59
-17
lines changed

4 files changed

+59
-17
lines changed

beacon_node/http_api/src/publish_blocks.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ pub async fn publish_block<T: BeaconChainTypes, B: IntoGossipVerifiedBlock<T>>(
8686
network_globals: Arc<NetworkGlobals<T::EthSpec>>,
8787
) -> Result<Response, Rejection> {
8888
let seen_timestamp = timestamp_now();
89-
let block_publishing_delay = chain.config.block_publishing_delay;
90-
let data_column_publishing_delay = chain.config.data_column_publishing_delay;
89+
let block_publishing_delay_for_testing = chain.config.block_publishing_delay;
90+
let data_column_publishing_delay_for_testing = chain.config.data_column_publishing_delay;
9191

9292
let (unverified_block, unverified_blobs, is_locally_built_block) = match provenanced_block {
9393
ProvenancedBlock::Local(block, blobs, _) => (block, blobs, true),
@@ -105,13 +105,8 @@ pub async fn publish_block<T: BeaconChainTypes, B: IntoGossipVerifiedBlock<T>>(
105105
let publish_block_p2p = move |block: Arc<SignedBeaconBlock<T::EthSpec>>,
106106
sender,
107107
log,
108-
seen_timestamp,
109-
block_publishing_delay|
108+
seen_timestamp|
110109
-> Result<(), BlockError> {
111-
// Add delay before publishing the block to the network.
112-
if let Some(block_publishing_delay) = block_publishing_delay {
113-
std::thread::sleep(block_publishing_delay);
114-
}
115110
let publish_timestamp = timestamp_now();
116111
let publish_delay = publish_timestamp
117112
.checked_sub(seen_timestamp)
@@ -154,12 +149,19 @@ pub async fn publish_block<T: BeaconChainTypes, B: IntoGossipVerifiedBlock<T>>(
154149

155150
let should_publish_block = gossip_verified_block_result.is_ok();
156151
if BroadcastValidation::Gossip == validation_level && should_publish_block {
152+
if let Some(block_publishing_delay) = block_publishing_delay_for_testing {
153+
debug!(
154+
log,
155+
"Publishing block with artificial delay";
156+
"block_publishing_delay" => ?block_publishing_delay
157+
);
158+
tokio::time::sleep(block_publishing_delay).await;
159+
}
157160
publish_block_p2p(
158161
block.clone(),
159162
sender_clone.clone(),
160163
log.clone(),
161164
seen_timestamp,
162-
block_publishing_delay,
163165
)
164166
.map_err(|_| warp_utils::reject::custom_server_error("unable to publish".into()))?;
165167
}
@@ -175,7 +177,6 @@ pub async fn publish_block<T: BeaconChainTypes, B: IntoGossipVerifiedBlock<T>>(
175177
sender_clone.clone(),
176178
log.clone(),
177179
seen_timestamp,
178-
block_publishing_delay,
179180
)?,
180181
BroadcastValidation::ConsensusAndEquivocation => {
181182
check_slashable(&chain, block_root, &block_to_publish, &log)?;
@@ -184,7 +185,6 @@ pub async fn publish_block<T: BeaconChainTypes, B: IntoGossipVerifiedBlock<T>>(
184185
sender_clone.clone(),
185186
log.clone(),
186187
seen_timestamp,
187-
block_publishing_delay,
188188
)?;
189189
}
190190
};
@@ -217,9 +217,22 @@ pub async fn publish_block<T: BeaconChainTypes, B: IntoGossipVerifiedBlock<T>>(
217217
}
218218

219219
if gossip_verified_columns.iter().map(Option::is_some).count() > 0 {
220-
// Add delay before publishing the data columns to the network.
221-
if let Some(data_column_publishing_delay) = data_column_publishing_delay {
222-
tokio::time::sleep(data_column_publishing_delay).await;
220+
if let Some(data_column_publishing_delay) = data_column_publishing_delay_for_testing {
221+
// Subtract block publishing delay if it is also used.
222+
// Note: if `data_column_publishing_delay` is less than `block_publishing_delay`, it
223+
// will still be delayed by `block_publishing_delay`. This could be solved with spawning
224+
// async tasks but the limitation is minor and I believe it's probably not worth
225+
// affecting the mainnet code path.
226+
let block_publishing_delay = block_publishing_delay_for_testing.unwrap_or_default();
227+
let delay = data_column_publishing_delay.saturating_sub(block_publishing_delay);
228+
if !delay.is_zero() {
229+
debug!(
230+
log,
231+
"Publishing data columns with artificial delay";
232+
"data_column_publishing_delay" => ?data_column_publishing_delay
233+
);
234+
tokio::time::sleep(delay).await;
235+
}
223236
}
224237
publish_column_sidecars(network_tx, &gossip_verified_columns, &chain).map_err(|_| {
225238
warp_utils::reject::custom_server_error("unable to publish data column sidecars".into())

beacon_node/src/cli.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1597,7 +1597,8 @@ pub fn cli_app() -> Command {
15971597
.action(ArgAction::Set)
15981598
.help_heading(FLAG_HEADER)
15991599
.help("TESTING ONLY: Artificially delay block publishing by the specified number of seconds. \
1600-
DO NOT USE IN PRODUCTION.")
1600+
This only works for if `BroadcastValidation::Gossip` is used (default). \
1601+
DO NOT USE IN PRODUCTION.")
16011602
.hide(true)
16021603
.display_order(0)
16031604
)
@@ -1608,6 +1609,8 @@ pub fn cli_app() -> Command {
16081609
.action(ArgAction::Set)
16091610
.help_heading(FLAG_HEADER)
16101611
.help("TESTING ONLY: Artificially delay data column publishing by the specified number of seconds. \
1612+
Limitation: If `delay-block-publishing` is also used, data columns will be delayed for a \
1613+
minimum of `delay-block-publishing` seconds.
16111614
DO NOT USE IN PRODUCTION.")
16121615
.hide(true)
16131616
.display_order(0)

beacon_node/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,11 +888,11 @@ pub fn get_config<E: EthSpec>(
888888
clap_utils::parse_required(cli_args, "beacon-processor-aggregate-batch-size")?;
889889

890890
if let Some(delay) = clap_utils::parse_optional(cli_args, "delay-block-publishing")? {
891-
client_config.chain.block_publishing_delay = Some(Duration::from_secs(delay));
891+
client_config.chain.block_publishing_delay = Some(Duration::from_secs_f64(delay));
892892
}
893893

894894
if let Some(delay) = clap_utils::parse_optional(cli_args, "delay-data-column-publishing")? {
895-
client_config.chain.data_column_publishing_delay = Some(Duration::from_secs(delay));
895+
client_config.chain.data_column_publishing_delay = Some(Duration::from_secs_f64(delay));
896896
}
897897

898898
Ok(client_config)

lighthouse/tests/beacon_node.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2702,3 +2702,29 @@ fn beacon_node_backend_override() {
27022702
assert_eq!(config.store.backend, BeaconNodeBackend::LevelDb);
27032703
});
27042704
}
2705+
2706+
#[test]
2707+
fn block_publishing_delay_for_testing() {
2708+
CommandLineTest::new()
2709+
.flag("block_publishing_delay", Some("2.5"))
2710+
.run_with_zero_port()
2711+
.with_config(|config| {
2712+
assert_eq!(
2713+
chain.block_publishing_delay,
2714+
Some(Duration::from_secs_f64(2.5f64))
2715+
);
2716+
});
2717+
}
2718+
2719+
#[test]
2720+
fn data_column_publishing_delay_for_testing() {
2721+
CommandLineTest::new()
2722+
.flag("data_column_publishing_delay", Some("3.5"))
2723+
.run_with_zero_port()
2724+
.with_config(|config| {
2725+
assert_eq!(
2726+
chain.data_column_publishing_delay,
2727+
Some(Duration::from_secs_f64(3.5f64))
2728+
);
2729+
});
2730+
}

0 commit comments

Comments
 (0)