From 8c03fe3e9688d243ad3d3784a9d0d4e345cff3ad Mon Sep 17 00:00:00 2001 From: AdoAdoAdo Date: Wed, 16 Mar 2022 12:49:54 +0200 Subject: [PATCH 1/3] add check for headers with scheduled miniBlocks --- data/block/block.go | 5 +++++ data/block/blockV2.go | 17 +++++++++++++++++ data/block/metaBlock.go | 17 +++++++++++++++++ data/interface.go | 1 + 4 files changed, 40 insertions(+) diff --git a/data/block/block.go b/data/block/block.go index 3c230e102..69833a5ab 100644 --- a/data/block/block.go +++ b/data/block/block.go @@ -561,6 +561,11 @@ func (h *Header) HasScheduledSupport() bool { return false } +// HasScheduledMiniBlocks returns false as v1 Header has no support for scheduled data +func (h *Header) HasScheduledMiniBlocks() bool { + return false +} + // GetAdditionalData gets the additional version-related data for the header func (h *Header) GetAdditionalData() headerVersionData.HeaderAdditionalData { // no extra data for the initial version of shard block header diff --git a/data/block/blockV2.go b/data/block/blockV2.go index 923b08076..4aa1ec3b6 100644 --- a/data/block/blockV2.go +++ b/data/block/blockV2.go @@ -473,6 +473,23 @@ func (hv2 *HeaderV2) HasScheduledSupport() bool { return true } +// HasScheduledMiniBlocks returns true if the header has scheduled miniBlock headers +func (hv2 *HeaderV2) HasScheduledMiniBlocks() bool { + if hv2 == nil { + return false + } + + mbHeaderHandlers := hv2.GetMiniBlockHeaderHandlers() + for _, mbHeader := range mbHeaderHandlers { + processingType := ProcessingType(mbHeader.GetProcessingType()) + if processingType == Scheduled { + return true + } + } + + return false +} + // SetScheduledRootHash sets the scheduled root hash func (hv2 *HeaderV2) SetScheduledRootHash(rootHash []byte) error { if hv2 == nil { diff --git a/data/block/metaBlock.go b/data/block/metaBlock.go index 9ff547d02..041dc9010 100644 --- a/data/block/metaBlock.go +++ b/data/block/metaBlock.go @@ -397,6 +397,23 @@ func (m *MetaBlock) HasScheduledSupport() bool { return false } +// HasScheduledMiniBlocks returns true if the metaBlock holds scheduled miniBlocks +func (m *MetaBlock) HasScheduledMiniBlocks() bool { + if m == nil { + return false + } + + mbHeaderHandlers := m.GetMiniBlockHeaderHandlers() + for _, mbHeader := range mbHeaderHandlers { + processingType := ProcessingType(mbHeader.GetProcessingType()) + if processingType == Scheduled { + return true + } + } + + return false +} + // SetMiniBlockHeaderHandlers sets the miniBlock headers from the given miniBlock header handlers func (m *MetaBlock) SetMiniBlockHeaderHandlers(mbHeaderHandlers []data.MiniBlockHeaderHandler) error { if m == nil { diff --git a/data/interface.go b/data/interface.go index b2e24f881..1ef3280e5 100644 --- a/data/interface.go +++ b/data/interface.go @@ -56,6 +56,7 @@ type HeaderHandler interface { GetMiniBlockHeaderHandlers() []MiniBlockHeaderHandler HasScheduledSupport() bool GetAdditionalData() headerVersionData.HeaderAdditionalData + HasScheduledMiniBlocks() bool SetAccumulatedFees(value *big.Int) error SetDeveloperFees(value *big.Int) error From b5ee1f2658970e8f28cb835d1053770f507c805f Mon Sep 17 00:00:00 2001 From: AdoAdoAdo Date: Wed, 16 Mar 2022 14:57:54 +0200 Subject: [PATCH 2/3] data: add unit tests --- data/block/blockV2_test.go | 23 +++++++++++++++++++++++ data/block/block_test.go | 17 +++++++++++++++++ data/block/metaBlock_test.go | 17 +++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/data/block/blockV2_test.go b/data/block/blockV2_test.go index 33664b540..6503f92d1 100644 --- a/data/block/blockV2_test.go +++ b/data/block/blockV2_test.go @@ -1194,3 +1194,26 @@ func TestHeaderV2_SetAdditionalDataShouldWork(t *testing.T) { require.Equal(t, gasRefunded, shardBlock.GetScheduledGasRefunded()) require.Equal(t, gasProvided, shardBlock.GetScheduledGasProvided()) } + +func TestHeaderV2_HasScheduledMiniBlocks(t *testing.T) { + t.Parallel() + + mbh := &block.MiniBlockHeader{} + _ = mbh.SetProcessingType(int32(block.Scheduled)) + + shardBlock := &block.HeaderV2{ + Header: &block.Header{ + MiniBlockHeaders: []block.MiniBlockHeader{*mbh}, + }, + } + require.True(t, shardBlock.HasScheduledMiniBlocks()) + + _ = mbh.SetProcessingType(int32(block.Normal)) + shardBlock = &block.HeaderV2{ + Header: &block.Header{ + MiniBlockHeaders: []block.MiniBlockHeader{*mbh}, + }, + } + + require.False(t, shardBlock.HasScheduledMiniBlocks()) +} diff --git a/data/block/block_test.go b/data/block/block_test.go index 4958848a1..71c4b82d1 100644 --- a/data/block/block_test.go +++ b/data/block/block_test.go @@ -806,3 +806,20 @@ func TestHeader_GetAdditionalData(t *testing.T) { additionalData := h.GetAdditionalData() assert.Nil(t, additionalData) } + +func TestHeader_HasScheduledMiniBlocks(t *testing.T) { + t.Parallel() + + h := &block.Header{} + require.False(t, h.HasScheduledMiniBlocks()) + + mbHeader := &block.MiniBlockHeader{} + _ = mbHeader.SetProcessingType(int32(block.Normal)) + h.MiniBlockHeaders = []block.MiniBlockHeader{*mbHeader} + require.False(t, h.HasScheduledMiniBlocks()) + + // not suppoerted for v1 header, so it should return false + _ = mbHeader.SetProcessingType(int32(block.Scheduled)) + h.MiniBlockHeaders = []block.MiniBlockHeader{*mbHeader} + require.False(t, h.HasScheduledMiniBlocks()) +} diff --git a/data/block/metaBlock_test.go b/data/block/metaBlock_test.go index 56ec35cd3..3ed724b60 100644 --- a/data/block/metaBlock_test.go +++ b/data/block/metaBlock_test.go @@ -385,3 +385,20 @@ func TestMetaBlock_SetAdditionalDataShouldDoNothing(t *testing.T) { err := metaBlock.SetAdditionalData(&headerVersionData.AdditionalData{}) require.Nil(t, err) } + +func TestMetaBlock_HasScheduledMiniBlocks(t *testing.T) { + t.Parallel() + + metaBlock := &block.MetaBlock{} + require.False(t, metaBlock.HasScheduledMiniBlocks()) + + mbHeader := &block.MiniBlockHeader{} + _ = mbHeader.SetProcessingType(int32(block.Normal)) + metaBlock.MiniBlockHeaders = []block.MiniBlockHeader{*mbHeader} + require.False(t, metaBlock.HasScheduledMiniBlocks()) + + _ = mbHeader.SetProcessingType(int32(block.Scheduled)) + metaBlock.MiniBlockHeaders = []block.MiniBlockHeader{*mbHeader} + + require.True(t, metaBlock.HasScheduledMiniBlocks()) +} From 73a7bbc959da2d7462563e1036ed36ee0ff29e2a Mon Sep 17 00:00:00 2001 From: AdoAdoAdo Date: Wed, 16 Mar 2022 15:08:59 +0200 Subject: [PATCH 3/3] fix comment --- data/block/block_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/block/block_test.go b/data/block/block_test.go index 71c4b82d1..4105b7a2a 100644 --- a/data/block/block_test.go +++ b/data/block/block_test.go @@ -818,7 +818,7 @@ func TestHeader_HasScheduledMiniBlocks(t *testing.T) { h.MiniBlockHeaders = []block.MiniBlockHeader{*mbHeader} require.False(t, h.HasScheduledMiniBlocks()) - // not suppoerted for v1 header, so it should return false + // scheduled miniBlocks not supported for v1 header, so it should return false _ = mbHeader.SetProcessingType(int32(block.Scheduled)) h.MiniBlockHeaders = []block.MiniBlockHeader{*mbHeader} require.False(t, h.HasScheduledMiniBlocks())