refactor(l1): improve error handling in set_sync_block with eyre::Result#6465
refactor(l1): improve error handling in set_sync_block with eyre::Result#6465emirongrr wants to merge 7 commits intolambdaclass:mainfrom
Conversation
Greptile SummaryThis PR refactors Confidence Score: 2/5Not safe to merge — drops the optional env-var guard, breaking node startup when SYNC_BLOCK_NUM is unset. The single changed function contains a P0 behavioral regression: any node running without the SYNC_BLOCK_NUM env var will now fail to initialize. cmd/ethrex/initializers.rs — specifically the env::var call at line 465 that must restore the optional guard.
|
| Filename | Overview |
|---|---|
| cmd/ethrex/initializers.rs | Refactors set_sync_block to return eyre::Result<()>, but drops the if let Ok guard, making a missing SYNC_BLOCK_NUM env var propagate as an error and crash init_l1. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["init_l1()"] --> B["#[cfg(feature = sync-test)] set_sync_block()"]
B --> C{"env::var('SYNC_BLOCK_NUM')"}
C -- "Err(NotPresent)\n[OLD: silent no-op]\n[NEW: propagates error ❌]" --> D["return Ok(()) [OLD]\nreturn Err → init_l1 fails [NEW]"]
C -- "Ok(value)" --> E["parse as u64"]
E -- "parse error" --> F["eyre error → init_l1 fails"]
E -- "ok" --> G["store.get_canonical_block_hash()"]
G -- "store error" --> H["eyre error → init_l1 fails"]
G -- "None" --> I["eyre error (block hash not found)"]
G -- "Some(hash)" --> J["store.forkchoice_update()"]
J -- "error" --> K["eyre error → init_l1 fails"]
J -- "ok" --> L["Ok(())"]
Prompt To Fix All With AI
This is a comment left during a code review.
Path: cmd/ethrex/initializers.rs
Line: 465-467
Comment:
**Missing env var now crashes `init_l1`**
The original function was intentionally gated on `if let Ok(...) = env::var("SYNC_BLOCK_NUM")` — if the variable is absent, it was a silent no-op. Replacing that with `env::var(...)?` propagates `VarError::NotPresent` as an error, so every node started without `SYNC_BLOCK_NUM` set will fail inside `init_l1`. The fix is to restore the optional behavior:
```rust
async fn set_sync_block(store: &Store) -> eyre::Result<()> {
let Ok(block_number_str) = env::var("SYNC_BLOCK_NUM") else {
return Ok(());
};
let block_number: u64 = block_number_str
.parse()
.map_err(|_| eyre::eyre!("SYNC_BLOCK_NUM environment variable is not a valid number"))?;
// ... rest unchanged
```
How can I resolve this? If you propose a fix, please make it concise.Reviews (1): Last reviewed commit: "refactor(l1): improve error handling in ..." | Re-trigger Greptile
…om/emirongrr/ethrex into refactor/sync-block-error-handling
Motivation
The current
set_sync_block()function uses identical error messages for two different failurescenarios (store query failure vs. hash not found), making it impossible to distinguish which
layer failed. Using
eyre::Resultwith distinct error contexts for each failure point providesbetter diagnostics and aligns with the codebase's error handling standard.
Description
Refactored
set_sync_block()function incmd/ethrex/initializers.rsto useeyre::Result<()>error handling instead of
.expect()calls:async fn()toasync fn() -> eyre::Result<()>.expect()with explicit error handling usingwrap_err()andok_or_else()init_l1()to propagate errors with?operatorNote
If this refactoring is considered excessive or unnecessary, feel free to reject or close this PR.
A simpler alternative would be to just fix the duplicate error messages:
Checklist
STORE_SCHEMA_VERSION(crates/storage/lib.rs) if the PR includes breaking changes to theStorerequiring a re-sync.