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.

147 lines
2.9 KiB

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