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/crypto/ed25519/chainkd"
23 "github.com/bytom/database/leveldb"
24 "github.com/bytom/env"
25 "github.com/bytom/mining/cpuminer"
26 "github.com/bytom/mining/miningpool"
27 "github.com/bytom/netsync"
28 "github.com/bytom/protocol"
29 "github.com/bytom/protocol/bc"
30 "github.com/bytom/types"
31 w "github.com/bytom/wallet"
35 webAddress = "http://127.0.0.1:9888"
36 expireReservationsPeriod = time.Second
37 maxNewBlockChSize = 1024
46 syncManager *netsync.SyncManager
48 evsw types.EventSwitch // pub/sub for services
49 //bcReactor *bc.BlockchainReactor
51 accessTokens *accesstoken.CredentialStore
54 txfeed *txfeed.Tracker
55 cpuMiner *cpuminer.CPUMiner
56 miningPool *miningpool.MiningPool
60 func NewNode(config *cfg.Config) *Node {
61 ctx := context.Background()
62 initActiveNetParams(config)
64 txDB := dbm.NewDB("txdb", config.DBBackend, config.DBDir())
65 store := leveldb.NewStore(txDB)
67 tokenDB := dbm.NewDB("accesstoken", config.DBBackend, config.DBDir())
68 accessTokens := accesstoken.NewStore(tokenDB)
71 eventSwitch := types.NewEventSwitch()
72 _, err := eventSwitch.Start()
74 cmn.Exit(cmn.Fmt("Failed to start switch: %v", err))
77 txPool := protocol.NewTxPool()
78 chain, err := protocol.NewChain(store, txPool)
80 cmn.Exit(cmn.Fmt("Failed to create chain structure: %v", err))
83 var accounts *account.Manager = nil
84 var assets *asset.Registry = nil
85 var wallet *w.Wallet = nil
86 var txFeed *txfeed.Tracker = nil
88 txFeedDB := dbm.NewDB("txfeeds", config.DBBackend, config.DBDir())
89 txFeed = txfeed.NewTracker(txFeedDB, chain)
91 if err = txFeed.Prepare(ctx); err != nil {
92 log.WithField("error", err).Error("start txfeed")
96 hsm, err := pseudohsm.New(config.KeysDir())
98 cmn.Exit(cmn.Fmt("initialize HSM failed: %v", err))
101 if !config.Wallet.Disable {
102 walletDB := dbm.NewDB("wallet", config.DBBackend, config.DBDir())
103 accounts = account.NewManager(walletDB, chain)
104 assets = asset.NewRegistry(walletDB, chain)
105 wallet, err = w.NewWallet(walletDB, accounts, assets, hsm, chain)
107 log.WithField("error", err).Error("init NewWallet")
110 if err := initOrRecoverAccount(hsm, wallet); err != nil {
111 log.WithField("error", err).Error("initialize or recover account")
114 // Clean up expired UTXO reservations periodically.
115 go accounts.ExpireReservations(ctx, expireReservationsPeriod)
117 newBlockCh := make(chan *bc.Hash, maxNewBlockChSize)
119 syncManager, _ := netsync.NewSyncManager(config, chain, txPool, newBlockCh)
121 // run the profile server
122 profileHost := config.ProfListenAddress
123 if profileHost != "" {
124 // Profiling bytomd programs.see (https://blog.golang.org/profiling-go-programs)
125 // go tool pprof http://profileHose/debug/pprof/heap
127 http.ListenAndServe(profileHost, nil)
133 syncManager: syncManager,
135 accessTokens: accessTokens,
139 miningEnable: config.Mining,
142 node.cpuMiner = cpuminer.NewCPUMiner(chain, accounts, txPool, newBlockCh)
143 node.miningPool = miningpool.NewMiningPool(chain, accounts, txPool, newBlockCh)
145 node.BaseService = *cmn.NewBaseService(nil, "Node", node)
150 func initActiveNetParams(config *cfg.Config) {
152 consensus.ActiveNetParams, exist = consensus.NetParams[config.ChainID]
154 cmn.Exit(cmn.Fmt("chain_id[%v] don't exist", config.ChainID))
158 func initOrRecoverAccount(hsm *pseudohsm.HSM, wallet *w.Wallet) error {
159 xpubs := hsm.ListKeys()
162 xpub, err := hsm.XCreate("default", "123456")
167 wallet.AccountMgr.Create(nil, []chainkd.XPub{xpub.XPub}, 1, "default")
171 if _, err := wallet.AccountMgr.ListAccounts(""); err != nil {
178 // Lanch web broser or not
179 func lanchWebBroser() {
180 log.Info("Launching System Browser with :", webAddress)
181 if err := browser.Open(webAddress); err != nil {
182 log.Error(err.Error())
187 func (n *Node) initAndstartApiServer() {
188 n.api = api.NewAPI(n.syncManager, n.wallet, n.txfeed, n.cpuMiner, n.miningPool, n.chain, n.config, n.accessTokens)
190 listenAddr := env.String("LISTEN", n.config.ApiAddress)
192 n.api.StartServer(*listenAddr)
195 func (n *Node) OnStart() error {
199 n.syncManager.Start()
200 n.initAndstartApiServer()
201 if !n.config.Web.Closed {
208 func (n *Node) OnStop() {
209 n.BaseService.OnStop()
214 log.Info("Stopping Node")
215 // TODO: gracefully disconnect from peers.
218 func (n *Node) RunForever() {
219 // Sleep forever and then...
220 cmn.TrapSignal(func() {
225 func (n *Node) EventSwitch() types.EventSwitch {
229 func (n *Node) SyncManager() *netsync.SyncManager {
233 func (n *Node) MiningPool() *miningpool.MiningPool {