allow beacon_node to verify finalization when appropriate, and enable by default for make eth2_network_simulation

This commit is contained in:
Dustin Brody 2020-02-17 19:24:14 +01:00 committed by tersec
parent 11bd8e6e80
commit e13846bec6
3 changed files with 41 additions and 1 deletions

View File

@ -645,6 +645,21 @@ proc handleProposal(node: BeaconNode, head: BlockRef, slot: Slot):
return head
proc verifyFinalization(node: BeaconNode, slot: Slot) =
# Epoch must be >= 4 to check finalization
const SETTLING_TIME_OFFSET = 1'u64
let epoch = slot.compute_epoch_at_slot()
# Don't static-assert this -- if this isn't called, don't require it
doAssert SLOTS_PER_EPOCH > SETTLING_TIME_OFFSET
# Intentionally, loudly assert. Point is to fail visibly and unignorably
# during testing.
if epoch >= 4 and slot mod SLOTS_PER_EPOCH > SETTLING_TIME_OFFSET:
let finalizedEpoch =
node.blockPool.finalizedHead.blck.slot.compute_epoch_at_slot()
doAssert finalizedEpoch + 2 == epoch
proc onSlotStart(node: BeaconNode, lastSlot, scheduledSlot: Slot) {.gcsafe, async.} =
## Called at the beginning of a slot - usually every slot, but sometimes might
## skip a few in case we're running late.
@ -672,6 +687,17 @@ proc onSlotStart(node: BeaconNode, lastSlot, scheduledSlot: Slot) {.gcsafe, asyn
finalizedSlot = shortLog(node.blockPool.finalizedHead.blck.slot.compute_epoch_at_slot()),
cat = "scheduling"
# Check before any re-scheduling of onSlotStart()
if node.config.checkEpochs > 0'u64 and
scheduledSlot.compute_epoch_at_slot() >= node.config.checkEpochs:
info "Stopping at pre-chosen epoch",
chosenEpoch = node.config.checkEpochs,
epoch = scheduledSlot.compute_epoch_at_slot(),
slot = scheduledSlot
# Brute-force, but ensure it's reliably enough to run in CI.
quit(0)
if not wallSlot.afterGenesis or (wallSlot.slot < lastSlot):
let
slot =
@ -700,6 +726,9 @@ proc onSlotStart(node: BeaconNode, lastSlot, scheduledSlot: Slot) {.gcsafe, asyn
beacon_slot.set slot.int64
if node.config.verifyFinalization:
verifyFinalization(node, scheduledSlot)
if slot > lastSlot + SLOTS_PER_EPOCH:
# We've fallen behind more than an epoch - there's nothing clever we can
# do here really, except skip all the work and try again later.
@ -784,7 +813,7 @@ proc onSlotStart(node: BeaconNode, lastSlot, scheduledSlot: Slot) {.gcsafe, asyn
head = await handleProposal(node, head, slot)
# We've been doing lots of work up until now which took time. Normally, we
# send out attestations at the slot mid-point, so we go back to the clock
# send out attestations at the slot thirds-point, so we go back to the clock
# to see how much time we need to wait.
# TODO the beacon clock might jump here also. It's probably easier to complete
# the work for the whole slot using a monotonic clock instead, then deal

View File

@ -132,6 +132,16 @@ type
"If you set this to 'auto', a persistent automatically generated ID will be seleceted for each --dataDir folder."
name: "node-name" }: string
verifyFinalization* {.
defaultValue: false
desc: "Specify whether to verify finalization occurs on schedule, for testing."
name: "verify-finalization" }: bool
checkEpochs* {.
defaultValue: 0
desc: "A positive checkEpochs selects how many epochs to run."
name: "check-epochs" }: uint64
metricsServer* {.
defaultValue: false
desc: "Enable the metrics server."

View File

@ -63,6 +63,7 @@ cd "$DATA_DIR" && $NODE_BIN \
--state-snapshot=$SNAPSHOT_FILE \
$DEPOSIT_WEB3_URL_ARG \
--deposit-contract=$DEPOSIT_CONTRACT_ADDRESS \
--verify-finalization=on \
--metrics-server=on \
--metrics-server-address="127.0.0.1" \
--metrics-server-port="$(( $BASE_METRICS_PORT + $NODE_ID ))" \