-
Notifications
You must be signed in to change notification settings - Fork 496
/
operation.go
231 lines (196 loc) · 6.21 KB
/
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
package actions
import (
"context"
"fmt"
"net/http"
horizonContext "github.com/stellar/go/services/horizon/internal/context"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/ledger"
"github.com/stellar/go/services/horizon/internal/render/problem"
"github.com/stellar/go/services/horizon/internal/resourceadapter"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/render/hal"
supportProblem "github.com/stellar/go/support/render/problem"
"github.com/stellar/go/toid"
)
// Joinable query struct for join query parameter
type Joinable struct {
Join string `schema:"join" valid:"in(transactions)~Accepted values: transactions,optional"`
}
// IncludeTransactions returns extra fields to include in the response
func (qp Joinable) IncludeTransactions() bool {
return qp.Join == "transactions"
}
// OperationsQuery query struct for operations end-points
type OperationsQuery struct {
Joinable `valid:"optional"`
AccountID string `schema:"account_id" valid:"accountID,optional"`
ClaimableBalanceID string `schema:"claimable_balance_id" valid:"claimableBalanceID,optional"`
LiquidityPoolID string `schema:"liquidity_pool_id" valid:"sha256,optional"`
TransactionHash string `schema:"tx_id" valid:"transactionHash,optional"`
IncludeFailedTransactions bool `schema:"include_failed" valid:"-"`
LedgerID uint32 `schema:"ledger_id" valid:"-"`
}
// Validate runs extra validations on query parameters
func (qp OperationsQuery) Validate() error {
filters, err := countNonEmpty(
qp.AccountID,
qp.ClaimableBalanceID,
qp.LiquidityPoolID,
qp.LedgerID,
qp.TransactionHash,
)
if err != nil {
return supportProblem.BadRequest
}
if filters > 1 {
return supportProblem.MakeInvalidFieldProblem(
"filters",
errors.New("Use a single filter for operations, you can only use one of tx_id, account_id or ledger_id"),
)
}
return nil
}
// GetOperationsHandler is the action handler for all end-points returning a list of operations.
type GetOperationsHandler struct {
LedgerState *ledger.State
OnlyPayments bool
SkipTxMeta bool
}
// GetResourcePage returns a page of operations.
func (handler GetOperationsHandler) GetResourcePage(w HeaderWriter, r *http.Request) ([]hal.Pageable, error) {
ctx := r.Context()
pq, err := GetPageQuery(handler.LedgerState, r)
if err != nil {
return nil, err
}
err = validateCursorWithinHistory(handler.LedgerState, pq)
if err != nil {
return nil, err
}
qp := OperationsQuery{}
err = getParams(&qp, r)
if err != nil {
return nil, err
}
historyQ, err := horizonContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
query := historyQ.Operations()
switch {
case qp.AccountID != "":
query.ForAccount(ctx, qp.AccountID)
case qp.ClaimableBalanceID != "":
query.ForClaimableBalance(ctx, qp.ClaimableBalanceID)
case qp.LiquidityPoolID != "":
query.ForLiquidityPool(ctx, qp.LiquidityPoolID)
case qp.LedgerID > 0:
query.ForLedger(ctx, int32(qp.LedgerID))
case qp.TransactionHash != "":
query.ForTransaction(ctx, qp.TransactionHash)
}
// When querying operations for transaction return both successful
// and failed operations. We assume that because the user is querying
// this specific transactions, they knows its status.
if qp.TransactionHash != "" || qp.IncludeFailedTransactions {
query.IncludeFailed()
}
if qp.IncludeTransactions() {
query.IncludeTransactions()
}
if handler.OnlyPayments {
query.OnlyPayments()
}
ops, txs, err := query.Page(pq).Fetch(ctx)
if err != nil {
return nil, err
}
return buildOperationsPage(ctx, historyQ, ops, txs, qp.IncludeTransactions(), handler.SkipTxMeta)
}
// GetOperationByIDHandler is the action handler for all end-points returning a list of operations.
type GetOperationByIDHandler struct {
LedgerState *ledger.State
SkipTxMeta bool
}
// OperationQuery query struct for operation/id end-point
type OperationQuery struct {
LedgerState *ledger.State `valid:"-"`
Joinable `valid:"optional"`
ID uint64 `schema:"id" valid:"-"`
}
// Validate runs extra validations on query parameters
func (qp OperationQuery) Validate() error {
parsed := toid.Parse(int64(qp.ID))
if parsed.LedgerSequence < qp.LedgerState.CurrentStatus().HistoryElder {
return problem.BeforeHistory
}
return nil
}
// GetResource returns an operation page.
func (handler GetOperationByIDHandler) GetResource(w HeaderWriter, r *http.Request) (interface{}, error) {
ctx := r.Context()
qp := OperationQuery{
LedgerState: handler.LedgerState,
}
err := getParams(&qp, r)
if err != nil {
return nil, err
}
historyQ, err := horizonContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
op, tx, err := historyQ.OperationByID(ctx, qp.IncludeTransactions(), int64(qp.ID))
if err != nil {
return nil, err
}
var ledger history.Ledger
err = historyQ.LedgerBySequence(ctx, &ledger, op.LedgerSequence())
if err != nil {
return nil, err
}
return resourceadapter.NewOperation(
ctx,
op,
op.TransactionHash,
tx,
ledger,
handler.SkipTxMeta,
)
}
func buildOperationsPage(ctx context.Context, historyQ *history.Q, operations []history.Operation, transactions []history.Transaction, includeTransactions bool, skipTxMeta bool) ([]hal.Pageable, error) {
ledgerCache := history.LedgerCache{}
for _, record := range operations {
ledgerCache.Queue(record.LedgerSequence())
}
if err := ledgerCache.Load(ctx, historyQ); err != nil {
return nil, errors.Wrap(err, "failed to load ledger batch")
}
var response []hal.Pageable
for i, operationRecord := range operations {
ledger, found := ledgerCache.Records[operationRecord.LedgerSequence()]
if !found {
msg := fmt.Sprintf("could not find ledger data for sequence %d", operationRecord.LedgerSequence())
return nil, errors.New(msg)
}
var transactionRecord *history.Transaction
if includeTransactions {
transactionRecord = &transactions[i]
}
var res hal.Pageable
res, err := resourceadapter.NewOperation(
ctx,
operationRecord,
operationRecord.TransactionHash,
transactionRecord,
ledger,
skipTxMeta,
)
if err != nil {
return nil, err
}
response = append(response, res)
}
return response, nil
}