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.

69 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. c.Start()
  17. r := RPCRoutes(c)
  18. // build the handler...
  19. mux := http.NewServeMux()
  20. rpc.RegisterRPCFuncs(mux, r, logger)
  21. wm := rpc.NewWebsocketManager(r, rpc.EventSubscriber(c))
  22. wm.SetLogger(logger)
  23. core.SetLogger(logger)
  24. mux.HandleFunc(wsEndpoint, wm.WebsocketHandler)
  25. _, err := rpc.StartHTTPServer(listenAddr, mux, logger)
  26. return err
  27. }
  28. // RPCRoutes just routes everything to the given client, as if it were
  29. // a tendermint fullnode.
  30. //
  31. // if we want security, the client must implement it as a secure client
  32. func RPCRoutes(c rpcclient.Client) map[string]*rpc.RPCFunc {
  33. return map[string]*rpc.RPCFunc{
  34. // Subscribe/unsubscribe are reserved for websocket events.
  35. // We can just use the core tendermint impl, which uses the
  36. // EventSwitch we registered in NewWebsocketManager above
  37. "subscribe": rpc.NewWSRPCFunc(core.Subscribe, "query"),
  38. "unsubscribe": rpc.NewWSRPCFunc(core.Unsubscribe, "query"),
  39. // info API
  40. "status": rpc.NewRPCFunc(c.Status, ""),
  41. "blockchain": rpc.NewRPCFunc(c.BlockchainInfo, "minHeight,maxHeight"),
  42. "genesis": rpc.NewRPCFunc(c.Genesis, ""),
  43. "block": rpc.NewRPCFunc(c.Block, "height"),
  44. "commit": rpc.NewRPCFunc(c.Commit, "height"),
  45. "tx": rpc.NewRPCFunc(c.Tx, "hash,prove"),
  46. "validators": rpc.NewRPCFunc(c.Validators, ""),
  47. // broadcast API
  48. "broadcast_tx_commit": rpc.NewRPCFunc(c.BroadcastTxCommit, "tx"),
  49. "broadcast_tx_sync": rpc.NewRPCFunc(c.BroadcastTxSync, "tx"),
  50. "broadcast_tx_async": rpc.NewRPCFunc(c.BroadcastTxAsync, "tx"),
  51. // abci API
  52. "abci_query": rpc.NewRPCFunc(c.ABCIQuery, "path,data,prove"),
  53. "abci_info": rpc.NewRPCFunc(c.ABCIInfo, ""),
  54. }
  55. }