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

Add transaction rate-limit functionality #750

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

m-Peter
Copy link
Collaborator

@m-Peter m-Peter commented Feb 10, 2025

Closes: #746

Description

The default TX submission rate-limit is set for 1 request / 3s..
The 3s magic value here is the block production time on testnet.
Both of these are configurable with the flags below:

  • tx-request-limit (default is 0, meaning no rate-limit applies)
  • tx-request-duration-limit (default is 3s, for 3 seconds)

For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

Summary by CodeRabbit

  • New Features
    • Improved error handling to gracefully signal rate limit issues.
    • Introduced transaction submission rate limiting, adding controls for the number of allowed submissions and duration limits.
    • Enabled new command-line options for customized transaction rate parameters.
  • Bug Fixes
    • Enhanced transaction handling logic by implementing a rate limiting mechanism to prevent excessive submissions from the same address.

Copy link
Contributor

coderabbitai bot commented Feb 10, 2025

Walkthrough

This pull request extends error handling and transaction configuration by introducing an address-based rate limiting mechanism, especially for the testnet. A new case is added in the error handler to manage rate limit errors. Additionally, new configuration options and command-line flags are provided to control transaction submission frequency and duration. The changes also integrate a rate limiter into the transaction processing flow in the service layer, ensuring that excessive transaction submissions from the same address are curtailed.

Changes

File(s) Change Summary
api/utils.go Added a new case in handleError to check for errs.ErrRateLimit and return a zero value along with the error.
cmd/run/cmd.go, config/config.go Introduced new configuration options: added fields TxRequestLimit and TxRequestDurationLimit in the Config struct; added command-line flags --tx-request-limit and --tx-request-duration-limit.
services/requester/requester.go Added a new limiter field to the EVM struct and modified SendRawTransaction to enforce rate limiting on testnet by checking the limiter before processing transactions.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant EVM
    participant Limiter
    Client->>EVM: SendRawTransaction(tx)
    alt If testnet is used
        EVM->>Limiter: Request token (using sender's address)
        alt Token acquired
            Limiter-->>EVM: Token granted
            EVM->>EVM: Process transaction
            EVM-->>Client: Transaction processed
        else Token unavailable
            Limiter-->>EVM: No token available
            EVM-->>Client: Return ErrRateLimit error
        end
    else Non-testnet
        EVM->>EVM: Process transaction normally
        EVM-->>Client: Transaction processed
    end
Loading

Possibly related PRs

Suggested labels

Bugfix

Suggested reviewers

  • peterargue
  • devbugging
  • zhangchiqing

Poem

I'm a happy little rabbit, hopping through the code,
With rate limits in my garden, lightening every load.
I twirl through configs, skip errors with a bound,
In every flag and token, sweet logic is found.
Hoppin' in testnet meadows, where efficient flows abound,
A whimsical bunny coding joy with leaps profound!

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

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. (Beta)
  • @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
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: 2

🧹 Nitpick comments (1)
services/requester/requester.go (1)

172-180: Consider implementing a cleanup mechanism for the memory store.

The memory store could grow unbounded as it accumulates rate limit data for different addresses. Consider implementing a cleanup mechanism or using a store with built-in TTL support.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4d3dfcd and 69935b4.

📒 Files selected for processing (4)
  • api/utils.go (1 hunks)
  • cmd/run/cmd.go (3 hunks)
  • config/config.go (1 hunks)
  • services/requester/requester.go (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Test
🔇 Additional comments (4)
config/config.go (1)

92-96: LGTM! Well-documented configuration fields.

The new configuration fields for transaction rate limiting are well-typed and clearly documented.

api/utils.go (1)

134-135: LGTM! Consistent error handling.

The rate limit error handling is consistent with the existing error handling pattern.

cmd/run/cmd.go (1)

290-291: LGTM! Well-configured command-line flags.

The default values (1 request per 3 seconds) align well with the testnet block production time, and the flags are clearly documented.

services/requester/requester.go (1)

219-224: LGTM! Well-implemented rate limiting.

The rate limiting implementation is clean and effective:

  • Only applies to testnet
  • Uses sender's address as key for per-address limiting
  • Returns appropriate error when limit is reached

cmd/run/cmd.go Outdated
Comment on lines 225 to 229
txDuration, err := time.ParseDuration(txDurationLimit)
if err != nil {
return fmt.Errorf("invalid unit %s for TX duration limit: %w", txDuration, err)
}
cfg.TxDurationLimit = txDuration
Copy link
Contributor

@coderabbitai coderabbitai bot Feb 10, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix error message variable.

The error message uses txDuration instead of txDurationLimit which could be confusing.

Apply this diff to fix the error message:

-		return fmt.Errorf("invalid unit %s for TX duration limit: %w", txDuration, err)
+		return fmt.Errorf("invalid unit %s for TX duration limit: %w", txDurationLimit, err)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
txDuration, err := time.ParseDuration(txDurationLimit)
if err != nil {
return fmt.Errorf("invalid unit %s for TX duration limit: %w", txDuration, err)
}
cfg.TxDurationLimit = txDuration
txDuration, err := time.ParseDuration(txDurationLimit)
if err != nil {
return fmt.Errorf("invalid unit %s for TX duration limit: %w", txDurationLimit, err)
}
cfg.TxDurationLimit = txDuration

Copy link
Contributor

Choose a reason for hiding this comment

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

this is legit

Copy link
Contributor

Choose a reason for hiding this comment

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

@peterargue Thanks for confirming! I'll mark this as resolved.


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Comment on lines 220 to 228
_, _, _, ok, _ := e.limiter.Take(ctx, from.Hex())
if !ok {
return common.Hash{}, errs.ErrRateLimit
}
}
Copy link
Contributor

@coderabbitai coderabbitai bot Feb 10, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue

Handle the error from limiter.Take().

The error from limiter.Take() should be handled as it could indicate issues with the rate limiter.

Apply this diff to handle the error:

-		_, _, _, ok, _ := e.limiter.Take(ctx, from.Hex())
+		_, _, _, ok, err := e.limiter.Take(ctx, from.Hex())
+		if err != nil {
+			return common.Hash{}, fmt.Errorf("failed to check rate limit: %w", err)
+		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
_, _, _, ok, _ := e.limiter.Take(ctx, from.Hex())
if !ok {
return common.Hash{}, errs.ErrRateLimit
}
}
_, _, _, ok, err := e.limiter.Take(ctx, from.Hex())
if err != nil {
return common.Hash{}, fmt.Errorf("failed to check rate limit: %w", err)
}
if !ok {
return common.Hash{}, errs.ErrRateLimit
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

this sounds legit

Copy link
Contributor

Choose a reason for hiding this comment

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

@peterargue Thanks for confirming! Looking forward to the improved error handling. 👍


If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point indeed. Updated in 7b49b08

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

@@ -202,6 +216,13 @@ func (e *EVM) SendRawTransaction(ctx context.Context, data []byte) (common.Hash,
return common.Hash{}, fmt.Errorf("failed to derive the sender: %w", err)
}

if e.config.FlowNetworkID == flowGo.Testnet {
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to check whether tx-request-limit > 0:

rateLimitEnabled := tx-request-limit > 0
if rateLimitEnabled {
  ...
}

So that by default, the rate limit is disabled, for testnet, we will add a flag tx-request-limit=1 to enable it.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1. let's make this flexible so it could be used anywhere, and let the operator decide.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good idea 👍 Updated in 7b49b08

cmd/run/cmd.go Outdated
Comment on lines 225 to 229
txDuration, err := time.ParseDuration(txDurationLimit)
if err != nil {
return fmt.Errorf("invalid unit %s for TX duration limit: %w", txDuration, err)
}
cfg.TxDurationLimit = txDuration
Copy link
Contributor

Choose a reason for hiding this comment

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

this is legit

cmd/run/cmd.go Outdated
@@ -280,4 +287,6 @@ func init() {
Cmd.Flags().StringVar(&cfg.ProfilerHost, "profiler-host", "localhost", "Host for the Profiler server")
Cmd.Flags().IntVar(&cfg.ProfilerPort, "profiler-port", 6060, "Port for the Profiler server")
Cmd.Flags().StringVar(&txStateValidation, "tx-state-validation", "tx-seal", "Sets the transaction validation mechanism. It can validate using the local state index, or wait for the outer Flow transaction to seal. Available values ('local-index' / 'tx-seal'), defaults to 'tx-seal'.")
Cmd.Flags().Uint64Var(&cfg.TxRequestLimit, "tx-request-limit", 1, "Number of transaction submissions to allow per the specified interval. Applies only on Testnet.")
Cmd.Flags().StringVar(&txDurationLimit, "tx-duration-limit", "3s", "Time interval upon which to enforce transaction submission rate limiting. Applies only on Testnet.")
Copy link
Contributor

Choose a reason for hiding this comment

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

you could use DurationVar which will automatically parse the value into a time.Duration

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Awesome, I missed this method 💯
Updated in b33cd9c

cmd/run/cmd.go Outdated
@@ -280,4 +287,6 @@ func init() {
Cmd.Flags().StringVar(&cfg.ProfilerHost, "profiler-host", "localhost", "Host for the Profiler server")
Cmd.Flags().IntVar(&cfg.ProfilerPort, "profiler-port", 6060, "Port for the Profiler server")
Cmd.Flags().StringVar(&txStateValidation, "tx-state-validation", "tx-seal", "Sets the transaction validation mechanism. It can validate using the local state index, or wait for the outer Flow transaction to seal. Available values ('local-index' / 'tx-seal'), defaults to 'tx-seal'.")
Cmd.Flags().Uint64Var(&cfg.TxRequestLimit, "tx-request-limit", 1, "Number of transaction submissions to allow per the specified interval. Applies only on Testnet.")
Cmd.Flags().StringVar(&txDurationLimit, "tx-duration-limit", "3s", "Time interval upon which to enforce transaction submission rate limiting. Applies only on Testnet.")
Copy link
Contributor

Choose a reason for hiding this comment

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

you could use DurationVar which will automatically parse the value into a time.Duration

the current naming sounds like a limit for the duration, not the other way around. Also, this applies on whichever network the gateway is configured to use

Suggested change
Cmd.Flags().StringVar(&txDurationLimit, "tx-duration-limit", "3s", "Time interval upon which to enforce transaction submission rate limiting. Applies only on Testnet.")
Cmd.Flags().StringVar(&txDurationLimit, "tx-request-limit-duration", "3s", "Time interval upon which to enforce transaction submission rate limiting.")

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point 👍 Updated in b33cd9c

@@ -202,6 +216,13 @@ func (e *EVM) SendRawTransaction(ctx context.Context, data []byte) (common.Hash,
return common.Hash{}, fmt.Errorf("failed to derive the sender: %w", err)
}

if e.config.FlowNetworkID == flowGo.Testnet {
Copy link
Contributor

Choose a reason for hiding this comment

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

+1. let's make this flexible so it could be used anywhere, and let the operator decide.

Comment on lines 220 to 228
_, _, _, ok, _ := e.limiter.Take(ctx, from.Hex())
if !ok {
return common.Hash{}, errs.ErrRateLimit
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

this sounds legit

@m-Peter m-Peter force-pushed the mpeter/testnet-tx-submission-rate-limit branch 2 times, most recently from 5de2d4d to 7b49b08 Compare February 11, 2025 12:24
@m-Peter m-Peter requested a review from peterargue February 11, 2025 16:59
@m-Peter m-Peter force-pushed the mpeter/testnet-tx-submission-rate-limit branch from 7b49b08 to 4e60c99 Compare February 14, 2025 09:25
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

🧹 Nitpick comments (1)
services/requester/requester.go (1)

219-228: Consider adding metrics for rate limit rejections.

While the rate limit implementation is correct, adding metrics would help monitor the rate limiting behavior in production.

Add metrics before returning the rate limit error:

 		if !ok {
+			e.collector.RateLimitRejection(from.Hex())
 			return common.Hash{}, errs.ErrRateLimit
 		}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 7b49b08 and 4e60c99.

📒 Files selected for processing (4)
  • api/utils.go (1 hunks)
  • cmd/run/cmd.go (1 hunks)
  • config/config.go (1 hunks)
  • services/requester/requester.go (5 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • api/utils.go
  • cmd/run/cmd.go
  • config/config.go
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Test
🔇 Additional comments (4)
services/requester/requester.go (4)

24-25: LGTM! Appropriate imports added.

The required imports for rate limiting functionality are correctly added.


115-115: LGTM! Rate limiter field added.

The limiter field is appropriately added to the EVM struct with the correct type.


172-180: LGTM! Rate limiter initialization looks good.

The rate limiter is properly initialized with configuration parameters for tokens and interval.


219-228:

✅ Verification successful

Verify rate limit configuration in the codebase.

Let's ensure the rate limit configuration is properly documented and consistently used.


🏁 Script executed:

#!/bin/bash
# Search for rate limit configuration usage
rg -A 5 "TxRequestLimit|TxRequestDurationLimit"

Length of output: 1598


Rate Limit Configuration Verified

The rate limit configuration is properly documented and consistently used across the codebase. In particular:

  • Configuration Definitions:
    The TxRequestLimit and TxRequestDurationLimit fields are documented in config/config.go with clear comments and are set via CLI flags in cmd/run/cmd.go.

  • Usage Consistency:
    The requester service in services/requester/requester.go checks e.config.TxRequestLimit > 0 to enable rate limiting and correctly invokes the limiter with these configurations.

No further changes are required.

@m-Peter m-Peter changed the title Add transaction rate-limit functionality for testnet Add transaction rate-limit functionality Feb 14, 2025
@@ -280,4 +280,6 @@ func init() {
Cmd.Flags().StringVar(&cfg.ProfilerHost, "profiler-host", "localhost", "Host for the Profiler server")
Cmd.Flags().IntVar(&cfg.ProfilerPort, "profiler-port", 6060, "Port for the Profiler server")
Cmd.Flags().StringVar(&txStateValidation, "tx-state-validation", "tx-seal", "Sets the transaction validation mechanism. It can validate using the local state index, or wait for the outer Flow transaction to seal. Available values ('local-index' / 'tx-seal'), defaults to 'tx-seal'.")
Cmd.Flags().Uint64Var(&cfg.TxRequestLimit, "tx-request-limit", 0, "Number of transaction submissions to allow per the specified interval.")
Cmd.Flags().DurationVar(&cfg.TxRequestDurationLimit, "tx-request-duration-limit", time.Second*3, "Time interval upon which to enforce transaction submission rate limiting.")
Copy link
Contributor

Choose a reason for hiding this comment

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

tx-request-duration-limit sounds like the limit is applied on the duration, not the other way around

Suggested change
Cmd.Flags().DurationVar(&cfg.TxRequestDurationLimit, "tx-request-duration-limit", time.Second*3, "Time interval upon which to enforce transaction submission rate limiting.")
Cmd.Flags().DurationVar(&cfg.TxRequestDurationLimit, "tx-request-limit-duration", time.Second*3, "Time interval upon which to enforce transaction submission rate limiting.")

Copy link
Contributor

@peterargue peterargue left a comment

Choose a reason for hiding this comment

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

added couple small comments, but looks good

Comment on lines +219 to +220
rateLimitEnabled := e.config.TxRequestLimit > 0
if rateLimitEnabled {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit

Suggested change
rateLimitEnabled := e.config.TxRequestLimit > 0
if rateLimitEnabled {
if e.config.TxRequestLimit > 0 {

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

Successfully merging this pull request may close these issues.

Add address based rate limiting for testnet
4 participants