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.

120 lines
6.5 KiB

  1. package core
  2. import (
  3. "context"
  4. "github.com/tendermint/tendermint/libs/bytes"
  5. "github.com/tendermint/tendermint/rpc/coretypes"
  6. rpc "github.com/tendermint/tendermint/rpc/jsonrpc/server"
  7. "github.com/tendermint/tendermint/types"
  8. )
  9. // TODO: better system than "unsafe" prefix
  10. type RoutesMap map[string]*rpc.RPCFunc
  11. // RouteOptions provide optional settings to NewRoutesMap. A nil *RouteOptions
  12. // is ready for use and provides defaults as specified.
  13. type RouteOptions struct {
  14. Unsafe bool // include "unsafe" methods (default false)
  15. }
  16. // NewRoutesMap constructs an RPC routing map for the given service
  17. // implementation. If svc implements RPCUnsafe and opts.Unsafe is true, the
  18. // "unsafe" methods will also be added to the map. The caller may also edit the
  19. // map after construction; each call to NewRoutesMap returns a fresh map.
  20. func NewRoutesMap(svc RPCService, opts *RouteOptions) RoutesMap {
  21. if opts == nil {
  22. opts = new(RouteOptions)
  23. }
  24. out := RoutesMap{
  25. // subscribe/unsubscribe are reserved for websocket events.
  26. "subscribe": rpc.NewWSRPCFunc(svc.Subscribe, "query"),
  27. "unsubscribe": rpc.NewWSRPCFunc(svc.Unsubscribe, "query"),
  28. "unsubscribe_all": rpc.NewWSRPCFunc(svc.UnsubscribeAll),
  29. // info API
  30. "health": rpc.NewRPCFunc(svc.Health),
  31. "status": rpc.NewRPCFunc(svc.Status),
  32. "net_info": rpc.NewRPCFunc(svc.NetInfo),
  33. "blockchain": rpc.NewRPCFunc(svc.BlockchainInfo, "minHeight", "maxHeight"),
  34. "genesis": rpc.NewRPCFunc(svc.Genesis),
  35. "genesis_chunked": rpc.NewRPCFunc(svc.GenesisChunked, "chunk"),
  36. "header": rpc.NewRPCFunc(svc.Header, "height"),
  37. "header_by_hash": rpc.NewRPCFunc(svc.HeaderByHash, "hash"),
  38. "block": rpc.NewRPCFunc(svc.Block, "height"),
  39. "block_by_hash": rpc.NewRPCFunc(svc.BlockByHash, "hash"),
  40. "block_results": rpc.NewRPCFunc(svc.BlockResults, "height"),
  41. "commit": rpc.NewRPCFunc(svc.Commit, "height"),
  42. "check_tx": rpc.NewRPCFunc(svc.CheckTx, "tx"),
  43. "remove_tx": rpc.NewRPCFunc(svc.RemoveTx, "txkey"),
  44. "tx": rpc.NewRPCFunc(svc.Tx, "hash", "prove"),
  45. "tx_search": rpc.NewRPCFunc(svc.TxSearch, "query", "prove", "page", "per_page", "order_by"),
  46. "block_search": rpc.NewRPCFunc(svc.BlockSearch, "query", "page", "per_page", "order_by"),
  47. "validators": rpc.NewRPCFunc(svc.Validators, "height", "page", "per_page"),
  48. "dump_consensus_state": rpc.NewRPCFunc(svc.DumpConsensusState),
  49. "consensus_state": rpc.NewRPCFunc(svc.GetConsensusState),
  50. "consensus_params": rpc.NewRPCFunc(svc.ConsensusParams, "height"),
  51. "unconfirmed_txs": rpc.NewRPCFunc(svc.UnconfirmedTxs, "page", "per_page"),
  52. "num_unconfirmed_txs": rpc.NewRPCFunc(svc.NumUnconfirmedTxs),
  53. // tx broadcast API
  54. "broadcast_tx_commit": rpc.NewRPCFunc(svc.BroadcastTxCommit, "tx"),
  55. "broadcast_tx_sync": rpc.NewRPCFunc(svc.BroadcastTxSync, "tx"),
  56. "broadcast_tx_async": rpc.NewRPCFunc(svc.BroadcastTxAsync, "tx"),
  57. // abci API
  58. "abci_query": rpc.NewRPCFunc(svc.ABCIQuery, "path", "data", "height", "prove"),
  59. "abci_info": rpc.NewRPCFunc(svc.ABCIInfo),
  60. // evidence API
  61. "broadcast_evidence": rpc.NewRPCFunc(svc.BroadcastEvidence, "evidence"),
  62. }
  63. if u, ok := svc.(RPCUnsafe); ok && opts.Unsafe {
  64. out["unsafe_flush_mempool"] = rpc.NewRPCFunc(u.UnsafeFlushMempool)
  65. }
  66. return out
  67. }
  68. // RPCService defines the set of methods exported by the RPC service
  69. // implementation, for use in constructing a routing table.
  70. type RPCService interface {
  71. ABCIInfo(ctx context.Context) (*coretypes.ResultABCIInfo, error)
  72. ABCIQuery(ctx context.Context, path string, data bytes.HexBytes, height int64, prove bool) (*coretypes.ResultABCIQuery, error)
  73. Block(ctx context.Context, heightPtr *int64) (*coretypes.ResultBlock, error)
  74. BlockByHash(ctx context.Context, hash bytes.HexBytes) (*coretypes.ResultBlock, error)
  75. BlockResults(ctx context.Context, heightPtr *int64) (*coretypes.ResultBlockResults, error)
  76. BlockSearch(ctx context.Context, query string, pagePtr, perPagePtr *int, orderBy string) (*coretypes.ResultBlockSearch, error)
  77. BlockchainInfo(ctx context.Context, minHeight, maxHeight int64) (*coretypes.ResultBlockchainInfo, error)
  78. BroadcastEvidence(ctx context.Context, ev coretypes.Evidence) (*coretypes.ResultBroadcastEvidence, error)
  79. BroadcastTxAsync(ctx context.Context, tx types.Tx) (*coretypes.ResultBroadcastTx, error)
  80. BroadcastTxCommit(ctx context.Context, tx types.Tx) (*coretypes.ResultBroadcastTxCommit, error)
  81. BroadcastTxSync(ctx context.Context, tx types.Tx) (*coretypes.ResultBroadcastTx, error)
  82. CheckTx(ctx context.Context, tx types.Tx) (*coretypes.ResultCheckTx, error)
  83. Commit(ctx context.Context, heightPtr *int64) (*coretypes.ResultCommit, error)
  84. ConsensusParams(ctx context.Context, heightPtr *int64) (*coretypes.ResultConsensusParams, error)
  85. DumpConsensusState(ctx context.Context) (*coretypes.ResultDumpConsensusState, error)
  86. Genesis(ctx context.Context) (*coretypes.ResultGenesis, error)
  87. GenesisChunked(ctx context.Context, chunk uint) (*coretypes.ResultGenesisChunk, error)
  88. GetConsensusState(ctx context.Context) (*coretypes.ResultConsensusState, error)
  89. Header(ctx context.Context, heightPtr *int64) (*coretypes.ResultHeader, error)
  90. HeaderByHash(ctx context.Context, hash bytes.HexBytes) (*coretypes.ResultHeader, error)
  91. Health(ctx context.Context) (*coretypes.ResultHealth, error)
  92. NetInfo(ctx context.Context) (*coretypes.ResultNetInfo, error)
  93. NumUnconfirmedTxs(ctx context.Context) (*coretypes.ResultUnconfirmedTxs, error)
  94. RemoveTx(ctx context.Context, txkey types.TxKey) error
  95. Status(ctx context.Context) (*coretypes.ResultStatus, error)
  96. Subscribe(ctx context.Context, query string) (*coretypes.ResultSubscribe, error)
  97. Tx(ctx context.Context, hash bytes.HexBytes, prove bool) (*coretypes.ResultTx, error)
  98. TxSearch(ctx context.Context, query string, prove bool, pagePtr, perPagePtr *int, orderBy string) (*coretypes.ResultTxSearch, error)
  99. UnconfirmedTxs(ctx context.Context, page, perPage *int) (*coretypes.ResultUnconfirmedTxs, error)
  100. Unsubscribe(ctx context.Context, query string) (*coretypes.ResultUnsubscribe, error)
  101. UnsubscribeAll(ctx context.Context) (*coretypes.ResultUnsubscribe, error)
  102. Validators(ctx context.Context, heightPtr *int64, pagePtr, perPagePtr *int) (*coretypes.ResultValidators, error)
  103. }
  104. // RPCUnsafe defines the set of "unsafe" methods that may optionally be
  105. // exported by the RPC service.
  106. type RPCUnsafe interface {
  107. UnsafeFlushMempool(ctx context.Context) (*coretypes.ResultUnsafeFlushMempool, error)
  108. }