Skip to content

Commit 98e2967

Browse files
serban300github-actions[bot]bkontur
authored
Implement retry in bridges equivocation loop (#11283)
Implement retry mechanism in bridges equivocation loop --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Branislav Kontur <bkontur@gmail.com>
1 parent 7176c69 commit 98e2967

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

bridges/relays/equivocation/src/block_checker.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use bp_header_chain::{FinalityProof, FindEquivocations as FindEquivocationsT};
2323
use finality_relay::FinalityProofsBuf;
2424
use futures::future::{BoxFuture, FutureExt};
2525
use num_traits::Saturating;
26+
use std::time::Duration;
2627

2728
/// First step in the block checking state machine.
2829
///
@@ -217,6 +218,44 @@ impl<P: EquivocationDetectionPipeline> BlockChecker<P> {
217218
Self::ReadSyncedHeaders(ReadSyncedHeaders { target_block_num })
218219
}
219220

221+
pub fn run_with_retry<'a, SC: SourceClient<P>, TC: TargetClient<P>>(
222+
self,
223+
source_client: &'a mut SC,
224+
target_client: &'a mut TC,
225+
finality_proofs_buf: &'a mut FinalityProofsBuf<P>,
226+
reporter: &'a mut EquivocationsReporter<P, SC>,
227+
retry_params: (u32, Duration),
228+
) -> BoxFuture<'a, Result<(), Self>> {
229+
let (max_attempts, retry_tick) = retry_params;
230+
async move {
231+
let mut block_checker = self;
232+
let mut retry_range = (0..max_attempts).peekable();
233+
while let Some(_) = retry_range.next() {
234+
block_checker = match block_checker
235+
.run(source_client, target_client, finality_proofs_buf, reporter)
236+
.await
237+
{
238+
Ok(_) => return Ok(()),
239+
Err(err) => err,
240+
};
241+
242+
// We don't need to sleep after the last attempt
243+
if retry_range.peek().is_some() {
244+
tracing::warn!(
245+
target: "bridge",
246+
source=%P::SOURCE_NAME,
247+
target=%P::TARGET_NAME,
248+
"Error running block checker. Retrying."
249+
);
250+
tokio::time::sleep(retry_tick).await;
251+
}
252+
}
253+
254+
Err(block_checker)
255+
}
256+
.boxed()
257+
}
258+
220259
pub fn run<'a, SC: SourceClient<P>, TC: TargetClient<P>>(
221260
self,
222261
source_client: &'a mut SC,

bridges/relays/equivocation/src/equivocation_loop.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,12 @@ impl<P: EquivocationDetectionPipeline, SC: SourceClient<P>, TC: TargetClient<P>>
109109
self.finality_proofs_buf.fill(&mut self.finality_proofs_stream);
110110
let block_checker = BlockChecker::new(current_block_number);
111111
let _ = block_checker
112-
.run(
112+
.run_with_retry(
113113
&mut self.source_client,
114114
&mut self.target_client,
115115
&mut self.finality_proofs_buf,
116116
&mut self.reporter,
117+
(5, tick),
117118
)
118119
.await;
119120
current_block_number = current_block_number.saturating_add(1.into());

prdoc/pr_11283.prdoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
title: Implement retry in bridges equivocation loop
2+
doc:
3+
- audience: Node Dev
4+
description: Implement retry mechanism in bridges equivocation loop
5+
crates: []

0 commit comments

Comments
 (0)