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] Epoch start trigger factory tests #6676

Open
wants to merge 5 commits into
base: MX-15415-remove-returned-errors-factories
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
38 changes: 38 additions & 0 deletions factory/epochStartTrigger/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package epochStartTrigger

import (
"github.com/multiversx/mx-chain-core-go/core/check"

"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/factory"
"github.com/multiversx/mx-chain-go/process"
)

func checkNilArgs(args factory.ArgsEpochStartTrigger) error {
if check.IfNil(args.DataComps) {
return process.ErrNilDataComponentsHolder
}
if check.IfNil(args.DataComps.Datapool()) {
return process.ErrNilDataPoolHolder
}
if check.IfNil(args.DataComps.Blockchain()) {
return process.ErrNilBlockChain
}
if check.IfNil(args.DataComps.Datapool().MiniBlocks()) {
return dataRetriever.ErrNilMiniblocksPool
}
if check.IfNil(args.DataComps.Datapool().ValidatorsInfo()) {
return process.ErrNilValidatorInfoPool
}
if check.IfNil(args.BootstrapComponents) {
return process.ErrNilBootstrapComponentsHolder
}
if check.IfNil(args.BootstrapComponents.ShardCoordinator()) {
return process.ErrNilShardCoordinator
}
if check.IfNil(args.RequestHandler) {
return process.ErrNilRequestHandler
}

return nil
}
82 changes: 82 additions & 0 deletions factory/epochStartTrigger/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package epochStartTrigger

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/storage"
)

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

t.Run("nil data comps", func(t *testing.T) {
args := createArgs(0)
args.DataComps = nil
err := checkNilArgs(args)
require.Equal(t, process.ErrNilDataComponentsHolder, err)
})
t.Run("nil data pool", func(t *testing.T) {
args := createArgs(0)
dataComps := createDataCompsMock()
dataComps.DataPool = nil
args.DataComps = dataComps
err := checkNilArgs(args)
require.Equal(t, process.ErrNilDataPoolHolder, err)
})
t.Run("nil blockchain", func(t *testing.T) {
args := createArgs(0)
dataComps := createDataCompsMock()
dataComps.BlockChain = nil
args.DataComps = dataComps
err := checkNilArgs(args)
require.Equal(t, process.ErrNilBlockChain, err)
})
t.Run("nil mb pool", func(t *testing.T) {
args := createArgs(0)
dataComps := createDataCompsMock()
dataPool := createDataPoolMock()
dataPool.MiniBlocksCalled = func() storage.Cacher {
return nil
}
dataComps.DataPool = dataPool
args.DataComps = dataComps
err := checkNilArgs(args)
require.Equal(t, dataRetriever.ErrNilMiniblocksPool, err)
})
t.Run("nil validator pool", func(t *testing.T) {
args := createArgs(0)
dataComps := createDataCompsMock()
dataPool := createDataPoolMock()
dataPool.ValidatorsInfoCalled = func() dataRetriever.ShardedDataCacherNotifier {
return nil
}
dataComps.DataPool = dataPool
args.DataComps = dataComps
err := checkNilArgs(args)
require.Equal(t, process.ErrNilValidatorInfoPool, err)
})
t.Run("nil bootstrap comps", func(t *testing.T) {
args := createArgs(0)
args.BootstrapComponents = nil
err := checkNilArgs(args)
require.Equal(t, process.ErrNilBootstrapComponentsHolder, err)
})
t.Run("nil shard coordinator", func(t *testing.T) {
args := createArgs(0)
bootStrapComps := createBootstrapComps(0)
bootStrapComps.ShardCoordinatorCalled = nil
args.BootstrapComponents = bootStrapComps
err := checkNilArgs(args)
require.Equal(t, process.ErrNilShardCoordinator, err)
})
t.Run("nil request handler", func(t *testing.T) {
args := createArgs(0)
args.RequestHandler = nil
err := checkNilArgs(args)
require.Equal(t, process.ErrNilRequestHandler, err)
})
}
12 changes: 8 additions & 4 deletions factory/epochStartTrigger/epochStartTriggerFactory.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package epochStartTrigger

import (
"errors"
"fmt"
"time"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"

"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/epochStart/metachain"
"github.com/multiversx/mx-chain-go/epochStart/shardchain"
Expand All @@ -15,8 +16,6 @@ import (
"github.com/multiversx/mx-chain-go/process/block"
)

// TODO: MX-15632 Unit tests + fix import cycle

type epochStartTriggerFactory struct {
}

Expand All @@ -29,6 +28,11 @@ func NewEpochStartTriggerFactory() *epochStartTriggerFactory {

// CreateEpochStartTrigger creates an epoch start trigger for normal run type
func (f *epochStartTriggerFactory) CreateEpochStartTrigger(args factory.ArgsEpochStartTrigger) (epochStart.TriggerHandler, error) {
err := checkNilArgs(args)
if err != nil {
return nil, err
}

shardCoordinator := args.BootstrapComponents.ShardCoordinator()

if shardCoordinator.SelfId() < shardCoordinator.NumberOfShards() {
Expand All @@ -38,7 +42,7 @@ func (f *epochStartTriggerFactory) CreateEpochStartTrigger(args factory.ArgsEpoc
return createMetaEpochStartTrigger(args)
}

return nil, errors.New("error creating new start of epoch trigger because of invalid shard id")
return nil, fmt.Errorf("error creating new start of epoch trigger, errror: %w", process.ErrInvalidShardId)
}

func createShardEpochStartTrigger(args factory.ArgsEpochStartTrigger) (epochStart.TriggerHandler, error) {
Expand Down
153 changes: 153 additions & 0 deletions factory/epochStartTrigger/epochStartTriggerFactory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package epochStartTrigger

import (
"fmt"
"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-core-go/data/typeConverters"
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/consensus"
retriever "github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/factory"
nodeFactoryMock "github.com/multiversx/mx-chain-go/node/mock/factory"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/mock"
"github.com/multiversx/mx-chain-go/sharding"
shardingMock "github.com/multiversx/mx-chain-go/sharding/mock"
chainStorage "github.com/multiversx/mx-chain-go/storage"
"github.com/multiversx/mx-chain-go/testscommon"
"github.com/multiversx/mx-chain-go/testscommon/bootstrapMocks"
"github.com/multiversx/mx-chain-go/testscommon/dataRetriever"
testsFactory "github.com/multiversx/mx-chain-go/testscommon/factory"
"github.com/multiversx/mx-chain-go/testscommon/genesisMocks"
"github.com/multiversx/mx-chain-go/testscommon/mainFactoryMocks"
"github.com/multiversx/mx-chain-go/testscommon/pool"
"github.com/multiversx/mx-chain-go/testscommon/statusHandler"
"github.com/multiversx/mx-chain-go/testscommon/storage"
validatorInfoCacherStub "github.com/multiversx/mx-chain-go/testscommon/validatorInfoCacher"
updateMock "github.com/multiversx/mx-chain-go/update/mock"
)

func createArgs(shardID uint32) factory.ArgsEpochStartTrigger {
return factory.ArgsEpochStartTrigger{
RequestHandler: &testscommon.RequestHandlerStub{},
CoreData: &testsFactory.CoreComponentsHolderMock{
HasherCalled: func() hashing.Hasher {
return &testscommon.HasherStub{}
},
InternalMarshalizerCalled: func() marshal.Marshalizer {
return &testscommon.MarshallerStub{}
},
Uint64ByteSliceConverterCalled: func() typeConverters.Uint64ByteSliceConverter {
return &testscommon.Uint64ByteSliceConverterStub{}
},
EpochStartNotifierWithConfirmCalled: func() factory.EpochStartNotifierWithConfirm {
return &updateMock.EpochStartNotifierStub{}
},
RoundHandlerCalled: func() consensus.RoundHandler {
return &testscommon.RoundHandlerMock{}
},
EnableEpochsHandlerCalled: func() common.EnableEpochsHandler {
return &shardingMock.EnableEpochsHandlerMock{}
},
GenesisNodesSetupCalled: func() sharding.GenesisNodesSetupHandler {
return &genesisMocks.NodesSetupStub{}
},
},
BootstrapComponents: createBootstrapComps(shardID),
DataComps: createDataCompsMock(),
StatusCoreComponentsHolder: &testsFactory.StatusCoreComponentsStub{
AppStatusHandlerField: &statusHandler.AppStatusHandlerStub{},
},
RunTypeComponentsHolder: mainFactoryMocks.NewRunTypeComponentsStub(),
Config: config.Config{
EpochStartConfig: config.EpochStartConfig{
RoundsPerEpoch: 22,
MinRoundsBetweenEpochs: 22,
},
},
}
}

func createBootstrapComps(shardID uint32) *mainFactoryMocks.BootstrapComponentsStub {
return &mainFactoryMocks.BootstrapComponentsStub{
ShardCoordinatorCalled: func() sharding.Coordinator {
return &testscommon.ShardsCoordinatorMock{
NoShards: 1,
SelfIDCalled: func() uint32 {
return shardID
},
}
},
BootstrapParams: &bootstrapMocks.BootstrapParamsHandlerMock{},
HdrIntegrityVerifier: &mock.HeaderIntegrityVerifierStub{},
Bootstrapper: &bootstrapMocks.EpochStartBootstrapperStub{},
}
}

func createDataCompsMock() *nodeFactoryMock.DataComponentsMock {
return &nodeFactoryMock.DataComponentsMock{
DataPool: createDataPoolMock(),
Store: &storage.ChainStorerStub{
GetStorerCalled: func(unitType retriever.UnitType) (chainStorage.Storer, error) {
return &storage.StorerStub{}, nil
},
},
BlockChain: &testscommon.ChainHandlerStub{
GetGenesisHeaderCalled: func() data.HeaderHandler {
return &block.HeaderV2{}
},
},
}
}

func createDataPoolMock() *dataRetriever.PoolsHolderStub {
return &dataRetriever.PoolsHolderStub{
MetaBlocksCalled: func() chainStorage.Cacher {
return &testscommon.CacherStub{}
},
HeadersCalled: func() retriever.HeadersPool {
return &pool.HeadersPoolStub{}
},
ValidatorsInfoCalled: func() retriever.ShardedDataCacherNotifier {
return &testscommon.ShardedDataCacheNotifierMock{}
},
CurrEpochValidatorInfoCalled: func() retriever.ValidatorInfoCacher {
return &validatorInfoCacherStub.ValidatorInfoCacherStub{}
},
}
}

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

f := NewEpochStartTriggerFactory()
require.False(t, f.IsInterfaceNil())

t.Run("create for shard", func(t *testing.T) {
args := createArgs(0)
trigger, err := f.CreateEpochStartTrigger(args)
require.Nil(t, err)
require.Equal(t, "*shardchain.trigger", fmt.Sprintf("%T", trigger))
})
t.Run("create for meta", func(t *testing.T) {
args := createArgs(core.MetachainShardId)
trigger, err := f.CreateEpochStartTrigger(args)
require.Nil(t, err)
require.Equal(t, "*metachain.trigger", fmt.Sprintf("%T", trigger))
})
t.Run("invalid shard id", func(t *testing.T) {
args := createArgs(444)
trigger, err := f.CreateEpochStartTrigger(args)
require.ErrorIs(t, err, process.ErrInvalidShardId)
require.Nil(t, trigger)
})
}
29 changes: 2 additions & 27 deletions factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package epochStartTrigger

import (
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/epochStart"
"github.com/multiversx/mx-chain-go/epochStart/metachain"
"github.com/multiversx/mx-chain-go/epochStart/shardchain"
"github.com/multiversx/mx-chain-go/factory"
"github.com/multiversx/mx-chain-go/process"
)

// TODO: MX-15632 Unit tests + fix import cycle

type sovereignEpochStartTriggerFactory struct {
}

Expand All @@ -23,12 +18,12 @@ func NewSovereignEpochStartTriggerFactory() *sovereignEpochStartTriggerFactory {

// CreateEpochStartTrigger creates a meta epoch start trigger for sovereign run type
func (f *sovereignEpochStartTriggerFactory) CreateEpochStartTrigger(args factory.ArgsEpochStartTrigger) (epochStart.TriggerHandler, error) {
metaTriggerArgs, err := createMetaEpochStartTriggerArgs(args)
err := checkNilArgs(args)
if err != nil {
return nil, err
}

err = checkNilArgs(args)
metaTriggerArgs, err := createMetaEpochStartTriggerArgs(args)
if err != nil {
return nil, err
}
Expand All @@ -51,26 +46,6 @@ func (f *sovereignEpochStartTriggerFactory) CreateEpochStartTrigger(args factory
return metachain.NewSovereignTrigger(argsSovTrigger)
}

func checkNilArgs(args factory.ArgsEpochStartTrigger) error {
if check.IfNil(args.DataComps) {
return process.ErrNilDataComponentsHolder
}
if check.IfNil(args.DataComps.Datapool()) {
return process.ErrNilDataPoolHolder
}
if check.IfNil(args.DataComps.Datapool().MiniBlocks()) {
return dataRetriever.ErrNilMiniblocksPool
}
if check.IfNil(args.DataComps.Datapool().ValidatorsInfo()) {
return process.ErrNilValidatorInfoPool
}
if check.IfNil(args.RequestHandler) {
return process.ErrNilRequestHandler
}

return nil
}

// IsInterfaceNil checks if the underlying pointer is nil
func (f *sovereignEpochStartTriggerFactory) IsInterfaceNil() bool {
return f == nil
Expand Down
Loading
Loading