Revert "Chunked sync"

This reverts commit c84d930304.
This commit is contained in:
Zahary Karadjov 2019-04-09 02:19:33 +03:00
parent ba48fcd165
commit cdf9baf5e6
1 changed files with 34 additions and 45 deletions

View File

@ -77,13 +77,12 @@ p2pProtocol BeaconSync(version = 1,
networkId = peer.networkState.networkId networkId = peer.networkState.networkId
blockPool = node.blockPool blockPool = node.blockPool
latestState = blockPool.latestState() latestState = blockPool.latestState()
headBlock = blockPool.head
var var
latestFinalizedRoot: Eth2Digest # TODO latestFinalizedRoot: Eth2Digest # TODO
latestFinalizedEpoch = latestState.finalized_epoch latestFinalizedEpoch = latestState.finalized_epoch
bestRoot: Eth2Digest # TODO bestRoot: Eth2Digest # TODO
bestSlot = headBlock.slot bestSlot = latestState.slot
let m = await handshake(peer, timeout = 10.seconds, let m = await handshake(peer, timeout = 10.seconds,
status(networkId, latestFinalizedRoot, status(networkId, latestFinalizedRoot,
@ -98,37 +97,42 @@ p2pProtocol BeaconSync(version = 1,
# where it needs to sync and it should execute the sync algorithm with a certain # 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. # number of randomly selected peers. The algorithm itself must be extracted in a proc.
try: try:
debug "Peer connected. Initiating sync", peer, bestSlot, remoteBestSlot = m.bestSlot debug "Peer connected. Initiating sync", peer
let bestDiff = cmp((latestFinalizedEpoch, bestSlot), (m.latestFinalizedEpoch, m.bestSlot)) let bestDiff = cmp((latestFinalizedEpoch, bestSlot), (m.latestFinalizedEpoch, m.bestSlot))
if bestDiff >= 0: if bestDiff == 0:
# Nothing to do? # Nothing to do?
trace "Nothing to sync", peer = peer.remote trace "Nothing to sync", peer = peer.remote
else: else:
# TODO: Check for WEAK_SUBJECTIVITY_PERIOD difference and terminate the # TODO: Check for WEAK_SUBJECTIVITY_PERIOD difference and terminate the
# connection if it's too big. # connection if it's too big.
var s = bestSlot + 1 if bestDiff > 0:
while s <= m.bestSlot: # Send roots
debug "Waiting for block roots", fromPeer = peer, remoteBestSlot = m.bestSlot, peer # TODO: Currently we send all block roots in one "packet". Maybe
let r = await peer.getBeaconBlockRoots(s, 512) # they should be split to multiple packets.
if not r.isSome: type Root = (Eth2Digest, Slot)
debug "Block roots not received", peer var roots = newSeqOfCap[Root](128)
break for i in int(m.bestSlot) + 1 .. int(bestSlot):
let roots = r.get.roots for r in blockPool.blockRootsForSlot(i.Slot):
debug "Received block roots", len = roots.len, peer roots.add((r, i.Slot))
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 "Block headers received. Requesting block bodies", peer debug "Sending block roots", peer, coveredSlots = roots.len
let bodies = await peer.getBeaconBlockBodies(bodiesRequest) await peer.beaconBlockRoots(roots)
node.importBlocks(roots, headers.get.blockHeaders, bodies.get.blockBodies) else:
s = blockPool.head.slot + 1 # Receive roots
else: debug "Waiting for block roots", fromPeer = peer
break let roots = await peer.nextMsg(BeaconSync.beaconBlockRoots)
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])
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)
except CatchableError: except CatchableError:
warn "Failed to sync with peer", peer, err = getCurrentExceptionMsg() warn "Failed to sync with peer", peer, err = getCurrentExceptionMsg()
@ -141,24 +145,9 @@ p2pProtocol BeaconSync(version = 1,
bestRoot: Eth2Digest, bestRoot: Eth2Digest,
bestSlot: Slot) {.libp2pProtocol("hello", "1.0.0").} bestSlot: Slot) {.libp2pProtocol("hello", "1.0.0").}
requestResponse: proc beaconBlockRoots(
proc getBeaconBlockRoots(peer: Peer, fromSlot: Slot, maxRoots: int) = peer: Peer,
debug "GetBeaconBlockRoots" roots: openarray[(Eth2Digest, Slot)]) {.libp2pProtocol("rpc/beacon_block_roots", "1.0.0").}
doAssert(maxRoots <= 512) # TODO: Validate maxRoots properly
var s = fromSlot
var roots = newSeqOfCap[(Eth2Digest, Slot)](maxRoots)
let db = peer.networkState.db
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
debug "Sending roots", len = roots.len
await response.send(roots)
proc beaconBlockRoots(peer: Peer, roots: openarray[(Eth2Digest, Slot)])
requestResponse: requestResponse:
proc getBeaconBlockHeaders( proc getBeaconBlockHeaders(
@ -168,15 +157,15 @@ p2pProtocol BeaconSync(version = 1,
maxHeaders: int, maxHeaders: int,
skipSlots: int) {.libp2pProtocol("rpc/beacon_block_headers", "1.0.0").} = skipSlots: int) {.libp2pProtocol("rpc/beacon_block_headers", "1.0.0").} =
# TODO: validate maxHeaders and implement slipSlots # TODO: validate maxHeaders and implement slipSlots
var s = slot var s = slot.int
var headers = newSeqOfCap[BeaconBlockHeaderRLP](maxHeaders) var headers = newSeqOfCap[BeaconBlockHeaderRLP](maxHeaders)
let db = peer.networkState.db let db = peer.networkState.db
let blockPool = peer.networkState.node.blockPool let blockPool = peer.networkState.node.blockPool
while headers.len < maxHeaders: while headers.len < maxHeaders:
for r in blockPool.blockRootsForSlot(s): for r in blockPool.blockRootsForSlot(s.Slot):
headers.add(db.getBlock(r).get().toHeader) headers.add(db.getBlock(r).get().toHeader)
if headers.len == maxHeaders: break if headers.len == maxHeaders: break
s += 1 inc s
await response.send(headers) await response.send(headers)
proc beaconBlockHeaders(peer: Peer, blockHeaders: openarray[BeaconBlockHeaderRLP]) proc beaconBlockHeaders(peer: Peer, blockHeaders: openarray[BeaconBlockHeaderRLP])