@@ -32,8 +32,9 @@ import (
3232)
3333
3434const (
35- blockDelayTimeout = time .Second * 10 // timeout for a peer to announce a head that has already been confirmed by others
36- maxNodeCount = 20 // maximum number of fetcherTreeNode entries remembered for each peer
35+ blockDelayTimeout = time .Second * 10 // timeout for a peer to announce a head that has already been confirmed by others
36+ maxNodeCount = 20 // maximum number of fetcherTreeNode entries remembered for each peer
37+ serverStateAvailable = 100 // number of recent blocks where state availability is assumed
3738)
3839
3940// lightFetcher implements retrieval of newly announced headers. It also provides a peerHasBlock function for the
@@ -215,8 +216,8 @@ func (f *lightFetcher) syncLoop() {
215216// registerPeer adds a new peer to the fetcher's peer set
216217func (f * lightFetcher ) registerPeer (p * peer ) {
217218 p .lock .Lock ()
218- p .hasBlock = func (hash common.Hash , number uint64 ) bool {
219- return f .peerHasBlock (p , hash , number )
219+ p .hasBlock = func (hash common.Hash , number uint64 , hasState bool ) bool {
220+ return f .peerHasBlock (p , hash , number , hasState )
220221 }
221222 p .lock .Unlock ()
222223
@@ -344,21 +345,27 @@ func (f *lightFetcher) announce(p *peer, head *announceData) {
344345
345346// peerHasBlock returns true if we can assume the peer knows the given block
346347// based on its announcements
347- func (f * lightFetcher ) peerHasBlock (p * peer , hash common.Hash , number uint64 ) bool {
348+ func (f * lightFetcher ) peerHasBlock (p * peer , hash common.Hash , number uint64 , hasState bool ) bool {
348349 f .lock .Lock ()
349350 defer f .lock .Unlock ()
350351
352+ fp := f .peers [p ]
353+ if fp == nil || fp .root == nil {
354+ return false
355+ }
356+
357+ if hasState {
358+ if fp .lastAnnounced == nil || fp .lastAnnounced .number > number + serverStateAvailable {
359+ return false
360+ }
361+ }
362+
351363 if f .syncing {
352364 // always return true when syncing
353365 // false positives are acceptable, a more sophisticated condition can be implemented later
354366 return true
355367 }
356368
357- fp := f .peers [p ]
358- if fp == nil || fp .root == nil {
359- return false
360- }
361-
362369 if number >= fp .root .number {
363370 // it is recent enough that if it is known, is should be in the peer's block tree
364371 return fp .nodeByHash [hash ] != nil
0 commit comments