Skip to content

Commit

Permalink
Merge pull request #146 from stratosnet/upgradecosmos4503
Browse files Browse the repository at this point in the history
Upgradecosmos4503
  • Loading branch information
hong-pang authored Jun 7, 2022
2 parents af80bcc + 0f99a85 commit e0cd251
Show file tree
Hide file tree
Showing 7 changed files with 422 additions and 281 deletions.
10 changes: 6 additions & 4 deletions proto/stratos/register/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ message QueryStakeByNodeResponse {
// QueryStakeByOwnerRequest is request type for the Query/StakeByOwner RPC method
message QueryStakeByOwnerRequest {
// owner_addr defines the owner address to query for.
string network_addr = 1;
string moniker = 2;
string owner_addr = 3;
// string network_addr = 1;
// string moniker = 2;
string owner_addr = 1;
// int64 page = 2;
// int64 limit = 3;
// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 4;
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

// QueryStakeByOwnerResponse is response type for the Query/StakeByOwner RPC method
Expand Down
10 changes: 6 additions & 4 deletions x/register/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ func nodeStakingByNodeAddressFn(cliCtx client.Context, queryPath string) http.Ha
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
if rest.CheckInternalServerError(w, err) {
return
Expand All @@ -174,8 +173,8 @@ func nodeStakingByNodeAddressFn(cliCtx client.Context, queryPath string) http.Ha
func nodeStakingByOwnerFn(cliCtx client.Context, queryPath string) http.HandlerFunc {

return func(w http.ResponseWriter, r *http.Request) {
nodeWalletAddressStr := mux.Vars(r)["ownerAddress"]
nodeWalletAddress, ok := keeper.CheckAccAddr(w, r, nodeWalletAddressStr)
ownerAddressStr := mux.Vars(r)["ownerAddress"]
ownerAddress, ok := keeper.CheckAccAddr(w, r, ownerAddressStr)
if !ok {
return
}
Expand All @@ -190,7 +189,7 @@ func nodeStakingByOwnerFn(cliCtx client.Context, queryPath string) http.HandlerF
return
}

params := types.NewQueryNodesParams(page, limit, nil, "", nodeWalletAddress)
params := types.NewQueryNodesParams(page, limit, nil, "", ownerAddress)
bz, err := cliCtx.LegacyAmino.MarshalJSON(params)
if rest.CheckBadRequestError(w, err) {
return
Expand All @@ -203,6 +202,9 @@ func nodeStakingByOwnerFn(cliCtx client.Context, queryPath string) http.HandlerF
}

cliCtx = cliCtx.WithHeight(height)
if rest.CheckInternalServerError(w, err) {
return
}
rest.PostProcessResponse(w, cliCtx, res)
}
}
127 changes: 63 additions & 64 deletions x/register/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package keeper

import (
"context"
"strconv"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
stratos "github.com/stratosnet/stratos-chain/types"
"github.com/stratosnet/stratos-chain/x/register/types"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -156,84 +156,83 @@ func (q Querier) StakeByOwner(c context.Context, req *types.QueryStakeByOwnerReq
}
ctx := sdk.UnwrapSDKContext(c)

var (
params types.QueryNodesParams
stakingInfo types.StakingInfo
stakingInfos []*types.StakingInfo
)

networkAddr, er := stratos.SdsAddressFromBech32(req.GetNetworkAddr())
if er != nil {
return &types.QueryStakeByOwnerResponse{}, er
}

ownerAddr, er := sdk.AccAddressFromBech32(req.GetOwnerAddr())
if er != nil {
return &types.QueryStakeByOwnerResponse{}, er
}

page, er := strconv.Atoi(string(req.Pagination.Key))
if er != nil {
return &types.QueryStakeByOwnerResponse{}, er
}
store := ctx.KVStore(q.storeKey)
var stakingInfoResponses []*types.StakingInfo

// get resource nodes
var resourceNodes types.ResourceNodes
resourceNodeStore := prefix.NewStore(store, types.ResourceNodeKey)

params = types.NewQueryNodesParams(page, int(req.Pagination.Limit), networkAddr, req.GetMoniker(), ownerAddr)

resNodes := q.GetResourceNodesFiltered(ctx, params)
indNodes := q.GetMetaNodesFiltered(ctx, params)

for _, n := range indNodes {
networkAddr, _ := stratos.SdsAddressFromBech32(n.GetNetworkAddress())
unBondingStake, unBondedStake, bondedStake, er := q.getNodeStakes(
ctx,
n.GetStatus(),
networkAddr,
n.Tokens,
)
if er != nil {
return nil, er
resourceNodesPageRes, err := FilteredPaginate(q.cdc, resourceNodeStore, ownerAddr, req.Pagination, func(key []byte, value []byte, accumulate bool) (bool, error) {
val, err := types.UnmarshalResourceNode(q.cdc, value)
if err != nil {
return true, err
}
if !n.Equal(types.MetaNode{}) {
stakingInfo = types.NewStakingInfoByMetaNodeAddr(
n,
unBondingStake,
unBondedStake,
bondedStake,
)
stakingInfos = append(stakingInfos, &stakingInfo)

if accumulate {
resourceNodes = append(resourceNodes, val)
}

return true, nil
})

if err != nil {
return &types.QueryStakeByOwnerResponse{}, status.Error(codes.Internal, err.Error())
}
stakingInfoResponses, err = StakingInfosToStakingResourceNodes(ctx, q.Keeper, resourceNodes)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

// Continue to get meta nodes
if req.Pagination.Limit < resourceNodesPageRes.Total {
resourceNodesPageRes.Total = uint64(len(stakingInfoResponses))
return &types.QueryStakeByOwnerResponse{StakingInfos: stakingInfoResponses, Pagination: resourceNodesPageRes}, nil

}

metaNodesPageLimit := req.Pagination.Limit - resourceNodesPageRes.Total

metaNodesPageOffset := uint64(0)
if req.Pagination.Offset > resourceNodesPageRes.Total {
metaNodesPageOffset = req.Pagination.Offset - resourceNodesPageRes.Total
}
metaNodesPageRequest := query.PageRequest{Offset: metaNodesPageOffset, Limit: metaNodesPageLimit, CountTotal: req.Pagination.CountTotal}

for _, n := range resNodes {
networkAddr, _ := stratos.SdsAddressFromBech32(n.GetNetworkAddress())
unBondingStake, unBondedStake, bondedStake, er := q.getNodeStakes(
ctx,
n.GetStatus(),
networkAddr,
n.Tokens,
)
if er != nil {
return nil, er
var metaNodes types.MetaNodes
metaNodeStore := prefix.NewStore(store, types.MetaNodeKey)

_, err = FilteredPaginate(q.cdc, metaNodeStore, ownerAddr, &metaNodesPageRequest, func(key []byte, value []byte, accumulate bool) (bool, error) {
val, err := types.UnmarshalMetaNode(q.cdc, value)
if err != nil {
return true, err
}
if !n.Equal(types.ResourceNode{}) {
stakingInfo = types.NewStakingInfoByResourceNodeAddr(
n,
unBondingStake,
unBondedStake,
bondedStake,
)
stakingInfos = append(stakingInfos, &stakingInfo)

if accumulate {
metaNodes = append(metaNodes, val)
}

return true, nil
})

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

start, end := client.Paginate(len(stakingInfos), params.Page, params.Limit, QueryDefaultLimit)
if start < 0 || end < 0 {
return &types.QueryStakeByOwnerResponse{}, nil
} else {
stakingInfos = stakingInfos[start:end]
return &types.QueryStakeByOwnerResponse{StakingInfos: stakingInfos}, nil
metaNodesStakingInfoResponses, err := StakingInfosToStakingMetaNodes(ctx, q.Keeper, metaNodes)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}

stakingInfoResponses = append(stakingInfoResponses, metaNodesStakingInfoResponses...)
PageRes := resourceNodesPageRes
PageRes.Total = uint64(len(stakingInfoResponses))
return &types.QueryStakeByOwnerResponse{StakingInfos: stakingInfoResponses, Pagination: PageRes}, nil
}

func (q Querier) StakeTotal(c context.Context, _ *types.QueryTotalStakeRequest) (*types.QueryTotalStakeResponse, error) {
Expand Down
File renamed without changes.
Loading

0 comments on commit e0cd251

Please sign in to comment.