42
42
slot: Slot
43
43
44
44
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
46
49
47
50
proc importBlocks (state: BeaconSyncNetworkState ,
48
51
blocks: openarray [SignedBeaconBlock ]) {.gcsafe .} =
@@ -140,10 +143,12 @@ p2pProtocol BeaconSync(version = 1,
140
143
# Limit number of blocks in response
141
144
count = min (count.Natural , blocks.len)
142
145
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))
145
150
146
- for b in blocks[startIndex..^ 1 ]:
151
+ for b in blocks[startIndex.. endIndex ]:
147
152
doAssert not b.isNil, " getBlockRange should return non-nil blocks only"
148
153
trace " wrote response block" , slot = b.slot, roor = shortLog (b.root)
149
154
await response.write (pool.get (b).data)
@@ -157,16 +162,18 @@ p2pProtocol BeaconSync(version = 1,
157
162
libp2pProtocol (" beacon_blocks_by_root" , 1 ).} =
158
163
let
159
164
pool = peer.networkState.blockPool
165
+ count = min (blockRoots.len, MAX_REQUESTED_BLOCKS )
160
166
161
167
var found = 0
162
- for root in blockRoots:
168
+
169
+ for root in blockRoots[0 ..< count]:
163
170
let blockRef = pool.getRef (root)
164
171
if not isNil (blockRef):
165
172
await response.write (pool.get (blockRef).data)
166
173
inc found
167
174
168
175
debug " Block root request done" ,
169
- peer, roots = blockRoots.len, found
176
+ peer, roots = blockRoots.len, count, found
170
177
171
178
proc beaconBlocks (
172
179
peer: Peer ,
0 commit comments