9 log "github.com/sirupsen/logrus"
10 cmn "github.com/tendermint/tmlibs/common"
11 dbm "github.com/tendermint/tmlibs/db"
12 browser "github.com/toqueteos/webbrowser"
14 "github.com/bytom/accesstoken"
15 "github.com/bytom/account"
16 "github.com/bytom/api"
17 "github.com/bytom/asset"
18 "github.com/bytom/blockchain/pseudohsm"
19 "github.com/bytom/blockchain/txfeed"
20 cfg "github.com/bytom/config"
21 "github.com/bytom/consensus"
22 "github.com/bytom/database/leveldb"
23 "github.com/bytom/env"
24 "github.com/bytom/mining/cpuminer"
25 "github.com/bytom/mining/miningpool"
26 "github.com/bytom/netsync"
27 "github.com/bytom/protocol"
28 "github.com/bytom/protocol/bc"
29 "github.com/bytom/types"
30 w "github.com/bytom/wallet"
34 webAddress = "http://127.0.0.1:9888"
35 expireReservationsPeriod = time.Second
36 maxNewBlockChSize = 1024
45 syncManager *netsync.SyncManager
47 evsw types.EventSwitch // pub/sub for services
48 //bcReactor *bc.BlockchainReactor
50 accessTokens *accesstoken.CredentialStore
53 txfeed *txfeed.Tracker
54 cpuMiner *cpuminer.CPUMiner
55 miningPool *miningpool.MiningPool
59 func NewNode(config *cfg.Config) *Node {
60 ctx := context.Background()
61 initActiveNetParams(config)
63 txDB := dbm.NewDB("txdb", config.DBBackend, config.DBDir())
64 store := leveldb.NewStore(txDB)
66 tokenDB := dbm.NewDB("accesstoken", config.DBBackend, config.DBDir())
67 accessTokens := accesstoken.NewStore(tokenDB)
70 eventSwitch := types.NewEventSwitch()
71 _, err := eventSwitch.Start()
73 cmn.Exit(cmn.Fmt("Failed to start switch: %v", err))
76 txPool := protocol.NewTxPool()
77 chain, err := protocol.NewChain(store, txPool)
79 cmn.Exit(cmn.Fmt("Failed to create chain structure: %v", err))
82 var accounts *account.Manager = nil
83 var assets *asset.Registry = nil
84 var wallet *w.Wallet = nil
85 var txFeed *txfeed.Tracker = nil
87 txFeedDB := dbm.NewDB("txfeeds", config.DBBackend, config.DBDir())
88 txFeed = txfeed.NewTracker(txFeedDB, chain)
90 if err = txFeed.Prepare(ctx); err != nil {
91 log.WithField("error", err).Error("start txfeed")
95 hsm, err := pseudohsm.New(config.KeysDir())
97 cmn.Exit(cmn.Fmt("initialize HSM failed: %v", err))
100 if !config.Wallet.Disable {
101 walletDB := dbm.NewDB("wallet", config.DBBackend, config.DBDir())
102 accounts = account.NewManager(walletDB, chain)
103 assets = asset.NewRegistry(walletDB, chain)
104 wallet, err = w.NewWallet(walletDB, accounts, assets, hsm, chain)
106 log.WithField("error", err).Error("init NewWallet")
109 // Clean up expired UTXO reservations periodically.
110 go accounts.ExpireReservations(ctx, expireReservationsPeriod)
112 newBlockCh := make(chan *bc.Hash, maxNewBlockChSize)
114 syncManager, _ := netsync.NewSyncManager(config, chain, txPool, newBlockCh)
116 // run the profile server
117 profileHost := config.ProfListenAddress
118 if profileHost != "" {
119 // Profiling bytomd programs.see (https://blog.golang.org/profiling-go-programs)
120 // go tool pprof http://profileHose/debug/pprof/heap
122 http.ListenAndServe(profileHost, nil)
128 syncManager: syncManager,
130 accessTokens: accessTokens,
134 miningEnable: config.Mining,
137 node.cpuMiner = cpuminer.NewCPUMiner(chain, accounts, txPool, newBlockCh)
138 node.miningPool = miningpool.NewMiningPool(chain, accounts, txPool, newBlockCh)
140 node.BaseService = *cmn.NewBaseService(nil, "Node", node)
145 func initActiveNetParams(config *cfg.Config) {
147 consensus.ActiveNetParams, exist = consensus.NetParams[config.ChainID]
149 cmn.Exit(cmn.Fmt("chain_id[%v] don't exist", config.ChainID))
153 // Lanch web broser or not
154 func lanchWebBroser() {
155 log.Info("Launching System Browser with :", webAddress)
156 if err := browser.Open(webAddress); err != nil {
157 log.Error(err.Error())
162 func (n *Node) initAndstartApiServer() {
163 n.api = api.NewAPI(n.syncManager, n.wallet, n.txfeed, n.cpuMiner, n.miningPool, n.chain, n.config, n.accessTokens)
165 listenAddr := env.String("LISTEN", n.config.ApiAddress)
167 n.api.StartServer(*listenAddr)
170 func (n *Node) OnStart() error {
174 n.syncManager.Start()
175 n.initAndstartApiServer()
176 if !n.config.Web.Closed {
183 func (n *Node) OnStop() {
184 n.BaseService.OnStop()
189 log.Info("Stopping Node")
190 // TODO: gracefully disconnect from peers.
193 func (n *Node) RunForever() {
194 // Sleep forever and then...
195 cmn.TrapSignal(func() {
200 func (n *Node) EventSwitch() types.EventSwitch {
204 func (n *Node) SyncManager() *netsync.SyncManager {
208 func (n *Node) MiningPool() *miningpool.MiningPool {