Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sovereign] Set outgoing miniblock slice with type in header #348

Open
wants to merge 14 commits into
base: MX-16438-update-bridge-outgoing-data
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 95 additions & 16 deletions data/block/sovereignChainHeader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math/big"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/headerVersionData"
)
Expand All @@ -33,23 +32,34 @@ func (sch *SovereignChainHeader) SetAdditionalData(_ headerVersionData.HeaderAdd

// ShallowClone returns a clone of the object
func (sch *SovereignChainHeader) ShallowClone() data.HeaderHandler {
if sch == nil || sch.Header == nil {
if sch == nil {
return nil
}

internalHeaderCopy := *sch.Header

headerCopy := *sch
headerCopy.Header = &internalHeaderCopy
if len(sch.OutGoingMiniBlockHeaders) != 0 {
headerCopy.OutGoingMiniBlockHeaders = copyOutGoingMBHeaders(sch.OutGoingMiniBlockHeaders)
}

if !check.IfNil(sch.OutGoingMiniBlockHeader) {
internalOutGoingMbHeader := *sch.OutGoingMiniBlockHeader
headerCopy.OutGoingMiniBlockHeader = &internalOutGoingMbHeader
if sch.Header != nil {
internalHeaderCopy := *sch.Header
headerCopy.Header = &internalHeaderCopy
}

return &headerCopy
}

func copyOutGoingMBHeaders(outGoingMiniBlockHeaders []*OutGoingMiniBlockHeader) []*OutGoingMiniBlockHeader {
ret := make([]*OutGoingMiniBlockHeader, len(outGoingMiniBlockHeaders))

for idx, outGoingMBHdr := range outGoingMiniBlockHeaders {
copyMB := *outGoingMBHdr
ret[idx] = &copyMB
}

return ret
}

// GetShardID returns internal header shard id
func (sch *SovereignChainHeader) GetShardID() uint32 {
if sch == nil {
Expand Down Expand Up @@ -549,33 +559,83 @@ func (sch *SovereignChainHeader) CheckFieldsForNil() error {
return nil
}

// GetOutGoingMiniBlockHeaderHandler returns the outgoing mini block header
func (sch *SovereignChainHeader) GetOutGoingMiniBlockHeaderHandler() data.OutGoingMiniBlockHeaderHandler {
// GetOutGoingMiniBlockHeaderHandlers returns the outgoing mini block headers
func (sch *SovereignChainHeader) GetOutGoingMiniBlockHeaderHandlers() []data.OutGoingMiniBlockHeaderHandler {
if sch == nil {
return nil
}

return sch.GetOutGoingMiniBlockHeader()
mbHeaders := sch.GetOutGoingMiniBlockHeaders()
mbHeaderHandlers := make([]data.OutGoingMiniBlockHeaderHandler, len(mbHeaders))

for i := range mbHeaders {
mbHeaderHandlers[i] = mbHeaders[i]
}

return mbHeaderHandlers
}

// SetOutGoingMiniBlockHeaderHandler returns the outgoing mini block header
// GetOutGoingMiniBlockHeaderHandler returns the outgoing mb header with specified type, if found
func (sch *SovereignChainHeader) GetOutGoingMiniBlockHeaderHandler(mbType int32) data.OutGoingMiniBlockHeaderHandler {
if sch == nil {
return nil
}

for _, outGoingMbHdr := range sch.OutGoingMiniBlockHeaders {
if int32(outGoingMbHdr.Type) == mbType {
return outGoingMbHdr
}
}

return nil
}

// SetOutGoingMiniBlockHeaderHandler replaces the outgoing mb from the internal outgoing mb slice, if found.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

... based on mb type ...

// Otherwise, it adds it add the end of the slice.
func (sch *SovereignChainHeader) SetOutGoingMiniBlockHeaderHandler(mbHeader data.OutGoingMiniBlockHeaderHandler) error {
if sch == nil {
return data.ErrNilPointerReceiver
}

if check.IfNil(mbHeader) {
sch.OutGoingMiniBlockHeader = nil
return nil
outGoingMbHdr := createOutGoingMbHeader(mbHeader)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You deleted the check.IfNil(mbHeader)

for idx, currOutGoingMbHdr := range sch.OutGoingMiniBlockHeaders {
if int32(currOutGoingMbHdr.Type) == mbHeader.GetOutGoingMBTypeInt32() {
sch.OutGoingMiniBlockHeaders[idx] = outGoingMbHdr
return nil
}
}

sch.OutGoingMiniBlockHeader = &OutGoingMiniBlockHeader{
sch.OutGoingMiniBlockHeaders = append(sch.OutGoingMiniBlockHeaders, outGoingMbHdr)
return nil
}

func createOutGoingMbHeader(mbHeader data.OutGoingMiniBlockHeaderHandler) *OutGoingMiniBlockHeader {
return &OutGoingMiniBlockHeader{
Type: OutGoingMBType(mbHeader.GetOutGoingMBTypeInt32()),
Hash: mbHeader.GetHash(),
OutGoingOperationsHash: mbHeader.GetOutGoingOperationsHash(),
AggregatedSignatureOutGoingOperations: mbHeader.GetAggregatedSignatureOutGoingOperations(),
LeaderSignatureOutGoingOperations: mbHeader.GetLeaderSignatureOutGoingOperations(),
}
}

// SetOutGoingMiniBlockHeaderHandlers sets the outgoing mini block headers
func (sch *SovereignChainHeader) SetOutGoingMiniBlockHeaderHandlers(mbHeaders []data.OutGoingMiniBlockHeaderHandler) error {
if sch == nil {
return data.ErrNilPointerReceiver
}

if len(mbHeaders) == 0 {
sch.OutGoingMiniBlockHeaders = nil
return nil
}

miniBlockHeaders := make([]*OutGoingMiniBlockHeader, len(mbHeaders))
for i, mbHeaderHandler := range mbHeaders {
miniBlockHeaders[i] = createOutGoingMbHeader(mbHeaderHandler)
}

sch.OutGoingMiniBlockHeaders = miniBlockHeaders
return nil
}

Expand Down Expand Up @@ -725,6 +785,25 @@ func (omb *OutGoingMiniBlockHeader) SetAggregatedSignatureOutGoingOperations(sig
return nil
}

// GetOutGoingMBTypeInt32 returns the outgoing mb header type as int32
func (omb *OutGoingMiniBlockHeader) GetOutGoingMBTypeInt32() int32 {
if omb == nil {
return 0
}

return int32(omb.Type)
}

// SetOutGoingMBTypeInt32 sets the mini block type
func (omb *OutGoingMiniBlockHeader) SetOutGoingMBTypeInt32(mbType int32) error {
if omb == nil {
return data.ErrNilPointerReceiver
}

omb.Type = OutGoingMBType(mbType)
return nil
}

// IsInterfaceNil checks if the underlying interface is nil
func (omb *OutGoingMiniBlockHeader) IsInterfaceNil() bool {
return omb == nil
Expand Down
Loading