From 057bcad4b7a842f06c03a07e4e47ee49ef462a3b Mon Sep 17 00:00:00 2001 From: Marius C Date: Mon, 9 Dec 2024 16:17:03 +0200 Subject: [PATCH 1/4] FEAT: Extend sovereign EpochStart data interface with correct pointer receiver --- data/block/sovereignChainHeader.go | 157 ++++++++++++++++++++++++++++- data/interface.go | 2 +- 2 files changed, 155 insertions(+), 4 deletions(-) diff --git a/data/block/sovereignChainHeader.go b/data/block/sovereignChainHeader.go index fdeed3e5..5526e4f3 100644 --- a/data/block/sovereignChainHeader.go +++ b/data/block/sovereignChainHeader.go @@ -5,6 +5,7 @@ import ( "fmt" "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" @@ -637,10 +638,20 @@ func (sch *SovereignChainHeader) GetEpochStartHandler() data.EpochStartHandler { return nil } - return &EpochStart{ - LastFinalizedHeaders: make([]EpochStartShardData, 0), - Economics: sch.EpochStart.Economics, + 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 &sch.EpochStart } // GetShardInfoHandlers returns empty slice @@ -705,3 +716,143 @@ func (omb *OutGoingMiniBlockHeader) SetAggregatedSignatureOutGoingOperations(sig func (omb *OutGoingMiniBlockHeader) IsInterfaceNil() bool { return omb == nil } + +// SetShardID sets the epoch start shardID +func (essd *EpochStartCrossChainData) SetShardID(shardID uint32) error { + if essd == nil { + return data.ErrNilPointerReceiver + } + + essd.ShardID = shardID + + return nil +} + +// SetEpoch sets the epoch start epoch +func (essd *EpochStartCrossChainData) SetEpoch(epoch uint32) error { + if essd == nil { + return data.ErrNilPointerReceiver + } + + essd.Epoch = epoch + + return nil +} + +// SetRound sets the epoch start round +func (essd *EpochStartCrossChainData) SetRound(round uint64) error { + if essd == nil { + return data.ErrNilPointerReceiver + } + + essd.Round = round + + return nil +} + +// SetNonce sets the epoch start nonce +func (essd *EpochStartCrossChainData) SetNonce(nonce uint64) error { + if essd == nil { + return data.ErrNilPointerReceiver + } + + essd.Nonce = nonce + + return nil +} + +// SetHeaderHash sets the epoch start header hash +func (essd *EpochStartCrossChainData) SetHeaderHash(hash []byte) error { + if essd == nil { + return data.ErrNilPointerReceiver + } + + essd.HeaderHash = hash + + return nil +} + +func (essd *EpochStartCrossChainData) GetRootHash() []byte { + return nil +} +func (essd *EpochStartCrossChainData) GetFirstPendingMetaBlock() []byte { + return nil +} +func (essd *EpochStartCrossChainData) GetLastFinishedMetaBlock() []byte { + return nil +} +func (essd *EpochStartCrossChainData) GetPendingMiniBlockHeaderHandlers() []data.MiniBlockHeaderHandler { + return make([]data.MiniBlockHeaderHandler, 0) +} + +func (essd *EpochStartCrossChainData) SetRootHash([]byte) error { + return nil +} +func (essd *EpochStartCrossChainData) SetFirstPendingMetaBlock([]byte) error { + return nil +} +func (essd *EpochStartCrossChainData) SetLastFinishedMetaBlock([]byte) error { + return nil +} +func (essd *EpochStartCrossChainData) SetPendingMiniBlockHeaders(_ []data.MiniBlockHeaderHandler) error { + return nil +} + +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 +} + +func (m *EpochStartSovereign) GetEconomicsHandler() data.EconomicsHandler { + if m == nil { + return nil + } + + return &m.Economics +} + +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 data.ErrNilPointerReceiver +} + +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 +} diff --git a/data/interface.go b/data/interface.go index 31d349a1..a6cf9da7 100644 --- a/data/interface.go +++ b/data/interface.go @@ -309,7 +309,7 @@ type BodyHandler interface { Clone() BodyHandler // IntegrityAndValidity checks the integrity and validity of the block IntegrityAndValidity() error - // IsInterfaceNil returns true if there is no value under the interface + // IsInterfaceNil returns true if there is no valkue under the interface IsInterfaceNil() bool } From 1a8ee42b2c323e88a2c5c2a6213dd6e0ac48f0a0 Mon Sep 17 00:00:00 2001 From: Marius C Date: Mon, 9 Dec 2024 16:22:15 +0200 Subject: [PATCH 2/4] CLN: After merge + comms --- data/block/sovereignChainHeader.go | 33 +++++++++++++++++------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/data/block/sovereignChainHeader.go b/data/block/sovereignChainHeader.go index 75b773ca..09d7941d 100644 --- a/data/block/sovereignChainHeader.go +++ b/data/block/sovereignChainHeader.go @@ -638,19 +638,6 @@ 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 &sch.EpochStart } @@ -781,32 +768,47 @@ 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 @@ -820,6 +822,7 @@ func (m *EpochStartSovereign) GetLastFinalizedHeaderHandlers() []data.EpochStart return epochStartShardData } +// GetEconomicsHandler returns the economics func (m *EpochStartSovereign) GetEconomicsHandler() data.EconomicsHandler { if m == nil { return nil @@ -828,6 +831,7 @@ func (m *EpochStartSovereign) GetEconomicsHandler() data.EconomicsHandler { 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 @@ -845,9 +849,10 @@ func (m *EpochStartSovereign) SetLastFinalizedHeaders(epochStartShardDataHandler } } - return data.ErrNilPointerReceiver + return nil } +// SetEconomics sets economics func (m *EpochStartSovereign) SetEconomics(economicsHandler data.EconomicsHandler) error { if m == nil { return data.ErrNilPointerReceiver From 762196f8dd066b3ae6becaa6a92b1e240b918b9a Mon Sep 17 00:00:00 2001 From: Marius C Date: Mon, 9 Dec 2024 16:24:14 +0200 Subject: [PATCH 3/4] FIX: Typo --- data/interface.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/interface.go b/data/interface.go index d7040b8a..ee2f0ed0 100644 --- a/data/interface.go +++ b/data/interface.go @@ -317,7 +317,7 @@ type BodyHandler interface { Clone() BodyHandler // IntegrityAndValidity checks the integrity and validity of the block IntegrityAndValidity() error - // IsInterfaceNil returns true if there is no valkue under the interface + // IsInterfaceNil returns true if there is no value under the interface IsInterfaceNil() bool } From 5ea61e5dfe4b072742e9e4b52084fb48da96921b Mon Sep 17 00:00:00 2001 From: Marius C Date: Wed, 11 Dec 2024 11:14:01 +0200 Subject: [PATCH 4/4] FEAT: Add unit tests sovereignChainHeader_test.go --- data/block/sovereignChainHeader_test.go | 67 +++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 data/block/sovereignChainHeader_test.go diff --git a/data/block/sovereignChainHeader_test.go b/data/block/sovereignChainHeader_test.go new file mode 100644 index 00000000..fa6535c2 --- /dev/null +++ b/data/block/sovereignChainHeader_test.go @@ -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()) +}