Skip to content

Commit d85e6d4

Browse files
authored
chore(l1): cache tx hash (#4049)
**Motivation** We saw that the hashing of txs takes a significant amount of time and part of it is repeated. **Description** This PR create a new public hash function that allows to cache the computation of the hash for subsequent calls. After merging main there are some more changes needed. I'm taking a look at them. Closes #issue_number
1 parent 314ba22 commit d85e6d4

File tree

23 files changed

+101
-50
lines changed

23 files changed

+101
-50
lines changed

cmd/ef_tests/blockchain/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ impl From<Transaction> for EIP1559Transaction {
330330
signature_y_parity: !val.v.is_zero(),
331331
signature_r: val.r,
332332
signature_s: val.s,
333+
..Default::default()
333334
}
334335
}
335336
}
@@ -362,6 +363,7 @@ impl From<Transaction> for EIP4844Transaction {
362363
signature_y_parity: !val.v.is_zero(),
363364
signature_r: val.r,
364365
signature_s: val.s,
366+
..Default::default()
365367
}
366368
}
367369
}
@@ -405,6 +407,7 @@ impl From<Transaction> for EIP7702Transaction {
405407
signature_y_parity: !val.v.is_zero(),
406408
signature_r: val.r,
407409
signature_s: val.s,
410+
..Default::default()
408411
}
409412
}
410413
}
@@ -425,6 +428,7 @@ impl From<Transaction> for LegacyTransaction {
425428
v: val.v,
426429
r: val.r,
427430
s: val.s,
431+
..Default::default()
428432
}
429433
}
430434
}
@@ -452,6 +456,7 @@ impl From<Transaction> for EIP2930Transaction {
452456
signature_y_parity: !val.v.is_zero(),
453457
signature_r: val.r,
454458
signature_s: val.s,
459+
..Default::default()
455460
}
456461
}
457462
}

cmd/ethrex_replay/src/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ pub async fn run_tx(
5555
let (receipt, _) = vm.execute_tx(tx, &block.header, &mut remaining_gas, tx_sender)?;
5656
let account_updates = vm.get_state_transitions()?;
5757
wrapped_db.apply_account_updates(&account_updates)?;
58-
if tx.compute_hash() == tx_hash {
58+
if tx.hash() == tx_hash {
5959
return Ok((receipt, account_updates));
6060
}
6161
}

crates/blockchain/blockchain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ impl Blockchain {
610610
blobs_bundle.validate(&transaction, fork)?;
611611

612612
let transaction = Transaction::EIP4844Transaction(transaction);
613-
let hash = transaction.compute_hash();
613+
let hash = transaction.hash();
614614
if self.mempool.contains_tx(hash)? {
615615
return Ok(hash);
616616
}
@@ -637,7 +637,7 @@ impl Blockchain {
637637
if matches!(transaction, Transaction::EIP4844Transaction(_)) {
638638
return Err(MempoolError::BlobTxNoBlobsBundle);
639639
}
640-
let hash = transaction.compute_hash();
640+
let hash = transaction.hash();
641641
if self.mempool.contains_tx(hash)? {
642642
return Ok(hash);
643643
}

crates/blockchain/mempool.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl Mempool {
8080
self.blobs_bundle_pool
8181
.lock()
8282
.map_err(|error| StoreError::Custom(error.to_string()))?
83-
.remove(&tx.compute_hash());
83+
.remove(hash);
8484
}
8585

8686
self.txs_by_sender_nonce
@@ -283,7 +283,7 @@ impl Mempool {
283283
nonce: u64,
284284
tx: &Transaction,
285285
) -> Result<Option<H256>, MempoolError> {
286-
let Some(tx_in_pool) = self.contains_sender_nonce(sender, nonce, tx.compute_hash())? else {
286+
let Some(tx_in_pool) = self.contains_sender_nonce(sender, nonce, tx.hash())? else {
287287
return Ok(None);
288288
};
289289

@@ -321,7 +321,7 @@ impl Mempool {
321321
return Err(MempoolError::NonceTooLow);
322322
}
323323

324-
Ok(Some(tx_in_pool.compute_hash()))
324+
Ok(Some(tx_in_pool.hash()))
325325
}
326326
}
327327

@@ -768,8 +768,8 @@ mod tests {
768768
let blob_tx_decoded = Transaction::decode_canonical(&hex::decode("03f88f0780843b9aca008506fc23ac00830186a09400000000000000000000000000000000000001008080c001e1a0010657f37554c781402a22917dee2f75def7ab966d7b770905398eba3c44401401a0840650aa8f74d2b07f40067dc33b715078d73422f01da17abdbd11e02bbdfda9a04b2260f6022bf53eadb337b3e59514936f7317d872defb891a708ee279bdca90").unwrap()).unwrap();
769769
let blob_tx_sender = blob_tx_decoded.sender().unwrap();
770770
let blob_tx = MempoolTransaction::new(blob_tx_decoded, blob_tx_sender);
771-
let plain_tx_hash = plain_tx.compute_hash();
772-
let blob_tx_hash = blob_tx.compute_hash();
771+
let plain_tx_hash = plain_tx.hash();
772+
let blob_tx_hash = blob_tx.hash();
773773
let mempool = Mempool::new();
774774
let filter =
775775
|tx: &Transaction| -> bool { matches!(tx, Transaction::EIP4844Transaction(_)) };

crates/blockchain/payload.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,25 +410,22 @@ impl Blockchain {
410410

411411
// Check if we have enough gas to run the transaction
412412
if context.remaining_gas < head_tx.tx.gas_limit() {
413-
debug!(
414-
"Skipping transaction: {}, no gas left",
415-
head_tx.tx.compute_hash()
416-
);
413+
debug!("Skipping transaction: {}, no gas left", head_tx.tx.hash());
417414
// We don't have enough gas left for the transaction, so we skip all txs from this account
418415
txs.pop();
419416
continue;
420417
}
421418

422419
// TODO: maybe fetch hash too when filtering mempool so we don't have to compute it here (we can do this in the same refactor as adding timestamp)
423-
let tx_hash = head_tx.tx.compute_hash();
420+
let tx_hash = head_tx.tx.hash();
424421

425422
// Check whether the tx is replay-protected
426423
if head_tx.tx.protected() && !chain_config.is_eip155_activated(context.block_number()) {
427424
// Ignore replay protected tx & all txs from the sender
428425
// Pull transaction from the mempool
429426
debug!("Ignoring replay-protected transaction: {}", tx_hash);
430427
txs.pop();
431-
self.remove_transaction_from_pool(&head_tx.tx.compute_hash())?;
428+
self.remove_transaction_from_pool(&tx_hash)?;
432429
continue;
433430
}
434431

@@ -437,7 +434,7 @@ impl Blockchain {
437434
Ok(receipt) => {
438435
txs.shift()?;
439436
// Pull transaction from the mempool
440-
self.remove_transaction_from_pool(&head_tx.tx.compute_hash())?;
437+
self.remove_transaction_from_pool(&tx_hash)?;
441438

442439
metrics!(METRICS_TX.inc_tx_with_type(MetricsTxType(head_tx.tx_type())));
443440
receipt
@@ -479,7 +476,7 @@ impl Blockchain {
479476
context: &mut PayloadBuildContext,
480477
) -> Result<Receipt, ChainError> {
481478
// Fetch blobs bundle
482-
let tx_hash = head.tx.compute_hash();
479+
let tx_hash = head.tx.hash();
483480
let chain_config = context.chain_config()?;
484481
let max_blob_number_per_block = chain_config
485482
.get_fork_blob_schedule(context.payload.header.timestamp)

crates/blockchain/tracing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl Blockchain {
7070
// We are cloning the `Arc`s here, not the structs themselves
7171
let block = block.clone();
7272
let vm = vm.clone();
73-
let tx_hash = block.as_ref().body.transactions[index].compute_hash();
73+
let tx_hash = block.as_ref().body.transactions[index].hash();
7474
let call_trace = timeout_trace_operation(timeout, move || {
7575
vm.lock()
7676
.map_err(|_| EvmError::Custom("Unexpected Runtime Error".to_string()))?

0 commit comments

Comments
 (0)