From a08320dad0ee5c953afabe346a4209f121998a8f Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 9 Oct 2023 10:12:32 +0800 Subject: [PATCH 1/8] Cache tx hash on SignedTx --- lib/ain-evm/src/transaction/mod.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/ain-evm/src/transaction/mod.rs b/lib/ain-evm/src/transaction/mod.rs index d90dd071d5..069abe0601 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 { @@ -179,6 +180,7 @@ impl TryFrom for SignedTx { Ok(SignedTx { transaction: src, sender: public_key_to_address(&pubkey), + hash_cache: Cell::new(None), }) } } @@ -317,11 +319,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 { @@ -332,7 +339,7 @@ impl SignedTx { use std::{ cmp::min, convert::{TryFrom, TryInto}, - fmt, + fmt, cell::Cell, }; #[derive(Debug)] From a64950def3327ee3448cda02328bfc71f10044fe Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 9 Oct 2023 10:13:38 +0800 Subject: [PATCH 2/8] fmt --- lib/ain-evm/src/transaction/mod.rs | 3 ++- test/functional/interface_http_cors.py | 8 +++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/ain-evm/src/transaction/mod.rs b/lib/ain-evm/src/transaction/mod.rs index 069abe0601..0977f236b0 100644 --- a/lib/ain-evm/src/transaction/mod.rs +++ b/lib/ain-evm/src/transaction/mod.rs @@ -337,9 +337,10 @@ impl SignedTx { } use std::{ + cell::Cell, cmp::min, convert::{TryFrom, TryInto}, - fmt, cell::Cell, + fmt, }; #[derive(Debug)] diff --git a/test/functional/interface_http_cors.py b/test/functional/interface_http_cors.py index 40953cdac8..68df66c91c 100755 --- a/test/functional/interface_http_cors.py +++ b/test/functional/interface_http_cors.py @@ -21,7 +21,6 @@ def run_test(self): self.test_json_rpc_port() self.test_eth_json_rpc_port() - def test_json_rpc_port(self): url = urllib.parse.urlparse(self.nodes[0].url) authpair = url.username + ":" + url.password @@ -71,7 +70,9 @@ def test_eth_json_rpc_port(self): assert_equal(res.status, http.client.OK) res.close() - def check_cors_headers(self, res, check_allow_methods=True, check_allow_headers=True): + def check_cors_headers( + self, res, check_allow_methods=True, check_allow_headers=True + ): assert_equal(res.getheader("Access-Control-Allow-Origin"), self.cors_origin) assert_equal(res.getheader("Access-Control-Allow-Credentials"), "true") if check_allow_methods: @@ -80,7 +81,8 @@ def check_cors_headers(self, res, check_allow_methods=True, check_allow_headers= ) if check_allow_headers: assert_equal( - res.getheader("Access-Control-Allow-Headers"), "Content-Type, Authorization" + res.getheader("Access-Control-Allow-Headers"), + "Content-Type, Authorization", ) From 887b2d90f582059cf504490cf4337e16bf354069 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 9 Oct 2023 14:58:46 +0800 Subject: [PATCH 3/8] Update lib/ain-evm/src/transaction/mod.rs Co-authored-by: Jouzo <15011228+Jouzo@users.noreply.github.com> --- lib/ain-evm/src/transaction/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ain-evm/src/transaction/mod.rs b/lib/ain-evm/src/transaction/mod.rs index 0977f236b0..013041ed39 100644 --- a/lib/ain-evm/src/transaction/mod.rs +++ b/lib/ain-evm/src/transaction/mod.rs @@ -178,9 +178,9 @@ impl TryFrom for SignedTx { } }?; Ok(SignedTx { + hash: src.hash(), transaction: src, sender: public_key_to_address(&pubkey), - hash_cache: Cell::new(None), }) } } From 6968358e62cf6d2fabf7d6b88b79058ec5b7c6a8 Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 9 Oct 2023 14:58:51 +0800 Subject: [PATCH 4/8] Update lib/ain-evm/src/transaction/mod.rs Co-authored-by: Jouzo <15011228+Jouzo@users.noreply.github.com> --- lib/ain-evm/src/transaction/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ain-evm/src/transaction/mod.rs b/lib/ain-evm/src/transaction/mod.rs index 013041ed39..579fb4ccab 100644 --- a/lib/ain-evm/src/transaction/mod.rs +++ b/lib/ain-evm/src/transaction/mod.rs @@ -100,7 +100,7 @@ impl From<&LegacyTransaction> for LegacyUnsignedTransaction { pub struct SignedTx { pub transaction: TransactionV2, pub sender: H160, - hash_cache: Cell>, + hash: H256, } impl fmt::Debug for SignedTx { From 421e8a1dce3c5a8065661a4602c4a0fc6a09c97f Mon Sep 17 00:00:00 2001 From: Prasanna Loganathar Date: Mon, 9 Oct 2023 14:58:58 +0800 Subject: [PATCH 5/8] Update lib/ain-evm/src/transaction/mod.rs Co-authored-by: Jouzo <15011228+Jouzo@users.noreply.github.com> --- lib/ain-evm/src/transaction/mod.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/lib/ain-evm/src/transaction/mod.rs b/lib/ain-evm/src/transaction/mod.rs index 579fb4ccab..ae750472c4 100644 --- a/lib/ain-evm/src/transaction/mod.rs +++ b/lib/ain-evm/src/transaction/mod.rs @@ -319,16 +319,7 @@ impl SignedTx { } pub fn hash(&self) -> H256 { - 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() + self.hash } pub fn get_tx_type(&self) -> U256 { From 607abb390f2abb2da6898dc69ddce000c546c291 Mon Sep 17 00:00:00 2001 From: Bushstar Date: Mon, 9 Oct 2023 11:20:22 +0100 Subject: [PATCH 6/8] Fix rust lints --- lib/ain-evm/src/transaction/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/ain-evm/src/transaction/mod.rs b/lib/ain-evm/src/transaction/mod.rs index ae750472c4..2e679d275b 100644 --- a/lib/ain-evm/src/transaction/mod.rs +++ b/lib/ain-evm/src/transaction/mod.rs @@ -319,7 +319,7 @@ impl SignedTx { } pub fn hash(&self) -> H256 { - self.hash + self.hash } pub fn get_tx_type(&self) -> U256 { @@ -328,7 +328,6 @@ impl SignedTx { } use std::{ - cell::Cell, cmp::min, convert::{TryFrom, TryInto}, fmt, From 979ceb2bf80b96e3c3cccad8b6e5d71f2cf19818 Mon Sep 17 00:00:00 2001 From: jouzo Date: Mon, 9 Oct 2023 12:07:05 +0100 Subject: [PATCH 7/8] Use signed_tx.hash() consistently --- lib/ain-evm/src/evm.rs | 2 +- lib/ain-evm/src/executor.rs | 6 +++--- lib/ain-evm/src/receipt.rs | 2 +- lib/ain-grpc/src/rpc/eth.rs | 4 ++-- lib/ain-grpc/src/tests.rs | 2 +- lib/ain-grpc/src/transaction.rs | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) 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-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()), From 0f9b36fe15a2314008d9645d66aee7dfe754e71a Mon Sep 17 00:00:00 2001 From: jouzo Date: Mon, 9 Oct 2023 12:15:20 +0100 Subject: [PATCH 8/8] Revert lazy changes --- lib/ain-evm/src/transaction/mod.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/ain-evm/src/transaction/mod.rs b/lib/ain-evm/src/transaction/mod.rs index 2e679d275b..0977f236b0 100644 --- a/lib/ain-evm/src/transaction/mod.rs +++ b/lib/ain-evm/src/transaction/mod.rs @@ -100,7 +100,7 @@ impl From<&LegacyTransaction> for LegacyUnsignedTransaction { pub struct SignedTx { pub transaction: TransactionV2, pub sender: H160, - hash: H256, + hash_cache: Cell>, } impl fmt::Debug for SignedTx { @@ -178,9 +178,9 @@ impl TryFrom for SignedTx { } }?; Ok(SignedTx { - hash: src.hash(), transaction: src, sender: public_key_to_address(&pubkey), + hash_cache: Cell::new(None), }) } } @@ -319,7 +319,16 @@ impl SignedTx { } pub fn hash(&self) -> H256 { - self.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 { @@ -328,6 +337,7 @@ impl SignedTx { } use std::{ + cell::Cell, cmp::min, convert::{TryFrom, TryInto}, fmt,