Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Nov 14, 2023
1 parent 19c4dff commit 77c298d
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 23 deletions.
3 changes: 0 additions & 3 deletions client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ require (
google.golang.org/protobuf v1.31.0
gotest.tools/v3 v3.5.1
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/chzyer/readline v1.5.1 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
)
Expand Down
68 changes: 68 additions & 0 deletions client/v2/internal/prompt/validation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package prompt

import (
"fmt"
"net/url"
"unicode"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// ValidatePromptNotEmpty validates that the input is not empty.
func ValidatePromptNotEmpty(input string) error {
if input == "" {
return fmt.Errorf("input cannot be empty")
}

return nil
}

// ValidatePromptURL validates that the input is a valid URL.
func ValidatePromptURL(input string) error {
_, err := url.ParseRequestURI(input)
if err != nil {
return fmt.Errorf("invalid URL: %w", err)
}

return nil
}

// ValidatePromptAddress validates that the input is a valid Bech32 address.
func ValidatePromptAddress(input string) error { // TODO(@julienrbrt) remove and add prompts in AutoCLI
_, err := sdk.AccAddressFromBech32(input)
if err == nil {
return nil
}

_, err = sdk.ValAddressFromBech32(input)
if err == nil {
return nil
}

_, err = sdk.ConsAddressFromBech32(input)
if err == nil {
return nil
}

return fmt.Errorf("invalid address: %w", err)
}

// ValidatePromptYesNo validates that the input is valid sdk.COins
func ValidatePromptCoins(input string) error {
if _, err := sdk.ParseCoinsNormalized(input); err != nil {
return fmt.Errorf("invalid coins: %w", err)
}

return nil
}

// CamelCaseToString converts a camel case string to a string with spaces.
func CamelCaseToString(str string) string {
w := []rune(str)
for i := len(w) - 1; i > 1; i-- {
if unicode.IsUpper(w[i]) {
w = append(w[:i], append([]rune{' '}, w[i:]...)...)
}
}
return string(w)
}
39 changes: 39 additions & 0 deletions client/v2/internal/prompt/validation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package prompt_test

import (
"testing"

"github.com/stretchr/testify/require"

"cosmossdk.io/client/v2/internal/prompt"
)

func TestValidatePromptNotEmpty(t *testing.T) {
require := require.New(t)

require.NoError(prompt.ValidatePromptNotEmpty("foo"))
require.ErrorContains(prompt.ValidatePromptNotEmpty(""), "input cannot be empty")
}

func TestValidatePromptURL(t *testing.T) {
require := require.New(t)

require.NoError(prompt.ValidatePromptURL("https://example.com"))
require.ErrorContains(prompt.ValidatePromptURL("foo"), "invalid URL")
}

func TestValidatePromptAddress(t *testing.T) {
require := require.New(t)

require.NoError(prompt.ValidatePromptAddress("cosmos1huydeevpz37sd9snkgul6070mstupukw00xkw9"))
require.NoError(prompt.ValidatePromptAddress("cosmosvaloper1sjllsnramtg3ewxqwwrwjxfgc4n4ef9u2lcnj0"))
require.NoError(prompt.ValidatePromptAddress("cosmosvalcons1ntk8eualewuprz0gamh8hnvcem2nrcdsgz563h"))
require.ErrorContains(prompt.ValidatePromptAddress("foo"), "invalid address")
}

func TestValidatePromptCoins(t *testing.T) {
require := require.New(t)

require.NoError(prompt.ValidatePromptCoins("100stake"))
require.ErrorContains(prompt.ValidatePromptCoins("foo"), "invalid coins")
}
8 changes: 6 additions & 2 deletions x/auth/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Service: authv1beta1.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
RpcMethod: "UpdateParams",
Use: "update-params-proposal [params]",
Short: "Submit a proposal to update auth module params",
Example: fmt.Sprintf(`%s tx auth update-params-proposal '{ params }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
},
},
Expand Down
17 changes: 14 additions & 3 deletions x/bank/autocli.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package bank

import (
"fmt"
"strings"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1"

"github.com/cosmos/cosmos-sdk/version"
)

// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface.
Expand Down Expand Up @@ -109,13 +112,21 @@ Note: multiple coins can be send by space separated.`,
{
RpcMethod: "UpdateParams",
Use: "update-params-proposal [params]",
Short: "Submit a parameter change proposal for the bank module",
Short: "Submit a proposal to update bank module params",
Example: fmt.Sprintf(`%s tx bank update-params-proposal '{ params }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
{
RpcMethod: "SetSendEnabled",
Skip: true, // skipped because authority gated
RpcMethod: "SetSendEnabled",
Use: "set-send-enabled-proposal [send_enabled]",
Short: "Submit a proposal to set/update/delete send enabled entries",
Example: fmt.Sprintf(`%s tx bank set-send-enabled-proposal '{"denom":"stake","enabled":true}'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "send_enabled", Varargs: true}},
FlagOptions: map[string]*autocliv1.FlagOptions{
"use_default_for": {Name: "use-default-for", Usage: "Use default for the given denom (delete a send enabled entry)"},
},
GovProposal: true,
},
},
},
Expand Down
11 changes: 9 additions & 2 deletions x/consensus/autocli.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package consensus

import (
"fmt"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
consensusv1 "cosmossdk.io/api/cosmos/consensus/v1"

"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
"github.com/cosmos/cosmos-sdk/version"
)

// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface.
Expand All @@ -27,8 +30,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Service: consensusv1.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
RpcMethod: "UpdateParams",
Use: "update-params-proposal [params]",
Short: "Submit a proposal to update consensus module params",
Example: fmt.Sprintf(`%s tx consensus update-params-proposal '{ params }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
},
},
Expand Down
11 changes: 9 additions & 2 deletions x/crisis/autocli.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package crisis

import (
"fmt"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
crisisv1beta1 "cosmossdk.io/api/cosmos/crisis/v1beta1"
"github.com/cosmos/cosmos-sdk/version"
)

// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface.
Expand All @@ -21,8 +24,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
},
},
{
RpcMethod: "UpdateParams",
Skip: true, // Skipped because UpdateParams is authority gated
RpcMethod: "UpdateParams",
Use: "update-params-proposal [params]",
Short: "Submit a proposal to update crisis module params",
Example: fmt.Sprintf(`%s tx crisis update-params-proposal '{ params }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
},
},
Expand Down
10 changes: 7 additions & 3 deletions x/distribution/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,16 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
},
},
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
RpcMethod: "UpdateParams",
Use: "update-params-proposal [params]",
Short: "Submit a proposal to update distribution module params",
Example: fmt.Sprintf(`%s tx distribution update-params-proposal '{ params }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
{
RpcMethod: "CommunityPoolSpend",
Skip: true, // skipped because authority gated
Skip: true, // skipped because deprecated in favor of protocolpool
},
},
EnhanceCustomCommand: true,
Expand Down
8 changes: 6 additions & 2 deletions x/gov/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
},
},
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
RpcMethod: "UpdateParams",
Use: "update-params-proposal [params]",
Short: "Submit a proposal to update gov module params",
Example: fmt.Sprintf(`%s tx gov update-params-proposal '{ params }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
},
EnhanceCustomCommand: true, // We still have manual commands in gov that we want to keep
Expand Down
2 changes: 2 additions & 0 deletions x/gov/client/cli/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ var suggestedProposalTypes = []proposalType{
// Prompt prompts the user for all values of the given type.
// data is the struct to be filled
// namePrefix is the name to be displayed as "Enter <namePrefix> <field>"
// TODO: when bringing this in autocli, use proto message instead
// this will simplify the get address logic
func Prompt[T any](data T, namePrefix string) (T, error) {
v := reflect.ValueOf(&data).Elem()
if v.Kind() == reflect.Interface {
Expand Down
11 changes: 9 additions & 2 deletions x/mint/autocli.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package mint

import (
"fmt"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
mintv1beta1 "cosmossdk.io/api/cosmos/mint/v1beta1"
"github.com/cosmos/cosmos-sdk/version"
)

func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Expand Down Expand Up @@ -31,8 +34,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Service: mintv1beta1.Msg_ServiceDesc.ServiceName,
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
RpcMethod: "UpdateParams",
Use: "update-params-proposal [params]",
Short: "Submit a proposal to update mint module params",
Example: fmt.Sprintf(`%s tx mint update-params-proposal '{ params }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
},
},
Expand Down
8 changes: 6 additions & 2 deletions x/slashing/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
Example: fmt.Sprintf("%s tx slashing unjail --from [validator]", version.AppName),
},
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
RpcMethod: "UpdateParams",
Use: "update-params-proposal [params]",
Short: "Submit a proposal to update slashing module params",
Example: fmt.Sprintf(`%s tx slashing update-params-proposal '{ params }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
},
},
Expand Down
8 changes: 6 additions & 2 deletions x/staking/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,12 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "validator_address"}, {ProtoField: "amount"}, {ProtoField: "creation_height"}},
},
{
RpcMethod: "UpdateParams",
Skip: true, // skipped because authority gated
RpcMethod: "UpdateParams",
Use: "update-params-proposal [params]",
Short: "Submit a proposal to update staking module params",
Example: fmt.Sprintf(`%s tx staking update-params-proposal '{ params }'`, version.AppName),
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "params"}},
GovProposal: true,
},
},
EnhanceCustomCommand: true,
Expand Down

0 comments on commit 77c298d

Please sign in to comment.