From 693797939635e92bb77c98843150e4a1361af6c1 Mon Sep 17 00:00:00 2001 From: Alexgao001 Date: Mon, 6 May 2024 10:57:01 +0800 Subject: [PATCH] get rid of dependence on block hash for op crosschain --- executor/bsc_executor.go | 10 +++++++++- listener/bsc_listener.go | 38 ++++---------------------------------- vote/bsc_vote_processor.go | 19 ++----------------- 3 files changed, 15 insertions(+), 52 deletions(-) diff --git a/executor/bsc_executor.go b/executor/bsc_executor.go index 12e3ed4..6d69954 100644 --- a/executor/bsc_executor.go +++ b/executor/bsc_executor.go @@ -6,11 +6,12 @@ import ( "encoding/hex" "encoding/json" "fmt" - sdk "github.com/cosmos/cosmos-sdk/types" "math/big" "sync" "time" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/avast/retry-go/v4" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/accounts/abi/bind" @@ -212,6 +213,13 @@ func (e *BSCExecutor) getLatestBlockHeight(client *ethclient.Client, rpcClient * ctxWithTimeout, cancel := context.WithTimeout(context.Background(), RPCTimeout) defer cancel() if finalized { + if e.config.BSCConfig.IsOpCrossChain() { + header, err := client.HeaderByNumber(ctxWithTimeout, big.NewInt(int64(rpc.FinalizedBlockNumber))) + if err != nil { + return 0, err + } + return header.Number.Uint64(), nil + } return e.getFinalizedBlockHeight(ctxWithTimeout, rpcClient) } header, err := client.HeaderByNumber(ctxWithTimeout, nil) diff --git a/listener/bsc_listener.go b/listener/bsc_listener.go index abdc50d..02a735f 100644 --- a/listener/bsc_listener.go +++ b/listener/bsc_listener.go @@ -70,23 +70,17 @@ func (l *BSCListener) poll() error { if nextHeight <= latestPolledBlockHeight { nextHeight = latestPolledBlockHeight + 1 } - var latestBlockHeight uint64 - if l.isOpCrossChain() { - // currently Get finalized block is not support by OPBNB yet - latestBlockHeight, err = l.bscExecutor.GetLatestBlockHeightWithRetry() - } else { - latestBlockHeight, err = l.bscExecutor.GetLatestFinalizedBlockHeightWithRetry() - } + latestBlockHeight, err := l.bscExecutor.GetLatestFinalizedBlockHeightWithRetry() if err != nil { logging.Logger.Errorf("failed to get latest finalized blockHeight, error: %s", err.Error()) return err } - if int64(latestPolledBlockHeight) >= int64(latestBlockHeight)-1 { + if nextHeight > latestBlockHeight { time.Sleep(common.ListenerPauseTime) return nil } } - if err = l.monitorCrossChainPkgAt(nextHeight, latestPolledBlock); err != nil { + if err = l.monitorCrossChainPkgAt(nextHeight); err != nil { return err } return nil @@ -96,7 +90,7 @@ func (l *BSCListener) getLatestPolledBlock() (*model.BscBlock, error) { return l.DaoManager.BSCDao.GetLatestBlock() } -func (l *BSCListener) monitorCrossChainPkgAt(nextHeight uint64, latestPolledBlock *model.BscBlock) error { +func (l *BSCListener) monitorCrossChainPkgAt(nextHeight uint64) error { nextHeightBlockHeader, err := l.bscExecutor.GetBlockHeaderAtHeight(nextHeight) if err != nil { return fmt.Errorf("failed to get latest block header, error: %s", err.Error()) @@ -106,18 +100,6 @@ func (l *BSCListener) monitorCrossChainPkgAt(nextHeight uint64, latestPolledBloc return nil } logging.Logger.Infof("retrieved BSC block header at height=%d", nextHeight) - - if l.config.BSCConfig.IsOpCrossChain() { - // check if the latest polled block in DB is forked, if so, delete it. - isForked, err := l.isForkedBlockAndDelete(latestPolledBlock, nextHeight, nextHeightBlockHeader.ParentHash) - if err != nil { - return err - } - if isForked { - return fmt.Errorf("there is fork at block height=%d", latestPolledBlock.Height) - } - } - logs, err := l.queryCrossChainLogs(nextHeight) if err != nil { return fmt.Errorf("failed to get logs from block at height=%d, err=%s", nextHeight, err.Error()) @@ -168,18 +150,6 @@ func (l *BSCListener) queryCrossChainLogs(height uint64) ([]types.Log, error) { return logs, nil } -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 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) - return true, nil - } - return false, nil -} - func (l *BSCListener) getCrossChainPackageEventHash() ethcommon.Hash { return ethcommon.HexToHash(CrossChainPackageEventHex) } diff --git a/vote/bsc_vote_processor.go b/vote/bsc_vote_processor.go index 58d673e..0f020f9 100644 --- a/vote/bsc_vote_processor.go +++ b/vote/bsc_vote_processor.go @@ -9,13 +9,14 @@ import ( "sync" "time" + "gorm.io/gorm" + "github.com/avast/retry-go/v4" tmtypes "github.com/cometbft/cometbft/types" "github.com/cometbft/cometbft/votepool" sdk "github.com/cosmos/cosmos-sdk/types" oracletypes "github.com/cosmos/cosmos-sdk/x/oracle/types" "github.com/ethereum/go-ethereum/rlp" - "gorm.io/gorm" "github.com/bnb-chain/greenfield-relayer/common" "github.com/bnb-chain/greenfield-relayer/config" @@ -64,27 +65,11 @@ func (p *BSCVoteProcessor) SignAndBroadcastVoteLoop() { // SignAndBroadcastVoteLoop signs using the bls private key, and broadcast the vote to votepool func (p *BSCVoteProcessor) signAndBroadcast() error { - var ( - latestHeight uint64 - err error - ) - if p.isOpCrossChain() { - latestHeight, err = p.bscExecutor.GetLatestBlockHeightWithRetry() - if err != nil { - logging.Logger.Errorf("failed to get latest block height, error: %s", err.Error()) - return err - } - } // need to keep track of the height so that make sure that we aggregate packages are from only 1 block. leastSavedPkgHeight, err := p.daoManager.BSCDao.GetLeastSavedPackagesHeight() if err != nil { return fmt.Errorf("failed to get least saved packages' height, error: %s", err.Error()) } - if p.isOpCrossChain() { - if leastSavedPkgHeight+p.config.BSCConfig.NumberOfBlocksForFinality > latestHeight { - return nil - } - } pkgs, err := p.daoManager.BSCDao.GetPackagesByHeightAndStatus(db.Saved, leastSavedPkgHeight) if err != nil {