Skip to content

Commit 79684a0

Browse files
committed
fix: don't force connection on both nodes
1 parent a3314d6 commit 79684a0

1 file changed

Lines changed: 58 additions & 26 deletions

File tree

  • batcher/aligned-batcher/src

batcher/aligned-batcher/src/lib.rs

Lines changed: 58 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -322,38 +322,70 @@ impl Batcher {
322322
pub async fn listen_new_blocks_retryable(
323323
self: Arc<Self>,
324324
) -> Result<(), RetryError<BatcherError>> {
325-
let eth_ws_provider = Provider::connect(&self.eth_ws_url).await.map_err(|e| {
326-
warn!("Failed to instantiate Ethereum websocket provider");
327-
RetryError::Transient(BatcherError::EthereumSubscriptionError(e.to_string()))
328-
})?;
325+
// Try to connect at least to one of the nodes (main or fallback)
326+
let eth_ws_provider = Provider::connect(&self.eth_ws_url).await.ok();
327+
let eth_ws_provider_fallback = Provider::connect(&self.eth_ws_url_fallback).await.ok();
328+
if eth_ws_provider.is_none() {
329+
warn!("Failed to instantiate Ethereum main websocket provider");
330+
}
331+
if eth_ws_provider_fallback.is_none() {
332+
warn!("Failed to instantiate fallback Ethereum websocket provider");
333+
}
334+
if eth_ws_provider.is_none() && eth_ws_provider_fallback.is_none() {
335+
return Err(RetryError::Transient(
336+
BatcherError::EthereumSubscriptionError(
337+
"Both Ethereum websocket providers failed to connect".to_string(),
338+
),
339+
));
340+
}
329341

330-
let eth_ws_provider_fallback =
331-
Provider::connect(&self.eth_ws_url_fallback)
332-
.await
333-
.map_err(|e| {
334-
warn!("Failed to instantiate fallback Ethereum websocket provider");
335-
RetryError::Transient(BatcherError::EthereumSubscriptionError(e.to_string()))
336-
})?;
337-
338-
let mut stream = eth_ws_provider.subscribe_blocks().await.map_err(|e| {
339-
warn!("Error subscribing to blocks.");
340-
RetryError::Transient(BatcherError::EthereumSubscriptionError(e.to_string()))
341-
})?;
342-
343-
let mut stream_fallback =
344-
eth_ws_provider_fallback
345-
.subscribe_blocks()
346-
.await
347-
.map_err(|e| {
348-
warn!("Error subscribing to blocks.");
349-
RetryError::Transient(BatcherError::EthereumSubscriptionError(e.to_string()))
350-
})?;
342+
// Tru to connect to one stream (main or fallback)
343+
let mut stream = match &eth_ws_provider {
344+
Some(provider) => match provider.subscribe_blocks().await {
345+
Ok(s) => Some(s),
346+
Err(e) => {
347+
warn!("Error subscribing to blocks on primary provider: {:?}", e);
348+
None
349+
}
350+
},
351+
None => None,
352+
};
353+
let mut stream_fallback = match &eth_ws_provider_fallback {
354+
Some(provider) => match provider.subscribe_blocks().await {
355+
Ok(s) => Some(s),
356+
Err(e) => {
357+
warn!("Error subscribing to blocks on fallback provider: {:?}", e);
358+
None
359+
}
360+
},
361+
None => None,
362+
};
363+
if stream.is_none() && stream_fallback.is_none() {
364+
return Err(RetryError::Transient(
365+
BatcherError::EthereumSubscriptionError(
366+
"Both Ethereum block subscriptions failed".to_string(),
367+
),
368+
));
369+
}
351370

352371
let last_seen_block = Mutex::<u64>::new(0);
353372

354373
loop {
355374
// Wait for both responses
356-
let (block_main, block_fallback) = join!(stream.next(), stream_fallback.next());
375+
let (block_main, block_fallback) = join!(
376+
async {
377+
match stream.as_mut() {
378+
Some(s) => s.next().await,
379+
None => None,
380+
}
381+
},
382+
async {
383+
match stream_fallback.as_mut() {
384+
Some(s) => s.next().await,
385+
None => None,
386+
}
387+
}
388+
);
357389

358390
let block = if let Some(block) = block_main {
359391
block

0 commit comments

Comments
 (0)