Skip to content

Commit

Permalink
fix: service public port allocation error
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Dec 12, 2023
1 parent 98d596f commit f1c316c
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 40 deletions.
4 changes: 3 additions & 1 deletion cli/cmd/bridge/btp/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ func btpRelay(cmd *cobra.Command, args []string) {
cliContext.Fatal(err)
}

err = cliContext.FileHandler().WriteFile("dive.json", []byte(result))
serviceFileName := fmt.Sprintf(common.DiveOutFile, common.EnclaveName)

err = cliContext.FileHandler().WriteFile(serviceFileName, []byte(result))
if err != nil {
cliContext.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion cli/cmd/bridge/btp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ func runBtpSetupWhenChainsAreNotIcon(cli *common.Cli, enclaveContext *enclaves.E
func runBtpSetupWhenSingleChainRunning(cli *common.Cli, enclaveContext *enclaves.EnclaveContext, chains *utils.Chains, bridge bool) (string, error) {
var chainAServiceResponse, chainBServiceResponse, chainAServiceName, chainBServiceName, response string
var services = common.Services{}
err := cli.FileHandler().ReadJson("services.json", &services)
serviceFileName := fmt.Sprintf(common.ServiceFilePath, common.EnclaveName)
err := cli.FileHandler().ReadJson(serviceFileName, &services)

if err != nil {
return "", common.WrapMessageToError(err, fmt.Sprintf("BTP Setup Failed For ChainA %s and ChainB %s", chains.ChainA, chains.ChainB))
Expand Down
4 changes: 3 additions & 1 deletion cli/cmd/bridge/ibc/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func ibcRelay(cmd *cobra.Command, args []string) {
cliContext.Fatal(err)
}

err = cliContext.FileHandler().WriteFile("dive.json", []byte(result))
serviceFileName := fmt.Sprintf(common.DiveOutFile, common.EnclaveName)

err = cliContext.FileHandler().WriteFile(serviceFileName, []byte(result))
if err != nil {
cliContext.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion cli/cmd/bridge/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ func (chains *Chains) GetServicesResponse(cli *common.Cli) (string, string, erro

var serviceConfig = common.Services{}

err := cli.FileHandler().ReadJson("services.json", &serviceConfig)
serviceFileName := fmt.Sprintf(common.ServiceFilePath, common.EnclaveName)

err := cli.FileHandler().ReadJson(serviceFileName, &serviceConfig)

if err != nil {
return "", "", err
Expand Down
6 changes: 5 additions & 1 deletion cli/cmd/chains/archway/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package archway

import (
"fmt"

"github.com/hugobyte/dive-core/cli/common"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -40,7 +42,9 @@ func archway(cmd *cobra.Command, args []string) {
cliContext.Fatal(err)
}

err = common.WriteServiceResponseData(response.ServiceName, *response, cliContext)
serviceFileName := fmt.Sprintf(common.ServiceFilePath, common.EnclaveName)

err = common.WriteServiceResponseData(response.ServiceName, *response, cliContext, serviceFileName)
if err != nil {
cliContext.Fatal(err)

Expand Down
5 changes: 3 additions & 2 deletions cli/cmd/chains/eth/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eth

import (
"fmt"
"strings"

"github.com/hugobyte/dive-core/cli/common"
Expand Down Expand Up @@ -36,8 +37,8 @@ func eth(cmd *cobra.Command, args []string) {
cliContext.Fatal(err)
}
}

err = common.WriteServiceResponseData(responseData.ServiceName, *responseData, cliContext)
serviceFileName := fmt.Sprintf(common.ServiceFilePath, common.EnclaveName)
err = common.WriteServiceResponseData(responseData.ServiceName, *responseData, cliContext, serviceFileName)

if err != nil {
cliContext.Fatal(err)
Expand Down
5 changes: 3 additions & 2 deletions cli/cmd/chains/hardhat/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hardhat

import (
"fmt"
"strings"

"github.com/hugobyte/dive-core/cli/common"
Expand Down Expand Up @@ -35,8 +36,8 @@ func hardhat(cmd *cobra.Command, args []string) {
cliContext.Fatal(err)
}
}

err = common.WriteServiceResponseData(responseData.ServiceName, *responseData, cliContext)
serviceFileName := fmt.Sprintf(common.ServiceFilePath, common.EnclaveName)
err = common.WriteServiceResponseData(responseData.ServiceName, *responseData, cliContext, serviceFileName)

if err != nil {
cliContext.Fatal(err)
Expand Down
6 changes: 3 additions & 3 deletions cli/cmd/chains/hardhat/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ func RunHardhat(cli *common.Cli) (*common.DiveServiceResponse, error) {
enclaveContext, err := cli.Context().GetEnclaveContext(common.EnclaveName)

if err != nil {
return nil, common.WrapMessageToError(err, "Hardhat Run Failed")
return nil, common.WrapMessageToError(err, "Hardhat Run Failed While Getting Enclave Context")
}

runConfig := common.GetStarlarkRunConfig(`{}`, common.DiveEthHardhatNodeScript, "start_hardhat_node")

response, _, err := enclaveContext.RunStarlarkRemotePackage(cli.Context().GetContext(), common.DiveRemotePackagePath, runConfig)

if err != nil {
return nil, common.WrapMessageToErrorf(common.ErrStarlarkRunFailed, "%s. %s", err, "Hardhat Run Failed")
return nil, common.WrapMessageToErrorf(common.ErrStarlarkRunFailed, "%s. %s", err, "Hardhat Run Failed While Executing Starlark Package.")
}

responseData, services, skippedInstructions, err := common.GetSerializedData(cli, response)
if err != nil {
errRemove := cli.Context().RemoveServicesByServiceNames(services, common.EnclaveName)
if errRemove != nil {
return nil, common.WrapMessageToError(errRemove, "Hardhat Run Failed ")
return nil, common.WrapMessageToError(errRemove, "Hardhat Run Failed .Services Removed")
}

return nil, common.WrapMessageToError(err, "Hardhat Run Failed ")
Expand Down
5 changes: 4 additions & 1 deletion cli/cmd/chains/icon/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package icon

import (
"fmt"

"github.com/hugobyte/dive-core/cli/common"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -81,8 +83,9 @@ func icon(cmd *cobra.Command, args []string) {
}

}
serviceFileName := fmt.Sprintf(common.ServiceFilePath, common.EnclaveName)

err = common.WriteServiceResponseData(response.ServiceName, *response, cliContext)
err = common.WriteServiceResponseData(response.ServiceName, *response, cliContext, serviceFileName)
if err != nil {
cliContext.Error(err)
cliContext.Context().Exit(1)
Expand Down
5 changes: 4 additions & 1 deletion cli/cmd/chains/neutron/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package neutron

import (
"fmt"

"github.com/hugobyte/dive-core/cli/common"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -37,8 +39,9 @@ func neutron(cmd *cobra.Command, args []string) {
if err != nil {
cliContext.Fatal(err)
}
serviceFileName := fmt.Sprintf(common.ServiceFilePath, common.EnclaveName)

err = common.WriteServiceResponseData(response.ServiceName, *response, cliContext)
err = common.WriteServiceResponseData(response.ServiceName, *response, cliContext, serviceFileName)
if err != nil {
cliContext.Fatal(err)
}
Expand Down
47 changes: 39 additions & 8 deletions cli/cmd/chains/utils/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,35 @@ type CosmosServiceConfig struct {
PublicRPC *int `json:"public_rpc"`
}

func (cs *CosmosServiceConfig) LoadDefaultConfig() {
func (cs *CosmosServiceConfig) LoadDefaultConfig() error {
cs.ChainID = nil
cs.Key = nil
cs.Password = nil
cs.PublicGrpc = nil
cs.PublicHTTP = nil
cs.PublicRPC = nil
cs.PublicTCP = nil
cs.Password = nil
publicGrpc, err := common.GetAvailablePort()
if err != nil {
return common.WrapMessageToError(err, "error getting available gRPC port")
}
cs.PublicGrpc = &publicGrpc

publicHTTP, err := common.GetAvailablePort()
if err != nil {
return common.WrapMessageToError(err, "error getting available HTTP port")
}
cs.PublicHTTP = &publicHTTP

publicRPC, err := common.GetAvailablePort()
if err != nil {
return common.WrapMessageToError(err, "error getting available Rpc port")
}
cs.PublicRPC = &publicRPC

publicTCP, err := common.GetAvailablePort()
if err != nil {
return common.WrapMessageToError(err, "error getting available Tcp port")
}
cs.PublicTCP = &publicTCP

return nil
}

func (cs *CosmosServiceConfig) EncodeToString() (string, error) {
Expand Down Expand Up @@ -55,13 +75,24 @@ type IconServiceConfig struct {
Cid string `json:"cid"`
}

func (sc *IconServiceConfig) LoadDefaultConfig() {
func (sc *IconServiceConfig) LoadDefaultConfig() error {
sc.Port = 9080
sc.PublicPort = 8090
sc.P2PListenAddress = "7080"
sc.P2PAddress = "8080"
sc.Cid = "0xacbc4e"

if common.CheckPort(8090) {
sc.PublicPort = 8090
} else {
availablePort, err := common.GetAvailablePort()
if err != nil {
return err
}
sc.PublicPort = availablePort
}

return nil

}

func (sc *IconServiceConfig) EncodeToString() (string, error) {
Expand Down
5 changes: 5 additions & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func run(cmd *cobra.Command, args []string) error {
styles.RenderBanner()
cmd.Help()

err := cmd.ParseFlags(args)
if err != nil {
return err
}

return nil
}

Expand Down
18 changes: 12 additions & 6 deletions cli/cmd/utility/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ func clean(cmd *cobra.Command, args []string) {
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

err = cliContext.FileHandler().RemoveFiles([]string{common.DiveOutFile, common.ServiceFilePath})
if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}

enclaves, err := cliContext.Context().GetEnclaves()
if err != nil {
cliContext.Logger().SetErrorToStderr()
Expand All @@ -60,6 +54,13 @@ func clean(cmd *cobra.Command, args []string) {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}
for _, enclave := range enclaves {
err = cliContext.FileHandler().RemoveFiles([]string{fmt.Sprintf(common.DiveOutFile, enclave.Name), fmt.Sprintf(common.ServiceFilePath, enclave.Name)})
if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}
}

cliContext.Logger().Info(fmt.Sprintf("Enclaves Cleaned %v", enclavesInfo))

Expand All @@ -70,6 +71,11 @@ func clean(cmd *cobra.Command, args []string) {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}
err = cliContext.FileHandler().RemoveFiles([]string{fmt.Sprintf(common.DiveOutFile, common.EnclaveName), fmt.Sprintf(common.ServiceFilePath, common.EnclaveName)})
if err != nil {
cliContext.Logger().SetErrorToStderr()
cliContext.Logger().Fatal(common.CodeOf(err), err.Error())
}
}

cliContext.StopSpinnerIfNotVerbose("Clean Completed", common.DiveLogs)
Expand Down
6 changes: 4 additions & 2 deletions cli/common/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ func (c *Cli) Errorf(format string, err error, args ...interface{}) {

c.spinner.Stop()
c.log.SetErrorToStderr()
c.log.Errorf(CodeOf(err), format, args)
actualError, _ := CoderOf(err)
c.log.Errorf(actualError.ErrorCode(), "%s. message: %s", actualError.Error(), err.Error())
}

func (c *Cli) Fatalf(format string, err error, args ...interface{}) {
c.spinner.Stop()
c.log.SetErrorToStderr()
c.log.Fatalf(CodeOf(err), format, args)
actualError, _ := CoderOf(err)
c.log.Fatalf(actualError.ErrorCode(), "%s. message: %s", actualError.Error(), err.Error())
}

func (c *Cli) Error(err error) {
Expand Down
7 changes: 4 additions & 3 deletions cli/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const (
DiveHardhatNodeAlreadyRuning = "Hardhat Node Already Running"
DiveIconNodeAlreadyRunning = "Icon Node Already Running"
DiveLogDirectory = "/logs/"
DiveDitLogFile = "divelog.log"
DiveDitLogFile = "dive.log"
DiveErrorLogFile = "error.log"
DiveOutFile = "dive.json"
ServiceFilePath = "services.json"
DiveOutFile = "dive_%s.json"
ServiceFilePath = "services_%s.json"
starlarkScript = `
def run(plan, args):
plan.stop_service(name=args["service_name"])
Expand Down Expand Up @@ -78,4 +78,5 @@ const (
InvalidFileError
KurtosisServiceError
InvalidChain
PortError
)
1 change: 1 addition & 0 deletions cli/common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
ErrInvalidFile = NewBase(InvalidFileError, "Failed To Resolve to File")
ErrKurtosisService = NewBase(KurtosisServiceError, "Kurtosis Service Error")
ErrInvalidChain = NewBase(InvalidChain, "Not A Valid Chain")
ErrPortAllocation = NewBase(PortError, "Failed To Allocate Port")
)

func (c ErrorCode) New(msg string) error {
Expand Down
2 changes: 1 addition & 1 deletion cli/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ type EnclaveInfo struct {
}

type ConfigLoader interface {
LoadDefaultConfig()
LoadDefaultConfig() error
LoadConfigFromFile(cliContext *Cli, filePath string) error
}
Loading

0 comments on commit f1c316c

Please sign in to comment.