|
| 1 | +use std::{cmp, sync::Arc, time::Duration}; |
| 2 | + |
| 3 | +use alloy::rpc::types::Log; |
| 4 | +use async_trait::async_trait; |
| 5 | +use tracing::{info, warn}; |
| 6 | + |
| 7 | +use crate::{FixedRetryConfig, callback::EventCallback}; |
| 8 | + |
| 9 | +use super::{CallbackStrategy, fixed_retry::FixedRetryStrategy}; |
| 10 | + |
| 11 | +pub const STATE_SYNC_RETRY_INTERVAL: Duration = Duration::from_secs(30); |
| 12 | +pub const STATE_SYNC_RETRY_MAX_INTERVAL: Duration = Duration::from_secs(120); |
| 13 | +pub const STATE_SYNC_RETRY_MAX_ELAPSED: Duration = Duration::from_secs(600); |
| 14 | +pub const STATE_SYNC_RETRY_MULTIPLIER: f64 = 1.5; |
| 15 | + |
| 16 | +#[derive(Clone, Copy, Debug)] |
| 17 | +pub struct StateSyncConfig { |
| 18 | + pub initial_interval: Duration, |
| 19 | + pub max_interval: Duration, |
| 20 | + pub max_elapsed: Duration, |
| 21 | + pub multiplier: f64, |
| 22 | +} |
| 23 | + |
| 24 | +impl Default for StateSyncConfig { |
| 25 | + fn default() -> Self { |
| 26 | + Self { |
| 27 | + initial_interval: STATE_SYNC_RETRY_INTERVAL, |
| 28 | + max_interval: STATE_SYNC_RETRY_MAX_INTERVAL, |
| 29 | + max_elapsed: STATE_SYNC_RETRY_MAX_ELAPSED, |
| 30 | + multiplier: STATE_SYNC_RETRY_MULTIPLIER, |
| 31 | + } |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +pub struct StateSyncAwareStrategy { |
| 36 | + inner: FixedRetryStrategy, |
| 37 | + cfg: StateSyncConfig, |
| 38 | +} |
| 39 | + |
| 40 | +impl Default for StateSyncAwareStrategy { |
| 41 | + fn default() -> Self { |
| 42 | + Self::new() |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl StateSyncAwareStrategy { |
| 47 | + #[must_use] |
| 48 | + pub fn new() -> Self { |
| 49 | + Self { |
| 50 | + inner: FixedRetryStrategy::new(FixedRetryConfig::default()), |
| 51 | + cfg: StateSyncConfig::default(), |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + #[must_use] |
| 56 | + pub fn with_state_sync_config(mut self, cfg: StateSyncConfig) -> Self { |
| 57 | + self.cfg = cfg; |
| 58 | + self |
| 59 | + } |
| 60 | + |
| 61 | + #[must_use] |
| 62 | + pub fn with_fixed_retry_config(mut self, cfg: super::fixed_retry::FixedRetryConfig) -> Self { |
| 63 | + self.inner = FixedRetryStrategy::new(cfg); |
| 64 | + self |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +#[async_trait] |
| 69 | +impl CallbackStrategy for StateSyncAwareStrategy { |
| 70 | + async fn execute( |
| 71 | + &self, |
| 72 | + callback: &Arc<dyn EventCallback + Send + Sync>, |
| 73 | + log: &Log, |
| 74 | + ) -> anyhow::Result<()> { |
| 75 | + match callback.on_event(log).await { |
| 76 | + Ok(()) => Ok(()), |
| 77 | + Err(first_err) => { |
| 78 | + if is_missing_trie_node_error(&first_err) { |
| 79 | + // state sync aware retry path |
| 80 | + let mut delay = self.cfg.initial_interval; |
| 81 | + let start = tokio::time::Instant::now(); |
| 82 | + info!(initial_interval = ?self.cfg.initial_interval, max_interval = ?self.cfg.max_interval, |
| 83 | + max_elapsed = ?self.cfg.max_elapsed, "Starting state-sync aware retry"); |
| 84 | + let mut last_err: anyhow::Error = first_err; |
| 85 | + loop { |
| 86 | + if start.elapsed() >= self.cfg.max_elapsed { |
| 87 | + return Err(last_err); |
| 88 | + } |
| 89 | + tokio::time::sleep(delay).await; |
| 90 | + match callback.on_event(log).await { |
| 91 | + Ok(()) => return Ok(()), |
| 92 | + Err(e) => { |
| 93 | + last_err = e; |
| 94 | + let next_secs = delay.as_secs_f64() * self.cfg.multiplier; |
| 95 | + let next = Duration::from_secs_f64(next_secs); |
| 96 | + delay = cmp::min(self.cfg.max_interval, next); |
| 97 | + let elapsed = start.elapsed(); |
| 98 | + warn!(next_delay = ?delay, elapsed = ?elapsed, error = %last_err, |
| 99 | + "State-sync retry operation failed: will retry"); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } else { |
| 104 | + // Fixed retry for regular errors |
| 105 | + self.inner.execute(callback, log).await |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +fn is_missing_trie_node_error(err: &anyhow::Error) -> bool { |
| 113 | + let s = err.to_string().to_lowercase(); |
| 114 | + s.contains("missing trie node") && s.contains("state") && s.contains("not available") |
| 115 | +} |
0 commit comments