From b24feac743182b33192d46ed42d8f89578c084ab Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 27 Nov 2024 16:27:24 +0000 Subject: [PATCH] chore: check if a block is empty by taking its reference (backport #4063) (#4064) This will be helpful in Celestia-node to avoid copying the block when checking if it's empty
This is an automatic backport of pull request #4063 done by [Mergify](https://mergify.com). Co-authored-by: CHAMI Rachid --- app/extend_block.go | 11 ++++++++++- app/test/integration_test.go | 15 +++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/extend_block.go b/app/extend_block.go index f14a764b26..a9c1bbe072 100644 --- a/app/extend_block.go +++ b/app/extend_block.go @@ -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 +} diff --git a/app/test/integration_test.go b/app/test/integration_test.go index aec6d86e46..7ee8e08d2d 100644 --- a/app/test/integration_test.go +++ b/app/test/integration_test.go @@ -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) } }