Skip to content

Commit

Permalink
FEAT: Intercepted sovereign block header with mb headers checker
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusmihaic committed Nov 8, 2024
1 parent c5f11d7 commit cd563fb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 11 deletions.
6 changes: 3 additions & 3 deletions process/block/interceptedBlocks/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,13 @@ func checkShardData(sd data.ShardDataHandler, coordinator sharding.Coordinator)
return nil
}

func checkMiniBlocksHeaders(mbHeaders []data.MiniBlockHeaderHandler, coordinator sharding.Coordinator) error {
func checkMiniBlocksHeaders(mbHeaders []data.MiniBlockHeaderHandler, coordinator sharding.Coordinator, acceptedCrossShardID uint32) error {
for _, mbHeader := range mbHeaders {
isWrongSenderShardId := mbHeader.GetSenderShardID() >= coordinator.NumberOfShards() &&
mbHeader.GetSenderShardID() != core.MainChainShardId &&
mbHeader.GetSenderShardID() != acceptedCrossShardID &&
mbHeader.GetSenderShardID() != core.AllShardId
isWrongDestinationShardId := mbHeader.GetReceiverShardID() >= coordinator.NumberOfShards() &&
mbHeader.GetReceiverShardID() != core.MainChainShardId &&
mbHeader.GetReceiverShardID() != acceptedCrossShardID &&
mbHeader.GetReceiverShardID() != core.AllShardId
isWrongShardId := isWrongSenderShardId || isWrongDestinationShardId
if isWrongShardId {
Expand Down
15 changes: 8 additions & 7 deletions process/block/interceptedBlocks/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-go/process"
Expand Down Expand Up @@ -453,8 +454,8 @@ func TestCheckMiniBlocksHeaders_WithNilOrEmptyShouldReturnNil(t *testing.T) {

shardCoordinator := mock.NewOneShardCoordinatorMock()

err1 := checkMiniBlocksHeaders(nil, shardCoordinator)
err2 := checkMiniBlocksHeaders(make([]data.MiniBlockHeaderHandler, 0), shardCoordinator)
err1 := checkMiniBlocksHeaders(nil, shardCoordinator, core.MainChainShardId)
err2 := checkMiniBlocksHeaders(make([]data.MiniBlockHeaderHandler, 0), shardCoordinator, core.MainChainShardId)

assert.Nil(t, err1)
assert.Nil(t, err2)
Expand All @@ -473,7 +474,7 @@ func TestCheckMiniBlocksHeaders_WrongMiniblockSenderShardIdShouldErr(t *testing.
Type: 0,
}

err := checkMiniBlocksHeaders([]data.MiniBlockHeaderHandler{&miniblockHeader}, shardCoordinator)
err := checkMiniBlocksHeaders([]data.MiniBlockHeaderHandler{&miniblockHeader}, shardCoordinator, core.MainChainShardId)

assert.Equal(t, process.ErrInvalidShardId, err)
}
Expand All @@ -491,7 +492,7 @@ func TestCheckMiniBlocksHeaders_WrongMiniblockReceiverShardIdShouldErr(t *testin
Type: 0,
}

err := checkMiniBlocksHeaders([]data.MiniBlockHeaderHandler{&miniblockHeader}, shardCoordinator)
err := checkMiniBlocksHeaders([]data.MiniBlockHeaderHandler{&miniblockHeader}, shardCoordinator, core.MainChainShardId)

assert.Equal(t, process.ErrInvalidShardId, err)
}
Expand All @@ -509,7 +510,7 @@ func TestCheckMiniBlocksHeaders_ReservedPopulatedShouldErr(t *testing.T) {
Reserved: []byte("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr"),
}

err := checkMiniBlocksHeaders([]data.MiniBlockHeaderHandler{&miniblockHeader}, shardCoordinator)
err := checkMiniBlocksHeaders([]data.MiniBlockHeaderHandler{&miniblockHeader}, shardCoordinator, core.MainChainShardId)

assert.Equal(t, process.ErrReservedFieldInvalid, err)
}
Expand All @@ -527,7 +528,7 @@ func TestCheckMiniBlocksHeaders_ReservedPopulatedCorrectly(t *testing.T) {
Reserved: []byte("r"),
}

err := checkMiniBlocksHeaders([]data.MiniBlockHeaderHandler{&miniblockHeader}, shardCoordinator)
err := checkMiniBlocksHeaders([]data.MiniBlockHeaderHandler{&miniblockHeader}, shardCoordinator, core.MainChainShardId)

assert.Nil(t, err)
}
Expand All @@ -544,7 +545,7 @@ func TestCheckMiniBlocksHeaders_OkValsShouldWork(t *testing.T) {
Type: 0,
}

err := checkMiniBlocksHeaders([]data.MiniBlockHeaderHandler{&miniblockHeader}, shardCoordinator)
err := checkMiniBlocksHeaders([]data.MiniBlockHeaderHandler{&miniblockHeader}, shardCoordinator, core.MainChainShardId)

assert.Nil(t, err)
}
5 changes: 5 additions & 0 deletions process/block/interceptedBlocks/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package interceptedBlocks

import "errors"

var errNilInterceptedBlockHeader = errors.New("nil intercepted block header provided")
9 changes: 8 additions & 1 deletion process/block/interceptedBlocks/interceptedBlockHeader.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type InterceptedHeader struct {
isForCurrentShard bool
validityAttester process.ValidityAttester
epochStartTrigger process.EpochStartTriggerHandler

mbHeadersChecker mbHeadersChecker
}

// NewInterceptedHeader creates a new instance of InterceptedHeader struct
Expand All @@ -50,6 +52,7 @@ func NewInterceptedHeader(arg *ArgInterceptedBlockHeader) (*InterceptedHeader, e
epochStartTrigger: arg.EpochStartTrigger,
}
inHdr.processFields(arg.HdrBuff)
inHdr.mbHeadersChecker = inHdr

return inHdr, nil
}
Expand Down Expand Up @@ -138,14 +141,18 @@ func (inHdr *InterceptedHeader) integrity() error {
return err
}

err = checkMiniBlocksHeaders(inHdr.hdr.GetMiniBlockHeaderHandlers(), inHdr.shardCoordinator)
err = inHdr.mbHeadersChecker.checkMiniBlocksHeaders(inHdr.hdr.GetMiniBlockHeaderHandlers(), inHdr.shardCoordinator)
if err != nil {
return err
}

return nil
}

func (inHdr *InterceptedHeader) checkMiniBlocksHeaders(mbHeaders []data.MiniBlockHeaderHandler, coordinator sharding.Coordinator) error {
return checkMiniBlocksHeaders(mbHeaders, coordinator, core.MetachainShardId)
}

// Hash gets the hash of this header
func (inHdr *InterceptedHeader) Hash() []byte {
return inHdr.hash
Expand Down
35 changes: 35 additions & 0 deletions process/block/interceptedBlocks/interceptedSovereignBlockHeader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package interceptedBlocks

import (
"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-go/sharding"
)

type interceptedSovereignBlockHeader struct {
*InterceptedHeader
}

// NewSovereignInterceptedBlockHeader creates a new intercepted sovereign block header
func NewSovereignInterceptedBlockHeader(blockHeaderInterceptor *InterceptedHeader) (*interceptedSovereignBlockHeader, error) {
if check.IfNil(blockHeaderInterceptor) {
return nil, errNilInterceptedBlockHeader
}

sovInterceptedBlock := &interceptedSovereignBlockHeader{
blockHeaderInterceptor,
}

sovInterceptedBlock.mbHeadersChecker = sovInterceptedBlock
return sovInterceptedBlock, nil
}

func (isbh *interceptedSovereignBlockHeader) checkMiniBlocksHeaders(mbHeaders []data.MiniBlockHeaderHandler, coordinator sharding.Coordinator) error {
return checkMiniBlocksHeaders(mbHeaders, coordinator, core.MainChainShardId)
}

// IsInterfaceNil returns true if there is no value under the interface
func (isbh *interceptedSovereignBlockHeader) IsInterfaceNil() bool {
return isbh == nil
}
10 changes: 10 additions & 0 deletions process/block/interceptedBlocks/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package interceptedBlocks

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

type mbHeadersChecker interface {
checkMiniBlocksHeaders(mbHeaders []data.MiniBlockHeaderHandler, coordinator sharding.Coordinator) error
}

0 comments on commit cd563fb

Please sign in to comment.