Skip to content

Commit

Permalink
enha: make save_pending_execution cheaper
Browse files Browse the repository at this point in the history
  • Loading branch information
carneiro-cw committed Dec 12, 2024
1 parent 062fedd commit 1e4bb04
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/eth/executor/evm_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,16 @@ impl EvmInput {
self.to.is_some() && not(self.data.is_empty())
}
}

impl PartialEq<(&TransactionInput, &PendingBlockHeader)> for EvmInput {
fn eq(&self, other: &(&TransactionInput, &PendingBlockHeader)) -> bool {
self.block_number == other.1.number
&& self.block_timestamp == *other.1.timestamp
&& self.chain_id == other.0.chain_id
&& self.data == other.0.input
&& self.from == other.0.signer
&& self.nonce.is_some_and(|inner| inner == other.0.nonce)
&& self.value == other.0.value
&& self.to == other.0.to
}
}
11 changes: 7 additions & 4 deletions src/eth/storage/temporary/inmemory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::collections::HashMap;

use parking_lot::RwLock;
use parking_lot::RwLockUpgradableReadGuard;

use crate::eth::executor::EvmInput;
use crate::eth::primitives::Account;
Expand All @@ -18,6 +19,7 @@ use crate::eth::primitives::Slot;
use crate::eth::primitives::SlotIndex;
use crate::eth::primitives::StratusError;
use crate::eth::primitives::TransactionExecution;
use crate::eth::primitives::TransactionInput;
#[cfg(feature = "dev")]
use crate::eth::primitives::UnixTime;
#[cfg(feature = "dev")]
Expand Down Expand Up @@ -121,18 +123,19 @@ impl TemporaryStorage for InMemoryTemporaryStorage {

fn save_pending_execution(&self, tx: TransactionExecution, check_conflicts: bool) -> Result<(), StratusError> {
// check conflicts
let mut pending_block = self.pending_block.write();
let pending_block = self.pending_block.upgradable_read();
if let TransactionExecution::Local(tx) = &tx {
let expected_input = EvmInput::from_eth_transaction(&tx.input, &pending_block.block.header);

if expected_input != tx.evm_input {
if tx.evm_input != (&tx.input, &pending_block.block.header) {
let expected_input = EvmInput::from_eth_transaction(&tx.input, &pending_block.block.header);
return Err(StratusError::TransactionEvmInputMismatch {
expected: Box::new(expected_input),
actual: Box::new(tx.evm_input.clone()),
});
}
}

let mut pending_block = RwLockUpgradableReadGuard::<InMemoryTemporaryStorageState>::upgrade(pending_block);

if check_conflicts {
if let Some(conflicts) = self.check_conflicts(tx.execution())? {
return Err(StratusError::TransactionConflict(conflicts.into()));
Expand Down

0 comments on commit 1e4bb04

Please sign in to comment.