Skip to content

Commit

Permalink
fix(api/gateway): Handle err from tx properly to avoid panic (#2393)
Browse files Browse the repository at this point in the history
Fixes a panic that can occur if txResp is nil and also returns actual
error in the case of nil txResp.

Reported by @tuxcanfly
  • Loading branch information
renaynay committed Jun 23, 2023
1 parent f32c903 commit 8b28ad7
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions api/gateway/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,15 @@ func (h *Handler) handleSubmitPFB(w http.ResponseWriter, r *http.Request) {
}

// perform request
txResp, txerr := h.state.SubmitPayForBlob(r.Context(), fee, req.GasLimit, []*blob.Blob{constructedBlob})
if txerr != nil && txResp == nil {
// no tx data to return
writeError(w, http.StatusInternalServerError, submitPFBEndpoint, err)
txResp, err := h.state.SubmitPayForBlob(r.Context(), fee, req.GasLimit, []*blob.Blob{constructedBlob})
if err != nil {
if txResp == nil {
// no tx data to return
writeError(w, http.StatusBadRequest, submitPFBEndpoint, err)
return
}
// if error returned, change status from 200 to 206
w.WriteHeader(http.StatusPartialContent)
}

bs, err := json.Marshal(&txResp)
Expand All @@ -162,10 +167,6 @@ func (h *Handler) handleSubmitPFB(w http.ResponseWriter, r *http.Request) {
return
}

// if error returned, change status from 200 to 206
if txerr != nil {
w.WriteHeader(http.StatusPartialContent)
}
_, err = w.Write(bs)
if err != nil {
log.Errorw("writing response", "endpoint", submitPFBEndpoint, "err", err)
Expand Down

0 comments on commit 8b28ad7

Please sign in to comment.