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.

125 lines
6.9 KiB

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