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

docs: gas used and gas price estimation ADR #4238

Merged
merged 8 commits into from
Jan 20, 2025

Conversation

rach-id
Copy link
Member

@rach-id rach-id commented Jan 17, 2025

Overview

Adds an ADR for CIP-18

@rach-id rach-id added the documentation Improvements or additions to documentation label Jan 17, 2025
@rach-id rach-id self-assigned this Jan 17, 2025
@rach-id rach-id requested a review from a team as a code owner January 17, 2025 13:21
@rach-id rach-id requested review from rootulp and evan-forbes and removed request for a team January 17, 2025 13:21
Copy link
Contributor

coderabbitai bot commented Jan 17, 2025

📝 Walkthrough

Walkthrough

The pull request introduces ADR 023, which outlines the gas used and gas price estimation for the celestia-app, in accordance with CIP-18. It details the implementation of gas and fee estimation mechanisms, including methods for calculating gas usage through baseapp simulation and a tiered approach for gas price estimation based on transaction priority levels. The document serves as a foundational reference for transaction building within the application.

Changes

File Change Summary
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md New ADR document added detailing gas estimation strategy, including simulation methods, gas price calculation, and priority-based pricing approach.

Suggested labels

external

Suggested reviewers

  • cmwaters
  • evan-forbes
  • ninabarbakadze
  • rootulp

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:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

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

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

🧹 Nitpick comments (3)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (3)

29-34: Enhance the code example with return value usage.

The code snippet shows how to obtain gasInfo but doesn't demonstrate how this information should be used or what specific fields from gasInfo are relevant for the estimation. Consider adding:

  1. The expected structure of gasInfo
  2. Which fields should be used for estimation
  3. How to handle edge cases (e.g., minimum gas requirements)

40-43: Specify implementation details for gas price calculations.

While the priority levels and their calculations are well-defined, the following implementation details should be clarified:

  1. How to handle blocks with very few transactions (statistical significance)
  2. Whether the gas prices should be weighted by gas used
  3. How often these calculations should be updated (every block or cached)

83-83: Remove trailing space.

There is a trailing space at the end of this line.

-There are multiple ways to estimate the gas price. However, a first default implementation can be as basic as described above. 
+There are multiple ways to estimate the gas price. However, a first default implementation can be as basic as described above.
🧰 Tools
🪛 Markdownlint (0.37.0)

83-83: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2e18fb6 and 9dedf16.

📒 Files selected for processing (1)
  • docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.37.0)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md

83-83: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: docker-security-build / docker-build (GHCR; ghcr.io/celestiaorg/celestia-app)
  • GitHub Check: test / test-race
  • GitHub Check: test / test-fuzz
  • GitHub Check: test / test-coverage
  • GitHub Check: test / test
  • GitHub Check: markdown-linter / markdown-link-check
  • GitHub Check: Summary
🔇 Additional comments (1)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (1)

19-19: Clarify the fee estimation mechanism.

The fee estimation mechanism is marked as TBD. Since this is a crucial part of the implementation, it would be beneficial to outline at least the high-level approach or options being considered for fee estimation.

Would you like me to help draft potential fee estimation approaches based on common blockchain patterns?

@celestia-bot celestia-bot requested a review from rootulp January 17, 2025 15:20
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 (2)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (2)

13-13: Improve context section clarity.

The suggested improvement from the previous review provides better context by explicitly referencing CIP-18.

Apply this change:

-As per [CIP-18: Standardised Gas and Pricing Estimation Interface](https://github.com/celestiaorg/CIPs/blob/main/cips/cip-18.md), celestia-app will provide a base implementation for the gas and fee estimation so it can be used when building transactions.
+As per [CIP-18: Standardised Gas and Pricing Estimation Interface](https://github.com/celestiaorg/CIPs/blob/main/cips/cip-18.md), celestia-app will provide a base implementation for the gas and fee estimation so it can be used when building transactions.

89-89: Fix markdown formatting.

Remove trailing spaces to comply with markdown formatting guidelines.

🧰 Tools
🪛 Markdownlint (0.37.0)

89-89: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9dedf16 and 3c069cf.

📒 Files selected for processing (1)
  • docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.37.0)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md

89-89: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: docker-security-build / docker-build (GHCR; ghcr.io/celestiaorg/celestia-app)
  • GitHub Check: docker-txsim-build / docker-build (GHCR; ghcr.io/celestiaorg/txsim)
  • GitHub Check: test / test-short
  • GitHub Check: test / test-race
  • GitHub Check: test / test-fuzz
  • GitHub Check: test / test-coverage
  • GitHub Check: test / test
  • GitHub Check: markdown-linter / markdown-link-check
  • GitHub Check: Summary
🔇 Additional comments (2)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (2)

30-34: Consider gas adjustment multiplier for estimation.

The current implementation might underestimate gas usage. Consider incorporating a gas adjustment multiplier (e.g., 1.1) to provide a safety margin, as mentioned in the CLI's --gas-adjustment flag.

Would you like me to propose an implementation that includes the gas adjustment multiplier?


59-85: Improve numerical stability of statistical calculations.

The current implementation might face numerical stability issues with floating-point arithmetic. Consider implementing the previously suggested improvements for better numerical stability.

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)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (1)

43-56: ⚠️ Potential issue

Resolve inconsistency in gas price calculation methodology.

The document describes two different approaches:

  1. Using median of percentiles (lines 43-46)
  2. Using mean ± standard deviation (lines 53-56)

This creates ambiguity about which method will be implemented. Consider:

  • Choosing one consistent approach
  • Explaining why that approach was chosen
  • Removing references to the unused approach
🧹 Nitpick comments (2)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (2)

63-87: Enhance statistical calculations for better accuracy.

The standard deviation implementation could be improved:

  1. Use sample variance (n-1) instead of population variance (n)
  2. Consider numerical stability for large numbers

Apply these improvements:

 func standardDeviation(gasPrices []float64) float64 {
     if len(gasPrices) < 2 {
         return 0
     }
     meanGasPrice := mean(gasPrices)
     var variance float64
     for _, gasPrice := range gasPrices {
         variance += math.Pow(gasPrice-meanGasPrice, 2)
     }
-    variance /= float64(len(gasPrices))
+    variance /= float64(len(gasPrices) - 1)  // Use n-1 for sample variance
     return math.Sqrt(variance)
 }

92-92: Fix markdown formatting.

Remove trailing whitespace at the end of line 92.

🧰 Tools
🪛 Markdownlint (0.37.0)

92-92: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3c069cf and b31204f.

📒 Files selected for processing (1)
  • docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.37.0)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md

92-92: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: docker-txsim-build / docker-build (GHCR; ghcr.io/celestiaorg/txsim)
  • GitHub Check: docker-security-build / docker-build (GHCR; ghcr.io/celestiaorg/celestia-app)
  • GitHub Check: test / test-short
  • GitHub Check: test / test-race
  • GitHub Check: test / test-fuzz
  • GitHub Check: test / test-coverage
  • GitHub Check: test / test
  • GitHub Check: markdown-linter / markdown-link-check
  • GitHub Check: Summary
🔇 Additional comments (2)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (2)

19-19: Clarify the timeline and scope of fee estimation mechanism.

The document states that fee estimation is TBD. Consider adding more context about when this will be determined and what factors will influence the decision.


27-37: LGTM! Clear explanation of gas estimation approach.

The implementation using baseapp simulation mode is appropriate, and the note about client-side multipliers clearly addresses the previous concerns about gas adjustment.

evan-forbes
evan-forbes previously approved these changes Jan 20, 2025
rootulp
rootulp previously approved these changes Jan 20, 2025
Copy link
Collaborator

@rootulp rootulp left a comment

Choose a reason for hiding this comment

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

Approving to unblock.

I'm still a little confused on which approach this ADR proposes:

  • median of the top 10%
  • mean + 1.28 * standard deviation of gas prices

so seems clearer to remove one of those and describe the one that we'll use

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)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (1)

43-49: ⚠️ Potential issue

Resolve inconsistency in gas price calculation methods.

There's a discrepancy between the two approaches described:

  1. Lines 43-46 use percentile-based approach (median of top/bottom 10%)
  2. Lines 53-56 use statistical approach (mean ± standard deviation)

Choose one consistent approach, preferably the percentile-based method, as it's more robust and doesn't assume normal distribution of gas prices.

🧹 Nitpick comments (3)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (3)

19-19: Fix trailing whitespace.

Remove the trailing whitespace at the end of line 19.


76-87: Use sample standard deviation for better statistical accuracy.

The current implementation uses population standard deviation (n divisor). For sample data, it's more appropriate to use sample standard deviation (n-1 divisor).

Apply this change:

-	variance /= float64(len(gasPrices))
+	variance /= float64(len(gasPrices) - 1)  // Use n-1 for sample standard deviation

102-108: Consider additional consequences.

The consequences section could benefit from considering:

  • Performance impact of calculating statistics on historical block data
  • Memory usage for storing historical gas prices
  • Network load from frequent gas estimation requests
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b31204f and d91822e.

📒 Files selected for processing (1)
  • docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.37.0)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md

92-92: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: docker-security-build / docker-build (GHCR; ghcr.io/celestiaorg/celestia-app)
  • GitHub Check: docker-txsim-build / docker-build (GHCR; ghcr.io/celestiaorg/txsim)
  • GitHub Check: test / test-race
  • GitHub Check: test / test-fuzz
  • GitHub Check: test / test-coverage
  • GitHub Check: test / test
  • GitHub Check: test / test-short
  • GitHub Check: markdown-linter / markdown-link-check
  • GitHub Check: Summary
🔇 Additional comments (2)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (2)

27-37: Well-documented gas estimation approach!

The implementation using baseapp simulation mode is clear, and the note about client-side gas multipliers effectively addresses previous concerns about potential underestimation.


46-46: Remove redundant priority level.

The "None Priority" is equivalent to "Medium Priority", making it redundant. Consider removing this priority level to simplify the API.

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 (3)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (3)

30-34: Consider additional error cases in simulation.

The current error handling is basic. Consider handling specific error cases such as:

  • Out of gas errors
  • Invalid transaction format
  • State-dependent failures

This would help clients better understand why their transaction simulation failed.

 	gasInfo, _, err := app.BaseApp.Simulate(tx)
 	if err != nil {
+		switch {
+		case errors.Is(err, sdkerrors.ErrOutOfGas):
+			return nil, fmt.Errorf("out of gas error: %w", err)
+		case errors.Is(err, sdkerrors.ErrTxDecode):
+			return nil, fmt.Errorf("invalid transaction format: %w", err)
+		default:
+			return nil, fmt.Errorf("simulation failed: %w", err)
+		}
-		return nil, err
 	}

92-92: Fix trailing whitespace.

Remove the trailing whitespace at the end of line 92.

🧰 Tools
🪛 Markdownlint (0.37.0)

92-92: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)


102-108: Consider additional consequences.

The consequences section could be more comprehensive. Consider adding:

Negative consequences:

  • Additional computational overhead for nodes
  • Potential for gaming the estimation system

Neutral consequences:

  • Need for periodic review and adjustment of estimation parameters
  • Impact on node operators who need to maintain historical gas price data
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d91822e and 2f5ec14.

📒 Files selected for processing (1)
  • docs/architecture/adr-023-gas-used-and-gas-price-estimation.md (1 hunks)
🧰 Additional context used
🪛 Markdownlint (0.37.0)
docs/architecture/adr-023-gas-used-and-gas-price-estimation.md

92-92: Expected: 0 or 2; Actual: 1
Trailing spaces

(MD009, no-trailing-spaces)

⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: docker-security-build / docker-build (GHCR; ghcr.io/celestiaorg/celestia-app)
  • GitHub Check: docker-txsim-build / docker-build (GHCR; ghcr.io/celestiaorg/txsim)
  • GitHub Check: test / test-short
  • GitHub Check: test / test-race
  • GitHub Check: test / test-fuzz
  • GitHub Check: test / test-coverage
  • GitHub Check: test / test
  • GitHub Check: markdown-linter / markdown-link-check
  • GitHub Check: Summary

@rach-id rach-id merged commit d3dcaba into celestiaorg:main Jan 20, 2025
28 checks passed
Comment on lines +43 to +46
- High Priority: The gas price is the price at the start of the top 10% of transactions’ gas prices from the last 5 blocks.
- Medium Priority: The gas price is the median of all gas prices from the last 5 blocks.
- Low Priority: The gas price is the value at the end of the lowest 10% of gas prices from the last 5 blocks.
- None Priority: This is equivalent to the Medium priority, using the median of all gas prices from the last 5 blocks.
Copy link
Contributor

Choose a reason for hiding this comment

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

Just something to be wary of: this doesn't consider the fullness of the blocks. For example if there is only one transaction in the last 5 blocks, The new pricing will be in direct relation of what the price of that transaction was. Yet if the blocks continue to be empty, the estimation should be the minimum

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, in this case, the returned gas price will be that of that transaction. There are many edge cases we can consider in this and multiple ways to enhance this estimation.

The most important IMO is to return a gas price estimate > than the minimum, only in the case of the last 5 blocks are all > 50% full or some other number.

But I guess we just want to provide an API for now, even if the numbers are skewed, to lay ground for the community to build more accurate estimation algorithms.

If not, we can rework the estimation mechanism.

rach-id added a commit that referenced this pull request Jan 27, 2025
## Overview

Adds an ADR for CIP-18

---------

Co-authored-by: Rootul P <[email protected]>
(cherry picked from commit d3dcaba)
rach-id added a commit that referenced this pull request Jan 27, 2025
## Overview

Adds an ADR for CIP-18

---------

Co-authored-by: Rootul P <[email protected]>
(cherry picked from commit d3dcaba)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants