Skip to content

Commit

Permalink
delete votes if OPBNB roll back
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 committed Oct 25, 2023
1 parent f5b9b1c commit 347c4b5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ func (cfg *BSCConfig) Validate() {
if cfg.GasLimit == 0 {
panic("gas_limit of BNB Smart Chain should be larger than 0")
}
if cfg.NumberOfBlocksForFinality < 2 || cfg.NumberOfBlocksForFinality > 21 {
panic("NumberOfBlocksForFinality should be [2, 21]")
if cfg.NumberOfBlocksForFinality < 1 || cfg.NumberOfBlocksForFinality > 21 {
panic("NumberOfBlocksForFinality should be [1, 21]")
}
}

Expand Down
7 changes: 6 additions & 1 deletion db/dao/bsc_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dao

import (
"database/sql"
"github.com/cometbft/cometbft/votepool"
"time"

"gorm.io/gorm"
Expand Down Expand Up @@ -142,7 +143,7 @@ func (d *BSCDao) SaveBatchPackages(pkgs []*model.BscRelayPackage) error {
})
}

func (d *BSCDao) DeleteBlockAndPackagesAtHeight(height uint64) error {
func (d *BSCDao) DeleteBlockAndPackagesAndVotesAtHeight(height uint64) error {
return d.DB.Transaction(func(dbTx *gorm.DB) error {
err := dbTx.Where("height = ?", height).Delete(model.BscBlock{}).Error
if err != nil {
Expand All @@ -152,6 +153,10 @@ func (d *BSCDao) DeleteBlockAndPackagesAtHeight(height uint64) error {
if err != nil {
return err
}
err = dbTx.Where("height = ? and event_type = ?", height, votepool.FromOpCrossChainEvent).Delete(model.Vote{}).Error
if err != nil {
return err
}
return nil
})
}
Expand Down
4 changes: 2 additions & 2 deletions listener/bsc_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ func (l *BSCListener) queryCrossChainLogs(blockHash ethcommon.Hash) ([]types.Log

func (l *BSCListener) isForkedBlockAndDelete(latestPolledBlock *model.BscBlock, nextHeight uint64, parentHash ethcommon.Hash) (bool, error) {
if latestPolledBlock.Height != 0 && latestPolledBlock.Height+1 == nextHeight && parentHash.String() != latestPolledBlock.BlockHash {
// delete latestPolledBlock and its cross-chain packages from DB
if err := l.DaoManager.BSCDao.DeleteBlockAndPackagesAtHeight(latestPolledBlock.Height); err != nil {
// delete latestPolledBlock and its cross-chain packages and votes for these packages from DB.
if err := l.DaoManager.BSCDao.DeleteBlockAndPackagesAndVotesAtHeight(latestPolledBlock.Height); err != nil {
return true, err
}
logging.Logger.Infof("deleted block at height=%d from DB due to there is a fork", latestPolledBlock.Height)
Expand Down

0 comments on commit 347c4b5

Please sign in to comment.