-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Miscelaneous refactors to testnode while working on #2858 but doesn't actually close that issue. This PR adds more logging to help debug the second scenario alluded to in the original post. --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
- Loading branch information
1 parent
5564837
commit 30e907f
Showing
9 changed files
with
155 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package testnode | ||
|
||
import ( | ||
pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types" | ||
"github.com/cosmos/cosmos-sdk/server" | ||
) | ||
|
||
type KVAppOptions struct { | ||
options map[string]interface{} | ||
} | ||
|
||
// Get returns the option value for the given option key. | ||
func (ao *KVAppOptions) Get(option string) interface{} { | ||
return ao.options[option] | ||
} | ||
|
||
// Set sets a key-value app option. | ||
func (ao *KVAppOptions) Set(option string, value interface{}) { | ||
ao.options[option] = value | ||
} | ||
|
||
// DefaultAppOptions returns the default application options. | ||
func DefaultAppOptions() *KVAppOptions { | ||
opts := &KVAppOptions{options: make(map[string]interface{})} | ||
opts.Set(server.FlagPruning, pruningtypes.PruningOptionNothing) | ||
return opts | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package testnode | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"testing" | ||
|
||
"github.com/celestiaorg/celestia-app/test/util/genesis" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// NewNetwork starts a single validator celestia-app network using the provided | ||
// configurations. Configured accounts will be funded and their keys can be | ||
// accessed in keyring returned client.Context. All rpc, p2p, and grpc addresses | ||
// in the provided configs are overwritten to use open ports. The node can be | ||
// accessed via the returned client.Context or via the returned rpc and grpc | ||
// addresses. Configured genesis options will be applied after all accounts have | ||
// been initialized. | ||
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", mustGetFreePort()) | ||
tmCfg.P2P.ListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", mustGetFreePort()) | ||
tmCfg.RPC.GRPCListenAddress = fmt.Sprintf("tcp://127.0.0.1:%d", mustGetFreePort()) | ||
|
||
// initialize the genesis file and validator files for the first validator. | ||
baseDir, err := genesis.InitFiles(t.TempDir(), tmCfg, cfg.Genesis, 0) | ||
require.NoError(t, err) | ||
|
||
tmNode, app, err := NewCometNode(t, baseDir, cfg) | ||
require.NoError(t, err) | ||
|
||
cctx = NewContext(context.Background(), cfg.Genesis.Keyring(), tmCfg, cfg.Genesis.ChainID) | ||
|
||
cctx, stopNode, err := StartNode(t, tmNode, cctx) | ||
require.NoError(t, err) | ||
|
||
appCfg := cfg.AppConfig | ||
appCfg.GRPC.Address = fmt.Sprintf("127.0.0.1:%d", mustGetFreePort()) | ||
appCfg.API.Address = fmt.Sprintf("tcp://127.0.0.1:%d", mustGetFreePort()) | ||
|
||
cctx, cleanupGRPC, err := StartGRPCServer(app, appCfg, cctx) | ||
require.NoError(t, err) | ||
|
||
t.Cleanup(func() { | ||
t.Log("tearing down testnode") | ||
err := stopNode() | ||
if err != nil { | ||
// the test has already completed so log the error instead of | ||
// failing the test. | ||
t.Logf("error stopping node %v", err) | ||
} | ||
err = cleanupGRPC() | ||
if err != nil { | ||
// the test has already completed so just log the error instead of | ||
// failing the test. | ||
t.Logf("error when cleaning up GRPC %v", err) | ||
} | ||
}) | ||
|
||
return cctx, tmCfg.RPC.ListenAddress, appCfg.GRPC.Address | ||
} | ||
|
||
// getFreePort returns a free port and optionally an error. | ||
func getFreePort() (int, error) { | ||
a, err := net.ResolveTCPAddr("tcp", "localhost:0") | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
l, err := net.ListenTCP("tcp", a) | ||
if err != nil { | ||
return 0, err | ||
} | ||
defer l.Close() | ||
return l.Addr().(*net.TCPAddr).Port, nil | ||
} | ||
|
||
// mustGetFreePort returns a free port. Panics if no free ports are available or | ||
// an error is encountered. | ||
func mustGetFreePort() int { | ||
port, err := getFreePort() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return port | ||
} |
Oops, something went wrong.