Skip to content

Commit

Permalink
[CLOB-930, CORE-29] Extend the e2e test framework to start the cosmos…
Browse files Browse the repository at this point in the history
… app using cmd.

This works towards enabling and passing in flags for flag based testing.
This also works towards enabling daemons on e2e app start-up once the daemons can be cleaned up.
This relies on dydxprotocol/cosmos-sdk#29
  • Loading branch information
lcwik committed Oct 25, 2023
1 parent ed6429f commit 6cbfd6e
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 37 deletions.
9 changes: 1 addition & 8 deletions protocol/app/ante/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package ante_test
import (
"cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/baseapp"
bank "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/dydxprotocol/v4-chain/protocol/cmd/dydxprotocold/cmd"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
assets "github.com/dydxprotocol/v4-chain/protocol/x/assets/types"
"reflect"
Expand Down Expand Up @@ -180,12 +178,7 @@ func TestSubmitTxnWithGas(t *testing.T) {
},
}

tApp := testapp.NewTestAppBuilder(t).
WithAppOptions(
map[string]interface{}{},
baseapp.SetMinGasPrices(cmd.MinGasPrice),
).
Build()
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()

msgSendCheckTx := testapp.MustMakeCheckTxWithPrivKeySupplier(
Expand Down
5 changes: 3 additions & 2 deletions protocol/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ func TestAppIsFullyInitialized(t *testing.T) {
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
dydxApp := testapp.DefaultTestApp(tc.customFlags)
uninitializedFields := getUninitializedStructFields(reflect.ValueOf(*dydxApp))
tApp := testapp.NewTestAppBuilder(t).WithAppOptions(tc.customFlags).Build()
tApp.InitChain()
uninitializedFields := getUninitializedStructFields(reflect.ValueOf(*tApp.App))

// Note that the PriceFeedClient is currently hard coded as disabled in GetDefaultTestAppOptions.
// Normally it would be only disabled for non-validating full nodes or for nodes where the
Expand Down
17 changes: 10 additions & 7 deletions protocol/app/prepare/full_node_prepare_proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ import (
// TestFullNodePrepareProposalHandler test that the full-node PrepareProposal handler always returns
// an empty result.
func TestFullNodePrepareProposalHandler(t *testing.T) {
t.Cleanup(gometrics.Shutdown)

conf := gometrics.DefaultConfig("service")
sink := gometrics.NewInmemSink(time.Hour, time.Hour)
_, err := gometrics.NewGlobal(conf, sink)
require.NoError(t, err)

logger, logBuffer := testlog.TestLogger()
appOpts := map[string]interface{}{
flags.NonValidatingFullNodeFlag: true,
testlog.LoggerInstanceForTest: logger,
}
tApp := testApp.NewTestAppBuilder(t).WithAppOptions(appOpts).Build()
tApp.InitChain()

// Set up metrics after test app initialization to override the telemetry that it sets up.
// TODO(CLOB-930): Expose test app telemetry directly instead of requiring tests to do this setup and clean-up
// themselves.
t.Cleanup(gometrics.Shutdown)
conf := gometrics.DefaultConfig("service")
sink := gometrics.NewInmemSink(time.Hour, time.Hour)
_, err := gometrics.NewGlobal(conf, sink)
require.NoError(t, err)

found := false
tApp.AdvanceToBlock(2, testApp.AdvanceToBlockOptions{
Expand Down
4 changes: 2 additions & 2 deletions protocol/cmd/dydxprotocold/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type DydxAppConfig struct {

// initAppConfig helps to override default appConfig template and configs.
// return "", nil if no custom configuration is required for the application.
func initAppConfig() (string, interface{}) {
func initAppConfig() (string, *DydxAppConfig) {
// Optionally allow the chain developer to overwrite the SDK's default
// server config.
srvCfg := serverconfig.DefaultConfig()
Expand Down Expand Up @@ -68,7 +68,7 @@ func initAppConfig() (string, interface{}) {

appTemplate := serverconfig.DefaultConfigTemplate

return appTemplate, appConfig
return appTemplate, &appConfig
}

// initTendermintConfig helps to override default Tendermint Config values.
Expand Down
19 changes: 13 additions & 6 deletions protocol/cmd/dydxprotocold/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const (

// TODO(DEC-1097): improve `cmd/` by adding tests, custom app configs, custom init cmd, and etc.
// NewRootCmd creates a new root command for `dydxprotocold`. It is called once in the main function.
func NewRootCmd(option *RootCmdOption) *cobra.Command {
func NewRootCmd(option *RootCmdOption, serverCtxInterceptor func(serverCtxPtr *server.Context), appConfigInterceptor func(string, *DydxAppConfig) (string, *DydxAppConfig), appInterceptor func(app *dydxapp.App)) *cobra.Command {
encodingConfig := dydxapp.GetEncodingConfig()
initClientCtx := client.Context{}.
WithCodec(encodingConfig.Codec).
Expand Down Expand Up @@ -85,7 +85,7 @@ func NewRootCmd(option *RootCmdOption) *cobra.Command {
return err
}

customAppTemplate, customAppConfig := initAppConfig()
customAppTemplate, customAppConfig := appConfigInterceptor(initAppConfig())
customTMConfig := initTendermintConfig()

if err := server.InterceptConfigsPreRunHandler(
Expand All @@ -97,18 +97,20 @@ func NewRootCmd(option *RootCmdOption) *cobra.Command {
return err
}

serverCtxInterceptor(server.GetServerContextFromCmd(cmd))

return nil
},
SilenceUsage: true,
}

initRootCmd(rootCmd, option, encodingConfig)
initRootCmd(rootCmd, option, encodingConfig, appInterceptor)

return rootCmd
}

// initRootCmd initializes the app's root command with useful commands.
func initRootCmd(rootCmd *cobra.Command, option *RootCmdOption, encodingConfig dydxapp.EncodingConfig) {
func initRootCmd(rootCmd *cobra.Command, option *RootCmdOption, encodingConfig dydxapp.EncodingConfig, appInterceptor func(app *dydxapp.App)) {
gentxModule := basic_manager.ModuleBasics[genutiltypes.ModuleName].(genutil.AppModuleBasic)
rootCmd.AddCommand(
genutilcli.InitCmd(basic_manager.ModuleBasics, dydxapp.DefaultNodeHome),
Expand All @@ -131,7 +133,9 @@ func initRootCmd(rootCmd *cobra.Command, option *RootCmdOption, encodingConfig d
server.AddCommands(
rootCmd,
dydxapp.DefaultNodeHome,
a.newApp,
func(logger log.Logger, db dbm.DB, writer io.Writer, options servertypes.AppOptions) servertypes.Application {
return a.newApp(logger, db, writer, options, appInterceptor)
},
a.appExport,
func(cmd *cobra.Command) {
addModuleInitFlags(cmd)
Expand Down Expand Up @@ -222,6 +226,7 @@ func (a appCreator) newApp(
db dbm.DB,
traceStore io.Writer,
appOpts servertypes.AppOptions,
appInterceptor func(app *dydxapp.App),
) servertypes.Application {
var cache sdk.MultiStorePersistentCache

Expand Down Expand Up @@ -266,7 +271,7 @@ func (a appCreator) newApp(
cast.ToUint32(appOpts.Get(server.FlagStateSyncSnapshotKeepRecent)),
)

return dydxapp.New(
app := dydxapp.New(
logger,
db,
traceStore,
Expand All @@ -284,6 +289,8 @@ func (a appCreator) newApp(
baseapp.SetIAVLCacheSize(int(cast.ToUint64(appOpts.Get(flagIAVLCacheSize)))),
baseapp.SetChainID(chainID),
)
appInterceptor(app)
return app
}

// appExport creates and exports a new app, returns the state of the app for a genesis file.
Expand Down
10 changes: 9 additions & 1 deletion protocol/cmd/dydxprotocold/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"github.com/cosmos/cosmos-sdk/server"
"os"

svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
Expand All @@ -13,7 +14,14 @@ func main() {
config.SetupConfig()

option := cmd.GetOptionWithCustomStartCmd()
rootCmd := cmd.NewRootCmd(option)
rootCmd := cmd.NewRootCmd(
option,
func(serverCtxPtr *server.Context) {},
func(s string, appConfig *cmd.DydxAppConfig) (string, *cmd.DydxAppConfig) {
return s, appConfig
},
func(app *app.App) {},
)

cmd.AddTendermintSubcommands(rootCmd)

Expand Down
2 changes: 1 addition & 1 deletion protocol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ replace (
// Use dYdX fork of CometBFT
github.com/cometbft/cometbft => github.com/dydxprotocol/cometbft v0.37.3-0.20230908230338-65f7a2f25c18
// Use dYdX fork of Cosmos SDK
github.com/cosmos/cosmos-sdk => github.com/dydxprotocol/cosmos-sdk v0.47.5-0.20231011192538-b95c66dedbd5
github.com/cosmos/cosmos-sdk => github.com/dydxprotocol/cosmos-sdk v0.47.5-0.20231025180353-ae924e1b73a2
// Cosmos SDK 0.47.x upgrade guide (https://github.com/cosmos/cosmos-sdk/blob/main/UPGRADING.md#replaces) mentions
// that there are stability issues. See https://github.com/cosmos/cosmos-sdk/issues/14949 and
// https://github.com/ethereum/go-ethereum/pull/25413 for further context.
Expand Down
4 changes: 2 additions & 2 deletions protocol/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ github.com/dvsekhvalnov/jose2go v1.5.0 h1:3j8ya4Z4kMCwT5nXIKFSV84YS+HdqSSO0VsTQx
github.com/dvsekhvalnov/jose2go v1.5.0/go.mod h1:QsHjhyTlD/lAVqn/NSbVZmSCGeDehTB/mPZadG+mhXU=
github.com/dydxprotocol/cometbft v0.37.3-0.20230908230338-65f7a2f25c18 h1:1RIco92QcPS24BeNCNWJC4zXza4GEHHuoviWIQEQ/NI=
github.com/dydxprotocol/cometbft v0.37.3-0.20230908230338-65f7a2f25c18/go.mod h1:cpghf0+1GJpJvrqpTHE6UyTcD05m/xllo0xpufL3PgA=
github.com/dydxprotocol/cosmos-sdk v0.47.5-0.20231011192538-b95c66dedbd5 h1:9lSntpmEJcEhc3al6YmRh51ZHINjqJmzL5it9tMK5+0=
github.com/dydxprotocol/cosmos-sdk v0.47.5-0.20231011192538-b95c66dedbd5/go.mod h1:iaAXVu5Jcd//vREctLTuxLqj5ScUP4psgqW7M6XsaQ8=
github.com/dydxprotocol/cosmos-sdk v0.47.5-0.20231025180353-ae924e1b73a2 h1:6rsHwImoXXeMswCzJOl4znD9iVX8hRIorwO8VD0NyOU=
github.com/dydxprotocol/cosmos-sdk v0.47.5-0.20231025180353-ae924e1b73a2/go.mod h1:iaAXVu5Jcd//vREctLTuxLqj5ScUP4psgqW7M6XsaQ8=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-resiliency v1.3.0 h1:RRL0nge+cWGlxXbUzJ7yMcq6w2XBEr19dCN6HECGaT0=
github.com/eapache/go-resiliency v1.3.0/go.mod h1:5yPzW0MIvSe0JDsv0v+DvcjEv2FyD6iZYSs1ZI+iQho=
Expand Down
10 changes: 9 additions & 1 deletion protocol/testing/containertest/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package containertest
import (
"context"
"fmt"
"github.com/cosmos/cosmos-sdk/server"
"github.com/dydxprotocol/v4-chain/protocol/app"
"time"

comethttp "github.com/cometbft/cometbft/rpc/client/http"
Expand Down Expand Up @@ -105,7 +107,13 @@ func (n *Node) getContextForBroadcastTx(signer string) (*client.Context, *pflag.
WithViper(cmd.EnvPrefix)

option := cmd.GetOptionWithCustomStartCmd()
rootCmd := cmd.NewRootCmd(option)
rootCmd := cmd.NewRootCmd(option,
func(serverCtxPtr *server.Context) {},
func(s string, appConfig *cmd.DydxAppConfig) (string, *cmd.DydxAppConfig) {
return s, appConfig
},
func(app *app.App) {},
)
flags.AddTxFlagsToCmd(rootCmd)
flags := rootCmd.Flags()

Expand Down
Loading

0 comments on commit 6cbfd6e

Please sign in to comment.