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

Analysed optimization #317

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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: 20 additions & 1 deletion src/eth/evm/revm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
//! interacting with the project's storage backend to manage state. `Revm` embodies the practical application
//! of the `Evm` trait, serving as a bridge between Ethereum's abstract operations and Stratus's storage mechanisms.

use std::collections::HashMap;
use std::sync::Arc;
use std::time::Instant;

use anyhow::anyhow;
use anyhow::Context;
use itertools::Itertools;
use revm::interpreter::analysis::to_analysed;
use revm::interpreter::InstructionResult;
use revm::primitives::AccountInfo as RevmAccountInfo;
use revm::primitives::AccountInfo;
use revm::primitives::Address as RevmAddress;
use revm::primitives::Bytecode as RevmBytecode;
Expand All @@ -21,6 +25,7 @@ use revm::primitives::SpecId;
use revm::primitives::State as RevmState;
use revm::primitives::TransactTo;
use revm::primitives::B256;
use revm::primitives::KECCAK_EMPTY;
use revm::primitives::U256;
use revm::Database;
use revm::EVM;
Expand Down Expand Up @@ -138,6 +143,8 @@ struct RevmDatabaseSession {

/// Metrics collected during EVM execution.
metrics: ExecutionMetrics,

bytecodes: HashMap<Address, RevmBytecode>,
}

impl RevmDatabaseSession {
Expand All @@ -147,6 +154,7 @@ impl RevmDatabaseSession {
input,
storage_changes: Default::default(),
metrics: Default::default(),
bytecodes: HashMap::new(),
}
}
}
Expand Down Expand Up @@ -174,7 +182,18 @@ impl Database for RevmDatabaseSession {
.insert(account.address.clone(), ExecutionAccountChanges::from_existing_account(account.clone()));
}

Ok(Some(account.into()))
let bytecode = if let Some(bytes) = account.bytecode {
Some(self.bytecodes.entry(address).or_insert(to_analysed(bytes.into())).clone())
} else {
None
};

Ok(Some(RevmAccountInfo {
nonce: account.nonce.into(),
balance: account.balance.into(),
code_hash: KECCAK_EMPTY,
code: bytecode,
}))
}

fn code_by_hash(&mut self, _: B256) -> anyhow::Result<RevmBytecode> {
Expand Down
19 changes: 18 additions & 1 deletion src/eth/primitives/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,29 @@ use crate::eth::primitives::Hash;
use crate::eth::primitives::TransactionMined;
use crate::eth::primitives::UnixTime;

#[derive(Debug, Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)]
#[derive(Clone, PartialEq, Eq, fake::Dummy, serde::Serialize, serde::Deserialize)]
pub struct Block {
pub header: BlockHeader,
pub transactions: Vec<TransactionMined>,
}

// Custom Debug implementation for Block, for performance reasons we keep it short
impl std::fmt::Debug for Block {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let transaction_summary = format!(
"{} transaction(s), including hash summaries",
self.transactions.len()
);

f.debug_struct("Block")
.field("number", &self.header.number)
.field("hash", &format_args!("{:?}", self.header.hash))
.field("transactions", &transaction_summary)
.finish()
}
}


impl Block {
/// Creates a new block with the given number and transactions capacity.
pub fn new_with_capacity(number: BlockNumber, timestamp: UnixTime, capacity: usize) -> Self {
Expand Down
Loading