Skip to content

Commit

Permalink
Handle CallWithFlashbotsSignature returning non-JSONRPC error (#28)
Browse files Browse the repository at this point in the history
* Handle CallWithFlashbotsSignature returning non-JSONRPC error

* fixup: error if either JSONRPC id or jsonrpc field is missing

Co-authored-by: Chris Hager <[email protected]>

---------

Co-authored-by: Chris Hager <[email protected]>
  • Loading branch information
ryanschneider and metachris authored Jun 27, 2024
1 parent aec8186 commit bb36bda
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions flashbotsrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ func (rpc *FlashbotsRPC) CallWithFlashbotsSignature(method string, privKey *ecds
return nil, err
}

if resp.ID != request.ID || resp.JSONRPC != request.JSONRPC {
// this means we got back JSON but not a valid JSONRPC response
return nil, fmt.Errorf("%w: invalid JSONRPC response (HTTP status code: %d)", ErrRelayErrorResponse, response.StatusCode)
}

if resp.Error != nil {
return nil, fmt.Errorf("%w: %s", ErrRelayErrorResponse, (*resp).Error.Message)
}
Expand Down
18 changes: 18 additions & 0 deletions flashbotsrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"io"
"math/big"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
Expand Down Expand Up @@ -1304,3 +1305,20 @@ func newBigInt(s string) big.Int {
i, _ := new(big.Int).SetString(s, 10)
return *i
}

func TestCallWithFlashbotsSignature(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
_, err := w.Write([]byte(`{"something":"failed"}`))
require.NoError(t, err)
}))
defer server.Close()
rpc := NewFlashbotsRPC(server.URL)

key, err := crypto.GenerateKey()
require.NoError(t, err)
response, err := rpc.CallWithFlashbotsSignature("mock_Method", key, 42)
require.Nil(t, response)
require.Error(t, err, "500 should return an error")
require.True(t, errors.Is(err, ErrRelayErrorResponse))
}

0 comments on commit bb36bda

Please sign in to comment.