Skip to content

Commit

Permalink
refactor the implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
axenteoctavian committed Dec 6, 2024
1 parent 368e17a commit caba123
Show file tree
Hide file tree
Showing 27 changed files with 118 additions and 77 deletions.
12 changes: 9 additions & 3 deletions cmd/sovereignnode/chainSimulator/sovereignChainSimulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewSovereignChainSimulator(args ArgsSovereignChainSimulator) (chainSimulato
}

args.CreateRunTypeCoreComponents = func() (factory.RunTypeCoreComponentsHolder, error) {
return createSovereignRunTypeCoreComponents()
return createSovereignRunTypeCoreComponents(*configs.SovereignEpochConfig)
}
args.CreateIncomingHeaderSubscriber = func(config config.WebSocketConfig, dataPool dataRetriever.PoolsHolder, mainChainNotarizationStartRound uint64, runTypeComponents factory.RunTypeComponentsHolder) (process.IncomingHeaderSubscriber, error) {
return incomingHeader.CreateIncomingHeaderProcessor(config, dataPool, mainChainNotarizationStartRound, runTypeComponents)
Expand Down Expand Up @@ -85,17 +85,23 @@ func loadSovereignConfigs(configsPath string) (*sovereignConfig.SovereignConfig,
return nil, err
}

sovereignEpochConfig, err := sovereignConfig.LoadSovereignEpochConfig(path.Join(configsPath, "enableEpochs.toml"))
if err != nil {
return nil, err
}

return &sovereignConfig.SovereignConfig{
Configs: &config.Configs{
EpochConfig: epochConfig,
EconomicsConfig: economicsConfig,
},
SovereignExtraConfig: sovereignExtraConfig,
SovereignEpochConfig: sovereignEpochConfig,
}, nil
}

func createSovereignRunTypeCoreComponents() (factory.RunTypeCoreComponentsHolder, error) {
sovereignRunTypeCoreComponentsFactory := runType.NewSovereignRunTypeCoreComponentsFactory()
func createSovereignRunTypeCoreComponents(sovereignEpochConfig config.SovereignEpochConfig) (factory.RunTypeCoreComponentsHolder, error) {
sovereignRunTypeCoreComponentsFactory := runType.NewSovereignRunTypeCoreComponentsFactory(sovereignEpochConfig)
managedRunTypeCoreComponents, err := runType.NewManagedRunTypeCoreComponents(sovereignRunTypeCoreComponentsFactory)
if err != nil {
return nil, err
Expand Down
12 changes: 12 additions & 0 deletions cmd/sovereignnode/config/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

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

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

Expand All @@ -15,3 +16,14 @@ func LoadSovereignGeneralConfig(filepath string) (*config.SovereignConfig, error

return cfg, nil
}

// LoadSovereignEpochConfig returns the epoch config necessary by sovereign by reading it from the provided file
func LoadSovereignEpochConfig(filepath string) (*config.SovereignEpochConfig, error) {
cfg := &config.SovereignEpochConfig{}
err := core.LoadTomlFile(cfg, filepath)
if err != nil {
return nil, err
}

return cfg, nil
}
1 change: 1 addition & 0 deletions cmd/sovereignnode/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ import "github.com/multiversx/mx-chain-go/config"
type SovereignConfig struct {
*config.Configs
SovereignExtraConfig *config.SovereignConfig
SovereignEpochConfig *config.SovereignEpochConfig
}
15 changes: 12 additions & 3 deletions cmd/sovereignnode/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
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-go/cmd/node/factory"
"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/config/overridableConfig"
sovereignConfig "github.com/multiversx/mx-chain-go/sovereignnode/config"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-logger-go/file"
"github.com/urfave/cli"
// test point 1 for custom profiler
)

Expand Down Expand Up @@ -239,6 +240,13 @@ func readConfigs(ctx *cli.Context, log logger.Logger) (*sovereignConfig.Sovereig
}
log.Debug("config", "file", sovereignExtraConfigPath)

configurationPaths.Epoch = ctx.GlobalString(epochConfigurationFile.Name)
sovereignEpochConfig, err := sovereignConfig.LoadSovereignEpochConfig(configurationPaths.Epoch)
if err != nil {
return nil, err
}
log.Debug("config", "file", configurationPaths.Epoch)

sovereignExtraConfig.OutGoingBridgeCertificate = config.OutGoingBridgeCertificate{
CertificatePath: ctx.GlobalString(sovereignBridgeCertificateFile.Name),
CertificatePkPath: ctx.GlobalString(sovereignBridgeCertificatePkFile.Name),
Expand Down Expand Up @@ -277,6 +285,7 @@ func readConfigs(ctx *cli.Context, log logger.Logger) (*sovereignConfig.Sovereig
RoundConfig: roundConfig,
},
SovereignExtraConfig: sovereignExtraConfig,
SovereignEpochConfig: sovereignEpochConfig,
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions common/enablers/enableEpochsFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func NewEnableEpochsFactory() EnableEpochsFactory {
}

// CreateEnableEpochsHandler creates an enable epochs handler for regular chain
func (eef *enableEpochsFactory) CreateEnableEpochsHandler(epochConfig config.EpochConfig, epochNotifier process.EpochNotifier) (common.EnableEpochsHandler, error) {
return NewEnableEpochsHandler(epochConfig.EnableEpochs, epochNotifier)
func (eef *enableEpochsFactory) CreateEnableEpochsHandler(enableEpochs config.EnableEpochs, epochNotifier process.EpochNotifier) (common.EnableEpochsHandler, error) {
return NewEnableEpochsHandler(enableEpochs, epochNotifier)
}

// IsInterfaceNil checks if the underlying pointer is nil
Expand Down
2 changes: 1 addition & 1 deletion common/enablers/enableEpochsFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestEnableEpochsFactory_CreateEnableEpochsHandler(t *testing.T) {
factory := NewEnableEpochsFactory()
require.False(t, factory.IsInterfaceNil())

eeh, err := factory.CreateEnableEpochsHandler(config.EpochConfig{}, &epochNotifier.EpochNotifierStub{})
eeh, err := factory.CreateEnableEpochsHandler(config.EnableEpochs{}, &epochNotifier.EpochNotifierStub{})
require.Nil(t, err)
require.NotNil(t, eeh)
require.IsType(t, &enableEpochsHandler{}, eeh)
Expand Down
2 changes: 1 addition & 1 deletion common/enablers/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import "errors"
var errMissingRoundActivation = errors.New("missing round activation definition")

// ErrNilEnableEpochsFactory signals that a nil enable epochs factory has been provided
var ErrNilEnableEpochsFactory = errors.New("nil enable epochs factory")
var ErrNilEnableEpochsFactory = errors.New("nil enable epochs factory has been provided")
2 changes: 1 addition & 1 deletion common/enablers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (

// EnableEpochsFactory defines enable epochs handler factory behavior
type EnableEpochsFactory interface {
CreateEnableEpochsHandler(epochConfig config.EpochConfig, epochNotifier process.EpochNotifier) (common.EnableEpochsHandler, error)
CreateEnableEpochsHandler(enableEpochs config.EnableEpochs, epochNotifier process.EpochNotifier) (common.EnableEpochsHandler, error)
IsInterfaceNil() bool
}
15 changes: 9 additions & 6 deletions common/enablers/sovereignEnableEpochsFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import (
"github.com/multiversx/mx-chain-go/process"
)

type sovereignEnableEpochsFactory struct{}
type sovereignEnableEpochsFactory struct {
sovereignEpochConfig config.SovereignEpochConfig
}

// NewSovereignEnableEpochsFactory creates an enable epochs factory for sovereign chain
func NewSovereignEnableEpochsFactory() EnableEpochsFactory {
return &sovereignEnableEpochsFactory{}
func NewSovereignEnableEpochsFactory(sovereignEpochConfig config.SovereignEpochConfig) EnableEpochsFactory {
return &sovereignEnableEpochsFactory{
sovereignEpochConfig: sovereignEpochConfig,
}
}

// CreateEnableEpochsHandler creates an enable epochs handler for sovereign chain
func (seef *sovereignEnableEpochsFactory) CreateEnableEpochsHandler(epochConfig config.EpochConfig, epochNotifier process.EpochNotifier) (common.EnableEpochsHandler, error) {
return NewSovereignEnableEpochsHandler(epochConfig.EnableEpochs, epochConfig.SovereignEnableEpochs, epochConfig.SovereignChainSpecificEnableEpochs,
epochNotifier)
func (seef *sovereignEnableEpochsFactory) CreateEnableEpochsHandler(enableEpochs config.EnableEpochs, epochNotifier process.EpochNotifier) (common.EnableEpochsHandler, error) {
return NewSovereignEnableEpochsHandler(enableEpochs, seef.sovereignEpochConfig, epochNotifier)
}

// IsInterfaceNil checks if the underlying pointer is nil
Expand Down
4 changes: 2 additions & 2 deletions common/enablers/sovereignEnableEpochsFactory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
func TestSovereignEnableEpochsFactory_CreateEnableEpochsHandler(t *testing.T) {
t.Parallel()

factory := NewSovereignEnableEpochsFactory()
factory := NewSovereignEnableEpochsFactory(config.SovereignEpochConfig{})
require.False(t, factory.IsInterfaceNil())

eeh, err := factory.CreateEnableEpochsHandler(config.EpochConfig{}, &epochNotifier.EpochNotifierStub{})
eeh, err := factory.CreateEnableEpochsHandler(config.EnableEpochs{}, &epochNotifier.EpochNotifierStub{})
require.Nil(t, err)
require.NotNil(t, eeh)
require.IsType(t, &sovereignEnableEpochsHandler{}, eeh)
Expand Down
7 changes: 3 additions & 4 deletions common/enablers/sovereignEnableEpochsHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ type sovereignEnableEpochsHandler struct {
// NewSovereignEnableEpochsHandler creates a new instance of sovereign enable epochs handler
func NewSovereignEnableEpochsHandler(
enableEpochsConfig config.EnableEpochs,
sovereignEnableEpochsConfig config.SovereignEnableEpochs,
sovereignChainSpecificEnableEpochsConfig config.SovereignChainSpecificEnableEpochs,
sovereignEpochConfig config.SovereignEpochConfig,
epochNotifier process.EpochNotifier,
) (*sovereignEnableEpochsHandler, error) {
if check.IfNil(epochNotifier) {
Expand All @@ -29,8 +28,8 @@ func NewSovereignEnableEpochsHandler(
enableEpochsHandler: &enableEpochsHandler{
enableEpochsConfig: enableEpochsConfig,
},
sovereignEnableEpochsConfig: sovereignEnableEpochsConfig,
sovereignChainSpecificEnableEpochsConfig: sovereignChainSpecificEnableEpochsConfig,
sovereignEnableEpochsConfig: sovereignEpochConfig.SovereignEnableEpochs,
sovereignChainSpecificEnableEpochsConfig: sovereignEpochConfig.SovereignChainSpecificEnableEpochs,
}

sovHandler.createAllFlagsMap()
Expand Down
4 changes: 2 additions & 2 deletions common/enablers/sovereignEnableEpochsHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ func TestNewSovereignEnableEpochsHandler(t *testing.T) {
t.Run("nil epoch notifier should error", func(t *testing.T) {
t.Parallel()

sovHandler, err := NewSovereignEnableEpochsHandler(createEnableEpochsConfig(), config.SovereignEnableEpochs{}, config.SovereignChainSpecificEnableEpochs{}, nil)
sovHandler, err := NewSovereignEnableEpochsHandler(createEnableEpochsConfig(), config.SovereignEpochConfig{}, nil)
require.Equal(t, process.ErrNilEpochNotifier, err)
require.True(t, sovHandler.IsInterfaceNil())
})
t.Run("should work", func(t *testing.T) {
t.Parallel()

wasCalled := false
sovHandler, err := NewSovereignEnableEpochsHandler(createEnableEpochsConfig(), config.SovereignEnableEpochs{}, config.SovereignChainSpecificEnableEpochs{}, &epochNotifier.EpochNotifierStub{
sovHandler, err := NewSovereignEnableEpochsHandler(createEnableEpochsConfig(), config.SovereignEpochConfig{}, &epochNotifier.EpochNotifierStub{
RegisterNotifyHandlerCalled: func(handler vmcommon.EpochSubscriberHandler) {
wasCalled = true
},
Expand Down
12 changes: 2 additions & 10 deletions config/epochConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package config

// EpochConfig will hold epoch configurations
type EpochConfig struct {
EnableEpochs EnableEpochs
SovereignEnableEpochs SovereignEnableEpochs
SovereignChainSpecificEnableEpochs SovereignChainSpecificEnableEpochs
GasSchedule GasScheduleConfig
EnableEpochs EnableEpochs
GasSchedule GasScheduleConfig
}

// GasScheduleConfig represents the versioning config area for the gas schedule toml
Expand Down Expand Up @@ -128,12 +126,6 @@ type EnableEpochs struct {
BLSMultiSignerEnableEpoch []MultiSignerConfig
}

// SovereignEnableEpochs will hold the configuration for sovereign activation epochs
type SovereignEnableEpochs struct{}

// SovereignChainSpecificEnableEpochs will hold the configuration for sovereign chain specific activation epochs
type SovereignChainSpecificEnableEpochs struct{}

// GasScheduleByEpochs represents a gas schedule toml entry that will be applied from the provided epoch
type GasScheduleByEpochs struct {
StartEpoch uint32
Expand Down
13 changes: 13 additions & 0 deletions config/sovereignEpochConfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package config

// SovereignEpochConfig will hold sovereign epoch configurations
type SovereignEpochConfig struct {
SovereignEnableEpochs SovereignEnableEpochs
SovereignChainSpecificEnableEpochs SovereignChainSpecificEnableEpochs
}

// SovereignEnableEpochs will hold the configuration for sovereign activation epochs
type SovereignEnableEpochs struct{}

// SovereignChainSpecificEnableEpochs will hold the configuration for sovereign chain specific activation epochs
type SovereignChainSpecificEnableEpochs struct{}
2 changes: 1 addition & 1 deletion factory/core/coreComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (ccf *coreComponentsFactory) Create() (*coreComponents, error) {
}

epochNotifier := forking.NewGenericEpochNotifier()
enableEpochsHandler, err := ccf.enableEpochsFactory.CreateEnableEpochsHandler(ccf.epochConfig, epochNotifier)
enableEpochsHandler, err := ccf.enableEpochsFactory.CreateEnableEpochsHandler(ccf.epochConfig.EnableEpochs, epochNotifier)
if err != nil {
return nil, err
}
Expand Down
10 changes: 7 additions & 3 deletions factory/runType/sovereignRunTypeCoreComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ package runType

import (
"github.com/multiversx/mx-chain-go/common/enablers"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/process/rating"
"github.com/multiversx/mx-chain-go/sharding"
)

type sovereignRunTypeCoreComponentsFactory struct {
sovereignEpochConfig config.SovereignEpochConfig
}

// NewSovereignRunTypeCoreComponentsFactory will return a new instance of sovereign runType core components factory
func NewSovereignRunTypeCoreComponentsFactory() *sovereignRunTypeCoreComponentsFactory {
return &sovereignRunTypeCoreComponentsFactory{}
func NewSovereignRunTypeCoreComponentsFactory(sovereignEpochConfig config.SovereignEpochConfig) *sovereignRunTypeCoreComponentsFactory {
return &sovereignRunTypeCoreComponentsFactory{
sovereignEpochConfig: sovereignEpochConfig,
}
}

// Create will return a new instance of runType core components
func (srccf *sovereignRunTypeCoreComponentsFactory) Create() *runTypeCoreComponents {
return &runTypeCoreComponents{
genesisNodesSetupFactory: sharding.NewSovereignGenesisNodesSetupFactory(),
ratingsDataFactory: rating.NewSovereignRatingsDataFactory(),
enableEpochsFactory: enablers.NewSovereignEnableEpochsFactory(),
enableEpochsFactory: enablers.NewSovereignEnableEpochsFactory(srccf.sovereignEpochConfig),
}
}

Expand Down
3 changes: 2 additions & 1 deletion factory/runType/sovereignRunTypeCoreComponents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ import (

"github.com/stretchr/testify/require"

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

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

srccf := runType.NewSovereignRunTypeCoreComponentsFactory()
srccf := runType.NewSovereignRunTypeCoreComponentsFactory(config.SovereignEpochConfig{})
require.NotNil(t, srccf)

rcc := srccf.Create()
Expand Down
12 changes: 6 additions & 6 deletions genesis/process/genesisBlockCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func mustDoGenesisProcess(arg ArgsGenesisBlockCreator) bool {
}

func (gbc *genesisBlockCreator) createEmptyGenesisBlocks() (map[uint32]data.HeaderHandler, error) {
err := gbc.computeInitialDNSAddresses(createGenesisConfig(gbc.arg.EpochConfig))
err := gbc.computeInitialDNSAddresses(createGenesisConfig(gbc.arg.EpochConfig.EnableEpochs))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -308,7 +308,7 @@ func (gbc *genesisBlockCreator) CreateGenesisBlocks() (map[uint32]data.HeaderHan
return nil, err
}

err = gbc.computeInitialDNSAddresses(gbc.arg.EpochConfig)
err = gbc.computeInitialDNSAddresses(gbc.arg.EpochConfig.EnableEpochs)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -449,19 +449,19 @@ func (gbc *genesisBlockCreator) createHeaders(args *headerCreatorArgs) (map[uint
return genesisBlocks, nil
}

func (gbc *genesisBlockCreator) computeInitialDNSAddresses(epochsConfig config.EpochConfig) error {
func (gbc *genesisBlockCreator) computeInitialDNSAddresses(enableEpochsConfig config.EnableEpochs) error {
isForCurrentShard := func([]byte) bool {
// after hardfork we are interested only in the smart contract addresses, as they are already deployed
return true
}
initialAddresses := intermediate.GenerateInitialPublicKeys(genesis.InitialDNSAddress, isForCurrentShard)

return gbc.computeDNSAddresses(epochsConfig, initialAddresses)
return gbc.computeDNSAddresses(enableEpochsConfig, initialAddresses)
}

// in case of hardfork initial smart contracts deployment is not called as they are all imported from previous state
func (gbc *genesisBlockCreator) computeDNSAddresses(
epochsConfig config.EpochConfig,
enableEpochsConfig config.EnableEpochs,
initialAddresses [][]byte,
) error {
var dnsSC genesis.InitialSmartContractHandler
Expand All @@ -480,7 +480,7 @@ func (gbc *genesisBlockCreator) computeDNSAddresses(
Epoch: gbc.arg.StartEpochNum,
TimeStamp: gbc.arg.GenesisTime,
}
enableEpochsHandler, err := gbc.arg.EnableEpochsFactory.CreateEnableEpochsHandler(epochsConfig, epochNotifier)
enableEpochsHandler, err := gbc.arg.EnableEpochsFactory.CreateEnableEpochsHandler(enableEpochsConfig, epochNotifier)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion genesis/process/genesisBlockCreator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ func TestGenesisBlockCreator_CreateGenesisBlockAfterHardForkShouldCreateSCResult
)
hardForkGbc, err := NewGenesisBlockCreator(newArgs)
assert.Nil(t, err)
err = hardForkGbc.computeInitialDNSAddresses(gbc.arg.EpochConfig)
err = hardForkGbc.computeInitialDNSAddresses(gbc.arg.EpochConfig.EnableEpochs)
assert.Nil(t, err)

mapAfterHardForkAddresses, err := newArgs.SmartContractParser.GetDeployedSCAddresses(genesis.DNSType)
Expand Down
Loading

0 comments on commit caba123

Please sign in to comment.