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

test: improve tx injector and common test suite #61

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

tzdybal
Copy link
Member

@tzdybal tzdybal commented Feb 19, 2025

Overview

This PR enables test suites to provide actual transactions, because we cannot just use random ones (they would most probably be invalid in actual execution environments).

Resolves #33.

Summary by CodeRabbit

  • New Features

    • Enabled dynamic generation of transactions for a more robust simulation.
  • Refactor

    • Streamlined internal state verification and chain initialization processes to enhance clarity.
    • Improved the organization of test cases and transaction injection methods in the test suite.
  • Tests

    • Updated test scenarios with a table-driven approach to validate diverse transaction execution cases and finalization flows.

Copy link
Contributor

coderabbitai bot commented Feb 19, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes simplify and clarify various test files. In the gRPC client–server test, an intermediate state variable is removed in favor of using the expected state root directly. The dummy executor now injects transactions using a new random generation method, replacing the static injection approach. The test suite has been refactored to adopt the new transaction injection method and includes an added helper for chain initialization. These modifications standardize the transaction creation process and improve test clarity.

Changes

File(s) Change Summary
proxy/.../client_server_test.go Removed the intermediate variable stateRootHash and updated the InitChain return and assertions to use expectedStateRoot directly, thereby streamlining the test.
test/.../dummy.go, test/.../dummy_test.go Removed InjectTx and added InjectRandomTx along with a new mustGetRandomBytes function for random transaction generation. Tests now use the random transaction approach instead of static byte slices.
test/suite.go Updated the TxInjector interface to use InjectRandomTx, added an initChain helper method, and refactored tests (TestGetTxs, TestExecuteTxs, TestSetFinal, TestMultipleBlocks) for improved chain initialization and execution.

Possibly related PRs

Suggested labels

testing

Suggested reviewers

  • yarikbratashchuk
  • gupadhyaya
  • Manav-Aggarwal
  • tuxcanfly

Poem

In my burrow of code, I happily hop,
Removing old clutter with a joyful bop.
Random bytes now dance, swift and free,
In tests where clarity is the key.
With carrot-fueled cheer, I celebrate this day—
A bunny’s delight in every new way!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@tzdybal tzdybal marked this pull request as ready for review February 20, 2025 15:51
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
proxy/grpc/util.go (1)

5-9: Add input validation and documentation to improve robustness.

The function could benefit from input validation and documentation to improve its robustness and maintainability.

Apply this diff to add input validation and documentation:

+// copyHash creates a new Hash by copying the contents of the input byte slice.
+// It panics if the input slice is not of the expected length for a Hash.
 func copyHash(src []byte) types.Hash {
+	if len(src) != 32 {
+		panic("invalid hash length")
+	}
-	dst := make([]byte, len(src))
+	dst := make([]byte, 32)
 	copy(dst, src)
 	return dst
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d48c9dc and 07faf8e.

📒 Files selected for processing (7)
  • proxy/grpc/client.go (2 hunks)
  • proxy/grpc/client_server_test.go (2 hunks)
  • proxy/grpc/server.go (1 hunks)
  • proxy/grpc/util.go (1 hunks)
  • test/dummy.go (2 hunks)
  • test/dummy_test.go (1 hunks)
  • test/suite.go (5 hunks)
🧰 Additional context used
🪛 golangci-lint (1.62.2)
test/suite.go

105-105: ineffectual assignment to maxBytes

(ineffassign)

🔇 Additional comments (10)
test/dummy_test.go (1)

31-32: LGTM! The changes align with PR objectives.

The switch from static to random transactions improves the test suite by making it more realistic while maintaining the same test logic.

proxy/grpc/client_server_test.go (2)

71-73: LGTM! Good addition of timeout context.

The addition of a timeout context improves test reliability by preventing potential hangs.


59-60: LGTM! Clean state root handling.

The changes to use types.Hash directly and compare it in assertions make the code more type-safe and cleaner.

Also applies to: 76-76

proxy/grpc/server.go (1)

86-86: LGTM! Consistent use of copyHash.

The change to use copyHash aligns with the new utility function and maintains consistency across the codebase.

test/dummy.go (2)

48-53: LGTM! Good practice to return the generated transaction.

The implementation allows test cases to verify the exact transaction that was injected, improving test reliability.


55-62: LGTM! Secure random number generation with proper error handling.

Using crypto/rand instead of math/rand is a good security practice, and panicking in test code is acceptable.

proxy/grpc/client.go (1)

64-64: LGTM! Consistent use of copyHash helper improves code maintainability.

The refactoring eliminates duplicate code and reduces the chance of copy-related bugs.

Also applies to: 101-101

test/suite.go (3)

20-20: LGTM! Good practice to define timeout constant.

The 3-second timeout is reasonable for tests and helps prevent hanging tests.


24-24: LGTM! Interface change improves test verification capabilities.

Returning the generated transaction allows test cases to verify the exact transaction that was injected.


121-129: LGTM! Helper function reduces code duplication.

The initChain helper improves code maintainability by centralizing chain initialization logic.

# Conflicts:
#	test/dummy.go
Previously, hashes was not copied properly because of nil destination for copy function.

Replaced manual slice copying with a dedicated `copyHash` function for cleaner and reusable code. Updated related assertions and method calls to align with the new approach, improving code readability and maintainability.
Introduced a context with a timeout to ensure all tests in `ExecutorSuite` and the gRPC client-server tests properly respect execution time limits. Standardized the use of a `maxTestDuration` constant to improve maintainability and readability across the test suite.
@tzdybal tzdybal force-pushed the tzdybal/tx_injector branch from 07faf8e to 4c257e1 Compare February 20, 2025 15:55
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

♻️ Duplicate comments (1)
test/suite.go (1)

105-105: ⚠️ Potential issue

Fix ineffectual assignment to maxBytes.

The maxBytes variable is assigned but never used in the TestMultipleBlocks function.

Apply this diff to fix the issue:

-	genesisTime, stateRoot, maxBytes := s.initChain(ctx, initialHeight)
+	genesisTime, stateRoot, _ := s.initChain(ctx, initialHeight)
🧰 Tools
🪛 GitHub Check: lint / golangci-lint

[failure] 105-105:
ineffectual assignment to maxBytes (ineffassign)

🪛 golangci-lint (1.62.2)

105-105: ineffectual assignment to maxBytes

(ineffassign)

🪛 GitHub Actions: CI and Release

[error] 105-105: ineffectual assignment to maxBytes (ineffassign)

🧹 Nitpick comments (1)
test/dummy.go (1)

67-74: Consider returning error instead of panicking.

While panicking in test code is generally acceptable, returning an error would provide better error handling and flexibility.

-func mustGetRandomBytes(n int) []byte {
+func getRandomBytes(n int) ([]byte, error) {
 	b := make([]byte, n)
 	_, err := rand.Read(b)
 	if err != nil {
-		panic(fmt.Errorf("failed to generate random bytes: %w", err))
+		return nil, fmt.Errorf("failed to generate random bytes: %w", err)
 	}
-	return b
+	return b, nil
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 07faf8e and 4c257e1.

📒 Files selected for processing (7)
  • proxy/grpc/client.go (2 hunks)
  • proxy/grpc/client_server_test.go (2 hunks)
  • proxy/grpc/server.go (1 hunks)
  • proxy/grpc/util.go (1 hunks)
  • test/dummy.go (2 hunks)
  • test/dummy_test.go (1 hunks)
  • test/suite.go (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (4)
  • proxy/grpc/util.go
  • test/dummy_test.go
  • proxy/grpc/client.go
  • proxy/grpc/server.go
🧰 Additional context used
🪛 GitHub Check: lint / golangci-lint
test/suite.go

[failure] 105-105:
ineffectual assignment to maxBytes (ineffassign)

🪛 golangci-lint (1.62.2)
test/suite.go

105-105: ineffectual assignment to maxBytes

(ineffassign)

🪛 GitHub Actions: CI and Release
test/suite.go

[error] 105-105: ineffectual assignment to maxBytes (ineffassign)

🔇 Additional comments (5)
proxy/grpc/client_server_test.go (1)

59-77: Great improvements to type safety and context handling!

The changes enhance the code quality by:

  1. Using types.Hash instead of raw byte slices for state root
  2. Adding proper timeout context with cancellation
  3. Simplifying state root comparison
test/dummy.go (1)

57-65: Implementation contradicts PR objectives.

The PR objectives state the goal is to "allow test suites to provide actual transactions instead of relying on random transactions". However, this change removes InjectTx in favor of InjectRandomTx, which seems to do the opposite by enforcing random transactions instead of allowing actual transactions to be provided.

Please clarify if this is intentional or if we should maintain both capabilities:

  1. InjectTx(tx types.Tx) - for providing actual transactions
  2. InjectRandomTx() types.Tx - for generating random transactions

Likely an incorrect or invalid review comment.

test/suite.go (3)

20-20: Good addition of timeout constant.

Adding maxTestDuration improves consistency in timeout handling across tests.


22-25: TxInjector interface change contradicts PR objectives.

Similar to the changes in dummy.go, removing InjectTx in favor of InjectRandomTx contradicts the PR objective of allowing test suites to provide actual transactions.

Likely an incorrect or invalid review comment.


33-35: Great improvement in context handling!

The consistent use of timeout contexts with proper cancellation across all test methods is a significant improvement.

Also applies to: 46-48, 71-73, 85-87, 102-103

tac0turtle
tac0turtle previously approved these changes Feb 20, 2025
Replaced the reuse of "stateRoot" with "prevStateRoot" for clarity and updated state transition logic. Added assertion to ensure state roots differ between iterations, improving test robustness.
@tzdybal tzdybal changed the title feat: improve tx injector test: improve tx injector for common test suite Feb 21, 2025
@tzdybal tzdybal changed the title test: improve tx injector for common test suite test: improve tx injector and common test suite Feb 21, 2025
@tzdybal tzdybal enabled auto-merge February 21, 2025 10:35
@tzdybal tzdybal marked this pull request as draft February 21, 2025 10:51
auto-merge was automatically disabled February 21, 2025 10:51

Pull request was converted to draft

Add a `stateRootChanged` flag to test cases to explicitly verify state root changes. Update logic in `Exec.ExecuteTxs` to handle state root equality checks properly and return previous state root for empty transactions in `dummy.go`.
Refactored `TxInjector` to replace `InjectRandomTx` with `GetRandomTxs` and `InjectTxs` for better flexibility and clarity. Updated related test cases to reflect the new interface, ensuring consistency and enhanced test coverage.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

Use contexts with timeouts in tests
2 participants