OSDN Git Service

Add png dir
[bytom/vapor.git] / node / node.go
index 8021a66..c30ce92 100644 (file)
@@ -27,8 +27,6 @@ import (
        "github.com/vapor/common"
        cfg "github.com/vapor/config"
        "github.com/vapor/consensus"
-       engine "github.com/vapor/consensus/consensus"
-       "github.com/vapor/consensus/consensus/dpos"
        "github.com/vapor/crypto/ed25519/chainkd"
        "github.com/vapor/database/leveldb"
        "github.com/vapor/env"
@@ -46,8 +44,6 @@ const (
        maxNewBlockChSize = 1024
 )
 
-var consensusEngine engine.Engine
-
 type Node struct {
        cmn.BaseService
 
@@ -99,6 +95,11 @@ func NewNode(config *cfg.Config) *Node {
                cmn.Exit(cmn.Fmt("Failed to create chain structure: %v", err))
        }
 
+       switch config.Consensus.Type {
+       case "dpos":
+               initDpos(chain, config)
+       }
+
        var accounts *account.Manager = nil
        var assets *asset.Registry = nil
        var wallet *w.Wallet = nil
@@ -118,7 +119,7 @@ func NewNode(config *cfg.Config) *Node {
        }
 
        if !config.Wallet.Disable {
-               address, err := common.DecodeAddress(config.Consensus.Dpos.Coinbase, &consensus.ActiveNetParams)
+               address, err := common.DecodeAddress(config.Consensus.Coinbase, &consensus.ActiveNetParams)
                if err != nil {
                        cmn.Exit(cmn.Fmt("DecodeAddress: %v", err))
                }
@@ -155,7 +156,6 @@ func NewNode(config *cfg.Config) *Node {
                        }
                }()
        }
-
        node := &Node{
                config:       config,
                syncManager:  syncManager,
@@ -169,10 +169,7 @@ func NewNode(config *cfg.Config) *Node {
                notificationMgr: notificationMgr,
        }
 
-       //node.cpuMiner = cpuminer.NewCPUMiner(chain, accounts, txPool, newBlockCh)
-       consensusEngine = createConsensusEngine(config, store)
-       node.miner = miner.NewMiner(chain, accounts, txPool, newBlockCh, consensusEngine)
-
+       node.miner = miner.NewMiner(chain, accounts, txPool, newBlockCh)
        node.BaseService = *cmn.NewBaseService(nil, "Node", node)
 
        return node
@@ -297,6 +294,10 @@ func (n *Node) OnStart() error {
 }
 
 func (n *Node) OnStop() {
+       if err := n.chain.Engine.Finish(); err != nil {
+               log.Errorf("OnStop: %v", err)
+       }
+
        n.notificationMgr.Shutdown()
        n.notificationMgr.WaitForShutdown()
        n.BaseService.OnStop()
@@ -362,24 +363,35 @@ func initConsensusConfig(config *cfg.Config) {
                        cmn.Exit(cmn.Fmt("invalid consensus file: %v", err))
                }
 
-               for _, v := range config.Consensus.Dpos.SelfVoteSigners {
+               for _, v := range config.Consensus.SelfVoteSigners {
                        address, err := common.DecodeAddress(v, &consensus.ActiveNetParams)
                        if err != nil {
                                cmn.Exit(cmn.Fmt("Address resolution failed: %v", err))
                        }
-                       config.Consensus.Dpos.Signers = append(config.Consensus.Dpos.Signers, address)
+                       config.Consensus.Signers = append(config.Consensus.Signers, address)
                }
        }
 }
 
-func createConsensusEngine(config *cfg.Config, store protocol.Store) engine.Engine {
-       if config.Consensus.Dpos != nil {
-               return dpos.New(config.Consensus.Dpos, store)
-       } else {
-               return nil
-       }
-}
+func initDpos(chain *protocol.Chain, config *cfg.Config) {
+       header := chain.BestBlockHeader()
+       height := header.Height
+       hash := header.Hash()
+       maxSignerCount := config.Consensus.MaxSignerCount
+       period := config.Consensus.Period
+       err := chain.Engine.Init(chain, maxSignerCount, period, height, hash)
+
+       if height > 0 {
+               oldBlockHeight := chain.Engine.GetOldBlockHeight()
+               oldBlockHash := chain.Engine.GetOldBlockHash()
+               if err != nil {
+                       oldBlockHeight = 0
+                       header, _ = chain.GetHeaderByHeight(oldBlockHeight)
+                       oldBlockHash = header.Hash()
+               }
 
-func GetConsensusEngine() engine.Engine {
-       return consensusEngine
+               if err := chain.RepairDPoSData(oldBlockHeight, oldBlockHash); err != nil {
+                       cmn.Exit(cmn.Fmt("initVote failed: %v", err))
+               }
+       }
 }