This repository has been archived by the owner on Jan 22, 2020. It is now read-only.
/
actions_transaction.go
223 lines (194 loc) · 5.74 KB
/
actions_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
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
package horizon
import (
"net/http"
"github.com/stellar/horizon/db2"
"github.com/stellar/horizon/db2/history"
"github.com/stellar/horizon/render/hal"
"github.com/stellar/horizon/render/problem"
"github.com/stellar/horizon/render/sse"
"github.com/stellar/horizon/resource"
"github.com/stellar/horizon/txsub"
)
// This file contains the actions:
//
// TransactionIndexAction: pages of transactions
// TransactionShowAction: single transaction by sequence, by hash or id
// TransactionIndexAction renders a page of ledger resources, identified by
// a normal page query.
type TransactionIndexAction struct {
Action
LedgerFilter int32
AccountFilter string
PagingParams db2.PageQuery
Records []history.Transaction
Page hal.Page
}
// JSON is a method for actions.JSON
func (action *TransactionIndexAction) JSON() {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.ValidateCursorWithinHistory,
action.loadRecords,
action.loadPage,
func() {
hal.Render(action.W, action.Page)
},
)
}
// SSE is a method for actions.SSE
func (action *TransactionIndexAction) SSE(stream sse.Stream) {
action.Setup(
action.EnsureHistoryFreshness,
action.loadParams,
action.ValidateCursorWithinHistory,
)
action.Do(
action.loadRecords,
func() {
stream.SetLimit(int(action.PagingParams.Limit))
records := action.Records[stream.SentCount():]
for _, record := range records {
var res resource.Transaction
res.Populate(action.Ctx, record)
stream.Send(sse.Event{ID: res.PagingToken(), Data: res})
}
},
)
}
func (action *TransactionIndexAction) loadParams() {
action.ValidateCursorAsDefault()
action.AccountFilter = action.GetString("account_id")
action.LedgerFilter = action.GetInt32("ledger_id")
action.PagingParams = action.GetPageQuery()
}
func (action *TransactionIndexAction) loadRecords() {
q := action.HistoryQ()
txs := q.Transactions()
switch {
case action.AccountFilter != "":
txs.ForAccount(action.AccountFilter)
case action.LedgerFilter > 0:
txs.ForLedger(action.LedgerFilter)
}
action.Err = txs.Page(action.PagingParams).Select(&action.Records)
}
func (action *TransactionIndexAction) loadPage() {
for _, record := range action.Records {
var res resource.Transaction
res.Populate(action.Ctx, record)
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()
}
// TransactionShowAction renders a ledger found by its sequence number.
type TransactionShowAction struct {
Action
Hash string
Record history.Transaction
Resource resource.Transaction
}
func (action *TransactionShowAction) loadParams() {
action.Hash = action.GetString("id")
}
func (action *TransactionShowAction) loadRecord() {
action.Err = action.HistoryQ().TransactionByHash(&action.Record, action.Hash)
}
func (action *TransactionShowAction) loadResource() {
action.Resource.Populate(action.Ctx, action.Record)
}
// JSON is a method for actions.JSON
func (action *TransactionShowAction) JSON() {
action.Do(
action.EnsureHistoryFreshness,
action.loadParams,
action.loadRecord,
action.loadResource,
func() { hal.Render(action.W, action.Resource) },
)
}
// TransactionCreateAction submits a transaction to the stellar-core network
// on behalf of the requesting client.
type TransactionCreateAction struct {
Action
TX string
Result txsub.Result
Resource resource.TransactionSuccess
}
// JSON format action handler
func (action *TransactionCreateAction) JSON() {
action.Do(
action.loadTX,
action.loadResult,
action.loadResource,
func() {
hal.Render(action.W, action.Resource)
})
}
func (action *TransactionCreateAction) loadTX() {
action.ValidateBodyType()
action.TX = action.GetString("tx")
}
func (action *TransactionCreateAction) loadResult() {
submission := action.App.submitter.Submit(action.Ctx, action.TX)
select {
case result := <-submission:
action.Result = result
case <-action.Ctx.Done():
action.Err = &problem.Timeout
}
}
func (action *TransactionCreateAction) loadResource() {
if action.Result.Err == nil {
action.Resource.Populate(action.Ctx, action.Result)
return
}
if action.Result.Err == txsub.ErrTimeout {
action.Err = &problem.Timeout
return
}
if action.Result.Err == txsub.ErrCanceled {
action.Err = &problem.Timeout
return
}
switch err := action.Result.Err.(type) {
case *txsub.FailedTransactionError:
rcr := resource.TransactionResultCodes{}
rcr.Populate(action.Ctx, err)
action.Err = &problem.P{
Type: "transaction_failed",
Title: "Transaction Failed",
Status: http.StatusBadRequest,
Detail: "The transaction failed when submitted to the stellar network. " +
"The `extras.result_codes` field on this response contains further " +
"details. Descriptions of each code can be found at: " +
"https://www.stellar.org/developers/learn/concepts/list-of-operations.html",
Extras: map[string]interface{}{
"envelope_xdr": action.Result.EnvelopeXDR,
"result_xdr": err.ResultXDR,
"result_codes": rcr,
},
}
case *txsub.MalformedTransactionError:
action.Err = &problem.P{
Type: "transaction_malformed",
Title: "Transaction Malformed",
Status: http.StatusBadRequest,
Detail: "Horizon could not decode the transaction envelope in this " +
"request. A transaction should be an XDR TransactionEnvelope struct " +
"encoded using base64. The envelope read from this request is " +
"echoed in the `extras.envelope_xdr` field of this response for your " +
"convenience.",
Extras: map[string]interface{}{
"envelope_xdr": err.EnvelopeXDR,
},
}
default:
action.Err = err
}
}