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: check if a block is empty by taking its reference #4063

Merged
merged 2 commits into from
Nov 27, 2024

Conversation

rach-id
Copy link
Member

@rach-id rach-id commented Nov 27, 2024

This will be helpful in Celestia-node to avoid copying the block when checking if it's empty

@rach-id rach-id added chore optional label for items that follow the `chore` conventional commit backport:v3.x PR will be backported automatically to the v3.x branch upon merging labels Nov 27, 2024
@rach-id rach-id self-assigned this Nov 27, 2024
@rach-id rach-id requested a review from a team as a code owner November 27, 2024 15:47
@rach-id rach-id requested review from cmwaters and evan-forbes and removed request for a team November 27, 2024 15:47
@celestia-bot celestia-bot requested a review from a team November 27, 2024 15:47
rootulp
rootulp previously approved these changes Nov 27, 2024
Copy link
Contributor

coderabbitai bot commented Nov 27, 2024

Caution

Review failed

The pull request is closed.

📝 Walkthrough

Walkthrough

The changes involve modifications to the IsEmptyBlock function in app/extend_block.go, which has been renamed and supplemented with a new function called IsEmptyBlockRef. The original function now includes a deprecation notice. In addition, the integration tests in app/test/integration_test.go have been updated to reflect these changes, with the TestEmptyBlock method renamed to TestIsEmptyBlock and a new test method TestIsEmptyBlockRef added.

Changes

File Change Summary
app/extend_block.go - Renamed IsEmptyBlock to IsEmptyBlock (no functional change).
- Added IsEmptyBlockRef function.
- Original IsEmptyBlock now includes a deprecation notice.
app/test/integration_test.go - Renamed TestEmptyBlock to TestIsEmptyBlock (no functional change).
- Added TestIsEmptyBlockRef to test the new IsEmptyBlockRef function.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ExtendBlock
    participant Data

    Client->>ExtendBlock: Call IsEmptyBlock
    ExtendBlock->>Data: Check if transactions are empty
    Data-->>ExtendBlock: Return empty status
    ExtendBlock-->>Client: Return empty status

    Client->>ExtendBlock: Call IsEmptyBlockRef
    ExtendBlock->>Data: Check if transactions are empty (by reference)
    Data-->>ExtendBlock: Return empty status
    ExtendBlock-->>Client: Return empty status
Loading

Warning

Rate limit exceeded

@rach-id has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 45 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 593fc86 and d4d9237.


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

🧹 Outside diff range and nitpick comments (4)
app/extend_block.go (2)

28-34: Consider enhancing the deprecation notice and reviewing the version parameter.

The deprecation notice is good, but could be more specific about when this will be removed. Also, the unused version parameter (uint64) might indicate a design consideration that should be documented or removed if not needed.

Consider updating the deprecation notice:

-// Deprecated: Use IsEmptyBlockRef for better performance with large data structures.
+// Deprecated: Use IsEmptyBlockRef for better performance with large data structures.
+// This function will be removed in the next major version (v4.0.0).

If the version parameter is truly unused across all implementations, consider removing it:

-func IsEmptyBlock(data coretypes.Data, _ uint64) bool {
+func IsEmptyBlock(data coretypes.Data) bool {

28-41: Consider providing migration guidance and benchmarks.

Since this is a core package functionality, it would be valuable to:

  1. Document the expected performance improvement with some benchmark numbers
  2. Provide migration examples for existing users
  3. Consider adding these functions to a more specific package (e.g., blockutils) in a future refactoring

Would you like me to help create a benchmark test to quantify the performance improvement?

app/test/integration_test.go (2)

278-287: Consider deprecating this test in favor of TestIsEmptyBlockRef

The staticcheck suppression indicates this test is using a deprecated method. Since we now have TestIsEmptyBlockRef testing the new implementation, we should:

  1. Add a TODO comment indicating when this test will be removed
  2. Document why we're keeping both tests temporarily
 func (s *IntegrationTestSuite) TestIsEmptyBlock() {
+    // TODO: Remove this test once IsEmptyBlock is removed in the next major version
+    // Keeping this temporarily to ensure backward compatibility during transition
     t := s.T()
     emptyHeights := []int64{1, 2, 3}

289-297: Consider adding negative test cases

The test effectively validates empty blocks, but could be more comprehensive. Consider adding:

  1. Test cases with non-empty blocks to verify false negatives
  2. Edge cases like blocks with empty transactions array vs nil
 func (s *IntegrationTestSuite) TestIsEmptyBlockRef() {
     t := s.T()
+    testCases := []struct {
+        name     string
+        height   int64
+        expected bool
+    }{
+        {"empty block", 1, true},
+        {"block with transactions", 4, false},  // Ensure this height has transactions
+    }
-    emptyHeights := []int64{1, 2, 3}
-    for _, h := range emptyHeights {
+    for _, tc := range testCases {
+        s.T().Run(tc.name, func(t *testing.T) {
-            blockRes, err := s.cctx.Client.Block(s.cctx.GoContext(), &h)
+            blockRes, err := s.cctx.Client.Block(s.cctx.GoContext(), &tc.height)
             require.NoError(t, err)
-            require.True(t, app.IsEmptyBlockRef(&blockRes.Block.Data, blockRes.Block.Header.Version.App))
+            result := app.IsEmptyBlockRef(&blockRes.Block.Data, blockRes.Block.Header.Version.App)
+            require.Equal(t, tc.expected, result)
             ExtendBlockTest(t, blockRes.Block)
+        })
     }
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 593fc86 and d4d9237.

📒 Files selected for processing (2)
  • app/extend_block.go (1 hunks)
  • app/test/integration_test.go (1 hunks)

Comment on lines +36 to +41
// IsEmptyBlockRef returns true if the application considers the given block data
// empty at a given version.
// This method passes the block data by reference for improved performance.
func IsEmptyBlockRef(data *coretypes.Data, _ uint64) bool {
return len(data.Txs) == 0
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Add nil pointer check and enhance documentation.

While the implementation is correct, there are a few improvements that could make it more robust and informative:

  1. Add nil pointer check for safety
  2. Document the performance benefit more specifically
  3. Consider the unused version parameter

Consider this enhanced implementation:

 // IsEmptyBlockRef returns true if the application considers the given block data
 // empty at a given version.
-// This method passes the block data by reference for improved performance.
+// This method passes the block data by reference to avoid copying large data structures,
+// particularly beneficial when dealing with blocks containing many transactions.
 func IsEmptyBlockRef(data *coretypes.Data, _ uint64) bool {
+    if data == nil {
+        return true
+    }
     return len(data.Txs) == 0
 }
📝 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
// IsEmptyBlockRef returns true if the application considers the given block data
// empty at a given version.
// This method passes the block data by reference for improved performance.
func IsEmptyBlockRef(data *coretypes.Data, _ uint64) bool {
return len(data.Txs) == 0
}
// IsEmptyBlockRef returns true if the application considers the given block data
// empty at a given version.
// This method passes the block data by reference to avoid copying large data structures,
// particularly beneficial when dealing with blocks containing many transactions.
func IsEmptyBlockRef(data *coretypes.Data, _ uint64) bool {
if data == nil {
return true
}
return len(data.Txs) == 0
}

@rootulp rootulp merged commit 2ca0feb into main Nov 27, 2024
28 of 29 checks passed
@rootulp rootulp deleted the ref-is-empty-block branch November 27, 2024 16:09
mergify bot pushed a commit that referenced this pull request Nov 27, 2024
This will be helpful in Celestia-node to avoid copying the block when
checking if it's empty

(cherry picked from commit 2ca0feb)
// IsEmptyBlockRef returns true if the application considers the given block data
// empty at a given version.
// This method passes the block data by reference for improved performance.
func IsEmptyBlockRef(data *coretypes.Data, _ uint64) bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the second argument for? Do we still need to keep it around? Also if we're not planning on backporting this can we just break the go API?

Copy link
Member Author

Choose a reason for hiding this comment

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

what's the second argument for?

in case at some point we want to have specific method for each version. For now, it's not used and not sure why it was added in first place

Also if we're not planning on backporting this can we just break the go API?

I'm backporting this to v3 to be released and used in node

Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. It's for app version
  2. We are backporting it

rootulp pushed a commit that referenced this pull request Nov 27, 2024
) (#4064)

This will be helpful in Celestia-node to avoid copying the block when
checking if it's empty<hr>This is an automatic backport of pull request
#4063 done by [Mergify](https://mergify.com).

Co-authored-by: CHAMI Rachid <[email protected]>
Comment on lines +38 to +39
// This method passes the block data by reference for improved performance.
func IsEmptyBlockRef(data *coretypes.Data, _ uint64) bool {
Copy link
Member

@Wondertan Wondertan Nov 27, 2024

Choose a reason for hiding this comment

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

nit: there are no references in Golang, only pointers

see unsafe.Pointer or reflect.PointerTo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:v3.x PR will be backported automatically to the v3.x branch upon merging chore optional label for items that follow the `chore` conventional commit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants