2020-09-14 14:26:31 +00:00
|
|
|
# beacon_chain
|
2023-01-17 15:45:04 +00:00
|
|
|
# Copyright (c) 2020-2023 Status Research & Development GmbH
|
2020-09-14 14:26:31 +00:00
|
|
|
# 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.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2020-09-14 14:26:31 +00:00
|
|
|
|
|
|
|
import
|
|
|
|
# Standard libraries
|
2021-10-20 11:36:38 +00:00
|
|
|
std/[deques, sets],
|
2020-09-14 14:26:31 +00:00
|
|
|
# Internal
|
2022-08-23 15:30:46 +00:00
|
|
|
../spec/datatypes/base,
|
|
|
|
../spec/[helpers, state_transition_block],
|
2022-07-06 10:33:02 +00:00
|
|
|
"."/[attestation_pool, blockchain_dag]
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2023-02-17 13:35:12 +00:00
|
|
|
from ../spec/beaconstate import check_bls_to_execution_change
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
export base, deques, blockchain_dag
|
2020-10-07 16:57:21 +00:00
|
|
|
|
|
|
|
const
|
2023-01-17 15:45:04 +00:00
|
|
|
ATTESTER_SLASHINGS_BOUND = MAX_ATTESTER_SLASHINGS * 4
|
|
|
|
PROPOSER_SLASHINGS_BOUND = MAX_PROPOSER_SLASHINGS * 4
|
|
|
|
VOLUNTARY_EXITS_BOUND = MAX_VOLUNTARY_EXITS * 4
|
2021-10-18 16:37:27 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
# For Capella launch; scale back later
|
2023-02-03 15:28:28 +00:00
|
|
|
BLS_TO_EXECUTION_CHANGES_BOUND = 16384'u64
|
2023-01-19 22:00:40 +00:00
|
|
|
|
2021-10-18 16:37:27 +00:00
|
|
|
type
|
2023-01-17 15:45:04 +00:00
|
|
|
OnVoluntaryExitCallback =
|
2023-08-25 09:29:07 +00:00
|
|
|
proc(data: SignedVoluntaryExit) {.gcsafe, raises: [].}
|
2023-12-22 13:52:43 +00:00
|
|
|
OnBLSToExecutionChangeCallback =
|
|
|
|
proc(data: SignedBLSToExecutionChange) {.gcsafe, raises: [].}
|
2021-10-18 16:37:27 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
ValidatorChangePool* = object
|
|
|
|
## The validator change pool tracks attester slashings, proposer slashings,
|
|
|
|
## voluntary exits, and BLS to execution changes that could be added to a
|
|
|
|
## proposed block.
|
2021-10-18 16:37:27 +00:00
|
|
|
|
|
|
|
attester_slashings*: Deque[AttesterSlashing] ## \
|
|
|
|
## Not a function of chain DAG branch; just used as a FIFO queue for blocks
|
|
|
|
|
|
|
|
proposer_slashings*: Deque[ProposerSlashing] ## \
|
|
|
|
## Not a function of chain DAG branch; just used as a FIFO queue for blocks
|
|
|
|
|
|
|
|
voluntary_exits*: Deque[SignedVoluntaryExit] ## \
|
|
|
|
## Not a function of chain DAG branch; just used as a FIFO queue for blocks
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
bls_to_execution_changes_gossip*: Deque[SignedBLSToExecutionChange] ## \
|
|
|
|
## Not a function of chain DAG branch; just used as a FIFO queue for blocks
|
|
|
|
|
|
|
|
bls_to_execution_changes_api*: Deque[SignedBLSToExecutionChange] ## \
|
2023-01-19 22:00:40 +00:00
|
|
|
## Not a function of chain DAG branch; just used as a FIFO queue for blocks
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
prior_seen_attester_slashed_indices: HashSet[uint64] ## \
|
2021-10-18 16:37:27 +00:00
|
|
|
## Records attester-slashed indices seen.
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
prior_seen_proposer_slashed_indices: HashSet[uint64] ## \
|
2021-10-18 16:37:27 +00:00
|
|
|
## Records proposer-slashed indices seen.
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
prior_seen_voluntary_exit_indices: HashSet[uint64] ##\
|
2021-10-18 16:37:27 +00:00
|
|
|
## Records voluntary exit indices seen.
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
prior_seen_bls_to_execution_change_indices: HashSet[uint64] ##\
|
2023-01-19 22:00:40 +00:00
|
|
|
## Records BLS to execution change indices seen.
|
|
|
|
|
2021-10-18 16:37:27 +00:00
|
|
|
dag*: ChainDAGRef
|
2023-02-03 15:28:28 +00:00
|
|
|
attestationPool: ref AttestationPool
|
2021-10-18 16:37:27 +00:00
|
|
|
onVoluntaryExitReceived*: OnVoluntaryExitCallback
|
2023-12-22 13:52:43 +00:00
|
|
|
onBLSToExecutionChangeReceived*: OnBLSToExecutionChangeCallback
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
func init*(T: type ValidatorChangePool, dag: ChainDAGRef,
|
2022-07-06 10:33:02 +00:00
|
|
|
attestationPool: ref AttestationPool = nil,
|
2023-12-22 13:52:43 +00:00
|
|
|
onVoluntaryExit: OnVoluntaryExitCallback = nil,
|
|
|
|
onBLSToExecutionChange: OnBLSToExecutionChangeCallback = nil): T =
|
2023-01-19 22:00:40 +00:00
|
|
|
## Initialize an ValidatorChangePool from the dag `headState`
|
2020-09-14 14:26:31 +00:00
|
|
|
T(
|
2023-01-19 22:00:40 +00:00
|
|
|
# Allow filtering some validator change messages during block production
|
2020-09-14 14:26:31 +00:00
|
|
|
attester_slashings:
|
2020-10-07 16:57:21 +00:00
|
|
|
initDeque[AttesterSlashing](initialSize = ATTESTER_SLASHINGS_BOUND.int),
|
2020-09-14 14:26:31 +00:00
|
|
|
proposer_slashings:
|
2020-10-07 16:57:21 +00:00
|
|
|
initDeque[ProposerSlashing](initialSize = PROPOSER_SLASHINGS_BOUND.int),
|
2020-09-14 14:26:31 +00:00
|
|
|
voluntary_exits:
|
2020-10-07 16:57:21 +00:00
|
|
|
initDeque[SignedVoluntaryExit](initialSize = VOLUNTARY_EXITS_BOUND.int),
|
2023-02-03 15:28:28 +00:00
|
|
|
bls_to_execution_changes_gossip:
|
|
|
|
# TODO scale-back to BLS_TO_EXECUTION_CHANGES_BOUND post-capella, but
|
|
|
|
# given large bound, allow to grow dynamically rather than statically
|
|
|
|
# allocate all at once
|
|
|
|
initDeque[SignedBLSToExecutionChange](initialSize = 1024),
|
|
|
|
bls_to_execution_changes_api:
|
2023-01-19 22:00:40 +00:00
|
|
|
# TODO scale-back to BLS_TO_EXECUTION_CHANGES_BOUND post-capella, but
|
|
|
|
# given large bound, allow to grow dynamically rather than statically
|
|
|
|
# allocate all at once
|
|
|
|
initDeque[SignedBLSToExecutionChange](initialSize = 1024),
|
2021-06-01 11:13:40 +00:00
|
|
|
dag: dag,
|
2022-07-06 10:33:02 +00:00
|
|
|
attestationPool: attestationPool,
|
2023-12-22 13:52:43 +00:00
|
|
|
onVoluntaryExitReceived: onVoluntaryExit,
|
|
|
|
onBLSToExecutionChangeReceived: onBLSToExecutionChange)
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
func addValidatorChangeMessage(
|
|
|
|
subpool: var auto, seenpool: var auto, validatorChangeMessage: auto,
|
|
|
|
bound: static[uint64]) =
|
|
|
|
# Prefer newer to older validator change messages
|
2020-09-14 14:26:31 +00:00
|
|
|
while subpool.lenu64 >= bound:
|
2023-01-19 22:00:40 +00:00
|
|
|
# TODO remove temporary workaround once capella happens
|
|
|
|
when bound == BLS_TO_EXECUTION_CHANGES_BOUND:
|
|
|
|
seenpool.excl subpool.popFirst().message.validator_index
|
|
|
|
else:
|
|
|
|
discard subpool.popFirst()
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
subpool.addLast(validatorChangeMessage)
|
2020-09-14 14:26:31 +00:00
|
|
|
doAssert subpool.lenu64 <= bound
|
|
|
|
|
2020-10-07 16:57:21 +00:00
|
|
|
iterator getValidatorIndices(proposer_slashing: ProposerSlashing): uint64 =
|
|
|
|
yield proposer_slashing.signed_header_1.message.proposer_index
|
|
|
|
|
|
|
|
iterator getValidatorIndices(voluntary_exit: SignedVoluntaryExit): uint64 =
|
|
|
|
yield voluntary_exit.message.validator_index
|
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
iterator getValidatorIndices(
|
|
|
|
bls_to_execution_change: SignedBLSToExecutionChange): uint64 =
|
|
|
|
yield bls_to_execution_change.message.validator_index
|
|
|
|
|
|
|
|
func isSeen*(pool: ValidatorChangePool, msg: AttesterSlashing): bool =
|
2021-11-05 15:39:47 +00:00
|
|
|
for idx in getValidatorIndices(msg):
|
|
|
|
# One index is enough!
|
|
|
|
if idx notin pool.prior_seen_attester_slashed_indices:
|
|
|
|
return false
|
|
|
|
true
|
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
func isSeen*(pool: ValidatorChangePool, msg: ProposerSlashing): bool =
|
2021-11-05 15:39:47 +00:00
|
|
|
msg.signed_header_1.message.proposer_index in
|
|
|
|
pool.prior_seen_proposer_slashed_indices
|
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
func isSeen*(pool: ValidatorChangePool, msg: SignedVoluntaryExit): bool =
|
|
|
|
msg.message.validator_index in pool.prior_seen_voluntary_exit_indices
|
|
|
|
|
|
|
|
func isSeen*(pool: ValidatorChangePool, msg: SignedBLSToExecutionChange): bool =
|
2021-11-05 15:39:47 +00:00
|
|
|
msg.message.validator_index in
|
2023-01-19 22:00:40 +00:00
|
|
|
pool.prior_seen_bls_to_execution_change_indices
|
2021-11-05 15:39:47 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
func addMessage*(pool: var ValidatorChangePool, msg: AttesterSlashing) =
|
2021-11-05 15:39:47 +00:00
|
|
|
for idx in getValidatorIndices(msg):
|
|
|
|
pool.prior_seen_attester_slashed_indices.incl idx
|
2022-07-06 10:33:02 +00:00
|
|
|
if pool.attestationPool != nil:
|
|
|
|
let i = ValidatorIndex.init(idx).valueOr:
|
|
|
|
continue
|
|
|
|
pool.attestationPool.forkChoice.process_equivocation(i)
|
2021-11-05 15:39:47 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
pool.attester_slashings.addValidatorChangeMessage(
|
|
|
|
pool.prior_seen_attester_slashed_indices, msg, ATTESTER_SLASHINGS_BOUND)
|
2021-11-05 15:39:47 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
func addMessage*(pool: var ValidatorChangePool, msg: ProposerSlashing) =
|
2021-11-05 15:39:47 +00:00
|
|
|
pool.prior_seen_proposer_slashed_indices.incl(
|
|
|
|
msg.signed_header_1.message.proposer_index)
|
2023-01-19 22:00:40 +00:00
|
|
|
pool.proposer_slashings.addValidatorChangeMessage(
|
|
|
|
pool.prior_seen_proposer_slashed_indices, msg, PROPOSER_SLASHINGS_BOUND)
|
2021-11-05 15:39:47 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
func addMessage*(pool: var ValidatorChangePool, msg: SignedVoluntaryExit) =
|
2021-11-05 15:39:47 +00:00
|
|
|
pool.prior_seen_voluntary_exit_indices.incl(
|
|
|
|
msg.message.validator_index)
|
2023-01-19 22:00:40 +00:00
|
|
|
pool.voluntary_exits.addValidatorChangeMessage(
|
|
|
|
pool.prior_seen_voluntary_exit_indices, msg, VOLUNTARY_EXITS_BOUND)
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
func addMessage*(
|
|
|
|
pool: var ValidatorChangePool, msg: SignedBLSToExecutionChange,
|
|
|
|
localPriorityMessage: bool) =
|
2023-01-19 22:00:40 +00:00
|
|
|
pool.prior_seen_bls_to_execution_change_indices.incl(
|
|
|
|
msg.message.validator_index)
|
2023-02-03 15:28:28 +00:00
|
|
|
template addMessageAux(subpool) =
|
|
|
|
addValidatorChangeMessage(
|
|
|
|
subpool, pool.prior_seen_bls_to_execution_change_indices, msg,
|
|
|
|
BLS_TO_EXECUTION_CHANGES_BOUND)
|
|
|
|
if localPriorityMessage:
|
|
|
|
addMessageAux(pool.bls_to_execution_changes_api)
|
|
|
|
else:
|
|
|
|
addMessageAux(pool.bls_to_execution_changes_gossip)
|
|
|
|
|
|
|
|
proc validateValidatorChangeMessage(
|
2022-08-23 15:30:46 +00:00
|
|
|
cfg: RuntimeConfig, state: ForkyBeaconState, msg: ProposerSlashing): bool =
|
|
|
|
check_proposer_slashing(state, msg, {}).isOk
|
2023-02-03 15:28:28 +00:00
|
|
|
proc validateValidatorChangeMessage(
|
2022-08-23 15:30:46 +00:00
|
|
|
cfg: RuntimeConfig, state: ForkyBeaconState, msg: AttesterSlashing): bool =
|
|
|
|
check_attester_slashing(state, msg, {}).isOk
|
2023-02-03 15:28:28 +00:00
|
|
|
proc validateValidatorChangeMessage(
|
2022-08-23 15:30:46 +00:00
|
|
|
cfg: RuntimeConfig, state: ForkyBeaconState, msg: SignedVoluntaryExit):
|
|
|
|
bool =
|
|
|
|
check_voluntary_exit(cfg, state, msg, {}).isOk
|
2023-02-03 15:28:28 +00:00
|
|
|
proc validateValidatorChangeMessage(
|
2023-01-19 22:00:40 +00:00
|
|
|
cfg: RuntimeConfig, state: ForkyBeaconState,
|
|
|
|
msg: SignedBLSToExecutionChange): bool =
|
2023-02-17 13:35:12 +00:00
|
|
|
check_bls_to_execution_change(cfg.genesisFork, state, msg, {}).isOk
|
2022-08-23 15:30:46 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
proc getValidatorChangeMessagesForBlock(
|
2022-08-23 15:30:46 +00:00
|
|
|
subpool: var Deque, cfg: RuntimeConfig, state: ForkyBeaconState,
|
|
|
|
seen: var HashSet, output: var List) =
|
2020-10-07 16:57:21 +00:00
|
|
|
# Approach taken here is to simply collect messages, effectively, a circular
|
|
|
|
# buffer and only re-validate that they haven't already found themselves out
|
|
|
|
# of the network eventually via some exit message at block construction time
|
|
|
|
# at which point we use exit_epoch. It doesn't matter which of these message
|
|
|
|
# types has triggered that exit, as the validation on incoming messages will
|
|
|
|
# find it to either be IGNORE (if it's the same type of exit message) or, if
|
|
|
|
# it's a different type, REJECT. Neither is worth packaging into BeaconBlock
|
|
|
|
# messages we broadcast.
|
|
|
|
#
|
2022-08-23 15:30:46 +00:00
|
|
|
# Beyond that, it may happen that messages were signed in an epoch pre-dating
|
|
|
|
# the current state by two or more forks - such messages can no longer be
|
|
|
|
# validated in the context of the given state and are therefore dropped.
|
2020-10-07 16:57:21 +00:00
|
|
|
#
|
|
|
|
# This overall approach handles a scenario wherein we receive an exit message
|
|
|
|
# over gossip and put it in the pool; receive a block X, with that message in
|
|
|
|
# it, and select it as head; then orphan block X and build instead on X-1. If
|
|
|
|
# this occurs, only validating after the fact ensures that we still broadcast
|
|
|
|
# out those exit messages that were in orphaned block X by not having eagerly
|
|
|
|
# removed them, if we have the chance.
|
2021-10-18 16:37:27 +00:00
|
|
|
while subpool.len > 0 and output.len < output.maxLen:
|
2020-10-07 16:57:21 +00:00
|
|
|
# Prefer recent messages
|
2023-02-03 15:28:28 +00:00
|
|
|
let validator_change_message = subpool.popLast()
|
2022-08-23 15:30:46 +00:00
|
|
|
# Re-check that message is still valid in the state that we're proposing
|
2023-02-03 15:28:28 +00:00
|
|
|
if not validateValidatorChangeMessage(cfg, state, validator_change_message):
|
2022-08-23 15:30:46 +00:00
|
|
|
continue
|
2020-10-07 16:57:21 +00:00
|
|
|
|
2022-08-23 15:30:46 +00:00
|
|
|
var skip = false
|
2023-02-03 15:28:28 +00:00
|
|
|
for slashed_index in getValidatorIndices(validator_change_message):
|
2021-10-18 16:37:27 +00:00
|
|
|
if seen.containsOrIncl(slashed_index):
|
2022-08-23 15:30:46 +00:00
|
|
|
skip = true
|
2021-10-18 16:37:27 +00:00
|
|
|
break
|
2022-08-23 15:30:46 +00:00
|
|
|
if skip:
|
|
|
|
continue
|
|
|
|
|
2023-02-03 15:28:28 +00:00
|
|
|
if not output.add validator_change_message:
|
2022-08-23 15:30:46 +00:00
|
|
|
break
|
2020-10-07 16:57:21 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
proc getBeaconBlockValidatorChanges*(
|
|
|
|
pool: var ValidatorChangePool, cfg: RuntimeConfig, state: ForkyBeaconState):
|
|
|
|
BeaconBlockValidatorChanges =
|
2021-10-18 16:37:27 +00:00
|
|
|
var
|
|
|
|
indices: HashSet[uint64]
|
2023-01-19 22:00:40 +00:00
|
|
|
res: BeaconBlockValidatorChanges
|
2021-10-18 16:37:27 +00:00
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
getValidatorChangeMessagesForBlock(
|
2022-08-23 15:30:46 +00:00
|
|
|
pool.attester_slashings, cfg, state, indices, res.attester_slashings)
|
2023-01-19 22:00:40 +00:00
|
|
|
getValidatorChangeMessagesForBlock(
|
2022-08-23 15:30:46 +00:00
|
|
|
pool.proposer_slashings, cfg, state, indices, res.proposer_slashings)
|
2023-01-19 22:00:40 +00:00
|
|
|
getValidatorChangeMessagesForBlock(
|
2022-08-23 15:30:46 +00:00
|
|
|
pool.voluntary_exits, cfg, state, indices, res.voluntary_exits)
|
2023-09-27 15:10:28 +00:00
|
|
|
when typeof(state).kind >= ConsensusFork.Capella:
|
2023-02-03 15:28:28 +00:00
|
|
|
# Prioritize these
|
|
|
|
getValidatorChangeMessagesForBlock(
|
|
|
|
pool.bls_to_execution_changes_api, cfg, state, indices,
|
|
|
|
res.bls_to_execution_changes)
|
|
|
|
|
2023-01-19 22:00:40 +00:00
|
|
|
getValidatorChangeMessagesForBlock(
|
2023-02-03 15:28:28 +00:00
|
|
|
pool.bls_to_execution_changes_gossip, cfg, state, indices,
|
2023-01-19 22:00:40 +00:00
|
|
|
res.bls_to_execution_changes)
|
2021-10-18 16:37:27 +00:00
|
|
|
|
2021-10-20 11:36:38 +00:00
|
|
|
res
|