Skip to content

Commit

Permalink
Merge pull request #72 from ElrondNetwork/has-scheduled-for-header
Browse files Browse the repository at this point in the history
Has scheduled for header
  • Loading branch information
AdoAdoAdo authored Apr 6, 2022
2 parents 7817ba1 + 73a7bbc commit 7c0088a
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
5 changes: 5 additions & 0 deletions data/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions data/block/blockV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 23 additions & 0 deletions data/block/blockV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
17 changes: 17 additions & 0 deletions data/block/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

// 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())
}
17 changes: 17 additions & 0 deletions data/block/metaBlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 17 additions & 0 deletions data/block/metaBlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
1 change: 1 addition & 0 deletions data/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7c0088a

Please sign in to comment.