2018-12-14 11:09:39 -08:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
2019-05-09 10:38:15 -07:00
|
|
|
"fmt"
|
|
|
|
|
2018-12-14 11:09:39 -08:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
|
2019-07-02 06:00:46 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2018-12-14 11:09:39 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewQuerier creates a new querier for slashing clients.
|
2019-05-16 08:25:32 -07:00
|
|
|
func NewQuerier(k Keeper) sdk.Querier {
|
2018-12-14 11:09:39 -08:00
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, sdk.Error) {
|
|
|
|
switch path[0] {
|
|
|
|
case QueryParameters:
|
2019-05-16 08:25:32 -07:00
|
|
|
return queryParams(ctx, k)
|
2019-05-09 10:38:15 -07:00
|
|
|
case QuerySigningInfo:
|
2019-05-16 08:25:32 -07:00
|
|
|
return querySigningInfo(ctx, req, k)
|
2019-05-09 10:38:15 -07:00
|
|
|
case QuerySigningInfos:
|
2019-05-16 08:25:32 -07:00
|
|
|
return querySigningInfos(ctx, req, k)
|
2018-12-14 11:09:39 -08:00
|
|
|
default:
|
2019-01-11 12:08:01 -08:00
|
|
|
return nil, sdk.ErrUnknownRequest("unknown staking query endpoint")
|
2018-12-14 11:09:39 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
func queryParams(ctx sdk.Context, k Keeper) ([]byte, sdk.Error) {
|
2018-12-14 11:09:39 -08:00
|
|
|
params := k.GetParams(ctx)
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(ModuleCdc, params)
|
2018-12-14 11:09:39 -08:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to marshal JSON", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2019-05-09 10:38:15 -07:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
func querySigningInfo(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
|
2019-05-09 10:38:15 -07:00
|
|
|
var params QuerySigningInfoParams
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
err := ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-05-09 10:38:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
signingInfo, found := k.getValidatorSigningInfo(ctx, params.ConsAddress)
|
|
|
|
if !found {
|
|
|
|
return nil, ErrNoSigningInfoFound(DefaultCodespace, params.ConsAddress)
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(ModuleCdc, signingInfo)
|
2019-05-09 10:38:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
func querySigningInfos(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
|
2019-05-09 10:38:15 -07:00
|
|
|
var params QuerySigningInfosParams
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
err := ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
|
2019-05-09 10:38:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
var signingInfos []ValidatorSigningInfo
|
|
|
|
|
|
|
|
k.IterateValidatorSigningInfos(ctx, func(consAddr sdk.ConsAddress, info ValidatorSigningInfo) (stop bool) {
|
|
|
|
signingInfos = append(signingInfos, info)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2019-07-02 06:00:46 -07:00
|
|
|
start, end := client.Paginate(len(signingInfos), params.Page, params.Limit, int(k.sk.MaxValidators(ctx)))
|
|
|
|
if start < 0 || end < 0 {
|
2019-05-09 10:38:15 -07:00
|
|
|
signingInfos = []ValidatorSigningInfo{}
|
|
|
|
} else {
|
|
|
|
signingInfos = signingInfos[start:end]
|
|
|
|
}
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
res, err := codec.MarshalJSONIndent(ModuleCdc, signingInfos)
|
2019-05-09 10:38:15 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", err.Error()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|