minor fixes and enhancements

This commit is contained in:
Manav Darji 2022-06-02 16:34:46 +05:30
parent d4e7603315
commit 8255d194ae
4 changed files with 40 additions and 13 deletions

View file

@ -91,17 +91,17 @@ func (h *HeimdallClient) FetchStateSyncEvents(fromID uint64, to int64) ([]*Event
// FetchLatestCheckpoint fetches the latest bor submitted checkpoint from heimdall // FetchLatestCheckpoint fetches the latest bor submitted checkpoint from heimdall
func (h *HeimdallClient) FetchLatestCheckpoint() (*Checkpoint, error) { func (h *HeimdallClient) FetchLatestCheckpoint() (*Checkpoint, error) {
var checkpoint Checkpoint checkpoint := &Checkpoint{}
response, err := h.Fetch("/checkpoints/latest", "") response, err := h.Fetch("/checkpoints/latest", "")
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := json.Unmarshal(response.Result, &checkpoint); err != nil { if err = json.Unmarshal(response.Result, checkpoint); err != nil {
return nil, err return nil, err
} }
return &checkpoint, nil return checkpoint, nil
} }
// Fetch fetches response from heimdall // Fetch fetches response from heimdall

View file

@ -605,7 +605,7 @@ func (s *Ethereum) startCheckpointWhitelistService() {
return return
} }
log.Warn("the first run", "err", err) log.Warn("unable to whitelist checkpoint - first run", "err", err)
} }
ticker := time.NewTicker(100 * time.Second) ticker := time.NewTicker(100 * time.Second)
@ -616,7 +616,7 @@ func (s *Ethereum) startCheckpointWhitelistService() {
case <-ticker.C: case <-ticker.C:
err := s.handleWhitelistCheckpoint() err := s.handleWhitelistCheckpoint()
if err != nil { if err != nil {
log.Warn("couldn't get whitelist checkpoint", "err", err) log.Warn("unable to whitelist checkpoint", "err", err)
} }
case <-s.closeCh: case <-s.closeCh:
return return
@ -625,8 +625,8 @@ func (s *Ethereum) startCheckpointWhitelistService() {
} }
var ( var (
ErrNotBorConsensus = errors.New("not Bor consensus was given") ErrNotBorConsensus = errors.New("not bor consensus was given")
ErrBorConsensusWithoutHeimdall = errors.New("Bor consensus without Heimdall") ErrBorConsensusWithoutHeimdall = errors.New("bor consensus without heimdall")
) )
// handleWhitelistCheckpoint handles the checkpoint whitelist mechanism. // handleWhitelistCheckpoint handles the checkpoint whitelist mechanism.

View file

@ -85,11 +85,17 @@ func (w *Service) ProcessCheckpoint(endBlockNum uint64, endBlockHash common.Hash
// GetCheckpointWhitelist returns the existing whitelisted // GetCheckpointWhitelist returns the existing whitelisted
// entries of checkpoint of the form block number -> block hash. // entries of checkpoint of the form block number -> block hash.
func (w *Service) GetCheckpointWhitelist() map[uint64]common.Hash { func (w *Service) GetCheckpointWhitelist() map[uint64]common.Hash {
w.m.Lock()
defer w.m.Unlock()
return w.checkpointWhitelist return w.checkpointWhitelist
} }
// PurgeCheckpointWhitelist purges data from checkpoint whitelist map // PurgeCheckpointWhitelist purges data from checkpoint whitelist map
func (w *Service) PurgeCheckpointWhitelist() { func (w *Service) PurgeCheckpointWhitelist() {
w.m.Lock()
defer w.m.Unlock()
w.checkpointWhitelist = make(map[uint64]common.Hash) w.checkpointWhitelist = make(map[uint64]common.Hash)
w.checkpointOrder = make([]uint64, 0) w.checkpointOrder = make([]uint64, 0)
} }

View file

@ -12,6 +12,27 @@ import (
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
) )
var (
// errCheckpoint is returned when we are unable to fetch the
// latest checkpoint from the local heimdall.
errCheckpoint = errors.New("failed to fetch latest checkpoint")
// errMissingCheckpoint is returned when we don't have the
// checkpoint blocks locally, yet.
errMissingCheckpoint = errors.New("missing checkpoint blocks")
// errRootHash is returned when we aren't able to calculate the root hash
// locally for a range of blocks.
errRootHash = errors.New("failed to get local root hash")
// errCheckpointRootHashMismatch is returned when the local root hash
// doesn't match with the root hash in checkpoint.
errCheckpointRootHashMismatch = errors.New("checkpoint roothash mismatch")
// errEndBlock is returned when we're unable to fetch a block locally.
errEndBlock = errors.New("failed to get end block")
)
// 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(bor *bor.Bor) (uint64, common.Hash, error) { func (h *ethHandler) fetchWhitelistCheckpoint(bor *bor.Bor) (uint64, common.Hash, error) {
@ -19,33 +40,33 @@ func (h *ethHandler) fetchWhitelistCheckpoint(bor *bor.Bor) (uint64, common.Hash
checkpoint, err := 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{}, errCheckpoint
} }
// 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 behind checkpoint block", "head", head, "checkpoint end block", checkpoint.EndBlock)
return 0, common.Hash{}, errors.New("missing checkpoint blocks") return 0, common.Hash{}, errMissingCheckpoint
} }
// 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{}, errors.New("failed to get local root hash") return 0, common.Hash{}, errRootHash
} }
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{}, errors.New("checkpoint roothash mismatch") return 0, common.Hash{}, errCheckpointRootHashMismatch
} }
// 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{}, errors.New("failed to get end block") return 0, common.Hash{}, errEndBlock
} }
hash := fmt.Sprintf("%v", block["hash"]) hash := fmt.Sprintf("%v", block["hash"])