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.

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