Skip to content

Commit

Permalink
fix: default configs should have free ports
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-forbes committed Jul 26, 2023
1 parent 2720c17 commit 69df0e2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
6 changes: 6 additions & 0 deletions test/util/testnode/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testnode

import (
"fmt"
"time"

"github.com/celestiaorg/celestia-app/cmd/celestia-appd/cmd"
Expand Down Expand Up @@ -190,5 +191,10 @@ func DefaultTendermintConfig() *tmconfig.Config {
// chosen only as an arbitrary large number).
tmCfg.RPC.MaxBodyBytes = 200_000_000

// set all the ports to random open ones
tmCfg.RPC.ListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())
tmCfg.P2P.ListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())
tmCfg.RPC.GRPCListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())

return tmCfg
}
18 changes: 4 additions & 14 deletions test/util/testnode/full_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package testnode
import (
"context"
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -155,11 +154,6 @@ func DefaultGenesisState(fundedAccounts ...string) (map[string]json.RawMessage,
func NewNetwork(t testing.TB, cfg *Config) (cctx Context, rpcAddr, grpcAddr string) {
t.Helper()

tmCfg := cfg.TmConfig
tmCfg.RPC.ListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())
tmCfg.P2P.ListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())
tmCfg.RPC.GRPCListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())

genState, kr, err := DefaultGenesisState(cfg.Accounts...)
require.NoError(t, err)

Expand All @@ -169,22 +163,18 @@ func NewNetwork(t testing.TB, cfg *Config) (cctx Context, rpcAddr, grpcAddr stri

chainID := cfg.ChainID

baseDir, kr, err := InitFiles(t, cfg.ConsensusParams, tmCfg, genState, kr, chainID)
baseDir, kr, err := InitFiles(t, cfg.ConsensusParams, cfg.TmConfig, genState, kr, chainID)
require.NoError(t, err)

tmNode, app, err := NewCometNode(t, baseDir, cfg)
require.NoError(t, err)

cctx = NewContext(context.TODO(), kr, tmCfg, chainID)
cctx = NewContext(context.TODO(), kr, cfg.TmConfig, chainID)

cctx, stopNode, err := StartNode(tmNode, cctx)
require.NoError(t, err)

appCfg := cfg.AppConfig
appCfg.GRPC.Address = fmt.Sprintf("127.0.0.1:%d", GetFreePort())
appCfg.API.Address = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())

cctx, cleanupGRPC, err := StartGRPCServer(app, appCfg, cctx)
cctx, cleanupGRPC, err := StartGRPCServer(app, cfg.AppConfig, cctx)
require.NoError(t, err)

t.Cleanup(func() {
Expand All @@ -193,7 +183,7 @@ func NewNetwork(t testing.TB, cfg *Config) (cctx Context, rpcAddr, grpcAddr stri
require.NoError(t, cleanupGRPC())
})

return cctx, tmCfg.RPC.ListenAddress, appCfg.GRPC.Address
return cctx, cfg.TmConfig.RPC.ListenAddress, cfg.AppConfig.GRPC.Address
}

func GetFreePort() int {
Expand Down
6 changes: 5 additions & 1 deletion test/util/testnode/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package testnode

import (
"context"
"fmt"
"os"
"path"
"strings"
Expand Down Expand Up @@ -79,7 +80,10 @@ func StartGRPCServer(app srvtypes.Application, appCfg *srvconfig.Config, cctx Co

// DefaultAppConfig wraps the default config described in the server
func DefaultAppConfig() *srvconfig.Config {
return srvconfig.DefaultConfig()
appCfg := srvconfig.DefaultConfig()
appCfg.GRPC.Address = fmt.Sprintf("127.0.0.1:%d", GetFreePort())
appCfg.API.Address = fmt.Sprintf("tcp://127.0.0.1:%d", GetFreePort())
return appCfg
}

// removeDir removes the directory `rootDir`.
Expand Down

0 comments on commit 69df0e2

Please sign in to comment.