diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 3d5da1e2b..e1b6210bf 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -110,11 +110,11 @@ func (s *Server) Start(errChan chan error) { s.log.Info("RPC server is not enabled") return } - s.Handler = http.HandlerFunc(s.requestHandler) + s.Handler = http.HandlerFunc(s.handleHTTPRequest) s.log.Info("starting rpc-server", zap.String("endpoint", s.Addr)) if cfg := s.config.TLSConfig; cfg.Enabled { - s.https.Handler = http.HandlerFunc(s.requestHandler) + s.https.Handler = http.HandlerFunc(s.handleHTTPRequest) s.log.Info("starting rpc-server (https)", zap.String("endpoint", s.https.Addr)) go func() { err := s.https.ListenAndServeTLS(cfg.CertFile, cfg.KeyFile) @@ -148,7 +148,7 @@ func (s *Server) Shutdown() error { return err } -func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request) { +func (s *Server) handleHTTPRequest(w http.ResponseWriter, httpRequest *http.Request) { req := request.NewIn() if httpRequest.Method != "POST" { @@ -168,16 +168,16 @@ func (s *Server) requestHandler(w http.ResponseWriter, httpRequest *http.Request return } + s.handleRequest(w, req) +} + +func (s *Server) handleRequest(w http.ResponseWriter, req *request.In) { reqParams, err := req.Params() if err != nil { s.WriteErrorResponse(req, w, response.NewInvalidParamsError("Problem parsing request parameters", err)) return } - s.methodHandler(w, req, *reqParams) -} - -func (s *Server) methodHandler(w http.ResponseWriter, req *request.In, reqParams request.Params) { s.log.Debug("processing rpc request", zap.String("method", req.Method), zap.String("params", fmt.Sprintf("%v", reqParams))) @@ -191,7 +191,7 @@ func (s *Server) methodHandler(w http.ResponseWriter, req *request.In, reqParams handler, ok := rpcHandlers[req.Method] if ok { - results, resultsErr = handler(s, reqParams) + results, resultsErr = handler(s, *reqParams) } else { resultsErr = response.NewMethodNotFoundError(fmt.Sprintf("Method '%s' not supported", req.Method), nil) } diff --git a/pkg/rpc/server/server_helper_test.go b/pkg/rpc/server/server_helper_test.go index 1263c0dcd..9b99a6505 100644 --- a/pkg/rpc/server/server_helper_test.go +++ b/pkg/rpc/server/server_helper_test.go @@ -55,7 +55,7 @@ func initServerWithInMemoryChain(t *testing.T) (*core.Blockchain, http.HandlerFu server, err := network.NewServer(serverConfig, chain, logger) require.NoError(t, err) rpcServer := New(chain, cfg.ApplicationConfiguration.RPC, server, logger) - handler := http.HandlerFunc(rpcServer.requestHandler) + handler := http.HandlerFunc(rpcServer.handleHTTPRequest) return chain, handler }