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 frequency check for ema update; cleanup/refactor limit checking #10

Merged
merged 2 commits into from
Nov 10, 2024

Conversation

dshiell
Copy link
Member

@dshiell dshiell commented Nov 10, 2024

  • add EMA_UPDATE_DELTA_SECS to control how frequently we update gas/blob price limits
  • refactor code to compute elapsed time since last update and update both gas and blob prices
  • fix bug always update both gas/blob prices (if required) and do both checks for logging purposes

Summary by CodeRabbit

  • New Features

    • Improved logic for managing transaction fees and gas limits for enhanced efficiency.
    • Introduced a new constant to define update frequency for gas limit calculations.
  • Bug Fixes

    • Enhanced fee rejection logic for clearer and more structured handling of transaction fees.

Copy link

coderabbitai bot commented Nov 10, 2024

Walkthrough

The pull request introduces modifications to the src/server.ts file, focusing on the management of transaction fees and gas limits. A new constant, EMA_UPDATE_DELTA_SECS, is defined to set the update frequency for the exponential moving average of gas limits. Additionally, the feesTooHigh function's logic is refined to conditionally update gas and blob limits based on elapsed time since the last update. The function's return value is changed to a variable, rejectTxn, which centralizes fee rejection logic.

Changes

File Change Summary
src/server.ts - Added constant: EMA_UPDATE_DELTA_SECS.
- Added variable: lastUpdateTime.
- Modified feesTooHigh function to update gas and blob limits conditionally based on elapsed time and changed its return value to rejectTxn.

Possibly related PRs

  • adding gas limit #4: This PR modifies the feesTooHigh function and introduces constants for gas price limits, which are directly related to the changes made in the main PR regarding fee validation and limit updates.
  • add exponential weighting on txn price limits #6: This PR also updates the feesTooHigh function and introduces a new computation method for gas limits, which aligns with the main PR's enhancements to the logic for managing transaction fees and gas limits.

Suggested reviewers

  • Inkvi
  • jlafiandra6
  • alfredo-stonk

🐇 In the server's heart, where fees take flight,
A constant was born, to set things right.
With time as our guide, we now calculate,
Gas limits and blobs, we regulate.
So hop along, dear code, in harmony we thrive,
With clearer logic, our functions come alive! 🌟


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: 3

🧹 Outside diff range and nitpick comments (4)
src/server.ts (4)

22-25: Consider increasing the default TX_ALPHA value.

The default TX_ALPHA value of 1 (equivalent to 0.01) makes the EMA very slow to react to price changes. Consider increasing this value to make the system more responsive to market conditions while still maintaining stability.

-let TX_ALPHA = BigInt(process.env.TX_ALPHA ?? "1") // default alpha is equivalent to 0.01
+let TX_ALPHA = BigInt(process.env.TX_ALPHA ?? "10") // default alpha is equivalent to 0.1

117-131: Improve error logging for fee rejections.

The current error logging could be more informative for debugging purposes.

   if (gasPrice > TX_GASPRICE_LIMIT) {
-    console.error('Tx fees too high: %d > %d', gasPrice, TX_GASPRICE_LIMIT);
+    console.error('Transaction rejected: Gas price (%d) exceeds limit (%d) by %d', 
+      gasPrice, TX_GASPRICE_LIMIT, gasPrice - TX_GASPRICE_LIMIT);
     rejectTxn = true;
   }

   if (transactionArgs.blobVersionedHashes && transactionArgs.blobVersionedHashes.length > 0) {
     if (maxFeePerBlobGas > TX_BLOBPRICE_LIMIT) {
-      console.error('Blob fees too high: %d > %d', maxFeePerBlobGas, TX_BLOBPRICE_LIMIT );
+      console.error('Transaction rejected: Blob fee (%d) exceeds limit (%d) by %d',
+        maxFeePerBlobGas, TX_BLOBPRICE_LIMIT, maxFeePerBlobGas - TX_BLOBPRICE_LIMIT);
       rejectTxn = true;
     }
   }

Line range hint 67-73: Add input validation to computeLimitEMA function.

The function assumes non-negative inputs but doesn't validate them. This could lead to unexpected behavior with negative values.

 function computeLimitEMA(price: bigint, limit: bigint, alpha: bigint) {
+  if (price < 0 || limit < 0 || alpha < 0) {
+    throw new Error('computeLimitEMA: Inputs must be non-negative');
+  }
+  if (alpha > 100n) {
+    throw new Error('computeLimitEMA: Alpha must be <= 100');
+  }
   return (price - limit) * alpha / BigInt(100) + limit;
 }

97-131: Consider adding metrics for fee rejections.

To better understand the system's behavior in production, consider adding metrics for:

  • Number of transactions rejected due to high fees
  • Current limits vs actual fees
  • Frequency of limit updates

Would you like me to provide an example implementation using a metrics library?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4edcfe5 and 4cfd067.

📒 Files selected for processing (1)
  • src/server.ts (2 hunks)

src/server.ts Show resolved Hide resolved
src/server.ts Outdated Show resolved Hide resolved
src/server.ts Show resolved Hide resolved
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

🧹 Outside diff range and nitpick comments (4)
src/server.ts (4)

22-22: Fix misleading comment about TX_ALPHA value.

The comment states that alpha of 1 is equivalent to 0.01, but this is incorrect. With the current implementation, an alpha of 1 means 0.01 (1/100) weight in the EMA calculation.

-let TX_ALPHA = BigInt(process.env.TX_ALPHA ?? "1") // default alpha is equivalent to 0.01
+let TX_ALPHA = BigInt(process.env.TX_ALPHA ?? "1") // default alpha of 1 means 0.01 (1/100) weight in EMA

22-25: Consider improving type safety and immutability.

The current implementation uses mutable globals with implicit typing. Consider:

  1. Using TypeScript's const assertions
  2. Adding explicit type annotations
  3. Making variables readonly where possible
-let TX_ALPHA = BigInt(process.env.TX_ALPHA ?? "1")
-const EMA_UPDATE_DELTA_SECS = BigInt(process.env.EMA_UPDATE_DELTA_SECS ?? "60")
-let lastUpdateTime = 0;
+const TX_ALPHA: bigint = BigInt(process.env.TX_ALPHA ?? "1") as const;
+const EMA_UPDATE_DELTA_SECS: bigint = BigInt(process.env.EMA_UPDATE_DELTA_SECS ?? "60") as const;
+let lastUpdateTime: number = 0;

97-99: Consider using monotonic time for elapsed time calculation.

Using Date.now() for timing can be problematic if the system clock is adjusted. Consider using process.hrtime.bigint() for more reliable timing.

-const now = Math.floor(Date.now() / 1000);
-const elapsed_secs = now - lastUpdateTime;
+const now = process.hrtime.bigint() / BigInt(1e9);
+const elapsed_secs = Number(now - BigInt(lastUpdateTime));

117-120: Standardize error logging format.

The error logging format is inconsistent between gas and blob price checks. Consider using structured logging with consistent fields.

-console.error('Tx fees too high: %d > %d', gasPrice, TX_GASPRICE_LIMIT);
-console.error('Blob fees too high: %d > %d', maxFeePerBlobGas, TX_BLOBPRICE_LIMIT);
+console.error('Fee limit exceeded', {
+  type: 'gas',
+  actual: gasPrice.toString(),
+  limit: TX_GASPRICE_LIMIT.toString()
+});
+console.error('Fee limit exceeded', {
+  type: 'blob',
+  actual: maxFeePerBlobGas.toString(),
+  limit: TX_BLOBPRICE_LIMIT.toString()
+});

Also applies to: 123-127

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 4cfd067 and dec4be1.

📒 Files selected for processing (1)
  • src/server.ts (2 hunks)
🔇 Additional comments (1)
src/server.ts (1)

103-115: The past review comment about thread safety is still valid.

The concurrent updates to lastUpdateTime and limits could lead to race conditions. The suggested mutex solution from the past review should be implemented.

Comment on lines +97 to +131
const now = Math.floor(Date.now() / 1000);
const elapsed_secs = now - lastUpdateTime;
const doUpdate = (elapsed_secs >= EMA_UPDATE_DELTA_SECS);
const gasPrice = (maxFeePerGas + maxPriorityFeePerGas);
let rejectTxn = false;

if (doUpdate) {
lastUpdateTime = now;

// update gas limit
const newGasLimit = computeLimitEMA(gasPrice, TX_GASPRICE_LIMIT, TX_ALPHA);
console.log('Updating TX_GASPRICE_LIMIT: %d -> %d', TX_GASPRICE_LIMIT, newGasLimit);
TX_GASPRICE_LIMIT = newGasLimit;

// update blob limit
const newBlobLimit = computeLimitEMA(maxFeePerBlobGas, TX_BLOBPRICE_LIMIT, TX_ALPHA);
console.log('Updating TX_BLOBPRICE_LIMIT: %d -> %d', TX_BLOBPRICE_LIMIT, newBlobLimit);
TX_BLOBPRICE_LIMIT = newBlobLimit;
}

// check gas price limit
if (gasPrice > TX_GASPRICE_LIMIT) {
console.error('Tx fees too high: %d > %d', gasPrice, TX_GASPRICE_LIMIT);
return true;
rejectTxn = true;
}

// check blob price limits if applicable
if (transactionArgs.blobVersionedHashes && transactionArgs.blobVersionedHashes.length > 0) {
const newBlobLimit = computeLimitEMA(maxFeePerBlobGas, TX_BLOBPRICE_LIMIT, TX_ALPHA);
console.log('Updating TX_BLOBPRICE_LIMIT: %d -> %d', TX_BLOBPRICE_LIMIT, newBlobLimit);
TX_BLOBPRICE_LIMIT = newBlobLimit;

if (maxFeePerBlobGas > TX_BLOBPRICE_LIMIT) {
console.error('Blob fees too high: %d > %d', maxFeePerBlobGas, TX_BLOBPRICE_LIMIT );
return true;
rejectTxn = true;
}
}

return false;
return rejectTxn;
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

🛠️ Refactor suggestion

Input validation for negative values is essential

The review comment is correct. Here's why:

  1. The computeLimitEMA function's implementation shows it performs subtraction (price - limit) which could result in negative values with incorrect inputs
  2. The function's comment explicitly states "@brief Assumes that all inputs are non-negative" but doesn't enforce this assumption
  3. Using BigInt arithmetic with negative values could lead to unexpected results in the EMA calculation

The suggested input validation in the review comment should be implemented to prevent potential issues with negative fee values.

🔗 Analysis chain

Add input validation for negative values.

The function assumes all inputs are non-negative but doesn't validate this assumption. Consider adding checks for negative values.

+  // Validate non-negative inputs
+  if (maxFeePerGas < 0 || maxPriorityFeePerGas < 0 || maxFeePerBlobGas < 0) {
+    console.error('Negative fee values are not allowed');
+    return true;
+  }

Additionally, verify that the EMA calculation in computeLimitEMA handles negative price-limit differences correctly.


Let me search for the implementation of computeLimitEMA to analyze how it handles negative values.


Let me try a different approach to find the implementation of computeLimitEMA.


Let me try one final search with a broader pattern to locate the function definition.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for tests or usage of computeLimitEMA with negative values
rg -l "computeLimitEMA.*-" || echo "No tests found for negative values"

Length of output: 107


Script:

#!/bin/bash
# Find implementation of computeLimitEMA
ast-grep --pattern 'function computeLimitEMA'

Length of output: 47


Script:

#!/bin/bash
# Search for computeLimitEMA implementation using ripgrep
rg -A 5 "computeLimitEMA.*=" 

Length of output: 30


Script:

#!/bin/bash
# Search for any mention of computeLimitEMA in the codebase
rg "computeLimitEMA" -C 5

Length of output: 1373

@dshiell dshiell merged commit 4640587 into main Nov 10, 2024
1 check passed
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