Skip to content

Commit

Permalink
perf: query bytecodes with &B256 to avoid copying code hash (#13559)
Browse files Browse the repository at this point in the history
  • Loading branch information
hai-rise authored Dec 26, 2024
1 parent 951e2fd commit 56ce046
Show file tree
Hide file tree
Showing 13 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crates/chain-state/src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ mod tests {
Ok(None)
}

fn bytecode_by_hash(&self, _code_hash: B256) -> ProviderResult<Option<Bytecode>> {
fn bytecode_by_hash(&self, _code_hash: &B256) -> ProviderResult<Option<Bytecode>> {
Ok(None)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/chain-state/src/memory_overlay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ impl<N: NodePrimitives> StateProvider for MemoryOverlayStateProviderRef<'_, N> {
self.historical.storage(address, storage_key)
}

fn bytecode_by_hash(&self, code_hash: B256) -> ProviderResult<Option<Bytecode>> {
fn bytecode_by_hash(&self, code_hash: &B256) -> ProviderResult<Option<Bytecode>> {
for block in &self.in_memory {
if let Some(contract) = block.execution_output.bytecode(&code_hash) {
if let Some(contract) = block.execution_output.bytecode(code_hash) {
return Ok(Some(contract));
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/revm/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub trait EvmStateProvider: Send + Sync {
/// Get account code by hash.
fn bytecode_by_hash(
&self,
code_hash: B256,
code_hash: &B256,
) -> ProviderResult<Option<reth_primitives::Bytecode>>;

/// Get storage of the given account.
Expand All @@ -48,7 +48,7 @@ impl<T: reth_storage_api::StateProvider> EvmStateProvider for T {

fn bytecode_by_hash(
&self,
code_hash: B256,
code_hash: &B256,
) -> ProviderResult<Option<reth_primitives::Bytecode>> {
<T as reth_storage_api::StateProvider>::bytecode_by_hash(self, code_hash)
}
Expand Down Expand Up @@ -148,7 +148,7 @@ impl<DB: EvmStateProvider> DatabaseRef for StateProviderDatabase<DB> {
///
/// Returns `Ok` with the bytecode if found, or the default bytecode otherwise.
fn code_by_hash_ref(&self, code_hash: B256) -> Result<Bytecode, Self::Error> {
Ok(self.bytecode_by_hash(code_hash)?.unwrap_or_default().0)
Ok(self.bytecode_by_hash(&code_hash)?.unwrap_or_default().0)
}

/// Retrieves the storage value at a specific index for a given address.
Expand Down
4 changes: 2 additions & 2 deletions crates/revm/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl StateProvider for StateProviderTest {
Ok(self.accounts.get(&account).and_then(|(storage, _)| storage.get(&storage_key).copied()))
}

fn bytecode_by_hash(&self, code_hash: B256) -> ProviderResult<Option<Bytecode>> {
Ok(self.contracts.get(&code_hash).cloned())
fn bytecode_by_hash(&self, code_hash: &B256) -> ProviderResult<Option<Bytecode>> {
Ok(self.contracts.get(code_hash).cloned())
}
}
2 changes: 1 addition & 1 deletion crates/rpc/rpc-eth-types/src/cache/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl StateProvider for StateProviderTraitObjWrapper<'_> {

fn bytecode_by_hash(
&self,
code_hash: B256,
code_hash: &B256,
) -> reth_errors::ProviderResult<Option<reth_primitives::Bytecode>> {
self.0.bytecode_by_hash(code_hash)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ impl<SP: StateProvider, EDP: ExecutionDataProvider> StateProvider for BundleStat
self.state_provider.storage(account, storage_key)
}

fn bytecode_by_hash(&self, code_hash: B256) -> ProviderResult<Option<Bytecode>> {
fn bytecode_by_hash(&self, code_hash: &B256) -> ProviderResult<Option<Bytecode>> {
if let Some(bytecode) =
self.block_execution_data_provider.execution_outcome().bytecode(&code_hash)
self.block_execution_data_provider.execution_outcome().bytecode(code_hash)
{
return Ok(Some(bytecode))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/provider/src/providers/state/historical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,8 +438,8 @@ impl<Provider: DBProvider + BlockNumReader + BlockHashReader + StateCommitmentPr
}

/// Get account code by its hash
fn bytecode_by_hash(&self, code_hash: B256) -> ProviderResult<Option<Bytecode>> {
self.tx().get::<tables::Bytecodes>(code_hash).map_err(Into::into)
fn bytecode_by_hash(&self, code_hash: &B256) -> ProviderResult<Option<Bytecode>> {
self.tx().get_by_encoded_key::<tables::Bytecodes>(code_hash).map_err(Into::into)
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/storage/provider/src/providers/state/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ impl<Provider: DBProvider + BlockHashReader + StateCommitmentProvider> StateProv
}

/// Get account code by its hash
fn bytecode_by_hash(&self, code_hash: B256) -> ProviderResult<Option<Bytecode>> {
self.tx().get::<tables::Bytecodes>(code_hash).map_err(Into::into)
fn bytecode_by_hash(&self, code_hash: &B256) -> ProviderResult<Option<Bytecode>> {
self.tx().get_by_encoded_key::<tables::Bytecodes>(code_hash).map_err(Into::into)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/storage/provider/src/providers/state/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ macro_rules! delegate_provider_impls {
}
StateProvider $(where [$($generics)*])? {
fn storage(&self, account: alloy_primitives::Address, storage_key: alloy_primitives::StorageKey) -> reth_storage_errors::provider::ProviderResult<Option<alloy_primitives::StorageValue>>;
fn bytecode_by_hash(&self, code_hash: alloy_primitives::B256) -> reth_storage_errors::provider::ProviderResult<Option<reth_primitives::Bytecode>>;
fn bytecode_by_hash(&self, code_hash: &alloy_primitives::B256) -> reth_storage_errors::provider::ProviderResult<Option<reth_primitives::Bytecode>>;
}
StateRootProvider $(where [$($generics)*])? {
fn state_root(&self, state: reth_trie::HashedPostState) -> reth_storage_errors::provider::ProviderResult<alloy_primitives::B256>;
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/provider/src/test_utils/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,11 +693,11 @@ impl StateProvider for MockEthProvider {
Ok(lock.get(&account).and_then(|account| account.storage.get(&storage_key)).copied())
}

fn bytecode_by_hash(&self, code_hash: B256) -> ProviderResult<Option<Bytecode>> {
fn bytecode_by_hash(&self, code_hash: &B256) -> ProviderResult<Option<Bytecode>> {
let lock = self.accounts.lock();
Ok(lock.values().find_map(|account| {
match (account.account.bytecode_hash.as_ref(), account.bytecode.as_ref()) {
(Some(bytecode_hash), Some(bytecode)) if *bytecode_hash == code_hash => {
(Some(bytecode_hash), Some(bytecode)) if bytecode_hash == code_hash => {
Some(bytecode.clone())
}
_ => None,
Expand Down
2 changes: 1 addition & 1 deletion crates/storage/storage-api/src/noop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ impl<C: Send + Sync, N: NodePrimitives> StateProvider for NoopProvider<C, N> {
Ok(None)
}

fn bytecode_by_hash(&self, _code_hash: B256) -> ProviderResult<Option<Bytecode>> {
fn bytecode_by_hash(&self, _code_hash: &B256) -> ProviderResult<Option<Bytecode>> {
Ok(None)
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/storage/storage-api/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub trait StateProvider:
) -> ProviderResult<Option<StorageValue>>;

/// Get account code by its hash
fn bytecode_by_hash(&self, code_hash: B256) -> ProviderResult<Option<Bytecode>>;
fn bytecode_by_hash(&self, code_hash: &B256) -> ProviderResult<Option<Bytecode>>;

/// Get account code by its address.
///
Expand All @@ -53,7 +53,7 @@ pub trait StateProvider:
return Ok(None)
}
// Get the code from the code hash
return self.bytecode_by_hash(code_hash)
return self.bytecode_by_hash(&code_hash)
}

// Return `None` if no code hash is set
Expand Down
4 changes: 2 additions & 2 deletions crates/transaction-pool/src/validate/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ where
//
// Any other case means that the account is not an EOA, and should not be able to send
// transactions.
if account.has_bytecode() {
if let Some(code_hash) = &account.bytecode_hash {
let is_eip7702 = if self.fork_tracker.is_prague_activated() {
match state.bytecode_by_hash(account.get_bytecode_hash()) {
match state.bytecode_by_hash(code_hash) {
Ok(bytecode) => bytecode.unwrap_or_default().is_eip7702(),
Err(err) => {
return TransactionValidationOutcome::Error(
Expand Down

0 comments on commit 56ce046

Please sign in to comment.