Skip to content

Commit

Permalink
!feat(api/gateway): return txdata from submitPFB if it is present (#2329
Browse files Browse the repository at this point in the history
)

Closes #2328
  • Loading branch information
walldiss authored and Wondertan committed Jun 6, 2023
1 parent 55e362b commit 03ff94a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
19 changes: 13 additions & 6 deletions api/gateway/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,26 @@ func (h *Handler) handleSubmitPFB(w http.ResponseWriter, r *http.Request) {
writeError(w, http.StatusBadRequest, submitPFBEndpoint, err)
return
}
fee := types.NewInt(req.Fee)

// perform request
txResp, err := h.state.SubmitPayForBlob(r.Context(), nID, data, fee, req.GasLimit)
if err != nil {
fee := types.NewInt(req.Fee)
txResp, txerr := h.state.SubmitPayForBlob(r.Context(), nID, data, fee, req.GasLimit)
if txerr != nil && txResp == nil {
// no tx data to return
writeError(w, http.StatusInternalServerError, submitPFBEndpoint, err)
return
}
resp, err := json.Marshal(txResp)

bs, err := json.Marshal(&txResp)
if err != nil {
writeError(w, http.StatusInternalServerError, submitPFBEndpoint, err)
return
}
_, err = w.Write(resp)

// 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
48 changes: 48 additions & 0 deletions api/gateway/state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package gateway

import (
"bytes"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

stateMock "github.com/celestiaorg/celestia-node/nodebuilder/state/mocks"
"github.com/celestiaorg/celestia-node/state"
)

func TestHandleSubmitPFB(t *testing.T) {
ctrl := gomock.NewController(t)
mock := stateMock.NewMockModule(ctrl)
handler := NewHandler(mock, nil, nil, nil)

t.Run("partial response", func(t *testing.T) {
txResponse := state.TxResponse{
Height: 1,
TxHash: "hash",
Codespace: "codespace",
Code: 1,
}
// simulate core-app err, since it is not exported
timedErr := errors.New("timed out waiting for tx to be included in a block")
mock.EXPECT().SubmitPayForData(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
Return(&txResponse, timedErr)

bs, err := json.Marshal(submitPFBRequest{})
require.NoError(t, err)
httpreq := httptest.NewRequest("GET", "/", bytes.NewReader(bs))
respRec := httptest.NewRecorder()
handler.handleSubmitPFB(respRec, httpreq)

var resp state.TxResponse
err = json.NewDecoder(respRec.Body).Decode(&resp)
require.NoError(t, err)

require.Equal(t, http.StatusPartialContent, respRec.Code)
require.Equal(t, resp, txResponse)
})
}

0 comments on commit 03ff94a

Please sign in to comment.