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

chore: Fix offset calculation to correctly skip blocks from startHeight #4267

Closed
wants to merge 1 commit into from

Conversation

fragchain
Copy link

Overview

The offset parameter wasn't properly accounting for the startHeight when skipping blocks.
This change replaces offset with offset-startHeight to ensure the correct number of blocks are skipped starting from startHeight.
This should resolve any inconsistencies in block processing.

@fragchain fragchain requested a review from a team as a code owner January 26, 2025 19:00
@fragchain fragchain requested review from cmwaters and ninabarbakadze and removed request for a team January 26, 2025 19:00
Copy link
Contributor

coderabbitai bot commented Jan 26, 2025

📝 Walkthrough

Walkthrough

The pull request involves a modification to the main.go file in the tools/bytes-in-block directory. The change focuses on adjusting the calculation of the offset parameter in the URL for a GET request to an API. The update ensures that the offset is calculated relative to the starting height, providing a more precise method of specifying the block range for the API query.

Changes

File Change Summary
tools/bytes-in-block/main.go Modified URL offset calculation by subtracting startHeight from the original offset value

Sequence Diagram

sequenceDiagram
    participant Client
    participant API
    Client->>API: GET Request with Adjusted Offset
    Note over Client, API: offset = originalOffset - startHeight
    API-->>Client: Response
Loading

The sequence diagram illustrates how the client now sends a GET request to the API with an offset that is relative to the starting height, ensuring more accurate block range specification.

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

‼️ 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: 0

🧹 Nitpick comments (3)
tools/bytes-in-block/main.go (3)

41-41: LGTM! The offset calculation fix looks correct.

The change to subtract startHeight from offset ensures that the API correctly skips the right number of blocks when paginating from the specified start height.

However, consider making the API endpoint configurable:

+const defaultAPIEndpoint = "https://api-mocha.celenium.io/v1"
+
 func main() {
+    apiEndpoint := os.Getenv("CELESTIA_API_ENDPOINT")
+    if apiEndpoint == "" {
+        apiEndpoint = defaultAPIEndpoint
+    }
     ...
-    url := fmt.Sprintf("https://api-mocha.celenium.io/v1/block?limit=%d&offset=%d&sort=asc&stats=true", numberOfBlocksPerRequest, offset-startHeight)
+    url := fmt.Sprintf("%s/block?limit=%d&offset=%d&sort=asc&stats=true", apiEndpoint, numberOfBlocksPerRequest, offset-startHeight)

Line range hint 27-36: Consider validating input parameters.

The start and end heights are hardcoded. Consider adding validation to ensure:

  1. startHeight is positive
  2. endHeight is greater than startHeight
  3. numberOfBlocksPerRequest is within reasonable limits
 func main() {
     startHeight := 4170798
     endHeight := 4170898
     numberOfBlocksPerRequest := 100
 
+    if startHeight < 0 {
+        log.Fatal("startHeight must be positive")
+    }
+    if endHeight <= startHeight {
+        log.Fatal("endHeight must be greater than startHeight")
+    }
+    if numberOfBlocksPerRequest <= 0 || numberOfBlocksPerRequest > 1000 {
+        log.Fatal("numberOfBlocksPerRequest must be between 1 and 1000")
+    }

Line range hint 43-49: Enhance error handling for HTTP responses.

The current error handling only checks if the request failed, but not the HTTP status code. Consider adding status code validation:

     resp, err := http.Get(url)
     if err != nil {
         log.Fatalf("Failed to GET %s: %v", url, err)
     }
+    if resp.StatusCode != http.StatusOK {
+        body, _ := io.ReadAll(resp.Body)
+        resp.Body.Close()
+        log.Fatalf("API returned non-200 status code %d: %s", resp.StatusCode, body)
+    }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 629b807 and 824c861.

📒 Files selected for processing (1)
  • tools/bytes-in-block/main.go (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Summary

@@ -38,7 +38,7 @@ func main() {
// you don't exceed the last block. But the endpoint might just return fewer
// blocks if you overshoot. In practice, `offset` is just how many items to skip.
// We'll keep it simple:
url := fmt.Sprintf("https://api-mocha.celenium.io/v1/block?limit=%d&offset=%d&sort=asc&stats=true", numberOfBlocksPerRequest, offset)
url := fmt.Sprintf("https://api-mocha.celenium.io/v1/block?limit=%d&offset=%d&sort=asc&stats=true", numberOfBlocksPerRequest, offset-startHeight)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this is how it's supposed to work. Apparently in this API, this request queries for the blocks between [offset, offset + numberOfBlocksPerRequest]. In this case offset starts at startHeight and keeps increasing by numberOfBlocksPerRequest on each iteration until it reaches the end.

So no change to this call is required.

Copy link
Member

Choose a reason for hiding this comment

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

@rootulp can confirm

Copy link
Author

Choose a reason for hiding this comment

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

rach-id thank you for your feedback! I see your point, but I’d like to clarify why I believe this change is necessary:

The current implementation assumes that offset always starts at startHeight and increments by numberOfBlocksPerRequest. However, if offset isn't explicitly reset to account for startHeight, it can lead to incorrect behavior in scenarios where startHeight is modified dynamically or when the API is used in a context where offset doesn’t align perfectly with startHeight.

My change ensures that the calculation explicitly accounts for startHeight, guaranteeing that the query starts from the intended block range every time. Without this adjustment, there’s a risk of missing or duplicating blocks in edge cases, especially when the API is reused with varying startHeight values.

I’d love to hear your thoughts on this and can provide additional examples if needed.

Copy link
Collaborator

Choose a reason for hiding this comment

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

scenarios where startHeight is modified dynamically

I don't see when or how this can happen. The script works as-is. We want to include blocks starting at start height and querying 100 blocks each time.

@fragchain fragchain requested a review from rach-id January 27, 2025 12:20
@rootulp rootulp closed this Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants