From 31af7b1ad7f57651f1a3abca0cc631d186dd6a5e Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Thu, 18 Apr 2024 10:34:01 +0300 Subject: [PATCH 1/6] - made the proxy start on a random port - print the URL for the proxy after the chain simulator is up and running --- cmd/chainsimulator/main.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/cmd/chainsimulator/main.go b/cmd/chainsimulator/main.go index 8b73d2b1..5b952f96 100644 --- a/cmd/chainsimulator/main.go +++ b/cmd/chainsimulator/main.go @@ -6,6 +6,7 @@ import ( "os" "os/signal" "runtime/debug" + "strconv" "syscall" "time" @@ -25,6 +26,8 @@ import ( "github.com/urfave/cli" ) +const timeToAllowProxyToStart = time.Second * 2 + var ( log = logger.GetOrCreate("chainsimulator") helpTemplate = `NAME: @@ -135,8 +138,21 @@ func startChainSimulator(ctx *cli.Context) error { return errors.New("invalid value for the number of validators for metachain") } + localRestApiInterface := "localhost" + apiConfigurator := api.NewFreePortAPIConfigurator(localRestApiInterface) + proxyPort := cfg.Config.Simulator.ServerPort + proxyURL := fmt.Sprintf(fmt.Sprintf("%s:%d", localRestApiInterface, proxyPort)) + if proxyPort == 0 { + proxyURL = apiConfigurator.RestApiInterface(0) + portString := proxyURL[len(localRestApiInterface)+1:] + port, errConvert := strconv.Atoi(portString) + if errConvert != nil { + return fmt.Errorf("internal error while searching a free port for the proxy component: %w", errConvert) + } + proxyPort = port + } + startTimeUnix := ctx.GlobalInt64(startTime.Name) - apiConfigurator := api.NewFreePortAPIConfigurator("localhost") argsChainSimulator := chainSimulator.ArgsChainSimulator{ BypassTxSignatureCheck: bypassTxsSignature, TempDir: os.TempDir(), @@ -166,7 +182,7 @@ func startChainSimulator(ctx *cli.Context) error { outputProxyConfigs, err := configs.CreateProxyConfigs(configs.ArgsProxyConfigs{ TemDir: os.TempDir(), PathToProxyConfig: proxyConfigs, - ServerPort: cfg.Config.Simulator.ServerPort, + ServerPort: proxyPort, RestApiInterfaces: restApiInterfaces, InitialWallets: simulator.GetInitialWalletKeys().ShardWallets, }) @@ -203,6 +219,9 @@ func startChainSimulator(ctx *cli.Context) error { proxyInstance.Start() + time.Sleep(timeToAllowProxyToStart) + log.Info(fmt.Sprintf("chain simulator's is accessible through the URL %s", proxyURL)) + interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM) <-interrupt From eab52e1bef35e9ae5c50fa890d45a72a7a444fd9 Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Thu, 18 Apr 2024 10:37:07 +0300 Subject: [PATCH 2/6] - adjusted delay --- cmd/chainsimulator/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/chainsimulator/main.go b/cmd/chainsimulator/main.go index 5b952f96..c76c7122 100644 --- a/cmd/chainsimulator/main.go +++ b/cmd/chainsimulator/main.go @@ -26,7 +26,7 @@ import ( "github.com/urfave/cli" ) -const timeToAllowProxyToStart = time.Second * 2 +const timeToAllowProxyToStart = time.Millisecond * 10 var ( log = logger.GetOrCreate("chainsimulator") From ce20894faa519153a256c394a44c7a2d42d48b3d Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Thu, 18 Apr 2024 10:49:56 +0300 Subject: [PATCH 3/6] - fixes & documented the PR --- .github/workflows/golangci-lint.yml | 2 +- Makefile | 12 ++++++++++++ README.md | 8 +++++++- cmd/chainsimulator/flags.go | 2 +- cmd/chainsimulator/main.go | 2 +- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 9847dee7..ed7336d7 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -22,7 +22,7 @@ jobs: uses: golangci/golangci-lint-action@v3 with: # Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. - version: v1.54.2 + version: v1.57.2 # Optional: working directory, useful for monorepos # working-directory: somedir diff --git a/Makefile b/Makefile index 86357d94..ae3df370 100644 --- a/Makefile +++ b/Makefile @@ -18,3 +18,15 @@ run-faucet-test: sleep 2s cd examples/faucet && /bin/bash faucet.sh docker stop "${IMAGE_NAME}" + +lint-install: +ifeq (,$(wildcard test -f bin/golangci-lint)) + @echo "Installing golint" + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s +endif + +run-lint: + @echo "Running golint" + bin/golangci-lint run --max-issues-per-linter 0 --max-same-issues 0 --timeout=2m + +lint: lint-install run-lint \ No newline at end of file diff --git a/README.md b/README.md index 54ce0486..fe0e111b 100644 --- a/README.md +++ b/README.md @@ -213,7 +213,7 @@ The **_[config.toml](./cmd/chainsimulator/config/config.toml)_** file: ```toml [config] [config.simulator] - # server-port paramter specifies the port of the http server + # server-port paramter specifies the port of the http server that the proxy component will respond on server-port = 8085 # num-of-shards parameter specifies the number of shard that chain simulator will simulate num-of-shards = 3 @@ -232,6 +232,12 @@ The **_[config.toml](./cmd/chainsimulator/config/config.toml)_** file: logs-path = "logs" ``` +**Note:** If the port for the proxy server is set to 0, a random free port will be selected. The URL for the proxy +is printed in the logs in a line that looks like: +``` +INFO [2024-04-18 10:48:47.231] chain simulator's is accessible through the URL localhost:38099 +``` + ## Contribution diff --git a/cmd/chainsimulator/flags.go b/cmd/chainsimulator/flags.go index 2df8387f..138e6186 100644 --- a/cmd/chainsimulator/flags.go +++ b/cmd/chainsimulator/flags.go @@ -59,7 +59,7 @@ var ( } serverPort = cli.IntFlag{ Name: "server-port", - Usage: "The port of the http server", + Usage: "The port of the http server that the proxy component will respond on. If this is set to 0, a random free port will be selected.", Value: 8085, } roundDurationInMs = cli.IntFlag{ diff --git a/cmd/chainsimulator/main.go b/cmd/chainsimulator/main.go index c76c7122..687150e2 100644 --- a/cmd/chainsimulator/main.go +++ b/cmd/chainsimulator/main.go @@ -141,7 +141,7 @@ func startChainSimulator(ctx *cli.Context) error { localRestApiInterface := "localhost" apiConfigurator := api.NewFreePortAPIConfigurator(localRestApiInterface) proxyPort := cfg.Config.Simulator.ServerPort - proxyURL := fmt.Sprintf(fmt.Sprintf("%s:%d", localRestApiInterface, proxyPort)) + proxyURL := fmt.Sprintf("%s:%d", localRestApiInterface, proxyPort) if proxyPort == 0 { proxyURL = apiConfigurator.RestApiInterface(0) portString := proxyURL[len(localRestApiInterface)+1:] From 6f64581de80f6283e8eabe6b2a6d88a379e3ebac Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Thu, 18 Apr 2024 10:54:36 +0300 Subject: [PATCH 4/6] - added empty line --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ae3df370..ecc5f21a 100644 --- a/Makefile +++ b/Makefile @@ -29,4 +29,4 @@ run-lint: @echo "Running golint" bin/golangci-lint run --max-issues-per-linter 0 --max-same-issues 0 --timeout=2m -lint: lint-install run-lint \ No newline at end of file +lint: lint-install run-lint From d682f2a710dc12dc4697b0bc5a9fd167530610cb Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Thu, 18 Apr 2024 10:55:39 +0300 Subject: [PATCH 5/6] - fixed line break in README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index fe0e111b..7266fba4 100644 --- a/README.md +++ b/README.md @@ -232,8 +232,8 @@ The **_[config.toml](./cmd/chainsimulator/config/config.toml)_** file: logs-path = "logs" ``` -**Note:** If the port for the proxy server is set to 0, a random free port will be selected. The URL for the proxy -is printed in the logs in a line that looks like: +**Note:** If the port for the proxy server is set to 0, a random free port will be selected. +The URL for the proxy is printed in the logs in a line that looks like: ``` INFO [2024-04-18 10:48:47.231] chain simulator's is accessible through the URL localhost:38099 ``` From 3e60c3f8a76900252ac5a8c16d2953a7469fda5b Mon Sep 17 00:00:00 2001 From: Iulian Pascalau Date: Thu, 18 Apr 2024 14:41:45 +0300 Subject: [PATCH 6/6] - fix after merge --- cmd/chainsimulator/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/chainsimulator/main.go b/cmd/chainsimulator/main.go index d6bae50b..e2986c31 100644 --- a/cmd/chainsimulator/main.go +++ b/cmd/chainsimulator/main.go @@ -178,7 +178,6 @@ func startChainSimulator(ctx *cli.Context) error { } startTimeUnix := ctx.GlobalInt64(startTime.Name) - apiConfigurator := api.NewFreePortAPIConfigurator("localhost") tempDir, err := os.MkdirTemp(os.TempDir(), "") if err != nil {