Skip to content

Commit f6684dd

Browse files
committed
silence cometbft rpc unavailable block err logs
1 parent 68f3cb4 commit f6684dd

File tree

3 files changed

+92
-11
lines changed

3 files changed

+92
-11
lines changed

chain/src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use clap::Parser;
1616
use namada_sdk::masp_primitives::transaction::Transaction as MaspTransaction;
1717
use shared::block::Block;
1818
use shared::client::Client;
19-
use shared::error::{IntoMainError, MainError};
19+
use shared::error::{CometWrapperError, IntoMainError, MainError};
2020
use shared::height::{BlockHeight, FollowingHeights, UnprocessedBlocks};
2121
use shared::indexed_tx::MaspIndexedTx;
2222
use shared::transaction::Transaction;
@@ -194,7 +194,8 @@ fn fetch_blocks_and_get_handle(
194194
&client,
195195
block_height,
196196
)
197-
.await?;
197+
.await
198+
.map_err(CometWrapperError)?;
198199

199200
with_time_taken(&mut checkpoint, |time_taken| {
200201
tracing::info!(
@@ -204,7 +205,7 @@ fn fetch_blocks_and_get_handle(
204205
);
205206
});
206207

207-
anyhow::Ok(block_data)
208+
Ok::<_, CometWrapperError>(block_data)
208209
})
209210
.await
210211
else {

shared/src/error.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,72 @@
11
use std::fmt;
22

3+
use tendermint_rpc::error::{
4+
Error as TendermintRpcError, ErrorDetail as TendermintRpcErrorDetail,
5+
};
6+
use tendermint_rpc::response_error::Code as TendermintRpcCode;
7+
8+
/// Override the log level of errors.
9+
///
10+
/// Changes logs from [`tracing::error`] to [`tracing::debug`].
11+
pub trait SilenceLog {
12+
fn silence_log(&self) -> bool;
13+
}
14+
15+
impl SilenceLog for CometWrapperError {
16+
fn silence_log(&self) -> bool {
17+
// Check if the error originates from a block that hasn't been committed
18+
// yet
19+
if let Some(err) = self
20+
.0
21+
.chain()
22+
.find_map(|err| err.downcast_ref::<TendermintRpcError>())
23+
{
24+
let TendermintRpcErrorDetail::Response(resp) = err.detail() else {
25+
return false;
26+
};
27+
28+
if resp.source.code() != TendermintRpcCode::InternalError {
29+
return false;
30+
}
31+
32+
return resp.source.data().is_some_and(|msg| {
33+
msg.contains("could not find results for height")
34+
});
35+
}
36+
37+
// By default, never silence error logs
38+
false
39+
}
40+
}
41+
42+
impl SilenceLog for MainError {
43+
fn silence_log(&self) -> bool {
44+
false
45+
}
46+
}
47+
48+
impl SilenceLog for anyhow::Error {
49+
fn silence_log(&self) -> bool {
50+
false
51+
}
52+
}
53+
54+
pub struct CometWrapperError(pub anyhow::Error);
55+
56+
impl fmt::Debug for CometWrapperError {
57+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
58+
<anyhow::Error as fmt::Debug>::fmt(&self.0, f)
59+
}
60+
}
61+
62+
impl fmt::Display for CometWrapperError {
63+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
64+
<anyhow::Error as fmt::Display>::fmt(&self.0, f)
65+
}
66+
}
67+
68+
impl std::error::Error for CometWrapperError {}
69+
370
#[derive(PartialEq, Eq)]
471
pub struct MainError;
572

shared/src/retry.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::task::Poll;
77

88
use tokio::time::Duration;
99

10+
use crate::error::SilenceLog;
1011
use crate::exit_handle;
1112

1213
/// Retry a future generated by `future_generator`, if it fails.
@@ -16,7 +17,7 @@ pub async fn every<F, T, E>(
1617
) -> ControlFlow<(), T>
1718
where
1819
F: AsyncFnMut() -> Result<T, E>,
19-
E: Display + Debug,
20+
E: Display + Debug + SilenceLog,
2021
{
2122
loop {
2223
let fut = future_generator();
@@ -42,19 +43,31 @@ where
4243
}
4344
}
4445

45-
async fn retry_sleep_with_jitter<E: Display + Debug>(duration: Duration, e: E) {
46+
async fn retry_sleep_with_jitter<E>(duration: Duration, e: E)
47+
where
48+
E: Display + Debug + SilenceLog,
49+
{
4650
if exit_handle::must_exit() {
4751
return;
4852
}
4953

5054
let jitter = duration.mul_f64(rand::random_range(0.75f64..=1.25));
5155

52-
tracing::error!(
53-
summary = %e,
54-
full = ?e,
55-
after = ?jitter,
56-
"Retrying execution"
57-
);
56+
if !e.silence_log() {
57+
tracing::error!(
58+
summary = %e,
59+
full = ?e,
60+
after = ?jitter,
61+
"Retrying execution"
62+
);
63+
} else {
64+
tracing::debug!(
65+
summary = %e,
66+
full = ?e,
67+
after = ?jitter,
68+
"Retrying execution"
69+
);
70+
}
5871

5972
let fut = tokio::time::sleep(jitter);
6073
futures::pin_mut!(fut);

0 commit comments

Comments
 (0)