Skip to content

Commit

Permalink
feat: reverse search links (#793)
Browse files Browse the repository at this point in the history
## Description
This PR adds the ability to reverse search for app links and chain links owners. The new following queries have been added: 
- search for app link owners using an application name (optional), and username (optional, works only if the application is specified as well)
- search for chain link owners using a chain name (optional), and target address (optional, works only if the chain name is specified as well)  

Closes: #777
Closes: #794

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://docs.cosmos.network/v0.44/building-modules/intro.html)
- [ ] included the necessary unit and integration [tests](https://github.com/desmos-labs/desmos/blob/master/CONTRIBUTING.md#testing)
- [x] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
RiccardoM authored Mar 28, 2022
1 parent 3039589 commit acdecc9
Show file tree
Hide file tree
Showing 24 changed files with 4,167 additions and 550 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: feat
module: x/profiles
pull_request: 793
description: Added the ability to reverse search application links and chain links
backward_compatible: true
date: 2022-03-23T14:27:24.394374729Z
1,476 changes: 1,127 additions & 349 deletions client/docs/swagger-ui/swagger.yaml

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions proto/desmos/profiles/v2/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ service Query {
option (google.api.http).get = "/desmos/profiles/v2/chain-links";
}

// ChainLinkOwners queries for the owners of chain links, optionally searching
// for a specific chain name and external address
rpc ChainLinkOwners(QueryChainLinkOwnersRequest)
returns (QueryChainLinkOwnersResponse) {
option (google.api.http).get = "/desmos/profiles/v2/chain-links/owners";
}

// ApplicationLinks queries the applications links associated to the given
// user, if provided. Otherwise, it queries all the application links stored.
rpc ApplicationLinks(QueryApplicationLinksRequest)
Expand All @@ -51,6 +58,13 @@ service Query {
"/desmos/profiles/v2/app-links/clients/{client_id}";
}

// ApplicationLinkOwners queries for the owners of applications links,
// optionally searching for a specific application and username.
rpc ApplicationLinkOwners(QueryApplicationLinkOwnersRequest)
returns (QueryApplicationLinkOwnersResponse) {
option (google.api.http).get = "/desmos/profiles/v2/app-links/owners";
}

// Params queries the profiles module params
rpc Params(QueryParamsRequest) returns (QueryParamsResponse) {
option (google.api.http).get = "/desmos/profiles/v2/params";
Expand Down
34 changes: 34 additions & 0 deletions proto/desmos/profiles/v2/query_app_links.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,38 @@ message QueryApplicationLinkByClientIDRequest {
// request allowing to get an application link using a client id
message QueryApplicationLinkByClientIDResponse {
ApplicationLink link = 1 [ (gogoproto.nullable) = false ];
}

// QueryApplicationLinkOwnersRequest contains the data of the request that can
// be used to get application link owners
message QueryApplicationLinkOwnersRequest {
// (Optional) Application name to search link owners of. If not specified, all
// links stored will be searched instead.
string application = 1;

// (Optional) Username to search for. This will only be used if the
// application is specified as well
string username = 2;

// Pagination defines an optional pagination for the request
cosmos.base.query.v1beta1.PageRequest pagination = 3;
}

// QueryApplicationLinkOwnersResponse contains the data returned by the request
// allowing to get application link owners.
message QueryApplicationLinkOwnersResponse {
// ApplicationLinkOwnerDetails contains the details of a single application
// link owner
message ApplicationLinkOwnerDetails {
string user = 1;
string application = 2;
string username = 3;
}

// Addresses of the application links owners
repeated ApplicationLinkOwnerDetails owners = 1
[ (gogoproto.nullable) = false ];

// Pagination defines the pagination response
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
32 changes: 32 additions & 0 deletions proto/desmos/profiles/v2/query_chain_links.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,35 @@ message QueryChainLinksResponse {
// Pagination defines the pagination response
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QueryChainLinkOwnersRequest contains the data of the request that can
// be used to get chain link owners
message QueryChainLinkOwnersRequest {
// (Optional) Chain name to search link owners of. If not specified, all
// links stored will be searched instead.
string chain_name = 1;

// (Optional) External address to search for. This will only be used if the
// chain name is specified as well
string target = 2;

// Pagination defines an optional pagination for the request
cosmos.base.query.v1beta1.PageRequest pagination = 3;
}

// QueryChainLinkOwnersResponse contains the data returned by the request
// allowing to get chain link owners.
message QueryChainLinkOwnersResponse {
// ChainLinkOwnerDetails contains the details of a single chain link owner
message ChainLinkOwnerDetails {
string user = 1;
string chain_name = 2;
string target = 3;
}

// Addresses of the chain links owners
repeated ChainLinkOwnerDetails owners = 1 [ (gogoproto.nullable) = false ];

// Pagination defines the pagination response
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
11 changes: 11 additions & 0 deletions testutil/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/codec"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -48,6 +50,15 @@ func PubKeyFromBech32(pubKey string) cryptotypes.PubKey {
return publicKey
}

func PubKeyFromJSON(cdc codec.Codec, pubKey string) cryptotypes.PubKey {
var publicKey cryptotypes.PubKey
err := cdc.UnmarshalInterfaceJSON([]byte(pubKey), &publicKey)
if err != nil {
panic(err)
}
return publicKey
}

func ProfileFromAddr(address string) *types.Profile {
profile, err := types.NewProfile(
fmt.Sprintf("%s-dtag", address),
Expand Down
72 changes: 72 additions & 0 deletions x/profiles/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,42 @@ func (k Keeper) ChainLinks(ctx context.Context, request *types.QueryChainLinksRe
return &types.QueryChainLinksResponse{Links: links, Pagination: pageRes}, nil
}

// ChainLinkOwners implements the Query/ChainLinkOwners gRPC method
func (k Keeper) ChainLinkOwners(ctx context.Context, request *types.QueryChainLinkOwnersRequest) (*types.QueryChainLinkOwnersResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(k.storeKey)

ownersPrefix := types.ChainLinkChainPrefix
switch {
case request.ChainName != "" && request.Target != "":
ownersPrefix = types.ChainLinkChainAddressKey(request.ChainName, request.Target)
case request.ChainName != "":
ownersPrefix = types.ChainLinkChainKey(request.ChainName)
}

var owners []types.QueryChainLinkOwnersResponse_ChainLinkOwnerDetails
ownersStore := prefix.NewStore(store, ownersPrefix)
pageRes, err := query.Paginate(ownersStore, request.Pagination, func(key []byte, value []byte) error {
// Re-add the prefix because the prefix store trims it out, and we need it to get the data
keyWithPrefix := append(ownersPrefix, key...)
chainName, target, user := types.GetChainLinkOwnerData(keyWithPrefix)

owners = append(owners, types.QueryChainLinkOwnersResponse_ChainLinkOwnerDetails{
User: user,
ChainName: chainName,
Target: target,
})

return nil
})

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryChainLinkOwnersResponse{Owners: owners, Pagination: pageRes}, nil
}

// ApplicationLinks implements the Query/ApplicationLinks gRPC method
func (k Keeper) ApplicationLinks(ctx context.Context, request *types.QueryApplicationLinksRequest) (*types.QueryApplicationLinksResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
Expand Down Expand Up @@ -174,6 +210,42 @@ func (k Keeper) ApplicationLinkByClientID(ctx context.Context, request *types.Qu
return &types.QueryApplicationLinkByClientIDResponse{Link: link}, nil
}

// ApplicationLinkOwners implements the Query/ApplicationLinkOwners gRPC method
func (k Keeper) ApplicationLinkOwners(ctx context.Context, request *types.QueryApplicationLinkOwnersRequest) (*types.QueryApplicationLinkOwnersResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
store := sdkCtx.KVStore(k.storeKey)

ownersPrefix := types.ApplicationLinkAppPrefix
switch {
case request.Application != "" && request.Username != "":
ownersPrefix = types.ApplicationLinkAppUsernameKey(request.Application, request.Username)
case request.Application != "":
ownersPrefix = types.ApplicationLinkAppKey(request.Application)
}

var owners []types.QueryApplicationLinkOwnersResponse_ApplicationLinkOwnerDetails
ownersStore := prefix.NewStore(store, ownersPrefix)
pageRes, err := query.Paginate(ownersStore, request.Pagination, func(key []byte, value []byte) error {
// Re-add the prefix because the prefix store trims it out, and we need it to get the data
keyWithPrefix := append(ownersPrefix, key...)
application, username, user := types.GetApplicationLinkOwnerData(keyWithPrefix)

owners = append(owners, types.QueryApplicationLinkOwnersResponse_ApplicationLinkOwnerDetails{
User: user,
Application: application,
Username: username,
})

return nil
})

if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

return &types.QueryApplicationLinkOwnersResponse{Owners: owners, Pagination: pageRes}, nil
}

// Params implements the Query/Params gRPC method
func (k Keeper) Params(ctx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
Expand Down
Loading

0 comments on commit acdecc9

Please sign in to comment.