Skip to content

Commit

Permalink
Revert "shield: add query providers and purchases (shentufoundation#106
Browse files Browse the repository at this point in the history
…)" (shentufoundation#107)

This reverts commit 28d4030.
  • Loading branch information
hacheigriega authored Oct 22, 2020
1 parent 28d4030 commit ca90c64
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 242 deletions.
11 changes: 9 additions & 2 deletions x/gov/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ func queryVotesOnProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {
return
}

params := govTypes.NewQueryProposalVotesParams(proposalID, page, limit)
params := govTypes.NewQueryProposalVotesParams(proposalID, page, limit+1)

bz, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
Expand Down Expand Up @@ -403,7 +403,14 @@ func queryVotesOnProposalHandlerFn(cliCtx context.CLIContext) http.HandlerFunc {

votesWithPower, w := getVotesWithPower(cliCtx, w, res)

rest.PostProcessResponse(w, cliCtx, votesWithPower)
nextPgExists := false
if len(votesWithPower) == limit+1 {
nextPgExists = true
votesWithPower = votesWithPower[:limit]
}

withNextPg := VotesWithPowerAndNextPage{nextPgExists, votesWithPower}
rest.PostProcessResponse(w, cliCtx, withNextPg)
}
}

Expand Down
74 changes: 1 addition & 73 deletions x/shield/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cli

import (
"fmt"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -11,7 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/version"

"github.com/certikfoundation/shentu/x/shield/types"
)
Expand All @@ -31,9 +29,7 @@ func GetQueryCmd(queryRoute string, cdc *codec.Codec) *cobra.Command {
GetCmdPurchaseList(queryRoute, cdc),
GetCmdPurchaserPurchases(queryRoute, cdc),
GetCmdPoolPurchases(queryRoute, cdc),
GetCmdPurchases(queryRoute, cdc),
GetCmdProvider(queryRoute, cdc),
GetCmdProviders(queryRoute, cdc),
GetCmdPoolParams(queryRoute, cdc),
GetCmdClaimParams(queryRoute, cdc),
)...)
Expand Down Expand Up @@ -158,7 +154,7 @@ func GetCmdPurchaserPurchases(queryRoute string, cdc *codec.Codec) *cobra.Comman
// purchases in a given pool.
func GetCmdPoolPurchases(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "pool-purchases [pool_ID]",
Use: "purchases [pool_ID]",
Short: "query purchases in a given pool",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -179,30 +175,6 @@ func GetCmdPoolPurchases(queryRoute string, cdc *codec.Codec) *cobra.Command {
return cmd
}

// GetCmdPurchases returns the command for querying all purchases.
func GetCmdPurchases(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "purchases",
Short: "query all purchases",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryPurchases)
res, _, err := cliCtx.QueryWithData(route, nil)
if err != nil {
return err
}

var out []types.Purchase
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}

return cmd
}

// GetCmdProvider returns the command for querying a provider.
func GetCmdProvider(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Expand All @@ -227,50 +199,6 @@ func GetCmdProvider(queryRoute string, cdc *codec.Codec) *cobra.Command {
return cmd
}

// GetCmdProviders returns the command for querying all providers.
func GetCmdProviders(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "providers",
Args: cobra.ExactArgs(0),
Short: "query all providers",
Long: strings.TrimSpace(
fmt.Sprintf(`Query providers with pagination parameters
Example:
$ %[1]s query shield providers
$ %[1]s query shield providers --page=2 --limit=100
`,
version.ClientName,
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)

page := viper.GetInt(flags.FlagPage)
limit := viper.GetInt(flags.FlagLimit)

params := types.NewQueryPaginationParams(page, limit)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryProviders)
res, _, err := cliCtx.QueryWithData(route, bz)
if err != nil {
return err
}

var out []types.Provider
cdc.MustUnmarshalJSON(res, &out)
return cliCtx.PrintOutput(out)
},
}
cmd.Flags().Int(flags.FlagPage, 1, "pagination page of providers to to query for")
cmd.Flags().Int(flags.FlagLimit, 100, "pagination limit of providers to query for")
return cmd
}

// GetCmdPoolParams returns the command for querying pool parameters.
func GetCmdPoolParams(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Expand Down
54 changes: 0 additions & 54 deletions x/shield/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ func registerQueryRoutes(cliCtx context.CLIContext, r *mux.Router) {
r.HandleFunc(fmt.Sprintf("/%s/pool/{poolID}/purchases", types.QuerierRoute), queryPoolPurchasesHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/pool/{poolID}/purchaser/{address}/purchases", types.QuerierRoute), queryPurchaseListHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/provider/{address}", types.QuerierRoute), queryProviderHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/providers", types.QuerierRoute), queryProvidersHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/purchaser/{address}/purchases", types.QuerierRoute), queryPurchaserPurchasesHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/purchases", types.QuerierRoute), queryPurchasesHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/collaterals/{address}", types.QuerierRoute), queryProviderCollateralsHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/pool_params", types.QuerierRoute), queryPoolParamsHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/claim_params", types.QuerierRoute), queryClaimParamsHandler(cliCtx)).Methods("GET")
Expand Down Expand Up @@ -202,25 +200,6 @@ func queryPoolPurchasesHandler(cliCtx context.CLIContext) http.HandlerFunc {
}
}

func queryPurchasesHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryPurchases)
res, height, err := cliCtx.QueryWithData(route, nil)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}

func queryProviderHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
Expand All @@ -243,39 +222,6 @@ func queryProviderHandler(cliCtx context.CLIContext) http.HandlerFunc {
}
}

func queryProvidersHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, page, limit, err := rest.ParseHTTPArgsWithLimit(r, 0)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

params := types.NewQueryPaginationParams(page, limit)

bz, err := cliCtx.Codec.MarshalJSON(params)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryProviders)
res, height, err := cliCtx.QueryWithData(route, bz)
if err != nil {
rest.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}

cliCtx = cliCtx.WithHeight(height)
rest.PostProcessResponse(w, cliCtx, res)
}
}

func queryPoolParamsHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
Expand Down
32 changes: 0 additions & 32 deletions x/shield/keeper/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,35 +131,3 @@ func (k Keeper) GetAllProviders(ctx sdk.Context) (providers []types.Provider) {
})
return
}

// GetProvidersPaginated performs paginated query of providers.
func (k Keeper) GetProvidersPaginated(ctx sdk.Context, page, limit uint) (providers []types.Provider) {
k.IterateProvidersPaginated(ctx, page, limit, func(provider types.Provider) bool {
providers = append(providers, provider)
return false
})
return
}

// IterateProvidersPaginated iterates over providers based on
// pagination parameters and performs a callback function.
func (k Keeper) IterateProvidersPaginated(ctx sdk.Context, page, limit uint, cb func(vote types.Provider) (stop bool)) {
iterator := k.GetProvidersIteratorPaginated(ctx, page, limit)

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var provider types.Provider
k.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &provider)

if cb(provider) {
break
}
}
}

// GetProvidersIteratorPaginated returns an iterator to go over
// providers based on pagination parameters.
func (k Keeper) GetProvidersIteratorPaginated(ctx sdk.Context, page, limit uint) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIteratorPaginated(store, types.ProviderKey, page, limit)
}
48 changes: 18 additions & 30 deletions x/shield/keeper/purchase.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ func (k Keeper) PurchaseShield(ctx sdk.Context, poolID uint64, shield sdk.Coins,
return k.purchaseShield(ctx, poolID, shield, description, purchaser, serviceFees)
}

// IterateAllPurchases iterates over the all the stored purchases and performs a callback function.
func (k Keeper) IterateAllPurchases(ctx sdk.Context, callback func(purchase types.Purchase) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.PurchaseListKey)

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var purchase types.Purchase
k.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &purchase)

if callback(purchase) {
break
}
}
}

// RemoveExpiredPurchasesAndDistributeFees removes expired purchases and distributes fees for current block.
func (k Keeper) RemoveExpiredPurchasesAndDistributeFees(ctx sdk.Context) {
store := ctx.KVStore(k.storeKey)
Expand Down Expand Up @@ -285,7 +301,7 @@ func (k Keeper) IteratePoolPurchaseLists(ctx sdk.Context, poolID uint64, callbac
}
}

// GetAllPurchaseLists retrieves all purchase lists.
// GetAllPurchaseLists retrieves all purchases.
func (k Keeper) GetAllPurchaseLists(ctx sdk.Context) (purchases []types.PurchaseList) {
k.IteratePurchaseLists(ctx, func(purchase types.PurchaseList) bool {
purchases = append(purchases, purchase)
Expand All @@ -294,7 +310,7 @@ func (k Keeper) GetAllPurchaseLists(ctx sdk.Context) (purchases []types.Purchase
return
}

// InsertExpiringPurchaseQueue inserts a purchase into the expired purchase queue.
// InsertExpiredPurchaseQueue inserts a purchase into the expired purchase queue.
func (k Keeper) InsertExpiringPurchaseQueue(ctx sdk.Context, purchaseList types.PurchaseList, endTime time.Time) {
timeSlice := k.GetExpiringPurchaseQueueTimeSlice(ctx, endTime)

Expand Down Expand Up @@ -348,31 +364,3 @@ func (k Keeper) GetNextPurchaseID(ctx sdk.Context) uint64 {
opBz := store.Get(types.GetNextPurchaseIDKey())
return binary.LittleEndian.Uint64(opBz)
}

// GetAllPurchases retrieves all purchases.
func (k Keeper) GetAllPurchases(ctx sdk.Context) (purchases []types.Purchase) {
k.IteratePurchaseListEntries(ctx, func(purchase types.Purchase) bool {
purchases = append(purchases, purchase)
return false
})
return
}

// IteratePurchaseListEntries iterates through entries of
// all purchase lists.
func (k Keeper) IteratePurchaseListEntries(ctx sdk.Context, callback func(purchase types.Purchase) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.PurchaseListKey)

defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var purchaseList types.PurchaseList
k.cdc.MustUnmarshalBinaryLengthPrefixed(iterator.Value(), &purchaseList)

for _, entry := range purchaseList.Entries {
if callback(entry) {
break
}
}
}
}
33 changes: 0 additions & 33 deletions x/shield/keeper/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@ func NewQuerier(k Keeper) sdk.Querier {
return queryPurchaserPurchases(ctx, path[1:], k)
case types.QueryPoolPurchases:
return queryPoolPurchases(ctx, path[1:], k)
case types.QueryPurchases:
return queryPurchases(ctx, path[1:], k)
case types.QueryProvider:
return queryProvider(ctx, path[1:], k)
case types.QueryProviders:
return queryProviders(ctx, req, k)
case types.QueryPoolParams:
return queryPoolParams(ctx, path[1:], k)
case types.QueryClaimParams:
Expand Down Expand Up @@ -162,19 +158,6 @@ func queryPoolPurchases(ctx sdk.Context, path []string, k Keeper) (res []byte, e
return res, nil
}

// queryPurchases queries all purchases.
func queryPurchases(ctx sdk.Context, path []string, k Keeper) (res []byte, err error) {
if err := validatePathLength(path, 0); err != nil {
return nil, err
}

res, err = codec.MarshalJSONIndent(k.cdc, k.GetAllPurchases(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}

// queryProvider returns information about a provider.
func queryProvider(ctx sdk.Context, path []string, k Keeper) (res []byte, err error) {
if err := validatePathLength(path, 1); err != nil {
Expand All @@ -197,22 +180,6 @@ func queryProvider(ctx sdk.Context, path []string, k Keeper) (res []byte, err er
return res, nil
}

func queryProviders(ctx sdk.Context, req abci.RequestQuery, k Keeper) (res []byte, err error) {
var params types.QueryPaginationParams
err = k.cdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}

providers := k.GetProvidersPaginated(ctx, uint(params.Page), uint(params.Limit))

res, err = codec.MarshalJSONIndent(k.cdc, providers)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}

func queryPoolParams(ctx sdk.Context, path []string, k Keeper) (res []byte, err error) {
if err := validatePathLength(path, 0); err != nil {
return nil, err
Expand Down
Loading

0 comments on commit ca90c64

Please sign in to comment.