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: ignore failures in load_access_list #27

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
21 changes: 21 additions & 0 deletions crates/interpreter/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,27 @@ pub trait Host {
/// Returns a mutable reference to the environment.
fn env_mut(&mut self) -> &mut Env;

#[cfg(feature = "scroll")]
/// Check an address is in the access list.
fn is_address_in_access_list(&self, address: Address) -> bool {
self.env()
.tx
.access_list
.iter()
.any(|item| item.address == address)
}

#[cfg(feature = "scroll")]
/// Check a storage key is in the access list.
fn is_storage_key_in_access_list(&self, address: Address, index: U256) -> bool {
self.env()
.tx
.access_list
.iter()
.filter(|item| item.address == address)
.any(|item| item.storage_keys.contains(&B256::from(index)))
}

/// Load an account code.
fn load_account_delegated(&mut self, address: Address) -> Option<AccountLoad>;

Expand Down
19 changes: 18 additions & 1 deletion crates/interpreter/src/instructions/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,15 @@ pub fn call<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &
return;
};

let Some(account_load) = host.load_account_delegated(to) else {
#[allow(unused_mut)]
let Some(mut account_load) = host.load_account_delegated(to) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
#[cfg(feature = "scroll")]
if account_load.is_cold && host.is_address_in_access_list(interpreter.contract.target_address) {
account_load.is_cold = false;
}
let Some(mut gas_limit) =
calc_call_gas::<SPEC>(interpreter, account_load, has_transfer, local_gas_limit)
else {
Expand Down Expand Up @@ -464,6 +469,10 @@ pub fn call_code<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, ho
};
// set is_empty to false as we are not creating this account.
load.is_empty = false;
#[cfg(feature = "scroll")]
if load.is_cold && host.is_address_in_access_list(interpreter.contract.target_address) {
load.is_cold = false;
}
let Some(mut gas_limit) =
calc_call_gas::<SPEC>(interpreter, load, !value.is_zero(), local_gas_limit)
else {
Expand Down Expand Up @@ -512,6 +521,10 @@ pub fn delegate_call<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter
};
// set is_empty to false as we are not creating this account.
load.is_empty = false;
#[cfg(feature = "scroll")]
if load.is_cold && host.is_address_in_access_list(interpreter.contract.target_address) {
load.is_cold = false;
}
let Some(gas_limit) = calc_call_gas::<SPEC>(interpreter, load, false, local_gas_limit) else {
return;
};
Expand Down Expand Up @@ -553,6 +566,10 @@ pub fn static_call<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter,
};
// set is_empty to false as we are not creating this account.
load.is_empty = false;
#[cfg(feature = "scroll")]
if load.is_cold && host.is_address_in_access_list(interpreter.contract.target_address) {
load.is_cold = false;
}
let Some(gas_limit) = calc_call_gas::<SPEC>(interpreter, load, false, local_gas_limit) else {
return;
};
Expand Down
43 changes: 38 additions & 5 deletions crates/interpreter/src/instructions/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ use std::vec::Vec;

pub fn balance<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop_address!(interpreter, address);
let Some(balance) = host.balance(address) else {
#[allow(unused_mut)]
let Some(mut balance) = host.balance(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
#[cfg(feature = "scroll")]
if balance.is_cold && host.is_address_in_access_list(interpreter.contract.target_address) {
balance.is_cold = false;
}
gas!(
interpreter,
if SPEC::enabled(BERLIN) {
Expand Down Expand Up @@ -62,10 +67,14 @@ pub fn extcodesize<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter,
#[cfg(feature = "scroll")]
pub fn extcodesize<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop_address!(interpreter, address);
let Some((code_size, is_cold)) = host.code_size(address) else {
let Some((code_size, mut is_cold)) = host.code_size(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
#[cfg(feature = "scroll")]
if is_cold && host.is_address_in_access_list(interpreter.contract.target_address) {
is_cold = false;
}
gas!(interpreter, warm_cold_cost(is_cold));

push!(interpreter, U256::from(code_size));
Expand All @@ -75,10 +84,15 @@ pub fn extcodesize<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter,
pub fn extcodehash<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
check!(interpreter, CONSTANTINOPLE);
pop_address!(interpreter, address);
let Some(code_hash) = host.code_hash(address) else {
#[allow(unused_mut)]
let Some(mut code_hash) = host.code_hash(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
#[cfg(feature = "scroll")]
if code_hash.is_cold && host.is_address_in_access_list(interpreter.contract.target_address) {
code_hash.is_cold = false;
}
let (code_hash, load) = code_hash.into_components();
if SPEC::enabled(BERLIN) {
gas!(interpreter, warm_cold_cost_with_delegation(load))
Expand All @@ -94,10 +108,15 @@ pub fn extcodecopy<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter,
pop_address!(interpreter, address);
pop!(interpreter, memory_offset, code_offset, len_u256);

let Some(code) = host.code(address) else {
#[allow(unused_mut)]
let Some(mut code) = host.code(address) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
#[cfg(feature = "scroll")]
if code.is_cold && host.is_address_in_access_list(interpreter.contract.target_address) {
code.is_cold = false;
}

let len = as_usize_or_fail!(interpreter, len_u256);
let (code, load) = code.into_components();
Expand Down Expand Up @@ -164,10 +183,17 @@ pub fn blockhash<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, ho

pub fn sload<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host: &mut H) {
pop_top!(interpreter, index);
let Some(value) = host.sload(interpreter.contract.target_address, *index) else {
#[allow(unused_mut)]
let Some(mut value) = host.sload(interpreter.contract.target_address, *index) else {
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
#[cfg(feature = "scroll")]
if value.is_cold
&& host.is_storage_key_in_access_list(interpreter.contract.target_address, *index)
{
value.is_cold = false;
}
gas!(interpreter, gas::sload_cost(SPEC::SPEC_ID, value.is_cold));
*index = value.data;
}
Expand All @@ -180,6 +206,13 @@ pub fn sstore<H: Host + ?Sized, SPEC: Spec>(interpreter: &mut Interpreter, host:
interpreter.instruction_result = InstructionResult::FatalExternalError;
return;
};
#[cfg(feature = "scroll")]
if state_load.is_cold
&& host.is_storage_key_in_access_list(interpreter.contract.target_address, index)
{
interpreter.instruction_result = InstructionResult::NotActivated;
return;
}
gas_or_fail!(interpreter, {
let remaining_gas = interpreter.gas.remaining();
gas::sstore_cost(
Expand Down
26 changes: 24 additions & 2 deletions crates/revm/src/context/inner_evm_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,33 @@ impl<DB: Database> InnerEvmContext<DB> {
storage_keys,
} in self.env.tx.access_list.iter()
{
self.journaled_state.initial_account_load(
let result = self.journaled_state.initial_account_load(
*address,
storage_keys.iter().map(|i| U256::from_be_bytes(i.0)),
&mut self.db,
)?;
);
cfg_if::cfg_if! {
if #[cfg(feature = "scroll")] {
// In scroll, we don't include the access list accounts/storages in the partial
// merkle trie proofs if it was not actually accessed in the transaction.
// The load will fail in that case, we just ignore the error.
// This is not a problem as the accounts/storages was never accessed.
match result {
// the concrete error in scroll is
// https://github.com/scroll-tech/stateless-block-verifier/blob/851f5141ded76ddba7594814b9761df1dc469a12/crates/core/src/error.rs#L4-L13
// We cannot check it since `Database::Error` is an opaque type
// without any trait bounds (like `Debug` or `Display`).
// only thing we can do is to check the type name.
Err(EVMError::Database(e))
if core::any::type_name_of_val(&e) == "sbv_core::error::DatabaseError" => {}
_ => {
result?;
}
}
} else {
result?;
}
}
lightsing marked this conversation as resolved.
Show resolved Hide resolved
}
Ok(())
}
Expand Down
Loading