cosmos-sdk/x/upgrade/keeper/querier.go
Marie 7f59723d88
Make JSONMarshaler methods require proto.Message (#7054)
* Make JSONMarshaler require proto.Message

* Use &msg with MarshalJSON

* Use *LegacyAmino in queriers instead of JSONMarshaler

* Revert ABCIMessageLogs String() and coins tests

* Use LegacyAmino in client/debug and fix subspace tests

* Use LegacyAmino in all legacy queriers and adapt simulation

* Make AminoCodec implement Marshaler and some godoc fixes

* Test fixes

* Remove unrelevant comment

* Use TxConfig.TxJSONEncoder

* Use encoding/json in genutil cli migrate/validate genesis cmds

* Address simulation related comments

* Use JSONMarshaler in cli tests

* Use proto.Message as respType in cli tests

* Use tmjson for tm GenesisDoc

* Update types/module/simulation.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Update types/module/module_test.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Add godoc comments

* Remove unused InsertKeyJSON

* Fix tests

Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
2020-08-26 09:39:38 +00:00

65 lines
1.6 KiB
Go

package keeper
import (
"encoding/binary"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
abci "github.com/tendermint/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// NewQuerier creates a querier for upgrade cli and REST endpoints
func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case types.QueryCurrent:
return queryCurrent(ctx, req, k, legacyQuerierCdc)
case types.QueryApplied:
return queryApplied(ctx, req, k, legacyQuerierCdc)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
}
}
}
func queryCurrent(ctx sdk.Context, _ abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
plan, has := k.GetUpgradePlan(ctx)
if !has {
return nil, nil
}
res, err := legacyQuerierCdc.MarshalJSON(&plan)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
func queryApplied(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryAppliedPlanRequest
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
applied := k.GetDoneHeight(ctx, params.Name)
if applied == 0 {
return nil, nil
}
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, uint64(applied))
return bz, nil
}