req: cap requested blocks better

also cap blocks in roots request
This commit is contained in:
Jacek Sieka 2020-04-22 08:18:45 +02:00 committed by zah
parent 62e3c250c0
commit 65ca74c980
3 changed files with 14 additions and 9 deletions

View File

@ -478,8 +478,8 @@ proc getBlockRange*(
for j in 0..<skipStep: for j in 0..<skipStep:
b = b.parent b = b.parent
if b.blck.slot == b.slot: if b.blck.slot == b.slot:
output[o - 1] = b.blck
dec o dec o
output[o] = b.blck
# Make sure the given input is cleared, just in case # Make sure the given input is cleared, just in case
for i in 0..<o: for i in 0..<o:

View File

@ -7,8 +7,6 @@ export datatypes, digest, chronos, chronicles
logScope: logScope:
topics = "syncman" topics = "syncman"
const MAX_REQUESTED_BLOCKS* = 20'u64
type type
GetSlotCallback* = proc(): Slot {.gcsafe, raises: [Defect].} GetSlotCallback* = proc(): Slot {.gcsafe, raises: [Defect].}

View File

@ -42,7 +42,10 @@ type
slot: Slot slot: Slot
const const
MAX_REQUESTED_BLOCKS = 20'u64 MAX_REQUESTED_BLOCKS = SLOTS_PER_EPOCH * 4
# A boundary on the number of blocks we'll allow in any single block
# request - typically clients will ask for an epoch or so at a time, but we
# allow a little bit more in case they want to stream blocks faster
proc importBlocks(state: BeaconSyncNetworkState, proc importBlocks(state: BeaconSyncNetworkState,
blocks: openarray[SignedBeaconBlock]) {.gcsafe.} = blocks: openarray[SignedBeaconBlock]) {.gcsafe.} =
@ -140,10 +143,12 @@ p2pProtocol BeaconSync(version = 1,
# Limit number of blocks in response # Limit number of blocks in response
count = min(count.Natural, blocks.len) count = min(count.Natural, blocks.len)
let startIndex = let
pool.getBlockRange(startSlot, step, blocks.toOpenArray(0, count - 1)) endIndex = count - 1
startIndex =
pool.getBlockRange(startSlot, step, blocks.toOpenArray(0, endIndex))
for b in blocks[startIndex..^1]: for b in blocks[startIndex..endIndex]:
doAssert not b.isNil, "getBlockRange should return non-nil blocks only" doAssert not b.isNil, "getBlockRange should return non-nil blocks only"
trace "wrote response block", slot = b.slot, roor = shortLog(b.root) trace "wrote response block", slot = b.slot, roor = shortLog(b.root)
await response.write(pool.get(b).data) await response.write(pool.get(b).data)
@ -157,16 +162,18 @@ p2pProtocol BeaconSync(version = 1,
libp2pProtocol("beacon_blocks_by_root", 1).} = libp2pProtocol("beacon_blocks_by_root", 1).} =
let let
pool = peer.networkState.blockPool pool = peer.networkState.blockPool
count = min(blockRoots.len, MAX_REQUESTED_BLOCKS)
var found = 0 var found = 0
for root in blockRoots:
for root in blockRoots[0..<count]:
let blockRef = pool.getRef(root) let blockRef = pool.getRef(root)
if not isNil(blockRef): if not isNil(blockRef):
await response.write(pool.get(blockRef).data) await response.write(pool.get(blockRef).data)
inc found inc found
debug "Block root request done", debug "Block root request done",
peer, roots = blockRoots.len, found peer, roots = blockRoots.len, count, found
proc beaconBlocks( proc beaconBlocks(
peer: Peer, peer: Peer,