Skip to content

Commit

Permalink
req: cap requested blocks better
Browse files Browse the repository at this point in the history
also cap blocks in roots request
  • Loading branch information
arnetheduck authored and zah committed Apr 22, 2020
1 parent 62e3c25 commit 65ca74c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion beacon_chain/block_pool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ proc getBlockRange*(
for j in 0..<skipStep:
b = b.parent
if b.blck.slot == b.slot:
output[o - 1] = b.blck
dec o
output[o] = b.blck

# Make sure the given input is cleared, just in case
for i in 0..<o:
Expand Down
2 changes: 0 additions & 2 deletions beacon_chain/sync_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export datatypes, digest, chronos, chronicles
logScope:
topics = "syncman"

const MAX_REQUESTED_BLOCKS* = 20'u64

type
GetSlotCallback* = proc(): Slot {.gcsafe, raises: [Defect].}

Expand Down
19 changes: 13 additions & 6 deletions beacon_chain/sync_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ type
slot: Slot

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,
blocks: openarray[SignedBeaconBlock]) {.gcsafe.} =
Expand Down Expand Up @@ -140,10 +143,12 @@ p2pProtocol BeaconSync(version = 1,
# Limit number of blocks in response
count = min(count.Natural, blocks.len)

let startIndex =
pool.getBlockRange(startSlot, step, blocks.toOpenArray(0, count - 1))
let
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"
trace "wrote response block", slot = b.slot, roor = shortLog(b.root)
await response.write(pool.get(b).data)
Expand All @@ -157,16 +162,18 @@ p2pProtocol BeaconSync(version = 1,
libp2pProtocol("beacon_blocks_by_root", 1).} =
let
pool = peer.networkState.blockPool
count = min(blockRoots.len, MAX_REQUESTED_BLOCKS)

var found = 0
for root in blockRoots:

for root in blockRoots[0..<count]:
let blockRef = pool.getRef(root)
if not isNil(blockRef):
await response.write(pool.get(blockRef).data)
inc found

debug "Block root request done",
peer, roots = blockRoots.len, found
peer, roots = blockRoots.len, count, found

proc beaconBlocks(
peer: Peer,
Expand Down

0 comments on commit 65ca74c

Please sign in to comment.