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.

148 lines
2.9 KiB

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