Deduplicate blocks in getAncestorBlocks (#254)

* Deduplicate blocks in getAncestorBlocks

* Limit ancestor blocks in getAncestorBlocks
This commit is contained in:
Yuriy Glukhov 2019-04-26 09:21:46 +03:00 committed by GitHub
parent e69d5ecfe6
commit ddcdb63430
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 1 deletions

View File

@ -26,6 +26,7 @@ type
const
MaxRootsToRequest = 512
MaxHeadersToRequest = MaxRootsToRequest
MaxAncestorBlocksResponse = 256
func toHeader(b: BeaconBlock): BeaconBlockHeaderRLP =
BeaconBlockHeaderRLP(
@ -205,8 +206,11 @@ p2pProtocol BeaconSync(version = 1,
proc getAncestorBlocks(
peer: Peer,
needed: openarray[FetchRecord]) =
var resp = newseq[BeaconBlock]()
var resp = newSeqOfCap[BeaconBlock](needed.len)
let db = peer.networkState.db
var neededRoots = initSet[Eth2Digest]()
for rec in needed: neededRoots.incl(rec.root)
for rec in needed:
if (var blck = db.getBlock(rec.root); blck.isSome()):
# TODO validate historySlots
@ -214,11 +218,21 @@ p2pProtocol BeaconSync(version = 1,
for i in 0..<rec.historySlots.int:
resp.add(blck.get())
if resp.len >= MaxAncestorBlocksResponse:
break
if blck.get().previous_block_root in neededRoots:
# Don't send duplicate blocks, if neededRoots has roots that are
# in the same chain
break
if (blck = db.getBlock(blck.get().previous_block_root);
blck.isNone() or blck.get().slot < firstSlot):
break
if resp.len >= MaxAncestorBlocksResponse:
break
await response.send(resp)
proc ancestorBlocks(peer: Peer, blocks: openarray[BeaconBlock])