-
Notifications
You must be signed in to change notification settings - Fork 102
/
querier.go
126 lines (117 loc) · 4.64 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
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
package keeper
import (
"fmt"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/pokt-network/pocket-core/codec"
sdk "github.com/pokt-network/pocket-core/types"
"github.com/pokt-network/pocket-core/x/pocketcore/types"
)
// "NewQuerier" - Creates an sdk.Querier for the pocket core module
func NewQuerier(k Keeper) sdk.Querier {
return func(ctx sdk.Ctx, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
// query pocket supported supported non-native blockchains
case types.QuerySupportedBlockchains:
return querySupportedBlockchains(ctx, req, k)
// query the parameters of the pocketcore module
case types.QueryParameters:
return queryParameters(ctx, k)
// endpoint allowing a client to query a relay to a non-native blockchain
case types.QueryRelay:
return queryRelay(ctx, req, k)
// endpoint allowing a client to receive the nodes for their session
case types.QueryDispatch:
return queryDispatch(ctx, req, k)
// endpoint allowing a client to submit a challenge for an invalid relay-response
case types.QueryChallenge:
return queryChallenge(ctx, req, k)
default:
return nil, sdk.ErrUnknownRequest("unknown pocketcore query endpoint")
}
}
}
// "queryChallenge" - Is a handler for the challenge query
// The challenge query allows clients to submit a challenge for invalid relay responses
func queryChallenge(ctx sdk.Ctx, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
// unmarshal data into a query params object
var params types.QueryChallengeParams
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
}
// handle the challenge from the params
response, er := k.HandleChallenge(ctx, params.Challenge)
if er != nil {
return nil, er
}
// marshal the response data into amino-json
res, err := codec.MarshalJSONIndent(types.ModuleCdc, response)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", err.Error()))
}
return res, nil
}
// "queryRelay" - Is a handler for the relay query
// The relay query allows clients to submit a request to a non-native blockchain
func queryRelay(ctx sdk.Ctx, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
// unmarshal data into a query params object
var params types.QueryRelayParams
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
}
// handle the relay from the params
response, er := k.HandleRelay(ctx, params.Relay)
if er != nil {
return nil, er
}
// marshals the response data into amino-json
res, err := codec.MarshalJSONIndent(types.ModuleCdc, response)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", err.Error()))
}
return res, nil
}
// "queryDispatch" - Is a handler for the dispatch query
// The dispatch query allows clients retrieve their session information
func queryDispatch(ctx sdk.Ctx, req abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
// unmarshal data into a query params object
var params types.QueryDispatchParams
err := types.ModuleCdc.UnmarshalJSON(req.Data, ¶ms)
if err != nil {
return nil, sdk.ErrInternal(fmt.Sprintf("failed to parse params: %s", err))
}
// handle the dispatch from the params
response, er := k.HandleDispatch(ctx, params.SessionHeader)
if er != nil {
return nil, er
}
// marshals the response data into amino-json
res, err := codec.MarshalJSONIndent(types.ModuleCdc, *response)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", err.Error()))
}
return res, nil
}
// "queryParameters" - Is a handler for the parameters query
// Returns all the parameters in the module
func queryParameters(ctx sdk.Ctx, k Keeper) ([]byte, sdk.Error) {
// get the params
params := k.GetParams(ctx)
// marshal response data into amino-json
res, err := codec.MarshalJSONIndent(types.ModuleCdc, params)
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err.Error()))
}
return res, nil
}
// "querySupportedBlockchains" - Is a handler for the supported blockchains query
// Returns the non native chains supported on pocket network
func querySupportedBlockchains(ctx sdk.Ctx, _ abci.RequestQuery, k Keeper) ([]byte, sdk.Error) {
// marshal supported blockchains into amino-json
res, err := codec.MarshalJSONIndent(types.ModuleCdc, k.SupportedBlockchains(ctx))
if err != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("failed to JSON marshal result: %s", err.Error()))
}
return res, nil
}