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: use remote chain id for pre-fork simulation #567

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Changes from 1 commit
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
96 changes: 68 additions & 28 deletions crates/edr_provider/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,14 +598,13 @@ impl<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch> ProviderData<LoggerErr
let block_number = self.block_number_by_block_spec(block_spec)?;

let chain_id = if let Some(block_number) = block_number {
self.blockchain.chain_id_at_block_number(block_number)?
self.chain_id_at_block_number(block_number, block_spec)?
} else {
self.blockchain.chain_id()
};

Ok(chain_id)
}

pub fn coinbase(&self) -> Address {
self.beneficiary
}
Expand All @@ -622,9 +621,8 @@ impl<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch> ProviderData<LoggerErr
.ok_or_else(|| ProviderError::InvalidTransactionHash(*transaction_hash))?;

let header = block.header();
let block_spec = Some(BlockSpec::Number(header.number));

let cfg_env = self.create_evm_config(block_spec.as_ref())?;
let cfg_env = self.create_evm_config_at_block_spec(&BlockSpec::Number(header.number))?;

let transactions = block.transactions().to_vec();

Expand Down Expand Up @@ -674,7 +672,7 @@ impl<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch> ProviderData<LoggerErr
block_spec: &BlockSpec,
trace_config: DebugTraceConfig,
) -> Result<DebugTraceResultWithTraces, ProviderError<LoggerErrorT>> {
let cfg_env = self.create_evm_config(Some(block_spec))?;
let cfg_env = self.create_evm_config_at_block_spec(block_spec)?;

let tx_env: TxEnv = transaction.into();

Expand Down Expand Up @@ -706,7 +704,7 @@ impl<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch> ProviderData<LoggerErr
transaction: transaction::Signed,
block_spec: &BlockSpec,
) -> Result<EstimateGasResult, ProviderError<LoggerErrorT>> {
let cfg_env = self.create_evm_config(Some(block_spec))?;
let cfg_env = self.create_evm_config_at_block_spec(block_spec)?;
// Minimum gas cost that is required for transaction to be included in
// a block
let minimum_cost = transaction::initial_cost(&transaction, self.spec_id());
Expand Down Expand Up @@ -1437,7 +1435,7 @@ impl<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch> ProviderData<LoggerErr
block_spec: &BlockSpec,
state_overrides: &StateOverrides,
) -> Result<CallResult, ProviderError<LoggerErrorT>> {
let cfg_env = self.create_evm_config(Some(block_spec))?;
let cfg_env = self.create_evm_config_at_block_spec(block_spec)?;
let tx_env = transaction.into();

let mut debugger = Debugger::with_mocker(
Expand Down Expand Up @@ -1924,40 +1922,63 @@ impl<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch> ProviderData<LoggerErr
Ok(transaction_hash)
}

/// Creates a configuration, taking into the hardfork and chain id at the
/// provided `BlockSpec`. If none is provided, assumes the hardfork for
/// newly mined blocks.
/// Wrapper over `Blockchain::chain_id_at_block_number` that handles error
/// conversion.
fn chain_id_at_block_number(
&self,
block_number: u64,
block_spec: &BlockSpec,
) -> Result<u64, ProviderError<LoggerErrorT>> {
self.blockchain
.chain_id_at_block_number(block_number)
.map_err(|err| match err {
BlockchainError::UnknownBlockNumber => ProviderError::InvalidBlockNumberOrHash {
block_spec: block_spec.clone(),
latest_block_number: self.blockchain.last_block_number(),
},
_ => ProviderError::Blockchain(err),
})
}

/// Creates an EVM configuration with the provided hardfork and chain id
fn create_evm_config(
&self,
block_spec: Option<&BlockSpec>,
spec_id: SpecId,
chain_id: u64,
) -> Result<CfgEnvWithHandlerCfg, ProviderError<LoggerErrorT>> {
let block_number = block_spec
.map(|block_spec| self.block_number_by_block_spec(block_spec))
.transpose()?
.flatten();
let mut cfg_env = CfgEnv::default();
cfg_env.chain_id = chain_id;
cfg_env.limit_contract_code_size = if self.allow_unlimited_contract_size {
Some(usize::MAX)
} else {
None
};
cfg_env.disable_eip3607 = true;

Ok(CfgEnvWithHandlerCfg::new_with_spec_id(cfg_env, spec_id))
}

/// Creates a configuration, taking into the hardfork and chain id at the
/// provided `BlockSpec`.
fn create_evm_config_at_block_spec(
&self,
block_spec: &BlockSpec,
) -> Result<CfgEnvWithHandlerCfg, ProviderError<LoggerErrorT>> {
let block_number = self.block_number_by_block_spec(block_spec)?;

let spec_id = if let Some(block_number) = block_number {
self.blockchain.spec_at_block_number(block_number)?
self.spec_at_block_number(block_number, block_spec)?
} else {
self.blockchain.spec_id()
};

let chain_id = if let Some(block_number) = block_number {
self.blockchain.chain_id_at_block_number(block_number)?
self.chain_id_at_block_number(block_number, block_spec)?
} else {
self.blockchain.chain_id()
};

let mut cfg_env = CfgEnv::default();
cfg_env.chain_id = chain_id;
cfg_env.limit_contract_code_size = if self.allow_unlimited_contract_size {
Some(usize::MAX)
} else {
None
};
cfg_env.disable_eip3607 = true;

Ok(CfgEnvWithHandlerCfg::new_with_spec_id(cfg_env, spec_id))
self.create_evm_config(spec_id, chain_id)
}

fn execute_in_block_context<T>(
Expand Down Expand Up @@ -2016,7 +2037,8 @@ impl<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch> ProviderData<LoggerErr
options.beneficiary = Some(options.beneficiary.unwrap_or(self.beneficiary));
options.gas_limit = Some(options.gas_limit.unwrap_or_else(|| self.block_gas_limit()));

let evm_config = self.create_evm_config(None)?;
let evm_config =
self.create_evm_config(self.blockchain.spec_id(), self.blockchain.chain_id())?;

if options.mix_hash.is_none() && evm_config.handler_cfg.spec_id >= SpecId::MERGE {
options.mix_hash = Some(self.prev_randao_generator.next_value());
Expand Down Expand Up @@ -2249,6 +2271,24 @@ impl<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch> ProviderData<LoggerErr
}
}

/// Wrapper over `Blockchain::spec_at_block_number` that handles error
/// conversion.
fn spec_at_block_number(
&self,
block_number: u64,
block_spec: &BlockSpec,
) -> Result<SpecId, ProviderError<LoggerErrorT>> {
self.blockchain
.spec_at_block_number(block_number)
.map_err(|err| match err {
BlockchainError::UnknownBlockNumber => ProviderError::InvalidBlockNumberOrHash {
Wodann marked this conversation as resolved.
Show resolved Hide resolved
block_spec: block_spec.clone(),
latest_block_number: self.blockchain.last_block_number(),
},
_ => ProviderError::Blockchain(err),
})
}

pub fn sign_transaction_request(
&self,
transaction_request: TransactionRequestAndSender,
Expand Down
Loading