-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquery.go
56 lines (48 loc) · 1.36 KB
/
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
package challenger
import (
"encoding/base64"
dbtypes "github.com/initia-labs/opinit-bots/db/types"
challengertypes "github.com/initia-labs/opinit-bots/challenger/types"
)
func (c *Challenger) QueryChallenges(offset string, limit uint64, descOrder bool) (res challengertypes.QueryChallengesResponse, err error) {
challenges := []challengertypes.Challenge{}
next := ""
count := uint64(0)
fetchFn := func(key, value []byte) (bool, error) {
if count >= limit {
next = base64.StdEncoding.EncodeToString(key)
return true, nil
}
challenge := challengertypes.Challenge{}
err = challenge.Unmarshal(value)
if err != nil {
return true, err
}
count++
challenges = append(challenges, challenge)
return false, nil
}
var startKey []byte
if offset != "" {
startKey, err = base64.StdEncoding.DecodeString(offset)
if err != nil {
return challengertypes.QueryChallengesResponse{}, err
}
}
if descOrder {
err = c.db.ReverseIterate(dbtypes.AppendSplitter(challengertypes.ChallengeKey), startKey, fetchFn)
if err != nil {
return challengertypes.QueryChallengesResponse{}, err
}
} else {
err = c.db.Iterate(dbtypes.AppendSplitter(challengertypes.ChallengeKey), startKey, fetchFn)
if err != nil {
return challengertypes.QueryChallengesResponse{}, err
}
}
if next != "" {
res.Next = &next
}
res.Challenges = challenges
return res, nil
}