-
Notifications
You must be signed in to change notification settings - Fork 4
/
legacy_querier.go
137 lines (104 loc) · 3.85 KB
/
legacy_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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package keeper
import (
"github.com/cosmos/cosmos-sdk/codec"
"github.com/unification-com/mainchain/x/wrkchain/types"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"
)
// query endpoints supported by the wrkchain Querier
const (
QueryParameters = "params"
QueryWrkChain = "wrkchain"
QueryWrkChainsFiltered = "wrkchains-filtered"
QueryWrkChainBlock = "block"
)
// NewLegacyQuerier is the module level router for state queries
func NewLegacyQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
switch path[0] {
case QueryParameters:
return queryParams(ctx, keeper, legacyQuerierCdc)
case QueryWrkChain:
return queryWrkChain(ctx, path[1:], req, keeper, legacyQuerierCdc)
case QueryWrkChainsFiltered:
return queryWrkChainsFiltered(ctx, path[1:], req, keeper, legacyQuerierCdc)
case QueryWrkChainBlock:
return queryWrkChainBlock(ctx, path[1:], req, keeper, legacyQuerierCdc)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query path: %s", path[0])
}
}
}
func queryParams(ctx sdk.Context, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
params := k.GetParams(ctx)
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return res, nil
}
// nolint: unparam
func queryWrkChain(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
wrkchainID, err := strconv.Atoi(path[0])
if err != nil {
wrkchainID = 0
}
wrkchain, found := keeper.GetWrkChain(ctx, uint64(wrkchainID))
if !found {
return nil, sdkerrors.Wrapf(types.ErrWrkChainDoesNotExist, "wrkchain %d not found", wrkchainID)
}
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, wrkchain)
if err != nil {
panic("could not marshal result to JSON")
}
return res, nil
}
func queryWrkChainBlock(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
wrkchainID, err := strconv.Atoi(path[0])
if err != nil {
wrkchainID = 0
}
height, err := strconv.Atoi(path[1])
if err != nil {
height = 0
}
wrkchain, found := keeper.GetWrkChain(ctx, uint64(wrkchainID))
if !found {
return nil, sdkerrors.Wrapf(types.ErrWrkChainDoesNotExist, "wrkchain %d not found", wrkchainID)
}
wrkchainBlock, found := keeper.GetWrkChainBlock(ctx, uint64(wrkchainID), uint64(height))
if !found {
return nil, sdkerrors.Wrapf(types.ErrWrkChainDoesNotExist, "block %d not found for wrkchain %d", height, wrkchainID)
}
legacyBlk := types.WrkChainBlockLegacy{
WrkChainID: wrkchain.WrkchainId,
Height: wrkchainBlock.Height,
BlockHash: wrkchainBlock.Blockhash,
ParentHash: wrkchainBlock.Parenthash,
Hash1: wrkchainBlock.Hash1,
Hash2: wrkchainBlock.Hash2,
Hash3: wrkchainBlock.Hash3,
SubmitTime: wrkchainBlock.SubTime,
Owner: wrkchain.Owner,
}
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, legacyBlk)
if err != nil {
panic("could not marshal result to JSON")
}
return res, nil
}
func queryWrkChainsFiltered(ctx sdk.Context, _ []string, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var queryParams types.QueryWrkChainsFilteredRequest
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &queryParams)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
filteredWrkChains := k.GetWrkChainsFiltered(ctx, queryParams)
res, err := codec.MarshalJSONIndent(legacyQuerierCdc, filteredWrkChains)
if err != nil {
panic("could not marshal result to JSON")
}
return res, nil
}