-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: validate validator addresses in update-validator-auths proposal #411
Changes from all commits
3ff04ac
9d55654
cafa3e1
b15f23f
c7d0e87
2b49756
ed23de1
f499052
5a24745
2ef6d70
b7c2deb
d5aabc1
3dcb48f
e3b7433
5b53414
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,15 +14,15 @@ service Query { | |
option (google.api.http).get = "/lbm/consortium/v1/params"; | ||
} | ||
|
||
// ValidatorAuths queries authorization infos of validators. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reposition the two functions |
||
rpc ValidatorAuths(QueryValidatorAuthsRequest) returns (QueryValidatorAuthsResponse) { | ||
option (google.api.http).get = "/lbm/consortium/v1/validators"; | ||
} | ||
|
||
// ValidatorAuth queries authorization info of a validator. | ||
rpc ValidatorAuth(QueryValidatorAuthRequest) returns (QueryValidatorAuthResponse) { | ||
option (google.api.http).get = "/lbm/consortium/v1/validators/{validator_address}"; | ||
} | ||
|
||
// ValidatorAuths queries authorization infos of validators. | ||
rpc ValidatorAuths(QueryValidatorAuthsRequest) returns (QueryValidatorAuthsResponse) { | ||
option (google.api.http).get = "/lbm/consortium/v1/validators"; | ||
} | ||
} | ||
|
||
// QueryParamsRequest is the request type for the Query/Params RPC method. | ||
|
@@ -33,6 +33,19 @@ message QueryParamsResponse { | |
Params params = 1; | ||
} | ||
|
||
// QueryValidatorAuthRequest is the request type for the | ||
// Query/ValidatorAuth RPC method. | ||
message QueryValidatorAuthRequest { | ||
// validator_address defines the validator address to query for. | ||
string validator_address = 1; | ||
} | ||
|
||
// QueryValidatorAuthResponse is the request type for the | ||
// Query/ValidatorAuth RPC method. | ||
message QueryValidatorAuthResponse { | ||
ValidatorAuth auth = 1; | ||
} | ||
|
||
// QueryValidatorAuthsRequest is the request type for the | ||
// Query/ValidatorAuths RPC method. | ||
message QueryValidatorAuthsRequest { | ||
|
@@ -48,16 +61,3 @@ message QueryValidatorAuthsResponse { | |
// pagination defines the pagination in the response. | ||
lbm.base.query.v1.PageResponse pagination = 2; | ||
} | ||
|
||
// QueryValidatorAuthRequest is the request type for the | ||
// Query/ValidatorAuth RPC method. | ||
message QueryValidatorAuthRequest { | ||
// validator_address defines the validator address to query for. | ||
string validator_address = 1; | ||
} | ||
|
||
// QueryValidatorAuthResponse is the request type for the | ||
// Query/ValidatorAuth RPC method. | ||
message QueryValidatorAuthResponse { | ||
ValidatorAuth auth = 1; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Package rest provides HTTP types and primitives for REST | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. import rest.go from cosmos-sdk |
||
// requests validation and responses handling. | ||
package rest | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// GetRequest defines a wrapper around an HTTP GET request with a provided URL. | ||
// An error is returned if the request or reading the body fails. | ||
func GetRequest(url string) ([]byte, error) { | ||
res, err := http.Get(url) // nolint:gosec | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer res.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return body, nil | ||
} | ||
|
||
// PostRequest defines a wrapper around an HTTP POST request with a provided URL and data. | ||
// An error is returned if the request or reading the body fails. | ||
func PostRequest(url string, contentType string, data []byte) ([]byte, error) { | ||
res, err := http.Post(url, contentType, bytes.NewBuffer(data)) // nolint:gosec | ||
if err != nil { | ||
return nil, fmt.Errorf("error while sending post request: %w", err) | ||
} | ||
defer res.Body.Close() | ||
|
||
bz, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading response body: %w", err) | ||
} | ||
|
||
return bz, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,24 +11,24 @@ import ( | |
"github.com/line/lbm-sdk/x/consortium/types" | ||
) | ||
|
||
// GetQueryCmd returns the parent command for all x/consortium CLi query commands. | ||
func GetQueryCmd() *cobra.Command { | ||
// NewQueryCmd returns the parent command for all x/consortium CLi query commands. | ||
func NewQueryCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: types.ModuleName, | ||
Short: "Querying commands for the consortium module", | ||
} | ||
|
||
cmd.AddCommand( | ||
GetParamsCmd(), | ||
GetValidatorAuthsCmd(), | ||
GetValidatorAuthCmd(), | ||
NewQueryCmdParams(), | ||
NewQueryCmdValidatorAuth(), | ||
NewQueryCmdValidatorAuths(), | ||
Comment on lines
-22
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename the functions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you change the name with prefix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's a matter of choice. You can use both of New and Get, then they would mean creating a new command instance and get a command instance, respectively. An example of New: An example of Get: It is interesting that they are both in the same module. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understood. Thank you. |
||
) | ||
|
||
return cmd | ||
} | ||
|
||
// GetParamsCmd returns the query consortium parameters command. | ||
func GetParamsCmd() *cobra.Command { | ||
// NewQueryCmdParams returns the query consortium parameters command. | ||
func NewQueryCmdParams() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "params", | ||
Short: "Query consortium params", | ||
|
@@ -56,27 +56,27 @@ func GetParamsCmd() *cobra.Command { | |
return cmd | ||
} | ||
|
||
// GetValidatorAuthsCmd returns validator authorization by consortium | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. reposition the two functions |
||
func GetValidatorAuthsCmd() *cobra.Command { | ||
// NewQueryCmdValidatorAuth returns validator authorization by consortium | ||
func NewQueryCmdValidatorAuth() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "validator-auths", | ||
Short: "Query validator authorizations", | ||
Long: "Gets validator authorizations by consortium", | ||
Args: cobra.NoArgs, | ||
Use: "validator-auth [validator-address]", | ||
Short: "Query validator authorization", | ||
Long: "Gets validator authorization by consortium", | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientQueryContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
queryClient := types.NewQueryClient(clientCtx) | ||
|
||
pageReq, err := client.ReadPageRequest(cmd.Flags()) | ||
if err != nil { | ||
valAddr := args[0] | ||
if err = sdk.ValidateValAddress(valAddr); err != nil { | ||
return err | ||
} | ||
|
||
params := types.QueryValidatorAuthsRequest{Pagination: pageReq} | ||
res, err := queryClient.ValidatorAuths(context.Background(), ¶ms) | ||
params := types.QueryValidatorAuthRequest{ValidatorAddress: valAddr} | ||
res, err := queryClient.ValidatorAuth(context.Background(), ¶ms) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -86,32 +86,31 @@ func GetValidatorAuthsCmd() *cobra.Command { | |
} | ||
|
||
flags.AddQueryFlagsToCmd(cmd) | ||
flags.AddPaginationFlagsToCmd(cmd, "validator auths") | ||
|
||
return cmd | ||
} | ||
|
||
// GetValidatorAuthCmd returns validator authorizations by consortium | ||
func GetValidatorAuthCmd() *cobra.Command { | ||
// NewQueryCmdValidatorAuths returns validator authorizations by consortium | ||
func NewQueryCmdValidatorAuths() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "validator-auth [validator-address]", | ||
Short: "Query validator authorization", | ||
Long: "Gets validator authorization by consortium", | ||
Args: cobra.ExactArgs(1), | ||
Use: "validator-auths", | ||
Short: "Query validator authorizations", | ||
Long: "Gets validator authorizations by consortium", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientQueryContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
queryClient := types.NewQueryClient(clientCtx) | ||
|
||
valAddr := args[0] | ||
if err = sdk.ValidateValAddress(valAddr); err != nil { | ||
pageReq, err := client.ReadPageRequest(cmd.Flags()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
params := types.QueryValidatorAuthRequest{ValidatorAddress: valAddr} | ||
res, err := queryClient.ValidatorAuth(context.Background(), ¶ms) | ||
params := types.QueryValidatorAuthsRequest{Pagination: pageReq} | ||
res, err := queryClient.ValidatorAuths(context.Background(), ¶ms) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -121,6 +120,7 @@ func GetValidatorAuthCmd() *cobra.Command { | |
} | ||
|
||
flags.AddQueryFlagsToCmd(cmd) | ||
flags.AddPaginationFlagsToCmd(cmd, "validator auths") | ||
|
||
return cmd | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,6 @@ import ( | |
"github.com/line/lbm-sdk/client" | ||
"github.com/line/lbm-sdk/client/tx" | ||
sdk "github.com/line/lbm-sdk/types" | ||
sdkerrors "github.com/line/lbm-sdk/types/errors" | ||
"github.com/line/lbm-sdk/version" | ||
"github.com/line/lbm-sdk/x/consortium/types" | ||
"github.com/line/lbm-sdk/x/gov/client/cli" | ||
|
@@ -22,8 +21,8 @@ const ( | |
FlagAllowedValidatorDelete = "delete" | ||
) | ||
|
||
// NewCmdSubmitUpdateConsortiumParamsProposal implements the command to submit an update-consortium-params proposal | ||
func NewCmdSubmitUpdateConsortiumParamsProposal() *cobra.Command { | ||
// NewProposalCmdUpdateConsortiumParams implements the command to submit an update-consortium-params proposal | ||
func NewProposalCmdUpdateConsortiumParams() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "update-consortium-params", | ||
Args: cobra.NoArgs, | ||
|
@@ -69,7 +68,6 @@ $ %s tx gov submit-proposal update-consortium-params [flags] | |
Enabled: false, | ||
} | ||
content := types.NewUpdateConsortiumParamsProposal(title, description, params) | ||
|
||
msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from) | ||
if err != nil { | ||
return err | ||
|
@@ -90,8 +88,8 @@ $ %s tx gov submit-proposal update-consortium-params [flags] | |
return cmd | ||
} | ||
|
||
// NewCmdSubmitUpdateValidatorAuthsProposal implements the command to submit an update-validator-auths proposal | ||
func NewCmdSubmitUpdateValidatorAuthsProposal() *cobra.Command { | ||
// NewProposalCmdUpdateValidatorAuths implements the command to submit an update-validator-auths proposal | ||
func NewProposalCmdUpdateValidatorAuths() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "update-validator-auths", | ||
Args: cobra.NoArgs, | ||
|
@@ -151,18 +149,8 @@ $ %s tx gov submit-proposal update-validator-auths [flags] | |
} | ||
deletingValidators := parseCommaSeparated(deletingValidatorsStr) | ||
|
||
createAuths := func(addings, deletings []string) ([]*types.ValidatorAuth, error) { | ||
createAuths := func(addings, deletings []string) []*types.ValidatorAuth { | ||
var auths []*types.ValidatorAuth | ||
|
||
// check duplications | ||
usedAddrs := map[string]bool{} | ||
for _, addr := range append(addings, deletings...) { | ||
if usedAddrs[addr] { | ||
return auths, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "multiple auths for same validator: %s", addr) | ||
} | ||
usedAddrs[addr] = true | ||
} | ||
|
||
for _, addr := range addings { | ||
Comment on lines
-156
to
154
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the validation logic because we call |
||
auth := &types.ValidatorAuth{ | ||
OperatorAddress: addr, | ||
|
@@ -178,15 +166,11 @@ $ %s tx gov submit-proposal update-validator-auths [flags] | |
auths = append(auths, auth) | ||
} | ||
|
||
return auths, nil | ||
return auths | ||
} | ||
|
||
auths, err := createAuths(addingValidators, deletingValidators) | ||
if err != nil { | ||
return err | ||
} | ||
auths := createAuths(addingValidators, deletingValidators) | ||
content := types.NewUpdateValidatorAuthsProposal(title, description, auths) | ||
|
||
msg, err := govtypes.NewMsgSubmitProposal(content, deposit, from) | ||
if err != nil { | ||
return err | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// +build norace | ||
|
||
package testutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
"github.com/line/lbm-sdk/testutil/network" | ||
) | ||
|
||
func TestIntegrationTestSuite(t *testing.T) { | ||
cfg := network.DefaultConfig() | ||
cfg.NumValidators = 1 | ||
suite.Run(t, NewIntegrationTestSuite(cfg)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reposition the two functions