/
querier.go
49 lines (41 loc) · 1.35 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
package requestchain
import (
"fmt"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
)
// query endpoints supported by the requestchain Querier
const (
QueryGetBlockName = "getblock"
QueryGetBlockCount = "blockcount"
)
// NewQuerier is the module level router for state queries
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case QueryGetBlockName:
return queryGetBlock(ctx, path[1:], req, keeper)
case QueryGetBlockCount:
return queryGetBlockCount(ctx, path[1:], req, keeper)
default:
return nil, sdk.ErrUnknownRequest(fmt.Sprintf("unknown requestchain query endpoint %v", path[0]))
}
}
}
// nolint: unparam
func queryGetBlock(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
blockIndex, err := strconv.ParseUint(path[0], 10, 64)
if err != nil {
panic(err)
}
res := keeper.GetBlock(ctx, blockIndex)
if len(res) == 0 {
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("Index doesn't exist %v", blockIndex))
}
return res, nil
}
// nolint: unparam
func queryGetBlockCount(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) ([]byte, sdk.Error) {
return []byte(fmt.Sprint(keeper.GetBlockCount(ctx))), nil
}