OSDN Git Service

rename (#465)
[bytom/vapor.git] / toolbar / vote_reward / synchron / block_keeper.go
index 9903480..e067096 100644 (file)
@@ -6,12 +6,12 @@ import (
        "github.com/jinzhu/gorm"
        log "github.com/sirupsen/logrus"
 
-       "github.com/vapor/errors"
-       "github.com/vapor/protocol/bc/types"
-       "github.com/vapor/toolbar/apinode"
-       "github.com/vapor/toolbar/common"
-       "github.com/vapor/toolbar/vote_reward/config"
-       "github.com/vapor/toolbar/vote_reward/database/orm"
+       "github.com/bytom/vapor/errors"
+       "github.com/bytom/vapor/protocol/bc/types"
+       "github.com/bytom/vapor/toolbar/apinode"
+       "github.com/bytom/vapor/toolbar/common"
+       "github.com/bytom/vapor/toolbar/vote_reward/config"
+       "github.com/bytom/vapor/toolbar/vote_reward/database/orm"
 )
 
 var ErrInconsistentDB = errors.New("inconsistent db status")
@@ -84,7 +84,12 @@ func (c *ChainKeeper) syncChainStatus(db *gorm.DB, chainStatus *orm.ChainStatus)
                return err
        }
 
-       return c.DetachBlock(db, chainStatus, currentBlock)
+       preBlock, err := c.node.GetBlockByHash(currentBlock.PreviousBlockHash.String())
+       if err != nil {
+               return err
+       }
+
+       return c.DetachBlock(db, chainStatus, preBlock)
 }
 
 func (c *ChainKeeper) AttachBlock(db *gorm.DB, chainStatus *orm.ChainStatus, block *types.Block) error {
@@ -131,11 +136,11 @@ func (c *ChainKeeper) AttachBlock(db *gorm.DB, chainStatus *orm.ChainStatus, blo
 }
 
 func (c *ChainKeeper) DetachBlock(db *gorm.DB, chainStatus *orm.ChainStatus, block *types.Block) error {
-       if err := db.Where(&orm.Utxo{VoteHeight: block.Height}).Delete(&orm.Utxo{}).Error; err != nil {
+       if err := db.Where(&orm.Utxo{VoteHeight: chainStatus.BlockHeight}).Delete(&orm.Utxo{}).Error; err != nil {
                return err
        }
 
-       if err := db.Where(&orm.Utxo{VetoHeight: block.Height}).Update("veto_height", 0).Error; err != nil {
+       if err := db.Model(&orm.Utxo{}).Where(&orm.Utxo{VetoHeight: chainStatus.BlockHeight}).Update("veto_height", 0).Error; err != nil {
                return err
        }