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: add concurrency tests #109

Merged
merged 7 commits into from
Nov 19, 2024
Merged

feat: add concurrency tests #109

merged 7 commits into from
Nov 19, 2024

Conversation

beer-1
Copy link
Collaborator

@beer-1 beer-1 commented Nov 13, 2024

Description

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Introduced a new function to the Counter contract allowing multiple increments in a single transaction.
    • Added a fuzz testing function to evaluate concurrent behavior of the smart contract counter.
  • Bug Fixes

    • Updated assertions in test cases to ensure correct logging behavior and validate contract call outcomes.
  • Documentation

    • Updated ABI and binary representations of the Counter contract to reflect new functionalities.
  • Chores

    • Added testdata to .gitignore to prevent tracking of test-related files.
    • Updated coverage settings in .codecov.yml for more specific ignored paths.
    • Added a new target in the Makefile for fuzz testing the EVM keeper package.

@beer-1 beer-1 requested a review from a team as a code owner November 13, 2024 07:25
Copy link

coderabbitai bot commented Nov 13, 2024

Caution

Review failed

The pull request is closed.

Walkthrough

The pull request introduces several changes across multiple files. The .gitignore file has been updated to include the testdata directory, ensuring it is ignored by Git. A new target named fuzz has been added to the Makefile to facilitate fuzz testing for the ./x/evm/keeper package. Additionally, the Counter contract has been enhanced with a new function, increase_for_fuzz, allowing multiple increments in a single transaction. Corresponding updates were made to the Go code and test files to support these changes, including new fuzz tests and modifications to existing test cases.

Changes

File Change Summary
.gitignore Added entry testdata to ignore this directory in Git.
Makefile Introduced new target fuzz to run fuzz tests on ./x/evm/keeper package with a specific command.
x/evm/contracts/counter/Counter.go Added method IncreaseForFuzz for the Counter contract to handle paid mutator transactions.
x/evm/contracts/counter/Counter.sol Added public function increase_for_fuzz(uint64 num) and updated increase() to be public.
x/evm/keeper/context_test.go Updated assertions in Test_Call and expanded Test_ExecuteCosmosWithOptions with additional scenarios.
x/evm/keeper/fuzz_test.go Introduced Fuzz_Concurrent_Counter function for concurrent testing of the Counter contract.
x/evm/keeper/precompiles.go Modified precompileAddrs method to clone the slice of active precompiles before appending addresses.
.codecov.yml Updated ignored paths for coverage settings, specifying x/evm/contracts and excluding **/*.sol.

🐰 In the meadow, hops a little hare,
With changes in code, we dance without care.
Ignoring testdata, we keep things neat,
Fuzzing our tests, oh, what a treat!
The Counter now leaps, incrementing fast,
In joyful transactions, our code will last!
🌼


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 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.

Copy link

@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: 5

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3264f25 and 8c4d4a5.

📒 Files selected for processing (6)
  • .gitignore (1 hunks)
  • Makefile (1 hunks)
  • x/evm/contracts/counter/Counter.go (2 hunks)
  • x/evm/contracts/counter/Counter.sol (1 hunks)
  • x/evm/keeper/context_test.go (1 hunks)
  • x/evm/keeper/fuzz_test.go (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .gitignore
🔇 Additional comments (5)
x/evm/contracts/counter/Counter.sol (1)

23-23: LGTM! Visibility change is appropriate

Changing increase() from external to public is necessary to support internal calls from increase_for_fuzz().

x/evm/keeper/context_test.go (1)

121-121: More precise assertion for event logs

The assertion has been strengthened to expect exactly 2 logs instead of just checking for non-empty logs. This is a good improvement in test precision.

Let's verify the expected number of logs by checking the contract implementation:

x/evm/contracts/counter/Counter.go (3)

34-35: LGTM: ABI and binary updates for new fuzz testing method.

The ABI and binary updates correctly reflect the addition of the new increase_for_fuzz method.


393-413: LGTM: New IncreaseForFuzz method implementations.

The implementations follow the standard Go binding patterns and are properly documented.


393-413: Verify concurrent safety of the new method.

Since this PR focuses on concurrency tests, please ensure:

  1. The method is thread-safe when called concurrently
  2. Race conditions are properly handled
  3. Test coverage includes concurrent access scenarios
✅ Verification successful

Let me gather more specific information about the concurrent testing implementation for this contract.


Let me try one more search to find the contract implementation and its tests.


The method is properly tested for concurrent safety

The fuzz test implementation shows proper concurrent safety measures:

  • Uses sync.WaitGroup for coordinating concurrent operations
  • Employs atomic.Pointer for thread-safe bloom filter updates
  • Each goroutine operates on its own cached context (CacheContext())
  • Multiple threads execute the increase_for_fuzz method simultaneously
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for race condition tests and concurrent access patterns

# Search for race condition tests
echo "Searching for race condition tests..."
rg -l "race|concurrent|sync|atomic" --type go

# Search for sync/atomic usage in implementation
echo "Checking for synchronization mechanisms..."
rg -l "sync\.|atomic\." --type go

# Look for test files with parallel test execution
echo "Checking for parallel test execution..."
rg -l "t.Parallel|go test.*-race" --type go

Length of output: 1907


Script:

#!/bin/bash
# Check the fuzz test implementation and concurrent testing patterns
echo "Checking fuzz test implementation..."
rg -A 10 "IncreaseForFuzz" x/evm/keeper/fuzz_test.go

# Look for mutex/atomic operations in the contract implementation
echo "Checking contract state handling..."
rg -A 10 "Counter" x/evm/state/statedb.go

# Check the keeper implementation for synchronization
echo "Examining keeper implementation..."
rg -A 10 "Counter" x/evm/keeper/keeper.go

Length of output: 379


Script:

#!/bin/bash
# Find the contract implementation and tests
echo "Looking for Counter contract files..."
fd -t f Counter

# Check the content of any test files
echo "Checking test implementations..."
rg -C 10 "func.*Test.*Counter" --type go

# Look for concurrent test patterns in the test files
echo "Looking for concurrent test patterns..."
rg -C 10 "go func|sync\." x/evm/keeper/fuzz_test.go

Length of output: 1300

x/evm/contracts/counter/Counter.sol Show resolved Hide resolved
Makefile Show resolved Hide resolved
x/evm/keeper/fuzz_test.go Show resolved Hide resolved
x/evm/keeper/fuzz_test.go Show resolved Hide resolved
x/evm/keeper/fuzz_test.go Outdated Show resolved Hide resolved
Copy link

codecov bot commented Nov 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 40.55%. Comparing base (aef4430) to head (84fd0fb).
Report is 2 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##             main     #109       +/-   ##
===========================================
+ Coverage   28.16%   40.55%   +12.39%     
===========================================
  Files         126      106       -20     
  Lines       14285     9919     -4366     
===========================================
  Hits         4023     4023               
+ Misses       9686     5320     -4366     
  Partials      576      576               
Files with missing lines Coverage Δ
x/evm/keeper/precompiles.go 77.77% <100.00%> (ø)

... and 20 files with indirect coverage changes

---- 🚨 Try these New Features:

Copy link

@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

🧹 Outside diff range and nitpick comments (1)
x/evm/keeper/precompiles.go (1)

47-47: Excellent improvement for concurrent safety

The use of slices.Clone is a crucial improvement that prevents potential race conditions in concurrent scenarios by ensuring each caller gets their own copy of the precompile addresses instead of potentially modifying a shared slice.

This change aligns well with the PR's objective of improving concurrent testing support. The defensive copying pattern used here is a best practice for concurrent systems.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 112e836 and 225a49e.

📒 Files selected for processing (1)
  • x/evm/keeper/precompiles.go (2 hunks)
🔇 Additional comments (2)
x/evm/keeper/precompiles.go (2)

4-5: LGTM: Good use of standard library

The addition of the slices package is appropriate for safe slice operations.


47-47: Verify concurrent usage patterns

Let's ensure this concurrent-safe pattern is consistently applied across related code.

✅ Verification successful

Let me gather more specific information about the concurrent usage patterns.


Let me check one final aspect to understand the precompiles method's usage context.


The concurrent-safe pattern is correctly implemented and no issues found

The use of slices.Clone() with vm.ActivePrecompiles() is appropriate here as:

  1. The precompileAddrs method is used in EVM state preparation during calls/creates
  2. The method creates a new slice copy before appending, preventing any concurrent modification issues
  3. The only concurrent usage is in fuzz tests, which properly handles concurrency through goroutines and WaitGroups
  4. The method is consistently used across all EVM operations (StaticCall, Call, Create)
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for potential concurrent access patterns to precompileAddrs
# and similar slice operations that might need the same treatment

# Look for test files that might use this method concurrently
rg -l "precompileAddrs.*go.*func" 

# Look for similar patterns that might need concurrent safety
ast-grep --pattern 'append($_, vm.ActivePrecompiles($_))'

# Look for concurrent test patterns
rg -l "parallel|go func|sync\." --type go

Length of output: 603


Script:

#!/bin/bash
# Check the specific method implementation and its usage
ast-grep --pattern 'func $_($_, $_, $_) []common.Address {
  $$$
  append($_, vm.ActivePrecompiles($_))
  $$$
}'

# Look for test files specifically using this precompiles-related code
rg -l "TestPrecompile.*parallel" --type go

# Check for any concurrent test setup in the keeper tests
rg "t.Parallel\(\)" x/evm/keeper/

# Look for any sync.* usage near precompiles
rg -B 2 -A 2 "sync\." x/evm/keeper/

Length of output: 507


Script:

#!/bin/bash
# Check the actual implementation and usage of precompileAddrs
rg -A 5 "precompileAddrs" x/evm/keeper/

# Look for any other slice operations with vm.ActivePrecompiles
rg "vm\.ActivePrecompiles" x/evm/keeper/

# Check the fuzz test implementation for concurrent access patterns
cat x/evm/keeper/fuzz_test.go

Length of output: 4434

Copy link

@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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 225a49e and c58d5e5.

📒 Files selected for processing (1)
  • .codecov.yml (1 hunks)
🔇 Additional comments (1)
.codecov.yml (1)

38-39: LGTM! Appropriate ignore patterns for contract files.

The new ignore patterns correctly exclude Solidity contract files from coverage calculations, which is appropriate since we measure coverage of the test code rather than the contract implementations.

.codecov.yml Show resolved Hide resolved
@beer-1 beer-1 merged commit b441435 into main Nov 19, 2024
6 of 7 checks passed
@beer-1 beer-1 deleted the feat/concurrency-tests branch November 19, 2024 05:12
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