diff --git a/eth/backend.go b/eth/backend.go index eb4223bf6a..8f3930f605 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -184,7 +184,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { // END: Bor changes bcVersion := rawdb.ReadDatabaseVersion(chainDb) - var dbVer = "" + dbVer := "" if bcVersion != nil { dbVer = fmt.Sprintf("%d", *bcVersion) } @@ -598,22 +598,25 @@ func (s *Ethereum) Start() error { // StartCheckpointWhitelistService starts the goroutine to fetch checkpoints and update the // checkpoint whitelist map. func (s *Ethereum) startCheckpointWhitelistService() { - every := time.Duration(100) * time.Second - ticker := time.NewTicker(every) - defer ticker.Stop() - // first run the checkpoint whitelist err := s.handleWhitelistCheckpoint() if err != nil { + if errors.Is(err, ErrBorConsensusWithoutHeimdall) || errors.Is(err, ErrNotBorConsensus) { + return + } + log.Warn("the first run", "err", err) } + ticker := time.NewTicker(100 * time.Second) + defer ticker.Stop() + for { select { case <-ticker.C: err := s.handleWhitelistCheckpoint() if err != nil { - log.Warn(err.Error()) + log.Warn("couldn't get whitelist checkpoint", "err", err) } case <-s.closeCh: return @@ -621,20 +624,32 @@ func (s *Ethereum) startCheckpointWhitelistService() { } } +var ( + ErrNotBorConsensus = errors.New("not Bor consensus was given") + ErrBorConsensusWithoutHeimdall = errors.New("Bor consensus without Heimdall") +) + // handleWhitelistCheckpoint handles the checkpoint whitelist mechanism. func (s *Ethereum) handleWhitelistCheckpoint() error { ethHandler := (*ethHandler)(s.handler) - if !ethHandler.chain.Engine().(*bor.Bor).WithoutHeimdall { - endBlockNum, endBlockHash, err := ethHandler.fetchWhitelistCheckpoint() - if err != nil { - return err - } + bor, ok := ethHandler.chain.Engine().(*bor.Bor) + if !ok { + return ErrNotBorConsensus + } - // Update the checkpoint whitelist map. - ethHandler.downloader.ProcessCheckpoint(endBlockNum, endBlockHash) + if bor.WithoutHeimdall { + return ErrBorConsensusWithoutHeimdall } + endBlockNum, endBlockHash, err := ethHandler.fetchWhitelistCheckpoint(bor) + if err != nil { + return err + } + + // Update the checkpoint whitelist map. + ethHandler.downloader.ProcessCheckpoint(endBlockNum, endBlockHash) + return nil } diff --git a/eth/handler_bor.go b/eth/handler_bor.go index 02f9918f50..ad1ef16288 100644 --- a/eth/handler_bor.go +++ b/eth/handler_bor.go @@ -2,8 +2,8 @@ package eth import ( "context" - "fmt" "errors" + "fmt" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" @@ -14,9 +14,9 @@ import ( // fetchWhitelistCheckpoint fetched the latest checkpoint from it's local heimdall // and verifies the data against bor data. -func (h *ethHandler) fetchWhitelistCheckpoint() (uint64, common.Hash, error) { +func (h *ethHandler) fetchWhitelistCheckpoint(bor *bor.Bor) (uint64, common.Hash, error) { // check for checkpoint whitelisting: bor - checkpoint, err := h.chain.Engine().(*bor.Bor).HeimdallClient.FetchLatestCheckpoint() + checkpoint, err := bor.HeimdallClient.FetchLatestCheckpoint() if err != nil { log.Debug("Failed to fetch latest checkpoint for whitelisting") return 0, common.Hash{}, errors.New("failed to fetch latest checkpoint") @@ -47,6 +47,8 @@ func (h *ethHandler) fetchWhitelistCheckpoint() (uint64, common.Hash, error) { log.Debug("Failed to get end block hash of checkpoint while whitelisting") return 0, common.Hash{}, errors.New("failed to get end block") } + hash := fmt.Sprintf("%v", block["hash"]) + return checkpoint.EndBlock.Uint64(), common.HexToHash(hash), nil }