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
)

## Overview

Closes #2328
  • Loading branch information
walldiss authored Jun 6, 2023
1 parent 72757e3 commit a2685d4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
16 changes: 11 additions & 5 deletions api/gateway/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,23 @@ func (h *Handler) handleSubmitPFB(w http.ResponseWriter, r *http.Request) {
}

// perform request
txResp, err := h.state.SubmitPayForBlob(r.Context(), fee, req.GasLimit, []*apptypes.Blob{blob})
if err != nil {
txResp, txerr := h.state.SubmitPayForBlob(r.Context(), fee, req.GasLimit, []*apptypes.Blob{blob})
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().SubmitPayForBlob(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 a2685d4

Please sign in to comment.