dont panic

This commit is contained in:
Evgeny Danienko 2022-05-19 14:38:49 +03:00
parent d4938065c1
commit 89fadee71e
2 changed files with 34 additions and 17 deletions

View file

@ -184,7 +184,7 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
// END: Bor changes // END: Bor changes
bcVersion := rawdb.ReadDatabaseVersion(chainDb) bcVersion := rawdb.ReadDatabaseVersion(chainDb)
var dbVer = "<nil>" dbVer := "<nil>"
if bcVersion != nil { if bcVersion != nil {
dbVer = fmt.Sprintf("%d", *bcVersion) dbVer = fmt.Sprintf("%d", *bcVersion)
} }
@ -598,22 +598,25 @@ func (s *Ethereum) Start() error {
// StartCheckpointWhitelistService starts the goroutine to fetch checkpoints and update the // StartCheckpointWhitelistService starts the goroutine to fetch checkpoints and update the
// checkpoint whitelist map. // checkpoint whitelist map.
func (s *Ethereum) startCheckpointWhitelistService() { func (s *Ethereum) startCheckpointWhitelistService() {
every := time.Duration(100) * time.Second
ticker := time.NewTicker(every)
defer ticker.Stop()
// first run the checkpoint whitelist // first run the checkpoint whitelist
err := s.handleWhitelistCheckpoint() err := s.handleWhitelistCheckpoint()
if err != nil { if err != nil {
if errors.Is(err, ErrBorConsensusWithoutHeimdall) || errors.Is(err, ErrNotBorConsensus) {
return
}
log.Warn("the first run", "err", err) log.Warn("the first run", "err", err)
} }
ticker := time.NewTicker(100 * time.Second)
defer ticker.Stop()
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
err := s.handleWhitelistCheckpoint() err := s.handleWhitelistCheckpoint()
if err != nil { if err != nil {
log.Warn(err.Error()) log.Warn("couldn't get whitelist checkpoint", "err", err)
} }
case <-s.closeCh: case <-s.closeCh:
return 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. // handleWhitelistCheckpoint handles the checkpoint whitelist mechanism.
func (s *Ethereum) handleWhitelistCheckpoint() error { func (s *Ethereum) handleWhitelistCheckpoint() error {
ethHandler := (*ethHandler)(s.handler) ethHandler := (*ethHandler)(s.handler)
if !ethHandler.chain.Engine().(*bor.Bor).WithoutHeimdall { bor, ok := ethHandler.chain.Engine().(*bor.Bor)
endBlockNum, endBlockHash, err := ethHandler.fetchWhitelistCheckpoint() if !ok {
if err != nil { return ErrNotBorConsensus
return err
}
// 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 return nil
} }

View file

@ -2,8 +2,8 @@ package eth
import ( import (
"context" "context"
"fmt"
"errors" "errors"
"fmt"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/common/hexutil"
@ -14,9 +14,9 @@ import (
// fetchWhitelistCheckpoint fetched the latest checkpoint from it's local heimdall // fetchWhitelistCheckpoint fetched the latest checkpoint from it's local heimdall
// and verifies the data against bor data. // 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 // check for checkpoint whitelisting: bor
checkpoint, err := h.chain.Engine().(*bor.Bor).HeimdallClient.FetchLatestCheckpoint() checkpoint, err := bor.HeimdallClient.FetchLatestCheckpoint()
if err != nil { if err != nil {
log.Debug("Failed to fetch latest checkpoint for whitelisting") log.Debug("Failed to fetch latest checkpoint for whitelisting")
return 0, common.Hash{}, errors.New("failed to fetch latest checkpoint") 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") log.Debug("Failed to get end block hash of checkpoint while whitelisting")
return 0, common.Hash{}, errors.New("failed to get end block") return 0, common.Hash{}, errors.New("failed to get end block")
} }
hash := fmt.Sprintf("%v", block["hash"]) hash := fmt.Sprintf("%v", block["hash"])
return checkpoint.EndBlock.Uint64(), common.HexToHash(hash), nil return checkpoint.EndBlock.Uint64(), common.HexToHash(hash), nil
} }