diff --git a/cmd/chainsimulator/main.go b/cmd/chainsimulator/main.go index 6e7db336..3c01f32b 100644 --- a/cmd/chainsimulator/main.go +++ b/cmd/chainsimulator/main.go @@ -14,6 +14,7 @@ import ( logger "github.com/multiversx/mx-chain-logger-go" "github.com/multiversx/mx-chain-logger-go/file" "github.com/multiversx/mx-chain-simulator-go/config" + "github.com/multiversx/mx-chain-simulator-go/pkg/facade" "github.com/multiversx/mx-chain-simulator-go/pkg/proxy" "github.com/urfave/cli" ) @@ -115,21 +116,24 @@ func startChainSimulator(ctx *cli.Context) error { time.Sleep(time.Second) - metaNode := simulator.GetNodeHandler(core.MetachainShardId) - proxyInstance, err := proxy.CreateProxy(proxy.ArgsProxy{ - Config: outputProxyConfigs.Config, - NodeHandler: metaNode, - PathToConfig: outputProxyConfigs.PathToTempConfig, - }) + simulatorFacade, err := facade.NewSimulatorFacade(simulator) if err != nil { return err } - err = simulator.GenerateBlocks(85) + metaNode := simulator.GetNodeHandler(core.MetachainShardId) + proxyInstance, err := proxy.CreateProxy(proxy.ArgsProxy{ + Config: outputProxyConfigs.Config, + NodeHandler: metaNode, + PathToConfig: outputProxyConfigs.PathToTempConfig, + SimulatorFacade: simulatorFacade, + }) if err != nil { return err } + proxyInstance.Start() + interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM) <-interrupt diff --git a/pkg/facade/interface.go b/pkg/facade/interface.go new file mode 100644 index 00000000..a4626be7 --- /dev/null +++ b/pkg/facade/interface.go @@ -0,0 +1,6 @@ +package facade + +type SimulatorHandler interface { + GenerateBlocks(numOfBlocks int) error + IsInterfaceNil() bool +} diff --git a/pkg/facade/simulatorFacade.go b/pkg/facade/simulatorFacade.go new file mode 100644 index 00000000..94c84337 --- /dev/null +++ b/pkg/facade/simulatorFacade.go @@ -0,0 +1,32 @@ +package facade + +import ( + "errors" + + "github.com/multiversx/mx-chain-core-go/core/check" +) + +type simulatorFacade struct { + simulator SimulatorHandler +} + +func NewSimulatorFacade(simulator SimulatorHandler) (*simulatorFacade, error) { + if check.IfNil(simulator) { + return nil, errors.New("nil simulator handler ") + } + + return &simulatorFacade{ + simulator: simulator, + }, nil +} + +func (sf *simulatorFacade) GenerateBlocks(numOfBlocks int) error { + if numOfBlocks <= 0 { + return errors.New("num of blocks must be greater than zero") + } + return sf.simulator.GenerateBlocks(numOfBlocks) +} + +func (sf *simulatorFacade) IsInterfaceNil() bool { + return sf == nil +} diff --git a/pkg/proxy/creator.go b/pkg/proxy/creator.go index c583e0a4..e6054808 100644 --- a/pkg/proxy/creator.go +++ b/pkg/proxy/creator.go @@ -7,11 +7,14 @@ import ( "net/http" "os" "path" + "strconv" "time" + "github.com/gin-gonic/gin" "github.com/multiversx/mx-chain-go/node/chainSimulator/process" logger "github.com/multiversx/mx-chain-logger-go" "github.com/multiversx/mx-chain-proxy-go/api" + "github.com/multiversx/mx-chain-proxy-go/api/shared" "github.com/multiversx/mx-chain-proxy-go/common" "github.com/multiversx/mx-chain-proxy-go/config" "github.com/multiversx/mx-chain-proxy-go/data" @@ -28,20 +31,23 @@ var log = logger.GetOrCreate("proxy") // ArgsProxy holds the arguments needed to create a new instance of proxy type ArgsProxy struct { - Config *config.Config - NodeHandler process.NodeHandler - PathToConfig string + Config *config.Config + NodeHandler process.NodeHandler + PathToConfig string + SimulatorFacade SimulatorFacade } type proxy struct { closableComponents *data.ClosableComponentsHandler httpServer *http.Server + simulatorFacade SimulatorFacade } // CreateProxy will create a new instance of proxy func CreateProxy(args ArgsProxy) (ProxyHandler, error) { proxyInstance := &proxy{ closableComponents: data.NewClosableComponentsHandler(), + simulatorFacade: args.SimulatorFacade, } statusMetricsProvider := metrics.NewStatusMetrics() @@ -204,15 +210,45 @@ func CreateProxy(args ArgsProxy) (ProxyHandler, error) { false, ) + proxyInstance.addExtraEndpoints() + + return proxyInstance, nil +} + +func (p *proxy) Start() { go func() { - err = proxyInstance.httpServer.ListenAndServe() + err := p.httpServer.ListenAndServe() if err != nil { log.Error("cannot ListenAndServe()", "err", err) os.Exit(1) } }() +} - return proxyInstance, nil +func (p *proxy) addExtraEndpoints() { + ws := p.httpServer.Handler.(*gin.Engine) + + ws.GET("/simulator/generate-blocks/:num", func(c *gin.Context) { + numStr := c.Param("num") + if numStr == "" { + shared.RespondWithBadRequest(c, "err invalid number of blocks") + return + } + + num, err := strconv.Atoi(numStr) + if err != nil { + shared.RespondWithBadRequest(c, "cannot convert string to number") + return + } + + err = p.simulatorFacade.GenerateBlocks(num) + if err != nil { + shared.RespondWithInternalError(c, errors.New("cannot generate blocks"), err) + return + } + + shared.RespondWith(c, http.StatusOK, gin.H{}, "", data.ReturnCodeSuccess) + }) } // Close will close the proxy diff --git a/pkg/proxy/interface.go b/pkg/proxy/interface.go index 39ed7ed9..b9f57bf3 100644 --- a/pkg/proxy/interface.go +++ b/pkg/proxy/interface.go @@ -2,5 +2,11 @@ package proxy // ProxyHandler defines what a proxy handler should be able to do type ProxyHandler interface { + Start() Close() } + +type SimulatorFacade interface { + GenerateBlocks(numOfBlocks int) error + IsInterfaceNil() bool +}