[voting-reward] fix voting power table init
@@ -92,17 +92,8 @@ func GetConstructor(cfg *config.Config, hub *component.ComponentHub, cdb consens
}
}
-// New returns a new DPos object
-func New(cfg *config.Config, hub *component.ComponentHub, cdb consensus.ChainDB,
- sdb *state.ChainStateDB) (consensus.Consensus, error) {
-
- chain.DecorateBlockRewardFn(sendVotingReward)
-
- bpc, err := bp.NewCluster(cdb)
- if err != nil {
- return nil, err
- }
-
+func getStateDB(cfg *config.Config, cdb consensus.ChainDB, sdb *state.ChainStateDB) (*state.StateDB, error) {
+ if cfg.Blockchain.VerifyOnly {
vprInitBlockNo := func(blockNo types.BlockNo) types.BlockNo {
if blockNo == 0 {
return blockNo
@@ -114,13 +105,32 @@ func New(cfg *config.Config, hub *component.ComponentHub, cdb consensus.ChainDB,
if block, err := cdb.GetBlockByNo(vprInitBlockNo(cfg.Blockchain.VerifyBlock)); err != nil {
return nil, err
} else {
- err = InitVprWithRoot(sdb, block.GetHeader().GetBlocksRootHash())
+ return sdb.OpenNewStateDB(block.GetHeader().GetBlocksRootHash()), nil
}
+ }
+ return sdb.GetStateDB(), nil
+}
+
+// New returns a new DPos object
+func New(cfg *config.Config, hub *component.ComponentHub, cdb consensus.ChainDB,
+ sdb *state.ChainStateDB) (consensus.Consensus, error) {
+ chain.DecorateBlockRewardFn(sendVotingReward)
+
+ bpc, err := bp.NewCluster(cdb)
if err != nil {
return nil, err
}
+ var state *state.StateDB
+ if state, err = getStateDB(cfg, cdb, sdb); err != nil {
+ return nil, err
+ }
+
+ if err = InitVPR(state); err != nil {
+ return nil, err
+ }
+
Init(bpc.Size())
quitC := make(chan interface{})
@@ -204,10 +214,6 @@ func InitVPR(sdb *state.StateDB) error {
return system.InitVotingPowerRank(s)
}
-func InitVprWithRoot(cSdb *state.ChainStateDB, rootHash []byte) error {
- return InitVPR(cSdb.OpenNewStateDB(rootHash))
-}
-
// Init initilizes the DPoS parameters.
func Init(bpCount uint16) {
blockProducers = bpCount
