diff --git a/lib/ain-evm/src/evm.rs b/lib/ain-evm/src/evm.rs index 66490edc4d..c43652e5f5 100644 --- a/lib/ain-evm/src/evm.rs +++ b/lib/ain-evm/src/evm.rs @@ -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(()) } diff --git a/lib/ain-evm/src/executor.rs b/lib/ain-evm/src/executor.rs index 9febf70f34..f580edce25 100644 --- a/lib/ain-evm/src/executor.rs +++ b/lib/ain-evm/src/executor.rs @@ -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 = @@ -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 ); @@ -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 ); diff --git a/lib/ain-evm/src/receipt.rs b/lib/ain-evm/src/receipt.rs index a9f4f9f16c..3de9cb0f8d 100644 --- a/lib/ain-evm/src/receipt.rs +++ b/lib/ain-evm/src/receipt.rs @@ -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, diff --git a/lib/ain-evm/src/transaction/mod.rs b/lib/ain-evm/src/transaction/mod.rs index 392609e62f..0dcce081a4 100644 --- a/lib/ain-evm/src/transaction/mod.rs +++ b/lib/ain-evm/src/transaction/mod.rs @@ -100,6 +100,7 @@ impl From<&LegacyTransaction> for LegacyUnsignedTransaction { pub struct SignedTx { pub transaction: TransactionV2, pub sender: H160, + hash_cache: Cell>, } impl fmt::Debug for SignedTx { @@ -180,6 +181,7 @@ impl TryFrom for SignedTx { Ok(SignedTx { transaction: src, sender: public_key_to_address(&pubkey), + hash_cache: Cell::new(None), }) } } @@ -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 { @@ -331,6 +338,7 @@ impl SignedTx { } use std::{ + cell::Cell, cmp::min, convert::{TryFrom, TryInto}, fmt, diff --git a/lib/ain-grpc/src/rpc/eth.rs b/lib/ain-grpc/src/rpc/eth.rs index 2cd340ca9f..7572a9121d 100644 --- a/lib/ain-grpc/src/rpc/eth.rs +++ b/lib/ain-grpc/src/rpc/eth.rs @@ -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 @@ -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!( diff --git a/lib/ain-grpc/src/tests.rs b/lib/ain-grpc/src/tests.rs index ad57190d49..87187d7386 100644 --- a/lib/ain-grpc/src/tests.rs +++ b/lib/ain-grpc/src/tests.rs @@ -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(); diff --git a/lib/ain-grpc/src/transaction.rs b/lib/ain-grpc/src/transaction.rs index 97c043647d..7a7313a32c 100644 --- a/lib/ain-grpc/src/transaction.rs +++ b/lib/ain-grpc/src/transaction.rs @@ -32,7 +32,7 @@ impl From 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()),