Skip to content

Commit

Permalink
chore: check if a block is empty by taking its reference (backport #4063
Browse files Browse the repository at this point in the history
) (#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]>
  • Loading branch information
mergify[bot] and rach-id authored Nov 27, 2024
1 parent 8495a2a commit b24feac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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 {
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

0 comments on commit b24feac

Please sign in to comment.