Skip to content

Commit

Permalink
Register blockchain.* handlers in jsonrpc_service.go.
Browse files Browse the repository at this point in the history
  • Loading branch information
moodyjon committed Sep 7, 2022
1 parent 20e3243 commit 8c8871b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
32 changes: 10 additions & 22 deletions server/jsonrpc_blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down
23 changes: 15 additions & 8 deletions server/jsonrpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
pb "github.com/lbryio/herald.go/protobuf/go"
)

type JSONServer struct {
type ClaimtrieService struct {
DB *db.ReadOnlyDBColumnFamily
}

Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 8c8871b

Please sign in to comment.