Skip to content

Commit

Permalink
Small refactoring and added UTs ro RPC (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
begmaroman authored Jan 4, 2024
1 parent 6073290 commit 133e46a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
22 changes: 1 addition & 21 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,11 @@ func JSONRPCCall(url, method string, params ...interface{}) (Response, error) {
// the provided method and parameters, which is compatible with the Ethereum
// JSON RPC Server.
func JSONRPCCallWithContext(ctx context.Context, url, method string, parameters ...interface{}) (Response, error) {
params, err := json.Marshal(parameters)
httpReq, err := BuildJsonHTTPRequest(ctx, url, method, parameters...)
if err != nil {
return Response{}, err
}

req := Request{
JSONRPC: "2.0",
ID: float64(1),
Method: method,
Params: params,
}

reqBody, err := json.Marshal(req)
if err != nil {
return Response{}, err
}

reqBodyReader := bytes.NewReader(reqBody)
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, reqBodyReader)
if err != nil {
return Response{}, err
}

httpReq.Header.Add("Content-type", "application/json")

httpRes, err := http.DefaultClient.Do(httpReq)
if err != nil {
return Response{}, err
Expand Down
66 changes: 66 additions & 0 deletions rpc/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package rpc

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func Test_JSONRPCCallWithContext(t *testing.T) {
tests := []struct {
name string
result string
resp Response
statusCode int
err error
}{
{
name: "successfully executed request",
result: `{"result":"test"}`,
resp: Response{
Result: json.RawMessage(`"test"`),
},
},
{
name: "unsuccessful status code returned by server",
statusCode: http.StatusUnauthorized,
err: errors.New("invalid status code, expected: 200, found: 401"),
},
}
for _, tt := range tests {
tt := tt

t.Run(tt.name, func(t *testing.T) {
t.Parallel()

svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var res Request
require.NoError(t, json.NewDecoder(r.Body).Decode(&res))
require.Equal(t, "test", res.Method)

if tt.statusCode > 0 {
w.WriteHeader(tt.statusCode)
}

_, err := fmt.Fprint(w, tt.result)
require.NoError(t, err)
}))
defer svr.Close()

got, err := JSONRPCCallWithContext(context.Background(), svr.URL, "test")
if tt.err != nil {
require.Error(t, err)
require.EqualError(t, tt.err, err.Error())
} else {
require.NoError(t, err)
require.Equal(t, tt.resp, got)
}
})
}
}
1 change: 1 addition & 0 deletions rpc/dbtxmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type DBTxScopedFn func(ctx context.Context, dbTx db.Tx) (interface{}, Error)
// NewDbTxScope function to initiate DB scopped txs
func (f *DBTxManager) NewDbTxScope(db db.DB, scopedFn DBTxScopedFn) (interface{}, Error) {
ctx := context.Background()

dbTx, err := db.BeginStateTransaction(ctx)
if err != nil {
return RPCErrorResponse(DefaultErrorCode, "failed to connect to the state", err)
Expand Down

0 comments on commit 133e46a

Please sign in to comment.