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 chain simulator implementation #6134

Open
wants to merge 50 commits into
base: MX-15222-sovereign-chain-simulator
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
6863445
sovereign chain simulator implementation
axenteoctavian Apr 23, 2024
733c986
sovereign chain simulator fixes
axenteoctavian Apr 24, 2024
ce6d3cf
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian Apr 26, 2024
20f8147
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian Apr 26, 2024
f6a0baa
tests fix
axenteoctavian Apr 26, 2024
f4006aa
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian Apr 26, 2024
cf697d4
fix the config for chain simulator
axenteoctavian Apr 26, 2024
84f1025
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian Apr 26, 2024
5467acf
fixes after review
axenteoctavian Apr 26, 2024
0f69ab0
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 9, 2024
6c69519
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 9, 2024
f5ae43e
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 9, 2024
7facbef
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 14, 2024
de66c2f
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 17, 2024
bf0ca77
fixes after merge
axenteoctavian May 17, 2024
479b3d2
deleted unused func
axenteoctavian May 17, 2024
618b4a2
imports fix
axenteoctavian May 17, 2024
b53eacb
tests updates
axenteoctavian May 17, 2024
1e5bb53
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 17, 2024
365ae5a
fixes after merge
axenteoctavian May 17, 2024
8cb867d
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 20, 2024
5545e18
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 20, 2024
3279377
clean up
axenteoctavian May 20, 2024
c1ef183
fix imports
axenteoctavian May 20, 2024
f0202d2
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 20, 2024
a23c1a5
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 20, 2024
0a32e55
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 20, 2024
1975859
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 20, 2024
b37894b
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 21, 2024
341497d
fixes after merge
axenteoctavian May 21, 2024
89cfe67
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 22, 2024
52af475
fixes after merge
axenteoctavian May 22, 2024
de5a96c
some test fixes
axenteoctavian May 22, 2024
a541026
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 24, 2024
de7a972
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 24, 2024
4b28fac
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 24, 2024
9fe7f1a
use common tests
axenteoctavian May 24, 2024
a45cf74
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 27, 2024
4eb1af5
fixes after merge
axenteoctavian May 27, 2024
120e700
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 27, 2024
5673bc1
fix common func
axenteoctavian May 27, 2024
af4344f
fixes after review
axenteoctavian May 27, 2024
d053bca
fixes after review
axenteoctavian May 27, 2024
6bdbe83
common func comment
axenteoctavian May 27, 2024
473153f
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 29, 2024
df8f62f
fixes after review
axenteoctavian May 29, 2024
02c344e
fixes after review
axenteoctavian May 29, 2024
9b33545
num of shards default to 1
axenteoctavian May 29, 2024
7b003b8
Merge branch 'MX-15222-sovereign-chain-simulator' into MX-15222-actua…
axenteoctavian May 31, 2024
36e60b0
import fix
axenteoctavian May 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package process

import (
"time"

chainSimulatorProcess "github.com/multiversx/mx-chain-go/node/chainSimulator/process"
"github.com/multiversx/mx-chain-go/process"

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

type sovereignBlockProcessorFactory struct {
}

// NewSovereignBlockProcessorFactory creates a new block processor factory for sovereign chain simulator
func NewSovereignBlockProcessorFactory() chainSimulatorProcess.BlocksProcessorFactory {
return &sovereignBlockProcessorFactory{}
}

// ProcessBlock will create and process a block in sovereign chain simulator
func (sbpf *sovereignBlockProcessorFactory) ProcessBlock(blockProcessor process.BlockProcessor, header data.HeaderHandler) (data.HeaderHandler, data.BodyHandler, error) {
if check.IfNil(blockProcessor) {
return nil, nil, process.ErrNilBlockProcessor
}
if check.IfNil(header) {
return nil, nil, process.ErrNilHeaderHandler
}

header, block, err := blockProcessor.CreateBlock(header, func() bool {
return true
})
if err != nil {
return nil, nil, err
}

return blockProcessor.ProcessBlock(header, block, func() time.Duration {
return time.Second
})
}

// IsInterfaceNil returns true if there is no value under the interface
func (sbpf *sovereignBlockProcessorFactory) IsInterfaceNil() bool {
return sbpf == nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package process

import (
"testing"
"time"

"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/testscommon"

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

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

fact := NewSovereignBlockProcessorFactory()

require.False(t, fact.IsInterfaceNil())
require.IsType(t, new(sovereignBlockProcessorFactory), fact)
}

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

t.Run("nil block processor should error", func(t *testing.T) {
fact := NewSovereignBlockProcessorFactory()
header, block, err := fact.ProcessBlock(nil, &testscommon.HeaderHandlerStub{})
require.ErrorIs(t, err, process.ErrNilBlockProcessor)
require.Nil(t, header)
require.Nil(t, block)
})
t.Run("nil header handler should error", func(t *testing.T) {
fact := NewSovereignBlockProcessorFactory()
header, block, err := fact.ProcessBlock(&testscommon.BlockProcessorStub{}, nil)
require.ErrorIs(t, err, process.ErrNilHeaderHandler)
require.Nil(t, header)
require.Nil(t, block)
})
t.Run("should work", func(t *testing.T) {
fact := NewSovereignBlockProcessorFactory()

createWasCalled := false
processWasCalled := false
bp := &testscommon.BlockProcessorStub{
CreateBlockCalled: func(initialHdrData data.HeaderHandler, haveTime func() bool) (data.HeaderHandler, data.BodyHandler, error) {
createWasCalled = true
return nil, nil, nil
},
ProcessBlockCalled: func(header data.HeaderHandler, body data.BodyHandler, haveTime func() time.Duration) (data.HeaderHandler, data.BodyHandler, error) {
processWasCalled = true
return nil, nil, nil
},
}

_, _, err := fact.ProcessBlock(bp, &testscommon.HeaderHandlerStub{})
require.Nil(t, err)
require.Equal(t, true, createWasCalled)
require.Equal(t, true, processWasCalled)
})
}
25 changes: 25 additions & 0 deletions cmd/sovereignnode/chainSimulator/sovereignChainHandlerFactory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package chainSimulator

import (
"github.com/multiversx/mx-chain-go/node/chainSimulator"
"github.com/multiversx/mx-chain-go/node/chainSimulator/process"
sovProcess "github.com/multiversx/mx-chain-go/sovereignnode/chainSimulator/process"
)

type sovereignProcessorFactory struct {
}

// NewSovereignChainHandlerFactory creates a new chain handler factory for sovereign chain simulator
func NewSovereignChainHandlerFactory() chainSimulator.ChainHandlerFactory {
return &sovereignProcessorFactory{}
}

// CreateChainHandler creates a new chain handler for sovereign chain simulator
func (spf *sovereignProcessorFactory) CreateChainHandler(nodeHandler process.NodeHandler) (chainSimulator.ChainHandler, error) {
return process.NewBlocksCreator(nodeHandler, sovProcess.NewSovereignBlockProcessorFactory())
}

// IsInterfaceNil returns true if there is no value under the interface
func (spf *sovereignProcessorFactory) IsInterfaceNil() bool {
return spf == nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package chainSimulator

import (
"testing"

"github.com/multiversx/mx-chain-go/node/chainSimulator/process"
"github.com/multiversx/mx-chain-go/testscommon/chainSimulator"

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

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

fact := NewSovereignChainHandlerFactory()
require.False(t, fact.IsInterfaceNil())
require.IsType(t, new(sovereignProcessorFactory), fact)
}

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

t.Run("nil node handler should error", func(t *testing.T) {
fact := NewSovereignChainHandlerFactory()
chainHandler, err := fact.CreateChainHandler(nil)
require.Nil(t, chainHandler)
require.ErrorIs(t, err, process.ErrNilNodeHandler)
})
t.Run("should work", func(t *testing.T) {
fact := NewSovereignChainHandlerFactory()
chainHandler, err := fact.CreateChainHandler(&chainSimulator.NodeHandlerMock{})
require.Nil(t, err)
require.NotNil(t, chainHandler)
})
}
124 changes: 124 additions & 0 deletions cmd/sovereignnode/chainSimulator/sovereignChainSimulator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package chainSimulator

import (
"path"

"github.com/multiversx/mx-chain-go/common"
"github.com/multiversx/mx-chain-go/config"
"github.com/multiversx/mx-chain-go/dataRetriever"
"github.com/multiversx/mx-chain-go/factory"
"github.com/multiversx/mx-chain-go/factory/runType"
chainSimulatorIntegrationTests "github.com/multiversx/mx-chain-go/integrationTests/chainSimulator"
"github.com/multiversx/mx-chain-go/node"
"github.com/multiversx/mx-chain-go/node/chainSimulator"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/process/rating"
"github.com/multiversx/mx-chain-go/sharding"
sovereignConfig "github.com/multiversx/mx-chain-go/sovereignnode/config"
"github.com/multiversx/mx-chain-go/sovereignnode/incomingHeader"
sovRunType "github.com/multiversx/mx-chain-go/sovereignnode/runType"

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

const (
numOfShards = 1
)

// ArgsSovereignChainSimulator holds the arguments for sovereign chain simulator
type ArgsSovereignChainSimulator struct {
SovereignConfigPath string
*chainSimulator.ArgsChainSimulator
}

// NewSovereignChainSimulator will create a new instance of sovereign chain simulator
func NewSovereignChainSimulator(args ArgsSovereignChainSimulator) (chainSimulatorIntegrationTests.ChainSimulator, error) {
args.NumOfShards = numOfShards

alterConfigs := args.AlterConfigsFunction
configs, err := loadSovereignConfigs(args.SovereignConfigPath)
if err != nil {
return nil, err
}
args.AlterConfigsFunction = func(cfg *config.Configs) {
cfg.EconomicsConfig = configs.EconomicsConfig
cfg.EpochConfig = configs.EpochConfig
cfg.GeneralConfig.SovereignConfig = *configs.SovereignExtraConfig
cfg.GeneralConfig.VirtualMachine.Execution.WasmVMVersions = []config.WasmVMVersionByEpoch{{StartEpoch: 0, Version: "v1.5"}}
cfg.GeneralConfig.VirtualMachine.Querying.WasmVMVersions = []config.WasmVMVersionByEpoch{{StartEpoch: 0, Version: "v1.5"}}
mariusmihaic marked this conversation as resolved.
Show resolved Hide resolved

if alterConfigs != nil {
alterConfigs(cfg)
}
}

args.CreateGenesisNodesSetup = func(nodesFilePath string, addressPubkeyConverter core.PubkeyConverter, validatorPubkeyConverter core.PubkeyConverter, _ uint32) (sharding.GenesisNodesSetupHandler, error) {
return sharding.NewSovereignNodesSetup(&sharding.SovereignNodesSetupArgs{
NodesFilePath: nodesFilePath,
AddressPubKeyConverter: addressPubkeyConverter,
ValidatorPubKeyConverter: validatorPubkeyConverter,
})
}
args.CreateRatingsData = func(arg rating.RatingsDataArg) (process.RatingsInfoHandler, error) {
return rating.NewSovereignRatingsData(arg)
}
args.CreateIncomingHeaderSubscriber = func(config *config.NotifierConfig, dataPool dataRetriever.PoolsHolder, mainChainNotarizationStartRound uint64, runTypeComponents factory.RunTypeComponentsHolder) (process.IncomingHeaderSubscriber, error) {
return incomingHeader.CreateIncomingHeaderProcessor(config, dataPool, mainChainNotarizationStartRound, runTypeComponents)
}
args.CreateRunTypeComponents = func(argsRunType runType.ArgsRunTypeComponents) (factory.RunTypeComponentsHolder, error) {
return createSovereignRunTypeComponents(argsRunType, *configs.SovereignExtraConfig)
}
args.NodeFactory = node.NewSovereignNodeFactory()
args.ChainProcessorFactory = NewSovereignChainHandlerFactory()

return chainSimulator.NewChainSimulator(*args.ArgsChainSimulator)
}

// loadSovereignConfigs loads sovereign configs
func loadSovereignConfigs(configsPath string) (*sovereignConfig.SovereignConfig, error) {
epochConfig, err := common.LoadEpochConfig(path.Join(configsPath, "enableEpochs.toml"))
if err != nil {
return nil, err
}

economicsConfig, err := common.LoadEconomicsConfig(path.Join(configsPath, "economics.toml"))
if err != nil {
return nil, err
}

sovereignExtraConfig, err := sovereignConfig.LoadSovereignGeneralConfig(path.Join(configsPath, "sovereignConfig.toml"))
if err != nil {
return nil, err
}

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

func createSovereignRunTypeComponents(args runType.ArgsRunTypeComponents, sovereignExtraConfig config.SovereignConfig) (factory.RunTypeComponentsHolder, error) {
argsSovRunType, err := sovRunType.CreateSovereignArgsRunTypeComponents(args, sovereignExtraConfig)
if err != nil {
return nil, err
}

sovereignComponentsFactory, err := runType.NewSovereignRunTypeComponentsFactory(*argsSovRunType)
if err != nil {
return nil, err
}

managedRunTypeComponents, err := runType.NewManagedRunTypeComponents(sovereignComponentsFactory)
if err != nil {
return nil, err
}
err = managedRunTypeComponents.Create()
if err != nil {
return nil, err
}

return managedRunTypeComponents, nil
}