From bb36bdac9c743155d77817ce5172948808e0dc63 Mon Sep 17 00:00:00 2001 From: Ryan Schneider Date: Thu, 27 Jun 2024 10:37:25 -0700 Subject: [PATCH] Handle CallWithFlashbotsSignature returning non-JSONRPC error (#28) * Handle CallWithFlashbotsSignature returning non-JSONRPC error * fixup: error if either JSONRPC id or jsonrpc field is missing Co-authored-by: Chris Hager --------- Co-authored-by: Chris Hager --- flashbotsrpc.go | 5 +++++ flashbotsrpc_test.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/flashbotsrpc.go b/flashbotsrpc.go index 7b67058..45e00bf 100644 --- a/flashbotsrpc.go +++ b/flashbotsrpc.go @@ -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) } diff --git a/flashbotsrpc_test.go b/flashbotsrpc_test.go index 92e1853..67e1862 100644 --- a/flashbotsrpc_test.go +++ b/flashbotsrpc_test.go @@ -10,6 +10,7 @@ import ( "io" "math/big" "net/http" + "net/http/httptest" "strconv" "testing" "time" @@ -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)) +}