Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve performance by not decoding sender when dealing with imported external transactions #763

Merged
merged 3 commits into from
May 3, 2024

Conversation

dinhani-cw
Copy link
Contributor

@dinhani-cw dinhani-cw commented May 3, 2024

No description provided.

@dinhani-cw dinhani-cw requested a review from a team as a code owner May 3, 2024 04:28
@dinhani-cw dinhani-cw changed the title feat: improve performance by not decoding sender when dealing with im… feat: improve performance by not decoding sender when dealing with imported external transactions May 3, 2024
Copy link

github-actions bot commented May 3, 2024

PR Review

⏱️ Estimated effort to review [1-5]

2, because the changes are localized to specific files with a clear focus on improving performance by optimizing transaction handling. The modifications are not extensive and involve mostly refactoring and reorganization of existing logic.

🧪 Relevant tests

No

🔍 Possible issues

Possible Bug: The removal of decoding for the sender in try_from_ethers_transaction when compute_signer is false might lead to unexpected behavior if the sender's address is required downstream in the system. This change assumes that the sender's address is not needed, which should be verified.

🔒 Security concerns

No

Code feedback:
relevant filesrc/eth/primitives/transaction_input.rs
suggestion      

Consider adding error handling for the case where gas_price is None in the try_from_ethers_transaction function. Currently, it logs an error and returns, but you might want to handle this more gracefully or ensure that transactions without a gas price are filtered out earlier in the process. [important]

relevant lineNone => return log_and_err!("transaction without gas_price id is not allowed"),

relevant filesrc/eth/primitives/transaction_input.rs
suggestion      

Refactor the repeated logic for converting chain_id, nonce, gas_limit, and other fields into a separate function or macro to reduce code duplication and improve maintainability. This can help centralize changes to the conversion logic in the future. [medium]

relevant linechain_id: match value.chain_id {

relevant filesrc/eth/primitives/transaction_input.rs
suggestion      

Ensure that the from field conversion using Address::new(value.from.into()) is necessary and correctly handles all expected input cases, especially since from is directly set without additional checks when compute_signer is false. [medium]

relevant linefalse => value.from.into(),


✨ Review tool usage guide:

Overview:
The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

  • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
/review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
[pr_reviewer]
some_config1=...
some_config2=...

See the review usage page for a comprehensive guide on using this tool.

Copy link

github-actions bot commented May 3, 2024

PR Code Suggestions

CategorySuggestions                                                                                                                                                       
Bug
Add error handling for missing from field in transactions.

Consider handling the case where value.from might be None in the
try_from_ethers_transaction function. This will prevent potential runtime errors when
value.from is not provided.

src/eth/primitives/transaction_input.rs [120]

-false => value.from.into(),
+false => value.from.ok_or(anyhow!("`from` field is missing in the transaction"))?.into(),
 
Replace undefined macro with standard error handling.

Replace the log_and_err! macro with a proper error handling mechanism, as the macro is not
defined in the provided code snippets. This will ensure that the code compiles and
provides meaningful error messages.

src/eth/primitives/transaction_input.rs [126]

-None => return log_and_err!("transaction without gas_price id is not allowed"),
+None => return Err(anyhow!("Transaction without gas_price is not allowed")),
 
Maintainability
Refactor repeated conversion logic into separate functions.

Refactor the repeated logic for converting chain_id, nonce, gas, and other fields into a
separate function to improve code maintainability and reduce redundancy.

src/eth/primitives/transaction_input.rs [130-146]

-chain_id: match value.chain_id {
-    Some(chain_id) => Some(chain_id.try_into()?),
-    None => None,
-},
-hash: value.hash.into(),
-nonce: value.nonce.try_into()?,
-signer,
-from: Address::new(value.from.into()),
-to: value.to.map_into(),
-value: value.value.into(),
-input: value.input.clone().into(),
-gas_limit: value.gas.try_into()?,
-gas_price,
-v: value.v,
-r: value.r,
-s: value.s,
+TransactionInput {
+    chain_id: convert_chain_id(value.chain_id)?,
+    hash: value.hash.into(),
+    nonce: convert_nonce(value.nonce)?,
+    signer,
+    from: convert_from(value.from)?,
+    to: value.to.map_into(),
+    value: value.value.into(),
+    input: value.input.clone().into(),
+    gas_limit: convert_gas(value.gas)?,
+    gas_price: convert_gas_price(value.gas_price)?,
+    v: value.v,
+    r: value.r,
+    s: value.s,
+}
 
Enhancement
Ensure robustness by adding unit tests for the transaction conversion function.

Add unit tests for the new try_from_ethers_transaction function to ensure that it handles
various edge cases correctly, such as missing fields or invalid data.

src/eth/primitives/transaction_input.rs [110]

-fn try_from_ethers_transaction(value: EthersTransaction, compute_signer: bool) -> anyhow::Result<TransactionInput> {
+#[cfg(test)]
+mod tests {
+    use super::*;
 
+    #[test]
+    fn test_try_from_ethers_transaction_missing_from() {
+        let transaction = EthersTransaction { from: None, ..Default::default() };
+        assert!(try_from_ethers_transaction(transaction, false).is_err());
+    }
+
+    #[test]
+    fn test_try_from_ethers_transaction_valid_data() {
+        let transaction = EthersTransaction { from: Some("0x123".into()), ..Default::default() };
+        assert!(try_from_ethers_transaction(transaction, true).is_ok());
+    }
+}
+
Best practice
Use more descriptive variable names for clarity.

Consider using more descriptive variable names in the try_from_ethers_transaction function
to enhance code readability and maintainability.

src/eth/primitives/transaction_input.rs [112]

-let signer: Address = match compute_signer {
+let transaction_signer: Address = match compute_signer {
 

✨ Improve tool usage guide:

Overview:
The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

  • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
/improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
[pr_code_suggestions]
some_config1=...
some_config2=...

See the improve usage page for a comprehensive guide on using this tool.

@dinhani-cw dinhani-cw enabled auto-merge (squash) May 3, 2024 04:31
@dinhani-cw dinhani-cw disabled auto-merge May 3, 2024 04:32
@dinhani-cw dinhani-cw enabled auto-merge (squash) May 3, 2024 04:33
@dinhani-cw dinhani-cw merged commit 1b3dff6 into main May 3, 2024
25 checks passed
@dinhani-cw dinhani-cw deleted the performance-magic branch May 3, 2024 04:38
Copy link
Contributor

@renancloudwalk renancloudwalk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants