-
Notifications
You must be signed in to change notification settings - Fork 0
/
submit_transaction_async.go
138 lines (122 loc) · 4.58 KB
/
submit_transaction_async.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
package actions
import (
"context"
"net/http"
"github.com/shantanu-hashcash/go/protocols/aurora"
proto "github.com/shantanu-hashcash/go/protocols/hcnetcore"
hProblem "github.com/shantanu-hashcash/go/services/aurora/internal/render/problem"
"github.com/shantanu-hashcash/go/support/errors"
"github.com/shantanu-hashcash/go/support/log"
"github.com/shantanu-hashcash/go/support/render/problem"
)
type coreClient interface {
SubmitTx(ctx context.Context, rawTx string) (resp *proto.TXResponse, err error)
}
type AsyncSubmitTransactionHandler struct {
NetworkPassphrase string
DisableTxSub bool
ClientWithMetrics coreClient
CoreStateGetter
}
func (handler AsyncSubmitTransactionHandler) GetResource(_ HeaderWriter, r *http.Request) (interface{}, error) {
// TODO: Move the problem responses to a separate file as constants or a function.
logger := log.Ctx(r.Context())
if err := validateBodyType(r); err != nil {
return nil, err
}
raw, err := getString(r, "tx")
if err != nil {
return nil, err
}
if handler.DisableTxSub {
return nil, &problem.P{
Type: "transaction_submission_disabled",
Title: "Transaction Submission Disabled",
Status: http.StatusForbidden,
Detail: "Transaction submission has been disabled for Aurora. " +
"To enable it again, remove env variable DISABLE_TX_SUB.",
Extras: map[string]interface{}{
"envelope_xdr": raw,
},
}
}
info, err := extractEnvelopeInfo(raw, handler.NetworkPassphrase)
if err != nil {
return nil, &problem.P{
Type: "transaction_malformed",
Title: "Transaction Malformed",
Status: http.StatusBadRequest,
Detail: "Aurora 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,
"error": err,
},
}
}
coreState := handler.GetCoreState()
if !coreState.Synced {
return nil, hProblem.StaleHistory
}
resp, err := handler.ClientWithMetrics.SubmitTx(r.Context(), raw)
if err != nil {
return nil, &problem.P{
Type: "transaction_submission_failed",
Title: "Transaction Submission Failed",
Status: http.StatusInternalServerError,
Detail: "Could not submit transaction to hcnet-core. " +
"The `extras.error` field on this response contains further " +
"details. Descriptions of each code can be found at: " +
"https://developers.hcnet.org/api/errors/http-status-codes/aurora-specific/transaction-submission-async/transaction_submission_failed",
Extras: map[string]interface{}{
"envelope_xdr": raw,
"error": err,
},
}
}
if resp.IsException() {
logger.WithField("envelope_xdr", raw).WithError(errors.Errorf(resp.Exception)).Error("Transaction submission exception from hcnet-core")
return nil, &problem.P{
Type: "transaction_submission_exception",
Title: "Transaction Submission Exception",
Status: http.StatusInternalServerError,
Detail: "Received exception from hcnet-core." +
"The `extras.error` field on this response contains further " +
"details. Descriptions of each code can be found at: " +
"https://developers.hcnet.org/api/errors/http-status-codes/aurora-specific/transaction-submission-async/transaction_submission_exception",
Extras: map[string]interface{}{
"envelope_xdr": raw,
"error": resp.Exception,
},
}
}
switch resp.Status {
case proto.TXStatusError, proto.TXStatusPending, proto.TXStatusDuplicate, proto.TXStatusTryAgainLater:
response := aurora.AsyncTransactionSubmissionResponse{
TxStatus: resp.Status,
Hash: info.hash,
}
if resp.Status == proto.TXStatusError {
response.ErrorResultXDR = resp.Error
}
return response, nil
default:
logger.WithField("envelope_xdr", raw).WithError(errors.Errorf(resp.Error)).Error("Received invalid submission status from hcnet-core")
return nil, &problem.P{
Type: "transaction_submission_invalid_status",
Title: "Transaction Submission Invalid Status",
Status: http.StatusInternalServerError,
Detail: "Received invalid status from hcnet-core." +
"The `extras.error` field on this response contains further " +
"details. Descriptions of each code can be found at: " +
"https://developers.hcnet.org/api/errors/http-status-codes/aurora-specific/transaction-submission-async/transaction_submission_invalid_status",
Extras: map[string]interface{}{
"envelope_xdr": raw,
"error": resp.Error,
},
}
}
}