Skip to content

Commit

Permalink
refactor!: remove as many usages of FreeTCPAddr as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
facundomedica committed Feb 2, 2023
1 parent 79f5140 commit d877dfe
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
3 changes: 3 additions & 0 deletions server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Server struct {
// this mutex to avoid data races.
mtx sync.Mutex
listener net.Listener

ListenerAddr string
}

// CustomGRPCHeaderMatcher for mapping request headers to
Expand Down Expand Up @@ -105,6 +107,7 @@ func (s *Server) Start(cfg config.Config) error {
return err
}

s.ListenerAddr = strings.Replace(listener.Addr().Network()+"://"+listener.Addr().String(), "[::]", "0.0.0.0", 1)
s.listener = listener
s.mtx.Unlock()

Expand Down
12 changes: 7 additions & 5 deletions server/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grpc
import (
"fmt"
"net"
"strings"
"time"

"google.golang.org/grpc"
Expand All @@ -18,7 +19,7 @@ import (
)

// StartGRPCServer starts a gRPC server on the given address.
func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config.GRPCConfig) (*grpc.Server, error) {
func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config.GRPCConfig) (*grpc.Server, string, error) {
maxSendMsgSize := cfg.MaxSendMsgSize
if maxSendMsgSize == 0 {
maxSendMsgSize = config.DefaultGRPCMaxSendMsgSize
Expand Down Expand Up @@ -53,7 +54,7 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config
InterfaceRegistry: clientCtx.InterfaceRegistry,
})
if err != nil {
return nil, err
return nil, "", err
}

// Reflection allows external clients to see what services and methods
Expand All @@ -62,7 +63,7 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config

listener, err := net.Listen("tcp", cfg.Address)
if err != nil {
return nil, err
return nil, "", err
}

errCh := make(chan error)
Expand All @@ -75,10 +76,11 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config

select {
case err := <-errCh:
return nil, err
return nil, "", err

case <-time.After(types.ServerStartTime):
// assume server started successfully
return grpcSrv, nil
addr := strings.Replace(listener.Addr().String(), "[::]", "0.0.0.0", 1)
return grpcSrv, addr, nil
}
}
2 changes: 1 addition & 1 deletion server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ func startInProcess(ctx *Context, clientCtx client.Context, appCreator types.App
ctx.Logger.Debug("grpc client assigned to client context", "target", grpcAddress)

// start grpc server
grpcSrv, err = servergrpc.StartGRPCServer(clientCtx, app, config.GRPC)
grpcSrv, _, err = servergrpc.StartGRPCServer(clientCtx, app, config.GRPC)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/gov/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package gov
import (
"fmt"

"github.com/cosmos/gogoproto/proto"

"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/cosmos/gogoproto/proto"
)

func (s *E2ETestSuite) TestGetProposalGRPC() {
Expand Down
15 changes: 5 additions & 10 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,8 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
if cfg.APIAddress != "" {
apiListenAddr = cfg.APIAddress
} else {
var err error
apiListenAddr, _, err = FreeTCPAddr()
if err != nil {
return nil, err
}
// use a random free tcp port
apiListenAddr = "tcp://0.0.0.0:0"
}

appCfg.API.Address = apiListenAddr
Expand All @@ -364,16 +361,14 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) {
return nil, err
}
tmCfg.RPC.ListenAddress = rpcAddr
// tmCfg.RPC.ListenAddress = "tcp://0.0.0.0:0"
}

if cfg.GRPCAddress != "" {
appCfg.GRPC.Address = cfg.GRPCAddress
} else {
_, grpcPort, err := FreeTCPAddr()
if err != nil {
return nil, err
}
appCfg.GRPC.Address = fmt.Sprintf("0.0.0.0:%s", grpcPort)
// use a random free tcp port
appCfg.GRPC.Address = "0.0.0.0:0"
}
appCfg.GRPC.Enable = true
appCfg.GRPCWeb.Enable = true
Expand Down
12 changes: 11 additions & 1 deletion testutil/network/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net"
"net/url"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -77,12 +78,13 @@ func startInProcess(cfg Config, val *Validator) error {
}

if val.AppConfig.GRPC.Enable {
grpcSrv, err := servergrpc.StartGRPCServer(val.ClientCtx, app, val.AppConfig.GRPC)
grpcSrv, addr, err := servergrpc.StartGRPCServer(val.ClientCtx, app, val.AppConfig.GRPC)
if err != nil {
return err
}

val.grpc = grpcSrv
val.AppConfig.GRPC.Address = addr
}

if val.APIAddress != "" {
Expand All @@ -104,6 +106,14 @@ func startInProcess(cfg Config, val *Validator) error {
}

val.api = apiSrv

// Update the APIAddress in case the port was set to 0 (random port).
apiURL, err := url.Parse(apiSrv.ListenerAddr)
if err != nil {
return err
}
val.APIAddress = fmt.Sprintf("http://%s:%s", apiURL.Hostname(), apiURL.Port())
val.AppConfig.API.Address = apiSrv.ListenerAddr
}

return nil
Expand Down

0 comments on commit d877dfe

Please sign in to comment.