From 8255d194ae43333ab06ccb413999e5ceb9efdf75 Mon Sep 17 00:00:00 2001 From: Manav Darji Date: Thu, 2 Jun 2022 16:34:46 +0530 Subject: [PATCH] minor fixes and enhancements --- consensus/bor/rest.go | 6 +++--- eth/backend.go | 8 +++---- eth/downloader/whitelist/service.go | 6 ++++++ eth/handler_bor.go | 33 +++++++++++++++++++++++------ 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/consensus/bor/rest.go b/consensus/bor/rest.go index 627262ffe6..cee7c11063 100644 --- a/consensus/bor/rest.go +++ b/consensus/bor/rest.go @@ -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 diff --git a/eth/backend.go b/eth/backend.go index 8f3930f605..bd941a6b11 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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. diff --git a/eth/downloader/whitelist/service.go b/eth/downloader/whitelist/service.go index 62bc842186..c3b6ad6462 100644 --- a/eth/downloader/whitelist/service.go +++ b/eth/downloader/whitelist/service.go @@ -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) } diff --git a/eth/handler_bor.go b/eth/handler_bor.go index ad1ef16288..11896f3c47 100644 --- a/eth/handler_bor.go +++ b/eth/handler_bor.go @@ -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"])