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.

149 lines
3.0 KiB

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