-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc_query.go
97 lines (77 loc) · 2.78 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
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
package keeper
import (
"bytes"
"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/nephirim/blackfury/x/claimsmanager/types"
)
var _ types.QueryServer = Keeper{}
func (k Keeper) Claims(c context.Context, req *types.QueryClaimsRequest) (*types.QueryClaimsResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "empty request")
}
ctx := sdk.UnwrapSDKContext(c)
var claims []types.Claim
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixClaim)
pageRes, err := query.FilteredPaginate(store, req.Pagination, func(_, value []byte, accumulate bool) (bool, error) {
var claim types.Claim
if err := k.cdc.Unmarshal(value, &claim); err != nil {
return false, err
}
if claim.ChainId == req.ChainId {
claims = append(claims, claim)
return true, nil
}
return false, nil
})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &types.QueryClaimsResponse{
Claims: claims,
Pagination: pageRes,
}, nil
}
func (k Keeper) LastEpochClaims(c context.Context, q *types.QueryClaimsRequest) (*types.QueryClaimsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
out := []types.Claim{}
k.IterateLastEpochClaims(ctx, q.ChainId, func(_ int64, claim types.Claim) (stop bool) {
out = append(out, claim)
return false
})
return &types.QueryClaimsResponse{Claims: out}, nil
}
func (k Keeper) UserClaims(c context.Context, q *types.QueryClaimsRequest) (*types.QueryClaimsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
out := []types.Claim{}
addrBytes := []byte(q.Address)
k.IterateAllClaims(ctx, func(_ int64, key []byte, claim types.Claim) (stop bool) {
// check for the presence of the addr bytes in the key.
// first prefix byte is 0x00; so cater for that! Then + 1 to skip the separator.
idx := bytes.Index(key[1:], []byte{0x00}) + 1 + 1
if bytes.Equal(key[idx:idx+len(addrBytes)], addrBytes) {
out = append(out, claim)
}
return false
})
return &types.QueryClaimsResponse{Claims: out}, nil
}
func (k Keeper) UserLastEpochClaims(c context.Context, q *types.QueryClaimsRequest) (*types.QueryClaimsResponse, error) {
ctx := sdk.UnwrapSDKContext(c)
out := []types.Claim{}
addrBytes := []byte(q.Address)
k.IterateAllLastEpochClaims(ctx, func(_ int64, key []byte, claim types.Claim) (stop bool) {
// check for the presence of the addr bytes in the key.
// First byte is 0x01 here, so no need to consider it; + 1 to skip the separator.
idx := bytes.Index(key, []byte{0x00}) + 1
if bytes.Equal(key[idx:idx+len(addrBytes)], addrBytes) {
out = append(out, claim)
}
return false
})
return &types.QueryClaimsResponse{Claims: out}, nil
}