2018-11-23 23:58:49 +00:00
|
|
|
import
|
2019-02-18 10:34:39 +00:00
|
|
|
options, tables,
|
2019-03-05 22:54:08 +00:00
|
|
|
chronicles, chronos, ranges/bitranges,
|
2019-03-13 21:23:01 +00:00
|
|
|
spec/[datatypes, crypto, digest], eth/rlp,
|
2019-03-12 15:03:14 +00:00
|
|
|
beacon_node_types, eth2_network, beacon_chain_db, block_pool, time, ssz
|
|
|
|
|
|
|
|
from beacon_node import onBeaconBlock
|
|
|
|
# Careful handling of beacon_node <-> sync_protocol
|
|
|
|
# to avoid recursive dependencies
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
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
|
|
|
|
|
2019-01-17 18:27:11 +00:00
|
|
|
ValidatorSet = seq[Validator]
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-02-18 10:34:39 +00:00
|
|
|
BeaconSyncState* = ref object
|
|
|
|
node*: BeaconNode
|
|
|
|
db*: BeaconChainDB
|
|
|
|
|
|
|
|
func toHeader(b: BeaconBlock): BeaconBlockHeader =
|
2019-03-01 20:09:20 +00:00
|
|
|
BeaconBlockHeader(
|
2019-03-12 22:21:32 +00:00
|
|
|
slot: b.slot.uint64,
|
2019-03-01 20:09:20 +00:00
|
|
|
parent_root: b.parent_root,
|
|
|
|
state_root: b.state_root,
|
|
|
|
randao_reveal: b.randao_reveal,
|
|
|
|
eth1_data : b.eth1_data,
|
|
|
|
signature: b.signature,
|
|
|
|
body: hash_tree_root_final(b.body)
|
|
|
|
)
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2019-03-05 13:55:09 +00:00
|
|
|
proc fromHeaderAndBody(b: var BeaconBlock, h: BeaconBlockHeader, body: BeaconBlockBody) =
|
|
|
|
assert(hash_tree_root_final(body) == h.body)
|
2019-03-12 22:21:32 +00:00
|
|
|
b.slot = h.slot.Slot
|
2019-02-18 10:34:39 +00:00
|
|
|
b.parent_root = h.parent_root
|
|
|
|
b.state_root = h.state_root
|
|
|
|
b.randao_reveal = h.randao_reveal
|
|
|
|
b.eth1_data = h.eth1_data
|
|
|
|
b.signature = h.signature
|
2019-03-05 13:55:09 +00:00
|
|
|
b.body = body
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2019-03-13 21:23:01 +00:00
|
|
|
proc importBlocks(node: BeaconNode,
|
|
|
|
roots: openarray[(Eth2Digest, Slot)],
|
|
|
|
headers: openarray[BeaconBlockHeader],
|
|
|
|
bodies: openarray[BeaconBlockBody]) =
|
2019-02-18 10:34:39 +00:00
|
|
|
var bodyMap = initTable[Eth2Digest, int]()
|
|
|
|
|
|
|
|
for i, b in bodies:
|
|
|
|
bodyMap[hash_tree_root_final(b)] = i
|
|
|
|
|
|
|
|
var goodBlocks, badBlocks = 0
|
|
|
|
for h in headers:
|
|
|
|
let iBody = bodyMap.getOrDefault(h.body, -1)
|
|
|
|
if iBody >= 0:
|
|
|
|
var blk: BeaconBlock
|
2019-03-05 13:55:09 +00:00
|
|
|
blk.fromHeaderAndBody(h, bodies[iBody])
|
2019-02-18 10:34:39 +00:00
|
|
|
node.onBeaconBlock(blk)
|
|
|
|
inc goodBlocks
|
|
|
|
else:
|
|
|
|
inc badBlocks
|
|
|
|
|
|
|
|
info "Forward sync imported blocks", goodBlocks, badBlocks, headers = headers.len, bodies = bodies.len, roots = roots.len
|
|
|
|
|
2018-11-29 01:08:34 +00:00
|
|
|
p2pProtocol BeaconSync(version = 1,
|
2019-02-18 10:34:39 +00:00
|
|
|
shortName = "bcs",
|
|
|
|
networkState = BeaconSyncState):
|
|
|
|
|
|
|
|
onPeerConnected do(peer: Peer):
|
|
|
|
const
|
|
|
|
protocolVersion = 1 # TODO: Spec doesn't specify this yet
|
|
|
|
networkId = 1
|
|
|
|
let node = peer.networkState.node
|
|
|
|
|
|
|
|
var
|
|
|
|
latestFinalizedRoot: Eth2Digest # TODO
|
2019-03-13 21:23:01 +00:00
|
|
|
latestFinalizedEpoch = node.state.data.finalized_epoch
|
2019-02-18 10:34:39 +00:00
|
|
|
bestRoot: Eth2Digest # TODO
|
2019-03-13 21:23:01 +00:00
|
|
|
bestSlot = node.state.data.slot
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
let m = await handshake(peer, timeout = 500,
|
|
|
|
status(networkId, latestFinalizedRoot,
|
|
|
|
latestFinalizedEpoch, bestRoot, bestSlot))
|
2019-02-18 10:34:39 +00:00
|
|
|
let bestDiff = cmp((latestFinalizedEpoch, bestSlot), (m.latestFinalizedEpoch, m.bestSlot))
|
|
|
|
if bestDiff == 0:
|
|
|
|
# Nothing to do?
|
2019-03-11 15:38:36 +00:00
|
|
|
trace "Nothing to sync", peer = peer.remote
|
2019-02-18 10:34:39 +00:00
|
|
|
else:
|
|
|
|
# TODO: Check for WEAK_SUBJECTIVITY_PERIOD difference and terminate the
|
|
|
|
# connection if it's too big.
|
2019-03-11 15:38:36 +00:00
|
|
|
let blockPool = peer.networkState.node.blockPool
|
2019-02-18 10:34:39 +00:00
|
|
|
|
|
|
|
if bestDiff > 0:
|
|
|
|
# Send roots
|
|
|
|
# TODO: Currently we send all block roots in one "packet". Maybe
|
|
|
|
# they should be split to multiple packets.
|
2019-03-13 21:23:01 +00:00
|
|
|
type Root = (Eth2Digest, Slot)
|
2019-02-18 10:34:39 +00:00
|
|
|
var roots = newSeqOfCap[Root](128)
|
2019-03-13 21:23:01 +00:00
|
|
|
for i in int(m.bestSlot) .. int(bestSlot):
|
|
|
|
for r in blockPool.blockRootsForSlot(i.Slot):
|
|
|
|
roots.add((r, i.Slot))
|
2019-02-18 10:34:39 +00:00
|
|
|
|
|
|
|
await peer.beaconBlockRoots(roots)
|
|
|
|
else:
|
|
|
|
# Receive roots
|
|
|
|
let roots = await peer.nextMsg(BeaconSync.beaconBlockRoots)
|
|
|
|
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 bodies = await peer.getBeaconBlockBodies(bodiesRequest)
|
|
|
|
node.importBlocks(roots.roots, headers.get.blockHeaders, bodies.get.blockBodies)
|
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
proc status(
|
|
|
|
peer: Peer,
|
|
|
|
networkId: int,
|
|
|
|
latestFinalizedRoot: Eth2Digest,
|
2019-03-13 21:23:01 +00:00
|
|
|
latestFinalizedEpoch: Epoch,
|
2019-03-05 22:54:08 +00:00
|
|
|
bestRoot: Eth2Digest,
|
2019-03-13 21:23:01 +00:00
|
|
|
bestSlot: Slot) {.libp2pProtocol("hello", "1.0.0").}
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
proc beaconBlockRoots(
|
|
|
|
peer: Peer,
|
2019-03-13 21:23:01 +00:00
|
|
|
roots: openarray[(Eth2Digest, Slot)]) {.libp2pProtocol("rpc/beacon_block_roots", "1.0.0").}
|
2019-02-18 10:34:39 +00:00
|
|
|
|
|
|
|
requestResponse:
|
2019-03-05 22:54:08 +00:00
|
|
|
proc getBeaconBlockHeaders(
|
|
|
|
peer: Peer,
|
|
|
|
blockRoot: Eth2Digest,
|
2019-03-13 21:23:01 +00:00
|
|
|
slot: Slot,
|
2019-03-05 22:54:08 +00:00
|
|
|
maxHeaders: int,
|
|
|
|
skipSlots: int) {.libp2pProtocol("rpc/beacon_block_headers", "1.0.0").} =
|
2019-03-05 13:55:09 +00:00
|
|
|
# TODO: validate maxHeaders and implement slipSlots
|
2019-03-13 21:23:01 +00:00
|
|
|
var s = slot.int
|
2019-02-18 10:34:39 +00:00
|
|
|
var headers = newSeqOfCap[BeaconBlockHeader](maxHeaders)
|
|
|
|
let db = peer.networkState.db
|
2019-03-11 15:38:36 +00:00
|
|
|
let blockPool = peer.networkState.node.blockPool
|
2019-02-18 10:34:39 +00:00
|
|
|
while headers.len < maxHeaders:
|
2019-03-13 21:23:01 +00:00
|
|
|
for r in blockPool.blockRootsForSlot(s.Slot):
|
2019-02-18 10:34:39 +00:00
|
|
|
headers.add(db.getBlock(r).get().toHeader)
|
|
|
|
if headers.len == maxHeaders: break
|
|
|
|
inc s
|
2019-03-05 22:54:08 +00:00
|
|
|
await response.send(headers)
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
proc beaconBlockHeaders(
|
|
|
|
peer: Peer,
|
|
|
|
blockHeaders: openarray[BeaconBlockHeader])
|
2019-02-18 10:34:39 +00:00
|
|
|
|
|
|
|
requestResponse:
|
2019-03-05 22:54:08 +00:00
|
|
|
proc getBeaconBlockBodies(
|
|
|
|
peer: Peer,
|
|
|
|
blockRoots: openarray[Eth2Digest]) {.libp2pProtocol("rpc/beacon_block_bodies", "1.0.0").} =
|
2019-02-18 10:34:39 +00:00
|
|
|
# TODO: Validate blockRoots.len
|
|
|
|
var bodies = newSeqOfCap[BeaconBlockBody](blockRoots.len)
|
|
|
|
let db = peer.networkState.db
|
|
|
|
for r in blockRoots:
|
|
|
|
if (let blk = db.getBlock(r); blk.isSome):
|
|
|
|
bodies.add(blk.get().body)
|
2019-03-05 22:54:08 +00:00
|
|
|
await response.send(bodies)
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
proc beaconBlockBodies(
|
|
|
|
peer: Peer,
|
|
|
|
blockBodies: openarray[BeaconBlockBody])
|
2018-11-23 23:58:49 +00:00
|
|
|
|