forked from evmos/evmos
-
Notifications
You must be signed in to change notification settings - Fork 3
/
grpc_query.go
68 lines (55 loc) · 1.65 KB
/
grpc_query.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
package keeper
import (
"context"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/EscanBE/evermint/v12/x/epochs/types"
)
var _ types.QueryServer = Keeper{}
// EpochInfos provide running epochInfos
func (k Keeper) EpochInfos(
c context.Context,
req *types.QueryEpochsInfoRequest,
) (*types.QueryEpochsInfoResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
var epochs []types.EpochInfo
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixEpoch)
pageRes, err := query.Paginate(store, req.Pagination, func(_, value []byte) error {
var epoch types.EpochInfo
if err := k.cdc.Unmarshal(value, &epoch); err != nil {
return err
}
epochs = append(epochs, epoch)
return nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryEpochsInfoResponse{
Epochs: epochs,
Pagination: pageRes,
}, nil
}
// CurrentEpoch provides current epoch of specified identifier
func (k Keeper) CurrentEpoch(
c context.Context,
req *types.QueryCurrentEpochRequest,
) (*types.QueryCurrentEpochResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
info, found := k.GetEpochInfo(ctx, req.Identifier)
if !found {
return nil, status.Errorf(codes.NotFound, "epoch info not found: %s", req.Identifier)
}
return &types.QueryCurrentEpochResponse{
CurrentEpoch: info.CurrentEpoch,
}, nil
}