-
Notifications
You must be signed in to change notification settings - Fork 18
/
query.go
198 lines (173 loc) · 5.93 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package service
import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
sdk "github.com/bianjieai/irita-sdk-go/types"
)
// queryRequestContextByTxQuery will query for a single request context via a direct txs tags query.
func (s serviceClient) queryRequestContextByTxQuery(reqCtxID string) (RequestContext, error) {
txHash, msgIndex, err := splitRequestContextID(reqCtxID)
if err != nil {
return RequestContext{}, err
}
txInfo, err := s.QueryTx(hex.EncodeToString(txHash))
if err != nil {
return RequestContext{}, err
}
if int64(len(txInfo.Tx.GetMsgs())) > msgIndex {
msg := txInfo.Tx.GetMsgs()[msgIndex]
if msg, ok := msg.(MsgCallService); ok {
return RequestContext{
ServiceName: msg.ServiceName,
Providers: msg.Providers,
Consumer: msg.Consumer,
Input: msg.Input,
ServiceFeeCap: msg.ServiceFeeCap,
Timeout: msg.Timeout,
SuperMode: msg.SuperMode,
Repeated: msg.Repeated,
RepeatedFrequency: msg.RepeatedFrequency,
RepeatedTotal: msg.RepeatedTotal,
BatchCounter: uint64(msg.RepeatedTotal),
BatchRequestCount: 0,
BatchResponseCount: 0,
BatchState: BATCHCOMPLETED,
State: COMPLETED,
ResponseThreshold: 0,
ModuleName: "",
}, nil
}
}
return RequestContext{}, fmt.Errorf("invalid reqCtxID:%s", reqCtxID)
}
// queryRequestByTxQuery will query for a single request via a direct txs tags query.
func (s serviceClient) queryRequestByTxQuery(requestID string) (Request, error) {
reqCtxID, _, requestHeight, batchRequestIndex, err := splitRequestID(requestID)
if err != nil {
return Request{}, err
}
// query request context
reqCtx, err := s.QueryRequestContext(hex.EncodeToString(reqCtxID))
if err != nil {
return Request{}, err
}
blockResult, err := s.BlockResults(&requestHeight)
if err != nil {
return Request{}, err
}
for _, event := range blockResult.EndBlockEvents {
if event.Type == eventTypeNewBatchRequest {
var found bool
var requestsBz []byte
for _, attribute := range event.Attributes {
if string(attribute.Key) == attributeKeyRequests {
requestsBz = attribute.GetValue()
}
if string(attribute.Key) == attributeKeyRequestContextID &&
string(attribute.GetValue()) == reqCtxID.String() {
found = true
}
}
if found {
var requests []CompactRequest
if err := json.Unmarshal(requestsBz, &requests); err != nil {
return Request{}, err
}
if len(requests) > int(batchRequestIndex) {
compactRequest := requests[batchRequestIndex]
return Request{
ID: sdk.MustHexBytesFrom(requestID),
ServiceName: reqCtx.ServiceName,
Provider: compactRequest.Provider,
Consumer: reqCtx.Consumer,
Input: reqCtx.Input,
ServiceFee: compactRequest.ServiceFee,
SuperMode: reqCtx.SuperMode,
RequestHeight: compactRequest.RequestHeight,
ExpirationHeight: compactRequest.RequestHeight + reqCtx.Timeout,
RequestContextID: compactRequest.RequestContextID,
RequestContextBatchCounter: compactRequest.RequestContextBatchCounter,
}, nil
}
}
}
}
return Request{}, fmt.Errorf("invalid requestID:%s", requestID)
}
// queryResponseByTxQuery will query for a single request via a direct txs tags query.
func (s serviceClient) queryResponseByTxQuery(requestID string) (Response, error) {
builder := sdk.NewEventQueryBuilder().AddCondition(
sdk.NewCond(
sdk.EventTypeMessage,
sdk.AttributeKeyAction,
).EQ("respond_service"),
).AddCondition(
sdk.NewCond(
sdk.EventTypeMessage,
sdk.AttributeKeyAction,
).EQ(attributeKeyRequestID),
)
result, err := s.QueryTxs(builder, 1, 1)
if err != nil {
return Response{}, err
}
if len(result.Txs) == 0 {
return Response{}, fmt.Errorf("unknown response: %s", requestID)
}
reqCtxID, batchCounter, _, _, err := splitRequestID(requestID)
if err != nil {
return Response{}, err
}
// query request context
reqCtx, err := s.QueryRequestContext(hex.EncodeToString(reqCtxID))
if err != nil {
return Response{}, err
}
for _, msg := range result.Txs[0].Tx.GetMsgs() {
if responseMsg, ok := msg.(MsgRespondService); ok {
if responseMsg.RequestID.String() != requestID {
continue
}
return Response{
Provider: responseMsg.Provider,
Consumer: reqCtx.Consumer,
Output: responseMsg.Output,
Result: responseMsg.Result,
RequestContextID: reqCtxID,
RequestContextBatchCounter: batchCounter,
}, nil
}
}
return Response{}, nil
}
// SplitRequestContextID splits the given contextID to txHash and msgIndex
func splitRequestContextID(reqCtxID string) (sdk.HexBytes, int64, error) {
contextID, err := hex.DecodeString(reqCtxID)
if err != nil {
return nil, 0, errors.New("invalid request context id")
}
if len(contextID) != contextIDLen {
return nil, 0, fmt.Errorf("invalid request context id:%s", reqCtxID)
}
txHash := contextID[0:32]
msgIndex := int64(binary.BigEndian.Uint64(contextID[32:40]))
return txHash, msgIndex, nil
}
// SplitRequestID splits the given contextID to contextID, batchCounter, requestHeight, batchRequestIndex
func splitRequestID(reqID string) (sdk.HexBytes, uint64, int64, int16, error) {
requestID, err := hex.DecodeString(reqID)
if err != nil {
return nil, 0, 0, 0, errors.New("invalid request id")
}
if len(requestID) != requestIDLen {
return nil, 0, 0, 0, errors.New("invalid request id")
}
reqCtxID := requestID[0:40]
batchCounter := binary.BigEndian.Uint64(requestID[40:48])
requestHeight := int64(binary.BigEndian.Uint64(requestID[48:56]))
batchRequestIndex := int16(binary.BigEndian.Uint16(requestID[56:]))
return reqCtxID, batchCounter, requestHeight, batchRequestIndex, nil
}