forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit_transaction.go
185 lines (162 loc) · 5.02 KB
/
submit_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
package actions
import (
"context"
"encoding/hex"
"mime"
"net/http"
"github.com/pownieh/stellar_go/gxdr"
"github.com/pownieh/stellar_go/network"
"github.com/pownieh/stellar_go/protocols/horizon"
hProblem "github.com/pownieh/stellar_go/services/horizon/internal/render/problem"
"github.com/pownieh/stellar_go/services/horizon/internal/resourceadapter"
"github.com/pownieh/stellar_go/services/horizon/internal/txsub"
"github.com/pownieh/stellar_go/support/errors"
"github.com/pownieh/stellar_go/support/render/hal"
"github.com/pownieh/stellar_go/support/render/problem"
"github.com/pownieh/stellar_go/xdr"
)
type NetworkSubmitter interface {
Submit(ctx context.Context, rawTx string, envelope xdr.TransactionEnvelope, hash string) <-chan txsub.Result
}
type SubmitTransactionHandler struct {
Submitter NetworkSubmitter
NetworkPassphrase string
DisableTxSub bool
CoreStateGetter
}
type envelopeInfo struct {
hash string
innerHash string
raw string
parsed xdr.TransactionEnvelope
}
func extractEnvelopeInfo(raw string, passphrase string) (envelopeInfo, error) {
result := envelopeInfo{raw: raw}
if err := gxdr.ValidateTransactionEnvelope(raw, gxdr.DefaultMaxDepth); err != nil {
return result, err
}
err := xdr.SafeUnmarshalBase64(raw, &result.parsed)
if err != nil {
return result, err
}
var hash [32]byte
hash, err = network.HashTransactionInEnvelope(result.parsed, passphrase)
if err != nil {
return result, err
}
result.hash = hex.EncodeToString(hash[:])
if result.parsed.IsFeeBump() {
hash, err = network.HashTransaction(result.parsed.FeeBump.Tx.InnerTx.V1.Tx, passphrase)
if err != nil {
return result, err
}
result.innerHash = hex.EncodeToString(hash[:])
}
return result, nil
}
func (handler SubmitTransactionHandler) validateBodyType(r *http.Request) error {
c := r.Header.Get("Content-Type")
if c == "" {
return nil
}
mt, _, err := mime.ParseMediaType(c)
if err != nil {
return errors.Wrap(err, "Could not determine mime type")
}
if mt != "application/x-www-form-urlencoded" && mt != "multipart/form-data" {
return &hProblem.UnsupportedMediaType
}
return nil
}
func (handler SubmitTransactionHandler) response(r *http.Request, info envelopeInfo, result txsub.Result) (hal.Pageable, error) {
if result.Err == nil {
var resource horizon.Transaction
err := resourceadapter.PopulateTransaction(
r.Context(),
info.hash,
&resource,
result.Transaction,
)
return resource, err
}
if result.Err == txsub.ErrTimeout {
return nil, &hProblem.Timeout
}
if result.Err == txsub.ErrCanceled {
return nil, &hProblem.ClientDisconnected
}
switch err := result.Err.(type) {
case *txsub.FailedTransactionError:
rcr := horizon.TransactionResultCodes{}
resourceadapter.PopulateTransactionResultCodes(
r.Context(),
info.hash,
&rcr,
err,
)
return nil, &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://developers.stellar.org/api/errors/http-status-codes/horizon-specific/transaction-failed/",
Extras: map[string]interface{}{
"envelope_xdr": info.raw,
"result_xdr": err.ResultXDR,
"result_codes": rcr,
},
}
}
return nil, result.Err
}
func (handler SubmitTransactionHandler) GetResource(w HeaderWriter, r *http.Request) (interface{}, error) {
if err := handler.validateBodyType(r); err != nil {
return nil, err
}
if handler.DisableTxSub {
return nil, &problem.P{
Type: "transaction_submission_disabled",
Title: "Transaction Submission Disabled",
Status: http.StatusMethodNotAllowed,
Detail: "Transaction submission has been disabled for Horizon. " +
"To enable it again, remove env variable DISABLE_TX_SUB.",
Extras: map[string]interface{}{},
}
}
raw, err := getString(r, "tx")
if err != nil {
return nil, err
}
info, err := extractEnvelopeInfo(raw, handler.NetworkPassphrase)
if err != nil {
return nil, &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": raw,
},
}
}
coreState := handler.GetCoreState()
if !coreState.Synced {
return nil, hProblem.StaleHistory
}
submission := handler.Submitter.Submit(r.Context(), info.raw, info.parsed, info.hash)
select {
case result := <-submission:
return handler.response(r, info, result)
case <-r.Context().Done():
if r.Context().Err() == context.Canceled {
return nil, hProblem.ClientDisconnected
}
return nil, hProblem.Timeout
}
}