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] Fix epoch start pointer receivers interfaces #337

Merged
Merged
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
121 changes: 104 additions & 17 deletions data/block/sovereignChainHeader.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,23 +638,7 @@ func (sch *SovereignChainHeader) GetEpochStartHandler() data.EpochStartHandler {
return nil
}

lastFinalizedCrossChainHeaderData := EpochStartShardData{
ShardID: sch.EpochStart.LastFinalizedCrossChainHeader.ShardID,
Epoch: sch.EpochStart.LastFinalizedCrossChainHeader.Epoch,
Round: sch.EpochStart.LastFinalizedCrossChainHeader.Round,
Nonce: sch.EpochStart.LastFinalizedCrossChainHeader.Nonce,
HeaderHash: sch.EpochStart.LastFinalizedCrossChainHeader.HeaderHash,
}

epochStartShardData := make([]EpochStartShardData, 0)
if lastFinalizedCrossChainHeaderData.ShardID == core.MainChainShardId {
epochStartShardData = append(epochStartShardData, lastFinalizedCrossChainHeaderData)
}

return &EpochStart{
LastFinalizedHeaders: epochStartShardData,
Economics: sch.EpochStart.Economics,
}
return &sch.EpochStart
}

// GetLastFinalizedCrossChainHeaderHandler returns the last finalized cross chain header data
Expand Down Expand Up @@ -783,3 +767,106 @@ func (essd *EpochStartCrossChainData) SetHeaderHash(hash []byte) error {

return nil
}

// GetRootHash returns nothing
func (essd *EpochStartCrossChainData) GetRootHash() []byte {
return nil
}

// GetFirstPendingMetaBlock returns nothing
func (essd *EpochStartCrossChainData) GetFirstPendingMetaBlock() []byte {
return nil
}

// GetLastFinishedMetaBlock returns nothing
func (essd *EpochStartCrossChainData) GetLastFinishedMetaBlock() []byte {
return nil
}

// GetPendingMiniBlockHeaderHandlers returns empty slice
func (essd *EpochStartCrossChainData) GetPendingMiniBlockHeaderHandlers() []data.MiniBlockHeaderHandler {
return make([]data.MiniBlockHeaderHandler, 0)
}

// SetRootHash does nothing
func (essd *EpochStartCrossChainData) SetRootHash([]byte) error {
return nil
}

// SetFirstPendingMetaBlock does nothing
func (essd *EpochStartCrossChainData) SetFirstPendingMetaBlock([]byte) error {
return nil
}

// SetLastFinishedMetaBlock does nothing
func (essd *EpochStartCrossChainData) SetLastFinishedMetaBlock([]byte) error {
return nil
}

// SetPendingMiniBlockHeaders does nothing
func (essd *EpochStartCrossChainData) SetPendingMiniBlockHeaders(_ []data.MiniBlockHeaderHandler) error {
return nil
}

// GetLastFinalizedHeaderHandlers returns last cross main chain finalized header in a slice w.r.t to the interface
func (m *EpochStartSovereign) GetLastFinalizedHeaderHandlers() []data.EpochStartShardDataHandler {
if m == nil {
return nil
}

epochStartShardData := make([]data.EpochStartShardDataHandler, 0)
if m.LastFinalizedCrossChainHeader.ShardID == core.MainChainShardId {
epochStartShardData = append(epochStartShardData, &m.LastFinalizedCrossChainHeader)
}

return epochStartShardData
}

// GetEconomicsHandler returns the economics
func (m *EpochStartSovereign) GetEconomicsHandler() data.EconomicsHandler {
if m == nil {
return nil
}

return &m.Economics
}

// SetLastFinalizedHeaders sets epoch start data for main chain chain only
func (m *EpochStartSovereign) SetLastFinalizedHeaders(epochStartShardDataHandlers []data.EpochStartShardDataHandler) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is not used anywhere in mx-chain-go

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Doesn't matter, I had to follow the interface for EpochStart, which should have getters and setters for all fields.

if m == nil {
return data.ErrNilPointerReceiver
}

for _, epochStartShardData := range epochStartShardDataHandlers {
if epochStartShardData.GetShardID() == core.MainChainShardId {
m.LastFinalizedCrossChainHeader = EpochStartCrossChainData{
ShardID: epochStartShardData.GetShardID(),
Epoch: epochStartShardData.GetEpoch(),
Round: epochStartShardData.GetRound(),
Nonce: epochStartShardData.GetNonce(),
HeaderHash: epochStartShardData.GetHeaderHash(),
}
}
}

return nil
}

// SetEconomics sets economics
func (m *EpochStartSovereign) SetEconomics(economicsHandler data.EconomicsHandler) error {
if m == nil {
return data.ErrNilPointerReceiver
}

ec, ok := economicsHandler.(*Economics)
if !ok {
return data.ErrInvalidTypeAssertion
}
if ec == nil {
return data.ErrNilPointerDereference
}

m.Economics = *ec

return nil
}
67 changes: 67 additions & 0 deletions data/block/sovereignChainHeader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package block

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
)

func TestSovereignChainHeader_GetEpochStartHandler(t *testing.T) {
t.Parallel()

economics := Economics{
NodePrice: big.NewInt(100),
}
epochStartData := EpochStartCrossChainData{
Round: 4,
}
epochStartSov := EpochStartSovereign{
Economics: economics,
LastFinalizedCrossChainHeader: epochStartData,
}
sovHdr := &SovereignChainHeader{
EpochStart: epochStartSov,
}

require.Equal(t, &epochStartSov, sovHdr.GetEpochStartHandler())
require.Equal(t, &economics, sovHdr.GetEpochStartHandler().GetEconomicsHandler())
require.Empty(t, sovHdr.GetEpochStartHandler().GetLastFinalizedHeaderHandlers())

epochStartData.ShardID = 8
err := sovHdr.GetEpochStartHandler().SetLastFinalizedHeaders([]data.EpochStartShardDataHandler{&epochStartData})
require.Nil(t, err)
require.Empty(t, sovHdr.GetEpochStartHandler().GetLastFinalizedHeaderHandlers())

epochStartData.ShardID = core.MainChainShardId
epochStartSov.LastFinalizedCrossChainHeader = epochStartData
err = sovHdr.GetEpochStartHandler().SetLastFinalizedHeaders([]data.EpochStartShardDataHandler{&epochStartData})
require.Nil(t, err)

require.Equal(t, []data.EpochStartShardDataHandler{&epochStartData}, sovHdr.GetEpochStartHandler().GetLastFinalizedHeaderHandlers())
require.Equal(t, &epochStartSov, sovHdr.GetEpochStartHandler())
}

func TestSovereignChainHeader_GetEconomicsHandler(t *testing.T) {
t.Parallel()

economics := Economics{
NodePrice: big.NewInt(100),
}
sovHdr := &SovereignChainHeader{
EpochStart: EpochStartSovereign{
Economics: economics,
},
}
require.Equal(t, &economics, sovHdr.GetEpochStartHandler().GetEconomicsHandler())

economics2 := Economics{
NodePrice: big.NewInt(200),
}
err := sovHdr.GetEpochStartHandler().SetEconomics(&economics2)
require.Nil(t, err)
require.Equal(t, &economics2, sovHdr.GetEpochStartHandler().GetEconomicsHandler())
}
Loading