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.

77 lines
2.3 KiB

  1. package proxy
  2. import (
  3. "net/http"
  4. "github.com/tendermint/go-amino"
  5. "github.com/tendermint/tmlibs/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. _, err = rpc.StartHTTPServer(listenAddr, mux, logger)
  33. return err
  34. }
  35. // RPCRoutes just routes everything to the given client, as if it were
  36. // a tendermint fullnode.
  37. //
  38. // if we want security, the client must implement it as a secure client
  39. func RPCRoutes(c rpcclient.Client) map[string]*rpc.RPCFunc {
  40. return map[string]*rpc.RPCFunc{
  41. // Subscribe/unsubscribe are reserved for websocket events.
  42. // We can just use the core tendermint impl, which uses the
  43. // EventSwitch we registered in NewWebsocketManager above
  44. "subscribe": rpc.NewWSRPCFunc(core.Subscribe, "query"),
  45. "unsubscribe": rpc.NewWSRPCFunc(core.Unsubscribe, "query"),
  46. // info API
  47. "status": rpc.NewRPCFunc(c.Status, ""),
  48. "blockchain": rpc.NewRPCFunc(c.BlockchainInfo, "minHeight,maxHeight"),
  49. "genesis": rpc.NewRPCFunc(c.Genesis, ""),
  50. "block": rpc.NewRPCFunc(c.Block, "height"),
  51. "commit": rpc.NewRPCFunc(c.Commit, "height"),
  52. "tx": rpc.NewRPCFunc(c.Tx, "hash,prove"),
  53. "validators": rpc.NewRPCFunc(c.Validators, ""),
  54. // broadcast API
  55. "broadcast_tx_commit": rpc.NewRPCFunc(c.BroadcastTxCommit, "tx"),
  56. "broadcast_tx_sync": rpc.NewRPCFunc(c.BroadcastTxSync, "tx"),
  57. "broadcast_tx_async": rpc.NewRPCFunc(c.BroadcastTxAsync, "tx"),
  58. // abci API
  59. "abci_query": rpc.NewRPCFunc(c.ABCIQuery, "path,data,prove"),
  60. "abci_info": rpc.NewRPCFunc(c.ABCIInfo, ""),
  61. }
  62. }