Skip to content

Commit b7bbe66

Browse files
zsfelfoldiholiman
authored andcommitted
les: limit state ODR retrievals to the last 100 blocks (#17744)
1 parent 96fd50b commit b7bbe66

5 files changed

Lines changed: 26 additions & 19 deletions

File tree

‎les/fetcher.go‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ import (
3232
)
3333

3434
const (
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
216217
func (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

‎les/odr_requests.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (r *BlockRequest) GetCost(peer *peer) uint64 {
8484

8585
// CanSend tells if a certain peer is suitable for serving the given request
8686
func (r *BlockRequest) CanSend(peer *peer) bool {
87-
return peer.HasBlock(r.Hash, r.Number)
87+
return peer.HasBlock(r.Hash, r.Number, false)
8888
}
8989

9090
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
@@ -140,7 +140,7 @@ func (r *ReceiptsRequest) GetCost(peer *peer) uint64 {
140140

141141
// CanSend tells if a certain peer is suitable for serving the given request
142142
func (r *ReceiptsRequest) CanSend(peer *peer) bool {
143-
return peer.HasBlock(r.Hash, r.Number)
143+
return peer.HasBlock(r.Hash, r.Number, false)
144144
}
145145

146146
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
@@ -202,7 +202,7 @@ func (r *TrieRequest) GetCost(peer *peer) uint64 {
202202

203203
// CanSend tells if a certain peer is suitable for serving the given request
204204
func (r *TrieRequest) CanSend(peer *peer) bool {
205-
return peer.HasBlock(r.Id.BlockHash, r.Id.BlockNumber)
205+
return peer.HasBlock(r.Id.BlockHash, r.Id.BlockNumber, true)
206206
}
207207

208208
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)
@@ -272,7 +272,7 @@ func (r *CodeRequest) GetCost(peer *peer) uint64 {
272272

273273
// CanSend tells if a certain peer is suitable for serving the given request
274274
func (r *CodeRequest) CanSend(peer *peer) bool {
275-
return peer.HasBlock(r.Id.BlockHash, r.Id.BlockNumber)
275+
return peer.HasBlock(r.Id.BlockHash, r.Id.BlockNumber, true)
276276
}
277277

278278
// Request sends an ODR request to the LES network (implementation of LesOdrRequest)

‎les/odr_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) {
194194
client.peers.Register(client.rPeer)
195195
time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
196196
client.peers.lock.Lock()
197-
client.rPeer.hasBlock = func(common.Hash, uint64) bool { return true }
197+
client.rPeer.hasBlock = func(common.Hash, uint64, bool) bool { return true }
198198
client.peers.lock.Unlock()
199199
test(5)
200200
// still expect all retrievals to pass, now data should be cached locally

‎les/peer.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ type peer struct {
6767
sendQueue *execQueue
6868

6969
poolEntry *poolEntry
70-
hasBlock func(common.Hash, uint64) bool
70+
hasBlock func(common.Hash, uint64, bool) bool
7171
responseErrors int
7272

7373
fcClient *flowcontrol.ClientNode // nil if the peer is server only
@@ -171,11 +171,11 @@ func (p *peer) GetRequestCost(msgcode uint64, amount int) uint64 {
171171
}
172172

173173
// HasBlock checks if the peer has a given block
174-
func (p *peer) HasBlock(hash common.Hash, number uint64) bool {
174+
func (p *peer) HasBlock(hash common.Hash, number uint64, hasState bool) bool {
175175
p.lock.RLock()
176176
hasBlock := p.hasBlock
177177
p.lock.RUnlock()
178-
return hasBlock != nil && hasBlock(hash, number)
178+
return hasBlock != nil && hasBlock(hash, number, hasState)
179179
}
180180

181181
// SendAnnounce announces the availability of a number of blocks through

‎les/request_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func testAccess(t *testing.T, protocol int, fn accessTestFn) {
115115
client.peers.Register(client.rPeer)
116116
time.Sleep(time.Millisecond * 10) // ensure that all peerSetNotify callbacks are executed
117117
client.rPeer.lock.Lock()
118-
client.rPeer.hasBlock = func(common.Hash, uint64) bool { return true }
118+
client.rPeer.hasBlock = func(common.Hash, uint64, bool) bool { return true }
119119
client.rPeer.lock.Unlock()
120120
// expect all retrievals to pass
121121
test(5)

0 commit comments

Comments
 (0)