Skip to content
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

fix: derive block gas limit from rpc for forge script and anvil when forking #9753

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,8 @@ latest block number: {latest_block}"
};

let gas_limit = self.fork_gas_limit(&block);
self.gas_limit = Some(gas_limit);

env.block = BlockEnv {
number: U256::from(fork_block_number),
timestamp: U256::from(block.header.timestamp),
Expand Down
18 changes: 17 additions & 1 deletion crates/script/src/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use crate::{
use alloy_chains::Chain;
use alloy_consensus::TxEnvelope;
use alloy_eips::{eip2718::Encodable2718, BlockId};
use alloy_network::{AnyNetwork, EthereumWallet, TransactionBuilder};
use alloy_network::{
primitives::BlockTransactionsKind, AnyNetwork, EthereumWallet, TransactionBuilder,
};
use alloy_primitives::{
map::{AddressHashMap, AddressHashSet},
utils::format_units,
Expand Down Expand Up @@ -49,6 +51,20 @@ where
Ok(())
}

pub async fn get_block_gas_limit(provider_url: &str, block_number: Option<u64>) -> Result<u64> {
let provider = try_get_http_provider(provider_url)
.wrap_err_with(|| format!("bad fork_url provider: {provider_url}"))?;

let block_id = block_number.map_or(BlockId::latest(), BlockId::number);
let block = provider.get_block(block_id, BlockTransactionsKind::Hashes).await?;

if let Some(block) = block {
Ok(block.header.gas_limit)
} else {
bail!("Failed to get block for block number: {}", block_number.unwrap_or_default())
}
}

pub async fn next_nonce(
caller: Address,
provider_url: &str,
Expand Down
9 changes: 7 additions & 2 deletions crates/script/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extern crate foundry_common;
#[macro_use]
extern crate tracing;

use crate::runner::ScriptRunner;
use crate::{broadcast::get_block_gas_limit, runner::ScriptRunner};
use alloy_json_abi::{Function, JsonAbi};
use alloy_primitives::{
hex,
Expand Down Expand Up @@ -553,14 +553,19 @@ pub struct ScriptConfig {
}

impl ScriptConfig {
pub async fn new(config: Config, evm_opts: EvmOpts) -> Result<Self> {
pub async fn new(config: Config, mut evm_opts: EvmOpts) -> Result<Self> {
let sender_nonce = if let Some(fork_url) = evm_opts.fork_url.as_ref() {
next_nonce(evm_opts.sender, fork_url, evm_opts.fork_block_number).await?
} else {
// dapptools compatibility
1
};

if let Some(fork_url) = evm_opts.fork_url.as_ref() {
evm_opts.env.gas_limit =
get_block_gas_limit(fork_url, evm_opts.fork_block_number).await?.into();
}

Ok(Self { config, evm_opts, sender_nonce, backends: HashMap::default() })
}

Expand Down
Loading