diff --git a/beacon_chain/sync/sync_protocol.nim b/beacon_chain/sync/sync_protocol.nim index 056fbca7ae..32a365c324 100644 --- a/beacon_chain/sync/sync_protocol.nim +++ b/beacon_chain/sync/sync_protocol.nim @@ -261,148 +261,6 @@ p2pProtocol BeaconSync(version = 1, {.libp2pProtocol("metadata", 2, isRequired = true).} = return peer.network.metadata - proc beaconBlocksByRange( - peer: Peer, - startSlot: Slot, - reqCount: uint64, - reqStep: uint64, - response: MultipleChunksResponse[ - phase0.SignedBeaconBlock, MAX_REQUEST_BLOCKS]) - {.async, libp2pProtocol("beacon_blocks_by_range", 1).} = - # TODO Semantically, this request should return a non-ref, but doing so - # runs into extreme inefficiency due to the compiler introducing - # hidden copies - in future nim versions with move support, this should - # be revisited - # TODO This code is more complicated than it needs to be, since the type - # of the multiple chunks response is not actually used in this server - # implementation (it's used to derive the signature of the client - # function, not in the code below!) - # TODO although you can't tell from this function definition, a magic - # client call that returns `seq[ref SignedBeaconBlock]` will - # will be generated by the libp2p macro - we guarantee that seq items - # are `not-nil` in the implementation - # TODO reqStep is deprecated - future versions can remove support for - # values != 1: https://github.com/ethereum/consensus-specs/pull/2856 - trace "got range request", peer, startSlot, - count = reqCount, step = reqStep - if reqCount == 0'u64 or reqStep == 0'u64: - raise newException(InvalidInputsError, "Empty range requested") - - let - dag = peer.networkState.dag - - if startSlot.epoch >= dag.cfg.ALTAIR_FORK_EPOCH: - # "Clients MAY limit the number of blocks in the response." - # https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.1/specs/phase0/p2p-interface.md#beaconblocksbyrange - debug "Block range v1 request for post-altair range", - peer, startSlot, reqCount, reqStep - return - - # Phase 0 blocks are never optimistic. - - var blocks: array[MAX_REQUEST_BLOCKS, BlockId] - - let - # Limit number of blocks in response - count = int min(reqCount, blocks.lenu64) - endIndex = count - 1 - startIndex = - dag.getBlockRange(startSlot, reqStep, blocks.toOpenArray(0, endIndex)) - - var - found = 0 - bytes: seq[byte] - - for i in startIndex..endIndex: - if blocks[i].slot.epoch >= dag.cfg.ALTAIR_FORK_EPOCH: - # Skipping all subsequent blocks should be OK because the spec says: - # "Clients MAY limit the number of blocks in the response." - # https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/p2p-interface.md#beaconblocksbyrange - # - # Also, our response would be indistinguishable from a node - # that have been synced exactly to the altair transition slot. - break - if dag.getBlockSZ(blocks[i], bytes): - let uncompressedLen = uncompressedLenFramed(bytes).valueOr: - warn "Cannot read block size, database corrupt?", - bytes = bytes.len(), blck = shortLog(blocks[i]) - continue - - # TODO extract from libp2pProtocol - peer.awaitQuota(blockResponseCost, "beacon_blocks_by_range/1") - peer.network.awaitQuota(blockResponseCost, "beacon_blocks_by_range/1") - - await response.writeBytesSZ(uncompressedLen, bytes, []) # phase0 bytes - - inc found - - debug "Block range request done", - peer, startSlot, count, reqStep, found - - proc beaconBlocksByRoot( - peer: Peer, - # Please note that the SSZ list here ensures that the - # spec constant MAX_REQUEST_BLOCKS is enforced: - blockRoots: BlockRootsList, - response: MultipleChunksResponse[ - phase0.SignedBeaconBlock, MAX_REQUEST_BLOCKS]) - {.async, libp2pProtocol("beacon_blocks_by_root", 1).} = - # TODO Semantically, this request should return a non-ref, but doing so - # runs into extreme inefficiency due to the compiler introducing - # hidden copies - in future nim versions with move support, this should - # be revisited - # TODO This code is more complicated than it needs to be, since the type - # of the multiple chunks response is not actually used in this server - # implementation (it's used to derive the signature of the client - # function, not in the code below!) - # TODO although you can't tell from this function definition, a magic - # client call that returns `seq[ref SignedBeaconBlock]` will - # will be generated by the libp2p macro - we guarantee that seq items - # are `not-nil` in the implementation - - if blockRoots.len == 0: - raise newException(InvalidInputsError, "No blocks requested") - - let - dag = peer.networkState.dag - count = blockRoots.len - - var - found = 0 - bytes: seq[byte] - - for i in 0..= dag.cfg.ALTAIR_FORK_EPOCH: - # Skipping this block should be fine because the spec says: - # "Clients MAY limit the number of blocks in the response." - # https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.1/specs/phase0/p2p-interface.md#beaconblocksbyroot - # - # Also, our response would be indistinguishable from a node - # that have been synced exactly to the altair transition slot. - continue - - # Phase 0 blocks are never optimistic. - - if dag.getBlockSZ(blockRef.bid, bytes): - let uncompressedLen = uncompressedLenFramed(bytes).valueOr: - warn "Cannot read block size, database corrupt?", - bytes = bytes.len(), blck = shortLog(blockRef) - continue - - # TODO extract from libp2pProtocol - peer.awaitQuota(blockResponseCost, "beacon_blocks_by_root/1") - peer.network.awaitQuota(blockResponseCost, "beacon_blocks_by_root/1") - - await response.writeBytesSZ(uncompressedLen, bytes, []) # phase0 - inc found - - debug "Block root request done", - peer, roots = blockRoots.len, count, found - proc beaconBlocksByRange_v2( peer: Peer, startSlot: Slot,