Skip to content

Commit

Permalink
Fix error message
Browse files Browse the repository at this point in the history
  • Loading branch information
agostbiro committed Jul 29, 2024
1 parent 621535a commit 58d6c8b
Showing 1 changed file with 83 additions and 28 deletions.
111 changes: 83 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 @@ -1854,6 +1852,21 @@ impl<LoggerErrorT: Debug, TimerT: Clone + TimeSinceEpoch> ProviderData<LoggerErr
self.blockchain.spec_id()
}

pub fn spec_id_at_block_spec(

Check failure on line 1855 in crates/edr_provider/src/data.rs

View workflow job for this annotation

GitHub Actions / Check EDR

method `spec_id_at_block_spec` is never used
&self,
block_spec: &BlockSpec,
) -> Result<SpecId, ProviderError<LoggerErrorT>> {
let block_number = self.block_number_by_block_spec(block_spec)?;

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

Ok(spec_id)
}

pub fn stop_impersonating_account(&mut self, address: Address) -> bool {
self.impersonated_accounts.remove(&address)
}
Expand Down Expand Up @@ -1924,40 +1937,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 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 = block_spec
.map(|block_spec| self.block_number_by_block_spec(block_spec))
.transpose()?
.flatten();
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 +2052,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 +2286,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 {
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

0 comments on commit 58d6c8b

Please sign in to comment.