-
Notifications
You must be signed in to change notification settings - Fork 0
/
querier.go
61 lines (46 loc) · 1.52 KB
/
querier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package keeper
import (
"encoding/binary"
abci "github.com/evoblockchain/evochain/libs/tendermint/abci/types"
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
sdkerrors "github.com/evoblockchain/evochain/libs/cosmos-sdk/types/errors"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/x/upgrade/internal/types"
)
// NewQuerier creates a querier for upgrade cli and REST endpoints
func NewQuerier(k Keeper) 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)
case types.QueryApplied:
return queryApplied(ctx, req, k)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint: %s", types.ModuleName, path[0])
}
}
}
func queryCurrent(ctx sdk.Context, req abci.RequestQuery, k Keeper) ([]byte, error) {
plan, has := k.GetUpgradePlan(ctx)
if !has {
return nil, nil
}
res, err := k.cdc.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) ([]byte, error) {
var params types.QueryAppliedParams
err := k.cdc.UnmarshalJSON(req.Data, ¶ms)
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
}