Skip to content

Commit

Permalink
les: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
b00ris committed Jun 15, 2018
1 parent 298a8b5 commit 6a6f30b
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions les/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,14 +524,16 @@ func (f *lightFetcher) newFetcherDistReqForSync(bestHash common.Hash) *distReq {
go func() {
p := dp.(*peer)
p.Log().Debug("Synchronisation started")
f.lastTrustedHeader = f.chain.CurrentHeader()
if f.pm.isULCEnabled() {

This comment has been minimized.

Copy link
@themue

themue Jun 16, 2018

Does already look better in total. But my troubles are based on setting the last valid header inside the goroutine. IMHO it can be done direct in the func while synchronise() runs inside a goroutine.

//keep last validated header before sync
f.setLastValidHeader(f.chain.CurrentHeader())
}
f.pm.synchronise(p)
f.syncDone <- p
}()
return nil
},
}

}

// newFetcherDistReq creates a new request for the distributor.
Expand Down Expand Up @@ -718,7 +720,9 @@ func (f *lightFetcher) checkAnnouncedHeaders(fp *fetcherPeerInfo, headers []*typ
// downloaded headers as known. If none of the announced headers are found after
// syncing, the peer is dropped.
func (f *lightFetcher) checkSyncedHeaders(p *peer) {
f.peersLock.RLock()
fp := f.peers[p]
f.peersLock.RUnlock()
if fp == nil {
p.Log().Debug("Unknown peer to check sync headers")
return
Expand All @@ -729,20 +733,26 @@ func (f *lightFetcher) checkSyncedHeaders(p *peer) {
var unapprovedHashes []common.Hash
// Overwrite last announced for ULC mode
h, unapprovedHashes = f.lastValidTrieNode(p)
//rollback untrusted blocks
f.chain.Rollback(unapprovedHashes)
}

n := fp.lastAnnounced
var td *big.Int
trustedHeaderExisted := false

//find last trusted block
for n != nil {
//we found last trusted header
if n.hash == h.Hash() {
trustedHeaderExisted = true
}
if td = f.chain.GetTd(n.hash, n.number); td != nil && trustedHeaderExisted {
break
}
if n.hash == f.lastTrustedHeader.Hash() {

//break if we found last trusted hash before sync
if n.hash == f.getLastValidHeader().Hash() {
break
}
n = n.parent
Expand All @@ -763,7 +773,7 @@ func (f *lightFetcher) lastValidTrieNode(p *peer) (*types.Header, []common.Hash)
unapprovedHashes := make([]common.Hash, 0)

current := f.chain.CurrentHeader()
for current != nil || current != f.lastTrustedHeader {
for current != nil || current.Hash() != f.getLastValidHeader().Hash() {
if f.isTrustedHash(current.Hash()) {
break
}
Expand All @@ -775,6 +785,18 @@ func (f *lightFetcher) lastValidTrieNode(p *peer) (*types.Header, []common.Hash)
return current, unapprovedHashes
}

func (f *lightFetcher) setLastValidHeader(h *types.Header) {
f.lock.Lock()
defer f.lock.Unlock()
f.lastTrustedHeader = h
}

func (f *lightFetcher) getLastValidHeader() *types.Header {
f.lock.Lock()
defer f.lock.Unlock()
return f.lastTrustedHeader
}

// checkKnownNode checks if a block tree node is known (downloaded and validated)
// If it was not known previously but found in the database, sets its known flag
func (f *lightFetcher) checkKnownNode(p *peer, n *fetcherTreeNode) bool {
Expand Down

0 comments on commit 6a6f30b

Please sign in to comment.