You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.7 KiB

  1. package proxy
  2. import (
  3. "net/http"
  4. amino "github.com/tendermint/go-amino"
  5. "github.com/tendermint/tendermint/libs/log"
  6. rpcclient "github.com/tendermint/tendermint/rpc/client"
  7. "github.com/tendermint/tendermint/rpc/core"
  8. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  9. rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
  10. )
  11. const (
  12. wsEndpoint = "/websocket"
  13. )
  14. // StartProxy will start the websocket manager on the client,
  15. // set up the rpc routes to proxy via the given client,
  16. // and start up an http/rpc server on the location given by bind (eg. :1234)
  17. // NOTE: This function blocks - you may want to call it in a go-routine.
  18. func StartProxy(c rpcclient.Client, listenAddr string, logger log.Logger, maxOpenConnections int) error {
  19. err := c.Start()
  20. if err != nil {
  21. return err
  22. }
  23. cdc := amino.NewCodec()
  24. ctypes.RegisterAmino(cdc)
  25. r := RPCRoutes(c)
  26. // build the handler...
  27. mux := http.NewServeMux()
  28. rpcserver.RegisterRPCFuncs(mux, r, cdc, logger)
  29. wm := rpcserver.NewWebsocketManager(r, cdc, rpcserver.EventSubscriber(c))
  30. wm.SetLogger(logger)
  31. core.SetLogger(logger)
  32. mux.HandleFunc(wsEndpoint, wm.WebsocketHandler)
  33. l, err := rpcserver.Listen(listenAddr, rpcserver.Config{MaxOpenConnections: maxOpenConnections})
  34. if err != nil {
  35. return err
  36. }
  37. return rpcserver.StartHTTPServer(l, mux, logger)
  38. }
  39. // RPCRoutes just routes everything to the given client, as if it were
  40. // a tendermint fullnode.
  41. //
  42. // if we want security, the client must implement it as a secure client
  43. func RPCRoutes(c rpcclient.Client) map[string]*rpcserver.RPCFunc {
  44. return map[string]*rpcserver.RPCFunc{
  45. // Subscribe/unsubscribe are reserved for websocket events.
  46. // We can just use the core tendermint impl, which uses the
  47. // EventSwitch we registered in NewWebsocketManager above
  48. "subscribe": rpcserver.NewWSRPCFunc(core.Subscribe, "query"),
  49. "unsubscribe": rpcserver.NewWSRPCFunc(core.Unsubscribe, "query"),
  50. // info API
  51. "status": rpcserver.NewRPCFunc(c.Status, ""),
  52. "blockchain": rpcserver.NewRPCFunc(c.BlockchainInfo, "minHeight,maxHeight"),
  53. "genesis": rpcserver.NewRPCFunc(c.Genesis, ""),
  54. "block": rpcserver.NewRPCFunc(c.Block, "height"),
  55. "commit": rpcserver.NewRPCFunc(c.Commit, "height"),
  56. "tx": rpcserver.NewRPCFunc(c.Tx, "hash,prove"),
  57. "validators": rpcserver.NewRPCFunc(c.Validators, "height"),
  58. // broadcast API
  59. "broadcast_tx_commit": rpcserver.NewRPCFunc(c.BroadcastTxCommit, "tx"),
  60. "broadcast_tx_sync": rpcserver.NewRPCFunc(c.BroadcastTxSync, "tx"),
  61. "broadcast_tx_async": rpcserver.NewRPCFunc(c.BroadcastTxAsync, "tx"),
  62. // abci API
  63. "abci_query": rpcserver.NewRPCFunc(c.ABCIQuery, "path,data"),
  64. "abci_info": rpcserver.NewRPCFunc(c.ABCIInfo, ""),
  65. }
  66. }