-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp_debug.go
182 lines (149 loc) · 5.36 KB
/
http_debug.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
package gsi
import (
"bytes"
"encoding/json"
"io"
"log/slog"
"net/http"
"cosmossdk.io/core/transaction"
"cosmossdk.io/server/v2/appmanager"
banktypes "cosmossdk.io/x/bank/types"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/gorilla/mux"
)
type debugHandler struct {
log *slog.Logger
txCodec transaction.Codec[transaction.Tx]
codec codec.Codec
am appmanager.AppManager[transaction.Tx]
txBuf *SDKTxBuf
}
func setDebugRoutes(log *slog.Logger, cfg HTTPServerConfig, r *mux.Router) {
h := debugHandler{
log: log,
txCodec: cfg.TxCodec,
codec: cfg.Codec,
am: cfg.AppManager,
txBuf: cfg.TxBuffer,
}
r.HandleFunc("/debug/submit_tx", h.HandleSubmitTx).Methods("POST")
r.HandleFunc("/debug/simulate_tx", h.HandleSimulateTx).Methods("POST")
r.HandleFunc("/debug/pending_txs", h.HandlePendingTxs).Methods("GET")
r.HandleFunc("/debug/accounts/{id}/balance", h.HandleAccountBalance).Methods("GET")
}
func (h debugHandler) HandleSubmitTx(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
b, err := io.ReadAll(req.Body)
if err != nil {
h.log.Warn("Failed to read request body", "route", "submit_tx", "err", err)
http.Error(w, "failed to read request body", http.StatusBadRequest)
return
}
tx, err := h.txCodec.DecodeJSON(b)
if err != nil {
h.log.Warn("Failed to decode transaction", "route", "submit_tx", "err", err)
http.Error(w, "failed to decode transaction", http.StatusBadRequest)
return
}
// TODO: should this have a configurable timeout?
// Probably fine to skip since this is a "debug" endpoint for now,
// but if this gets promoted to a non-debug route,
// it should have a timeout.
ctx := req.Context()
res, err := h.am.ValidateTx(ctx, tx)
if err != nil {
// ValidateTx should only return an error at this level,
// if it failed to get state from the store.
h.log.Warn("Error attempting to validate transaction", "route", "submit_tx", "err", err)
http.Error(w, "internal error while attempting to validate transaction", http.StatusInternalServerError)
return
}
if res.Error != nil {
// This is fine from the server's perspective, no need to log.
http.Error(w, "transaction validation failed: "+res.Error.Error(), http.StatusBadRequest)
return
}
// If it passed basic validation, then we can attempt to add it to the buffer.
if err := h.txBuf.AddTx(ctx, tx); err != nil {
// We could potentially check if it is a TxInvalidError here
// and adjust the status code,
// but since this is a debug endpoint, we'll ignore the type.
http.Error(w, "failed to add transaction to buffer: "+err.Error(), http.StatusBadRequest)
return
}
if err := json.NewEncoder(w).Encode(res); err != nil {
h.log.Warn("Failed to encode submit_tx result", "err", err)
}
}
func (h debugHandler) HandleSimulateTx(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
b, err := io.ReadAll(req.Body)
if err != nil {
h.log.Warn("Failed to read request body", "route", "simulate_tx", "err", err)
http.Error(w, "failed to read request body", http.StatusBadRequest)
return
}
tx, err := h.txCodec.DecodeJSON(b)
if err != nil {
h.log.Warn("Failed to decode transaction", "route", "simulate_tx", "err", err)
http.Error(w, "failed to decode transaction", http.StatusBadRequest)
return
}
// TODO: should this have a configurable timeout?
// Probably fine to skip since this is a "debug" endpoint for now,
// but if this gets promoted to a non-debug route,
// it should have a timeout.
ctx := req.Context()
res, _, err := h.am.Simulate(ctx, tx)
if err != nil {
// Simulate should only return an error at this level,
// if it failed to get state from the store.
h.log.Warn("Error attempting to simulate transaction", "route", "simulate_tx", "err", err)
http.Error(w, "internal error while attempting to simulate transaction", http.StatusInternalServerError)
return
}
if res.Error != nil {
// This is fine from the server's perspective, no need to log.
http.Error(w, "transaction simulation failed: "+res.Error.Error(), http.StatusBadRequest)
return
}
if err := json.NewEncoder(w).Encode(res); err != nil {
h.log.Warn("Failed to encode simulate_tx result", "err", err)
}
}
func (h debugHandler) HandlePendingTxs(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
txs := h.txBuf.Buffered(req.Context(), nil)
encodedTxs := make([]json.RawMessage, len(txs))
for i, tx := range txs {
b, err := json.Marshal(tx)
if err != nil {
http.Error(w, "failed to encode transaction: "+err.Error(), http.StatusInternalServerError)
return
}
encodedTxs[i] = json.RawMessage(b)
}
if err := json.NewEncoder(w).Encode(encodedTxs); err != nil {
h.log.Warn("Failed to encode pending transactions", "err", err)
}
}
func (h debugHandler) HandleAccountBalance(w http.ResponseWriter, r *http.Request) {
accountID := mux.Vars(r)["id"]
msg, err := h.am.Query(r.Context(), 0, &banktypes.QueryBalanceRequest{
Address: accountID,
Denom: "stake",
})
if err != nil {
h.log.Warn("Failed to query account balance", "id", accountID, "err", err)
http.Error(w, "query failed", http.StatusBadRequest)
return
}
b, err := h.codec.MarshalJSON(msg)
if err != nil {
http.Error(w, "failed to encode response: "+err.Error(), http.StatusInternalServerError)
return
}
if _, err := io.Copy(w, bytes.NewReader(b)); err != nil {
h.log.Warn("Failed to encode account balance response", "err", err)
}
}