Skip to content

Commit 14542b7

Browse files
committed
Retry startup
1 parent 91e8cf2 commit 14542b7

File tree

2 files changed

+63
-32
lines changed

2 files changed

+63
-32
lines changed

apps/fortuna/src/command/run.rs

Lines changed: 60 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use {
33
api::{self, ApiBlockChainState, BlockchainState, ChainId},
44
chain::ethereum::InstrumentedPythContract,
55
command::register_provider::CommitmentMetadata,
6-
config::{Commitment, Config, EthereumConfig, RunOptions},
6+
config::{Commitment, Config, EthereumConfig, ProviderConfig, RunOptions},
77
eth_utils::traced_client::RpcMetrics,
88
keeper::{self, keeper_metrics::KeeperMetrics},
99
state::{HashChainState, PebbleHashChain},
@@ -104,36 +104,29 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
104104
let chains = chains.clone();
105105
let secret_copy = secret.clone();
106106
let rpc_metrics = rpc_metrics.clone();
107+
let provider_config = config.provider.clone();
107108
spawn(async move {
108-
let state = setup_chain_state(
109-
&config.provider.address,
110-
&secret_copy,
111-
config.provider.chain_sample_interval,
112-
&chain_id,
113-
&chain_config,
114-
rpc_metrics.clone(),
115-
)
116-
.await;
117-
match state {
118-
Ok(state) => {
119-
keeper_metrics.add_chain(chain_id.clone(), state.provider_address);
120-
chains.write().await.insert(
121-
chain_id.clone(),
122-
ApiBlockChainState::Initialized(state.clone()),
123-
);
124-
if let Some(keeper_private_key) = keeper_private_key_option {
125-
spawn(keeper::run_keeper_threads(
126-
keeper_private_key,
127-
chain_config,
128-
state,
129-
keeper_metrics.clone(),
130-
rpc_metrics.clone(),
131-
));
109+
loop {
110+
let setup_result = setup_chain(
111+
provider_config.clone(),
112+
&chain_id,
113+
chain_config.clone(),
114+
keeper_metrics.clone(),
115+
keeper_private_key_option.clone(),
116+
chains.clone(),
117+
&secret_copy,
118+
rpc_metrics.clone(),
119+
)
120+
.await;
121+
match setup_result {
122+
Ok(_) => {
123+
tracing::info!("Chain {} initialized successfully", chain_id);
124+
break;
125+
}
126+
Err(e) => {
127+
tracing::error!("Failed to initialize chain {}: {}", chain_id, e);
128+
tokio::time::sleep(tokio::time::Duration::from_secs(15)).await;
132129
}
133-
}
134-
Err(e) => {
135-
tracing::error!("Failed to setup {} {}", chain_id, e);
136-
//TODO: Retry
137130
}
138131
}
139132
});
@@ -155,6 +148,44 @@ pub async fn run(opts: &RunOptions) -> Result<()> {
155148
Ok(())
156149
}
157150

151+
#[allow(clippy::too_many_arguments)]
152+
async fn setup_chain(
153+
provider_config: ProviderConfig,
154+
chain_id: &ChainId,
155+
chain_config: EthereumConfig,
156+
keeper_metrics: Arc<KeeperMetrics>,
157+
keeper_private_key_option: Option<String>,
158+
chains: Arc<RwLock<HashMap<ChainId, ApiBlockChainState>>>,
159+
secret_copy: &str,
160+
rpc_metrics: Arc<RpcMetrics>,
161+
) -> Result<()> {
162+
let state = setup_chain_state(
163+
&provider_config.address,
164+
secret_copy,
165+
provider_config.chain_sample_interval,
166+
chain_id,
167+
&chain_config,
168+
rpc_metrics.clone(),
169+
)
170+
.await?;
171+
keeper_metrics.add_chain(chain_id.clone(), state.provider_address);
172+
chains.write().await.insert(
173+
chain_id.clone(),
174+
ApiBlockChainState::Initialized(state.clone()),
175+
);
176+
if let Some(keeper_private_key) = keeper_private_key_option {
177+
keeper::run_keeper_threads(
178+
keeper_private_key,
179+
chain_config,
180+
state,
181+
keeper_metrics.clone(),
182+
rpc_metrics.clone(),
183+
)
184+
.await?;
185+
}
186+
Ok(())
187+
}
188+
158189
async fn setup_chain_state(
159190
provider: &Address,
160191
secret: &str,

apps/fortuna/src/keeper.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub async fn run_keeper_threads(
6060
chain_state: BlockchainState,
6161
metrics: Arc<KeeperMetrics>,
6262
rpc_metrics: Arc<RpcMetrics>,
63-
) {
63+
) -> anyhow::Result<()> {
6464
tracing::info!("Starting keeper");
6565
let latest_safe_block = get_latest_safe_block(&chain_state).in_current_span().await;
6666
tracing::info!("Latest safe block: {}", &latest_safe_block);
@@ -72,8 +72,7 @@ pub async fn run_keeper_threads(
7272
chain_state.id.clone(),
7373
rpc_metrics.clone(),
7474
)
75-
.await
76-
.expect("Chain config should be valid"),
75+
.await?,
7776
);
7877
let keeper_address = contract.wallet().address();
7978

@@ -230,4 +229,5 @@ pub async fn run_keeper_threads(
230229
}
231230
.in_current_span(),
232231
);
232+
Ok(())
233233
}

0 commit comments

Comments
 (0)