Skip to content

Commit

Permalink
chore: remove postgres slot prediction (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhani-cw authored Jun 14, 2024
1 parent c7abf7f commit 9f86ef3
Show file tree
Hide file tree
Showing 25 changed files with 8 additions and 500 deletions.
1 change: 0 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ impl ExecutorConfig {
// create evm resources
let evm_config = EvmConfig {
chain_id: self.chain_id.into(),
prefetch_slots: false,
};
let evm_storage = Arc::clone(&storage);
let evm_tokio = Handle::current();
Expand Down
29 changes: 0 additions & 29 deletions src/eth/evm/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use std::borrow::Cow;

use display_json::DebugAsJson;
use itertools::Itertools;

use crate::eth::primitives::Address;
use crate::eth::primitives::BlockNumber;
Expand Down Expand Up @@ -58,16 +57,11 @@ pub trait Evm {
fn execute(&mut self, input: EvmInput) -> anyhow::Result<EvmExecutionResult>;
}

pub type EvmInputSlotKeys = Vec<Vec<u8>>;

/// EVM configuration.
#[derive(DebugAsJson, Clone, serde::Serialize)]
pub struct EvmConfig {
/// Chain ID of the EVM.
pub chain_id: ChainId,

/// Should try to predict and prefetch slots before executing transactions?
pub prefetch_slots: bool,
}

/// EVM input data. Usually derived from a transaction or call.
Expand Down Expand Up @@ -205,27 +199,4 @@ impl EvmInput {
},
})
}

/// Calculates all possible 32 byte keys that can be used to access storage slots.
///
/// Possible inputs are:
/// * Sender address.
/// * Receiver address (unlikely).
/// * Every 32 bytes of the data field.
pub fn possible_slot_keys(&self) -> EvmInputSlotKeys {
let mut inputs = vec![];

// from
inputs.push(self.from.as_bytes().to_vec());

// to
if let Some(ref to) = self.to {
inputs.push(to.as_bytes().to_vec());
}

// data
inputs.extend(self.data.0.rchunks_exact(32).map(|chunk| chunk.to_vec()).unique().collect_vec());

inputs
}
}
1 change: 0 additions & 1 deletion src/eth/evm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ pub use evm::Evm;
pub use evm::EvmConfig;
pub use evm::EvmExecutionResult;
pub use evm::EvmInput;
pub use evm::EvmInputSlotKeys;
pub use evm_error::EvmError;
57 changes: 5 additions & 52 deletions src/eth/evm/revm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
//! of the `Evm` trait, serving as a bridge between Ethereum's abstract operations and Stratus's storage mechanisms.
use std::cmp::min;
use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;

use anyhow::anyhow;
Expand All @@ -33,7 +31,6 @@ use crate::eth::evm::Evm;
use crate::eth::evm::EvmConfig;
use crate::eth::evm::EvmError;
use crate::eth::evm::EvmInput;
use crate::eth::primitives::parse_bytecode_slots_indexes;
use crate::eth::primitives::Account;
use crate::eth::primitives::Address;
use crate::eth::primitives::Bytes;
Expand All @@ -46,7 +43,6 @@ use crate::eth::primitives::ExecutionValueChange;
use crate::eth::primitives::Gas;
use crate::eth::primitives::Log;
use crate::eth::primitives::Slot;
use crate::eth::primitives::SlotAccess;
use crate::eth::primitives::SlotIndex;
use crate::eth::storage::StratusStorage;
use crate::ext::not;
Expand Down Expand Up @@ -163,8 +159,6 @@ impl Evm for Revm {
{
metrics::inc_evm_execution(start.elapsed(), &session_point_in_time, execution.is_ok());
metrics::inc_evm_execution_account_reads(session_metrics.account_reads);
metrics::inc_evm_execution_slot_reads(session_metrics.slot_reads);
metrics::inc_evm_execution_slot_reads_cached(session_metrics.slot_reads_cached);
}

execution.map(|execution| EvmExecutionResult {
Expand All @@ -184,17 +178,14 @@ struct RevmSession {
storage: Arc<StratusStorage>,

/// EVM global configuraiton directives,
config: EvmConfig,
_config: EvmConfig,

/// Input passed to EVM to execute the transaction.
input: EvmInput,

/// Changes made to the storage during the execution of the transaction.
storage_changes: ExecutionChanges,

/// Slots cached during account load.
account_slots_cache: HashMap<Address, HashMap<SlotIndex, Slot>>,

/// Metrics collected during EVM execution.
metrics: ExecutionMetrics,
}
Expand All @@ -204,19 +195,17 @@ impl RevmSession {
pub fn new(storage: Arc<StratusStorage>, config: EvmConfig) -> Self {
Self {
storage,
config,
_config: config,
input: Default::default(),
storage_changes: Default::default(),
metrics: Default::default(),
account_slots_cache: Default::default(),
}
}

/// Resets the session to be used with a new transaction.
pub fn reset(&mut self, input: EvmInput) {
self.input = input;
self.storage_changes = Default::default();
self.account_slots_cache.clear();
self.metrics = Default::default();
}
}
Expand All @@ -239,15 +228,6 @@ impl Database for RevmSession {
}
}

// prefetch slots
if self.config.prefetch_slots {
let slot_indexes = account.slot_indexes(self.input.possible_slot_keys());
let slots = handle.block_on(self.storage.read_slots(&address, &slot_indexes, &self.input.point_in_time))?;
for slot in slots {
self.account_slots_cache.entry(address).or_default().insert(slot.index, slot);
}
}

// early convert response because account will be moved
let revm_account: AccountInfo = (&account).into();

Expand All @@ -272,25 +252,8 @@ impl Database for RevmSession {
let address: Address = revm_address.into();
let index: SlotIndex = revm_index.into();

// load slot from storage or (cache and storage)
let slot = match self.config.prefetch_slots {
// try cache
true => {
let cached_slot = self.account_slots_cache.get(&address).and_then(|slot_cache| slot_cache.get(&index));
match cached_slot {
// not found, query storage
None => handle.block_on(self.storage.read_slot(&address, &index, &self.input.point_in_time))?,

// cached
Some(slot) => {
self.metrics.slot_reads_cached += 1;
*slot
}
}
}
// ignore cache
false => handle.block_on(self.storage.read_slot(&address, &index, &self.input.point_in_time))?,
};
// load slot from storage
let slot = handle.block_on(self.storage.read_slot(&address, &index, &self.input.point_in_time))?;

// track original value, except if ignored address
if not(address.is_ignored()) {
Expand Down Expand Up @@ -378,7 +341,7 @@ fn parse_revm_state(revm_state: RevmState, mut execution_changes: ExecutionChang
let (account_created, account_touched) = (revm_account.is_created(), revm_account.is_touched());

// parse revm types to stratus primitives
let mut account: Account = (revm_address, revm_account.info).into();
let account: Account = (revm_address, revm_account.info).into();
let account_modified_slots: Vec<Slot> = revm_account
.storage
.into_iter()
Expand All @@ -390,16 +353,6 @@ fn parse_revm_state(revm_state: RevmState, mut execution_changes: ExecutionChang

// handle account created (contracts) or touched (everything else)
if account_created {
// parse bytecode slots
let slot_indexes: HashSet<SlotAccess> = match account.bytecode {
Some(ref bytecode) if not(bytecode.is_empty()) => parse_bytecode_slots_indexes(bytecode.clone()),
_ => HashSet::new(),
};
for index in slot_indexes {
account.add_bytecode_slot_index(index);
}

// track account
let account_changes = ExecutionAccountChanges::from_modified_values(account, account_modified_slots);
execution_changes.insert(account_changes.address, account_changes);
} else if account_touched {
Expand Down
2 changes: 0 additions & 2 deletions src/eth/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ impl Executor {
{
metrics::inc_executor_external_block(start.elapsed());
metrics::inc_executor_external_block_account_reads(block_metrics.account_reads);
metrics::inc_executor_external_block_slot_reads(block_metrics.slot_reads);
metrics::inc_executor_external_block_slot_reads_cached(block_metrics.slot_reads_cached);
}

Ok(())
Expand Down
47 changes: 0 additions & 47 deletions src/eth/primitives/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,13 @@
//! contract accounts.
use display_json::DebugAsJson;
use itertools::Itertools;
use revm::primitives::AccountInfo as RevmAccountInfo;
use revm::primitives::Address as RevmAddress;

use crate::eth::evm::EvmInputSlotKeys;
use crate::eth::primitives::Address;
use crate::eth::primitives::Bytes;
use crate::eth::primitives::CodeHash;
use crate::eth::primitives::Nonce;
use crate::eth::primitives::SlotAccess;
use crate::eth::primitives::SlotIndexes;
use crate::eth::primitives::Wei;
use crate::ext::OptionExt;

Expand All @@ -42,12 +38,6 @@ pub struct Account {

/// Keccak256 Hash of the bytecode. If bytecode is null, then the hash of empty string.
pub code_hash: CodeHash,

/// Slots indexes that are accessed statically.
pub static_slot_indexes: Option<SlotIndexes>,

/// Slots indexes that are accessed using the mapping hash algorithm.
pub mapping_slot_indexes: Option<SlotIndexes>,
}

impl Account {
Expand All @@ -64,8 +54,6 @@ impl Account {
balance,
bytecode: None,
code_hash: CodeHash::default(),
static_slot_indexes: None,
mapping_slot_indexes: None,
}
}

Expand All @@ -83,39 +71,6 @@ impl Account {
None => false,
}
}

/// Compute slot indexes to be accessed for a give input.
pub fn slot_indexes(&self, input_keys: EvmInputSlotKeys) -> SlotIndexes {
let mut slot_indexes = SlotIndexes::new();

// calculate static indexes
if let Some(ref indexes) = self.static_slot_indexes {
slot_indexes.extend(indexes.0.clone());
}

// calculate mapping indexes
if let Some(ref indexes) = self.mapping_slot_indexes {
for (base_slot_index, input_key) in indexes.iter().cartesian_product(input_keys.into_iter()) {
let mapping_slot_index = base_slot_index.to_mapping_index(input_key);
slot_indexes.insert(mapping_slot_index);
}
}

slot_indexes
}

/// Adds a bytecode slot index according to its type.
pub fn add_bytecode_slot_index(&mut self, index: SlotAccess) {
match index {
SlotAccess::Static(index) => {
self.static_slot_indexes.get_or_insert_with(SlotIndexes::new).insert(index);
}
SlotAccess::Mapping(index) => {
self.mapping_slot_indexes.get_or_insert_with(SlotIndexes::new).insert(index);
}
_ => {}
}
}
}

// -----------------------------------------------------------------------------
Expand All @@ -131,8 +86,6 @@ impl From<(RevmAddress, RevmAccountInfo)> for Account {
balance: info.balance.into(),
bytecode: info.code.map_into(),
code_hash: info.code_hash.into(),
static_slot_indexes: None,
mapping_slot_indexes: None,
}
}
}
Expand Down
Loading

0 comments on commit 9f86ef3

Please sign in to comment.