Skip to content

Commit

Permalink
Merge pull request #5699 from multiversx/enable-http-server
Browse files Browse the repository at this point in the history
[chain_simulator] Enable http server option
  • Loading branch information
miiu96 committed Nov 14, 2023
2 parents a2bdb15 + 90bf06f commit a050b92
Show file tree
Hide file tree
Showing 20 changed files with 601 additions and 130 deletions.
38 changes: 22 additions & 16 deletions node/chainSimulator/chainSimulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewChainSimulator(
genesisTimestamp int64,
roundDurationInMillis uint64,
roundsPerEpoch core.OptionalUint64,
apiInterface components.APIConfigurator,
) (*simulator, error) {
syncedBroadcastNetwork := components.NewSyncedBroadcastNetwork()

Expand All @@ -42,7 +43,7 @@ func NewChainSimulator(
chanStopNodeProcess: make(chan endProcess.ArgEndProcess),
}

err := instance.createChainHandlers(tempDir, numOfShards, pathToInitialConfig, genesisTimestamp, roundDurationInMillis, roundsPerEpoch)
err := instance.createChainHandlers(tempDir, numOfShards, pathToInitialConfig, genesisTimestamp, roundDurationInMillis, roundsPerEpoch, apiInterface)
if err != nil {
return nil, err
}
Expand All @@ -57,6 +58,7 @@ func (s *simulator) createChainHandlers(
genesisTimestamp int64,
roundDurationInMillis uint64,
roundsPerEpoch core.OptionalUint64,
apiInterface components.APIConfigurator,
) error {
outputConfigs, err := configs.CreateChainSimulatorConfigs(configs.ArgsChainSimulatorConfigs{
NumOfShards: numOfShards,
Expand All @@ -76,7 +78,7 @@ func (s *simulator) createChainHandlers(
}

for idx := range outputConfigs.ValidatorsPrivateKeys {
node, errCreate := s.createTestNode(outputConfigs.Configs, idx, outputConfigs.GasScheduleFilename)
node, errCreate := s.createTestNode(outputConfigs.Configs, idx, outputConfigs.GasScheduleFilename, apiInterface)
if errCreate != nil {
return errCreate
}
Expand Down Expand Up @@ -106,22 +108,16 @@ func (s *simulator) createTestNode(
configs *config.Configs,
skIndex int,
gasScheduleFilename string,
apiInterface components.APIConfigurator,
) (process.NodeHandler, error) {
args := components.ArgsTestOnlyProcessingNode{
Config: *configs.GeneralConfig,
EpochConfig: *configs.EpochConfig,
EconomicsConfig: *configs.EconomicsConfig,
RoundsConfig: *configs.RoundConfig,
PreferencesConfig: *configs.PreferencesConfig,
ImportDBConfig: *configs.ImportDbConfig,
ContextFlagsConfig: *configs.FlagsConfig,
SystemSCConfig: *configs.SystemSCConfig,
ConfigurationPathsHolder: *configs.ConfigurationPathsHolder,
ChanStopNodeProcess: s.chanStopNodeProcess,
SyncedBroadcastNetwork: s.syncedBroadcastNetwork,
NumShards: s.numOfShards,
GasScheduleFilename: gasScheduleFilename,
SkIndex: skIndex,
Configs: *configs,
ChanStopNodeProcess: s.chanStopNodeProcess,
SyncedBroadcastNetwork: s.syncedBroadcastNetwork,
NumShards: s.numOfShards,
GasScheduleFilename: gasScheduleFilename,
SkIndex: skIndex,
APIInterface: apiInterface,
}

return components.NewTestOnlyProcessingNode(args)
Expand Down Expand Up @@ -161,6 +157,16 @@ func (s *simulator) GetNodeHandler(shardID uint32) process.NodeHandler {
return s.nodes[shardID]
}

// GetRestAPIInterfaces will return a map with the rest api interfaces for every node
func (s *simulator) GetRestAPIInterfaces() map[uint32]string {
resMap := make(map[uint32]string)
for shardID, node := range s.nodes {
resMap[shardID] = node.GetFacadeHandler().RestApiInterface()
}

return resMap
}

// Close will stop and close the simulator
func (s *simulator) Close() error {
var errorStrings []string
Expand Down
9 changes: 6 additions & 3 deletions node/chainSimulator/chainSimulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-go/node/chainSimulator/components/api"
"github.com/multiversx/mx-chain-go/node/chainSimulator/testdata"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -18,7 +19,7 @@ const (
func TestNewChainSimulator(t *testing.T) {
startTime := time.Now().Unix()
roundDurationInMillis := uint64(6000)
chainSimulator, err := NewChainSimulator(t.TempDir(), 3, defaultPathToInitialConfig, startTime, roundDurationInMillis, core.OptionalUint64{})
chainSimulator, err := NewChainSimulator(t.TempDir(), 3, defaultPathToInitialConfig, startTime, roundDurationInMillis, core.OptionalUint64{}, api.NewNoApiInterface())
require.Nil(t, err)
require.NotNil(t, chainSimulator)

Expand All @@ -31,7 +32,7 @@ func TestNewChainSimulator(t *testing.T) {
func TestChainSimulator_GenerateBlocksShouldWork(t *testing.T) {
startTime := time.Now().Unix()
roundDurationInMillis := uint64(6000)
chainSimulator, err := NewChainSimulator(t.TempDir(), 3, defaultPathToInitialConfig, startTime, roundDurationInMillis, core.OptionalUint64{})
chainSimulator, err := NewChainSimulator(t.TempDir(), 3, defaultPathToInitialConfig, startTime, roundDurationInMillis, core.OptionalUint64{}, api.NewNoApiInterface())
require.Nil(t, err)
require.NotNil(t, chainSimulator)

Expand All @@ -51,7 +52,7 @@ func TestChainSimulator_GenerateBlocksAndEpochChangeShouldWork(t *testing.T) {
HasValue: true,
Value: 20,
}
chainSimulator, err := NewChainSimulator(t.TempDir(), 3, defaultPathToInitialConfig, startTime, roundDurationInMillis, roundsPerEpoch)
chainSimulator, err := NewChainSimulator(t.TempDir(), 3, defaultPathToInitialConfig, startTime, roundDurationInMillis, roundsPerEpoch, api.NewNoApiInterface())
require.Nil(t, err)
require.NotNil(t, chainSimulator)

Expand All @@ -72,6 +73,8 @@ func TestChainSimulator_GenerateBlocksAndEpochChangeShouldWork(t *testing.T) {
assert.True(t, accountAfterRewards.GetBalance().Cmp(initialAccount.GetBalance()) > 0,
fmt.Sprintf("initial balance %s, balance after rewards %s", initialAccount.GetBalance().String(), accountAfterRewards.GetBalance().String()))

fmt.Println(chainSimulator.GetRestAPIInterfaces())

err = chainSimulator.Close()
assert.Nil(t, err)
}
21 changes: 21 additions & 0 deletions node/chainSimulator/components/api/fixedAPIInterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package api

import "fmt"

type fixedPortAPIConfigurator struct {
restAPIInterface string
mapShardPort map[uint32]int
}

// NewFixedPortAPIConfigurator will create a new instance of fixedPortAPIConfigurator
func NewFixedPortAPIConfigurator(restAPIInterface string, mapShardPort map[uint32]int) *fixedPortAPIConfigurator {
return &fixedPortAPIConfigurator{
restAPIInterface: restAPIInterface,
mapShardPort: mapShardPort,
}
}

// RestApiInterface will return the api interface for the provided shard
func (f *fixedPortAPIConfigurator) RestApiInterface(shardID uint32) string {
return fmt.Sprintf("%s:%d", f.restAPIInterface, f.mapShardPort[shardID])
}
37 changes: 37 additions & 0 deletions node/chainSimulator/components/api/freeAPIInterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package api

import (
"fmt"
"net"
)

type freePortAPIConfigurator struct {
restAPIInterface string
}

// NewFreePortAPIConfigurator will create a new instance of freePortAPIConfigurator
func NewFreePortAPIConfigurator(restAPIInterface string) *freePortAPIConfigurator {
return &freePortAPIConfigurator{
restAPIInterface: restAPIInterface,
}
}

// RestApiInterface will return the rest api interface with a free port
func (f *freePortAPIConfigurator) RestApiInterface(_ uint32) string {
return fmt.Sprintf("%s:%d", f.restAPIInterface, getFreePort())
}

func getFreePort() int {
// Listen on port 0 to get a free port
l, err := net.Listen("tcp", "localhost:0")
if err != nil {
panic(err)
}
defer func() {
_ = l.Close()
}()

// Get the port number that was assigned
addr := l.Addr().(*net.TCPAddr)
return addr.Port
}
15 changes: 15 additions & 0 deletions node/chainSimulator/components/api/noApiInterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package api

import "github.com/multiversx/mx-chain-go/facade"

type noAPIInterface struct{}

// NewNoApiInterface will create a new instance of noAPIInterface
func NewNoApiInterface() *noAPIInterface {
return new(noAPIInterface)
}

// RestApiInterface will return the value for disable api interface
func (n noAPIInterface) RestApiInterface(_ uint32) string {
return facade.DefaultRestPortOff
}
19 changes: 17 additions & 2 deletions node/chainSimulator/components/bootstrapComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type bootstrapComponentsHolder struct {
guardedAccountHandler process.GuardedAccountHandler
}

// CreateBootstrapComponentHolder will create a new instance of bootstrap components holder
func CreateBootstrapComponentHolder(args ArgsBootstrapComponentsHolder) (factory.BootstrapComponentsHolder, error) {
// CreateBootstrapComponents will create a new instance of bootstrap components holder
func CreateBootstrapComponents(args ArgsBootstrapComponentsHolder) (factory.BootstrapComponentsHandler, error) {
instance := &bootstrapComponentsHolder{
closeHandler: NewCloseHandler(),
}
Expand Down Expand Up @@ -137,3 +137,18 @@ func (b *bootstrapComponentsHolder) Close() error {
func (b *bootstrapComponentsHolder) IsInterfaceNil() bool {
return b == nil
}

// Create will do nothing
func (b *bootstrapComponentsHolder) Create() error {
return nil
}

// CheckSubcomponents will do nothing
func (b *bootstrapComponentsHolder) CheckSubcomponents() error {
return nil
}

// String will do nothing
func (b *bootstrapComponentsHolder) String() string {
return ""
}
19 changes: 17 additions & 2 deletions node/chainSimulator/components/coreComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ type ArgsCoreComponentsHolder struct {
WorkingDir string
}

// CreateCoreComponentsHolder will create a new instance of factory.CoreComponentsHolder
func CreateCoreComponentsHolder(args ArgsCoreComponentsHolder) (factory.CoreComponentsHolder, error) {
// CreateCoreComponents will create a new instance of factory.CoreComponentsHolder
func CreateCoreComponents(args ArgsCoreComponentsHolder) (factory.CoreComponentsHandler, error) {
var err error
instance := &coreComponentsHolder{
closeHandler: NewCloseHandler(),
Expand Down Expand Up @@ -433,3 +433,18 @@ func (c *coreComponentsHolder) Close() error {
func (c *coreComponentsHolder) IsInterfaceNil() bool {
return c == nil
}

// Create will do nothing
func (c *coreComponentsHolder) Create() error {
return nil
}

// CheckSubcomponents will do nothing
func (c *coreComponentsHolder) CheckSubcomponents() error {
return nil
}

// String will do nothing
func (c *coreComponentsHolder) String() string {
return ""
}
24 changes: 22 additions & 2 deletions node/chainSimulator/components/cryptoComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ type cryptoComponentsHolder struct {
publicKeyString string
}

// CreateCryptoComponentsHolder will create a new instance of cryptoComponentsHolder
func CreateCryptoComponentsHolder(args ArgsCryptoComponentsHolder) (factory.CryptoComponentsHolder, error) {
// CreateCryptoComponents will create a new instance of cryptoComponentsHolder
func CreateCryptoComponents(args ArgsCryptoComponentsHolder) (factory.CryptoComponentsHandler, error) {
instance := &cryptoComponentsHolder{}

cryptoComponentsHandlerArgs := cryptoComp.CryptoComponentsFactoryArgs{
Expand Down Expand Up @@ -237,3 +237,23 @@ func (c *cryptoComponentsHolder) Clone() interface{} {
func (c *cryptoComponentsHolder) IsInterfaceNil() bool {
return c == nil
}

// Create will do nothing
func (c *cryptoComponentsHolder) Create() error {
return nil
}

// CheckSubcomponents will do nothing
func (c *cryptoComponentsHolder) CheckSubcomponents() error {
return nil
}

// String will do nothing
func (c *cryptoComponentsHolder) String() string {
return ""
}

// Close will do nothing
func (c *cryptoComponentsHolder) Close() error {
return nil
}
19 changes: 17 additions & 2 deletions node/chainSimulator/components/dataComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ type dataComponentsHolder struct {
miniBlockProvider factory.MiniBlockProvider
}

// CreateDataComponentsHolder will create the data components holder
func CreateDataComponentsHolder(args ArgsDataComponentsHolder) (factory.DataComponentsHolder, error) {
// CreateDataComponents will create the data components holder
func CreateDataComponents(args ArgsDataComponentsHolder) (factory.DataComponentsHandler, error) {
miniBlockStorer, err := args.StorageService.GetStorer(dataRetriever.MiniBlockUnit)
if err != nil {
return nil, err
Expand Down Expand Up @@ -106,3 +106,18 @@ func (d *dataComponentsHolder) Close() error {
func (d *dataComponentsHolder) IsInterfaceNil() bool {
return d == nil
}

// Create will do nothing
func (d *dataComponentsHolder) Create() error {
return nil
}

// CheckSubcomponents will do nothing
func (d *dataComponentsHolder) CheckSubcomponents() error {
return nil
}

// String will do nothing
func (d *dataComponentsHolder) String() string {
return ""
}
5 changes: 5 additions & 0 deletions node/chainSimulator/components/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ type SyncedBroadcastNetworkHandler interface {
GetConnectedPeersOnTopic(topic string) []core.PeerID
IsInterfaceNil() bool
}

// APIConfigurator defines what an api configurator should be able to do
type APIConfigurator interface {
RestApiInterface(shardID uint32) string
}
19 changes: 17 additions & 2 deletions node/chainSimulator/components/networkComponents.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type networkComponentsHolder struct {
fullArchivePreferredPeersHolderHandler factory.PreferredPeersHolderHandler
}

// CreateNetworkComponentsHolder creates a new networkComponentsHolder instance
func CreateNetworkComponentsHolder(network SyncedBroadcastNetworkHandler) (*networkComponentsHolder, error) {
// CreateNetworkComponents creates a new networkComponentsHolder instance
func CreateNetworkComponents(network SyncedBroadcastNetworkHandler) (factory.NetworkComponentsHandler, error) {
messenger, err := NewSyncedMessenger(network)
if err != nil {
return nil, err
Expand Down Expand Up @@ -125,3 +125,18 @@ func (holder *networkComponentsHolder) Close() error {
func (holder *networkComponentsHolder) IsInterfaceNil() bool {
return holder == nil
}

// Create will do nothing
func (holder *networkComponentsHolder) Create() error {
return nil
}

// CheckSubcomponents will do nothing
func (holder *networkComponentsHolder) CheckSubcomponents() error {
return nil
}

// String will do nothing
func (holder *networkComponentsHolder) String() string {
return ""
}

0 comments on commit a050b92

Please sign in to comment.