2022-09-07 20:36:07 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-09-28 00:07:16 +02:00
|
|
|
"fmt"
|
2022-09-07 20:36:07 +02:00
|
|
|
"net/http"
|
2022-09-28 00:07:16 +02:00
|
|
|
"strings"
|
2022-09-07 20:36:07 +02:00
|
|
|
|
2022-09-28 00:07:16 +02:00
|
|
|
gorilla_mux "github.com/gorilla/mux"
|
|
|
|
gorilla_rpc "github.com/gorilla/rpc"
|
|
|
|
gorilla_json "github.com/gorilla/rpc/json"
|
2022-09-08 18:50:06 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-09-07 20:36:07 +02:00
|
|
|
)
|
|
|
|
|
2022-09-28 00:07:16 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-07 20:36:07 +02:00
|
|
|
// StartJsonRPC starts the json rpc server and registers the endpoints.
|
|
|
|
func (s *Server) StartJsonRPC() error {
|
|
|
|
port := ":" + s.Args.JSONRPCPort
|
|
|
|
|
2022-09-28 00:07:16 +02:00
|
|
|
s1 := gorilla_rpc.NewServer() // Create a new RPC server
|
2022-09-07 22:01:47 +02:00
|
|
|
// Register the type of data requested as JSON, with custom codec.
|
2022-09-28 00:07:16 +02:00
|
|
|
s1.RegisterCodec(&gorillaRpcCodec{gorilla_json.NewCodec()}, "application/json")
|
2022-09-07 22:01:47 +02:00
|
|
|
|
|
|
|
// Register "blockchain.claimtrie.*"" handlers.
|
|
|
|
claimtrieSvc := &ClaimtrieService{s.DB}
|
2022-09-28 00:10:49 +02:00
|
|
|
err := s1.RegisterTCPService(claimtrieSvc, "blockchain_claimtrie")
|
2022-09-08 18:50:06 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RegisterService: %v\n", err)
|
|
|
|
}
|
2022-09-07 22:01:47 +02:00
|
|
|
|
|
|
|
// Register other "blockchain.{block,address,scripthash}.*" handlers.
|
|
|
|
blockchainSvc := &BlockchainService{s.DB, s.Chain}
|
2022-09-08 18:50:06 +02:00
|
|
|
err = s1.RegisterService(blockchainSvc, "blockchain_block")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RegisterService: %v\n", err)
|
|
|
|
}
|
|
|
|
err = s1.RegisterService(&BlockchainAddressService{*blockchainSvc}, "blockchain_address")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RegisterService: %v\n", err)
|
|
|
|
}
|
|
|
|
err = s1.RegisterService(&BlockchainScripthashService{*blockchainSvc}, "blockchain_scripthash")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("RegisterService: %v\n", err)
|
|
|
|
}
|
2022-09-07 20:36:07 +02:00
|
|
|
|
2022-09-28 00:07:16 +02:00
|
|
|
r := gorilla_mux.NewRouter()
|
2022-09-07 20:36:07 +02:00
|
|
|
r.Handle("/rpc", s1)
|
|
|
|
log.Fatal(http.ListenAndServe(port, r))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|