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 config flag and run type components #470

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 38 additions & 1 deletion cmd/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ import (
marshalFactory "github.com/multiversx/mx-chain-core-go/marshal/factory"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-logger-go/file"
"github.com/urfave/cli"

"github.com/multiversx/mx-chain-proxy-go/api"
"github.com/multiversx/mx-chain-proxy-go/common"
"github.com/multiversx/mx-chain-proxy-go/config"
"github.com/multiversx/mx-chain-proxy-go/data"
"github.com/multiversx/mx-chain-proxy-go/factory"
"github.com/multiversx/mx-chain-proxy-go/factory/runType"
"github.com/multiversx/mx-chain-proxy-go/metrics"
"github.com/multiversx/mx-chain-proxy-go/observer"
"github.com/multiversx/mx-chain-proxy-go/process"
"github.com/multiversx/mx-chain-proxy-go/process/cache"
processFactory "github.com/multiversx/mx-chain-proxy-go/process/factory"
"github.com/multiversx/mx-chain-proxy-go/testing"
versionsFactory "github.com/multiversx/mx-chain-proxy-go/versions/factory"
"github.com/urfave/cli"
)

const (
Expand Down Expand Up @@ -160,6 +163,11 @@ VERSION:
Name: "start-swagger-ui",
Usage: "If set to true, will start a Swagger UI on the root",
}
// sovereignConfig defines a flag that specifies if what run type components should use
sovereignConfig = cli.BoolFlag{
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
Name: "sovereign-config",
Usage: "If set to true, will use sovereign run type components",
}

testServer *testing.TestHttpServer
)
Expand All @@ -184,6 +192,7 @@ func main() {
workingDirectory,
memBallast,
startSwaggerUI,
sovereignConfig,
}
app.Authors = []cli.Author{
{
Expand Down Expand Up @@ -373,6 +382,7 @@ func createVersionsRegistryTestOrProduction(
ctx.GlobalString(walletKeyPemFile.Name),
ctx.GlobalString(apiConfigDirectory.Name),
closableComponents,
ctx.GlobalBool(sovereignConfig.Name),
)
}

Expand All @@ -383,6 +393,7 @@ func createVersionsRegistryTestOrProduction(
ctx.GlobalString(walletKeyPemFile.Name),
ctx.GlobalString(apiConfigDirectory.Name),
closableComponents,
ctx.GlobalBool(sovereignConfig.Name),
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
)
}

Expand All @@ -393,6 +404,7 @@ func createVersionsRegistry(
pemFileLocation string,
apiConfigDirectoryPath string,
closableComponents *data.ClosableComponentsHandler,
isSovereignConfig bool,
) (data.VersionsRegistryHandler, error) {
pubKeyConverter, err := pubkeyConverter.NewBech32PubkeyConverter(cfg.AddressPubkeyConverter.Length, addressHRP)
if err != nil {
Expand Down Expand Up @@ -459,12 +471,23 @@ func createVersionsRegistry(
return nil, err
}

var runTypeComponents factory.RunTypeComponentsHandler
if isSovereignConfig {
runTypeComponents, err = createManagedRunTypeComponents(runType.NewSovereignRunTypeComponentsFactory())
} else {
runTypeComponents, err = createManagedRunTypeComponents(runType.NewRunTypeComponentsFactory())
}
if err != nil {
return nil, err
}

txProc, err := processFactory.CreateTransactionProcessor(
bp,
pubKeyConverter,
hasher,
marshalizer,
cfg.GeneralSettings.AllowEntireTxPoolFetch,
runTypeComponents,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -561,6 +584,20 @@ func createVersionsRegistry(
return versionsFactory.CreateVersionsRegistry(facadeArgs, apiConfigParser)
}

func createManagedRunTypeComponents(factory runType.RunTypeComponentsCreator) (factory.RunTypeComponentsHandler, error) {
managedRunTypeComponents, err := runType.NewManagedRunTypeComponents(factory)
if err != nil {
return nil, err
}

err = managedRunTypeComponents.Create()
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}

return managedRunTypeComponents, nil
}

func startWebServer(
versionsRegistry data.VersionsRegistryHandler,
generalConfig *config.Config,
Expand Down
8 changes: 8 additions & 0 deletions factory/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package factory

import (
"errors"
)

// ErrNilRunTypeComponents signals that nil run type components were provided
var ErrNilRunTypeComponents = errors.New("nil run type components")
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
24 changes: 24 additions & 0 deletions factory/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package factory

// ComponentHandler defines the actions common to all component handlers
type ComponentHandler interface {
Create() error
Close() error
CheckSubcomponents() error
String() string
}

// RunTypeComponentsHandler defines the run type components handler actions
type RunTypeComponentsHandler interface {
ComponentHandler
RunTypeComponentsHolder
}

// RunTypeComponentsHolder holds the run type components
type RunTypeComponentsHolder interface {
Create() error
Close() error
CheckSubcomponents() error
String() string
IsInterfaceNil() bool
}
7 changes: 7 additions & 0 deletions factory/runType/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package runType

// RunTypeComponentsCreator is the interface for the runTypeComponentsCreator
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
type RunTypeComponentsCreator interface {
Create() *runTypeComponents
IsInterfaceNil() bool
}
13 changes: 13 additions & 0 deletions factory/runType/runTypeComponents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package runType

type runTypeComponents struct{}

// Close does nothing
func (rtc *runTypeComponents) Close() error {
return nil
}

// IsInterfaceNil returns true if there is no value under the interface
func (rtc *runTypeComponents) IsInterfaceNil() bool {
return rtc == nil
}
18 changes: 18 additions & 0 deletions factory/runType/runTypeComponentsFactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package runType

type runTypeComponentsFactory struct{}

// NewRunTypeComponentsFactory will return a new instance of run type components factory
func NewRunTypeComponentsFactory() *runTypeComponentsFactory {
return &runTypeComponentsFactory{}
}

// Create will create the run type components
func (rtcf *runTypeComponentsFactory) Create() *runTypeComponents {
return &runTypeComponents{}
}

// IsInterfaceNil returns true if there is no value under the interface
func (rtcf *runTypeComponentsFactory) IsInterfaceNil() bool {
return rtcf == nil
}
83 changes: 83 additions & 0 deletions factory/runType/runTypeComponentsHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package runType

import (
"sync"

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

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

const runTypeComponentsName = "managedRunTypeComponents"

var _ factory.ComponentHandler = (*managedRunTypeComponents)(nil)
var _ factory.RunTypeComponentsHandler = (*managedRunTypeComponents)(nil)
var _ factory.RunTypeComponentsHolder = (*managedRunTypeComponents)(nil)

type managedRunTypeComponents struct {
*runTypeComponents
factory RunTypeComponentsCreator
mutRunTypeCoreComponents sync.RWMutex
}

// NewManagedRunTypeComponents returns a news instance of managed runType core components
func NewManagedRunTypeComponents(rtc RunTypeComponentsCreator) (*managedRunTypeComponents, error) {
if rtc == nil {
return nil, factory.ErrNilRunTypeComponents
}

return &managedRunTypeComponents{
runTypeComponents: nil,
factory: rtc,
}, nil
}

// Create will create the managed components
func (mrtc *managedRunTypeComponents) Create() error {
rtc := mrtc.factory.Create()

mrtc.mutRunTypeCoreComponents.Lock()
mrtc.runTypeComponents = rtc
mrtc.mutRunTypeCoreComponents.Unlock()

return nil
}

// Close will close all underlying subcomponents
func (mrtc *managedRunTypeComponents) Close() error {
mrtc.mutRunTypeCoreComponents.Lock()
defer mrtc.mutRunTypeCoreComponents.Unlock()

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

err := mrtc.runTypeComponents.Close()
if err != nil {
return err
}
mrtc.runTypeComponents = nil

return nil
}

// CheckSubcomponents verifies all subcomponents
func (mrtc *managedRunTypeComponents) CheckSubcomponents() error {
mrtc.mutRunTypeCoreComponents.RLock()
defer mrtc.mutRunTypeCoreComponents.RUnlock()

if check.IfNil(mrtc.runTypeComponents) {
return factory.ErrNilRunTypeComponents
}
return nil
}

// IsInterfaceNil returns true if the interface is nil
func (mrtc *managedRunTypeComponents) IsInterfaceNil() bool {
return mrtc == nil
}

// String returns the name of the component
func (mrtc *managedRunTypeComponents) String() string {
return runTypeComponentsName
}
76 changes: 76 additions & 0 deletions factory/runType/runTypeComponentsHandler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package runType

import (
"testing"

"github.com/stretchr/testify/require"

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

func createComponents() (factory.RunTypeComponentsHandler, error) {
rtcf := NewRunTypeComponentsFactory()
return NewManagedRunTypeComponents(rtcf)
}

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

t.Run("should error", func(t *testing.T) {
managedRunTypeComponents, err := NewManagedRunTypeComponents(nil)
require.ErrorIs(t, err, factory.ErrNilRunTypeComponents)
require.True(t, managedRunTypeComponents.IsInterfaceNil())
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved
})
t.Run("should work", func(t *testing.T) {
rtcf := NewRunTypeComponentsFactory()
managedRunTypeComponents, err := NewManagedRunTypeComponents(rtcf)
require.NoError(t, err)
require.False(t, managedRunTypeComponents.IsInterfaceNil())
})
}

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

t.Run("should work with getters", func(t *testing.T) {
t.Parallel()

managedRunTypeComponents, err := createComponents()
require.NoError(t, err)

err = managedRunTypeComponents.Create()
require.NoError(t, err)

require.Equal(t, runTypeComponentsName, managedRunTypeComponents.String())
require.NoError(t, managedRunTypeComponents.Close())
})
}

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

managedRunTypeComponents, _ := createComponents()
require.NoError(t, managedRunTypeComponents.Close())

err := managedRunTypeComponents.Create()
require.NoError(t, err)

require.NoError(t, managedRunTypeComponents.Close())
}

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

managedRunTypeComponents, _ := createComponents()
err := managedRunTypeComponents.CheckSubcomponents()
require.Equal(t, factory.ErrNilRunTypeComponents, err)

err = managedRunTypeComponents.Create()
require.NoError(t, err)

//TODO check for nil each subcomponent - MX-15371
err = managedRunTypeComponents.CheckSubcomponents()
require.NoError(t, err)

require.NoError(t, managedRunTypeComponents.Close())
}
38 changes: 38 additions & 0 deletions factory/runType/runTypeComponents_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package runType

import (
"testing"

"github.com/stretchr/testify/require"
)

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

t.Run("should work", func(t *testing.T) {
rtc := NewRunTypeComponentsFactory()
require.NotNil(t, rtc)
})
}

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

rtcf := NewRunTypeComponentsFactory()
require.NotNil(t, rtcf)

rtc := rtcf.Create()
require.NotNil(t, rtc)
}

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

rtcf := NewRunTypeComponentsFactory()
require.NotNil(t, rtcf)

rtc := rtcf.Create()
require.NotNil(t, rtc)

require.NoError(t, rtc.Close())
}
Loading
Loading