Skip to content

Commit

Permalink
Merge pull request #78 from neutron-org/feat/kv-query-keys-validation
Browse files Browse the repository at this point in the history
feat: kv query keys validation
  • Loading branch information
pr0n00gler authored Oct 21, 2022
2 parents 45b8fb3 + 0cb9228 commit 750ec1b
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 1 deletion.
4 changes: 4 additions & 0 deletions x/interchainqueries/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ var (
ErrInvalidHeight = sdkerrors.Register(ModuleName, 1114, "height is invalid")
ErrNoQueryResult = sdkerrors.Register(ModuleName, 1115, "no query result")
ErrNotContract = sdkerrors.Register(ModuleName, 1116, "not a contract")
ErrEmptyKeys = sdkerrors.Register(ModuleName, 1117, "keys are empty")
ErrEmptyKeyPath = sdkerrors.Register(ModuleName, 1118, "key path is empty")
ErrEmptyKeyID = sdkerrors.Register(ModuleName, 1119, "key id is empty")
ErrTooManyKVQueryKeys = sdkerrors.Register(ModuleName, 1120, "too many keys")
)
32 changes: 32 additions & 0 deletions x/interchainqueries/types/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/cosmos/ibc-go/v3/modules/core/exported"
)

const (
MaxKVQueryKeysCount = 32
)

var _ codectypes.UnpackInterfacesMessage = MsgSubmitQueryResult{}

func (msg MsgSubmitQueryResult) Route() string {
Expand Down Expand Up @@ -88,6 +92,15 @@ func (msg MsgRegisterInterchainQuery) ValidateBasic() error {
return sdkerrors.Wrap(ErrInvalidQueryType, "invalid query type")
}

if InterchainQueryType(msg.QueryType).IsKV() {
if len(msg.Keys) == 0 {
return sdkerrors.Wrap(ErrEmptyKeys, "keys cannot be empty")
}
if err := validateKeys(msg.GetKeys()); err != nil {
return err
}
}

if InterchainQueryType(msg.QueryType).IsTX() {
if err := ValidateTransactionsFilter(msg.TransactionsFilter); err != nil {
return sdkerrors.Wrap(ErrInvalidTransactionsFilter, err.Error())
Expand Down Expand Up @@ -159,6 +172,10 @@ func (msg MsgUpdateInterchainQueryRequest) ValidateBasic() error {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "one of new_keys or new_update_period should be set")
}

if err := validateKeys(msg.GetNewKeys()); err != nil {
return err
}

if strings.TrimSpace(msg.Sender) == "" {
return sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "missing sender address")
}
Expand All @@ -180,3 +197,18 @@ func (msg MsgUpdateInterchainQueryRequest) GetSigners() []sdk.AccAddress {
}
return []sdk.AccAddress{senderAddr}
}

func validateKeys(keys []*KVKey) error {
if uint64(len(keys)) > MaxKVQueryKeysCount {
return sdkerrors.Wrapf(ErrTooManyKVQueryKeys, "keys count cannot be more than %d", MaxKVQueryKeysCount)
}
for _, key := range keys {
if len(key.Path) == 0 {
return sdkerrors.Wrap(ErrEmptyKeyPath, "keys path cannot be empty")
}
if len(key.Key) == 0 {
return sdkerrors.Wrap(ErrEmptyKeyID, "keys id cannot be empty")
}
}
return nil
}
108 changes: 107 additions & 1 deletion x/interchainqueries/types/tx_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types_test

import (
"fmt"
"strconv"
"testing"

sdktypes "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -48,6 +50,20 @@ func TestMsgRegisterInterchainQueryValidate(t *testing.T) {
},
iqtypes.ErrInvalidTransactionsFilter,
},
{
"too many keys",
func() sdktypes.Msg {
return &iqtypes.MsgRegisterInterchainQuery{
ConnectionId: "connection-0",
TransactionsFilter: "[]",
Keys: craftKVKeys(200),
QueryType: string(iqtypes.InterchainQueryTypeKV),
UpdatePeriod: 1,
Sender: TestAddress,
}
},
iqtypes.ErrTooManyKVQueryKeys,
},
{
"invalid update period",
func() sdktypes.Msg {
Expand Down Expand Up @@ -105,7 +121,7 @@ func TestMsgRegisterInterchainQueryValidate(t *testing.T) {
iqtypes.ErrInvalidConnectionID,
},
{
"valid",
"empty keys",
func() sdktypes.Msg {
return &iqtypes.MsgRegisterInterchainQuery{
ConnectionId: "connection-0",
Expand All @@ -116,6 +132,48 @@ func TestMsgRegisterInterchainQueryValidate(t *testing.T) {
Sender: TestAddress,
}
},
iqtypes.ErrEmptyKeys,
},
{
"empty key path",
func() sdktypes.Msg {
return &iqtypes.MsgRegisterInterchainQuery{
ConnectionId: "connection-0",
TransactionsFilter: "{}",
Keys: []*iqtypes.KVKey{{Key: []byte("key1"), Path: ""}},
QueryType: string(iqtypes.InterchainQueryTypeKV),
UpdatePeriod: 1,
Sender: TestAddress,
}
},
iqtypes.ErrEmptyKeyPath,
},
{
"empty key id",
func() sdktypes.Msg {
return &iqtypes.MsgRegisterInterchainQuery{
ConnectionId: "connection-0",
TransactionsFilter: "{}",
Keys: []*iqtypes.KVKey{{Key: []byte(""), Path: "path"}},
QueryType: string(iqtypes.InterchainQueryTypeKV),
UpdatePeriod: 1,
Sender: TestAddress,
}
},
iqtypes.ErrEmptyKeyID,
},
{
"valid",
func() sdktypes.Msg {
return &iqtypes.MsgRegisterInterchainQuery{
ConnectionId: "connection-0",
TransactionsFilter: "{}",
Keys: []*iqtypes.KVKey{{Key: []byte("key1"), Path: "path1"}},
QueryType: string(iqtypes.InterchainQueryTypeKV),
UpdatePeriod: 1,
Sender: TestAddress,
}
},
nil,
},
}
Expand Down Expand Up @@ -345,6 +403,42 @@ func TestMsgUpdateQueryRequestValidate(t *testing.T) {
},
sdkerrors.ErrInvalidRequest,
},
{
"empty key path",
func() sdktypes.Msg {
return &iqtypes.MsgUpdateInterchainQueryRequest{
QueryId: 1,
NewKeys: []*iqtypes.KVKey{{Key: []byte("key1"), Path: ""}},
NewUpdatePeriod: 0,
Sender: TestAddress,
}
},
iqtypes.ErrEmptyKeyPath,
},
{
"empty key id",
func() sdktypes.Msg {
return &iqtypes.MsgUpdateInterchainQueryRequest{
QueryId: 1,
NewKeys: []*iqtypes.KVKey{{Key: []byte(""), Path: "path"}},
NewUpdatePeriod: 0,
Sender: TestAddress,
}
},
iqtypes.ErrEmptyKeyID,
},
{
"too many keys",
func() sdktypes.Msg {
return &iqtypes.MsgUpdateInterchainQueryRequest{
QueryId: 1,
NewKeys: craftKVKeys(200),
NewUpdatePeriod: 0,
Sender: TestAddress,
}
},
iqtypes.ErrTooManyKVQueryKeys,
},
{
"invalid query id",
func() sdktypes.Msg {
Expand Down Expand Up @@ -573,3 +667,15 @@ func TestMsgRemoveQueryGetSigners(t *testing.T) {
require.Equal(t, msg.GetSigners(), []sdktypes.AccAddress{addr})
}
}

func craftKVKeys(n uint64) []*iqtypes.KVKey {
keys := make([]*iqtypes.KVKey, n)
for i := uint64(0); i < n; i++ {
keys[i] = &iqtypes.KVKey{
Path: "path-" + strconv.FormatUint(i, 10),
Key: []byte(fmt.Sprintf("key-%d", i)),
}
}

return keys
}

0 comments on commit 750ec1b

Please sign in to comment.