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.

73 lines
2.2 KiB

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