From 0f03cb537acd5b7172fa1f2362b57320aef61cda Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Tue, 26 Sep 2023 16:24:51 +0800 Subject: [PATCH 1/5] Return effective gas price in gasPrice field --- lib/ain-grpc/src/rpc/eth.rs | 21 +++++++++++++++++++-- lib/ain-grpc/src/transaction.rs | 2 +- lib/proto/types/eth.proto | 2 +- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/ain-grpc/src/rpc/eth.rs b/lib/ain-grpc/src/rpc/eth.rs index 3f7422b1a4..88e57b0a2b 100644 --- a/lib/ain-grpc/src/rpc/eth.rs +++ b/lib/ain-grpc/src/rpc/eth.rs @@ -499,8 +499,11 @@ impl MetachainRPCServer for MetachainRPCModule { .get_transaction_by_hash(&hash) .map_err(to_jsonrpsee_custom_error)? .map_or(Ok(None), |tx| { - let mut transaction_info: EthTransactionInfo = - tx.try_into().map_err(to_jsonrpsee_custom_error)?; + let signed_tx: SignedTx = tx.try_into().map_err(to_jsonrpsee_custom_error)?; + let mut transaction_info: EthTransactionInfo = signed_tx + .clone() + .try_into() + .map_err(to_jsonrpsee_custom_error)?; // TODO: Improve efficiency by indexing the block_hash, block_number, and transaction_index fields. // Temporary workaround: Makes an additional call to get_receipt where these fields are available. @@ -514,6 +517,20 @@ impl MetachainRPCServer for MetachainRPCModule { transaction_info.block_number = Some(format_u256(receipt.block_number)); transaction_info.transaction_index = Some(format_u256(U256::from(receipt.tx_index))); + transaction_info.gas_price = Some(format_u256( + signed_tx.effective_gas_price( + self.handler + .storage + .get_block_by_hash(&receipt.block_hash) + .map_err(to_jsonrpsee_custom_error)? + .ok_or(to_jsonrpsee_custom_error(format!( + "Block with hash {} not found", + receipt.block_hash + )))? + .header + .base_fee, + ), + )) } Ok(Some(transaction_info)) diff --git a/lib/ain-grpc/src/transaction.rs b/lib/ain-grpc/src/transaction.rs index 3565cdde4e..8c8592965c 100644 --- a/lib/ain-grpc/src/transaction.rs +++ b/lib/ain-grpc/src/transaction.rs @@ -36,7 +36,7 @@ impl From for EthTransactionInfo { from: format_address(signed_tx.sender), to: signed_tx.to().map(format_address), gas: format_u256(signed_tx.gas_limit()), - gas_price: format_u256(signed_tx.gas_price()), + gas_price: None, value: format_u256(signed_tx.value()), input, nonce: format_u256(signed_tx.nonce()), diff --git a/lib/proto/types/eth.proto b/lib/proto/types/eth.proto index 328259366d..bb362a7099 100644 --- a/lib/proto/types/eth.proto +++ b/lib/proto/types/eth.proto @@ -10,7 +10,7 @@ message EthTransactionInfo { optional string blockNumber = 2; // The block number. null when its pending block. string from = 3; // The address from which the transaction is sent string gas = 4; // The integer of gas provided for the transaction execution. - string gasPrice = 5; // The integer of gas price used for each paid gas encoded as hexadecimal + optional string gasPrice = 5; // The integer of gas price used for each paid gas encoded as hexadecimal string hash = 6; // Hash of the transaction. string input = 7; // The data sent along with the transaction. string nonce = 8; // The integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce. From c3c8fc9778bdbe521112362aa894e0b645ad75c3 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Tue, 26 Sep 2023 16:33:19 +0800 Subject: [PATCH 2/5] Use effective gas price from receipt --- lib/ain-grpc/src/rpc/eth.rs | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/lib/ain-grpc/src/rpc/eth.rs b/lib/ain-grpc/src/rpc/eth.rs index 88e57b0a2b..b08f134de4 100644 --- a/lib/ain-grpc/src/rpc/eth.rs +++ b/lib/ain-grpc/src/rpc/eth.rs @@ -499,11 +499,8 @@ impl MetachainRPCServer for MetachainRPCModule { .get_transaction_by_hash(&hash) .map_err(to_jsonrpsee_custom_error)? .map_or(Ok(None), |tx| { - let signed_tx: SignedTx = tx.try_into().map_err(to_jsonrpsee_custom_error)?; - let mut transaction_info: EthTransactionInfo = signed_tx - .clone() - .try_into() - .map_err(to_jsonrpsee_custom_error)?; + let mut transaction_info: EthTransactionInfo = + tx.try_into().map_err(to_jsonrpsee_custom_error)?; // TODO: Improve efficiency by indexing the block_hash, block_number, and transaction_index fields. // Temporary workaround: Makes an additional call to get_receipt where these fields are available. @@ -517,20 +514,7 @@ impl MetachainRPCServer for MetachainRPCModule { transaction_info.block_number = Some(format_u256(receipt.block_number)); transaction_info.transaction_index = Some(format_u256(U256::from(receipt.tx_index))); - transaction_info.gas_price = Some(format_u256( - signed_tx.effective_gas_price( - self.handler - .storage - .get_block_by_hash(&receipt.block_hash) - .map_err(to_jsonrpsee_custom_error)? - .ok_or(to_jsonrpsee_custom_error(format!( - "Block with hash {} not found", - receipt.block_hash - )))? - .header - .base_fee, - ), - )) + transaction_info.gas_price = Some(format_u256(receipt.effective_gas_price)) } Ok(Some(transaction_info)) From ad935c4350cd52c7af558cd81cc9f01e6a7d25a3 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Tue, 26 Sep 2023 16:37:52 +0800 Subject: [PATCH 3/5] Fall back to max fee per gas for pending transactions --- lib/ain-grpc/src/rpc/eth.rs | 2 +- lib/ain-grpc/src/transaction.rs | 2 +- lib/proto/types/eth.proto | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ain-grpc/src/rpc/eth.rs b/lib/ain-grpc/src/rpc/eth.rs index b08f134de4..9139c70ece 100644 --- a/lib/ain-grpc/src/rpc/eth.rs +++ b/lib/ain-grpc/src/rpc/eth.rs @@ -514,7 +514,7 @@ impl MetachainRPCServer for MetachainRPCModule { transaction_info.block_number = Some(format_u256(receipt.block_number)); transaction_info.transaction_index = Some(format_u256(U256::from(receipt.tx_index))); - transaction_info.gas_price = Some(format_u256(receipt.effective_gas_price)) + transaction_info.gas_price = format_u256(receipt.effective_gas_price) } Ok(Some(transaction_info)) diff --git a/lib/ain-grpc/src/transaction.rs b/lib/ain-grpc/src/transaction.rs index 8c8592965c..3565cdde4e 100644 --- a/lib/ain-grpc/src/transaction.rs +++ b/lib/ain-grpc/src/transaction.rs @@ -36,7 +36,7 @@ impl From for EthTransactionInfo { from: format_address(signed_tx.sender), to: signed_tx.to().map(format_address), gas: format_u256(signed_tx.gas_limit()), - gas_price: None, + gas_price: format_u256(signed_tx.gas_price()), value: format_u256(signed_tx.value()), input, nonce: format_u256(signed_tx.nonce()), diff --git a/lib/proto/types/eth.proto b/lib/proto/types/eth.proto index bb362a7099..328259366d 100644 --- a/lib/proto/types/eth.proto +++ b/lib/proto/types/eth.proto @@ -10,7 +10,7 @@ message EthTransactionInfo { optional string blockNumber = 2; // The block number. null when its pending block. string from = 3; // The address from which the transaction is sent string gas = 4; // The integer of gas provided for the transaction execution. - optional string gasPrice = 5; // The integer of gas price used for each paid gas encoded as hexadecimal + string gasPrice = 5; // The integer of gas price used for each paid gas encoded as hexadecimal string hash = 6; // Hash of the transaction. string input = 7; // The data sent along with the transaction. string nonce = 8; // The integer of a nonce. This allows to overwrite your own pending transactions that use the same nonce. From ba17cf1d3ff50749e12bbb5288c868d0daae3867 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Tue, 26 Sep 2023 16:44:54 +0800 Subject: [PATCH 4/5] Add receipt info to getTransactionByBlock* RPCs, fix RPC input type --- lib/ain-grpc/src/rpc/eth.rs | 52 +++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/lib/ain-grpc/src/rpc/eth.rs b/lib/ain-grpc/src/rpc/eth.rs index 9139c70ece..1d9f24aa7e 100644 --- a/lib/ain-grpc/src/rpc/eth.rs +++ b/lib/ain-grpc/src/rpc/eth.rs @@ -129,7 +129,7 @@ pub trait MetachainRPC { fn get_transaction_by_block_hash_and_index( &self, hash: H256, - index: usize, + index: U256, ) -> RpcResult>; /// Retrieves a specific transaction, identified by the block number and transaction index. @@ -137,7 +137,7 @@ pub trait MetachainRPC { fn get_transaction_by_block_number_and_index( &self, block_number: U256, - index: usize, + index: U256, ) -> RpcResult>; /// Retrieves the list of pending transactions. @@ -535,14 +535,32 @@ impl MetachainRPCServer for MetachainRPCModule { fn get_transaction_by_block_hash_and_index( &self, hash: H256, - index: usize, + index: U256, ) -> RpcResult> { self.handler .storage - .get_transaction_by_block_hash_and_index(&hash, index) + .get_transaction_by_block_hash_and_index(&hash, index.as_usize()) .map_err(to_jsonrpsee_custom_error)? .map_or(Ok(None), |tx| { - let transaction_info = tx.try_into().map_err(to_jsonrpsee_custom_error)?; + let tx_hash = &tx.hash(); + let mut transaction_info: EthTransactionInfo = + tx.try_into().map_err(to_jsonrpsee_custom_error)?; + + // TODO: Improve efficiency by indexing the block_hash, block_number, and transaction_index fields. + // Temporary workaround: Makes an additional call to get_receipt where these fields are available. + if let Some(receipt) = self + .handler + .storage + .get_receipt(tx_hash) + .map_err(to_jsonrpsee_custom_error)? + { + transaction_info.block_hash = Some(format_h256(receipt.block_hash)); + transaction_info.block_number = Some(format_u256(receipt.block_number)); + transaction_info.transaction_index = + Some(format_u256(U256::from(receipt.tx_index))); + transaction_info.gas_price = format_u256(receipt.effective_gas_price) + } + Ok(Some(transaction_info)) }) } @@ -550,14 +568,32 @@ impl MetachainRPCServer for MetachainRPCModule { fn get_transaction_by_block_number_and_index( &self, number: U256, - index: usize, + index: U256, ) -> RpcResult> { self.handler .storage - .get_transaction_by_block_number_and_index(&number, index) + .get_transaction_by_block_number_and_index(&number, index.as_usize()) .map_err(to_jsonrpsee_custom_error)? .map_or(Ok(None), |tx| { - let transaction_info = tx.try_into().map_err(to_jsonrpsee_custom_error)?; + let tx_hash = &tx.hash(); + let mut transaction_info: EthTransactionInfo = + tx.try_into().map_err(to_jsonrpsee_custom_error)?; + + // TODO: Improve efficiency by indexing the block_hash, block_number, and transaction_index fields. + // Temporary workaround: Makes an additional call to get_receipt where these fields are available. + if let Some(receipt) = self + .handler + .storage + .get_receipt(tx_hash) + .map_err(to_jsonrpsee_custom_error)? + { + transaction_info.block_hash = Some(format_h256(receipt.block_hash)); + transaction_info.block_number = Some(format_u256(receipt.block_number)); + transaction_info.transaction_index = + Some(format_u256(U256::from(receipt.tx_index))); + transaction_info.gas_price = format_u256(receipt.effective_gas_price) + } + Ok(Some(transaction_info)) }) } From 97f298f4fdba4e6ac5bbcaf4f1d465f2ac8d08a6 Mon Sep 17 00:00:00 2001 From: Shoham Chakraborty Date: Tue, 26 Sep 2023 18:19:36 +0800 Subject: [PATCH 5/5] Populate gasPrice when constructing EthTransactionInfo from block --- lib/ain-grpc/src/transaction.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ain-grpc/src/transaction.rs b/lib/ain-grpc/src/transaction.rs index 3565cdde4e..97c043647d 100644 --- a/lib/ain-grpc/src/transaction.rs +++ b/lib/ain-grpc/src/transaction.rs @@ -106,6 +106,7 @@ impl EthTransactionInfo { block_hash: Some(format_h256(block.header.hash())), block_number: Some(format_u256(block.header.number)), transaction_index: Some(format_u256(U256::from(index))), + gas_price: format_u256(signed_tx.effective_gas_price(block.header.base_fee)), ..EthTransactionInfo::from(signed_tx) }) }