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(sidecar): min priority fee #246

Merged
merged 12 commits into from
Oct 2, 2024
Merged

Conversation

namn-grg
Copy link
Contributor

@namn-grg namn-grg commented Sep 30, 2024

fixes #228

Summary by CodeRabbit

Release Notes

  • New Features

    • Enhanced configuration options for setting minimum priority fees via command-line arguments.
    • Added validation methods to ensure priority fees meet specified conditions.
  • Bug Fixes

    • Improved validation logic for transaction fees, ensuring better handling of maximum and minimum priority fees.
  • Documentation

    • Clarified documentation for the calculate_max_basefee function regarding input units.
  • Refactor

    • Streamlined code in testing utilities for improved efficiency and clarity.

Copy link
Contributor

@thedevbirb thedevbirb left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Some observations

Comment on lines 58 to 60
/// Min priority fee to accept for a commitment
#[clap(long, env = "BOLT_SIDECAR_MIN_PRIORITY_FEE")]
pub(super) min_priority_fee: Option<NonZero<u64>>,
Copy link
Contributor

@thedevbirb thedevbirb Sep 30, 2024

Choose a reason for hiding this comment

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

Isn't u128 a better type to express priority fees? I'm not sure tho, could you take a look?

Comment on lines 162 to 163
.max_priority_fee_per_gas()
.is_some_and(|max_priority_fee| max_priority_fee < min_priority_fee)
Copy link
Contributor

Choose a reason for hiding this comment

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

What if in all the txs the max_priority_fee_per_gas is None? That is the case for legacy transactions and should be handled carefully by looking at the gas_price instead.

bolt-sidecar/src/state/execution.rs Show resolved Hide resolved
@namn-grg namn-grg requested a review from thedevbirb September 30, 2024 13:20
Comment on lines 150 to 152
if max_priority_fee + gas_price > tx.max_fee_per_gas() {
return false;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

btw this was already there though

Comment on lines 168 to 177
let gas_price = tx.as_legacy().map(|tx| tx.gas_price).unwrap_or(0);
let gas_price = if max_base_fee >= gas_price {
0
} else {
(gas_price - max_base_fee) / GWEI_TO_WEI as u128
};
let max_priority_fee = tx.max_priority_fee_per_gas().unwrap_or(0);

if gas_price + max_priority_fee < min_priority_fee {
return false;
Copy link
Contributor

@thedevbirb thedevbirb Oct 1, 2024

Choose a reason for hiding this comment

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

I don't have clear some points:

  • How do you handle type 1 / EIP-2930 transactions?
  • Why are you dividing the gas price for GWEI_TO_WEI?
  • Can't you just check if to use gas_price or max_priority_fee by checking the type of the transaction?
  • Can you add some comments as well with steps and motivation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good point, addressed, thanks

Copy link
Contributor

@thedevbirb thedevbirb left a comment

Choose a reason for hiding this comment

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

Some nits! Now it looks good

bolt-sidecar/src/config/mod.rs Show resolved Hide resolved
bolt-sidecar/src/config/mod.rs Show resolved Hide resolved
bolt-sidecar/src/primitives/commitment.rs Outdated Show resolved Hide resolved
bolt-sidecar/src/primitives/commitment.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@merklefruit merklefruit left a comment

Choose a reason for hiding this comment

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

I like reusing Reth's logic, +1

@mempirate
Copy link
Contributor

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Oct 2, 2024

Walkthrough

The pull request introduces several modifications across multiple files in the Bolt sidecar project. Key changes include updates to documentation for the calculate_max_basefee function, enhancements to configuration options by adding new constants and fields, and refinements to validation logic within the InclusionRequest and ExecutionState structures. Additionally, new methods for calculating effective tips and validating priority fees are introduced, while minor refactoring and clarifications are made in test utility functions.

Changes

File Path Change Summary
bolt-sidecar/src/common.rs Documentation for calculate_max_basefee updated (unit changed from "gwei" to "wei").
bolt-sidecar/src/config/mod.rs Added constants for limits and a new field min_priority_fee in Opts and Limits structs.
bolt-sidecar/src/primitives/commitment.rs Renamed validate_priority_fee to validate_max_priority_fee, added validate_min_priority_fee.
bolt-sidecar/src/primitives/mod.rs Added effective_tip_per_gas method to FullTransaction, modified digest methods in messages.
bolt-sidecar/src/state/execution.rs Added MaxPriorityFeePerGasTooLow variant in ValidationError, updated validation methods.
bolt-sidecar/src/test_util.rs Updated comments and simplified transaction collection logic in test utility functions.

Assessment against linked issues

Objective Addressed Explanation
Set up a configuration option for a minimum priority fee (228)

Possibly related PRs

Suggested labels

C: bolt-sidecar, T: chore

Suggested reviewers

  • thedevbirb

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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Files that changed from the base of the PR and between 107f098 and 86410fe.

📒 Files selected for processing (6)
  • bolt-sidecar/src/common.rs (1 hunks)
  • bolt-sidecar/src/config/mod.rs (4 hunks)
  • bolt-sidecar/src/primitives/commitment.rs (1 hunks)
  • bolt-sidecar/src/primitives/mod.rs (3 hunks)
  • bolt-sidecar/src/state/execution.rs (10 hunks)
  • bolt-sidecar/src/test_util.rs (2 hunks)
🔇 Additional comments (17)
bolt-sidecar/src/primitives/commitment.rs (1)

146-157: LGTM: Improved method name and logic.

The renaming and logic update correctly validate the max priority fee against the max fee per gas, aligning with EIP-1559.

bolt-sidecar/src/test_util.rs (3)

115-115: Good clarification.

Comment explains the value unit. Helpful for understanding.


192-192: Efficient iteration.

New order reduces cloning operations. Improves performance for large collections.


Line range hint 1-282: LGTM. Minor improvements made.

Changes enhance clarity and efficiency. No issues found.

bolt-sidecar/src/config/mod.rs (6)

31-34: LGTM! Constants added as suggested.

The new constants are well-defined and exposed. The DEFAULT_MIN_PRIORITY_FEE is set to 1 Gwei as recommended.


63-65: LGTM! min_priority_fee field added correctly.

The new field uses u128 as suggested and is properly defined as an optional NonZero value.


169-171: LGTM! min_priority_fee field added to Limits struct.

The field is correctly defined as a NonZero<u128> with an accurate comment.


177-181: LGTM! Default implementation updated correctly.

The min_priority_fee is properly initialized with the default constant.


212-215: LGTM! TryFrom<Opts> implementation updated correctly.

The min_priority_fee option is properly handled and assigned to the config.


Line range hint 1-285: Overall, the changes successfully implement the minimum priority fee configuration.

The implementation aligns well with the PR objectives and addresses the linked issue #228. The code quality is good, and past review suggestions have been incorporated effectively.

bolt-sidecar/src/primitives/mod.rs (2)

464-465: LGTM. Simplified implementation.

The change removes unnecessary references, improving code clarity.


486-487: LGTM. Consistent with DelegationMessage.

The change aligns with the DelegationMessage implementation, maintaining consistency.

bolt-sidecar/src/state/execution.rs (5)

57-59: Add new validation error for minimum priority fee

The new ValidationError variant MaxPriorityFeePerGasTooLow is properly declared with a clear error message. This enhances the error handling for transactions that do not meet the minimum priority fee requirement.


108-108: Include new error variant in to_tag_str method

Good job adding MaxPriorityFeePerGasTooLow to the to_tag_str method. This ensures the error is correctly tagged for metrics and logging purposes.


301-305: Validate minimum priority fee in validate_request

The addition of the minimum priority fee check in validate_request correctly ensures that max_priority_fee_per_gas meets or exceeds the configured minimum. The logic is sound and aligns with the intended behavior.


830-870: Add test for minimum priority fee validation

Great work on adding the test_invalid_inclusion_request_min_priority_fee test. It effectively checks scenarios where the priority fee is too low and confirms that transactions with sufficient priority fee are accepted.


872-917: Include test for legacy transactions

Excellent job addressing previous feedback by adding the test_invalid_inclusion_request_min_priority_fee_legacy test. This ensures that legacy transactions are also validated against the minimum priority fee, enhancing test coverage.

bolt-sidecar/src/common.rs Show resolved Hide resolved
Comment on lines 159 to 177
/// Validates the priority fee against a minimum priority fee.
/// Returns `true` if the "effective priority fee" is greater than or equal to the set minimum
/// priority fee, `false` otherwise.
pub fn validate_min_priority_fee(&self, max_base_fee: u128, min_priority_fee: u128) -> bool {
for tx in &self.txs {
// If this returns None, the fee is lower than the basefee
let Some(tip) = tx.effective_tip_per_gas(max_base_fee) else {
return false;
};

// Check if the effective gas tip is more than the minimum priority fee
if tip < min_priority_fee {
return false;
}
}

// If all transactions meet the minimum priority fee, return true
true
}
Copy link
Contributor

@coderabbitai coderabbitai bot Oct 2, 2024

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

LGTM: New method implements minimum priority fee validation.

The method correctly validates the effective priority fee against a minimum threshold, addressing the PR objective.

Consider using all() for a more concise implementation:

 pub fn validate_min_priority_fee(&self, max_base_fee: u128, min_priority_fee: u128) -> bool {
-    for tx in &self.txs {
-        // If this returns None, the fee is lower than the basefee
-        let Some(tip) = tx.effective_tip_per_gas(max_base_fee) else {
-            return false;
-        };
-
-        // Check if the effective gas tip is more than the minimum priority fee
-        if tip < min_priority_fee {
-            return false;
-        }
-    }
-
-    // If all transactions meet the minimum priority fee, return true
-    true
+    self.txs.iter().all(|tx| {
+        tx.effective_tip_per_gas(max_base_fee)
+            .map_or(false, |tip| tip >= min_priority_fee)
+    })
 }
📝 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
/// Validates the priority fee against a minimum priority fee.
/// Returns `true` if the "effective priority fee" is greater than or equal to the set minimum
/// priority fee, `false` otherwise.
pub fn validate_min_priority_fee(&self, max_base_fee: u128, min_priority_fee: u128) -> bool {
for tx in &self.txs {
// If this returns None, the fee is lower than the basefee
let Some(tip) = tx.effective_tip_per_gas(max_base_fee) else {
return false;
};
// Check if the effective gas tip is more than the minimum priority fee
if tip < min_priority_fee {
return false;
}
}
// If all transactions meet the minimum priority fee, return true
true
}
/// Validates the priority fee against a minimum priority fee.
/// Returns `true` if the "effective priority fee" is greater than or equal to the set minimum
/// priority fee, `false` otherwise.
pub fn validate_min_priority_fee(&self, max_base_fee: u128, min_priority_fee: u128) -> bool {
self.txs.iter().all(|tx| {
tx.effective_tip_per_gas(max_base_fee)
.map_or(false, |tip| tip >= min_priority_fee)
})
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

this is nice

Copy link
Contributor

Choose a reason for hiding this comment

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

@merklefruit, glad you liked it!

(_/)
(•‿•)
/︶\

Comment on lines +396 to +412
pub fn effective_tip_per_gas(&self, base_fee: u128) -> Option<u128> {
let max_fee_per_gas = self.max_fee_per_gas();

if max_fee_per_gas < base_fee {
return None;
}

// Calculate the difference between max_fee_per_gas and base_fee
let fee = max_fee_per_gas - base_fee;

// Compare the fee with max_priority_fee_per_gas (or gas price for non-EIP1559 transactions)
if let Some(priority_fee) = self.max_priority_fee_per_gas() {
Some(fee.min(priority_fee))
} else {
Some(fee)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick (assertive)

LGTM. Consider adding a comment for clarity.

The implementation looks correct. For better readability, consider adding a brief comment explaining the fee calculation.

 // Calculate the difference between max_fee_per_gas and base_fee
 let fee = max_fee_per_gas - base_fee;
+
+// Effective tip is the minimum of fee and max_priority_fee_per_gas (if available)
📝 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
pub fn effective_tip_per_gas(&self, base_fee: u128) -> Option<u128> {
let max_fee_per_gas = self.max_fee_per_gas();
if max_fee_per_gas < base_fee {
return None;
}
// Calculate the difference between max_fee_per_gas and base_fee
let fee = max_fee_per_gas - base_fee;
// Compare the fee with max_priority_fee_per_gas (or gas price for non-EIP1559 transactions)
if let Some(priority_fee) = self.max_priority_fee_per_gas() {
Some(fee.min(priority_fee))
} else {
Some(fee)
}
}
pub fn effective_tip_per_gas(&self, base_fee: u128) -> Option<u128> {
let max_fee_per_gas = self.max_fee_per_gas();
if max_fee_per_gas < base_fee {
return None;
}
// Calculate the difference between max_fee_per_gas and base_fee
let fee = max_fee_per_gas - base_fee;
// Effective tip is the minimum of fee and max_priority_fee_per_gas (if available)
// Compare the fee with max_priority_fee_per_gas (or gas price for non-EIP1559 transactions)
if let Some(priority_fee) = self.max_priority_fee_per_gas() {
Some(fee.min(priority_fee))
} else {
Some(fee)
}
}

Copy link
Contributor

@thedevbirb thedevbirb left a comment

Choose a reason for hiding this comment

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

Agreed that coderabbit made some nice suggestions! If you include them this is good

@mempirate mempirate merged commit 5825881 into unstable Oct 2, 2024
2 checks passed
@mempirate mempirate deleted the naman/feat/min-priority-fee branch October 2, 2024 09:59
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.

Minimum priority fee config
4 participants