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

improve tests #917

Merged
merged 2 commits into from
May 24, 2024
Merged

improve tests #917

merged 2 commits into from
May 24, 2024

Conversation

gabriel-aranha-cw
Copy link
Contributor

@gabriel-aranha-cw gabriel-aranha-cw commented May 24, 2024

  1. Add receipt validation on multiple txs test.
  2. Create evm_mine helper rpc method.
  3. Improve BLOCK_MODE condition to allow 1s mode.

Copy link

github-actions bot commented May 24, 2024

PR Review 🔍

(Review updated until commit 35874e6)

⏱️ Estimated effort to review [1-5]

2, because the PR involves a moderate amount of changes across multiple files, primarily focused on enhancing testing capabilities and modifying mining intervals. The logic is straightforward, but verifying the correctness of blockchain-related operations requires specific domain knowledge.

🧪 Relevant tests

Yes

⚡ Possible issues

Possible Bug: The use of process.env.BLOCK_MODE === '1s' in hardhat.config.ts might not correctly handle the environment variable as expected if it is not strictly equal to '1s'. This could lead to unexpected mining behavior.

🔒 Security concerns

No

Code feedback:
relevant filee2e/hardhat.config.ts
suggestion      

Consider using a more robust method to handle environment variables. Using process.env.BLOCK_MODE.includes('1s') might be more flexible if the environment variable can contain additional characters or be part of a larger string. [medium]

relevant lineauto: process.env.BLOCK_MODE === 'automine' || process.env.BLOCK_MODE === '1s',

relevant filee2e/test/helpers/rpc.ts
suggestion      

Add error handling in the sendEvmMine function to manage potential failures when calling evm_mine. This could improve the robustness of test scripts by allowing them to handle errors gracefully. [important]

relevant lineconst result = await send("evm_mine", []);

Copy link

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Score
Error handling
Add error handling to the sendEvmMine function calls

Ensure that the sendEvmMine function is properly handling exceptions or errors that might
occur during the mining process.

e2e/test/automine/e2e-json-rpc.test.ts [108]

-await sendEvmMine();
+try {
+    await sendEvmMine();
+} catch (error) {
+    console.error('Error during mining:', error);
+}
 
Suggestion importance[1-10]: 9

Why: Adding error handling to the sendEvmMine function calls is important for robustness and reliability, especially in a testing environment. This suggestion addresses a potential issue that could lead to unhandled exceptions.

9
Maintainability
Extract transaction receipt validation into a separate function

Refactor the repeated transaction receipt validation code into a separate function to
improve code reuse and maintainability.

e2e/test/external/e2e-multiple-tx.test.ts [48-64]

 for (let txHash of txHashes) {
+    validateTransactionReceipt(txHash, latestBlockAfterMining);
+}
+
+async function validateTransactionReceipt(txHash, latestBlock) {
     const receipt = await send('eth_getTransactionReceipt', [txHash]);
     expect(receipt).to.exist;
     expect(receipt.transactionHash).to.equal(txHash);
     ...
 }
 
Suggestion importance[1-10]: 8

Why: Extracting the transaction receipt validation into a separate function enhances code readability and maintainability by reducing redundancy. This is a good practice for cleaner and more modular code.

8
Replace hardcoded string literals with constants or enums for BLOCK_MODE

Consider using a constant or enum for BLOCK_MODE values to avoid hardcoding strings
multiple times, which can lead to errors if the strings are mistyped or changed in the
future.

e2e/hardhat.config.ts [24-25]

-auto: process.env.BLOCK_MODE === 'automine' || process.env.BLOCK_MODE === '1s',
-interval: process.env.BLOCK_MODE === 'automine' ? undefined : (process.env.BLOCK_MODE === '1s' ? 1000 : 0)
+auto: process.env.BLOCK_MODE === BLOCK_MODES.AUTOMINE || process.env.BLOCK_MODE === BLOCK_MODES.INTERVAL,
+interval: process.env.BLOCK_MODE === BLOCK_MODES.AUTOMINE ? undefined : (process.env.BLOCK_MODE === BLOCK_MODES.INTERVAL ? 1000 : 0)
 
Suggestion importance[1-10]: 7

Why: Using constants or enums for BLOCK_MODE values improves maintainability and reduces the risk of errors due to mistyped strings. However, this suggestion is not critical and does not address any major bugs or security issues.

7
Best practice
Add type annotations to the sendEvmMine function

Add type annotations to the sendEvmMine function to improve code clarity and type safety.

e2e/test/helpers/rpc.ts [210-212]

-export async function sendEvmMine(): Promise<any> {
+export async function sendEvmMine(): Promise<MineResult> {
     const result = await send("evm_mine", []);
     return result;
 }
 
Suggestion importance[1-10]: 6

Why: Adding type annotations improves code clarity and type safety, which is beneficial for maintainability and reducing potential type-related errors. However, this suggestion is more of a best practice and does not address any critical issues.

6

@gabriel-aranha-cw gabriel-aranha-cw marked this pull request as ready for review May 24, 2024 14:04
@gabriel-aranha-cw gabriel-aranha-cw requested a review from a team as a code owner May 24, 2024 14:04
@gabriel-aranha-cw gabriel-aranha-cw enabled auto-merge (squash) May 24, 2024 14:04
@gabriel-aranha-cw gabriel-aranha-cw merged commit 7ba6862 into main May 24, 2024
30 checks passed
@gabriel-aranha-cw gabriel-aranha-cw deleted the enha-e2e-tests branch May 24, 2024 14:05
Copy link

Persistent review updated to latest commit 35874e6

Copy link

PR Code Suggestions ✨

CategorySuggestion                                                                                                                                    Score
Error handling
Add error handling to the sendEvmMine function to improve robustness

Add error handling for the sendEvmMine function to manage potential failures in the send
function call. This can be achieved by using a try-catch block to catch and handle errors
appropriately.

e2e/test/helpers/rpc.ts [210-212]

 export async function sendEvmMine(): Promise<any> {
-    const result = await send("evm_mine", []);
-    return result;
+    try {
+        const result = await send("evm_mine", []);
+        return result;
+    } catch (error) {
+        console.error("Failed to mine a block:", error);
+        throw error;
+    }
 }
 
Suggestion importance[1-10]: 9

Why: Adding error handling significantly improves the robustness of the function by managing potential failures. This is a crucial improvement for reliability.

9
Maintainability
Refactor repeated transaction receipt checks into a separate function to improve code organization

Refactor the repeated transaction receipt validation logic into a separate function to
reduce code duplication and improve test readability.

e2e/test/external/e2e-multiple-tx.test.ts [48-64]

 for (let txHash of txHashes) {
+    validateTransactionReceipt(txHash, latestBlockAfterMining);
+}
+
+async function validateTransactionReceipt(txHash: string, block: any) {
     const receipt = await send('eth_getTransactionReceipt', [txHash]);
     expect(receipt).to.exist;
     expect(receipt.transactionHash).to.equal(txHash);
     ...
 }
 
Suggestion importance[1-10]: 8

Why: Refactoring repeated code into a separate function reduces duplication and enhances readability and maintainability. This is a good practice for cleaner code.

8
Replace magic numbers with named constants for clarity and maintainability

Consider using a constant for the interval values to avoid magic numbers and improve
maintainability. Define constants for the interval values like AUTOMINE_INTERVAL and
ONE_SECOND_INTERVAL at the top of your file or in a configuration file.

e2e/hardhat.config.ts [24-25]

 auto: process.env.BLOCK_MODE === 'automine' || process.env.BLOCK_MODE === '1s',
-interval: process.env.BLOCK_MODE === 'automine' ? undefined : (process.env.BLOCK_MODE === '1s' ? 1000 : 0)
+interval: process.env.BLOCK_MODE === 'automine' ? undefined : (process.env.BLOCK_MODE === '1s' ? AUTOMINE_INTERVAL : ONE_SECOND_INTERVAL)
 
Suggestion importance[1-10]: 7

Why: Using named constants instead of magic numbers improves code readability and maintainability. This suggestion is correct and beneficial, though it addresses a minor issue.

7
Readability
Improve variable naming for better code readability

Use a more descriptive variable name than prev_number to enhance code readability.
Consider renaming it to previousBlockNumber.

e2e/test/automine/e2e-json-rpc.test.ts [107-109]

-let prev_number = (await latest()).block_number;
+let previousBlockNumber = (await latest()).block_number;
 await sendEvmMine();
-expect((await latest()).block_number).eq(prev_number + 1);
+expect((await latest()).block_number).eq(previousBlockNumber + 1);
 
Suggestion importance[1-10]: 6

Why: Using more descriptive variable names enhances code readability. This suggestion is correct but addresses a minor readability issue.

6

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.

1 participant