Skip to content

Commit

Permalink
dont panic
Browse files Browse the repository at this point in the history
  • Loading branch information
JekaMas committed May 19, 2022
1 parent d493806 commit 89fadee
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
41 changes: 28 additions & 13 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
// END: Bor changes

bcVersion := rawdb.ReadDatabaseVersion(chainDb)
var dbVer = "<nil>"
dbVer := "<nil>"
if bcVersion != nil {
dbVer = fmt.Sprintf("%d", *bcVersion)
}
Expand Down Expand Up @@ -598,43 +598,58 @@ 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
}
}
}

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
}

Expand Down
8 changes: 5 additions & 3 deletions eth/handler_bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package eth

import (
"context"
"fmt"
"errors"
"fmt"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -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")
Expand Down Expand Up @@ -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
}

0 comments on commit 89fadee

Please sign in to comment.