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.

199 lines
5.0 KiB

10 years ago
11 years ago
11 years ago
11 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
11 years ago
11 years ago
10 years ago
11 years ago
11 years ago
10 years ago
11 years ago
10 years ago
  1. package daemon
  2. import (
  3. "os"
  4. "os/signal"
  5. bc "github.com/tendermint/tendermint/blockchain"
  6. . "github.com/tendermint/tendermint/common"
  7. "github.com/tendermint/tendermint/config"
  8. "github.com/tendermint/tendermint/consensus"
  9. dbm "github.com/tendermint/tendermint/db"
  10. mempl "github.com/tendermint/tendermint/mempool"
  11. "github.com/tendermint/tendermint/p2p"
  12. "github.com/tendermint/tendermint/rpc"
  13. sm "github.com/tendermint/tendermint/state"
  14. )
  15. type Node struct {
  16. lz []p2p.Listener
  17. sw *p2p.Switch
  18. book *p2p.AddrBook
  19. blockStore *bc.BlockStore
  20. pexReactor *p2p.PEXReactor
  21. bcReactor *bc.BlockchainReactor
  22. mempoolReactor *mempl.MempoolReactor
  23. consensusState *consensus.ConsensusState
  24. consensusReactor *consensus.ConsensusReactor
  25. privValidator *sm.PrivValidator
  26. }
  27. func NewNode() *Node {
  28. // Get BlockStore
  29. blockStoreDB := dbm.GetDB("blockstore")
  30. blockStore := bc.NewBlockStore(blockStoreDB)
  31. // Get State
  32. stateDB := dbm.GetDB("state")
  33. state := sm.LoadState(stateDB)
  34. if state == nil {
  35. state = sm.MakeGenesisStateFromFile(stateDB, config.App().GetString("GenesisFile"))
  36. state.Save()
  37. }
  38. // Get PrivValidator
  39. var privValidator *sm.PrivValidator
  40. if _, err := os.Stat(config.App().GetString("PrivValidatorFile")); err == nil {
  41. privValidator = sm.LoadPrivValidator(config.App().GetString("PrivValidatorFile"))
  42. log.Info("Loaded PrivValidator", "file", config.App().GetString("PrivValidatorFile"), "privValidator", privValidator)
  43. } else {
  44. log.Info("No PrivValidator found", "file", config.App().GetString("PrivValidatorFile"))
  45. }
  46. // Get PEXReactor
  47. book := p2p.NewAddrBook(config.App().GetString("AddrBookFile"))
  48. pexReactor := p2p.NewPEXReactor(book)
  49. // Get BlockchainReactor
  50. bcReactor := bc.NewBlockchainReactor(blockStore)
  51. // Get MempoolReactor
  52. mempool := mempl.NewMempool(state.Copy())
  53. mempoolReactor := mempl.NewMempoolReactor(mempool)
  54. // Get ConsensusReactor
  55. consensusState := consensus.NewConsensusState(state, blockStore, mempoolReactor)
  56. consensusReactor := consensus.NewConsensusReactor(consensusState, blockStore)
  57. if privValidator != nil {
  58. consensusReactor.SetPrivValidator(privValidator)
  59. }
  60. sw := p2p.NewSwitch()
  61. sw.SetChainId(state.Hash(), config.App().GetString("Network"))
  62. sw.AddReactor("PEX", pexReactor)
  63. //sw.AddReactor("BLOCKCHAIN", bcReactor)
  64. sw.AddReactor("MEMPOOL", mempoolReactor)
  65. sw.AddReactor("CONSENSUS", consensusReactor)
  66. return &Node{
  67. sw: sw,
  68. book: book,
  69. blockStore: blockStore,
  70. pexReactor: pexReactor,
  71. bcReactor: bcReactor,
  72. mempoolReactor: mempoolReactor,
  73. consensusState: consensusState,
  74. consensusReactor: consensusReactor,
  75. privValidator: privValidator,
  76. }
  77. }
  78. func (n *Node) Start() {
  79. log.Info("Starting Node")
  80. for _, l := range n.lz {
  81. go n.inboundConnectionRoutine(l)
  82. }
  83. n.book.Start()
  84. n.sw.StartAll()
  85. }
  86. func (n *Node) Stop() {
  87. log.Info("Stopping Node")
  88. // TODO: gracefully disconnect from peers.
  89. n.sw.StopAll()
  90. n.book.Stop()
  91. }
  92. // Add a Listener to accept inbound peer connections.
  93. func (n *Node) AddListener(l p2p.Listener) {
  94. log.Info(Fmt("Added %v", l))
  95. n.lz = append(n.lz, l)
  96. n.book.AddOurAddress(l.ExternalAddress())
  97. }
  98. func (n *Node) inboundConnectionRoutine(l p2p.Listener) {
  99. for {
  100. inConn, ok := <-l.Connections()
  101. if !ok {
  102. break
  103. }
  104. // New inbound connection!
  105. peer, err := n.sw.AddPeerWithConnection(inConn, false)
  106. if err != nil {
  107. log.Info("Ignoring error from inbound connection: %v\n%v",
  108. peer, err)
  109. continue
  110. }
  111. // NOTE: We don't yet have the external address of the
  112. // remote (if they have a listener at all).
  113. // PEXReactor's pexRoutine will handle that.
  114. }
  115. // cleanup
  116. }
  117. func (n *Node) DialSeed() {
  118. addr := p2p.NewNetAddressString(config.App().GetString("SeedNode"))
  119. peer, err := n.sw.DialPeerWithAddress(addr)
  120. if err != nil {
  121. log.Error("Error dialing seed", "error", err)
  122. //n.book.MarkAttempt(addr)
  123. return
  124. } else {
  125. log.Info("Connected to seed", "peer", peer)
  126. n.book.AddAddress(addr, addr)
  127. }
  128. }
  129. func (n *Node) StartRpc() {
  130. rpc.SetRPCBlockStore(n.blockStore)
  131. rpc.SetRPCConsensusState(n.consensusState)
  132. rpc.SetRPCMempoolReactor(n.mempoolReactor)
  133. rpc.SetRPCSwitch(n.sw)
  134. rpc.StartHTTPServer()
  135. }
  136. func (n *Node) ConsensusState() *consensus.ConsensusState {
  137. return n.consensusState
  138. }
  139. func (n *Node) MempoolReactor() *mempl.MempoolReactor {
  140. return n.mempoolReactor
  141. }
  142. func Daemon() {
  143. // Create & start node
  144. n := NewNode()
  145. l := p2p.NewDefaultListener("tcp", config.App().GetString("ListenAddr"), false)
  146. n.AddListener(l)
  147. n.Start()
  148. // If seedNode is provided by config, dial out.
  149. if config.App().GetString("SeedNode") != "" {
  150. n.DialSeed()
  151. }
  152. // Run the RPC server.
  153. if config.App().GetString("RPC.HTTP.ListenAddr") != "" {
  154. n.StartRpc()
  155. }
  156. // Sleep forever and then...
  157. trapSignal(func() {
  158. n.Stop()
  159. })
  160. }
  161. func trapSignal(cb func()) {
  162. c := make(chan os.Signal, 1)
  163. signal.Notify(c, os.Interrupt)
  164. go func() {
  165. for sig := range c {
  166. log.Info(Fmt("captured %v, exiting..", sig))
  167. cb()
  168. os.Exit(1)
  169. }
  170. }()
  171. select {}
  172. }