diff --git a/data/block/block.go b/data/block/block.go index 0ac8fd2a..b1b305a8 100644 --- a/data/block/block.go +++ b/data/block/block.go @@ -14,7 +14,7 @@ var _ = data.HeaderHandler(&Header{}) var _ = data.ShardHeaderHandler(&Header{}) // MiniBlockSlice should be used when referring to subset of mini blocks that is not -// necessarily representing a full block body +// necessarily representing a full block body type MiniBlockSlice []*MiniBlock // MiniblockAndHash holds the info related to a miniblock and its hash @@ -278,6 +278,17 @@ func (h *Header) ShallowClone() data.HeaderHandler { return &headerCopy } +// SetBlockBodyTypeInt32 sets the blockBodyType in the header +func (h *Header) SetBlockBodyTypeInt32(blockBodyType int32) error { + if h == nil { + return data.ErrNilPointerReceiver + } + + h.BlockBodyType = Type(blockBodyType) + + return nil +} + // IsInterfaceNil returns true if there is no value under the interface func (h *Header) IsInterfaceNil() bool { return h == nil diff --git a/data/block/blockV2.go b/data/block/blockV2.go index 0a690e2e..4fb87a15 100644 --- a/data/block/blockV2.go +++ b/data/block/blockV2.go @@ -401,6 +401,15 @@ func (hv2 *HeaderV2) ShallowClone() data.HeaderHandler { return &headerCopy } +// SetBlockBodyTypeInt32 sets the blockBodyType in the header +func (hv2 *HeaderV2) SetBlockBodyTypeInt32(blockBodyType int32) error { + if hv2 == nil { + return data.ErrNilPointerReceiver + } + + return hv2.Header.SetBlockBodyTypeInt32(blockBodyType) +} + // IsInterfaceNil returns true if there is no value under the interface func (hv2 *HeaderV2) IsInterfaceNil() bool { return hv2 == nil diff --git a/data/block/blockV2_test.go b/data/block/blockV2_test.go index a0e28f58..6cedd08a 100644 --- a/data/block/blockV2_test.go +++ b/data/block/blockV2_test.go @@ -1217,3 +1217,29 @@ func TestHeaderV2_HasScheduledMiniBlocks(t *testing.T) { require.False(t, shardBlock.HasScheduledMiniBlocks()) } + +func TestHeaderV2_SetBlockBodyTypeInt32(t *testing.T) { + t.Parallel() + + t.Run("nil header should error", func(t *testing.T) { + t.Parallel() + + var header *block.HeaderV2 + err := header.SetBlockBodyTypeInt32(int32(block.ReceiptBlock)) + require.Equal(t, data.ErrNilPointerReceiver, err) + }) + t.Run("should work", func(t *testing.T) { + t.Parallel() + + header := &block.HeaderV2{ + Header: &block.Header{}, + } + err := header.SetBlockBodyTypeInt32(int32(block.ReceiptBlock)) + require.Nil(t, err) + require.Equal(t, int32(block.ReceiptBlock), header.GetBlockBodyTypeInt32()) + + err = header.SetBlockBodyTypeInt32(int32(block.TxBlock)) + require.Nil(t, err) + require.Equal(t, int32(block.TxBlock), header.GetBlockBodyTypeInt32()) + }) +} diff --git a/data/block/block_test.go b/data/block/block_test.go index 980543b2..c83d88e7 100644 --- a/data/block/block_test.go +++ b/data/block/block_test.go @@ -845,3 +845,27 @@ func TestMiniBlockHeader_GetMiniBlockHeaderReservedShouldErrWhenReservedFieldIsN assert.Nil(t, mbhr) assert.Equal(t, data.ErrNilReservedField, err) } + +func TestHeader_SetBlockBodyTypeInt32(t *testing.T) { + t.Parallel() + + t.Run("nil header should error", func(t *testing.T) { + t.Parallel() + + var header *block.Header + err := header.SetBlockBodyTypeInt32(int32(block.ReceiptBlock)) + require.Equal(t, data.ErrNilPointerReceiver, err) + }) + t.Run("should work", func(t *testing.T) { + t.Parallel() + + header := &block.Header{} + err := header.SetBlockBodyTypeInt32(int32(block.ReceiptBlock)) + require.Nil(t, err) + require.Equal(t, block.ReceiptBlock, header.BlockBodyType) + + err = header.SetBlockBodyTypeInt32(int32(block.TxBlock)) + require.Nil(t, err) + require.Equal(t, block.TxBlock, header.BlockBodyType) + }) +} diff --git a/data/interface.go b/data/interface.go index 80b039da..97f5ecd7 100644 --- a/data/interface.go +++ b/data/interface.go @@ -95,6 +95,7 @@ type ShardHeaderHandler interface { GetBlockBodyTypeInt32() int32 SetMetaBlockHashes(hashes [][]byte) error MapMiniBlockHashesToShards() map[string]uint32 + SetBlockBodyTypeInt32(blockBodyType int32) error } // MetaHeaderHandler defines getters and setters for the meta block header