Skip to content

Commit

Permalink
get rid of dependence on block hash for op crosschain
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 committed May 6, 2024
1 parent 8b64f24 commit 6937979
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 52 deletions.
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
19 changes: 2 additions & 17 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

0 comments on commit 6937979

Please sign in to comment.