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.

747 lines
23 KiB

  1. package node
  2. import (
  3. "bytes"
  4. "context"
  5. "errors"
  6. "fmt"
  7. "net"
  8. "net/http"
  9. "github.com/prometheus/client_golang/prometheus"
  10. "github.com/prometheus/client_golang/prometheus/promhttp"
  11. amino "github.com/tendermint/go-amino"
  12. abci "github.com/tendermint/tendermint/abci/types"
  13. "github.com/tendermint/tendermint/crypto/ed25519"
  14. cmn "github.com/tendermint/tendermint/libs/common"
  15. dbm "github.com/tendermint/tendermint/libs/db"
  16. "github.com/tendermint/tendermint/libs/log"
  17. bc "github.com/tendermint/tendermint/blockchain"
  18. cfg "github.com/tendermint/tendermint/config"
  19. cs "github.com/tendermint/tendermint/consensus"
  20. "github.com/tendermint/tendermint/evidence"
  21. mempl "github.com/tendermint/tendermint/mempool"
  22. "github.com/tendermint/tendermint/p2p"
  23. "github.com/tendermint/tendermint/p2p/pex"
  24. "github.com/tendermint/tendermint/privval"
  25. "github.com/tendermint/tendermint/proxy"
  26. rpccore "github.com/tendermint/tendermint/rpc/core"
  27. ctypes "github.com/tendermint/tendermint/rpc/core/types"
  28. grpccore "github.com/tendermint/tendermint/rpc/grpc"
  29. rpc "github.com/tendermint/tendermint/rpc/lib"
  30. rpcserver "github.com/tendermint/tendermint/rpc/lib/server"
  31. sm "github.com/tendermint/tendermint/state"
  32. "github.com/tendermint/tendermint/state/txindex"
  33. "github.com/tendermint/tendermint/state/txindex/kv"
  34. "github.com/tendermint/tendermint/state/txindex/null"
  35. "github.com/tendermint/tendermint/types"
  36. "github.com/tendermint/tendermint/version"
  37. _ "net/http/pprof"
  38. )
  39. //------------------------------------------------------------------------------
  40. // DBContext specifies config information for loading a new DB.
  41. type DBContext struct {
  42. ID string
  43. Config *cfg.Config
  44. }
  45. // DBProvider takes a DBContext and returns an instantiated DB.
  46. type DBProvider func(*DBContext) (dbm.DB, error)
  47. // DefaultDBProvider returns a database using the DBBackend and DBDir
  48. // specified in the ctx.Config.
  49. func DefaultDBProvider(ctx *DBContext) (dbm.DB, error) {
  50. dbType := dbm.DBBackendType(ctx.Config.DBBackend)
  51. return dbm.NewDB(ctx.ID, dbType, ctx.Config.DBDir()), nil
  52. }
  53. // GenesisDocProvider returns a GenesisDoc.
  54. // It allows the GenesisDoc to be pulled from sources other than the
  55. // filesystem, for instance from a distributed key-value store cluster.
  56. type GenesisDocProvider func() (*types.GenesisDoc, error)
  57. // DefaultGenesisDocProviderFunc returns a GenesisDocProvider that loads
  58. // the GenesisDoc from the config.GenesisFile() on the filesystem.
  59. func DefaultGenesisDocProviderFunc(config *cfg.Config) GenesisDocProvider {
  60. return func() (*types.GenesisDoc, error) {
  61. return types.GenesisDocFromFile(config.GenesisFile())
  62. }
  63. }
  64. // NodeProvider takes a config and a logger and returns a ready to go Node.
  65. type NodeProvider func(*cfg.Config, log.Logger) (*Node, error)
  66. // DefaultNewNode returns a Tendermint node with default settings for the
  67. // PrivValidator, ClientCreator, GenesisDoc, and DBProvider.
  68. // It implements NodeProvider.
  69. func DefaultNewNode(config *cfg.Config, logger log.Logger) (*Node, error) {
  70. return NewNode(config,
  71. privval.LoadOrGenFilePV(config.PrivValidatorFile()),
  72. proxy.DefaultClientCreator(config.ProxyApp, config.ABCI, config.DBDir()),
  73. DefaultGenesisDocProviderFunc(config),
  74. DefaultDBProvider,
  75. DefaultMetricsProvider(config.Instrumentation),
  76. logger,
  77. )
  78. }
  79. // MetricsProvider returns a consensus, p2p and mempool Metrics.
  80. type MetricsProvider func() (*cs.Metrics, *p2p.Metrics, *mempl.Metrics)
  81. // DefaultMetricsProvider returns Metrics build using Prometheus client library
  82. // if Prometheus is enabled. Otherwise, it returns no-op Metrics.
  83. func DefaultMetricsProvider(config *cfg.InstrumentationConfig) MetricsProvider {
  84. return func() (*cs.Metrics, *p2p.Metrics, *mempl.Metrics) {
  85. if config.Prometheus {
  86. return cs.PrometheusMetrics(), p2p.PrometheusMetrics(), mempl.PrometheusMetrics()
  87. }
  88. return cs.NopMetrics(), p2p.NopMetrics(), mempl.NopMetrics()
  89. }
  90. }
  91. //------------------------------------------------------------------------------
  92. // Node is the highest level interface to a full Tendermint node.
  93. // It includes all configuration information and running services.
  94. type Node struct {
  95. cmn.BaseService
  96. // config
  97. config *cfg.Config
  98. genesisDoc *types.GenesisDoc // initial validator set
  99. privValidator types.PrivValidator // local node's validator key
  100. // network
  101. sw *p2p.Switch // p2p connections
  102. addrBook pex.AddrBook // known peers
  103. // services
  104. eventBus *types.EventBus // pub/sub for services
  105. stateDB dbm.DB
  106. blockStore *bc.BlockStore // store the blockchain to disk
  107. bcReactor *bc.BlockchainReactor // for fast-syncing
  108. mempoolReactor *mempl.MempoolReactor // for gossipping transactions
  109. consensusState *cs.ConsensusState // latest consensus state
  110. consensusReactor *cs.ConsensusReactor // for participating in the consensus
  111. evidencePool *evidence.EvidencePool // tracking evidence
  112. proxyApp proxy.AppConns // connection to the application
  113. rpcListeners []net.Listener // rpc servers
  114. txIndexer txindex.TxIndexer
  115. indexerService *txindex.IndexerService
  116. prometheusSrv *http.Server
  117. }
  118. // NewNode returns a new, ready to go, Tendermint Node.
  119. func NewNode(config *cfg.Config,
  120. privValidator types.PrivValidator,
  121. clientCreator proxy.ClientCreator,
  122. genesisDocProvider GenesisDocProvider,
  123. dbProvider DBProvider,
  124. metricsProvider MetricsProvider,
  125. logger log.Logger) (*Node, error) {
  126. // Get BlockStore
  127. blockStoreDB, err := dbProvider(&DBContext{"blockstore", config})
  128. if err != nil {
  129. return nil, err
  130. }
  131. blockStore := bc.NewBlockStore(blockStoreDB)
  132. // Get State
  133. stateDB, err := dbProvider(&DBContext{"state", config})
  134. if err != nil {
  135. return nil, err
  136. }
  137. // Get genesis doc
  138. // TODO: move to state package?
  139. genDoc, err := loadGenesisDoc(stateDB)
  140. if err != nil {
  141. genDoc, err = genesisDocProvider()
  142. if err != nil {
  143. return nil, err
  144. }
  145. // save genesis doc to prevent a certain class of user errors (e.g. when it
  146. // was changed, accidentally or not). Also good for audit trail.
  147. saveGenesisDoc(stateDB, genDoc)
  148. }
  149. state, err := sm.LoadStateFromDBOrGenesisDoc(stateDB, genDoc)
  150. if err != nil {
  151. return nil, err
  152. }
  153. // Create the proxyApp, which manages connections (consensus, mempool, query)
  154. // and sync tendermint and the app by performing a handshake
  155. // and replaying any necessary blocks
  156. consensusLogger := logger.With("module", "consensus")
  157. handshaker := cs.NewHandshaker(stateDB, state, blockStore, genDoc)
  158. handshaker.SetLogger(consensusLogger)
  159. proxyApp := proxy.NewAppConns(clientCreator, handshaker)
  160. proxyApp.SetLogger(logger.With("module", "proxy"))
  161. if err := proxyApp.Start(); err != nil {
  162. return nil, fmt.Errorf("Error starting proxy app connections: %v", err)
  163. }
  164. // reload the state (it may have been updated by the handshake)
  165. state = sm.LoadState(stateDB)
  166. // If an address is provided, listen on the socket for a
  167. // connection from an external signing process.
  168. if config.PrivValidatorListenAddr != "" {
  169. var (
  170. // TODO: persist this key so external signer
  171. // can actually authenticate us
  172. privKey = ed25519.GenPrivKey()
  173. pvsc = privval.NewSocketPV(
  174. logger.With("module", "privval"),
  175. config.PrivValidatorListenAddr,
  176. privKey,
  177. )
  178. )
  179. if err := pvsc.Start(); err != nil {
  180. return nil, fmt.Errorf("Error starting private validator client: %v", err)
  181. }
  182. privValidator = pvsc
  183. }
  184. // Decide whether to fast-sync or not
  185. // We don't fast-sync when the only validator is us.
  186. fastSync := config.FastSync
  187. if state.Validators.Size() == 1 {
  188. addr, _ := state.Validators.GetByIndex(0)
  189. if bytes.Equal(privValidator.GetAddress(), addr) {
  190. fastSync = false
  191. }
  192. }
  193. // Log whether this node is a validator or an observer
  194. if state.Validators.HasAddress(privValidator.GetAddress()) {
  195. consensusLogger.Info("This node is a validator", "addr", privValidator.GetAddress(), "pubKey", privValidator.GetPubKey())
  196. } else {
  197. consensusLogger.Info("This node is not a validator", "addr", privValidator.GetAddress(), "pubKey", privValidator.GetPubKey())
  198. }
  199. csMetrics, p2pMetrics, memplMetrics := metricsProvider()
  200. // Make MempoolReactor
  201. mempoolLogger := logger.With("module", "mempool")
  202. mempool := mempl.NewMempool(
  203. config.Mempool,
  204. proxyApp.Mempool(),
  205. state.LastBlockHeight,
  206. mempl.WithMetrics(memplMetrics),
  207. )
  208. mempool.SetLogger(mempoolLogger)
  209. mempool.InitWAL() // no need to have the mempool wal during tests
  210. mempoolReactor := mempl.NewMempoolReactor(config.Mempool, mempool)
  211. mempoolReactor.SetLogger(mempoolLogger)
  212. if config.Consensus.WaitForTxs() {
  213. mempool.EnableTxsAvailable()
  214. }
  215. // Make Evidence Reactor
  216. evidenceDB, err := dbProvider(&DBContext{"evidence", config})
  217. if err != nil {
  218. return nil, err
  219. }
  220. evidenceLogger := logger.With("module", "evidence")
  221. evidenceStore := evidence.NewEvidenceStore(evidenceDB)
  222. evidencePool := evidence.NewEvidencePool(stateDB, evidenceStore)
  223. evidencePool.SetLogger(evidenceLogger)
  224. evidenceReactor := evidence.NewEvidenceReactor(evidencePool)
  225. evidenceReactor.SetLogger(evidenceLogger)
  226. blockExecLogger := logger.With("module", "state")
  227. // make block executor for consensus and blockchain reactors to execute blocks
  228. blockExec := sm.NewBlockExecutor(stateDB, blockExecLogger, proxyApp.Consensus(), mempool, evidencePool)
  229. // Make BlockchainReactor
  230. bcReactor := bc.NewBlockchainReactor(state.Copy(), blockExec, blockStore, fastSync)
  231. bcReactor.SetLogger(logger.With("module", "blockchain"))
  232. // Make ConsensusReactor
  233. consensusState := cs.NewConsensusState(
  234. config.Consensus,
  235. state.Copy(),
  236. blockExec,
  237. blockStore,
  238. mempool,
  239. evidencePool,
  240. cs.WithMetrics(csMetrics),
  241. )
  242. consensusState.SetLogger(consensusLogger)
  243. if privValidator != nil {
  244. consensusState.SetPrivValidator(privValidator)
  245. }
  246. consensusReactor := cs.NewConsensusReactor(consensusState, fastSync)
  247. consensusReactor.SetLogger(consensusLogger)
  248. p2pLogger := logger.With("module", "p2p")
  249. sw := p2p.NewSwitch(config.P2P, p2p.WithMetrics(p2pMetrics))
  250. sw.SetLogger(p2pLogger)
  251. sw.AddReactor("MEMPOOL", mempoolReactor)
  252. sw.AddReactor("BLOCKCHAIN", bcReactor)
  253. sw.AddReactor("CONSENSUS", consensusReactor)
  254. sw.AddReactor("EVIDENCE", evidenceReactor)
  255. // Optionally, start the pex reactor
  256. //
  257. // TODO:
  258. //
  259. // We need to set Seeds and PersistentPeers on the switch,
  260. // since it needs to be able to use these (and their DNS names)
  261. // even if the PEX is off. We can include the DNS name in the NetAddress,
  262. // but it would still be nice to have a clear list of the current "PersistentPeers"
  263. // somewhere that we can return with net_info.
  264. //
  265. // If PEX is on, it should handle dialing the seeds. Otherwise the switch does it.
  266. // Note we currently use the addrBook regardless at least for AddOurAddress
  267. addrBook := pex.NewAddrBook(config.P2P.AddrBookFile(), config.P2P.AddrBookStrict)
  268. addrBook.SetLogger(p2pLogger.With("book", config.P2P.AddrBookFile()))
  269. if config.P2P.PexReactor {
  270. // TODO persistent peers ? so we can have their DNS addrs saved
  271. pexReactor := pex.NewPEXReactor(addrBook,
  272. &pex.PEXReactorConfig{
  273. Seeds: cmn.SplitAndTrim(config.P2P.Seeds, ",", " "),
  274. SeedMode: config.P2P.SeedMode,
  275. })
  276. pexReactor.SetLogger(p2pLogger)
  277. sw.AddReactor("PEX", pexReactor)
  278. }
  279. sw.SetAddrBook(addrBook)
  280. // Filter peers by addr or pubkey with an ABCI query.
  281. // If the query return code is OK, add peer.
  282. // XXX: Query format subject to change
  283. if config.FilterPeers {
  284. // NOTE: addr is ip:port
  285. sw.SetAddrFilter(func(addr net.Addr) error {
  286. resQuery, err := proxyApp.Query().QuerySync(abci.RequestQuery{Path: cmn.Fmt("/p2p/filter/addr/%s", addr.String())})
  287. if err != nil {
  288. return err
  289. }
  290. if resQuery.IsErr() {
  291. return fmt.Errorf("Error querying abci app: %v", resQuery)
  292. }
  293. return nil
  294. })
  295. sw.SetIDFilter(func(id p2p.ID) error {
  296. resQuery, err := proxyApp.Query().QuerySync(abci.RequestQuery{Path: cmn.Fmt("/p2p/filter/id/%s", id)})
  297. if err != nil {
  298. return err
  299. }
  300. if resQuery.IsErr() {
  301. return fmt.Errorf("Error querying abci app: %v", resQuery)
  302. }
  303. return nil
  304. })
  305. }
  306. eventBus := types.NewEventBus()
  307. eventBus.SetLogger(logger.With("module", "events"))
  308. // services which will be publishing and/or subscribing for messages (events)
  309. // consensusReactor will set it on consensusState and blockExecutor
  310. consensusReactor.SetEventBus(eventBus)
  311. // Transaction indexing
  312. var txIndexer txindex.TxIndexer
  313. switch config.TxIndex.Indexer {
  314. case "kv":
  315. store, err := dbProvider(&DBContext{"tx_index", config})
  316. if err != nil {
  317. return nil, err
  318. }
  319. if config.TxIndex.IndexTags != "" {
  320. txIndexer = kv.NewTxIndex(store, kv.IndexTags(cmn.SplitAndTrim(config.TxIndex.IndexTags, ",", " ")))
  321. } else if config.TxIndex.IndexAllTags {
  322. txIndexer = kv.NewTxIndex(store, kv.IndexAllTags())
  323. } else {
  324. txIndexer = kv.NewTxIndex(store)
  325. }
  326. default:
  327. txIndexer = &null.TxIndex{}
  328. }
  329. indexerService := txindex.NewIndexerService(txIndexer, eventBus)
  330. indexerService.SetLogger(logger.With("module", "txindex"))
  331. // run the profile server
  332. profileHost := config.ProfListenAddress
  333. if profileHost != "" {
  334. go func() {
  335. logger.Error("Profile server", "err", http.ListenAndServe(profileHost, nil))
  336. }()
  337. }
  338. node := &Node{
  339. config: config,
  340. genesisDoc: genDoc,
  341. privValidator: privValidator,
  342. sw: sw,
  343. addrBook: addrBook,
  344. stateDB: stateDB,
  345. blockStore: blockStore,
  346. bcReactor: bcReactor,
  347. mempoolReactor: mempoolReactor,
  348. consensusState: consensusState,
  349. consensusReactor: consensusReactor,
  350. evidencePool: evidencePool,
  351. proxyApp: proxyApp,
  352. txIndexer: txIndexer,
  353. indexerService: indexerService,
  354. eventBus: eventBus,
  355. }
  356. node.BaseService = *cmn.NewBaseService(logger, "Node", node)
  357. return node, nil
  358. }
  359. // OnStart starts the Node. It implements cmn.Service.
  360. func (n *Node) OnStart() error {
  361. err := n.eventBus.Start()
  362. if err != nil {
  363. return err
  364. }
  365. // Create & add listener
  366. l := p2p.NewDefaultListener(
  367. n.config.P2P.ListenAddress,
  368. n.config.P2P.ExternalAddress,
  369. n.config.P2P.UPNP,
  370. n.Logger.With("module", "p2p"))
  371. n.sw.AddListener(l)
  372. // Generate node PrivKey
  373. // TODO: pass in like privValidator
  374. nodeKey, err := p2p.LoadOrGenNodeKey(n.config.NodeKeyFile())
  375. if err != nil {
  376. return err
  377. }
  378. n.Logger.Info("P2P Node ID", "ID", nodeKey.ID(), "file", n.config.NodeKeyFile())
  379. nodeInfo := n.makeNodeInfo(nodeKey.ID())
  380. n.sw.SetNodeInfo(nodeInfo)
  381. n.sw.SetNodeKey(nodeKey)
  382. // Add ourselves to addrbook to prevent dialing ourselves
  383. n.addrBook.AddOurAddress(nodeInfo.NetAddress())
  384. // Add private IDs to addrbook to block those peers being added
  385. n.addrBook.AddPrivateIDs(cmn.SplitAndTrim(n.config.P2P.PrivatePeerIDs, ",", " "))
  386. // Start the RPC server before the P2P server
  387. // so we can eg. receive txs for the first block
  388. if n.config.RPC.ListenAddress != "" {
  389. listeners, err := n.startRPC()
  390. if err != nil {
  391. return err
  392. }
  393. n.rpcListeners = listeners
  394. }
  395. if n.config.Instrumentation.Prometheus &&
  396. n.config.Instrumentation.PrometheusListenAddr != "" {
  397. n.prometheusSrv = n.startPrometheusServer(n.config.Instrumentation.PrometheusListenAddr)
  398. }
  399. // Start the switch (the P2P server).
  400. err = n.sw.Start()
  401. if err != nil {
  402. return err
  403. }
  404. // Always connect to persistent peers
  405. if n.config.P2P.PersistentPeers != "" {
  406. err = n.sw.DialPeersAsync(n.addrBook, cmn.SplitAndTrim(n.config.P2P.PersistentPeers, ",", " "), true)
  407. if err != nil {
  408. return err
  409. }
  410. }
  411. // start tx indexer
  412. return n.indexerService.Start()
  413. }
  414. // OnStop stops the Node. It implements cmn.Service.
  415. func (n *Node) OnStop() {
  416. n.BaseService.OnStop()
  417. n.Logger.Info("Stopping Node")
  418. // first stop the non-reactor services
  419. n.eventBus.Stop()
  420. n.indexerService.Stop()
  421. // now stop the reactors
  422. // TODO: gracefully disconnect from peers.
  423. n.sw.Stop()
  424. // finally stop the listeners / external services
  425. for _, l := range n.rpcListeners {
  426. n.Logger.Info("Closing rpc listener", "listener", l)
  427. if err := l.Close(); err != nil {
  428. n.Logger.Error("Error closing listener", "listener", l, "err", err)
  429. }
  430. }
  431. if pvsc, ok := n.privValidator.(*privval.SocketPV); ok {
  432. if err := pvsc.Stop(); err != nil {
  433. n.Logger.Error("Error stopping priv validator socket client", "err", err)
  434. }
  435. }
  436. if n.prometheusSrv != nil {
  437. if err := n.prometheusSrv.Shutdown(context.Background()); err != nil {
  438. // Error from closing listeners, or context timeout:
  439. n.Logger.Error("Prometheus HTTP server Shutdown", "err", err)
  440. }
  441. }
  442. }
  443. // RunForever waits for an interrupt signal and stops the node.
  444. func (n *Node) RunForever() {
  445. // Sleep forever and then...
  446. cmn.TrapSignal(func() {
  447. n.Stop()
  448. })
  449. }
  450. // AddListener adds a listener to accept inbound peer connections.
  451. // It should be called before starting the Node.
  452. // The first listener is the primary listener (in NodeInfo)
  453. func (n *Node) AddListener(l p2p.Listener) {
  454. n.sw.AddListener(l)
  455. }
  456. // ConfigureRPC sets all variables in rpccore so they will serve
  457. // rpc calls from this node
  458. func (n *Node) ConfigureRPC() {
  459. rpccore.SetStateDB(n.stateDB)
  460. rpccore.SetBlockStore(n.blockStore)
  461. rpccore.SetConsensusState(n.consensusState)
  462. rpccore.SetMempool(n.mempoolReactor.Mempool)
  463. rpccore.SetEvidencePool(n.evidencePool)
  464. rpccore.SetSwitch(n.sw)
  465. rpccore.SetPubKey(n.privValidator.GetPubKey())
  466. rpccore.SetGenesisDoc(n.genesisDoc)
  467. rpccore.SetAddrBook(n.addrBook)
  468. rpccore.SetProxyAppQuery(n.proxyApp.Query())
  469. rpccore.SetTxIndexer(n.txIndexer)
  470. rpccore.SetConsensusReactor(n.consensusReactor)
  471. rpccore.SetEventBus(n.eventBus)
  472. rpccore.SetLogger(n.Logger.With("module", "rpc"))
  473. }
  474. func (n *Node) startRPC() ([]net.Listener, error) {
  475. n.ConfigureRPC()
  476. listenAddrs := cmn.SplitAndTrim(n.config.RPC.ListenAddress, ",", " ")
  477. coreCodec := amino.NewCodec()
  478. ctypes.RegisterAmino(coreCodec)
  479. if n.config.RPC.Unsafe {
  480. rpccore.AddUnsafeRoutes()
  481. }
  482. // we may expose the rpc over both a unix and tcp socket
  483. listeners := make([]net.Listener, len(listenAddrs))
  484. for i, listenAddr := range listenAddrs {
  485. mux := http.NewServeMux()
  486. rpcLogger := n.Logger.With("module", "rpc-server")
  487. wm := rpcserver.NewWebsocketManager(rpccore.Routes, coreCodec, rpcserver.EventSubscriber(n.eventBus))
  488. wm.SetLogger(rpcLogger.With("protocol", "websocket"))
  489. mux.HandleFunc("/websocket", wm.WebsocketHandler)
  490. rpcserver.RegisterRPCFuncs(mux, rpccore.Routes, coreCodec, rpcLogger)
  491. listener, err := rpcserver.StartHTTPServer(
  492. listenAddr,
  493. mux,
  494. rpcLogger,
  495. rpcserver.Config{MaxOpenConnections: n.config.RPC.MaxOpenConnections},
  496. )
  497. if err != nil {
  498. return nil, err
  499. }
  500. listeners[i] = listener
  501. }
  502. // we expose a simplified api over grpc for convenience to app devs
  503. grpcListenAddr := n.config.RPC.GRPCListenAddress
  504. if grpcListenAddr != "" {
  505. listener, err := grpccore.StartGRPCServer(
  506. grpcListenAddr,
  507. grpccore.Config{
  508. MaxOpenConnections: n.config.RPC.GRPCMaxOpenConnections,
  509. },
  510. )
  511. if err != nil {
  512. return nil, err
  513. }
  514. listeners = append(listeners, listener)
  515. }
  516. return listeners, nil
  517. }
  518. // startPrometheusServer starts a Prometheus HTTP server, listening for metrics
  519. // collectors on addr.
  520. func (n *Node) startPrometheusServer(addr string) *http.Server {
  521. srv := &http.Server{
  522. Addr: addr,
  523. Handler: promhttp.InstrumentMetricHandler(
  524. prometheus.DefaultRegisterer, promhttp.HandlerFor(
  525. prometheus.DefaultGatherer,
  526. promhttp.HandlerOpts{MaxRequestsInFlight: n.config.Instrumentation.MaxOpenConnections},
  527. ),
  528. ),
  529. }
  530. go func() {
  531. if err := srv.ListenAndServe(); err != http.ErrServerClosed {
  532. // Error starting or closing listener:
  533. n.Logger.Error("Prometheus HTTP server ListenAndServe", "err", err)
  534. }
  535. }()
  536. return srv
  537. }
  538. // Switch returns the Node's Switch.
  539. func (n *Node) Switch() *p2p.Switch {
  540. return n.sw
  541. }
  542. // BlockStore returns the Node's BlockStore.
  543. func (n *Node) BlockStore() *bc.BlockStore {
  544. return n.blockStore
  545. }
  546. // ConsensusState returns the Node's ConsensusState.
  547. func (n *Node) ConsensusState() *cs.ConsensusState {
  548. return n.consensusState
  549. }
  550. // ConsensusReactor returns the Node's ConsensusReactor.
  551. func (n *Node) ConsensusReactor() *cs.ConsensusReactor {
  552. return n.consensusReactor
  553. }
  554. // MempoolReactor returns the Node's MempoolReactor.
  555. func (n *Node) MempoolReactor() *mempl.MempoolReactor {
  556. return n.mempoolReactor
  557. }
  558. // EvidencePool returns the Node's EvidencePool.
  559. func (n *Node) EvidencePool() *evidence.EvidencePool {
  560. return n.evidencePool
  561. }
  562. // EventBus returns the Node's EventBus.
  563. func (n *Node) EventBus() *types.EventBus {
  564. return n.eventBus
  565. }
  566. // PrivValidator returns the Node's PrivValidator.
  567. // XXX: for convenience only!
  568. func (n *Node) PrivValidator() types.PrivValidator {
  569. return n.privValidator
  570. }
  571. // GenesisDoc returns the Node's GenesisDoc.
  572. func (n *Node) GenesisDoc() *types.GenesisDoc {
  573. return n.genesisDoc
  574. }
  575. // ProxyApp returns the Node's AppConns, representing its connections to the ABCI application.
  576. func (n *Node) ProxyApp() proxy.AppConns {
  577. return n.proxyApp
  578. }
  579. func (n *Node) makeNodeInfo(nodeID p2p.ID) p2p.NodeInfo {
  580. txIndexerStatus := "on"
  581. if _, ok := n.txIndexer.(*null.TxIndex); ok {
  582. txIndexerStatus = "off"
  583. }
  584. nodeInfo := p2p.NodeInfo{
  585. ID: nodeID,
  586. Network: n.genesisDoc.ChainID,
  587. Version: version.Version,
  588. Channels: []byte{
  589. bc.BlockchainChannel,
  590. cs.StateChannel, cs.DataChannel, cs.VoteChannel, cs.VoteSetBitsChannel,
  591. mempl.MempoolChannel,
  592. evidence.EvidenceChannel,
  593. },
  594. Moniker: n.config.Moniker,
  595. Other: []string{
  596. cmn.Fmt("amino_version=%v", amino.Version),
  597. cmn.Fmt("p2p_version=%v", p2p.Version),
  598. cmn.Fmt("consensus_version=%v", cs.Version),
  599. cmn.Fmt("rpc_version=%v/%v", rpc.Version, rpccore.Version),
  600. cmn.Fmt("tx_index=%v", txIndexerStatus),
  601. },
  602. }
  603. if n.config.P2P.PexReactor {
  604. nodeInfo.Channels = append(nodeInfo.Channels, pex.PexChannel)
  605. }
  606. rpcListenAddr := n.config.RPC.ListenAddress
  607. nodeInfo.Other = append(nodeInfo.Other, cmn.Fmt("rpc_addr=%v", rpcListenAddr))
  608. if !n.sw.IsListening() {
  609. return nodeInfo
  610. }
  611. p2pListener := n.sw.Listeners()[0]
  612. p2pHost := p2pListener.ExternalAddressHost()
  613. p2pPort := p2pListener.ExternalAddress().Port
  614. nodeInfo.ListenAddr = cmn.Fmt("%v:%v", p2pHost, p2pPort)
  615. return nodeInfo
  616. }
  617. //------------------------------------------------------------------------------
  618. // NodeInfo returns the Node's Info from the Switch.
  619. func (n *Node) NodeInfo() p2p.NodeInfo {
  620. return n.sw.NodeInfo()
  621. }
  622. //------------------------------------------------------------------------------
  623. var (
  624. genesisDocKey = []byte("genesisDoc")
  625. )
  626. // panics if failed to unmarshal bytes
  627. func loadGenesisDoc(db dbm.DB) (*types.GenesisDoc, error) {
  628. bytes := db.Get(genesisDocKey)
  629. if len(bytes) == 0 {
  630. return nil, errors.New("Genesis doc not found")
  631. }
  632. var genDoc *types.GenesisDoc
  633. err := cdc.UnmarshalJSON(bytes, &genDoc)
  634. if err != nil {
  635. cmn.PanicCrisis(fmt.Sprintf("Failed to load genesis doc due to unmarshaling error: %v (bytes: %X)", err, bytes))
  636. }
  637. return genDoc, nil
  638. }
  639. // panics if failed to marshal the given genesis document
  640. func saveGenesisDoc(db dbm.DB, genDoc *types.GenesisDoc) {
  641. bytes, err := cdc.MarshalJSON(genDoc)
  642. if err != nil {
  643. cmn.PanicCrisis(fmt.Sprintf("Failed to save genesis doc due to marshaling error: %v", err))
  644. }
  645. db.SetSync(genesisDocKey, bytes)
  646. }