Skip to content

Commit

Permalink
Cache Keccak Hashes per SignedTx (#2544)
Browse files Browse the repository at this point in the history
* Cache tx hash on SignedTx

* fmt

* Update lib/ain-evm/src/transaction/mod.rs

Co-authored-by: Jouzo <[email protected]>

* Update lib/ain-evm/src/transaction/mod.rs

Co-authored-by: Jouzo <[email protected]>

* Update lib/ain-evm/src/transaction/mod.rs

Co-authored-by: Jouzo <[email protected]>

* Fix rust lints

* Use signed_tx.hash() consistently

* Revert lazy changes

---------

Co-authored-by: Jouzo <[email protected]>
Co-authored-by: Bushstar <[email protected]>
Co-authored-by: jouzo <[email protected]>
  • Loading branch information
4 people authored Oct 9, 2023
1 parent ecae64f commit cbdaa1e
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/ain-evm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ impl EVMServices {
.tx_queues
.push_in(queue_id, tx, hash, gas_used, state_root)?;

self.filters.add_tx_to_filters(signed_tx.transaction.hash());
self.filters.add_tx_to_filters(signed_tx.hash());

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions lib/ain-evm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ impl<'backend> AinExecutor<'backend> {
"[apply_queue_tx]receipt : {:?}, exit_reason {:#?} for signed_tx : {:#x}",
receipt,
tx_response.exit_reason,
signed_tx.transaction.hash()
signed_tx.hash()
);

let gas_fee =
Expand Down Expand Up @@ -326,7 +326,7 @@ impl<'backend> AinExecutor<'backend> {
"[apply_queue_tx] receipt : {:?}, exit_reason {:#?} for signed_tx : {:#x}, logs: {:x?}",
receipt,
tx_response.exit_reason,
signed_tx.transaction.hash(),
signed_tx.hash(),
tx_response.logs
);

Expand Down Expand Up @@ -401,7 +401,7 @@ impl<'backend> AinExecutor<'backend> {
"[apply_queue_tx] receipt : {:?}, exit_reason {:#?} for signed_tx : {:#x}, logs: {:x?}",
receipt,
tx_response.exit_reason,
signed_tx.transaction.hash(),
signed_tx.hash(),
tx_response.logs
);

Expand Down
2 changes: 1 addition & 1 deletion lib/ain-evm/src/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl ReceiptService {
receipt,
block_hash,
block_number,
tx_hash: signed_tx.transaction.hash(),
tx_hash: signed_tx.hash(),
from: signed_tx.sender,
to: signed_tx.to(),
tx_index: index,
Expand Down
16 changes: 12 additions & 4 deletions lib/ain-evm/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ impl From<&LegacyTransaction> for LegacyUnsignedTransaction {
pub struct SignedTx {
pub transaction: TransactionV2,
pub sender: H160,
hash_cache: Cell<Option<H256>>,
}

impl fmt::Debug for SignedTx {
Expand Down Expand Up @@ -180,6 +181,7 @@ impl TryFrom<TransactionV2> for SignedTx {
Ok(SignedTx {
transaction: src,
sender: public_key_to_address(&pubkey),
hash_cache: Cell::new(None),
})
}
}
Expand Down Expand Up @@ -318,11 +320,16 @@ impl SignedTx {
}

pub fn hash(&self) -> H256 {
match &self.transaction {
TransactionV2::Legacy(tx) => tx.hash(),
TransactionV2::EIP2930(tx) => tx.hash(),
TransactionV2::EIP1559(tx) => tx.hash(),
let h = &self.hash_cache;
if h.get().is_none() {
let val = match &self.transaction {
TransactionV2::Legacy(tx) => tx.hash(),
TransactionV2::EIP2930(tx) => tx.hash(),
TransactionV2::EIP1559(tx) => tx.hash(),
};
h.set(Some(val));
}
h.get().unwrap()
}

pub fn get_tx_type(&self) -> U256 {
Expand All @@ -331,6 +338,7 @@ impl SignedTx {
}

use std::{
cell::Cell,
cmp::min,
convert::{TryFrom, TryInto},
fmt,
Expand Down
4 changes: 2 additions & 2 deletions lib/ain-grpc/src/rpc/eth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ impl MetachainRPCServer for MetachainRPCModule {
);
debug!(target:"rpc",
"[send_raw_transaction] transaction hash : {:#x}",
signed_tx.transaction.hash()
signed_tx.hash()
);

if !self
Expand All @@ -751,7 +751,7 @@ impl MetachainRPCServer for MetachainRPCModule {
)));
}

Ok(format!("{:#x}", signed_tx.transaction.hash()))
Ok(format!("{:#x}", signed_tx.hash()))
} else {
debug!(target:"rpc", "[send_raw_transaction] Could not publish raw transaction: {tx} reason: {res_string}");
Err(Error::Custom(format!(
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-grpc/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn should_get_transaction_by_hash() {
handler.storage.put_block(block.clone());

let input = EthGetTransactionByHashInput {
hash: format!("{:x}", signed_tx.transaction.hash()),
hash: format!("{:x}", signed_tx.hash()),
};

let res = EthService::Eth_GetTransactionByHash(handler.clone(), input.clone().into()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion lib/ain-grpc/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl From<SignedTx> for EthTransactionInfo {
};

EthTransactionInfo {
hash: format_h256(signed_tx.transaction.hash()),
hash: format_h256(signed_tx.hash()),
from: format_address(signed_tx.sender),
to: signed_tx.to().map(format_address),
gas: format_u256(signed_tx.gas_limit()),
Expand Down

0 comments on commit cbdaa1e

Please sign in to comment.