Skip to content

Commit

Permalink
[DEC-2201] Ensure that we use the client context with AutoCLI (#966)
Browse files Browse the repository at this point in the history
* [DEC-2201] Ensure that we use the client context with AutoCLI

Note that the basis of this code is from https://github.com/dydxprotocol/cosmos-sdk/blob/b48250d4e0efacbd40f3e0b980c1e1004d29e351/simapp/simd/cmd/root.go#L103 which is linked from the Cosmos 0.50 upgrade
guide.
  • Loading branch information
lcwik committed Jan 16, 2024
1 parent e8bcc79 commit 3035168
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
10 changes: 8 additions & 2 deletions protocol/cmd/dydxprotocold/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ const (
// NewRootCmd creates a new root command for `dydxprotocold`. It is called once in the main function.
func NewRootCmd(
option *RootCmdOption,
homeDir string,
) *cobra.Command {
return NewRootCmdWithInterceptors(
option,
homeDir,
func(serverCtxPtr *server.Context) {

},
Expand All @@ -78,6 +80,7 @@ func NewRootCmd(

func NewRootCmdWithInterceptors(
option *RootCmdOption,
homeDir string,
serverCtxInterceptor func(serverCtxPtr *server.Context),
appConfigInterceptor func(string, *DydxAppConfig) (string, *DydxAppConfig),
appInterceptor func(app *dydxapp.App) *dydxapp.App,
Expand All @@ -91,7 +94,7 @@ func NewRootCmdWithInterceptors(
WithInput(os.Stdin).
WithAccountRetriever(types.AccountRetriever{}).
WithBroadcastMode(flags.BroadcastSync).
WithHomeDir(dydxapp.DefaultNodeHome).
WithHomeDir(homeDir).
WithViper(EnvPrefix)

rootCmd := &cobra.Command{
Expand Down Expand Up @@ -145,7 +148,10 @@ func NewRootCmdWithInterceptors(
}

initRootCmd(rootCmd, option, encodingConfig, appInterceptor)

initClientCtx, err := config.ReadFromClientConfig(initClientCtx)
if err != nil {
panic(err)
}
if err := autoCliOpts(encodingConfig, initClientCtx).EnhanceRootCommand(rootCmd); err != nil {
panic(err)
}
Expand Down
47 changes: 47 additions & 0 deletions protocol/cmd/dydxprotocold/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cmd_test

import (
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
"github.com/dydxprotocol/v4-chain/protocol/app"
"github.com/dydxprotocol/v4-chain/protocol/app/config"
"github.com/dydxprotocol/v4-chain/protocol/cmd/dydxprotocold/cmd"
"github.com/stretchr/testify/require"
"testing"
)

func TestNewRootCmd_UsesClientConfig(t *testing.T) {
tempDir := t.TempDir()

config.SetupConfig()

// Set the client config to point to a fake chain id since this is a required option
{
option := cmd.GetOptionWithCustomStartCmd()
rootCmd := cmd.NewRootCmd(option, tempDir)

cmd.AddTendermintSubcommands(rootCmd)
cmd.AddInitCmdPostRunE(rootCmd)
rootCmd.SetArgs([]string{"config", "set", "client", "chain-id", "fakeChainId"})
require.NoError(t, svrcmd.Execute(rootCmd, app.AppDaemonName, tempDir))
}

// Set the client config to point to a fake address
{
option := cmd.GetOptionWithCustomStartCmd()
rootCmd := cmd.NewRootCmd(option, tempDir)

cmd.AddTendermintSubcommands(rootCmd)
cmd.AddInitCmdPostRunE(rootCmd)
rootCmd.SetArgs([]string{"config", "set", "client", "node", "fakeTestAddress"})
require.NoError(t, svrcmd.Execute(rootCmd, app.AppDaemonName, tempDir))
}

// Run a query command (that will fail) to ensure that we are reading the client config
option := cmd.GetOptionWithCustomStartCmd()
rootCmd := cmd.NewRootCmd(option, tempDir)

cmd.AddTendermintSubcommands(rootCmd)
cmd.AddInitCmdPostRunE(rootCmd)
rootCmd.SetArgs([]string{"query", "auth", "params"})
require.ErrorContains(t, svrcmd.Execute(rootCmd, app.AppDaemonName, tempDir), "fakeTestAddress")
}
2 changes: 1 addition & 1 deletion protocol/cmd/dydxprotocold/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func main() {
config.SetupConfig()

option := cmd.GetOptionWithCustomStartCmd()
rootCmd := cmd.NewRootCmd(option)
rootCmd := cmd.NewRootCmd(option, app.DefaultNodeHome)

cmd.AddTendermintSubcommands(rootCmd)
cmd.AddInitCmdPostRunE(rootCmd)
Expand Down
3 changes: 2 additions & 1 deletion protocol/testing/containertest/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package containertest
import (
"context"
"fmt"
"github.com/dydxprotocol/v4-chain/protocol/app"
"time"

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

option := cmd.GetOptionWithCustomStartCmd()
rootCmd := cmd.NewRootCmd(option)
rootCmd := cmd.NewRootCmd(option, app.DefaultNodeHome)
flags.AddTxFlagsToCmd(rootCmd)
flags := rootCmd.Flags()

Expand Down
1 change: 1 addition & 0 deletions protocol/testutil/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ func launchValidatorInDir(
option := cmd.GetOptionWithCustomStartCmd()
rootCmd := cmd.NewRootCmdWithInterceptors(
option,
validatorHomeDir,
// Inject the app options and logger
func(serverCtxPtr *server.Context) {
for key, value := range appOptions {
Expand Down

0 comments on commit 3035168

Please sign in to comment.