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.

167 lines
3.3 KiB

  1. package core
  2. import (
  3. "github.com/tendermint/tendermint/consensus"
  4. "github.com/tendermint/tendermint/crypto"
  5. dbm "github.com/tendermint/tendermint/libs/db"
  6. "github.com/tendermint/tendermint/libs/log"
  7. mempl "github.com/tendermint/tendermint/mempool"
  8. "github.com/tendermint/tendermint/p2p"
  9. "github.com/tendermint/tendermint/proxy"
  10. rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
  11. sm "github.com/tendermint/tendermint/state"
  12. "github.com/tendermint/tendermint/state/txindex"
  13. "github.com/tendermint/tendermint/types"
  14. )
  15. const (
  16. // see README
  17. defaultPerPage = 30
  18. maxPerPage = 100
  19. )
  20. var subscribeTimeout = rpcserver.WriteTimeout / 2
  21. //----------------------------------------------
  22. // These interfaces are used by RPC and must be thread safe
  23. type Consensus interface {
  24. GetState() sm.State
  25. GetValidators() (int64, []*types.Validator)
  26. GetLastHeight() int64
  27. GetRoundStateJSON() ([]byte, error)
  28. GetRoundStateSimpleJSON() ([]byte, error)
  29. }
  30. type transport interface {
  31. Listeners() []string
  32. IsListening() bool
  33. NodeInfo() p2p.NodeInfo
  34. }
  35. type peers interface {
  36. DialPeersAsync(p2p.AddrBook, []string, bool) error
  37. NumPeers() (outbound, inbound, dialig int)
  38. Peers() p2p.IPeerSet
  39. }
  40. //----------------------------------------------
  41. // These package level globals come with setters
  42. // that are expected to be called only once, on startup
  43. var (
  44. // external, thread safe interfaces
  45. proxyAppQuery proxy.AppConnQuery
  46. // interfaces defined in types and above
  47. stateDB dbm.DB
  48. blockStore sm.BlockStore
  49. evidencePool sm.EvidencePool
  50. consensusState Consensus
  51. p2pPeers peers
  52. p2pTransport transport
  53. // objects
  54. pubKey crypto.PubKey
  55. genDoc *types.GenesisDoc // cache the genesis structure
  56. addrBook p2p.AddrBook
  57. txIndexer txindex.TxIndexer
  58. consensusReactor *consensus.ConsensusReactor
  59. eventBus *types.EventBus // thread safe
  60. mempool *mempl.Mempool
  61. logger log.Logger
  62. )
  63. func SetStateDB(db dbm.DB) {
  64. stateDB = db
  65. }
  66. func SetBlockStore(bs sm.BlockStore) {
  67. blockStore = bs
  68. }
  69. func SetMempool(mem *mempl.Mempool) {
  70. mempool = mem
  71. }
  72. func SetEvidencePool(evpool sm.EvidencePool) {
  73. evidencePool = evpool
  74. }
  75. func SetConsensusState(cs Consensus) {
  76. consensusState = cs
  77. }
  78. func SetP2PPeers(p peers) {
  79. p2pPeers = p
  80. }
  81. func SetP2PTransport(t transport) {
  82. p2pTransport = t
  83. }
  84. func SetPubKey(pk crypto.PubKey) {
  85. pubKey = pk
  86. }
  87. func SetGenesisDoc(doc *types.GenesisDoc) {
  88. genDoc = doc
  89. }
  90. func SetAddrBook(book p2p.AddrBook) {
  91. addrBook = book
  92. }
  93. func SetProxyAppQuery(appConn proxy.AppConnQuery) {
  94. proxyAppQuery = appConn
  95. }
  96. func SetTxIndexer(indexer txindex.TxIndexer) {
  97. txIndexer = indexer
  98. }
  99. func SetConsensusReactor(conR *consensus.ConsensusReactor) {
  100. consensusReactor = conR
  101. }
  102. func SetLogger(l log.Logger) {
  103. logger = l
  104. }
  105. func SetEventBus(b *types.EventBus) {
  106. eventBus = b
  107. }
  108. func validatePage(page, perPage, totalCount int) int {
  109. if perPage < 1 {
  110. return 1
  111. }
  112. pages := ((totalCount - 1) / perPage) + 1
  113. if page < 1 {
  114. page = 1
  115. } else if page > pages {
  116. page = pages
  117. }
  118. return page
  119. }
  120. func validatePerPage(perPage int) int {
  121. if perPage < 1 {
  122. return defaultPerPage
  123. } else if perPage > maxPerPage {
  124. return maxPerPage
  125. }
  126. return perPage
  127. }
  128. func validateSkipCount(page, perPage int) int {
  129. skipCount := (page - 1) * perPage
  130. if skipCount < 0 {
  131. return 0
  132. }
  133. return skipCount
  134. }