Skip to content

Commit

Permalink
Merge pull request #337 from multiversx/MX-15667-fix-epoch-start-poin…
Browse files Browse the repository at this point in the history
…ter-receivers

[sovereign] Fix epoch start pointer receivers interfaces
  • Loading branch information
mariusmihaic authored Dec 12, 2024
2 parents 16e4df5 + 5ea61e5 commit fcd76c0
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 17 deletions.
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 {
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())
}

0 comments on commit fcd76c0

Please sign in to comment.