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] Clean factories for run type components #6672

Merged
15 changes: 2 additions & 13 deletions epochStart/bootstrap/sovereignEpochStartBootstrapperFactory.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,12 @@
package bootstrap

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

// sovereignEpochStartBootstrapperFactory defines the epoch start bootstrapper factory for chain run sovereign
type sovereignEpochStartBootstrapperFactory struct {
epochStartBootstrapperFactory EpochStartBootstrapperCreator
}

// NewSovereignEpochStartBootstrapperFactory creates a new epoch start bootstrapper factory for chain run sovereign
func NewSovereignEpochStartBootstrapperFactory(esbf EpochStartBootstrapperCreator) (EpochStartBootstrapperCreator, error) {
if check.IfNil(esbf) {
return nil, errors.ErrNilEpochStartBootstrapperFactory
}
return &sovereignEpochStartBootstrapperFactory{
epochStartBootstrapperFactory: esbf,
}, nil
func NewSovereignEpochStartBootstrapperFactory() EpochStartBootstrapperCreator {
return &sovereignEpochStartBootstrapperFactory{}
}

// CreateEpochStartBootstrapper creates a new epoch start bootstrapper for sovereign chain operations
Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,43 @@
package bootstrap

import (
"fmt"
"testing"

"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/errors"

"github.com/multiversx/mx-chain-core-go/data/endProcess"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-go/config"
)

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

sebf, err := NewSovereignEpochStartBootstrapperFactory(nil)

require.Nil(t, sebf)
require.Equal(t, errors.ErrNilEpochStartBootstrapperFactory, err)

esbf := NewEpochStartBootstrapperFactory()
sebf, err = NewSovereignEpochStartBootstrapperFactory(esbf)

require.Nil(t, err)
require.NotNil(t, sebf)
sebf := NewSovereignEpochStartBootstrapperFactory()
require.False(t, sebf.IsInterfaceNil())
}

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

esbf := NewEpochStartBootstrapperFactory()
sebf, _ := NewSovereignEpochStartBootstrapperFactory(esbf)

sebf := NewSovereignEpochStartBootstrapperFactory()
seb, err := sebf.CreateEpochStartBootstrapper(getDefaultArgs())

require.Nil(t, err)
require.NotNil(t, seb)
require.Equal(t, "*bootstrap.sovereignChainEpochStartBootstrap", fmt.Sprintf("%T", seb))
}

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

esbf := NewEpochStartBootstrapperFactory()
sebf, _ := NewSovereignEpochStartBootstrapperFactory(esbf)

sebf := NewSovereignEpochStartBootstrapperFactory()
arg := ArgsStorageEpochStartBootstrap{
ArgsEpochStartBootstrap: getDefaultArgs(),
ImportDbConfig: config.ImportDbConfig{},
ChanGracefullyClose: make(chan endProcess.ArgEndProcess, 1),
TimeToWaitForRequestedData: 1,
}
esb, err := sebf.CreateStorageEpochStartBootstrapper(arg)

require.Nil(t, err)
require.NotNil(t, esb)

}

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

esbf := NewEpochStartBootstrapperFactory()
sebf, _ := NewSovereignEpochStartBootstrapperFactory(esbf)

require.False(t, sebf.IsInterfaceNil())
require.Equal(t, "*bootstrap.storageEpochStartBootstrap", fmt.Sprintf("%T", esb))

sebf = (*sovereignEpochStartBootstrapperFactory)(nil)
require.True(t, sebf.IsInterfaceNil())
}
97 changes: 16 additions & 81 deletions factory/runType/runTypeComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,79 +156,14 @@ func NewRunTypeComponentsFactory(args ArgsRunTypeComponents) (*runTypeComponents
}, nil
}

// TODO remove the error from the factories where it's possible - MX-15415
// Create creates the runType components
func (rcf *runTypeComponentsFactory) Create() (*runTypeComponents, error) {
blockChainHookHandlerFactory, err := hooks.NewBlockChainHookFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewBlockChainHookFactory failed: %w", err)
}

epochStartBootstrapperFactory := bootstrap.NewEpochStartBootstrapperFactory()
bootstrapperFromStorageFactory := storageBootstrap.NewShardStorageBootstrapperFactory()

shardBootstrapFactory, err := storageBootstrap.NewShardBootstrapFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewShardBootstrapFactory failed: %w", err)
}

blockProcessorFactory, err := block.NewShardBlockProcessorFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewShardBlockProcessorFactory failed: %w", err)
}

forkDetectorFactory, err := sync.NewShardForkDetectorFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewShardForkDetectorFactory failed: %w", err)
}

blockTrackerFactory, err := track.NewShardBlockTrackerFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewShardBlockTrackerFactory failed: %w", err)
}

requestHandlerFactory := requestHandlers.NewResolverRequestHandlerFactory()

headerValidatorFactory, err := block.NewShardHeaderValidatorFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewShardHeaderValidatorFactory failed: %w", err)
}

scheduledTxsExecutionFactory, err := preprocess.NewShardScheduledTxsExecutionFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewSovereignScheduledTxsExecutionFactory failed: %w", err)
}

scResultsPreProcessorCreator, err := preprocess.NewSmartContractResultPreProcessorFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewSmartContractResultPreProcessorFactory failed: %w", err)
}

transactionCoordinatorFactory, err := coordinator.NewShardTransactionCoordinatorFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewShardTransactionCoordinatorFactory failed: %w", err)
}

validatorStatisticsProcessorFactory, err := peer.NewValidatorStatisticsProcessorFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewShardBlockProcessorFactory failed: %w", err)
}

additionalStorageServiceCreator, err := storageFactory.NewShardAdditionalStorageServiceFactory()
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewShardAdditionalStorageServiceFactory failed: %w", err)
}

scProcessorCreator := processProxy.NewSCProcessProxyFactory()

vmContextCreator := systemSmartContracts.NewVMContextCreator()
vmContainerMetaCreator, err := factoryVm.NewVmContainerMetaFactory(vmContextCreator)
if err != nil {
return nil, fmt.Errorf("runTypeComponentsFactory - NewVmContainerMetaFactory failed: %w", err)
}

vmContainerShardCreator := factoryVm.NewVmContainerShardFactory()

totalSupply, ok := big.NewInt(0).SetString(rcf.configs.EconomicsConfig.GlobalSettings.GenesisTotalSupply, 10)
if !ok {
return nil, fmt.Errorf("can not parse total suply from economics.toml, %s is not a valid value",
Expand Down Expand Up @@ -273,24 +208,24 @@ func (rcf *runTypeComponentsFactory) Create() (*runTypeComponents, error) {
}

return &runTypeComponents{
blockChainHookHandlerCreator: blockChainHookHandlerFactory,
epochStartBootstrapperCreator: epochStartBootstrapperFactory,
bootstrapperFromStorageCreator: bootstrapperFromStorageFactory,
bootstrapperCreator: shardBootstrapFactory,
blockProcessorCreator: blockProcessorFactory,
forkDetectorCreator: forkDetectorFactory,
blockTrackerCreator: blockTrackerFactory,
requestHandlerCreator: requestHandlerFactory,
headerValidatorCreator: headerValidatorFactory,
scheduledTxsExecutionCreator: scheduledTxsExecutionFactory,
transactionCoordinatorCreator: transactionCoordinatorFactory,
validatorStatisticsProcessorCreator: validatorStatisticsProcessorFactory,
additionalStorageServiceCreator: additionalStorageServiceCreator,
scProcessorCreator: scProcessorCreator,
scResultPreProcessorCreator: scResultsPreProcessorCreator,
blockChainHookHandlerCreator: hooks.NewBlockChainHookFactory(),
epochStartBootstrapperCreator: bootstrap.NewEpochStartBootstrapperFactory(),
bootstrapperFromStorageCreator: storageBootstrap.NewShardStorageBootstrapperFactory(),
bootstrapperCreator: storageBootstrap.NewShardBootstrapFactory(),
blockProcessorCreator: block.NewShardBlockProcessorFactory(),
forkDetectorCreator: sync.NewShardForkDetectorFactory(),
blockTrackerCreator: track.NewShardBlockTrackerFactory(),
requestHandlerCreator: requestHandlers.NewResolverRequestHandlerFactory(),
headerValidatorCreator: block.NewShardHeaderValidatorFactory(),
scheduledTxsExecutionCreator: preprocess.NewShardScheduledTxsExecutionFactory(),
transactionCoordinatorCreator: coordinator.NewShardTransactionCoordinatorFactory(),
validatorStatisticsProcessorCreator: peer.NewValidatorStatisticsProcessorFactory(),
additionalStorageServiceCreator: storageFactory.NewShardAdditionalStorageServiceFactory(),
scProcessorCreator: processProxy.NewSCProcessProxyFactory(),
scResultPreProcessorCreator: preprocess.NewSmartContractResultPreProcessorFactory(),
consensusModel: consensus.ConsensusModelV1,
vmContainerMetaFactory: vmContainerMetaCreator,
vmContainerShardFactory: vmContainerShardCreator,
vmContainerShardFactory: factoryVm.NewVmContainerShardFactory(),
accountsParser: accountsParser,
accountsCreator: accountsCreator,
vmContextCreator: vmContextCreator,
Expand Down
32 changes: 5 additions & 27 deletions factory/runType/sovereignRunTypeComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,23 +94,6 @@ func (rcf *sovereignRunTypeComponentsFactory) Create() (*runTypeComponents, erro
return nil, err
}

sovBlockChainHookHandlerFactory, err := hooks.NewSovereignBlockChainHookFactory(rtc.blockChainHookHandlerCreator)
if err != nil {
return nil, fmt.Errorf("sovereignRunTypeComponentsFactory - NewSovereignBlockChainHookFactory failed: %w", err)
}

epochStartBootstrapperFactory, err := bootstrap.NewSovereignEpochStartBootstrapperFactory(rtc.epochStartBootstrapperCreator)
if err != nil {
return nil, fmt.Errorf("sovereignRunTypeComponentsFactory - NewSovereignEpochStartBootstrapperFactory failed: %w", err)
}

bootstrapperFromStorageFactory := storageBootstrap.NewSovereignShardStorageBootstrapperFactory()

bootstrapperFactory, err := storageBootstrap.NewSovereignShardBootstrapFactory(rtc.bootstrapperCreator)
if err != nil {
return nil, fmt.Errorf("sovereignRunTypeComponentsFactory - NewSovereignShardBootstrapFactory failed: %w", err)
}

blockProcessorFactory, err := block.NewSovereignBlockProcessorFactory(rtc.blockProcessorCreator)
if err != nil {
return nil, fmt.Errorf("sovereignRunTypeComponentsFactory - NewSovereignBlockProcessorFactory failed: %w", err)
Expand All @@ -136,11 +119,6 @@ func (rcf *sovereignRunTypeComponentsFactory) Create() (*runTypeComponents, erro
return nil, fmt.Errorf("sovereignRunTypeComponentsFactory - NewSovereignHeaderValidatorFactory failed: %w", err)
}

scheduledTxsExecutionFactory, err := preprocess.NewSovereignScheduledTxsExecutionFactory()
if err != nil {
return nil, fmt.Errorf("sovereignRunTypeComponentsFactory - NewSovereignScheduledTxsExecutionFactory failed: %w", err)
}

transactionCoordinatorFactory, err := coordinator.NewSovereignTransactionCoordinatorFactory(rtc.transactionCoordinatorCreator)
if err != nil {
return nil, fmt.Errorf("sovereignRunTypeComponentsFactory - NewSovereignTransactionCoordinatorFactory failed: %w", err)
Expand Down Expand Up @@ -234,16 +212,16 @@ func (rcf *sovereignRunTypeComponentsFactory) Create() (*runTypeComponents, erro
}

return &runTypeComponents{
blockChainHookHandlerCreator: sovBlockChainHookHandlerFactory,
epochStartBootstrapperCreator: epochStartBootstrapperFactory,
bootstrapperFromStorageCreator: bootstrapperFromStorageFactory,
bootstrapperCreator: bootstrapperFactory,
blockChainHookHandlerCreator: hooks.NewSovereignBlockChainHookFactory(),
epochStartBootstrapperCreator: bootstrap.NewSovereignEpochStartBootstrapperFactory(),
bootstrapperFromStorageCreator: storageBootstrap.NewSovereignShardStorageBootstrapperFactory(),
bootstrapperCreator: storageBootstrap.NewSovereignShardBootstrapFactory(),
blockProcessorCreator: blockProcessorFactory,
forkDetectorCreator: forkDetectorFactory,
blockTrackerCreator: blockTrackerFactory,
requestHandlerCreator: requestHandlerFactory,
headerValidatorCreator: headerValidatorFactory,
scheduledTxsExecutionCreator: scheduledTxsExecutionFactory,
scheduledTxsExecutionCreator: preprocess.NewSovereignScheduledTxsExecutionFactory(),
transactionCoordinatorCreator: transactionCoordinatorFactory,
validatorStatisticsProcessorCreator: validatorStatisticsProcessorFactory,
additionalStorageServiceCreator: storageFactory.NewSovereignAdditionalStorageServiceFactory(),
Expand Down
14 changes: 2 additions & 12 deletions integrationTests/testProcessorNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,7 +1028,7 @@ func (tpn *TestProcessorNode) createFullSCQueryService(gasMap map[string]map[str
argsHook.BlockChain = apiBlockchain

esdtTransferParser, _ := parsers.NewESDTTransferParser(TestMarshalizer)
blockChainHookImpl, _ := CreateBlockChainHook(argsHook)
blockChainHookImpl, _ := hooks.NewBlockChainHookImpl(argsHook)
argsNewVMFactory := shard.ArgVMContainerFactory{
Config: *vmConfig,
BlockChainHook: blockChainHookImpl,
Expand Down Expand Up @@ -1694,7 +1694,7 @@ func (tpn *TestProcessorNode) initInnerProcessors(gasMap map[string]map[string]u
}

maxGasLimitPerBlock := uint64(0xFFFFFFFFFFFFFFFF)
blockChainHookImpl, _ := CreateBlockChainHook(argsHook)
blockChainHookImpl, _ := hooks.NewBlockChainHookImpl(argsHook)
tpn.EnableEpochs.FailExecutionOnEveryAPIErrorEnableEpoch = 1
argsNewVMFactory := shard.ArgVMContainerFactory{
Config: *vmConfig,
Expand Down Expand Up @@ -3651,13 +3651,3 @@ func GetDefaultEnableEpochsConfig() *config.EnableEpochs {
StakingV4Step2EnableEpoch: UnreachableEpoch,
}
}

// CreateBlockChainHook creates a blockchain hook based on the chain run type (normal/sovereign)
func CreateBlockChainHook(args hooks.ArgBlockChainHook) (process.BlockChainHookWithAccountsAdapter, error) {
blockChainHookFactory, err := hooks.NewBlockChainHookFactory()
if err != nil {
return nil, err
}

return blockChainHookFactory.CreateBlockChainHookHandler(args)
}
5 changes: 3 additions & 2 deletions process/block/preprocess/shardScheduledTxsExecutionFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package preprocess
import (
"github.com/multiversx/mx-chain-core-go/hashing"
"github.com/multiversx/mx-chain-core-go/marshal"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/sharding"
Expand All @@ -24,8 +25,8 @@ type shardScheduledTxsExecutionFactory struct {
}

// NewShardScheduledTxsExecutionFactory creates a new shard scheduled txs execution factory
func NewShardScheduledTxsExecutionFactory() (*shardScheduledTxsExecutionFactory, error) {
return &shardScheduledTxsExecutionFactory{}, nil
func NewShardScheduledTxsExecutionFactory() *shardScheduledTxsExecutionFactory {
return &shardScheduledTxsExecutionFactory{}
}

// CreateScheduledTxsExecutionHandler creates a new scheduled txs execution handler for shard chain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ package preprocess
import (
"testing"

"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-go/testscommon"
commonMock "github.com/multiversx/mx-chain-go/testscommon/common"
"github.com/multiversx/mx-chain-go/testscommon/genericMocks"
"github.com/stretchr/testify/require"
)

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

stef, err := NewShardScheduledTxsExecutionFactory()
require.Nil(t, err)
stef := NewShardScheduledTxsExecutionFactory()
require.NotNil(t, stef)
require.IsType(t, &shardScheduledTxsExecutionFactory{}, stef)
}

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

stef, _ := NewShardScheduledTxsExecutionFactory()
stef := NewShardScheduledTxsExecutionFactory()

stxeh, err := stef.CreateScheduledTxsExecutionHandler(ScheduledTxsExecutionFactoryArgs{})
require.NotNil(t, err)
Expand All @@ -44,6 +44,6 @@ func TestShardScheduledTxsExecutionFactory_CreateScheduledTxsExecutionHandler(t
func TestShardScheduledTxsExecutionFactory_IsInterfaceNil(t *testing.T) {
t.Parallel()

stef, _ := NewShardScheduledTxsExecutionFactory()
stef := NewShardScheduledTxsExecutionFactory()
require.False(t, stef.IsInterfaceNil())
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type smartContractResultPreProcessorFactory struct {
}

// NewSmartContractResultPreProcessorFactory creates a new smart contract result pre processor factory
func NewSmartContractResultPreProcessorFactory() (*smartContractResultPreProcessorFactory, error) {
return &smartContractResultPreProcessorFactory{}, nil
func NewSmartContractResultPreProcessorFactory() *smartContractResultPreProcessorFactory {
return &smartContractResultPreProcessorFactory{}
}

// CreateSmartContractResultPreProcessor creates a new smart contract result pre processor
Expand Down
Loading