2020-09-14 14:26:31 +00:00
|
|
|
# beacon_chain
|
2022-01-18 13:36:52 +00:00
|
|
|
# Copyright (c) 2020-2022 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.
|
|
|
|
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
|
|
|
import
|
|
|
|
# Standard libraries
|
2021-10-20 11:36:38 +00:00
|
|
|
std/[deques, sets],
|
2020-09-14 14:26:31 +00:00
|
|
|
# Status libraries
|
2021-03-06 07:32:55 +00:00
|
|
|
chronicles,
|
2020-09-14 14:26:31 +00:00
|
|
|
# Internal
|
2021-10-18 16:37:27 +00:00
|
|
|
../spec/helpers,
|
2022-01-18 13:36:52 +00:00
|
|
|
../spec/datatypes/[phase0, altair],
|
2021-10-18 16:37:27 +00:00
|
|
|
./blockchain_dag
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2021-10-20 11:36:38 +00:00
|
|
|
export phase0, altair, merge, deques, sets, blockchain_dag
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2020-10-07 16:57:21 +00:00
|
|
|
logScope: topics = "exitpool"
|
|
|
|
|
|
|
|
const
|
2021-10-18 16:37:27 +00:00
|
|
|
ATTESTER_SLASHINGS_BOUND* = MAX_ATTESTER_SLASHINGS * 4
|
|
|
|
PROPOSER_SLASHINGS_BOUND* = MAX_PROPOSER_SLASHINGS * 4
|
|
|
|
VOLUNTARY_EXITS_BOUND* = MAX_VOLUNTARY_EXITS * 4
|
|
|
|
|
|
|
|
type
|
|
|
|
OnVoluntaryExitCallback* =
|
|
|
|
proc(data: SignedVoluntaryExit) {.gcsafe, raises: [Defect].}
|
|
|
|
|
|
|
|
ExitPool* = object
|
|
|
|
## The exit pool tracks attester slashings, proposer slashings, and
|
|
|
|
## voluntary exits that could be added to a proposed block.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-10-20 11:36:38 +00:00
|
|
|
prior_seen_attester_slashed_indices*: HashSet[uint64] ## \
|
2021-10-18 16:37:27 +00:00
|
|
|
## Records attester-slashed indices seen.
|
|
|
|
|
2021-10-20 11:36:38 +00:00
|
|
|
prior_seen_proposer_slashed_indices*: HashSet[uint64] ## \
|
2021-10-18 16:37:27 +00:00
|
|
|
## Records proposer-slashed indices seen.
|
|
|
|
|
2021-10-20 11:36:38 +00:00
|
|
|
prior_seen_voluntary_exit_indices*: HashSet[uint64] ##\
|
2021-10-18 16:37:27 +00:00
|
|
|
## Records voluntary exit indices seen.
|
|
|
|
|
|
|
|
dag*: ChainDAGRef
|
|
|
|
onVoluntaryExitReceived*: OnVoluntaryExitCallback
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2021-09-22 12:17:15 +00:00
|
|
|
proc init*(T: type ExitPool, dag: ChainDAGRef,
|
|
|
|
onVoluntaryExit: OnVoluntaryExitCallback = nil): T =
|
2021-06-01 11:13:40 +00:00
|
|
|
## Initialize an ExitPool from the dag `headState`
|
2020-09-14 14:26:31 +00:00
|
|
|
T(
|
2020-10-07 16:57:21 +00:00
|
|
|
# Allow for filtering out some exit 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),
|
2021-06-01 11:13:40 +00:00
|
|
|
dag: dag,
|
2021-09-22 12:17:15 +00:00
|
|
|
onVoluntaryExitReceived: onVoluntaryExit
|
2020-09-14 14:26:31 +00:00
|
|
|
)
|
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
func addExitMessage(subpool: var auto, exitMessage, bound: auto) =
|
2020-10-07 16:57:21 +00:00
|
|
|
# Prefer newer to older exit messages
|
2020-09-14 14:26:31 +00:00
|
|
|
while subpool.lenu64 >= bound:
|
|
|
|
discard subpool.popFirst()
|
|
|
|
|
|
|
|
subpool.addLast(exitMessage)
|
|
|
|
doAssert subpool.lenu64 <= bound
|
|
|
|
|
2020-10-07 16:57:21 +00:00
|
|
|
iterator getValidatorIndices(attester_slashing: AttesterSlashing): uint64 =
|
2021-10-20 11:36:38 +00:00
|
|
|
let attestation_2_indices =
|
|
|
|
toHashSet(attester_slashing.attestation_2.attesting_indices.asSeq)
|
|
|
|
|
|
|
|
for validator_index in attester_slashing.attestation_1.attesting_indices.asSeq:
|
|
|
|
if validator_index notin attestation_2_indices:
|
|
|
|
continue
|
|
|
|
yield validator_index
|
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
|
|
|
|
|
2021-11-05 15:39:47 +00:00
|
|
|
func isSeen*(pool: ExitPool, msg: AttesterSlashing): bool =
|
|
|
|
for idx in getValidatorIndices(msg):
|
|
|
|
# One index is enough!
|
|
|
|
if idx notin pool.prior_seen_attester_slashed_indices:
|
|
|
|
return false
|
|
|
|
true
|
|
|
|
|
|
|
|
func isSeen*(pool: ExitPool, msg: ProposerSlashing): bool =
|
|
|
|
msg.signed_header_1.message.proposer_index in
|
|
|
|
pool.prior_seen_proposer_slashed_indices
|
|
|
|
|
|
|
|
func isSeen*(pool: ExitPool, msg: SignedVoluntaryExit): bool =
|
|
|
|
msg.message.validator_index in
|
|
|
|
pool.prior_seen_voluntary_exit_indices
|
|
|
|
|
|
|
|
func addMessage*(pool: var ExitPool, msg: AttesterSlashing) =
|
|
|
|
for idx in getValidatorIndices(msg):
|
|
|
|
pool.prior_seen_attester_slashed_indices.incl idx
|
|
|
|
|
|
|
|
pool.attester_slashings.addExitMessage(msg, ATTESTER_SLASHINGS_BOUND)
|
|
|
|
|
|
|
|
func addMessage*(pool: var ExitPool, msg: ProposerSlashing) =
|
|
|
|
pool.prior_seen_proposer_slashed_indices.incl(
|
|
|
|
msg.signed_header_1.message.proposer_index)
|
|
|
|
pool.proposer_slashings.addExitMessage(msg, PROPOSER_SLASHINGS_BOUND)
|
|
|
|
|
|
|
|
func addMessage*(pool: var ExitPool, msg: SignedVoluntaryExit) =
|
|
|
|
pool.prior_seen_voluntary_exit_indices.incl(
|
|
|
|
msg.message.validator_index)
|
|
|
|
pool.voluntary_exits.addExitMessage(msg, VOLUNTARY_EXITS_BOUND)
|
|
|
|
|
2021-10-18 16:37:27 +00:00
|
|
|
func getExitMessagesForBlock(
|
|
|
|
subpool: var Deque, validators: auto, 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.
|
|
|
|
#
|
|
|
|
# Beyond that, no other criterion of the exit messages' validity changes from
|
|
|
|
# when they were created, so given that we validated them to start with, they
|
|
|
|
# otherwise remain as valid as when we received them. There's no need to thus
|
|
|
|
# re-validate them on their way out.
|
|
|
|
#
|
|
|
|
# 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
|
|
|
|
let exit_message = subpool.popLast()
|
|
|
|
|
2021-10-18 16:37:27 +00:00
|
|
|
for slashed_index in getValidatorIndices(exit_message):
|
|
|
|
if validators.lenu64 <= slashed_index:
|
|
|
|
continue
|
|
|
|
if validators[slashed_index].exit_epoch != FAR_FUTURE_EPOCH:
|
|
|
|
continue
|
|
|
|
if seen.containsOrIncl(slashed_index):
|
|
|
|
continue
|
2020-10-07 16:57:21 +00:00
|
|
|
|
2021-10-18 16:37:27 +00:00
|
|
|
if not output.add exit_message:
|
|
|
|
break
|
2020-10-07 16:57:21 +00:00
|
|
|
|
|
|
|
subpool.clear()
|
2021-10-18 16:37:27 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
func getBeaconBlockExits*(pool: var ExitPool, state: ForkyBeaconState): BeaconBlockExits =
|
2021-10-18 16:37:27 +00:00
|
|
|
var
|
|
|
|
indices: HashSet[uint64]
|
|
|
|
res: BeaconBlockExits
|
|
|
|
|
|
|
|
getExitMessagesForBlock(
|
|
|
|
pool.attester_slashings, state.validators.asSeq(), indices,
|
|
|
|
res.attester_slashings)
|
|
|
|
getExitMessagesForBlock(
|
|
|
|
pool.proposer_slashings, state.validators.asSeq(), indices,
|
|
|
|
res.proposer_slashings)
|
|
|
|
getExitMessagesForBlock(
|
|
|
|
pool.voluntary_exits, state.validators.asSeq(), indices,
|
|
|
|
res.voluntary_exits)
|
|
|
|
|
2021-10-20 11:36:38 +00:00
|
|
|
res
|