minor fixes

This commit is contained in:
Evgeny Danienko 2022-05-19 10:23:34 +03:00
parent b1b0c0e2fb
commit fb46dd8cdf
2 changed files with 16 additions and 7 deletions

View file

@ -586,10 +586,12 @@ func (s *Ethereum) Start() error {
} }
maxPeers -= s.config.LightPeers maxPeers -= s.config.LightPeers
} }
// Start the networking layer and the light server if requested // Start the networking layer and the light server if requested
s.handler.Start(maxPeers) s.handler.Start(maxPeers)
go s.startCheckpointWhitelistService() go s.startCheckpointWhitelistService()
return nil return nil
} }
@ -600,7 +602,12 @@ func (s *Ethereum) startCheckpointWhitelistService() {
ticker := time.NewTicker(every) ticker := time.NewTicker(every)
defer ticker.Stop() defer ticker.Stop()
Loop: // first run the checkpoint whitelist
err := s.handleWhitelistCheckpoint()
if err != nil {
log.Warn("the first run", "err", err)
}
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
@ -609,7 +616,7 @@ Loop:
log.Warn(err.Error()) log.Warn(err.Error())
} }
case <-s.closeCh: case <-s.closeCh:
break Loop return
} }
} }
} }

View file

@ -3,6 +3,7 @@ package eth
import ( import (
"context" "context"
"fmt" "fmt"
"errors"
"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"
@ -18,32 +19,33 @@ func (h *ethHandler) fetchWhitelistCheckpoint() (uint64, common.Hash, error) {
checkpoint, err := h.chain.Engine().(*bor.Bor).HeimdallClient.FetchLatestCheckpoint() checkpoint, err := h.chain.Engine().(*bor.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{}, fmt.Errorf("failed to fetch latest checkpoint") return 0, common.Hash{}, errors.New("failed to fetch latest checkpoint")
} }
// check if we have the checkpoint blocks // check if we have the checkpoint blocks
head := h.ethAPI.BlockNumber() head := h.ethAPI.BlockNumber()
if head < hexutil.Uint64(checkpoint.EndBlock.Uint64()) { if head < hexutil.Uint64(checkpoint.EndBlock.Uint64()) {
log.Debug("Head block behing checkpoint block", "head", head, "checkpoint end block", checkpoint.EndBlock) log.Debug("Head block behing checkpoint block", "head", head, "checkpoint end block", checkpoint.EndBlock)
return 0, common.Hash{}, fmt.Errorf("missing checkpoint blocks") return 0, common.Hash{}, errors.New("missing checkpoint blocks")
} }
// verify the root hash of checkpoint // verify the root hash of checkpoint
roothash, err := h.ethAPI.GetRootHash(context.Background(), checkpoint.StartBlock.Uint64(), checkpoint.EndBlock.Uint64()) roothash, err := h.ethAPI.GetRootHash(context.Background(), checkpoint.StartBlock.Uint64(), checkpoint.EndBlock.Uint64())
if err != nil { if err != nil {
log.Debug("Failed to get root hash of checkpoint while whitelisting") log.Debug("Failed to get root hash of checkpoint while whitelisting")
return 0, common.Hash{}, fmt.Errorf("failed to get local root hash") return 0, common.Hash{}, errors.New("failed to get local root hash")
} }
if roothash != checkpoint.RootHash.String()[2:] { if roothash != checkpoint.RootHash.String()[2:] {
log.Warn("Checkpoint root hash mismatch while whitelisting", "expected", checkpoint.RootHash.String()[2:], "got", roothash) log.Warn("Checkpoint root hash mismatch while whitelisting", "expected", checkpoint.RootHash.String()[2:], "got", roothash)
return 0, common.Hash{}, fmt.Errorf("checkpoint roothash mismatch") return 0, common.Hash{}, errors.New("checkpoint roothash mismatch")
} }
// fetch the end checkpoint block hash // fetch the end checkpoint block hash
block, err := h.ethAPI.GetBlockByNumber(context.Background(), rpc.BlockNumber(checkpoint.EndBlock.Uint64()), false) block, err := h.ethAPI.GetBlockByNumber(context.Background(), rpc.BlockNumber(checkpoint.EndBlock.Uint64()), false)
if err != nil { if err != nil {
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{}, fmt.Errorf("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