From 347c4b54eba229199c44b28e0898c3203125a87f Mon Sep 17 00:00:00 2001 From: Alexgao001 Date: Wed, 25 Oct 2023 11:19:21 +0800 Subject: [PATCH] delete votes if OPBNB roll back --- config/config.go | 4 ++-- db/dao/bsc_dao.go | 7 ++++++- listener/bsc_listener.go | 4 ++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 9dfa391..514fb3e 100644 --- a/config/config.go +++ b/config/config.go @@ -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]") } } diff --git a/db/dao/bsc_dao.go b/db/dao/bsc_dao.go index f93b47e..552a3c1 100644 --- a/db/dao/bsc_dao.go +++ b/db/dao/bsc_dao.go @@ -2,6 +2,7 @@ package dao import ( "database/sql" + "github.com/cometbft/cometbft/votepool" "time" "gorm.io/gorm" @@ -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 { @@ -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 }) } diff --git a/listener/bsc_listener.go b/listener/bsc_listener.go index c07ffde..0001f99 100644 --- a/listener/bsc_listener.go +++ b/listener/bsc_listener.go @@ -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)