Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use API to retrieve finalized block for opBNB #109

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion executor/bsc_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 4 additions & 34 deletions listener/bsc_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -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())
Expand Down Expand Up @@ -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)
}
Expand Down
23 changes: 2 additions & 21 deletions vote/bsc_vote_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -391,7 +376,3 @@ func (p *BSCVoteProcessor) reBroadcastVote(localVote *model.Vote) error {
func (p *BSCVoteProcessor) getChainId() sdk.ChainID {
return sdk.ChainID(p.config.BSCConfig.ChainId)
}

func (p *BSCVoteProcessor) isOpCrossChain() bool {
return p.config.BSCConfig.IsOpCrossChain()
}
Loading