From 8c8871b4d2859a2d88959dfaf021af6fcb7f28e2 Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Wed, 7 Sep 2022 15:01:47 -0500 Subject: [PATCH] Register blockchain.* handlers in jsonrpc_service.go. --- server/jsonrpc_blockchain.go | 32 ++++++++++---------------------- server/jsonrpc_service.go | 23 +++++++++++++++-------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/server/jsonrpc_blockchain.go b/server/jsonrpc_blockchain.go index 57cec37..11e9fc0 100644 --- a/server/jsonrpc_blockchain.go +++ b/server/jsonrpc_blockchain.go @@ -12,6 +12,7 @@ import ( "net/http" "strings" + "github.com/gorilla/rpc" "github.com/lbryio/herald.go/db" "github.com/lbryio/herald.go/internal" "github.com/lbryio/lbcd/chaincfg" @@ -22,35 +23,22 @@ import ( "golang.org/x/exp/constraints" ) -// TODO: import this from gorilla/rpc -// Codec creates a CodecRequest to process each request. -type Codec interface { - NewRequest(*http.Request) CodecRequest -} - -// TODO: import this from gorilla/rpc -// CodecRequest decodes a request and encodes a response using a specific -// serialization scheme. -type CodecRequest interface { - // Reads request and returns the RPC method name. - Method() (string, error) - // Reads request filling the RPC method args. - ReadRequest(interface{}) error - // Writes response using the RPC method reply. The error parameter is - // the error returned by the method call, if any. - WriteResponse(http.ResponseWriter, interface{}, error) error -} - type BlockchainCodec struct { - Codec + rpc.Codec } -func (c *BlockchainCodec) NewRequest(r *http.Request) CodecRequest { +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 { - CodecRequest + rpc.CodecRequest } func (cr *BlockchainCodecRequest) Method() (string, error) { diff --git a/server/jsonrpc_service.go b/server/jsonrpc_service.go index 95f45e8..8f32d16 100644 --- a/server/jsonrpc_service.go +++ b/server/jsonrpc_service.go @@ -11,7 +11,7 @@ import ( pb "github.com/lbryio/herald.go/protobuf/go" ) -type JSONServer struct { +type ClaimtrieService struct { DB *db.ReadOnlyDBColumnFamily } @@ -24,7 +24,7 @@ type Result struct { } // Resolve is the json rpc endpoint for resolve -func (t *JSONServer) Resolve(r *http.Request, args *ResolveData, result **pb.Outputs) error { +func (t *ClaimtrieService) Resolve(r *http.Request, args *ResolveData, result **pb.Outputs) error { log.Println("Resolve") res, err := InternalResolve(args.Data, t.DB) *result = res @@ -33,14 +33,21 @@ func (t *JSONServer) Resolve(r *http.Request, args *ResolveData, result **pb.Out // StartJsonRPC starts the json rpc server and registers the endpoints. func (s *Server) StartJsonRPC() error { - server := new(JSONServer) - server.DB = s.DB - port := ":" + s.Args.JSONRPCPort - s1 := rpc.NewServer() // Create a new RPC server - s1.RegisterCodec(json.NewCodec(), "application/json") // Register the type of data requested as JSON - s1.RegisterService(server, "") // Register the service by creating a new JSON server + s1 := 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") + + // Register "blockchain.claimtrie.*"" handlers. + claimtrieSvc := &ClaimtrieService{s.DB} + s1.RegisterService(claimtrieSvc, "blockchain_claimtrie") + + // Register other "blockchain.{block,address,scripthash}.*" handlers. + blockchainSvc := &BlockchainService{s.DB, s.Chain} + s1.RegisterService(&blockchainSvc, "blockchain_block") + s1.RegisterService(&BlockchainAddressService{*blockchainSvc}, "blockchain_address") + s1.RegisterService(&BlockchainScripthashService{*blockchainSvc}, "blockchain_scripthash") r := mux.NewRouter() r.Handle("/rpc", s1)