Skip to content
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

add basic registered query validation in genesis #230

Merged
merged 7 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
238 changes: 238 additions & 0 deletions x/interchainqueries/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,241 @@ func TestGenesisNullQueries(t *testing.T) {

require.ElementsMatch(t, genesisState.RegisteredQueries, got.RegisteredQueries)
}

func TestGenesisFilledQueries(t *testing.T) {
genesisState := types.GenesisState{
Params: types.DefaultParams(),
RegisteredQueries: []*types.RegisteredQuery{
{
Id: 4,
QueryType: "kv",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
Keys: []*types.KVKey{
{
Path: "newpath",
Key: []byte("newdata"),
},
},
},
{
Id: 3,
QueryType: "kv",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
Keys: []*types.KVKey{
{
Path: "newpath",
Key: []byte("newdata"),
},
},
},
{
Id: 2,
QueryType: "tx",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
TransactionsFilter: `[{"field":"tx.height","op":"Eq","value":1000}]`,
},
{
Id: 1,
QueryType: "tx",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
TransactionsFilter: `[{"field":"tx.height","op":"Eq","value":1000}]`,
},
},
}

k, ctx := keepertest.InterchainQueriesKeeper(t, nil, nil, nil, nil)
interchainqueries.InitGenesis(ctx, *k, genesisState)
got := interchainqueries.ExportGenesis(ctx, *k)
err := got.Validate()
require.NoError(t, err)

require.ElementsMatch(t, genesisState.RegisteredQueries, got.RegisteredQueries)
}

func TestGenesisMalformedQueriesInvalidTxFilter(t *testing.T) {
genesisState := types.GenesisState{
Params: types.DefaultParams(),
RegisteredQueries: []*types.RegisteredQuery{
{
Id: 4,
QueryType: "kv",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
Keys: []*types.KVKey{
{
Path: "newpath",
Key: []byte("newdata"),
},
},
},
{
Id: 3,
QueryType: "kv",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
Keys: []*types.KVKey{
{
Path: "newpath",
Key: []byte("newdata"),
},
},
},
{
Id: 2,
QueryType: "tx",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
TransactionsFilter: `[{"field":"tx.height","op":"Eq","value":1000}]`,
},
{
Id: 1,
QueryType: "tx",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
TransactionsFilter: `[{"fi><eld":"tx.height","op":"Eq","value":1000}]`,
},
},
}

k, ctx := keepertest.InterchainQueriesKeeper(t, nil, nil, nil, nil)
interchainqueries.InitGenesis(ctx, *k, genesisState)
got := interchainqueries.ExportGenesis(ctx, *k)
err := got.Validate()
require.ErrorContains(t, err, "invalid transactions filter")

require.ElementsMatch(t, genesisState.RegisteredQueries, got.RegisteredQueries)
}

func TestGenesisMalformedQueriesNoKvKeys(t *testing.T) {
genesisState := types.GenesisState{
Params: types.DefaultParams(),
RegisteredQueries: []*types.RegisteredQuery{
{
Id: 4,
QueryType: "kv",
Owner: "neutron18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
},
{
Id: 3,
QueryType: "kv",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
Keys: []*types.KVKey{
{
Path: "newpath",
Key: []byte("newdata"),
},
},
},
{
Id: 2,
QueryType: "tx",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
TransactionsFilter: `[{"field":"tx.height","op":"Eq","value":1000}]`,
},
{
Id: 1,
QueryType: "tx",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
TransactionsFilter: `[{"field":"tx.height","op":"Eq","value":1000}]`,
},
},
}

k, ctx := keepertest.InterchainQueriesKeeper(t, nil, nil, nil, nil)
interchainqueries.InitGenesis(ctx, *k, genesisState)
got := interchainqueries.ExportGenesis(ctx, *k)
err := got.Validate()
require.ErrorContains(t, err, "Invalid owner address")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why Invalid owner address if the test's name is TestGenesisMalformedQueriesNoKvKeys?


require.ElementsMatch(t, genesisState.RegisteredQueries, got.RegisteredQueries)
}

func TestGenesisMalformedQueriesInvalidQueryType(t *testing.T) {
genesisState := types.GenesisState{
Params: types.DefaultParams(),
RegisteredQueries: []*types.RegisteredQuery{
{
Id: 4,
QueryType: "fake",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
},
{
Id: 3,
QueryType: "kv",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
Keys: []*types.KVKey{
{
Path: "newpath",
Key: []byte("newdata"),
},
},
},
{
Id: 2,
QueryType: "tx",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
TransactionsFilter: `[{"field":"tx.height","op":"Eq","value":1000}]`,
},
{
Id: 1,
QueryType: "tx",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
TransactionsFilter: `[{"field":"tx.height","op":"Eq","value":1000}]`,
},
},
}

k, ctx := keepertest.InterchainQueriesKeeper(t, nil, nil, nil, nil)
interchainqueries.InitGenesis(ctx, *k, genesisState)
got := interchainqueries.ExportGenesis(ctx, *k)
err := got.Validate()
require.ErrorContains(t, err, "Unexpected query type")

require.ElementsMatch(t, genesisState.RegisteredQueries, got.RegisteredQueries)
}

func TestGenesisMalformedQueriesInvalidPrefix(t *testing.T) {
genesisState := types.GenesisState{
Params: types.DefaultParams(),
RegisteredQueries: []*types.RegisteredQuery{
{
Id: 4,
QueryType: "kv",
Owner: "neutron18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
Keys: []*types.KVKey{
{
Path: "newpath",
Key: []byte("newdata"),
},
},
},
{
Id: 3,
QueryType: "kv",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
Keys: []*types.KVKey{
{
Path: "newpath",
Key: []byte("newdata"),
},
},
},
{
Id: 2,
QueryType: "tx",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
TransactionsFilter: `[{"field":"tx.height","op":"Eq","value":1000}]`,
},
{
Id: 1,
QueryType: "tx",
Owner: "cosmos18g0avxazu3dkgd5n5ea8h8rtl78de0hytsj9vm",
TransactionsFilter: `[{"field":"tx.height","op":"Eq","value":1000}]`,
},
},
}

k, ctx := keepertest.InterchainQueriesKeeper(t, nil, nil, nil, nil)
interchainqueries.InitGenesis(ctx, *k, genesisState)
got := interchainqueries.ExportGenesis(ctx, *k)
err := got.Validate()
require.ErrorContains(t, err, "Invalid owner address")

require.ElementsMatch(t, genesisState.RegisteredQueries, got.RegisteredQueries)
}
43 changes: 22 additions & 21 deletions x/interchainqueries/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,26 @@ import (

// x/interchainqueries module sentinel errors
var (
ErrInvalidQueryID = sdkerrors.Register(ModuleName, 1100, "invalid query id")
ErrEmptyResult = sdkerrors.Register(ModuleName, 1101, "empty result")
ErrInvalidClientID = sdkerrors.Register(ModuleName, 1102, "invalid client id")
ErrInvalidUpdatePeriod = sdkerrors.Register(ModuleName, 1103, "invalid update period")
ErrInvalidConnectionID = sdkerrors.Register(ModuleName, 1104, "invalid connection id")
ErrInvalidQueryType = sdkerrors.Register(ModuleName, 1105, "invalid query type")
ErrInvalidTransactionsFilter = sdkerrors.Register(ModuleName, 1106, "invalid transactions filter")
ErrInvalidSubmittedResult = sdkerrors.Register(ModuleName, 1107, "invalid result")
ErrProtoMarshal = sdkerrors.Register(ModuleName, 1108, "failed to marshal protobuf bytes")
ErrProtoUnmarshal = sdkerrors.Register(ModuleName, 1109, "failed to unmarshal protobuf bytes")
ErrInvalidType = sdkerrors.Register(ModuleName, 1110, "invalid type")
ErrInternal = sdkerrors.Register(ModuleName, 1111, "internal error")
ErrInvalidProof = sdkerrors.Register(ModuleName, 1112, "merkle proof is invalid")
ErrInvalidHeader = sdkerrors.Register(ModuleName, 1113, "header is invalid")
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")
ErrInvalidQueryID = sdkerrors.Register(ModuleName, 1100, "invalid query id")
ErrEmptyResult = sdkerrors.Register(ModuleName, 1101, "empty result")
ErrInvalidClientID = sdkerrors.Register(ModuleName, 1102, "invalid client id")
ErrInvalidUpdatePeriod = sdkerrors.Register(ModuleName, 1103, "invalid update period")
ErrInvalidConnectionID = sdkerrors.Register(ModuleName, 1104, "invalid connection id")
ErrInvalidQueryType = sdkerrors.Register(ModuleName, 1105, "invalid query type")
ErrInvalidTransactionsFilter = sdkerrors.Register(ModuleName, 1106, "invalid transactions filter")
ErrInvalidSubmittedResult = sdkerrors.Register(ModuleName, 1107, "invalid result")
ErrProtoMarshal = sdkerrors.Register(ModuleName, 1108, "failed to marshal protobuf bytes")
ErrProtoUnmarshal = sdkerrors.Register(ModuleName, 1109, "failed to unmarshal protobuf bytes")
ErrInvalidType = sdkerrors.Register(ModuleName, 1110, "invalid type")
ErrInternal = sdkerrors.Register(ModuleName, 1111, "internal error")
ErrInvalidProof = sdkerrors.Register(ModuleName, 1112, "merkle proof is invalid")
ErrInvalidHeader = sdkerrors.Register(ModuleName, 1113, "header is invalid")
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")
ErrUnexpectedQueryTypeGenesis = sdkerrors.Register(ModuleName, 1121, "unexpected query type")
)
40 changes: 39 additions & 1 deletion x/interchainqueries/types/genesis.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// DefaultGenesis returns the default Capability genesis state
func DefaultGenesis() *GenesisState {
return &GenesisState{
Expand All @@ -10,5 +15,38 @@ func DefaultGenesis() *GenesisState {
// Validate performs basic genesis state validation returning an error upon any
// failure.
func (gs GenesisState) Validate() error {
return gs.Params.Validate()
err := gs.Params.Validate()
if err != nil {
return err
}
seenIDs := map[uint64]bool{}
foxpy marked this conversation as resolved.
Show resolved Hide resolved

for _, val := range gs.GetRegisteredQueries() {
if seenIDs[val.Id] {
return sdkerrors.Wrapf(ErrInvalidQueryID, "duplicate query id: %d", val.Id)
}
seenIDs[val.Id] = true

_, err = sdk.AccAddressFromBech32(val.Owner)
if err != nil {
return sdkerrors.Wrapf(err, "Invalid owner address (%s)", err)
}

switch val.QueryType {
case string(InterchainQueryTypeTX):
if err := ValidateTransactionsFilter(val.TransactionsFilter); err != nil {
return sdkerrors.Wrap(ErrInvalidTransactionsFilter, err.Error())
}
case string(InterchainQueryTypeKV):
if len(val.Keys) == 0 {
return sdkerrors.Wrap(ErrEmptyKeys, "keys cannot be empty")
}
if err := validateKeys(val.GetKeys()); err != nil {
return err
}
default:
return sdkerrors.Wrapf(ErrUnexpectedQueryTypeGenesis, "Unexpected query type: %s", val.QueryType)
}
}
return nil
}