-
Notifications
You must be signed in to change notification settings - Fork 2
/
request_search.go
executable file
·146 lines (129 loc) · 4.42 KB
/
request_search.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package common
import (
"fmt"
"sort"
"github.com/ODIN-PROTOCOL/odin-core/hooks/common"
commontypes "github.com/ODIN-PROTOCOL/odin-core/x/common/types"
oracletypes "github.com/ODIN-PROTOCOL/odin-core/x/oracle/types"
"github.com/cosmos/cosmos-sdk/client"
)
func queryLatestRequest(clientCtx client.Context, requestSearchRequest *oracletypes.QueryRequestSearchRequest) (oracletypes.RequestID, error) {
bin := clientCtx.Codec.MustMarshalJSON(requestSearchRequest)
bz, _, err := clientCtx.QueryWithData(fmt.Sprintf("%s/%s/1", common.AppHook, oracletypes.QueryLatestRequest), bin)
if err != nil {
return 0, err
}
var containerIDs oracletypes.QueryRequestIDs
err = clientCtx.Codec.UnmarshalJSON(bz, &containerIDs)
if err != nil {
return 0, err
}
if len(containerIDs.RequestIds) == 0 {
return 0, nil
}
if len(containerIDs.RequestIds) > 1 {
// NEVER EXPECT TO HIT.
panic("multi request limit=1")
}
return oracletypes.RequestID(containerIDs.RequestIds[0]), nil
}
func queryRequest(route string, clientCtx client.Context, rid oracletypes.RequestID) (oracletypes.QueryRequestResponse, int64, error) {
bz, height, err := clientCtx.Query(fmt.Sprintf("custom/%s/%s/%d", route, oracletypes.QueryRequests, rid))
if err != nil {
return oracletypes.QueryRequestResponse{}, 0, err
}
var queryResult commontypes.QueryResult
if err := clientCtx.LegacyAmino.UnmarshalJSON(bz, &queryResult); err != nil {
return oracletypes.QueryRequestResponse{}, 0, err
}
var result oracletypes.QueryRequestResponse
if err := clientCtx.LegacyAmino.UnmarshalJSON(queryResult.Result, &result); err != nil {
return oracletypes.QueryRequestResponse{}, 0, err
}
return result, height, nil
}
func QuerySearchLatestRequest(
route string, clientCtx client.Context, requestSearchRequest *oracletypes.QueryRequestSearchRequest,
) (*oracletypes.QueryRequestSearchResponse, int64, error) {
id, err := queryLatestRequest(clientCtx, requestSearchRequest)
if err != nil {
return nil, 0, err
}
if id == 0 {
return nil, 0, nil
}
req, h, err := queryRequest(route, clientCtx, id)
return oracletypes.NewQueryRequestSearchResponse(req), h, err
}
func queryMultiRequest(clientCtx client.Context, requestSearchParams *oracletypes.QueryRequestSearchRequest, limit int) (*oracletypes.QueryRequestIDs, error) {
bin := clientCtx.Codec.MustMarshalJSON(requestSearchParams)
bz, _, err := clientCtx.QueryWithData(fmt.Sprintf("%s/%s/%d", common.AppHook, oracletypes.QueryLatestRequest, limit), bin)
if err != nil {
return nil, err
}
var containerIDs oracletypes.QueryRequestIDs
err = clientCtx.Codec.UnmarshalJSON(bz, &containerIDs)
if err != nil {
return nil, err
}
return &containerIDs, nil
}
func queryRequests(
route string, clientCtx client.Context, containerIDs *oracletypes.QueryRequestIDs,
) ([]oracletypes.QueryRequestResponse, int64, error) {
type queryResult struct {
result oracletypes.QueryRequestResponse
err error
height int64
}
queryResultsChan := make(chan queryResult, len(containerIDs.RequestIds))
for _, rid := range containerIDs.RequestIds {
go func(rid int64) {
out, h, err := queryRequest(route, clientCtx, oracletypes.RequestID(rid))
if err != nil {
queryResultsChan <- queryResult{err: err}
return
}
queryResultsChan <- queryResult{result: out, height: h}
}(rid)
}
requests := make([]oracletypes.QueryRequestResponse, 0)
height := int64(0)
for idx := 0; idx < len(containerIDs.RequestIds); idx++ {
select {
case req := <-queryResultsChan:
if req.err != nil {
return nil, 0, req.err
}
if req.result.Result.Result != nil {
requests = append(requests, req.result)
if req.height > height {
height = req.height
}
}
}
}
sort.Slice(requests, func(i, j int) bool {
return requests[i].Result.ResolveTime > requests[j].Result.ResolveTime
})
return requests, height, nil
}
func QueryMultiSearchLatestRequest(
route string, clientCtx client.Context, requestSearchParams *oracletypes.QueryRequestSearchRequest, limit int,
) ([]oracletypes.QueryRequestResponse, int64, error) {
requestIDs, err := queryMultiRequest(clientCtx, requestSearchParams, limit)
if err != nil {
return nil, 0, err
}
queryRequestResults, h, err := queryRequests(route, clientCtx, requestIDs)
if err != nil {
return nil, 0, err
}
if len(queryRequestResults) == 0 {
return nil, 0, nil
}
if len(queryRequestResults) > limit {
queryRequestResults = queryRequestResults[:limit]
}
return queryRequestResults, h, nil
}