2018-11-23 23:58:49 +00:00
|
|
|
import
|
2019-10-25 10:59:56 +00:00
|
|
|
options, tables, sets, macros,
|
2020-03-22 20:54:47 +00:00
|
|
|
chronicles, chronos, stew/ranges/bitranges, libp2p/switch,
|
2020-05-12 16:26:58 +00:00
|
|
|
spec/[datatypes, crypto, digest],
|
2020-06-03 13:52:02 +00:00
|
|
|
beacon_node_types, eth2_network, block_pool
|
2019-03-12 15:03:14 +00:00
|
|
|
|
2019-11-11 01:28:13 +00:00
|
|
|
logScope:
|
|
|
|
topics = "sync"
|
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
const
|
|
|
|
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
|
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
type
|
2020-04-20 14:59:18 +00:00
|
|
|
StatusMsg* = object
|
|
|
|
forkDigest*: ForkDigest
|
|
|
|
finalizedRoot*: Eth2Digest
|
|
|
|
finalizedEpoch*: Epoch
|
|
|
|
headRoot*: Eth2Digest
|
|
|
|
headSlot*: Slot
|
|
|
|
|
2019-07-08 13:19:52 +00:00
|
|
|
ValidatorSetDeltaFlags {.pure.} = enum
|
|
|
|
Activation = 0
|
|
|
|
Exit = 1
|
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
ValidatorChangeLogEntry* = object
|
|
|
|
case kind*: ValidatorSetDeltaFlags
|
2018-12-03 17:46:22 +00:00
|
|
|
of Activation:
|
2018-11-29 01:08:34 +00:00
|
|
|
pubkey: ValidatorPubKey
|
2018-11-23 23:58:49 +00:00
|
|
|
else:
|
|
|
|
index: uint32
|
|
|
|
|
2020-02-29 15:15:44 +00:00
|
|
|
BeaconBlockCallback* = proc(signedBlock: SignedBeaconBlock) {.gcsafe.}
|
2020-04-15 02:41:22 +00:00
|
|
|
|
2019-09-08 22:03:41 +00:00
|
|
|
BeaconSyncNetworkState* = ref object
|
2019-11-25 14:36:25 +00:00
|
|
|
blockPool*: BlockPool
|
2020-04-15 02:41:22 +00:00
|
|
|
forkDigest*: ForkDigest
|
2019-11-25 14:36:25 +00:00
|
|
|
onBeaconBlock*: BeaconBlockCallback
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2019-09-08 22:03:41 +00:00
|
|
|
BeaconSyncPeerState* = ref object
|
2020-04-20 14:59:18 +00:00
|
|
|
initialStatusReceived*: bool
|
|
|
|
statusMsg*: StatusMsg
|
2019-09-08 22:03:41 +00:00
|
|
|
|
2019-07-03 07:35:05 +00:00
|
|
|
BlockRootSlot* = object
|
|
|
|
blockRoot: Eth2Digest
|
|
|
|
slot: Slot
|
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
BlockRootsList* = List[Eth2Digest, MAX_REQUESTED_BLOCKS]
|
2019-11-25 14:36:25 +00:00
|
|
|
|
2020-05-11 18:08:52 +00:00
|
|
|
proc shortLog*(s: StatusMsg): auto =
|
|
|
|
(
|
|
|
|
forkDigest: s.forkDigest,
|
|
|
|
finalizedRoot: shortLog(s.finalizedRoot),
|
|
|
|
finalizedEpoch: shortLog(s.finalizedEpoch),
|
|
|
|
headRoot: shortLog(s.headRoot),
|
|
|
|
headSlot: shortLog(s.headSlot)
|
|
|
|
)
|
|
|
|
chronicles.formatIt(StatusMsg): shortLog(it)
|
|
|
|
|
2020-06-20 07:24:33 +00:00
|
|
|
func disconnectReasonName(reason: uint64): string =
|
|
|
|
# haha, nim doesn't support uint64 in `case`!
|
|
|
|
if reason == uint64(ClientShutDown): "Client shutdown"
|
|
|
|
elif reason == uint64(IrrelevantNetwork): "Irrelevant network"
|
|
|
|
elif reason == uint64(FaultOrError): "Fault or error"
|
|
|
|
else: "Disconnected (" & $reason & ")"
|
|
|
|
|
2019-11-25 14:36:25 +00:00
|
|
|
proc importBlocks(state: BeaconSyncNetworkState,
|
2019-12-16 18:08:50 +00:00
|
|
|
blocks: openarray[SignedBeaconBlock]) {.gcsafe.} =
|
2019-06-10 11:13:53 +00:00
|
|
|
for blk in blocks:
|
2019-11-25 14:36:25 +00:00
|
|
|
state.onBeaconBlock(blk)
|
2019-06-10 11:13:53 +00:00
|
|
|
info "Forward sync imported blocks", len = blocks.len
|
|
|
|
|
2020-05-11 18:08:52 +00:00
|
|
|
proc getCurrentStatus*(state: BeaconSyncNetworkState): StatusMsg {.gcsafe.} =
|
2019-09-09 23:55:01 +00:00
|
|
|
let
|
2019-11-25 14:36:25 +00:00
|
|
|
blockPool = state.blockPool
|
2019-09-09 23:55:01 +00:00
|
|
|
headBlock = blockPool.head.blck
|
|
|
|
|
2019-09-11 16:45:22 +00:00
|
|
|
StatusMsg(
|
2020-04-15 02:41:22 +00:00
|
|
|
forkDigest: state.forkDigest,
|
2020-05-11 18:08:52 +00:00
|
|
|
finalizedRoot: blockPool.headState.data.data.finalized_checkpoint.root,
|
|
|
|
finalizedEpoch: blockPool.headState.data.data.finalized_checkpoint.epoch,
|
|
|
|
headRoot: headBlock.root,
|
|
|
|
headSlot: headBlock.slot)
|
2019-09-08 22:03:41 +00:00
|
|
|
|
2020-05-13 06:37:58 +00:00
|
|
|
proc handleStatus(peer: Peer,
|
|
|
|
state: BeaconSyncNetworkState,
|
|
|
|
ourStatus: StatusMsg,
|
|
|
|
theirStatus: StatusMsg): Future[void] {.gcsafe.}
|
2019-09-08 13:08:44 +00:00
|
|
|
|
2020-05-11 18:08:52 +00:00
|
|
|
proc setStatusMsg(peer: Peer, statusMsg: StatusMsg) {.gcsafe.}
|
|
|
|
|
2018-11-29 01:08:34 +00:00
|
|
|
p2pProtocol BeaconSync(version = 1,
|
2019-09-08 22:03:41 +00:00
|
|
|
networkState = BeaconSyncNetworkState,
|
|
|
|
peerState = BeaconSyncPeerState):
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
onPeerConnected do (peer: Peer) {.async.}:
|
2020-06-11 05:14:26 +00:00
|
|
|
debug "Peer connected",
|
|
|
|
peer, peerInfo = shortLog(peer.info), wasDialed = peer.wasDialed
|
2019-09-08 22:03:41 +00:00
|
|
|
if peer.wasDialed:
|
|
|
|
let
|
2019-11-25 14:36:25 +00:00
|
|
|
ourStatus = peer.networkState.getCurrentStatus()
|
2019-11-12 22:53:19 +00:00
|
|
|
# TODO: The timeout here is so high only because we fail to
|
|
|
|
# respond in time due to high CPU load in our single thread.
|
|
|
|
theirStatus = await peer.status(ourStatus, timeout = 60.seconds)
|
2019-09-09 23:55:01 +00:00
|
|
|
|
2020-05-12 22:37:07 +00:00
|
|
|
if theirStatus.isOk:
|
2020-05-13 06:37:58 +00:00
|
|
|
await peer.handleStatus(peer.networkState,
|
|
|
|
ourStatus, theirStatus.get())
|
2019-02-18 10:34:39 +00:00
|
|
|
else:
|
2020-06-11 05:14:26 +00:00
|
|
|
warn "Status response not received in time", peer
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
proc status(peer: Peer,
|
|
|
|
theirStatus: StatusMsg,
|
|
|
|
response: SingleChunkResponse[StatusMsg])
|
|
|
|
{.async, libp2pProtocol("status", 1).} =
|
|
|
|
let ourStatus = peer.networkState.getCurrentStatus()
|
|
|
|
trace "Sending status message", peer = peer, status = ourStatus
|
|
|
|
await response.send(ourStatus)
|
|
|
|
await peer.handleStatus(peer.networkState, ourStatus, theirStatus)
|
|
|
|
|
|
|
|
proc ping(peer: Peer, value: uint64): uint64
|
|
|
|
{.libp2pProtocol("ping", 1).} =
|
|
|
|
return peer.network.metadata.seq_number
|
|
|
|
|
|
|
|
proc getMetadata(peer: Peer): Eth2Metadata
|
|
|
|
{.libp2pProtocol("metadata", 1).} =
|
|
|
|
return peer.network.metadata
|
|
|
|
|
|
|
|
proc beaconBlocksByRange(peer: Peer,
|
|
|
|
startSlot: Slot,
|
|
|
|
count: uint64,
|
|
|
|
step: uint64,
|
|
|
|
response: MultipleChunksResponse[SignedBeaconBlock])
|
|
|
|
{.async, libp2pProtocol("beacon_blocks_by_range", 1).} =
|
|
|
|
trace "got range request", peer, startSlot, count, step
|
|
|
|
|
|
|
|
if count > 0'u64:
|
|
|
|
var blocks: array[MAX_REQUESTED_BLOCKS, BlockRef]
|
|
|
|
let
|
|
|
|
pool = peer.networkState.blockPool
|
|
|
|
# Limit number of blocks in response
|
|
|
|
count = min(count.Natural, blocks.len)
|
2019-09-08 22:03:41 +00:00
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
let
|
|
|
|
endIndex = count - 1
|
|
|
|
startIndex =
|
|
|
|
pool.getBlockRange(startSlot, step, blocks.toOpenArray(0, endIndex))
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
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)
|
2019-05-22 07:13:15 +00:00
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
debug "Block range request done",
|
|
|
|
peer, startSlot, count, step, found = count - startIndex
|
2019-08-05 00:00:49 +00:00
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
proc beaconBlocksByRoot(peer: Peer,
|
|
|
|
blockRoots: BlockRootsList,
|
|
|
|
response: MultipleChunksResponse[SignedBeaconBlock])
|
|
|
|
{.async, libp2pProtocol("beacon_blocks_by_root", 1).} =
|
|
|
|
let
|
|
|
|
pool = peer.networkState.blockPool
|
|
|
|
count = blockRoots.len
|
2020-04-22 06:18:45 +00:00
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
var found = 0
|
2020-04-21 06:43:39 +00:00
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
for root in blockRoots[0..<count]:
|
|
|
|
let blockRef = pool.getRef(root)
|
|
|
|
if not isNil(blockRef):
|
|
|
|
await response.write(pool.get(blockRef).data)
|
|
|
|
inc found
|
2019-08-05 00:00:49 +00:00
|
|
|
|
2020-05-23 22:24:47 +00:00
|
|
|
debug "Block root request done",
|
|
|
|
peer, roots = blockRoots.len, count, found
|
|
|
|
|
2020-05-26 17:07:18 +00:00
|
|
|
proc goodbye(peer: Peer,
|
2020-06-20 07:24:33 +00:00
|
|
|
reason: uint64)
|
2020-05-26 17:07:18 +00:00
|
|
|
{.async, libp2pProtocol("goodbye", 1).} =
|
2020-06-20 07:24:33 +00:00
|
|
|
debug "Received Goodbye message", reason = disconnectReasonName(reason), peer
|
2019-05-22 07:13:15 +00:00
|
|
|
|
2020-05-11 18:08:52 +00:00
|
|
|
proc setStatusMsg(peer: Peer, statusMsg: StatusMsg) =
|
|
|
|
debug "Peer status", peer, statusMsg
|
|
|
|
peer.state(BeaconSync).initialStatusReceived = true
|
|
|
|
peer.state(BeaconSync).statusMsg = statusMsg
|
|
|
|
|
|
|
|
proc updateStatus*(peer: Peer): Future[bool] {.async.} =
|
|
|
|
## Request `status` of remote peer ``peer``.
|
|
|
|
let
|
|
|
|
nstate = peer.networkState(BeaconSync)
|
|
|
|
ourStatus = getCurrentStatus(nstate)
|
|
|
|
|
|
|
|
let theirFut = awaitne peer.status(ourStatus,
|
|
|
|
timeout = chronos.seconds(60))
|
|
|
|
if theirFut.failed():
|
|
|
|
result = false
|
|
|
|
else:
|
|
|
|
let theirStatus = theirFut.read()
|
2020-05-12 22:37:07 +00:00
|
|
|
if theirStatus.isOk:
|
|
|
|
peer.setStatusMsg(theirStatus.get)
|
2020-05-11 18:08:52 +00:00
|
|
|
result = true
|
|
|
|
|
|
|
|
proc hasInitialStatus*(peer: Peer): bool {.inline.} =
|
|
|
|
## Returns head slot for specific peer ``peer``.
|
|
|
|
peer.state(BeaconSync).initialStatusReceived
|
|
|
|
|
|
|
|
proc getHeadSlot*(peer: Peer): Slot {.inline.} =
|
|
|
|
## Returns head slot for specific peer ``peer``.
|
|
|
|
result = peer.state(BeaconSync).statusMsg.headSlot
|
|
|
|
|
2020-05-13 06:37:58 +00:00
|
|
|
proc handleStatus(peer: Peer,
|
|
|
|
state: BeaconSyncNetworkState,
|
|
|
|
ourStatus: StatusMsg,
|
|
|
|
theirStatus: StatusMsg) {.async, gcsafe.} =
|
2020-04-15 02:41:22 +00:00
|
|
|
if theirStatus.forkDigest != state.forkDigest:
|
2020-05-11 18:08:52 +00:00
|
|
|
notice "Irrelevant peer", peer, theirStatus, ourStatus
|
2019-09-09 23:55:01 +00:00
|
|
|
await peer.disconnect(IrrelevantNetwork)
|
2020-05-13 06:37:58 +00:00
|
|
|
else:
|
|
|
|
if not peer.state(BeaconSync).initialStatusReceived:
|
|
|
|
# Initial/handshake status message handling
|
|
|
|
peer.state(BeaconSync).initialStatusReceived = true
|
|
|
|
debug "Peer connected", peer, ourStatus = shortLog(ourStatus),
|
|
|
|
theirStatus = shortLog(theirStatus)
|
|
|
|
var res: bool
|
|
|
|
if peer.wasDialed:
|
|
|
|
res = await handleOutgoingPeer(peer)
|
|
|
|
else:
|
|
|
|
res = await handleIncomingPeer(peer)
|
|
|
|
|
|
|
|
if not res:
|
|
|
|
debug "Peer is dead or already in pool", peer
|
|
|
|
await peer.disconnect(ClientShutDown)
|
|
|
|
|
|
|
|
peer.setStatusMsg(theirStatus)
|
2019-12-02 15:38:18 +00:00
|
|
|
|
2020-04-15 02:41:22 +00:00
|
|
|
proc initBeaconSync*(network: Eth2Node,
|
|
|
|
blockPool: BlockPool,
|
|
|
|
forkDigest: ForkDigest,
|
|
|
|
onBeaconBlock: BeaconBlockCallback) =
|
|
|
|
var networkState = network.protocolState(BeaconSync)
|
|
|
|
networkState.blockPool = blockPool
|
|
|
|
networkState.forkDigest = forkDigest
|
|
|
|
networkState.onBeaconBlock = onBeaconBlock
|