-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions_operation.go
348 lines (298 loc) · 9.21 KB
/
actions_operation.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package horizon
import (
"fmt"
"gitlab.com/tokend/horizon/db2"
"strconv"
"time"
"github.com/pkg/errors"
"gitlab.com/tokend/go/xdr"
"gitlab.com/tokend/horizon/db2/history"
"gitlab.com/tokend/horizon/ledger"
"gitlab.com/tokend/horizon/render/hal"
"gitlab.com/tokend/horizon/render/problem"
"gitlab.com/tokend/horizon/render/sse"
"gitlab.com/tokend/horizon/resource"
"gitlab.com/tokend/horizon/toid"
)
// This file contains the actions:
//
// OperationIndexAction: pages of operations
// OperationShowAction: single operation by id
// OperationIndexAction renders a page of operations resources, identified by
// a normal page query and optionally filtered by an account, ledger, or
// transaction.
type OperationIndexAction struct {
Action
Types []xdr.OperationType
LedgerFilter int32
AccountFilter string
AccountTypeFilter int32
BalanceFilter string
AssetFilter string
ExchangeFilter string
TransactionFilter string
CompletedOnlyFilter bool
SkipCanceled bool
PendingOnlyFilter bool
// ReferenceFilter substring
ReferenceFilter string
SinceFilter *time.Time
ToFilter *time.Time
PagingParams db2.PageQuery
Records []history.Operation
Participants map[int64]*history.OperationParticipants
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *OperationIndexAction) JSON() {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.checkAllowed,
action.ValidateCursorWithinHistory,
action.loadRecords,
action.loadParticipants,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
})
}
// SSE is a method for actions.SSE
func (action *OperationIndexAction) SSE(stream sse.Stream) {
action.Setup(
action.EnsureHistoryFreshness,
action.loadParams,
action.checkAllowed,
action.ValidateCursorWithinHistory,
)
action.Do(
func() {
// we will reuse this variable in sse, so re-initializing is required
action.Records = []history.Operation{}
},
action.loadRecords,
action.loadParticipants,
func() {
records := action.Records[:]
for _, record := range records {
opParticipants := action.Participants[record.ID]
res, err := resource.NewOperation(action.Ctx, record, opParticipants.Participants)
if err != nil {
stream.Err(action.Err)
return
}
stream.Send(sse.Event{
ID: res.PagingToken(),
Data: res,
})
action.PagingParams.Cursor = res.PagingToken()
}
})
}
func (action *OperationIndexAction) loadParams() {
action.ValidateCursorAsDefault()
action.AccountFilter = action.GetString("account_id")
action.AccountTypeFilter = action.GetInt32("account_type")
action.BalanceFilter = action.GetString("balance_id")
action.AssetFilter = action.GetString("asset")
action.TransactionFilter = action.GetString("tx_id")
action.ReferenceFilter = action.GetString("reference")
action.SinceFilter = action.TryGetTime("since")
action.ToFilter = action.TryGetTime("to")
action.CompletedOnlyFilter = action.GetBoolOrDefault("completed_only", true)
action.SkipCanceled = action.GetBoolOrDefault("skip_canceled", true)
action.PendingOnlyFilter = action.GetBool("pending_only")
if action.CompletedOnlyFilter && action.PendingOnlyFilter {
action.SetInvalidField("pending_only", errors.New("completed_only and pending_only filters cannot both be set"))
return
}
var err error
opTypeStr := action.GetString("operation_type")
opType := int64(0)
if opTypeStr != "" {
opType, err = strconv.ParseInt(opTypeStr, 10, 64)
if err != nil {
action.SetInvalidField("operation_type", err)
}
}
if opTypeStr != "" && len(action.Types) > 0 {
// operations were set already, so action is limited to some types
// checking if specified is one of them
opType = func() int64 {
for _, t := range action.Types {
if int64(t) == opType {
return opType
}
}
return 0
}()
if opType == 0 {
action.SetInvalidField(
"operation_type", errors.New("invalid in some way"))
}
}
if opTypeStr != "" {
action.Types = []xdr.OperationType{xdr.OperationType(opType)}
}
action.PagingParams = action.GetPageQuery()
action.Page.Filters = map[string]string{
"account_id": action.AccountFilter,
"account_type": fmt.Sprintf("%d", action.AccountTypeFilter),
"balance_id": action.BalanceFilter,
"asset": action.AssetFilter,
"tx_id": action.TransactionFilter,
"reference": action.ReferenceFilter,
"since": action.GetString("since"),
"to": action.GetString("to"),
"completed_only": action.GetString("completed_only"),
"skip_canceled": action.GetString("skip_canceled"),
"pending_only": action.GetString("pending_only"),
}
if action.SinceFilter != nil {
action.Page.Filters["since"] = action.SinceFilter.Format(time.RFC3339)
}
if action.ToFilter != nil {
action.Page.Filters["to"] = action.ToFilter.Format(time.RFC3339)
}
if opType != 0 {
action.Page.Filters["operation_type"] = fmt.Sprintf("%d", opType)
}
}
func (action *OperationIndexAction) loadRecords() {
ops := action.HistoryQ().Operations().WithoutCancelingManagerOffer().WithoutExternallyFullyMatched()
if len(action.Types) > 0 {
ops.ForTypes(action.Types)
}
if action.SkipCanceled {
ops = ops.WithoutCanceled()
}
if action.AccountFilter != "" || action.AccountTypeFilter != 0 {
ops.JoinOnAccount()
}
if action.AccountFilter != "" {
ops.ForAccount(action.AccountFilter)
}
if action.AccountTypeFilter != 0 {
ops.ForAccountType(action.AccountTypeFilter)
}
if action.BalanceFilter != "" {
ops.ForBalance(action.BalanceFilter)
}
if action.AssetFilter != "" || action.ExchangeFilter != "" {
ops.JoinOnBalance()
}
if action.AssetFilter != "" {
ops.ForAsset(action.AssetFilter)
}
if action.TransactionFilter != "" {
ops.ForTx(action.TransactionFilter)
}
if action.ReferenceFilter != "" {
ops.ForReference(action.ReferenceFilter)
}
if action.SinceFilter != nil {
ops.Since(*action.SinceFilter)
}
if action.ToFilter != nil {
ops.To(*action.ToFilter)
}
if action.CompletedOnlyFilter {
ops.CompletedOnly()
}
if action.PendingOnlyFilter {
ops.PendingOnly()
}
err := ops.Page(action.PagingParams).Select(&action.Records)
if err != nil {
action.Log.WithError(err).Error("Failed to get operations")
action.Err = &problem.ServerError
return
}
}
func (action *OperationIndexAction) loadParticipants() {
// initializing our operation -> participants map
action.Participants = map[int64]*history.OperationParticipants{}
for _, operation := range action.Records {
action.Participants[operation.ID] = &history.OperationParticipants{
operation.Type,
[]*history.Participant{},
}
}
action.LoadParticipants(action.AccountFilter, action.Participants)
}
func (action *OperationIndexAction) loadPage() {
for _, record := range action.Records {
var res hal.Pageable
opParticipants := action.Participants[record.ID]
res, err := resource.NewOperation(action.Ctx, record, opParticipants.Participants)
if err != nil {
action.Log.WithError(err).Error("failed to populate operation")
action.Err = &problem.ServerError
return
}
// add operation 2 time if it's within one account
if len(opParticipants.Participants) == 2 && record.Type != xdr.OperationTypeManageOffer && record.Type != xdr.OperationTypeCreateIssuanceRequest &&
opParticipants.Participants[0].AccountID == opParticipants.Participants[1].AccountID {
action.Page.Add(res)
}
action.Page.Add(res)
}
action.Page.BaseURL = action.BaseURL()
action.Page.BasePath = action.Path()
action.Page.Limit = action.PagingParams.Limit
action.Page.Cursor = action.PagingParams.Cursor
action.Page.Order = action.PagingParams.Order
action.Page.PopulateLinks()
}
func (action *OperationIndexAction) checkAllowed() {
action.IsAllowed(action.AccountFilter)
}
// OperationShowAction renders a ledger found by its sequence number.
type OperationShowAction struct {
Action
ID int64
Record history.Operation
Participants map[int64]*history.OperationParticipants
Resource interface{}
}
func (action *OperationShowAction) loadParams() {
action.ID = action.GetInt64("id")
}
func (action *OperationShowAction) loadRecord() {
err := action.HistoryQ().OperationByID(&action.Record, action.ID)
if err != nil {
action.Log.WithError(err).Error("failed to load record")
action.Err = &problem.ServerError
return
}
action.Participants = map[int64]*history.OperationParticipants{
action.ID: {},
}
action.LoadParticipants("", action.Participants)
}
func (action *OperationShowAction) loadResource() {
action.Resource, action.Err = resource.NewOperation(action.Ctx, action.Record, action.Participants[action.Record.ID].Participants)
}
// JSON is a method for actions.JSON
func (action *OperationShowAction) JSON() {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.checkAllowed,
action.verifyWithinHistory,
action.loadRecord,
action.loadResource,
)
action.Do(func() {
hal.Render(action.W, action.Resource)
})
}
func (action *OperationShowAction) verifyWithinHistory() {
parsed := toid.Parse(action.ID)
if parsed.LedgerSequence < ledger.CurrentState().History.OldestOnStart {
action.Err = &problem.BeforeHistory
}
}
func (action *OperationShowAction) checkAllowed() {
action.IsAllowed("")
}