Register blockchain.* handlers in jsonrpc_service.go.

This commit is contained in:
Jonathan Moody 2022-09-07 15:01:47 -05:00
parent 20e32437e9
commit 8c8871b4d2
2 changed files with 25 additions and 30 deletions

View file

@ -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) {

View file

@ -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)