Skip to content

Commit

Permalink
extra endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
miiu96 committed Nov 10, 2023
1 parent 0afc5b8 commit 893a2fa
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 12 deletions.
18 changes: 11 additions & 7 deletions cmd/chainsimulator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/facade/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package facade

type SimulatorHandler interface {
GenerateBlocks(numOfBlocks int) error
IsInterfaceNil() bool
}
32 changes: 32 additions & 0 deletions pkg/facade/simulatorFacade.go
Original file line number Diff line number Diff line change
@@ -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
}
46 changes: 41 additions & 5 deletions pkg/proxy/creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions pkg/proxy/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 893a2fa

Please sign in to comment.