Skip to content

Commit

Permalink
FEAT: Integrate outport data provider factory in runType
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusmihaic committed Dec 4, 2024
1 parent cf40302 commit 428bc9a
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 9 deletions.
3 changes: 3 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -871,3 +871,6 @@ var ErrNilShardRequestersContainerCreatorHandler = errors.New("nil shard request

// ErrNilAPIRewardsHandler signals that a nil api rewards handler has been provided
var ErrNilAPIRewardsHandler = errors.New("nil api rewards handler has been provided")

// ErrNilOutportDataProviderFactory signals that a nil outport data provider factory has been provided
var ErrNilOutportDataProviderFactory = errors.New("nil outport data provider factory has been provided")
8 changes: 8 additions & 0 deletions factory/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/multiversx/mx-chain-go/node/external/transactionAPI"
"github.com/multiversx/mx-chain-go/ntp"
"github.com/multiversx/mx-chain-go/outport"
outportFactory "github.com/multiversx/mx-chain-go/outport/process/factory"
"github.com/multiversx/mx-chain-go/p2p"
"github.com/multiversx/mx-chain-go/process"
processBlock "github.com/multiversx/mx-chain-go/process/block"
Expand Down Expand Up @@ -644,6 +645,7 @@ type RunTypeComponentsHolder interface {
ValidatorAccountsSyncerFactoryHandler() syncerFactory.ValidatorAccountsSyncerFactoryHandler
ShardRequestersContainerCreatorHandler() storageRequestFactory.ShardRequestersContainerCreatorHandler
APIRewardsTxHandler() transactionAPI.APIRewardTxHandler
OutportDataProviderFactory() OutportDataProviderFactoryHandler
Create() error
Close() error
CheckSubcomponents() error
Expand Down Expand Up @@ -713,3 +715,9 @@ type ExportHandlerFactoryCreator interface {
CreateExportFactoryHandler(args ArgsExporter) (update.ExportFactoryHandler, error)
IsInterfaceNil() bool
}

// OutportDataProviderFactoryHandler defines an outport data provider factory handler
type OutportDataProviderFactoryHandler interface {
CreateOutportDataProvider(arg outportFactory.ArgOutportDataProviderFactory) (outport.DataProviderOutport, error)
IsInterfaceNil() bool
}
2 changes: 1 addition & 1 deletion factory/processing/blockProcessorCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@ func (pcf *processComponentsFactory) createOutportDataProvider(
return nil, err
}

return factoryOutportProvider.CreateOutportDataProvider(factoryOutportProvider.ArgOutportDataProviderFactory{
return pcf.runTypeComponents.OutportDataProviderFactory().CreateOutportDataProvider(factoryOutportProvider.ArgOutportDataProviderFactory{
HasDrivers: pcf.statusComponents.OutportHandler().HasDrivers(),
AddressConverter: pcf.coreData.AddressPubKeyConverter(),
AccountsDB: pcf.state.AccountsAdapter(),
Expand Down
3 changes: 3 additions & 0 deletions factory/processing/processComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -2116,6 +2116,9 @@ func checkProcessComponentsArgs(args ProcessComponentsFactoryArgs) error {
if check.IfNil(args.RunTypeComponents.ExportHandlerFactoryCreator()) {
return fmt.Errorf("%s: %w", baseErrMessage, errorsMx.ErrNilExportHandlerFactoryCreator)
}
if check.IfNil(args.RunTypeComponents.OutportDataProviderFactory()) {
return fmt.Errorf("%s: %w", baseErrMessage, errorsMx.ErrNilOutportDataProviderFactory)
}

return nil
}
Expand Down
12 changes: 12 additions & 0 deletions factory/processing/processComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,17 @@ func TestNewProcessComponentsFactory(t *testing.T) {
require.True(t, errors.Is(err, errorsMx.ErrNilExportHandlerFactoryCreator))
require.Nil(t, pcf)
})
t.Run("nil OutportDataProviderFactory should error", func(t *testing.T) {
t.Parallel()

args := createMockProcessComponentsFactoryArgs()
rtMock := getRunTypeComponentsMock()
rtMock.OutportDataProviderFactoryField = nil
args.RunTypeComponents = rtMock
pcf, err := processComp.NewProcessComponentsFactory(args)
require.True(t, errors.Is(err, errorsMx.ErrNilOutportDataProviderFactory))
require.Nil(t, pcf)
})
t.Run("should work", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1127,6 +1138,7 @@ func getRunTypeComponents(rt runType.RunTypeComponentsHolder) *mainFactoryMocks.
ValidatorAccountsSyncerFactoryHandlerField: rt.ValidatorAccountsSyncerFactoryHandler(),
ShardRequestersContainerCreatorHandlerField: rt.ShardRequestersContainerCreatorHandler(),
APIRewardsTxHandlerField: rt.APIRewardsTxHandler(),
OutportDataProviderFactoryField: rt.OutportDataProviderFactory(),
}
}

Expand Down
3 changes: 3 additions & 0 deletions factory/runType/runTypeComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/multiversx/mx-chain-go/genesis/parsing"
processGenesis "github.com/multiversx/mx-chain-go/genesis/process"
"github.com/multiversx/mx-chain-go/node/external/transactionAPI"
outportFactory "github.com/multiversx/mx-chain-go/outport/process/factory"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/block"
processBlock "github.com/multiversx/mx-chain-go/process/block"
Expand Down Expand Up @@ -124,6 +125,7 @@ type runTypeComponents struct {
validatorAccountsSyncerFactoryHandler syncerFactory.ValidatorAccountsSyncerFactoryHandler
shardRequestersContainerCreatorHandler storageRequestFactory.ShardRequestersContainerCreatorHandler
apiRewardTxHandler transactionAPI.APIRewardTxHandler
outportDataProviderFactory mainFactory.OutportDataProviderFactoryHandler
}

// NewRunTypeComponentsFactory will return a new instance of runTypeComponentsFactory
Expand Down Expand Up @@ -302,6 +304,7 @@ func (rcf *runTypeComponentsFactory) Create() (*runTypeComponents, error) {
validatorAccountsSyncerFactoryHandler: syncerFactory.NewValidatorAccountsSyncerFactory(),
shardRequestersContainerCreatorHandler: storageRequestFactory.NewShardRequestersContainerCreator(),
apiRewardTxHandler: apiRewardTxHandler,
outportDataProviderFactory: outportFactory.NewOutportDataProviderFactory(),
}, nil
}

Expand Down
15 changes: 15 additions & 0 deletions factory/runType/runTypeComponentsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ func (mrc *managedRunTypeComponents) CheckSubcomponents() error {
if check.IfNil(mrc.apiRewardTxHandler) {
return errors.ErrNilAPIRewardsHandler
}
if check.IfNil(mrc.outportDataProviderFactory) {
return errors.ErrNilOutportDataProviderFactory
}

return nil
}
Expand Down Expand Up @@ -843,6 +846,18 @@ func (mrc *managedRunTypeComponents) APIRewardsTxHandler() transactionAPI.APIRew
return mrc.runTypeComponents.apiRewardTxHandler
}

// OutportDataProviderFactory returns the outport data provider factory
func (mrc *managedRunTypeComponents) OutportDataProviderFactory() factory.OutportDataProviderFactoryHandler {
mrc.mutRunTypeComponents.RLock()
defer mrc.mutRunTypeComponents.RUnlock()

if check.IfNil(mrc.runTypeComponents) {
return nil
}

return mrc.runTypeComponents.outportDataProviderFactory
}

// IsInterfaceNil returns true if the interface is nil
func (mrc *managedRunTypeComponents) IsInterfaceNil() bool {
return mrc == nil
Expand Down
2 changes: 2 additions & 0 deletions factory/runType/runTypeComponentsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func TestManagedRunTypeComponents_Create(t *testing.T) {
require.Nil(t, managedRunTypeComponents.ValidatorAccountsSyncerFactoryHandler())
require.Nil(t, managedRunTypeComponents.ShardRequestersContainerCreatorHandler())
require.Nil(t, managedRunTypeComponents.APIRewardsTxHandler())
require.Nil(t, managedRunTypeComponents.OutportDataProviderFactory())

err = managedRunTypeComponents.Create()
require.NoError(t, err)
Expand Down Expand Up @@ -155,6 +156,7 @@ func TestManagedRunTypeComponents_Create(t *testing.T) {
require.NotNil(t, managedRunTypeComponents.ValidatorAccountsSyncerFactoryHandler())
require.NotNil(t, managedRunTypeComponents.ShardRequestersContainerCreatorHandler())
require.NotNil(t, managedRunTypeComponents.APIRewardsTxHandler())
require.NotNil(t, managedRunTypeComponents.OutportDataProviderFactory())

require.Equal(t, factory.RunTypeComponentsName, managedRunTypeComponents.String())
require.NoError(t, managedRunTypeComponents.Close())
Expand Down
2 changes: 2 additions & 0 deletions factory/runType/sovereignRunTypeComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/multiversx/mx-chain-go/genesis/parsing"
processComp "github.com/multiversx/mx-chain-go/genesis/process"
"github.com/multiversx/mx-chain-go/node/external/transactionAPI"
outportFactory "github.com/multiversx/mx-chain-go/outport/process/factory"
"github.com/multiversx/mx-chain-go/process/block"
"github.com/multiversx/mx-chain-go/process/block/preprocess"
"github.com/multiversx/mx-chain-go/process/block/sovereign"
Expand Down Expand Up @@ -271,5 +272,6 @@ func (rcf *sovereignRunTypeComponentsFactory) Create() (*runTypeComponents, erro
validatorAccountsSyncerFactoryHandler: syncerFactory.NewSovereignValidatorAccountsSyncerFactory(),
shardRequestersContainerCreatorHandler: storageRequestFactory.NewSovereignShardRequestersContainerCreator(),
apiRewardTxHandler: apiRewardTxHandler,
outportDataProviderFactory: outportFactory.NewSovereignOutportDataProviderFactory(),
}, nil
}
31 changes: 27 additions & 4 deletions outport/process/factory/outportDataProviderFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/outport"
"github.com/multiversx/mx-chain-go/outport/process"
Expand All @@ -16,7 +18,6 @@ import (
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
"github.com/multiversx/mx-chain-go/state"
"github.com/multiversx/mx-chain-go/storage"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
)

// ArgOutportDataProviderFactory holds the arguments needed for creating a new instance of outport.DataProviderOutport
Expand All @@ -39,12 +40,29 @@ type ArgOutportDataProviderFactory struct {
ExecutionOrderGetter common.ExecutionOrderGetter
}

type outportDataProviderFactory struct {
}

// NewOutportDataProviderFactory creates a new outport data provider factory
func NewOutportDataProviderFactory() *outportDataProviderFactory {
return &outportDataProviderFactory{}
}

// CreateOutportDataProvider will create a new instance of outport.DataProviderOutport
func CreateOutportDataProvider(arg ArgOutportDataProviderFactory) (outport.DataProviderOutport, error) {
func (f *outportDataProviderFactory) CreateOutportDataProvider(arg ArgOutportDataProviderFactory) (outport.DataProviderOutport, error) {
if !arg.HasDrivers {
return disabled.NewDisabledOutportDataProvider(), nil
}

argsOutport, err := createArgs(arg)
if err != nil {
return nil, err
}

return process.NewOutportDataProvider(*argsOutport)
}

func createArgs(arg ArgOutportDataProviderFactory) (*process.ArgOutportDataProvider, error) {
err := checkArgOutportDataProviderFactory(arg)
if err != nil {
return nil, err
Expand Down Expand Up @@ -73,7 +91,7 @@ func CreateOutportDataProvider(arg ArgOutportDataProviderFactory) (outport.DataP
return nil, err
}

return process.NewOutportDataProvider(process.ArgOutportDataProvider{
return &process.ArgOutportDataProvider{
IsImportDBMode: arg.IsImportDBMode,
ShardCoordinator: arg.ShardCoordinator,
AlteredAccountsProvider: alteredAccountsProvider,
Expand All @@ -85,5 +103,10 @@ func CreateOutportDataProvider(arg ArgOutportDataProviderFactory) (outport.DataP
ExecutionOrderHandler: arg.ExecutionOrderGetter,
Hasher: arg.Hasher,
Marshaller: arg.Marshaller,
})
}, nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (f *outportDataProviderFactory) IsInterfaceNil() bool {
return f == nil
}
13 changes: 9 additions & 4 deletions outport/process/factory/outportDataProviderFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"testing"

"github.com/multiversx/mx-chain-go/outport/process/alteredaccounts"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-go/outport/process/alteredaccounts"
)

func TestCreateOutportDataProviderDisabled(t *testing.T) {
Expand All @@ -14,7 +15,8 @@ func TestCreateOutportDataProviderDisabled(t *testing.T) {
arg := createArgOutportDataProviderFactory()
arg.HasDrivers = false

provider, err := CreateOutportDataProvider(arg)
factory := NewOutportDataProviderFactory()
provider, err := factory.CreateOutportDataProvider(arg)
require.Nil(t, err)
require.NotNil(t, provider)
require.Equal(t, "*disabled.disabledOutportDataProvider", fmt.Sprintf("%T", provider))
Expand All @@ -27,7 +29,8 @@ func TestCreateOutportDataProviderError(t *testing.T) {
arg.HasDrivers = true
arg.AddressConverter = nil

provider, err := CreateOutportDataProvider(arg)
factory := NewOutportDataProviderFactory()
provider, err := factory.CreateOutportDataProvider(arg)
require.Nil(t, provider)
require.Equal(t, alteredaccounts.ErrNilPubKeyConverter, err)
}
Expand All @@ -38,8 +41,10 @@ func TestCreateOutportDataProvider(t *testing.T) {
arg := createArgOutportDataProviderFactory()
arg.HasDrivers = true

provider, err := CreateOutportDataProvider(arg)
factory := NewOutportDataProviderFactory()
provider, err := factory.CreateOutportDataProvider(arg)
require.Nil(t, err)
require.NotNil(t, provider)
require.Equal(t, "*process.outportDataProvider", fmt.Sprintf("%T", provider))
require.False(t, factory.IsInterfaceNil())
}
39 changes: 39 additions & 0 deletions outport/process/factory/sovereignOutportDataProviderFactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package factory

import (
"github.com/multiversx/mx-chain-go/outport"
"github.com/multiversx/mx-chain-go/outport/process"
"github.com/multiversx/mx-chain-go/outport/process/disabled"
)

type sovereignOutportDataProviderFactory struct {
}

// NewSovereignOutportDataProviderFactory creates a new outport data provider factory for sovereign chain
func NewSovereignOutportDataProviderFactory() *sovereignOutportDataProviderFactory {
return &sovereignOutportDataProviderFactory{}
}

// CreateOutportDataProvider will create a new instance of outport.DataProviderOutport
func (f *sovereignOutportDataProviderFactory) CreateOutportDataProvider(arg ArgOutportDataProviderFactory) (outport.DataProviderOutport, error) {
if !arg.HasDrivers {
return disabled.NewDisabledOutportDataProvider(), nil
}

argsOutport, err := createArgs(arg)
if err != nil {
return nil, err
}

odp, err := process.NewOutportDataProvider(*argsOutport)
if err != nil {
return nil, err
}

return process.NewSovereignOutportDataProvider(odp)
}

// IsInterfaceNil returns true if there is no value under the interface
func (f *sovereignOutportDataProviderFactory) IsInterfaceNil() bool {
return f == nil
}
1 change: 1 addition & 0 deletions testscommon/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,7 @@ func GetRunTypeComponentsStub(rt factory.RunTypeComponentsHandler) *mainFactoryM
ValidatorAccountsSyncerFactoryHandlerField: rt.ValidatorAccountsSyncerFactoryHandler(),
ShardRequestersContainerCreatorHandlerField: rt.ShardRequestersContainerCreatorHandler(),
APIRewardsTxHandlerField: rt.APIRewardsTxHandler(),
OutportDataProviderFactoryField: rt.OutportDataProviderFactory(),
}
}

Expand Down
26 changes: 26 additions & 0 deletions testscommon/factory/outportDataProviderFactoryMock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package factory

import (
"github.com/multiversx/mx-chain-go/outport"
"github.com/multiversx/mx-chain-go/outport/process/factory"
outportStub "github.com/multiversx/mx-chain-go/testscommon/outport"
)

// OutportDataProviderFactoryMock -
type OutportDataProviderFactoryMock struct {
CreateOutportDataProviderCalled func(arg factory.ArgOutportDataProviderFactory) (outport.DataProviderOutport, error)
}

// CreateOutportDataProvider -
func (f *OutportDataProviderFactoryMock) CreateOutportDataProvider(arg factory.ArgOutportDataProviderFactory) (outport.DataProviderOutport, error) {
if f.CreateOutportDataProviderCalled != nil {
return f.CreateOutportDataProviderCalled(arg)
}

return &outportStub.OutportDataProviderStub{}, nil
}

// IsInterfaceNil -
func (f *OutportDataProviderFactoryMock) IsInterfaceNil() bool {
return f == nil
}
7 changes: 7 additions & 0 deletions testscommon/mainFactoryMocks/runTypeComponentStub.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type RunTypeComponentsStub struct {
ValidatorAccountsSyncerFactoryHandlerField syncerFactory.ValidatorAccountsSyncerFactoryHandler
ShardRequestersContainerCreatorHandlerField storageRequestFactory.ShardRequestersContainerCreatorHandler
APIRewardsTxHandlerField transactionAPI.APIRewardTxHandler
OutportDataProviderFactoryField factory.OutportDataProviderFactoryHandler
}

// NewRunTypeComponentsStub -
Expand Down Expand Up @@ -151,6 +152,7 @@ func NewRunTypeComponentsStub() *RunTypeComponentsStub {
ValidatorAccountsSyncerFactoryHandlerField: &testFactory.ValidatorAccountsSyncerFactoryMock{},
ShardRequestersContainerCreatorHandlerField: &testFactory.ShardRequestersContainerCreatorMock{},
APIRewardsTxHandlerField: &apiTests.APIRewardsHandlerStub{},
OutportDataProviderFactoryField: &testFactory.OutportDataProviderFactoryMock{},
}
}

Expand Down Expand Up @@ -419,6 +421,11 @@ func (r *RunTypeComponentsStub) APIRewardsTxHandler() transactionAPI.APIRewardTx
return r.APIRewardsTxHandlerField
}

// OutportDataProviderFactory -
func (r *RunTypeComponentsStub) OutportDataProviderFactory() factory.OutportDataProviderFactoryHandler {
return r.OutportDataProviderFactoryField
}

// IsInterfaceNil -
func (r *RunTypeComponentsStub) IsInterfaceNil() bool {
return r == nil
Expand Down

0 comments on commit 428bc9a

Please sign in to comment.