les: refine nextRequest node selection logic

This commit is contained in:
Wenshao Zhong 2018-11-16 14:03:12 +08:00
parent 667f5eca14
commit 1fdf746450

View file

@ -413,12 +413,21 @@ func (f *lightFetcher) nextRequest() (*distReq, uint64) {
bestAmount-- bestAmount--
bestTd := f.maxConfirmedTd bestTd := f.maxConfirmedTd
bestSyncing := false bestSyncing := false
bestTdFound := false
for p, fp := range f.peers { for p, fp := range f.peers {
for hash, n := range fp.nodeByHash { for hash, n := range fp.nodeByHash {
if !f.checkKnownNode(p, n) && !n.requested && (bestTd == nil || n.td.Cmp(bestTd) >= 0) { if !f.checkKnownNode(p, n) && !n.requested && (bestTd == nil || n.td.Cmp(bestTd) >= 0) {
amount := f.requestAmount(p, n) amount := f.requestAmount(p, n)
if amount < bestAmount { if (bestTd == nil || n.td.Cmp(bestTd) > 0) && (!bestTdFound || amount < bestAmount) {
// found one greater total difficulty force to sync with greater td
bestHash = hash
bestAmount = amount
bestTd = n.td
bestSyncing = fp.bestConfirmed == nil || fp.root == nil || !f.checkKnownNode(p, fp.root)
bestTdFound = true
} else if !bestTdFound && amount < bestAmount {
// sync with same difficulty
bestHash = hash bestHash = hash
bestAmount = amount bestAmount = amount
bestTd = n.td bestTd = n.td