OSDN Git Service

init too;
[bytom/vapor.git] / node / node.go
index 8469de7..7e75267 100644 (file)
@@ -6,7 +6,6 @@ import (
        "net"
        "net/http"
        _ "net/http/pprof"
-       "os"
        "path/filepath"
        "reflect"
 
@@ -18,6 +17,7 @@ import (
        "github.com/vapor/accesstoken"
        "github.com/vapor/account"
        "github.com/vapor/api"
+       "github.com/vapor/application/mov"
        "github.com/vapor/asset"
        "github.com/vapor/blockchain/pseudohsm"
        cfg "github.com/vapor/config"
@@ -26,6 +26,7 @@ import (
        dbm "github.com/vapor/database/leveldb"
        "github.com/vapor/env"
        "github.com/vapor/event"
+       vaporLog "github.com/vapor/log"
        "github.com/vapor/net/websocket"
        "github.com/vapor/netsync"
        "github.com/vapor/proposal/blockproposer"
@@ -66,16 +67,22 @@ func NewNode(config *cfg.Config) *Node {
                cmn.Exit(cmn.Fmt("Failed to load federated information:[%s]", err.Error()))
        }
 
+       if err := vaporLog.InitLogFile(config); err != nil {
+               log.WithField("err", err).Fatalln("InitLogFile failed")
+       }
+
        log.WithFields(log.Fields{
                "module":             logModule,
                "pubkey":             config.PrivateKey().XPub(),
                "fed_xpubs":          config.Federation.Xpubs,
                "fed_quorum":         config.Federation.Quorum,
-               "fed_controlprogram": hex.EncodeToString(cfg.FederationProgrom(config)),
+               "fed_controlprogram": hex.EncodeToString(cfg.FederationWScript(config)),
        }).Info()
 
-       initLogFile(config)
-       initActiveNetParams(config)
+       if err := consensus.InitActiveNetParams(config.ChainID); err != nil {
+               log.Fatalf("Failed to init ActiveNetParams:[%s]", err.Error())
+       }
+
        initCommonConfig(config)
 
        // Get store
@@ -89,8 +96,9 @@ func NewNode(config *cfg.Config) *Node {
        accessTokens := accesstoken.NewStore(tokenDB)
 
        dispatcher := event.NewDispatcher()
-       txPool := protocol.NewTxPool(store, dispatcher)
-       chain, err := protocol.NewChain(store, txPool, dispatcher)
+       movCore := mov.NewMovCore(config.DBBackend, config.DBDir(), consensus.ActiveNetParams.MovStartHeight)
+       txPool := protocol.NewTxPool(store, []protocol.DustFilterer{movCore}, dispatcher)
+       chain, err := protocol.NewChain(store, txPool, []protocol.Protocoler{movCore}, dispatcher)
        if err != nil {
                cmn.Exit(cmn.Fmt("Failed to create chain structure: %v", err))
        }
@@ -110,9 +118,11 @@ func NewNode(config *cfg.Config) *Node {
 
        if !config.Wallet.Disable {
                walletDB := dbm.NewDB("wallet", config.DBBackend, config.DBDir())
-               accounts = account.NewManager(walletDB, chain)
+               walletStore := database.NewWalletStore(walletDB)
+               accountStore := database.NewAccountStore(walletDB)
+               accounts = account.NewManager(accountStore, chain)
                assets = asset.NewRegistry(walletDB, chain)
-               wallet, err = w.NewWallet(walletDB, accounts, assets, hsm, chain, dispatcher, config.Wallet.TxIndex)
+               wallet, err = w.NewWallet(walletStore, accounts, assets, hsm, chain, dispatcher, config.Wallet.TxIndex)
                if err != nil {
                        log.WithFields(log.Fields{"module": logModule, "error": err}).Error("init NewWallet")
                }
@@ -122,8 +132,8 @@ func NewNode(config *cfg.Config) *Node {
                        wallet.RescanBlocks()
                }
        }
-
-       syncManager, err := netsync.NewSyncManager(config, chain, txPool, dispatcher)
+       fastSyncDB := dbm.NewDB("fastsync", config.DBBackend, config.DBDir())
+       syncManager, err := netsync.NewSyncManager(config, chain, txPool, dispatcher, fastSyncDB)
        if err != nil {
                cmn.Exit(cmn.Fmt("Failed to create sync manager: %v", err))
        }
@@ -133,7 +143,7 @@ func NewNode(config *cfg.Config) *Node {
        // run the profile server
        profileHost := config.ProfListenAddress
        if profileHost != "" {
-               // Profiling bytomd programs.see (https://blog.golang.org/profiling-go-programs)
+               // Profiling vapord programs.see (https://blog.golang.org/profiling-go-programs)
                // go tool pprof http://profileHose/debug/pprof/heap
                go func() {
                        if err = http.ListenAndServe(profileHost, nil); err != nil {
@@ -161,7 +171,7 @@ func NewNode(config *cfg.Config) *Node {
 
 // find whether config xpubs equal genesis block xpubs
 func checkConfig(chain *protocol.Chain, config *cfg.Config) error {
-       fedpegScript := cfg.FederationProgrom(config)
+       fedpegScript := cfg.FederationWScript(config)
        genesisBlock, err := chain.GetBlockByHeight(0)
        if err != nil {
                return err
@@ -184,28 +194,6 @@ func lockDataDirectory(config *cfg.Config) error {
        return nil
 }
 
-func initActiveNetParams(config *cfg.Config) {
-       var exist bool
-       consensus.ActiveNetParams, exist = consensus.NetParams[config.ChainID]
-       if !exist {
-               cmn.Exit(cmn.Fmt("chain_id[%v] don't exist", config.ChainID))
-       }
-}
-
-func initLogFile(config *cfg.Config) {
-       if config.LogFile == "" {
-               return
-       }
-       cmn.EnsureDir(filepath.Dir(config.LogFile), 0700)
-       file, err := os.OpenFile(config.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
-       if err == nil {
-               log.SetOutput(file)
-       } else {
-               log.WithFields(log.Fields{"module": logModule, "err": err}).Info("using default")
-       }
-
-}
-
 func initCommonConfig(config *cfg.Config) {
        cfg.CommonConfig = config
 }