13 "github.com/kr/secureheader"
14 log "github.com/sirupsen/logrus"
15 crypto "github.com/tendermint/go-crypto"
16 wire "github.com/tendermint/go-wire"
17 cmn "github.com/tendermint/tmlibs/common"
18 dbm "github.com/tendermint/tmlibs/db"
20 bc "github.com/bytom/blockchain"
21 "github.com/bytom/blockchain/accesstoken"
22 "github.com/bytom/blockchain/account"
23 "github.com/bytom/blockchain/asset"
24 "github.com/bytom/blockchain/pseudohsm"
25 "github.com/bytom/blockchain/txdb"
26 "github.com/bytom/blockchain/txfeed"
27 w "github.com/bytom/blockchain/wallet"
28 cfg "github.com/bytom/config"
29 "github.com/bytom/env"
30 "github.com/bytom/errors"
31 "github.com/bytom/p2p"
32 "github.com/bytom/protocol"
33 "github.com/bytom/types"
34 "github.com/bytom/util/browser"
35 "github.com/bytom/version"
39 httpReadTimeout = 2 * time.Minute
40 httpWriteTimeout = time.Hour
41 webAddress = "http://127.0.0.1:9888"
51 privKey crypto.PrivKeyEd25519 // local node's p2p key
52 sw *p2p.Switch // p2p connections
53 addrBook *p2p.AddrBook // known peers
55 evsw types.EventSwitch // pub/sub for services
56 blockStore *txdb.Store
57 bcReactor *bc.BlockchainReactor
58 accounts *account.Manager
59 assets *asset.Registry
62 func NewNodeDefault(config *cfg.Config) *Node {
63 return NewNode(config)
66 func RedirectHandler(next http.Handler) http.Handler {
67 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
68 if req.URL.Path == "/" {
69 http.Redirect(w, req, "/dashboard/", http.StatusFound)
72 next.ServeHTTP(w, req)
76 type waitHandler struct {
81 func (wh *waitHandler) Set(h http.Handler) {
86 func (wh *waitHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
88 wh.h.ServeHTTP(w, req)
91 func rpcInit(h *bc.BlockchainReactor, config *cfg.Config, accessTokens *accesstoken.CredentialStore) {
92 // The waitHandler accepts incoming requests, but blocks until its underlying
93 // handler is set, when the second phase is complete.
94 var coreHandler waitHandler
96 mux := http.NewServeMux()
97 mux.Handle("/", &coreHandler)
99 var handler http.Handler = mux
101 if config.Auth.Disable == false {
102 handler = bc.AuthHandler(handler, accessTokens)
104 handler = RedirectHandler(handler)
106 secureheader.DefaultConfig.PermitClearLoopback = true
107 secureheader.DefaultConfig.HTTPSRedirect = false
108 secureheader.DefaultConfig.Next = handler
110 server := &http.Server{
111 // Note: we should not set TLSConfig here;
112 // we took care of TLS with the listener in maybeUseTLS.
113 Handler: secureheader.DefaultConfig,
114 ReadTimeout: httpReadTimeout,
115 WriteTimeout: httpWriteTimeout,
116 // Disable HTTP/2 for now until the Go implementation is more stable.
117 // https://github.com/golang/go/issues/16450
118 // https://github.com/golang/go/issues/17071
119 TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){},
121 listenAddr := env.String("LISTEN", config.ApiAddress)
122 listener, err := net.Listen("tcp", *listenAddr)
124 cmn.Exit(cmn.Fmt("Failed to register tcp port: %v", err))
127 // The `Serve` call has to happen in its own goroutine because
128 // it's blocking and we need to proceed to the rest of the core setup after
131 if err := server.Serve(listener); err != nil {
132 log.WithField("error", errors.Wrap(err, "Serve")).Error("Rpc server")
138 func NewNode(config *cfg.Config) *Node {
139 ctx := context.Background()
142 txDB := dbm.NewDB("txdb", config.DBBackend, config.DBDir())
143 store := txdb.NewStore(txDB)
145 tokenDB := dbm.NewDB("accesstoken", config.DBBackend, config.DBDir())
146 accessTokens := accesstoken.NewStore(tokenDB)
148 privKey := crypto.GenPrivKeyEd25519()
151 eventSwitch := types.NewEventSwitch()
152 _, err := eventSwitch.Start()
154 cmn.Exit(cmn.Fmt("Failed to start switch: %v", err))
157 sw := p2p.NewSwitch(config.P2P)
159 genesisBlock := cfg.GenerateGenesisBlock()
161 txPool := protocol.NewTxPool()
162 chain, err := protocol.NewChain(genesisBlock.Hash(), store, txPool)
164 cmn.Exit(cmn.Fmt("Failed to create chain structure: %v", err))
167 if chain.BestBlockHash() == nil {
168 if err := chain.SaveBlock(genesisBlock); err != nil {
169 cmn.Exit(cmn.Fmt("Failed to save genesisBlock to store: %v", err))
171 if err := chain.ConnectBlock(genesisBlock); err != nil {
172 cmn.Exit(cmn.Fmt("Failed to connect genesisBlock to chain: %v", err))
176 var accounts *account.Manager = nil
177 var assets *asset.Registry = nil
178 var wallet *w.Wallet = nil
179 var txFeed *txfeed.Tracker = nil
181 txFeedDB := dbm.NewDB("txfeeds", config.DBBackend, config.DBDir())
182 txFeed = txfeed.NewTracker(txFeedDB, chain)
184 if err = txFeed.Prepare(ctx); err != nil {
185 log.WithField("error", err).Error("start txfeed")
189 if !config.Wallet.Disable {
190 walletDB := dbm.NewDB("wallet", config.DBBackend, config.DBDir())
191 accounts = account.NewManager(walletDB, chain)
192 assets = asset.NewRegistry(walletDB, chain)
193 wallet, err = w.NewWallet(walletDB, accounts, assets, chain)
195 log.WithField("error", err).Error("init NewWallet")
199 hsm, err := pseudohsm.New(config.KeysDir())
201 cmn.Exit(cmn.Fmt("initialize HSM failed: %v", err))
203 bcReactor := bc.NewBlockchainReactor(chain, txPool, accounts, assets, sw, hsm, wallet, txFeed, accessTokens, config.Mining)
205 sw.AddReactor("BLOCKCHAIN", bcReactor)
207 rpcInit(bcReactor, config, accessTokens)
208 // Optionally, start the pex reactor
209 var addrBook *p2p.AddrBook
210 if config.P2P.PexReactor {
211 addrBook = p2p.NewAddrBook(config.P2P.AddrBookFile(), config.P2P.AddrBookStrict)
212 pexReactor := p2p.NewPEXReactor(addrBook)
213 sw.AddReactor("PEX", pexReactor)
216 // run the profile server
217 profileHost := config.ProfListenAddress
218 if profileHost != "" {
219 // Profiling bytomd programs.see (https://blog.golang.org/profiling-go-programs)
220 // go tool pprof http://profileHose/debug/pprof/heap
222 http.ListenAndServe(profileHost, nil)
234 bcReactor: bcReactor,
239 node.BaseService = *cmn.NewBaseService(nil, "Node", node)
244 // Lanch web broser or not
245 func lanchWebBroser(lanch bool) {
247 log.Info("Launching System Browser with :", webAddress)
248 if err := browser.Open(webAddress); err != nil {
249 log.Error(err.Error())
255 func (n *Node) OnStart() error {
256 // Create & add listener
257 p, address := ProtocolAndAddress(n.config.P2P.ListenAddress)
258 l := p2p.NewDefaultListener(p, address, n.config.P2P.SkipUPNP, nil)
262 n.sw.SetNodeInfo(n.makeNodeInfo())
263 n.sw.SetNodePrivKey(n.privKey)
264 _, err := n.sw.Start()
269 // If seeds exist, add them to the address book and dial out
270 if n.config.P2P.Seeds != "" {
272 seeds := strings.Split(n.config.P2P.Seeds, ",")
273 if err := n.DialSeeds(seeds); err != nil {
277 lanchWebBroser(!n.config.Web.Closed)
281 func (n *Node) OnStop() {
282 n.BaseService.OnStop()
284 log.Info("Stopping Node")
285 // TODO: gracefully disconnect from peers.
290 func (n *Node) RunForever() {
291 // Sleep forever and then...
292 cmn.TrapSignal(func() {
297 // Add a Listener to accept inbound peer connections.
298 // Add listeners before starting the Node.
299 // The first listener is the primary listener (in NodeInfo)
300 func (n *Node) AddListener(l p2p.Listener) {
304 func (n *Node) Switch() *p2p.Switch {
308 func (n *Node) EventSwitch() types.EventSwitch {
312 func (n *Node) makeNodeInfo() *p2p.NodeInfo {
313 nodeInfo := &p2p.NodeInfo{
314 PubKey: n.privKey.PubKey().Unwrap().(crypto.PubKeyEd25519),
315 Moniker: n.config.Moniker,
317 Version: version.Version,
319 cmn.Fmt("wire_version=%v", wire.Version),
320 cmn.Fmt("p2p_version=%v", p2p.Version),
324 if !n.sw.IsListening() {
328 p2pListener := n.sw.Listeners()[0]
329 p2pHost := p2pListener.ExternalAddress().IP.String()
330 p2pPort := p2pListener.ExternalAddress().Port
331 //rpcListenAddr := n.config.RPC.ListenAddress
333 // We assume that the rpcListener has the same ExternalAddress.
334 // This is probably true because both P2P and RPC listeners use UPnP,
335 // except of course if the rpc is only bound to localhost
336 nodeInfo.ListenAddr = cmn.Fmt("%v:%v", p2pHost, p2pPort)
337 //nodeInfo.Other = append(nodeInfo.Other, cmn.Fmt("rpc_addr=%v", rpcListenAddr))
341 //------------------------------------------------------------------------------
343 func (n *Node) NodeInfo() *p2p.NodeInfo {
344 return n.sw.NodeInfo()
347 func (n *Node) DialSeeds(seeds []string) error {
348 return n.sw.DialSeeds(n.addrBook, seeds)
352 func ProtocolAndAddress(listenAddr string) (string, string) {
353 p, address := "tcp", listenAddr
354 parts := strings.SplitN(address, "://", 2)
356 p, address = parts[0], parts[1]
361 //------------------------------------------------------------------------------