github.com/evdatsion/aphelion-dpos-bft@v0.32.1/rpc/core/pipe.go (about)

     1  package core
     2  
     3  import (
     4  	"time"
     5  
     6  	cfg "github.com/evdatsion/aphelion-dpos-bft/config"
     7  	"github.com/evdatsion/aphelion-dpos-bft/consensus"
     8  	"github.com/evdatsion/aphelion-dpos-bft/crypto"
     9  	dbm "github.com/evdatsion/aphelion-dpos-bft/libs/db"
    10  	"github.com/evdatsion/aphelion-dpos-bft/libs/log"
    11  	mempl "github.com/evdatsion/aphelion-dpos-bft/mempool"
    12  	"github.com/evdatsion/aphelion-dpos-bft/p2p"
    13  	"github.com/evdatsion/aphelion-dpos-bft/proxy"
    14  	sm "github.com/evdatsion/aphelion-dpos-bft/state"
    15  	"github.com/evdatsion/aphelion-dpos-bft/state/txindex"
    16  	"github.com/evdatsion/aphelion-dpos-bft/types"
    17  )
    18  
    19  const (
    20  	// see README
    21  	defaultPerPage = 30
    22  	maxPerPage     = 100
    23  
    24  	// SubscribeTimeout is the maximum time we wait to subscribe for an event.
    25  	// must be less than the server's write timeout (see rpcserver.DefaultConfig)
    26  	SubscribeTimeout = 5 * time.Second
    27  )
    28  
    29  //----------------------------------------------
    30  // These interfaces are used by RPC and must be thread safe
    31  
    32  type Consensus interface {
    33  	GetState() sm.State
    34  	GetValidators() (int64, []*types.Validator)
    35  	GetLastHeight() int64
    36  	GetRoundStateJSON() ([]byte, error)
    37  	GetRoundStateSimpleJSON() ([]byte, error)
    38  }
    39  
    40  type transport interface {
    41  	Listeners() []string
    42  	IsListening() bool
    43  	NodeInfo() p2p.NodeInfo
    44  }
    45  
    46  type peers interface {
    47  	AddPersistentPeers([]string) error
    48  	DialPeersAsync([]string) error
    49  	NumPeers() (outbound, inbound, dialig int)
    50  	Peers() p2p.IPeerSet
    51  }
    52  
    53  //----------------------------------------------
    54  // These package level globals come with setters
    55  // that are expected to be called only once, on startup
    56  
    57  var (
    58  	// external, thread safe interfaces
    59  	proxyAppQuery proxy.AppConnQuery
    60  
    61  	// interfaces defined in types and above
    62  	stateDB        dbm.DB
    63  	blockStore     sm.BlockStore
    64  	evidencePool   sm.EvidencePool
    65  	consensusState Consensus
    66  	p2pPeers       peers
    67  	p2pTransport   transport
    68  
    69  	// objects
    70  	pubKey           crypto.PubKey
    71  	genDoc           *types.GenesisDoc // cache the genesis structure
    72  	addrBook         p2p.AddrBook
    73  	txIndexer        txindex.TxIndexer
    74  	consensusReactor *consensus.ConsensusReactor
    75  	eventBus         *types.EventBus // thread safe
    76  	mempool          mempl.Mempool
    77  
    78  	logger log.Logger
    79  
    80  	config cfg.RPCConfig
    81  )
    82  
    83  func SetStateDB(db dbm.DB) {
    84  	stateDB = db
    85  }
    86  
    87  func SetBlockStore(bs sm.BlockStore) {
    88  	blockStore = bs
    89  }
    90  
    91  func SetMempool(mem mempl.Mempool) {
    92  	mempool = mem
    93  }
    94  
    95  func SetEvidencePool(evpool sm.EvidencePool) {
    96  	evidencePool = evpool
    97  }
    98  
    99  func SetConsensusState(cs Consensus) {
   100  	consensusState = cs
   101  }
   102  
   103  func SetP2PPeers(p peers) {
   104  	p2pPeers = p
   105  }
   106  
   107  func SetP2PTransport(t transport) {
   108  	p2pTransport = t
   109  }
   110  
   111  func SetPubKey(pk crypto.PubKey) {
   112  	pubKey = pk
   113  }
   114  
   115  func SetGenesisDoc(doc *types.GenesisDoc) {
   116  	genDoc = doc
   117  }
   118  
   119  func SetAddrBook(book p2p.AddrBook) {
   120  	addrBook = book
   121  }
   122  
   123  func SetProxyAppQuery(appConn proxy.AppConnQuery) {
   124  	proxyAppQuery = appConn
   125  }
   126  
   127  func SetTxIndexer(indexer txindex.TxIndexer) {
   128  	txIndexer = indexer
   129  }
   130  
   131  func SetConsensusReactor(conR *consensus.ConsensusReactor) {
   132  	consensusReactor = conR
   133  }
   134  
   135  func SetLogger(l log.Logger) {
   136  	logger = l
   137  }
   138  
   139  func SetEventBus(b *types.EventBus) {
   140  	eventBus = b
   141  }
   142  
   143  // SetConfig sets an RPCConfig.
   144  func SetConfig(c cfg.RPCConfig) {
   145  	config = c
   146  }
   147  
   148  func validatePage(page, perPage, totalCount int) int {
   149  	if perPage < 1 {
   150  		return 1
   151  	}
   152  
   153  	pages := ((totalCount - 1) / perPage) + 1
   154  	if page < 1 {
   155  		page = 1
   156  	} else if page > pages {
   157  		page = pages
   158  	}
   159  
   160  	return page
   161  }
   162  
   163  func validatePerPage(perPage int) int {
   164  	if perPage < 1 {
   165  		return defaultPerPage
   166  	} else if perPage > maxPerPage {
   167  		return maxPerPage
   168  	}
   169  	return perPage
   170  }
   171  
   172  func validateSkipCount(page, perPage int) int {
   173  	skipCount := (page - 1) * perPage
   174  	if skipCount < 0 {
   175  		return 0
   176  	}
   177  
   178  	return skipCount
   179  }