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
func (h *HeimdallClient) FetchLatestCheckpoint() (*Checkpoint, error) {
var checkpoint Checkpoint
checkpoint := &Checkpoint{}
response, err := h.Fetch("/checkpoints/latest", "")
if err != nil {
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 &checkpoint, nil
return checkpoint, nil
}
// Fetch fetches response from heimdall

View file

@ -605,7 +605,7 @@ func (s *Ethereum) startCheckpointWhitelistService() {
return
}
log.Warn("the first run", "err", err)
log.Warn("unable to whitelist checkpoint - first run", "err", err)
}
ticker := time.NewTicker(100 * time.Second)
@ -616,7 +616,7 @@ func (s *Ethereum) startCheckpointWhitelistService() {
case <-ticker.C:
err := s.handleWhitelistCheckpoint()
if err != nil {
log.Warn("couldn't get whitelist checkpoint", "err", err)
log.Warn("unable to whitelist checkpoint", "err", err)
}
case <-s.closeCh:
return
@ -625,8 +625,8 @@ func (s *Ethereum) startCheckpointWhitelistService() {
}
var (
ErrNotBorConsensus = errors.New("not Bor consensus was given")
ErrBorConsensusWithoutHeimdall = errors.New("Bor consensus without Heimdall")
ErrNotBorConsensus = errors.New("not bor consensus was given")
ErrBorConsensusWithoutHeimdall = errors.New("bor consensus without heimdall")
)
// 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
// entries of checkpoint of the form block number -> block hash.
func (w *Service) GetCheckpointWhitelist() map[uint64]common.Hash {
w.m.Lock()
defer w.m.Unlock()
return w.checkpointWhitelist
}
// PurgeCheckpointWhitelist purges data from checkpoint whitelist map
func (w *Service) PurgeCheckpointWhitelist() {
w.m.Lock()
defer w.m.Unlock()
w.checkpointWhitelist = make(map[uint64]common.Hash)
w.checkpointOrder = make([]uint64, 0)
}

View file

@ -12,6 +12,27 @@ import (
"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
// and verifies the data against bor data.
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()
if err != nil {
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
head := h.ethAPI.BlockNumber()
if head < hexutil.Uint64(checkpoint.EndBlock.Uint64()) {
log.Debug("Head block behing checkpoint block", "head", head, "checkpoint end block", checkpoint.EndBlock)
return 0, common.Hash{}, errors.New("missing checkpoint blocks")
log.Debug("Head block behind checkpoint block", "head", head, "checkpoint end block", checkpoint.EndBlock)
return 0, common.Hash{}, errMissingCheckpoint
}
// verify the root hash of checkpoint
roothash, err := h.ethAPI.GetRootHash(context.Background(), checkpoint.StartBlock.Uint64(), checkpoint.EndBlock.Uint64())
if err != nil {
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:] {
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
block, err := h.ethAPI.GetBlockByNumber(context.Background(), rpc.BlockNumber(checkpoint.EndBlock.Uint64()), false)
if err != nil {
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"])