Skip to content

Commit 65ca74c

Browse files
arnetheduckzah
authored andcommitted
req: cap requested blocks better
also cap blocks in roots request
1 parent 62e3c25 commit 65ca74c

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

beacon_chain/block_pool.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,8 @@ proc getBlockRange*(
478478
for j in 0..<skipStep:
479479
b = b.parent
480480
if b.blck.slot == b.slot:
481-
output[o - 1] = b.blck
482481
dec o
482+
output[o] = b.blck
483483

484484
# Make sure the given input is cleared, just in case
485485
for i in 0..<o:

beacon_chain/sync_manager.nim

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ export datatypes, digest, chronos, chronicles
77
logScope:
88
topics = "syncman"
99

10-
const MAX_REQUESTED_BLOCKS* = 20'u64
11-
1210
type
1311
GetSlotCallback* = proc(): Slot {.gcsafe, raises: [Defect].}
1412

beacon_chain/sync_protocol.nim

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ type
4242
slot: Slot
4343

4444
const
45-
MAX_REQUESTED_BLOCKS = 20'u64
45+
MAX_REQUESTED_BLOCKS = SLOTS_PER_EPOCH * 4
46+
# A boundary on the number of blocks we'll allow in any single block
47+
# request - typically clients will ask for an epoch or so at a time, but we
48+
# allow a little bit more in case they want to stream blocks faster
4649

4750
proc importBlocks(state: BeaconSyncNetworkState,
4851
blocks: openarray[SignedBeaconBlock]) {.gcsafe.} =
@@ -140,10 +143,12 @@ p2pProtocol BeaconSync(version = 1,
140143
# Limit number of blocks in response
141144
count = min(count.Natural, blocks.len)
142145

143-
let startIndex =
144-
pool.getBlockRange(startSlot, step, blocks.toOpenArray(0, count - 1))
146+
let
147+
endIndex = count - 1
148+
startIndex =
149+
pool.getBlockRange(startSlot, step, blocks.toOpenArray(0, endIndex))
145150

146-
for b in blocks[startIndex..^1]:
151+
for b in blocks[startIndex..endIndex]:
147152
doAssert not b.isNil, "getBlockRange should return non-nil blocks only"
148153
trace "wrote response block", slot = b.slot, roor = shortLog(b.root)
149154
await response.write(pool.get(b).data)
@@ -157,16 +162,18 @@ p2pProtocol BeaconSync(version = 1,
157162
libp2pProtocol("beacon_blocks_by_root", 1).} =
158163
let
159164
pool = peer.networkState.blockPool
165+
count = min(blockRoots.len, MAX_REQUESTED_BLOCKS)
160166

161167
var found = 0
162-
for root in blockRoots:
168+
169+
for root in blockRoots[0..<count]:
163170
let blockRef = pool.getRef(root)
164171
if not isNil(blockRef):
165172
await response.write(pool.get(blockRef).data)
166173
inc found
167174

168175
debug "Block root request done",
169-
peer, roots = blockRoots.len, found
176+
peer, roots = blockRoots.len, count, found
170177

171178
proc beaconBlocks(
172179
peer: Peer,

0 commit comments

Comments
 (0)