"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"
maxNewBlockChSize = 1024
)
-var consensusEngine engine.Engine
-
type Node struct {
cmn.BaseService
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
}
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))
}
}
}()
}
-
node := &Node{
config: config,
syncManager: syncManager,
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
}
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()
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))
+ }
+ }
}