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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/extend_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,17 @@ func ExtendBlock(data coretypes.Data, appVersion uint64) (*rsmt2d.ExtendedDataSq
return da.ExtendShares(share.ToBytes(dataSquare))
}

// EmptyBlock returns true if the given block data is considered empty by the
// IsEmptyBlock returns true if the given block data is considered empty by the
// application at a given version.
//
// Deprecated: Use IsEmptyBlockRef for better performance with large data structures.
func IsEmptyBlock(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 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

Comment on lines +38 to +39
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

return len(data.Txs) == 0
}
Comment on lines +36 to +41
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
}

15 changes: 13 additions & 2 deletions app/test/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,24 @@ func ExtendBlockTest(t *testing.T, block *coretypes.Block) {
}
}

func (s *IntegrationTestSuite) TestEmptyBlock() {
func (s *IntegrationTestSuite) TestIsEmptyBlock() {
t := s.T()
emptyHeights := []int64{1, 2, 3}
for _, h := range emptyHeights {
blockRes, err := s.cctx.Client.Block(s.cctx.GoContext(), &h)
require.NoError(t, err)
require.True(t, app.IsEmptyBlock(blockRes.Block.Data, blockRes.Block.Header.Version.App))
require.True(t, app.IsEmptyBlock(blockRes.Block.Data, blockRes.Block.Header.Version.App)) //nolint:staticcheck
ExtendBlockTest(t, blockRes.Block)
}
}

func (s *IntegrationTestSuite) TestIsEmptyBlockRef() {
t := s.T()
emptyHeights := []int64{1, 2, 3}
for _, h := range emptyHeights {
blockRes, err := s.cctx.Client.Block(s.cctx.GoContext(), &h)
require.NoError(t, err)
require.True(t, app.IsEmptyBlockRef(&blockRes.Block.Data, blockRes.Block.Header.Version.App))
ExtendBlockTest(t, blockRes.Block)
}
}
Expand Down
Loading