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.

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