From 54f894fc7e9b376c850912ffbb632dc91af09889 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Tue, 9 Apr 2019 10:53:40 +0300 Subject: [PATCH 1/2] Chunked sync second try This reverts commit cdf9baf5e6a82d3f69762462206f7f8e6d5fea01. --- beacon_chain/sync_protocol.nim | 91 ++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 33 deletions(-) diff --git a/beacon_chain/sync_protocol.nim b/beacon_chain/sync_protocol.nim index 73866720c..4583074a9 100644 --- a/beacon_chain/sync_protocol.nim +++ b/beacon_chain/sync_protocol.nim @@ -23,6 +23,10 @@ type node*: BeaconNode db*: BeaconChainDB +const + MaxRootsToRequest = 512 + MaxHeadersToRequest = MaxRootsToRequest + func toHeader(b: BeaconBlock): BeaconBlockHeaderRLP = BeaconBlockHeaderRLP( slot: b.slot.uint64, @@ -77,12 +81,13 @@ p2pProtocol BeaconSync(version = 1, networkId = peer.networkState.networkId blockPool = node.blockPool latestState = blockPool.latestState() + headBlock = blockPool.head var latestFinalizedRoot: Eth2Digest # TODO latestFinalizedEpoch = latestState.finalized_epoch bestRoot: Eth2Digest # TODO - bestSlot = latestState.slot + bestSlot = headBlock.slot let m = await handshake(peer, timeout = 10.seconds, status(networkId, latestFinalizedRoot, @@ -97,42 +102,49 @@ p2pProtocol BeaconSync(version = 1, # where it needs to sync and it should execute the sync algorithm with a certain # number of randomly selected peers. The algorithm itself must be extracted in a proc. try: - debug "Peer connected. Initiating sync", peer + debug "Peer connected. Initiating sync", peer, bestSlot, remoteBestSlot = m.bestSlot let bestDiff = cmp((latestFinalizedEpoch, bestSlot), (m.latestFinalizedEpoch, m.bestSlot)) - if bestDiff == 0: + if bestDiff >= 0: # Nothing to do? trace "Nothing to sync", peer = peer.remote else: # TODO: Check for WEAK_SUBJECTIVITY_PERIOD difference and terminate the # connection if it's too big. - if bestDiff > 0: - # Send roots - # TODO: Currently we send all block roots in one "packet". Maybe - # they should be split to multiple packets. - type Root = (Eth2Digest, Slot) - var roots = newSeqOfCap[Root](128) - for i in int(m.bestSlot) + 1 .. int(bestSlot): - for r in blockPool.blockRootsForSlot(i.Slot): - roots.add((r, i.Slot)) + var s = bestSlot + 1 + while s <= m.bestSlot: + debug "Waiting for block roots", fromPeer = peer, remoteBestSlot = m.bestSlot, peer + let r = await peer.getBeaconBlockRoots(s, MaxRootsToRequest) + if not r.isSome: + debug "Block roots not received", peer + break + let roots = r.get.roots + debug "Received block roots", len = roots.len, peer + if roots.len != 0: + let headers = await peer.getBeaconBlockHeaders(bestRoot, s, roots.len, 0) + var bodiesRequest = newSeqOfCap[Eth2Digest](roots.len) + for r in roots: + bodiesRequest.add(r[0]) - debug "Sending block roots", peer, coveredSlots = roots.len - await peer.beaconBlockRoots(roots) - else: - # Receive roots - debug "Waiting for block roots", fromPeer = peer - let roots = await peer.nextMsg(BeaconSync.beaconBlockRoots) + debug "Block headers received. Requesting block bodies", peer + let bodies = await peer.getBeaconBlockBodies(bodiesRequest) + node.importBlocks(roots, headers.get.blockHeaders, bodies.get.blockBodies) - debug "Block roots received. Requesting block headers", bestRoot, bestSlot - let headers = await peer.getBeaconBlockHeaders(bestRoot, bestSlot, roots.roots.len, 0) - var bodiesRequest = newSeqOfCap[Eth2Digest](roots.roots.len) - for r in roots.roots: - bodiesRequest.add(r[0]) + let lastSlot = roots[^1][1] + if roots.len >= MaxRootsToRequest: + # Next batch of roots starts with the last slot of the current one + # to make sure we did not miss any roots with this slot that did + # not fit into the response. - debug "Block headers received. Requesting block bodies", blocks = bodiesRequest - let bodies = await peer.getBeaconBlockBodies(bodiesRequest) - node.importBlocks(roots.roots, headers.get.blockHeaders, bodies.get.blockBodies) + if s == lastSlot: + info "Too many roots for a single slot while syncing" + break + s = lastSlot + else: + s = lastSlot + 1 + else: + break except CatchableError: warn "Failed to sync with peer", peer, err = getCurrentExceptionMsg() @@ -145,9 +157,21 @@ p2pProtocol BeaconSync(version = 1, bestRoot: Eth2Digest, bestSlot: Slot) {.libp2pProtocol("hello", "1.0.0").} - proc beaconBlockRoots( - peer: Peer, - roots: openarray[(Eth2Digest, Slot)]) {.libp2pProtocol("rpc/beacon_block_roots", "1.0.0").} + requestResponse: + proc getBeaconBlockRoots(peer: Peer, fromSlot: Slot, maxRoots: int) = + doAssert(maxRoots <= MaxRootsToRequest) # TODO: Validate maxRoots properly + var s = fromSlot + var roots = newSeqOfCap[(Eth2Digest, Slot)](maxRoots) + let blockPool = peer.networkState.node.blockPool + let maxSlot = blockPool.head.slot + while s <= maxSlot: + for r in blockPool.blockRootsForSlot(s): + roots.add((r, s)) + if roots.len == maxRoots: break + s += 1 + await response.send(roots) + + proc beaconBlockRoots(peer: Peer, roots: openarray[(Eth2Digest, Slot)]) requestResponse: proc getBeaconBlockHeaders( @@ -157,15 +181,16 @@ p2pProtocol BeaconSync(version = 1, maxHeaders: int, skipSlots: int) {.libp2pProtocol("rpc/beacon_block_headers", "1.0.0").} = # TODO: validate maxHeaders and implement slipSlots - var s = slot.int + var s = slot var headers = newSeqOfCap[BeaconBlockHeaderRLP](maxHeaders) let db = peer.networkState.db let blockPool = peer.networkState.node.blockPool - while headers.len < maxHeaders: - for r in blockPool.blockRootsForSlot(s.Slot): + let maxSlot = blockPool.head.slot + while s <= maxSlot: + for r in blockPool.blockRootsForSlot(s): headers.add(db.getBlock(r).get().toHeader) if headers.len == maxHeaders: break - inc s + s += 1 await response.send(headers) proc beaconBlockHeaders(peer: Peer, blockHeaders: openarray[BeaconBlockHeaderRLP]) From b916b1e3e4f16a7b3cbdf5a73dfd880f527244f4 Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Wed, 10 Apr 2019 15:47:37 +0300 Subject: [PATCH 2/2] Slightly more validations --- beacon_chain/sync_protocol.nim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/beacon_chain/sync_protocol.nim b/beacon_chain/sync_protocol.nim index 4583074a9..8c7a6d1bf 100644 --- a/beacon_chain/sync_protocol.nim +++ b/beacon_chain/sync_protocol.nim @@ -122,6 +122,11 @@ p2pProtocol BeaconSync(version = 1, let roots = r.get.roots debug "Received block roots", len = roots.len, peer if roots.len != 0: + if roots.len > MaxRootsToRequest: + # Attack? + await peer.disconnect(BreachOfProtocol, true) + break + let headers = await peer.getBeaconBlockHeaders(bestRoot, s, roots.len, 0) var bodiesRequest = newSeqOfCap[Eth2Digest](roots.len) for r in roots: @@ -132,7 +137,7 @@ p2pProtocol BeaconSync(version = 1, node.importBlocks(roots, headers.get.blockHeaders, bodies.get.blockBodies) let lastSlot = roots[^1][1] - if roots.len >= MaxRootsToRequest: + if roots.len == MaxRootsToRequest: # Next batch of roots starts with the last slot of the current one # to make sure we did not miss any roots with this slot that did # not fit into the response. @@ -159,7 +164,7 @@ p2pProtocol BeaconSync(version = 1, requestResponse: proc getBeaconBlockRoots(peer: Peer, fromSlot: Slot, maxRoots: int) = - doAssert(maxRoots <= MaxRootsToRequest) # TODO: Validate maxRoots properly + let maxRoots = min(MaxRootsToRequest, maxRoots) var s = fromSlot var roots = newSeqOfCap[(Eth2Digest, Slot)](maxRoots) let blockPool = peer.networkState.node.blockPool @@ -180,7 +185,8 @@ p2pProtocol BeaconSync(version = 1, slot: Slot, maxHeaders: int, skipSlots: int) {.libp2pProtocol("rpc/beacon_block_headers", "1.0.0").} = - # TODO: validate maxHeaders and implement slipSlots + # TODO: validate implement slipSlots + let maxHeaders = min(MaxHeadersToRequest, maxHeaders) var s = slot var headers = newSeqOfCap[BeaconBlockHeaderRLP](maxHeaders) let db = peer.networkState.db