From c9e2e941d52cb3d76cb4b97976949be56e4bd8d7 Mon Sep 17 00:00:00 2001 From: MariusC Date: Fri, 13 Dec 2024 15:38:10 +0200 Subject: [PATCH 1/4] FEAT: Finally create args for epoch start trigger factory --- .../epochStartTriggerFactory_test.go | 103 ++++++++++++++++++ .../factory/headerValidatorFactoryMock.go | 3 +- 2 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 factory/epochStartTrigger/epochStartTriggerFactory_test.go diff --git a/factory/epochStartTrigger/epochStartTriggerFactory_test.go b/factory/epochStartTrigger/epochStartTriggerFactory_test.go new file mode 100644 index 00000000000..b81f1b338c0 --- /dev/null +++ b/factory/epochStartTrigger/epochStartTriggerFactory_test.go @@ -0,0 +1,103 @@ +package epochStartTrigger + +import ( + "testing" + + "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/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/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() 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{} + }, + }, + BootstrapComponents: &mainFactoryMocks.BootstrapComponentsStub{ + ShardCoordinatorCalled: func() sharding.Coordinator { + return &testscommon.ShardsCoordinatorMock{ + NoShards: 1, + } + }, + BootstrapParams: &bootstrapMocks.BootstrapParamsHandlerMock{}, + HdrIntegrityVerifier: &mock.HeaderIntegrityVerifierStub{}, + }, + DataComps: &nodeFactoryMock.DataComponentsMock{ + DataPool: &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{} + }, + }, + Store: &storage.ChainStorerStub{ + GetStorerCalled: func(unitType retriever.UnitType) (chainStorage.Storer, error) { + return &storage.StorerStub{}, nil + }, + }, + }, + StatusCoreComponentsHolder: &testsFactory.StatusCoreComponentsStub{ + AppStatusHandlerField: &statusHandler.AppStatusHandlerStub{}, + }, + RunTypeComponentsHolder: mainFactoryMocks.NewRunTypeComponentsStub(), + Config: config.Config{}, + } +} + +func TestNewEpochStartTriggerFactory(t *testing.T) { + t.Parallel() + + f := NewEpochStartTriggerFactory() + + args := createArgs() + trigger, err := f.CreateEpochStartTrigger(args) + require.Nil(t, err) + require.NotNil(t, trigger) +} diff --git a/testscommon/factory/headerValidatorFactoryMock.go b/testscommon/factory/headerValidatorFactoryMock.go index 11fae039778..3bfb114035c 100644 --- a/testscommon/factory/headerValidatorFactoryMock.go +++ b/testscommon/factory/headerValidatorFactoryMock.go @@ -1,6 +1,7 @@ package factory import ( + "github.com/multiversx/mx-chain-go/node/mock" "github.com/multiversx/mx-chain-go/process" processBlock "github.com/multiversx/mx-chain-go/process/block" ) @@ -15,7 +16,7 @@ func (h *HeaderValidatorFactoryMock) CreateHeaderValidator(args processBlock.Arg if h.CreateHeaderValidatorCalled != nil { return h.CreateHeaderValidatorCalled(args) } - return nil, nil + return &mock.HeaderValidatorStub{}, nil } // IsInterfaceNil - From f2e38f9cf99e2245a33dff02363006536df74bc4 Mon Sep 17 00:00:00 2001 From: MariusC Date: Fri, 13 Dec 2024 16:19:46 +0200 Subject: [PATCH 2/4] FEAT: Extend tests for sovereignEpochStartTriggerFactory.go --- .../epochStartTriggerFactory.go | 8 +++- .../epochStartTriggerFactory_test.go | 43 ++++++++++++++++--- .../sovereignEpochStartTriggerFactory.go | 16 +++++-- .../sovereignEpochStartTriggerFactory_test.go | 19 ++++++++ 4 files changed, 74 insertions(+), 12 deletions(-) create mode 100644 factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go diff --git a/factory/epochStartTrigger/epochStartTriggerFactory.go b/factory/epochStartTrigger/epochStartTriggerFactory.go index e4c94c50608..d378efff4b9 100644 --- a/factory/epochStartTrigger/epochStartTriggerFactory.go +++ b/factory/epochStartTrigger/epochStartTriggerFactory.go @@ -6,6 +6,7 @@ import ( "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" @@ -15,8 +16,6 @@ import ( "github.com/multiversx/mx-chain-go/process/block" ) -// TODO: MX-15632 Unit tests + fix import cycle - type epochStartTriggerFactory struct { } @@ -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() { diff --git a/factory/epochStartTrigger/epochStartTriggerFactory_test.go b/factory/epochStartTrigger/epochStartTriggerFactory_test.go index b81f1b338c0..60ee5f12452 100644 --- a/factory/epochStartTrigger/epochStartTriggerFactory_test.go +++ b/factory/epochStartTrigger/epochStartTriggerFactory_test.go @@ -1,8 +1,12 @@ 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" @@ -22,6 +26,7 @@ import ( "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" @@ -30,7 +35,7 @@ import ( updateMock "github.com/multiversx/mx-chain-go/update/mock" ) -func createArgs() factory.ArgsEpochStartTrigger { +func createArgs(shardID uint32) factory.ArgsEpochStartTrigger { return factory.ArgsEpochStartTrigger{ RequestHandler: &testscommon.RequestHandlerStub{}, CoreData: &testsFactory.CoreComponentsHolderMock{ @@ -52,15 +57,22 @@ func createArgs() factory.ArgsEpochStartTrigger { EnableEpochsHandlerCalled: func() common.EnableEpochsHandler { return &shardingMock.EnableEpochsHandlerMock{} }, + GenesisNodesSetupCalled: func() sharding.GenesisNodesSetupHandler { + return &genesisMocks.NodesSetupStub{} + }, }, BootstrapComponents: &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{}, }, DataComps: &nodeFactoryMock.DataComponentsMock{ DataPool: &dataRetriever.PoolsHolderStub{ @@ -82,12 +94,22 @@ func createArgs() factory.ArgsEpochStartTrigger { return &storage.StorerStub{}, nil }, }, + BlockChain: &testscommon.ChainHandlerStub{ + GetGenesisHeaderCalled: func() data.HeaderHandler { + return &block.HeaderV2{} + }, + }, }, StatusCoreComponentsHolder: &testsFactory.StatusCoreComponentsStub{ AppStatusHandlerField: &statusHandler.AppStatusHandlerStub{}, }, RunTypeComponentsHolder: mainFactoryMocks.NewRunTypeComponentsStub(), - Config: config.Config{}, + Config: config.Config{ + EpochStartConfig: config.EpochStartConfig{ + RoundsPerEpoch: 22, + MinRoundsBetweenEpochs: 22, + }, + }, } } @@ -95,9 +117,18 @@ func TestNewEpochStartTriggerFactory(t *testing.T) { t.Parallel() f := NewEpochStartTriggerFactory() + require.False(t, f.IsInterfaceNil()) - args := createArgs() - trigger, err := f.CreateEpochStartTrigger(args) - require.Nil(t, err) - require.NotNil(t, trigger) + 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)) + }) } diff --git a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go index c4305c23c75..52c58458451 100644 --- a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go +++ b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go @@ -2,6 +2,7 @@ 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" @@ -10,8 +11,6 @@ import ( "github.com/multiversx/mx-chain-go/process" ) -// TODO: MX-15632 Unit tests + fix import cycle - type sovereignEpochStartTriggerFactory struct { } @@ -23,12 +22,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 } @@ -64,6 +63,15 @@ func checkNilArgs(args factory.ArgsEpochStartTrigger) error { if check.IfNil(args.DataComps.Datapool().ValidatorsInfo()) { return process.ErrNilValidatorInfoPool } + if check.IfNil(args.DataComps.Blockchain()) { + return process.ErrNilBlockChain + } + 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 } diff --git a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go new file mode 100644 index 00000000000..91bcbcf5de1 --- /dev/null +++ b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go @@ -0,0 +1,19 @@ +package epochStartTrigger + +import ( + "fmt" + "testing" + + "github.com/multiversx/mx-chain-core-go/core" + "github.com/stretchr/testify/require" +) + +func TestSovereignEpochStartTriggerFactory_CreateEpochStartTrigger(t *testing.T) { + f := NewSovereignEpochStartTriggerFactory() + require.False(t, f.IsInterfaceNil()) + + args := createArgs(core.SovereignChainShardId) + trigger, err := f.CreateEpochStartTrigger(args) + require.Nil(t, err) + require.Equal(t, "*metachain.sovereignTrigger", fmt.Sprintf("%T", trigger)) +} From 30c4c2399515b8f5a5ccf134be74d290d50cc990 Mon Sep 17 00:00:00 2001 From: MariusC Date: Mon, 16 Dec 2024 11:57:41 +0200 Subject: [PATCH 3/4] FEAT: Extend tests epoch start trigger factory --- .../epochStartTriggerFactory.go | 4 +- .../epochStartTriggerFactory_test.go | 97 +++++++++++-------- .../sovereignEpochStartTriggerFactory.go | 6 +- .../sovereignEpochStartTriggerFactory_test.go | 77 +++++++++++++++ ...tContractResultPreProcessorFactory_test.go | 3 +- 5 files changed, 141 insertions(+), 46 deletions(-) diff --git a/factory/epochStartTrigger/epochStartTriggerFactory.go b/factory/epochStartTrigger/epochStartTriggerFactory.go index d378efff4b9..c343acce180 100644 --- a/factory/epochStartTrigger/epochStartTriggerFactory.go +++ b/factory/epochStartTrigger/epochStartTriggerFactory.go @@ -1,7 +1,7 @@ package epochStartTrigger import ( - "errors" + "fmt" "time" "github.com/multiversx/mx-chain-core-go/core" @@ -42,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) { diff --git a/factory/epochStartTrigger/epochStartTriggerFactory_test.go b/factory/epochStartTrigger/epochStartTriggerFactory_test.go index 60ee5f12452..14f34b9c162 100644 --- a/factory/epochStartTrigger/epochStartTriggerFactory_test.go +++ b/factory/epochStartTrigger/epochStartTriggerFactory_test.go @@ -18,6 +18,7 @@ import ( 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" @@ -61,45 +62,8 @@ func createArgs(shardID uint32) factory.ArgsEpochStartTrigger { return &genesisMocks.NodesSetupStub{} }, }, - BootstrapComponents: &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{}, - }, - DataComps: &nodeFactoryMock.DataComponentsMock{ - DataPool: &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{} - }, - }, - 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{} - }, - }, - }, + BootstrapComponents: createBootstrapComps(shardID), + DataComps: createDataCompsMock(), StatusCoreComponentsHolder: &testsFactory.StatusCoreComponentsStub{ AppStatusHandlerField: &statusHandler.AppStatusHandlerStub{}, }, @@ -113,6 +77,55 @@ func createArgs(shardID uint32) factory.ArgsEpochStartTrigger { } } +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() @@ -131,4 +144,10 @@ func TestNewEpochStartTriggerFactory(t *testing.T) { 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) + }) } diff --git a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go index 52c58458451..a5bb9c8d3f6 100644 --- a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go +++ b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go @@ -57,15 +57,15 @@ func checkNilArgs(args factory.ArgsEpochStartTrigger) error { 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.DataComps.Blockchain()) { - return process.ErrNilBlockChain - } if check.IfNil(args.BootstrapComponents) { return process.ErrNilBootstrapComponentsHolder } diff --git a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go index 91bcbcf5de1..c8cb6eb145c 100644 --- a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go +++ b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go @@ -6,9 +6,15 @@ import ( "github.com/multiversx/mx-chain-core-go/core" "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 TestSovereignEpochStartTriggerFactory_CreateEpochStartTrigger(t *testing.T) { + t.Parallel() + f := NewSovereignEpochStartTriggerFactory() require.False(t, f.IsInterfaceNil()) @@ -17,3 +23,74 @@ func TestSovereignEpochStartTriggerFactory_CreateEpochStartTrigger(t *testing.T) require.Nil(t, err) require.Equal(t, "*metachain.sovereignTrigger", fmt.Sprintf("%T", trigger)) } + +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) + }) +} diff --git a/process/block/preprocess/smartContractResultPreProcessorFactory_test.go b/process/block/preprocess/smartContractResultPreProcessorFactory_test.go index 067d002dee1..4ae38d8c3cb 100644 --- a/process/block/preprocess/smartContractResultPreProcessorFactory_test.go +++ b/process/block/preprocess/smartContractResultPreProcessorFactory_test.go @@ -3,8 +3,6 @@ package preprocess_test import ( "testing" - "github.com/multiversx/mx-chain-go/testscommon/common" - "github.com/multiversx/mx-chain-core-go/data/smartContractResult" vmcommon "github.com/multiversx/mx-chain-vm-common-go" "github.com/stretchr/testify/require" @@ -13,6 +11,7 @@ import ( "github.com/multiversx/mx-chain-go/process/block/preprocess" "github.com/multiversx/mx-chain-go/process/mock" "github.com/multiversx/mx-chain-go/testscommon" + "github.com/multiversx/mx-chain-go/testscommon/common" "github.com/multiversx/mx-chain-go/testscommon/economicsmocks" "github.com/multiversx/mx-chain-go/testscommon/enableEpochsHandlerMock" "github.com/multiversx/mx-chain-go/testscommon/hashingMocks" From 875fc0e3f21aa9492dc41f312694a6db53995cb0 Mon Sep 17 00:00:00 2001 From: MariusC Date: Mon, 16 Dec 2024 12:02:16 +0200 Subject: [PATCH 4/4] FEAT: Move checkNilArgs in common.go --- factory/epochStartTrigger/common.go | 38 +++++++++ factory/epochStartTrigger/common_test.go | 82 +++++++++++++++++++ .../sovereignEpochStartTriggerFactory.go | 33 -------- .../sovereignEpochStartTriggerFactory_test.go | 75 ----------------- 4 files changed, 120 insertions(+), 108 deletions(-) create mode 100644 factory/epochStartTrigger/common.go create mode 100644 factory/epochStartTrigger/common_test.go diff --git a/factory/epochStartTrigger/common.go b/factory/epochStartTrigger/common.go new file mode 100644 index 00000000000..a44dfbdedc3 --- /dev/null +++ b/factory/epochStartTrigger/common.go @@ -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 +} diff --git a/factory/epochStartTrigger/common_test.go b/factory/epochStartTrigger/common_test.go new file mode 100644 index 00000000000..67cee081dc6 --- /dev/null +++ b/factory/epochStartTrigger/common_test.go @@ -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) + }) +} diff --git a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go index a5bb9c8d3f6..2d4b5ffe804 100644 --- a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go +++ b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory.go @@ -1,14 +1,10 @@ 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" ) type sovereignEpochStartTriggerFactory struct { @@ -50,35 +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.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 -} - // IsInterfaceNil checks if the underlying pointer is nil func (f *sovereignEpochStartTriggerFactory) IsInterfaceNil() bool { return f == nil diff --git a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go index c8cb6eb145c..33db15eaf57 100644 --- a/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go +++ b/factory/epochStartTrigger/sovereignEpochStartTriggerFactory_test.go @@ -6,10 +6,6 @@ import ( "github.com/multiversx/mx-chain-core-go/core" "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 TestSovereignEpochStartTriggerFactory_CreateEpochStartTrigger(t *testing.T) { @@ -23,74 +19,3 @@ func TestSovereignEpochStartTriggerFactory_CreateEpochStartTrigger(t *testing.T) require.Nil(t, err) require.Equal(t, "*metachain.sovereignTrigger", fmt.Sprintf("%T", trigger)) } - -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) - }) -}