Allow CHAIN_ID env var to override default chain selection#55
Conversation
Enables deploying the beaconator to non-Base chains (e.g. Arbitrum Sepolia 421614) by setting CHAIN_ID explicitly, while keeping the existing ENV-based mapping as fallback for backward compatibility.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughcreate_rocket now prefers a numeric Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/lib.rs`:
- Around line 177-193: The current CHAIN_ID handling silently ignores parse
errors because of .ok().and_then(...).unwrap_or_else, which can cause accidental
wrong-chain deployments; change this to fail fast when CHAIN_ID is set but
unparseable: explicitly check env::var("CHAIN_ID") and if it returns Ok(s)
attempt s.parse::<u64>() and panic (or return an error) with a clear message if
parse fails, otherwise use the parsed value; only fall back to the ENV-based
mapping (using env_type) when env::var returns Err (i.e., CHAIN_ID not set).
Ensure the code paths reference the existing symbols chain_id and env_type so
callers remain unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
| // Get environment configuration and chain ID | ||
| // CHAIN_ID env var overrides the default ENV-based mapping, enabling | ||
| // deployment to chains like Arbitrum Sepolia (421614) while keeping ENV=testnet. | ||
| let env_type = &rpc_config.env_type; | ||
| let chain_id = match env_type.to_lowercase().as_str() { | ||
| "testnet" => 84532u64, // Base Sepolia testnet | ||
| "mainnet" => 8453u64, // Base mainnet | ||
| "localnet" => 84532u64, // Use testnet chain ID for local development/CI | ||
| _ => panic!( | ||
| "Invalid ENV value '{env_type}'. Must be either 'mainnet', 'testnet', or 'localnet'" | ||
| ), | ||
| }; | ||
| let chain_id = env::var("CHAIN_ID") | ||
| .ok() | ||
| .and_then(|s| s.parse::<u64>().ok()) | ||
| .unwrap_or_else(|| { | ||
| match env_type.to_lowercase().as_str() { | ||
| "testnet" => 84532u64, // Base Sepolia testnet | ||
| "mainnet" => 8453u64, // Base mainnet | ||
| "localnet" => 84532u64, // Use testnet chain ID for local development/CI | ||
| _ => panic!( | ||
| "Invalid ENV value '{env_type}'. Must be either 'mainnet', 'testnet', or 'localnet'" | ||
| ), | ||
| } | ||
| }); |
There was a problem hiding this comment.
Silent fallback on invalid CHAIN_ID could lead to wrong-chain deployment.
If a user sets CHAIN_ID=arbitrum (string instead of number) or CHAIN_ID= (empty), the .parse::<u64>().ok() silently discards the error and falls back to the ENV-based default. This could result in deploying to Base when Arbitrum was intended, with no warning.
Consider either:
- Fail fast if CHAIN_ID is set but unparseable (safer), or
- Log a warning when CHAIN_ID is present but invalid (backward-compatible)
Option 1: Fail fast (recommended for safety-critical config)
// CHAIN_ID env var overrides the default ENV-based mapping, enabling
// deployment to chains like Arbitrum Sepolia (421614) while keeping ENV=testnet.
let env_type = &rpc_config.env_type;
- let chain_id = env::var("CHAIN_ID")
- .ok()
- .and_then(|s| s.parse::<u64>().ok())
- .unwrap_or_else(|| {
- match env_type.to_lowercase().as_str() {
- "testnet" => 84532u64, // Base Sepolia testnet
- "mainnet" => 8453u64, // Base mainnet
- "localnet" => 84532u64, // Use testnet chain ID for local development/CI
- _ => panic!(
- "Invalid ENV value '{env_type}'. Must be either 'mainnet', 'testnet', or 'localnet'"
- ),
- }
- });
+ let chain_id = match env::var("CHAIN_ID") {
+ Ok(s) if !s.is_empty() => {
+ let id = s.parse::<u64>().unwrap_or_else(|e| {
+ panic!("Invalid CHAIN_ID '{s}': must be a valid u64 chain ID ({e})")
+ });
+ tracing::info!("Using CHAIN_ID override: {id}");
+ id
+ }
+ _ => {
+ let id = match env_type.to_lowercase().as_str() {
+ "testnet" => 84532u64, // Base Sepolia testnet
+ "mainnet" => 8453u64, // Base mainnet
+ "localnet" => 84532u64, // Use testnet chain ID for local development/CI
+ _ => panic!(
+ "Invalid ENV value '{env_type}'. Must be either 'mainnet', 'testnet', or 'localnet'"
+ ),
+ };
+ tracing::info!("Using ENV-based chain ID: {id} (env_type={env_type})");
+ id
+ }
+ };Option 2: Warn but continue (backward-compatible)
let env_type = &rpc_config.env_type;
- let chain_id = env::var("CHAIN_ID")
- .ok()
- .and_then(|s| s.parse::<u64>().ok())
+ let chain_id = env::var("CHAIN_ID")
+ .ok()
+ .and_then(|s| {
+ if s.is_empty() {
+ return None;
+ }
+ match s.parse::<u64>() {
+ Ok(id) => {
+ tracing::info!("Using CHAIN_ID override: {id}");
+ Some(id)
+ }
+ Err(e) => {
+ tracing::warn!(
+ "CHAIN_ID '{s}' is not a valid u64 ({e}), falling back to ENV-based default"
+ );
+ None
+ }
+ }
+ })
.unwrap_or_else(|| {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/lib.rs` around lines 177 - 193, The current CHAIN_ID handling silently
ignores parse errors because of .ok().and_then(...).unwrap_or_else, which can
cause accidental wrong-chain deployments; change this to fail fast when CHAIN_ID
is set but unparseable: explicitly check env::var("CHAIN_ID") and if it returns
Ok(s) attempt s.parse::<u64>() and panic (or return an error) with a clear
message if parse fails, otherwise use the parsed value; only fall back to the
ENV-based mapping (using env_type) when env::var returns Err (i.e., CHAIN_ID not
set). Ensure the code paths reference the existing symbols chain_id and env_type
so callers remain unchanged.
Remove --rm flag so container logs are available when the health check fails, making it possible to diagnose startup crashes.
Summary
CHAIN_IDenv var that overrides the hardcodedENV -> chain_idmappingENV=testnetCHAIN_IDbehave identicallyContext
We're deploying beacons to Arbitrum Sepolia as Phase 1 of the Base -> Arbitrum migration. The Arbitrum beaconator instance will set
CHAIN_ID=421614alongsideENV=testnet.Test plan
make fmt-checkpassesmake lintpassesmake test-fastpasses (183 unit + 7 integration)the-beaconator-arbon Railway withCHAIN_ID=421614and verify healthSummary by CodeRabbit
New Features
Chores