From 89daab4f9331ade853971246afda573aed55bd94 Mon Sep 17 00:00:00 2001 From: b00ris Date: Tue, 10 Jul 2018 17:28:02 +0300 Subject: [PATCH] les: fix --- les/fetcher.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/les/fetcher.go b/les/fetcher.go index 579a052c5c4d..e0a1dcfd26ab 100644 --- a/les/fetcher.go +++ b/les/fetcher.go @@ -786,11 +786,13 @@ func (f *lightFetcher) lastTrustedTreeNode(p *peer) (*types.Header, []common.Has if canonical.Number.Uint64() > f.lastTrustedHeader.Number.Uint64() { canonical = f.chain.GetHeaderByNumber(f.lastTrustedHeader.Number.Uint64()) } - ancestorHash := rawdb.FindCommonAncestor(f.pm.chainDb, canonical, f.lastTrustedHeader).Hash() - for current.Hash() != f.lastTrustedHeader.Hash() || current.Hash() != ancestorHash { - if current == nil { - break - } + commonAncestor := rawdb.FindCommonAncestor(f.pm.chainDb, canonical, f.lastTrustedHeader) + if commonAncestor == nil { + log.Error("Common ancestor of last trusted header and canonical header is nil", "canonical hash", canonical.Hash(), "trusted hash", f.lastTrustedHeader.Hash()) + return current, unapprovedHashes + } + + for f.isStopValidationTree(current, commonAncestor) == false { if f.isTrustedHash(current.Hash()) { break } @@ -800,6 +802,28 @@ func (f *lightFetcher) lastTrustedTreeNode(p *peer) (*types.Header, []common.Has return current, unapprovedHashes } +//isStopValidationTree found when we should stop on finding last trusted header +func (f *lightFetcher) isStopValidationTree(current *types.Header, commonAncestor *types.Header) bool { + if current == nil { + return true + } + + currentHash := current.Hash() + ancestorHash := commonAncestor.Hash() + + //found lastTrustedHeader + if currentHash == f.lastTrustedHeader.Hash() { + return true + } + + //found common ancestor between lastTrustedHeader and + if current.Hash() == ancestorHash { + return true + } + + return false +} + func (f *lightFetcher) setLastTrustedHeader(h *types.Header) { f.lock.Lock() defer f.lock.Unlock()