-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
187 lines (155 loc) · 5.41 KB
/
transaction.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
package actions
import (
"context"
"net/http"
"github.com/shantanu-hashcash/go/protocols/aurora"
auroraContext "github.com/shantanu-hashcash/go/services/aurora/internal/context"
"github.com/shantanu-hashcash/go/services/aurora/internal/db2"
"github.com/shantanu-hashcash/go/services/aurora/internal/db2/history"
"github.com/shantanu-hashcash/go/services/aurora/internal/ledger"
"github.com/shantanu-hashcash/go/services/aurora/internal/resourceadapter"
"github.com/shantanu-hashcash/go/support/errors"
"github.com/shantanu-hashcash/go/support/render/hal"
supportProblem "github.com/shantanu-hashcash/go/support/render/problem"
"github.com/shantanu-hashcash/go/xdr"
)
// TransactionQuery query struct for transactions/id end-point
type TransactionQuery struct {
TransactionHash string `schema:"tx_id" valid:"transactionHash,optional"`
}
// GetTransactionByHashHandler is the action handler for the end-point returning a transaction.
type GetTransactionByHashHandler struct {
SkipTxMeta bool
}
// GetResource returns a transaction page.
func (handler GetTransactionByHashHandler) GetResource(w HeaderWriter, r *http.Request) (interface{}, error) {
ctx := r.Context()
qp := TransactionQuery{}
err := getParams(&qp, r)
if err != nil {
return nil, err
}
historyQ, err := auroraContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
var (
record history.Transaction
resource aurora.Transaction
)
err = historyQ.TransactionByHash(ctx, &record, qp.TransactionHash)
if err != nil {
return resource, errors.Wrap(err, "loading transaction record")
}
if err = resourceadapter.PopulateTransaction(ctx, qp.TransactionHash, &resource, record, handler.SkipTxMeta); err != nil {
return resource, errors.Wrap(err, "could not populate transaction")
}
return resource, nil
}
// TransactionsQuery query struct for transactions end-points
type TransactionsQuery struct {
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"`
IncludeFailedTransactions bool `schema:"include_failed" valid:"-"`
LedgerID uint32 `schema:"ledger_id" valid:"-"`
}
// Validate runs extra validations on query parameters
func (qp TransactionsQuery) Validate() error {
filters, err := countNonEmpty(
qp.AccountID,
qp.ClaimableBalanceID,
qp.LiquidityPoolID,
qp.LedgerID,
)
if err != nil {
return supportProblem.BadRequest
}
if filters > 1 {
return supportProblem.MakeInvalidFieldProblem(
"filters",
errors.New("Use a single filter for transaction, you can only use one of account_id, claimable_balance_id or ledger_id"),
)
}
return nil
}
// GetTransactionsHandler is the action handler for all end-points returning a list of transactions.
type GetTransactionsHandler struct {
LedgerState *ledger.State
SkipTxMeta bool
}
// GetResourcePage returns a page of transactions.
func (handler GetTransactionsHandler) 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 := TransactionsQuery{}
err = getParams(&qp, r)
if err != nil {
return nil, err
}
historyQ, err := auroraContext.HistoryQFromRequest(r)
if err != nil {
return nil, err
}
records, err := loadTransactionRecords(ctx, historyQ, qp, pq)
if err != nil {
return nil, errors.Wrap(err, "loading transaction records")
}
var response []hal.Pageable
for _, record := range records {
var res aurora.Transaction
err = resourceadapter.PopulateTransaction(ctx, record.TransactionHash, &res, record, handler.SkipTxMeta)
if err != nil {
return nil, errors.Wrap(err, "could not populate transaction")
}
response = append(response, res)
}
return response, nil
}
// loadTransactionRecords returns a slice of transaction records of an
// account/ledger identified by accountID/ledgerID based on pq and
// includeFailedTx.
func loadTransactionRecords(ctx context.Context, hq *history.Q, qp TransactionsQuery, pq db2.PageQuery) ([]history.Transaction, error) {
var records []history.Transaction
txs := hq.Transactions()
switch {
case qp.AccountID != "":
txs.ForAccount(ctx, qp.AccountID)
case qp.ClaimableBalanceID != "":
txs.ForClaimableBalance(ctx, qp.ClaimableBalanceID)
case qp.LiquidityPoolID != "":
txs.ForLiquidityPool(ctx, qp.LiquidityPoolID)
case qp.LedgerID > 0:
txs.ForLedger(ctx, int32(qp.LedgerID))
}
if qp.IncludeFailedTransactions {
txs.IncludeFailed()
}
err := txs.Page(pq).Select(ctx, &records)
if err != nil {
return nil, errors.Wrap(err, "executing transaction records query")
}
for _, t := range records {
if !qp.IncludeFailedTransactions {
if !t.Successful {
return nil, errors.Errorf("Corrupted data! `include_failed=false` but returned transaction is failed: %s", t.TransactionHash)
}
var resultXDR xdr.TransactionResult
err = xdr.SafeUnmarshalBase64(t.TxResult, &resultXDR)
if err != nil {
return nil, errors.Wrap(err, "unmarshaling tx result")
}
if !resultXDR.Successful() {
return nil, errors.Errorf("Corrupted data! `include_failed=false` but returned transaction is failed: %s %s", t.TransactionHash, t.TxResult)
}
}
}
return records, nil
}