2020-05-19 14:18:07 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2020-06-16 05:45:04 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2020-05-19 14:18:07 +00:00
|
|
|
import
|
2020-10-20 12:31:20 +00:00
|
|
|
std/tables,
|
2020-07-28 13:54:32 +00:00
|
|
|
chronicles,
|
2020-12-16 08:37:22 +00:00
|
|
|
stew/[assign2, results],
|
2020-10-28 13:04:21 +00:00
|
|
|
../extras, ../time,
|
2020-06-23 13:54:24 +00:00
|
|
|
../spec/[crypto, datatypes, digest, helpers, signatures, state_transition],
|
2020-07-31 14:49:06 +00:00
|
|
|
./block_pools_types, ./chain_dag, ./quarantine
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-05-21 17:08:31 +00:00
|
|
|
export results
|
|
|
|
|
2020-05-19 14:18:07 +00:00
|
|
|
# Clearance
|
|
|
|
# ---------------------------------------------
|
|
|
|
#
|
|
|
|
# This module is in charge of making the
|
|
|
|
# "quarantined" network blocks
|
2020-07-30 19:18:17 +00:00
|
|
|
# pass the firewall and be stored in the chain DAG
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-06-16 05:45:04 +00:00
|
|
|
logScope:
|
|
|
|
topics = "clearance"
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-31 14:49:06 +00:00
|
|
|
func getOrResolve*(dag: ChainDAGRef, quarantine: var QuarantineRef, root: Eth2Digest): BlockRef =
|
2020-05-19 14:18:07 +00:00
|
|
|
## Fetch a block ref, or nil if not found (will be added to list of
|
|
|
|
## blocks-to-resolve)
|
|
|
|
result = dag.getRef(root)
|
|
|
|
|
|
|
|
if result.isNil:
|
2020-11-16 19:15:43 +00:00
|
|
|
quarantine.addMissing(root)
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-09 09:29:32 +00:00
|
|
|
proc addRawBlock*(
|
2020-07-31 14:49:06 +00:00
|
|
|
dag: var ChainDAGRef, quarantine: var QuarantineRef,
|
2020-07-22 09:42:55 +00:00
|
|
|
signedBlock: SignedBeaconBlock, onBlockAdded: OnBlockAdded
|
2020-09-18 11:53:09 +00:00
|
|
|
): Result[BlockRef, (ValidationResult, BlockError)] {.gcsafe.}
|
2020-05-19 14:18:07 +00:00
|
|
|
|
|
|
|
proc addResolvedBlock(
|
2020-07-31 14:49:06 +00:00
|
|
|
dag: var ChainDAGRef, quarantine: var QuarantineRef,
|
2020-08-18 20:29:33 +00:00
|
|
|
state: var StateData, signedBlock: SignedBeaconBlock,
|
2020-08-05 06:28:43 +00:00
|
|
|
parent: BlockRef, cache: var StateCache,
|
2020-07-22 09:42:55 +00:00
|
|
|
onBlockAdded: OnBlockAdded
|
2020-08-18 20:29:33 +00:00
|
|
|
) =
|
2020-07-30 15:48:25 +00:00
|
|
|
# TODO move quarantine processing out of here
|
2020-08-18 20:29:33 +00:00
|
|
|
doAssert state.data.data.slot == signedBlock.message.slot,
|
|
|
|
"state must match block"
|
|
|
|
doAssert state.blck.root == signedBlock.message.parent_root,
|
|
|
|
"the StateData passed into the addResolved function not yet updated!"
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
let
|
|
|
|
blockRoot = signedBlock.root
|
|
|
|
blockRef = BlockRef.init(blockRoot, signedBlock.message)
|
2020-07-30 15:48:25 +00:00
|
|
|
blockEpoch = blockRef.slot.compute_epoch_at_slot()
|
2020-08-11 19:39:53 +00:00
|
|
|
|
|
|
|
link(parent, blockRef)
|
|
|
|
|
2020-08-18 20:29:33 +00:00
|
|
|
var epochRef = blockRef.findEpochRef(blockEpoch)
|
|
|
|
if epochRef == nil:
|
|
|
|
let prevEpochRef = blockRef.findEpochRef(blockEpoch - 1)
|
|
|
|
|
|
|
|
epochRef = EpochRef.init(state.data.data, cache, prevEpochRef)
|
2020-08-26 15:06:40 +00:00
|
|
|
let ancestor = blockRef.epochAncestor(blockEpoch)
|
|
|
|
epochRef.updateKeyStores(ancestor.blck.parent, dag.finalizedHead.blck)
|
|
|
|
|
|
|
|
ancestor.blck.epochRefs.add epochRef
|
2020-07-28 13:54:32 +00:00
|
|
|
|
2020-05-19 14:18:07 +00:00
|
|
|
dag.blocks[blockRoot] = blockRef
|
|
|
|
trace "Populating block dag", key = blockRoot, val = blockRef
|
|
|
|
|
|
|
|
# Resolved blocks should be stored in database
|
2020-07-16 13:16:51 +00:00
|
|
|
dag.putBlock(signedBlock)
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-28 13:54:32 +00:00
|
|
|
var foundHead: BlockRef
|
2020-05-19 14:18:07 +00:00
|
|
|
for head in dag.heads.mitems():
|
2020-07-28 13:54:32 +00:00
|
|
|
if head.isAncestorOf(blockRef):
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-28 13:54:32 +00:00
|
|
|
head = blockRef
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-28 13:54:32 +00:00
|
|
|
foundHead = head
|
2020-05-19 14:18:07 +00:00
|
|
|
break
|
|
|
|
|
2020-07-28 13:54:32 +00:00
|
|
|
if foundHead.isNil:
|
|
|
|
foundHead = blockRef
|
|
|
|
dag.heads.add(foundHead)
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-10-01 18:56:42 +00:00
|
|
|
debug "Block resolved",
|
2020-05-19 14:18:07 +00:00
|
|
|
blck = shortLog(signedBlock.message),
|
|
|
|
blockRoot = shortLog(blockRoot),
|
2020-07-16 13:16:51 +00:00
|
|
|
heads = dag.heads.len()
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-08-18 20:29:33 +00:00
|
|
|
state.blck = blockRef
|
|
|
|
|
2020-07-30 15:48:25 +00:00
|
|
|
# Notify others of the new block before processing the quarantine, such that
|
|
|
|
# notifications for parents happens before those of the children
|
2020-07-22 09:42:55 +00:00
|
|
|
if onBlockAdded != nil:
|
2020-08-18 20:29:33 +00:00
|
|
|
onBlockAdded(blockRef, signedBlock, epochRef, state.data)
|
2020-07-09 09:29:32 +00:00
|
|
|
|
2020-05-19 14:18:07 +00:00
|
|
|
# Now that we have the new block, we should see if any of the previously
|
|
|
|
# unresolved blocks magically become resolved
|
|
|
|
# TODO This code is convoluted because when there are more than ~1.5k
|
|
|
|
# blocks being synced, there's a stack overflow as `add` gets called
|
|
|
|
# for the whole chain of blocks. Instead we use this ugly field in `dag`
|
|
|
|
# which could be avoided by refactoring the code
|
2020-07-09 09:29:32 +00:00
|
|
|
# TODO unit test the logic, in particular interaction with fork choice block parents
|
2020-05-19 14:18:07 +00:00
|
|
|
if not quarantine.inAdd:
|
|
|
|
quarantine.inAdd = true
|
|
|
|
defer: quarantine.inAdd = false
|
2020-07-28 13:54:32 +00:00
|
|
|
var entries = 0
|
|
|
|
while entries != quarantine.orphans.len:
|
|
|
|
entries = quarantine.orphans.len # keep going while quarantine is shrinking
|
|
|
|
var resolved: seq[SignedBeaconBlock]
|
|
|
|
for _, v in quarantine.orphans:
|
|
|
|
if v.message.parent_root in dag.blocks: resolved.add(v)
|
|
|
|
|
|
|
|
for v in resolved:
|
2020-07-22 09:42:55 +00:00
|
|
|
discard addRawBlock(dag, quarantine, v, onBlockAdded)
|
2020-07-09 09:29:32 +00:00
|
|
|
|
|
|
|
proc addRawBlock*(
|
2020-07-31 14:49:06 +00:00
|
|
|
dag: var ChainDAGRef, quarantine: var QuarantineRef,
|
2020-07-09 09:29:32 +00:00
|
|
|
signedBlock: SignedBeaconBlock,
|
2020-07-22 09:42:55 +00:00
|
|
|
onBlockAdded: OnBlockAdded
|
2020-09-18 11:53:09 +00:00
|
|
|
): Result[BlockRef, (ValidationResult, BlockError)] =
|
2020-07-30 15:48:25 +00:00
|
|
|
## Try adding a block to the chain, verifying first that it passes the state
|
|
|
|
## transition function.
|
2020-06-10 06:58:12 +00:00
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
logScope:
|
|
|
|
blck = shortLog(signedBlock.message)
|
|
|
|
blockRoot = shortLog(signedBlock.root)
|
2020-06-10 06:58:12 +00:00
|
|
|
|
2020-07-16 13:16:51 +00:00
|
|
|
template blck(): untyped = signedBlock.message # shortcuts without copy
|
|
|
|
template blockRoot(): untyped = signedBlock.root
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-09 09:29:32 +00:00
|
|
|
if blockRoot in dag.blocks:
|
2020-07-16 13:16:51 +00:00
|
|
|
debug "Block already exists"
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-30 15:48:25 +00:00
|
|
|
# We should not call the block added callback for blocks that already
|
|
|
|
# existed in the pool, as that may confuse consumers such as the fork
|
2020-09-18 11:53:09 +00:00
|
|
|
# choice. While the validation result won't be accessed, it's IGNORE,
|
|
|
|
# according to the spec.
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, Duplicate))
|
2020-05-19 14:18:07 +00:00
|
|
|
|
|
|
|
quarantine.missing.del(blockRoot)
|
|
|
|
|
|
|
|
# If the block we get is older than what we finalized already, we drop it.
|
|
|
|
# One way this can happen is that we start resolving a block and finalization
|
|
|
|
# happens in the meantime - the block we requested will then be stale
|
|
|
|
# by the time it gets here.
|
|
|
|
if blck.slot <= dag.finalizedHead.slot:
|
|
|
|
debug "Old block, dropping",
|
|
|
|
finalizedHead = shortLog(dag.finalizedHead),
|
2020-07-16 13:16:51 +00:00
|
|
|
tail = shortLog(dag.tail)
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-09-18 11:53:09 +00:00
|
|
|
# Doesn't correspond to any specific validation condition, and still won't
|
|
|
|
# be used, but certainly would be IGNORE.
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, Unviable))
|
2020-05-19 14:18:07 +00:00
|
|
|
|
|
|
|
let parent = dag.blocks.getOrDefault(blck.parent_root)
|
|
|
|
|
|
|
|
if parent != nil:
|
|
|
|
if parent.slot >= blck.slot:
|
2020-07-01 15:21:21 +00:00
|
|
|
# A block whose parent is newer than the block itself is clearly invalid -
|
|
|
|
# discard it immediately
|
2020-10-01 18:56:42 +00:00
|
|
|
debug "Invalid block slot",
|
2020-05-19 14:18:07 +00:00
|
|
|
parentBlock = shortLog(parent)
|
|
|
|
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Reject, Invalid))
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-30 15:48:25 +00:00
|
|
|
if (parent.slot < dag.finalizedHead.slot) or
|
|
|
|
(parent.slot == dag.finalizedHead.slot and
|
|
|
|
parent != dag.finalizedHead.blck):
|
2020-07-01 15:21:21 +00:00
|
|
|
# We finalized a block that's newer than the parent of this block - this
|
|
|
|
# block, although recent, is thus building on a history we're no longer
|
|
|
|
# interested in pursuing. This can happen if a client produces a block
|
|
|
|
# while syncing - ie it's own head block will be old, but it'll create
|
|
|
|
# a block according to the wall clock, in its own little world - this is
|
|
|
|
# correct - from their point of view, the head block they have is the
|
|
|
|
# latest thing that happened on the chain and they're performing their
|
|
|
|
# duty correctly.
|
|
|
|
debug "Unviable block, dropping",
|
|
|
|
finalizedHead = shortLog(dag.finalizedHead),
|
2020-07-16 13:16:51 +00:00
|
|
|
tail = shortLog(dag.tail)
|
2020-07-01 15:21:21 +00:00
|
|
|
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, Unviable))
|
2020-07-01 15:21:21 +00:00
|
|
|
|
2020-06-18 10:03:36 +00:00
|
|
|
# The block might have been in either of `orphans` or `missing` - we don't
|
|
|
|
# want any more work done on its behalf
|
2020-09-04 06:39:46 +00:00
|
|
|
quarantine.removeOrphan(signedBlock)
|
2020-05-19 14:18:07 +00:00
|
|
|
|
|
|
|
# The block is resolved, now it's time to validate it to ensure that the
|
|
|
|
# blocks we add to the database are clean for the given state
|
|
|
|
|
|
|
|
# TODO if the block is from the future, we should not be resolving it (yet),
|
|
|
|
# but maybe we should use it as a hint that our clock is wrong?
|
2020-08-18 20:29:33 +00:00
|
|
|
var cache = getStateCache(parent, blck.slot.epoch)
|
2020-05-19 14:18:07 +00:00
|
|
|
updateStateData(
|
2020-10-18 15:47:39 +00:00
|
|
|
dag, dag.clearanceState, parent.atSlot(blck.slot), true, cache)
|
2020-05-19 14:18:07 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
poolPtr = unsafeAddr dag # safe because restore is short-lived
|
|
|
|
func restore(v: var HashedBeaconState) =
|
|
|
|
# TODO address this ugly workaround - there should probably be a
|
|
|
|
# `state_transition` that takes a `StateData` instead and updates
|
|
|
|
# the block as well
|
2020-07-22 06:25:13 +00:00
|
|
|
doAssert v.addr == addr poolPtr.clearanceState.data
|
|
|
|
assign(poolPtr.clearanceState, poolPtr.headState)
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-22 06:25:13 +00:00
|
|
|
if not state_transition(dag.runtimePreset, dag.clearanceState.data, signedBlock,
|
2020-08-18 20:29:33 +00:00
|
|
|
cache, dag.updateFlags + {slotProcessed}, restore):
|
2020-10-01 18:56:42 +00:00
|
|
|
info "Invalid block"
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Reject, Invalid))
|
2020-07-22 09:42:55 +00:00
|
|
|
|
|
|
|
# Careful, clearanceState.data has been updated but not blck - we need to
|
|
|
|
# create the BlockRef first!
|
2020-08-18 20:29:33 +00:00
|
|
|
addResolvedBlock(
|
|
|
|
dag, quarantine, dag.clearanceState, signedBlock, parent, cache,
|
|
|
|
onBlockAdded)
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-07-22 06:25:13 +00:00
|
|
|
return ok dag.clearanceState.blck
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-11-16 19:15:43 +00:00
|
|
|
# This is an unresolved block - add it to the quarantine, which will cause its
|
|
|
|
# parent to be scheduled for downloading
|
2020-08-31 09:00:38 +00:00
|
|
|
if not quarantine.add(dag, signedBlock):
|
|
|
|
debug "Block quarantine full"
|
2020-05-19 14:18:07 +00:00
|
|
|
|
|
|
|
if blck.parent_root in quarantine.missing or
|
2020-09-04 06:39:46 +00:00
|
|
|
containsOrphan(quarantine, signedBlock):
|
2020-08-04 19:28:40 +00:00
|
|
|
debug "Unresolved block (parent missing or orphaned)",
|
|
|
|
orphans = quarantine.orphans.len,
|
|
|
|
missing = quarantine.missing.len
|
|
|
|
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, MissingParent))
|
2020-05-19 14:18:07 +00:00
|
|
|
|
|
|
|
# TODO if we receive spam blocks, one heurestic to implement might be to wait
|
|
|
|
# for a couple of attestations to appear before fetching parents - this
|
|
|
|
# would help prevent using up network resources for spam - this serves
|
|
|
|
# two purposes: one is that attestations are likely to appear for the
|
|
|
|
# block only if it's valid / not spam - the other is that malicious
|
|
|
|
# validators that are not proposers can sign invalid blocks and send
|
|
|
|
# them out without penalty - but signing invalid attestations carries
|
|
|
|
# a risk of being slashed, making attestations a more valuable spam
|
|
|
|
# filter.
|
|
|
|
debug "Unresolved block (parent missing)",
|
2020-06-18 10:03:36 +00:00
|
|
|
orphans = quarantine.orphans.len,
|
2020-07-16 13:16:51 +00:00
|
|
|
missing = quarantine.missing.len
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, MissingParent))
|
2020-05-21 17:08:31 +00:00
|
|
|
|
2020-11-09 14:18:55 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.0/specs/phase0/p2p-interface.md#beacon_block
|
2020-05-19 14:18:07 +00:00
|
|
|
proc isValidBeaconBlock*(
|
2020-07-31 14:49:06 +00:00
|
|
|
dag: ChainDAGRef, quarantine: var QuarantineRef,
|
2020-10-28 13:04:21 +00:00
|
|
|
signed_beacon_block: SignedBeaconBlock, wallTime: BeaconTime,
|
2020-09-18 11:53:09 +00:00
|
|
|
flags: UpdateFlags):
|
|
|
|
Result[void, (ValidationResult, BlockError)] =
|
2020-06-16 05:45:04 +00:00
|
|
|
logScope:
|
|
|
|
topics = "clearance valid_blck"
|
|
|
|
received_block = shortLog(signed_beacon_block.message)
|
2020-07-16 13:16:51 +00:00
|
|
|
blockRoot = shortLog(signed_beacon_block.root)
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2020-05-19 14:18:07 +00:00
|
|
|
# In general, checks are ordered from cheap to expensive. Especially, crypto
|
|
|
|
# verification could be quite a bit more expensive than the rest. This is an
|
|
|
|
# externally easy-to-invoke function by tossing network packets at the node.
|
|
|
|
|
2020-10-28 13:04:21 +00:00
|
|
|
# [IGNORE] The block is not from a future slot (with a
|
|
|
|
# MAXIMUM_GOSSIP_CLOCK_DISPARITY allowance) -- i.e. validate that
|
|
|
|
# signed_beacon_block.message.slot <= current_slot (a client MAY queue future
|
|
|
|
# blocks for processing at the appropriate slot).
|
|
|
|
if not (signed_beacon_block.message.slot <=
|
|
|
|
(wallTime + MAXIMUM_GOSSIP_CLOCK_DISPARITY).slotOrZero):
|
2020-06-16 05:45:04 +00:00
|
|
|
debug "block is from a future slot",
|
2020-10-28 13:04:21 +00:00
|
|
|
wallSlot = wallTime.toSlot()
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, Invalid))
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-08-03 19:47:42 +00:00
|
|
|
# [IGNORE] The block is from a slot greater than the latest finalized slot --
|
|
|
|
# i.e. validate that signed_beacon_block.message.slot >
|
2020-05-19 14:18:07 +00:00
|
|
|
# compute_start_slot_at_epoch(state.finalized_checkpoint.epoch)
|
|
|
|
if not (signed_beacon_block.message.slot > dag.finalizedHead.slot):
|
2020-06-16 05:45:04 +00:00
|
|
|
debug "block is not from a slot greater than the latest finalized slot"
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, Invalid))
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-08-03 19:47:42 +00:00
|
|
|
# [IGNORE] The block is the first block with valid signature received for the
|
|
|
|
# proposer for the slot, signed_beacon_block.message.slot.
|
2020-05-19 14:18:07 +00:00
|
|
|
#
|
|
|
|
# While this condition is similar to the proposer slashing condition at
|
2020-11-09 14:18:55 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.0/specs/phase0/validator.md#proposer-slashing
|
2020-05-19 14:18:07 +00:00
|
|
|
# it's not identical, and this check does not address slashing:
|
|
|
|
#
|
|
|
|
# (1) The beacon blocks must be conflicting, i.e. different, for the same
|
|
|
|
# slot and proposer. This check also catches identical blocks.
|
|
|
|
#
|
|
|
|
# (2) By this point in the function, it's not been checked whether they're
|
|
|
|
# signed yet. As in general, expensive checks should be deferred, this
|
|
|
|
# would add complexity not directly relevant this function.
|
|
|
|
#
|
|
|
|
# (3) As evidenced by point (1), the similarity in the validation condition
|
|
|
|
# and slashing condition, while not coincidental, aren't similar enough
|
|
|
|
# to combine, as one or the other might drift.
|
|
|
|
#
|
|
|
|
# (4) Furthermore, this function, as much as possible, simply returns a yes
|
|
|
|
# or no answer, without modifying other state for p2p network interface
|
|
|
|
# validation. Complicating this interface, for the sake of sharing only
|
|
|
|
# couple lines of code, wouldn't be worthwhile.
|
|
|
|
#
|
|
|
|
# TODO might check unresolved/orphaned blocks too, and this might not see all
|
|
|
|
# blocks at a given slot (though, in theory, those get checked elsewhere), or
|
|
|
|
# adding metrics that count how often these conditions occur.
|
|
|
|
let
|
|
|
|
slotBlockRef = getBlockBySlot(dag, signed_beacon_block.message.slot)
|
|
|
|
|
|
|
|
if not slotBlockRef.isNil:
|
|
|
|
let blck = dag.get(slotBlockRef).data
|
|
|
|
if blck.message.proposer_index ==
|
|
|
|
signed_beacon_block.message.proposer_index and
|
|
|
|
blck.message.slot == signed_beacon_block.message.slot and
|
|
|
|
blck.signature.toRaw() != signed_beacon_block.signature.toRaw():
|
2020-10-01 18:56:42 +00:00
|
|
|
notice "block isn't first block with valid signature received for the proposer",
|
2020-05-19 14:18:07 +00:00
|
|
|
blckRef = slotBlockRef,
|
2020-06-16 05:45:04 +00:00
|
|
|
existing_block = shortLog(blck.message)
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, Invalid))
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-08-03 19:47:42 +00:00
|
|
|
# [IGNORE] The block's parent (defined by block.parent_root) has been seen
|
|
|
|
# (via both gossip and non-gossip sources) (a client MAY queue blocks for
|
|
|
|
# processing once the parent block is retrieved).
|
|
|
|
#
|
|
|
|
# And implicitly:
|
|
|
|
# [REJECT] The block's parent (defined by block.parent_root) passes validation.
|
2020-05-19 14:18:07 +00:00
|
|
|
let parent_ref = dag.getRef(signed_beacon_block.message.parent_root)
|
|
|
|
if parent_ref.isNil:
|
2020-07-31 14:49:06 +00:00
|
|
|
# Pending dag gets checked via `ChainDAGRef.add(...)` later, and relevant
|
2020-05-19 14:18:07 +00:00
|
|
|
# checks are performed there. In usual paths beacon_node adds blocks via
|
2020-09-24 15:16:00 +00:00
|
|
|
# ChainDAGRef.add(...) directly, with no additional validity checks.
|
2020-08-31 09:00:38 +00:00
|
|
|
debug "parent unknown, putting block in quarantine",
|
2020-10-28 13:04:21 +00:00
|
|
|
current_slot = wallTime.toSlot()
|
2020-08-31 09:00:38 +00:00
|
|
|
if not quarantine.add(dag, signed_beacon_block):
|
2020-11-16 19:15:43 +00:00
|
|
|
debug "Block quarantine full"
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, MissingParent))
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-08-03 19:47:42 +00:00
|
|
|
# [REJECT] The current finalized_checkpoint is an ancestor of block -- i.e.
|
|
|
|
# get_ancestor(store, block.parent_root,
|
|
|
|
# compute_start_slot_at_epoch(store.finalized_checkpoint.epoch)) ==
|
|
|
|
# store.finalized_checkpoint.root
|
|
|
|
let
|
|
|
|
finalized_checkpoint = dag.headState.data.data.finalized_checkpoint
|
|
|
|
ancestor = get_ancestor(
|
|
|
|
parent_ref, compute_start_slot_at_epoch(finalized_checkpoint.epoch))
|
|
|
|
|
|
|
|
if ancestor.isNil:
|
|
|
|
debug "couldn't find ancestor block"
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, Invalid)) # might not've received block
|
2020-08-03 19:47:42 +00:00
|
|
|
|
|
|
|
if not (finalized_checkpoint.root in [ancestor.root, Eth2Digest()]):
|
|
|
|
debug "block not descendent of finalized block"
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Reject, Invalid))
|
2020-08-03 19:47:42 +00:00
|
|
|
|
|
|
|
# [REJECT] The block is proposed by the expected proposer_index for the
|
|
|
|
# block's slot in the context of the current shuffling (defined by
|
|
|
|
# parent_root/slot). If the proposer_index cannot immediately be verified
|
|
|
|
# against the expected shuffling, the block MAY be queued for later
|
|
|
|
# processing while proposers for the block's branch are calculated -- in such
|
|
|
|
# a case do not REJECT, instead IGNORE this message.
|
2020-06-16 05:45:04 +00:00
|
|
|
let
|
|
|
|
proposer = getProposer(dag, parent_ref, signed_beacon_block.message.slot)
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-06-16 05:45:04 +00:00
|
|
|
if proposer.isNone:
|
2020-10-01 18:56:42 +00:00
|
|
|
warn "cannot compute proposer for message"
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Ignore, Invalid)) # internal issue
|
2020-06-16 05:45:04 +00:00
|
|
|
|
|
|
|
if proposer.get()[0] !=
|
|
|
|
ValidatorIndex(signed_beacon_block.message.proposer_index):
|
2020-10-01 18:56:42 +00:00
|
|
|
notice "block had unexpected proposer",
|
2020-06-16 05:45:04 +00:00
|
|
|
expected_proposer = proposer.get()[0]
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Reject, Invalid))
|
2020-06-16 05:45:04 +00:00
|
|
|
|
2020-08-03 19:47:42 +00:00
|
|
|
# [REJECT] The proposer signature, signed_beacon_block.signature, is valid
|
|
|
|
# with respect to the proposer_index pubkey.
|
2020-06-16 05:45:04 +00:00
|
|
|
if not verify_block_signature(
|
|
|
|
dag.headState.data.data.fork,
|
|
|
|
dag.headState.data.data.genesis_validators_root,
|
|
|
|
signed_beacon_block.message.slot,
|
|
|
|
signed_beacon_block.message,
|
|
|
|
proposer.get()[1],
|
|
|
|
signed_beacon_block.signature):
|
|
|
|
debug "block failed signature verification",
|
|
|
|
signature = shortLog(signed_beacon_block.signature)
|
|
|
|
|
2020-10-20 12:31:20 +00:00
|
|
|
return err((ValidationResult.Reject, Invalid))
|
2020-05-19 14:18:07 +00:00
|
|
|
|
2020-06-16 08:49:32 +00:00
|
|
|
ok()
|