From 40059969922e47b06e0c5f155ba1df38c5491cb6 Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Tue, 27 Sep 2022 17:07:16 -0500 Subject: [PATCH] Move and rename BlockchainCodec, BlockchainCodecRequest. These are not specifically "blockchain", rather they are specific to how gorilla/rpc works. --- server/jsonrpc_blockchain.go | 38 --------------------------- server/jsonrpc_service.go | 50 +++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/server/jsonrpc_blockchain.go b/server/jsonrpc_blockchain.go index 41b6809..7615793 100644 --- a/server/jsonrpc_blockchain.go +++ b/server/jsonrpc_blockchain.go @@ -10,9 +10,7 @@ import ( "errors" "fmt" "net/http" - "strings" - "github.com/gorilla/rpc" "github.com/lbryio/herald.go/db" "github.com/lbryio/herald.go/internal" "github.com/lbryio/lbcd/chaincfg" @@ -23,42 +21,6 @@ import ( "golang.org/x/exp/constraints" ) -type BlockchainCodec struct { - rpc.Codec -} - -func (c *BlockchainCodec) NewRequest(r *http.Request) rpc.CodecRequest { - return &BlockchainCodecRequest{c.Codec.NewRequest(r)} -} - -// BlockchainCodecRequest provides ability to rewrite the incoming -// request "method" field. For example: -// blockchain.block.get_header -> blockchain_block.Get_header -// blockchain.address.listunspent -> blockchain_address.Listunspent -// This makes the "method" string compatible with Gorilla/RPC -// requirements. -type BlockchainCodecRequest struct { - rpc.CodecRequest -} - -func (cr *BlockchainCodecRequest) Method() (string, error) { - rawMethod, err := cr.CodecRequest.Method() - if err != nil { - return rawMethod, err - } - parts := strings.Split(rawMethod, ".") - if len(parts) < 2 { - return rawMethod, fmt.Errorf("blockchain rpc: service/method ill-formed: %q", rawMethod) - } - service := strings.Join(parts[0:len(parts)-1], "_") - method := parts[len(parts)-1] - if len(method) < 1 { - return rawMethod, fmt.Errorf("blockchain rpc: method ill-formed: %q", method) - } - method = strings.ToUpper(string(method[0])) + string(method[1:]) - return service + "." + method, err -} - // BlockchainService methods handle "blockchain.block.*" RPCs type BlockchainService struct { DB *db.ReadOnlyDBColumnFamily diff --git a/server/jsonrpc_service.go b/server/jsonrpc_service.go index 130d2d3..c13fbcb 100644 --- a/server/jsonrpc_service.go +++ b/server/jsonrpc_service.go @@ -1,11 +1,13 @@ package server import ( + "fmt" "net/http" + "strings" - "github.com/gorilla/mux" - "github.com/gorilla/rpc" - "github.com/gorilla/rpc/json" + gorilla_mux "github.com/gorilla/mux" + gorilla_rpc "github.com/gorilla/rpc" + gorilla_json "github.com/gorilla/rpc/json" "github.com/lbryio/herald.go/db" pb "github.com/lbryio/herald.go/protobuf/go" log "github.com/sirupsen/logrus" @@ -31,13 +33,49 @@ func (t *ClaimtrieService) Resolve(r *http.Request, args *ResolveData, result ** return err } +type gorillaRpcCodec struct { + gorilla_rpc.Codec +} + +func (c *gorillaRpcCodec) NewRequest(r *http.Request) gorilla_rpc.CodecRequest { + return &gorillaRpcCodecRequest{c.Codec.NewRequest(r)} +} + +// gorillaRpcCodecRequest provides ability to rewrite the incoming +// request "method" field. For example: +// blockchain.block.get_header -> blockchain_block.Get_header +// blockchain.address.listunspent -> blockchain_address.Listunspent +// This makes the "method" string compatible with Gorilla/RPC +// requirements. +type gorillaRpcCodecRequest struct { + gorilla_rpc.CodecRequest +} + +func (cr *gorillaRpcCodecRequest) Method() (string, error) { + rawMethod, err := cr.CodecRequest.Method() + if err != nil { + return rawMethod, err + } + parts := strings.Split(rawMethod, ".") + if len(parts) < 2 { + return rawMethod, fmt.Errorf("blockchain rpc: service/method ill-formed: %q", rawMethod) + } + service := strings.Join(parts[0:len(parts)-1], "_") + method := parts[len(parts)-1] + if len(method) < 1 { + return rawMethod, fmt.Errorf("blockchain rpc: method ill-formed: %q", method) + } + method = strings.ToUpper(string(method[0])) + string(method[1:]) + return service + "." + method, err +} + // StartJsonRPC starts the json rpc server and registers the endpoints. func (s *Server) StartJsonRPC() error { port := ":" + s.Args.JSONRPCPort - s1 := rpc.NewServer() // Create a new RPC server + s1 := gorilla_rpc.NewServer() // Create a new RPC server // Register the type of data requested as JSON, with custom codec. - s1.RegisterCodec(&BlockchainCodec{json.NewCodec()}, "application/json") + s1.RegisterCodec(&gorillaRpcCodec{gorilla_json.NewCodec()}, "application/json") // Register "blockchain.claimtrie.*"" handlers. claimtrieSvc := &ClaimtrieService{s.DB} @@ -61,7 +99,7 @@ func (s *Server) StartJsonRPC() error { log.Errorf("RegisterService: %v\n", err) } - r := mux.NewRouter() + r := gorilla_mux.NewRouter() r.Handle("/rpc", s1) log.Fatal(http.ListenAndServe(port, r))