Skip to content

Commit 739fe13

Browse files
committed
Factor out await_with_slow_hook
1 parent f671291 commit 739fe13

2 files changed

Lines changed: 36 additions & 21 deletions

File tree

crates/node/src/assets.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,7 @@ where
289289
continue;
290290
}
291291
received = self.hot_receiver.recv_async() => {
292-
// can't fail, because self keeps a sender.
293-
let (id, value) = received.unwrap();
292+
let (id, value) = received.expect("should never fail because self keeps a sender");
294293
match self.cold_queue.lock().unwrap().add_if_condition_not_satisfied(id, value) {
295294
ColdQueueAddIfNotSatisfiedResult::ConditionSatisfied(value) => {
296295
return (id, value);
@@ -323,9 +322,8 @@ where
323322
continue;
324323
}
325324
received = self.hot_receiver.recv_async() => {
326-
// can't fail, because self keeps a sender.
327-
let (id, val) = received.unwrap();
328-
self.cold_queue.lock().unwrap().ingest(id, val);
325+
let (id, value) = received.expect("should never fail because self keeps a sender");
326+
self.cold_queue.lock().unwrap().ingest(id, value);
329327
}
330328
}
331329
}
@@ -640,6 +638,8 @@ where
640638
update.delete(self.col, &self.make_key(id));
641639
update
642640
.commit()
641+
// TODO(#4090): propagate err instead in here and rest of the functions
642+
// in this file.
643643
.expect("Unrecoverable error writing to database");
644644
(id, val)
645645
}

crates/node/src/providers/verify_foreign_tx/sign.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ fn build_signature_request(
5252
})
5353
}
5454

55+
// Awaits on the future for specified grace duration, and calls on_slow
56+
// if grace period expires.
57+
async fn await_with_slow_hook<F: Future>(
58+
grace: Duration,
59+
fut: F,
60+
on_slow: impl FnOnce(),
61+
) -> F::Output {
62+
tokio::pin!(fut);
63+
match timeout(grace, &mut fut).await {
64+
Ok(output) => output,
65+
Err(_) => {
66+
on_slow();
67+
fut.await
68+
}
69+
}
70+
}
71+
5572
impl VerifyForeignTxProvider {
5673
pub(crate) async fn make_verify_foreign_tx_leader(
5774
&self,
@@ -90,22 +107,20 @@ impl VerifyForeignTxProvider {
90107
// alive condition and be a subset of the chain's supporters. Since
91108
// presignature generation is not chain-aware, a compatible one may
92109
// not exist yet, count and log when the take has to wait.
93-
let take = keyshare
94-
.presignature_store
95-
.take_owned_matching(chain_supporters.iter().copied().collect());
96-
tokio::pin!(take);
97-
let (presignature_id, presignature) =
98-
match timeout(PRESIGNATURE_TAKE_GRACE_PERIOD, &mut take).await {
99-
Ok(taken) => taken,
100-
Err(_) => {
101-
metrics::MPC_NUM_VERIFY_FOREIGN_TX_PRESIGNATURE_WAITS.inc();
102-
tracing::warn!(
103-
?requested_chain,
104-
"no chain-compatible presignature available, waiting"
105-
);
106-
take.await
107-
}
108-
};
110+
let (presignature_id, presignature) = await_with_slow_hook(
111+
PRESIGNATURE_TAKE_GRACE_PERIOD,
112+
keyshare
113+
.presignature_store
114+
.take_owned_matching(chain_supporters.iter().copied().collect()),
115+
|| {
116+
metrics::MPC_NUM_VERIFY_FOREIGN_TX_PRESIGNATURE_WAITS.inc();
117+
tracing::warn!(
118+
?requested_chain,
119+
"no chain-compatible presignatures available, waiting"
120+
)
121+
},
122+
)
123+
.await;
109124
let participants = presignature.participants.clone();
110125
let channel = self.ecdsa_signature_provider.new_channel_for_task(
111126
VerifyForeignTxTaskId::VerifyForeignTx {

0 commit comments

Comments
 (0)