-
Notifications
You must be signed in to change notification settings - Fork 331
feat(fortuna): CLI command to debug fee estimation issues #3313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jayantk
wants to merge
1
commit into
main
Choose a base branch
from
fortuna_debug_gas
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+130
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| use { | ||
| crate::{ | ||
| chain::ethereum::{InstrumentedPythContract, InstrumentedSignablePythContract}, | ||
| config::{Config, DebugGasOptions}, | ||
| eth_utils::{traced_client::RpcMetrics, utils::estimate_tx_cost}, | ||
| }, | ||
| anyhow::Result, | ||
| ethers::providers::Middleware, | ||
| prometheus_client::registry::Registry, | ||
| std::sync::Arc, | ||
| tokio::{sync::RwLock, time::Duration}, | ||
| }; | ||
|
|
||
| const POLL_INTERVAL: Duration = Duration::from_secs(2); | ||
|
|
||
| pub async fn debug_gas(opts: &DebugGasOptions) -> Result<()> { | ||
| let config = Config::load(&opts.config.config)?; | ||
| let chain_config = config.get_chain_config(&opts.chain_id)?; | ||
|
|
||
| // Create metrics registry for the instrumented contract | ||
| let metrics_registry = Arc::new(RwLock::new(Registry::default())); | ||
| let rpc_metrics = Arc::new(RpcMetrics::new(metrics_registry.clone()).await); | ||
|
|
||
| // Get network_id by creating a temporary contract | ||
| let temp_contract = InstrumentedPythContract::from_config( | ||
| &chain_config, | ||
| opts.chain_id.clone(), | ||
| rpc_metrics.clone(), | ||
| )?; | ||
| let network_id = temp_contract.get_network_id().await?.as_u64(); | ||
|
|
||
| // Get the keeper private key | ||
| let keeper_private_key = | ||
| config.keeper.private_key.load()?.ok_or_else(|| { | ||
| anyhow::anyhow!("Keeper private key is required for debug_gas command") | ||
| })?; | ||
|
|
||
| // Instantiate InstrumentedSignablePythContract | ||
| let contract = Arc::new(InstrumentedSignablePythContract::from_config( | ||
| &chain_config, | ||
| &keeper_private_key, | ||
| opts.chain_id.clone(), | ||
| rpc_metrics, | ||
| network_id, | ||
| )?); | ||
|
|
||
| tracing::info!("Starting gas price debugger for chain: {}", opts.chain_id); | ||
| tracing::info!("Watching for new blocks and calling estimate_tx_cost on each block..."); | ||
| tracing::info!("Press Ctrl+C to stop"); | ||
|
|
||
| let middleware = contract.client().clone(); | ||
| let mut last_block_number: Option<u64> = None; | ||
|
|
||
| loop { | ||
| // Get the latest block number | ||
| let latest_block = match middleware.get_block_number().await { | ||
| Ok(block_num) => block_num.as_u64(), | ||
| Err(e) => { | ||
| tracing::error!("Failed to get latest block number: {}", e); | ||
| tokio::time::sleep(POLL_INTERVAL).await; | ||
| continue; | ||
| } | ||
| }; | ||
|
|
||
| // Check if we have a new block | ||
| if let Some(last) = last_block_number { | ||
| if latest_block <= last { | ||
| tokio::time::sleep(POLL_INTERVAL).await; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| // New block detected, estimate transaction cost | ||
| let gas_limit: u128 = chain_config.gas_limit as u128; | ||
| match estimate_tx_cost(middleware.clone(), chain_config.legacy_tx, gas_limit).await { | ||
| Ok(tx_cost) => { | ||
| let tx_cost_eth = tx_cost as f64 / 1e18; | ||
| let effective_gas_price = tx_cost / gas_limit; | ||
| let effective_gas_price_gwei = effective_gas_price as f64 / 1e9; | ||
| tracing::info!( | ||
| "Block {}: tx_cost ({} gas) = {} ETH ({} wei), effective_gas_price = {} gwei ({} wei)", | ||
| latest_block, | ||
| gas_limit, | ||
| tx_cost_eth, | ||
| tx_cost, | ||
| effective_gas_price_gwei, | ||
| effective_gas_price | ||
| ); | ||
| } | ||
| Err(e) => { | ||
| tracing::error!( | ||
| "Block {}: Failed to estimate transaction cost: {}", | ||
| latest_block, | ||
| e | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| last_block_number = Some(latest_block); | ||
| tokio::time::sleep(POLL_INTERVAL).await; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| use { | ||
| crate::{api::ChainId, config::ConfigOptions}, | ||
| clap::Args, | ||
| }; | ||
|
|
||
| #[derive(Args, Clone, Debug)] | ||
| #[command(next_help_heading = "Debug Gas Options")] | ||
| #[group(id = "DebugGas")] | ||
| pub struct DebugGasOptions { | ||
| #[command(flatten)] | ||
| pub config: ConfigOptions, | ||
|
|
||
| /// The chain ID to debug gas estimation for. | ||
| #[arg(long = "chain-id")] | ||
| pub chain_id: ChainId, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -99,4 +99,4 @@ | |
| "./package.json": "./package.json" | ||
| }, | ||
| "module": "./dist/esm/index.mjs" | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Division by zero panic when
gas_limitis 0The
effective_gas_pricecalculation at line 78 performstx_cost / gas_limitwheregas_limitis derived fromchain_config.gas_limit(au32). Ifgas_limitis 0, this causes a panic due to integer division by zero.Root Cause
The
gas_limitfield inEthereumConfig(apps/fortuna/src/config.rs:158) is au32with no default value and no validation that it is non-zero. Atapps/fortuna/src/command/debug_gas.rs:74, it is cast tou128and then used as a divisor at line 78:While a
gas_limitof 0 is an unusual configuration, there is no guard against it. Other callers ofestimate_tx_cost(e.g.,apps/fortuna/src/keeper/fee.rs:305-306) getgas_limitfrom the on-chain provider info rather than the config, so they don't have this specific issue.Impact: The debug tool panics at runtime with a division-by-zero error if the config has
gas_limit: 0.Was this helpful? React with 👍 or 👎 to provide feedback.