2020-09-14 14:26:31 +00:00
|
|
|
# beacon_chain
|
2021-01-26 11:52:00 +00:00
|
|
|
# Copyright (c) 2020-2021 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-03-06 07:32:55 +00:00
|
|
|
std/[deques, intsets, tables],
|
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-03-06 07:32:55 +00:00
|
|
|
../spec/[crypto, datatypes, helpers],
|
|
|
|
"."/[blockchain_dag, block_quarantine],
|
2021-03-04 09:13:44 +00:00
|
|
|
../beacon_node_types
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2021-01-26 11:52:00 +00:00
|
|
|
export beacon_node_types, intsets
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2020-10-07 16:57:21 +00:00
|
|
|
logScope: topics = "exitpool"
|
|
|
|
|
|
|
|
const
|
2021-03-06 07:32:55 +00:00
|
|
|
ATTESTER_SLASHINGS_BOUND* = MAX_ATTESTER_SLASHINGS * 2
|
|
|
|
PROPOSER_SLASHINGS_BOUND* = MAX_PROPOSER_SLASHINGS * 2
|
|
|
|
VOLUNTARY_EXITS_BOUND* = MAX_VOLUNTARY_EXITS * 2
|
2020-09-14 14:26:31 +00:00
|
|
|
|
|
|
|
proc init*(
|
|
|
|
T: type ExitPool, chainDag: ChainDAGRef, quarantine: QuarantineRef): T =
|
|
|
|
## Initialize an ExitPool from the chainDag `headState`
|
|
|
|
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),
|
2020-09-14 14:26:31 +00:00
|
|
|
chainDag: chainDag,
|
|
|
|
quarantine: quarantine
|
|
|
|
)
|
|
|
|
|
2020-09-24 17:05:49 +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 =
|
|
|
|
# TODO rely on sortedness and do this sans memory allocations, but it's only
|
|
|
|
# when producing a beacon block, which is rare bottlenecked elsewhere.
|
|
|
|
let
|
|
|
|
attestation_1_indices =
|
|
|
|
attester_slashing.attestation_1.attesting_indices.asSeq
|
|
|
|
attestation_2_indices =
|
|
|
|
attester_slashing.attestation_2.attesting_indices.asSeq
|
|
|
|
attester_slashed_indices =
|
2021-01-26 11:52:00 +00:00
|
|
|
toIntSet(attestation_1_indices) * toIntSet(attestation_2_indices)
|
2020-10-07 16:57:21 +00:00
|
|
|
|
|
|
|
for validator_index in attester_slashed_indices:
|
2021-01-26 11:52:00 +00:00
|
|
|
yield validator_index.uint64
|
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
|
|
|
|
|
|
|
|
# TODO stew/sequtils2
|
|
|
|
template allIt(s, pred: untyped): bool =
|
|
|
|
# https://github.com/nim-lang/Nim/blob/version-1-2/lib/pure/collections/sequtils.nim#L640-L662
|
|
|
|
# without the items(...)
|
|
|
|
var result = true
|
|
|
|
for it {.inject.} in s:
|
|
|
|
if not pred:
|
|
|
|
result = false
|
|
|
|
break
|
|
|
|
result
|
|
|
|
|
|
|
|
func getExitMessagesForBlock[T](
|
|
|
|
subpool: var Deque[T], pool: var ExitPool, bound: uint64): seq[T] =
|
|
|
|
# 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.
|
|
|
|
while true:
|
|
|
|
if subpool.len == 0 or result.lenu64 >= bound:
|
2020-09-14 14:26:31 +00:00
|
|
|
break
|
|
|
|
|
2020-10-07 16:57:21 +00:00
|
|
|
# Prefer recent messages
|
|
|
|
let exit_message = subpool.popLast()
|
|
|
|
|
|
|
|
if allIt(
|
|
|
|
getValidatorIndices(exit_message),
|
|
|
|
pool.chainDag.headState.data.data.validators[it].exit_epoch !=
|
|
|
|
FAR_FUTURE_EPOCH):
|
|
|
|
# A beacon block exit message already targeted all these validators
|
|
|
|
continue
|
|
|
|
|
|
|
|
result.add exit_message
|
|
|
|
|
|
|
|
subpool.clear()
|
2020-09-16 07:16:23 +00:00
|
|
|
doAssert result.lenu64 <= bound
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2020-09-16 07:16:23 +00:00
|
|
|
func getAttesterSlashingsForBlock*(pool: var ExitPool):
|
|
|
|
seq[AttesterSlashing] =
|
|
|
|
## Retrieve attester slashings that may be added to a new block
|
|
|
|
getExitMessagesForBlock[AttesterSlashing](
|
2020-10-07 16:57:21 +00:00
|
|
|
pool.attester_slashings, pool, MAX_ATTESTER_SLASHINGS)
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2020-09-16 07:16:23 +00:00
|
|
|
func getProposerSlashingsForBlock*(pool: var ExitPool):
|
|
|
|
seq[ProposerSlashing] =
|
|
|
|
## Retrieve proposer slashings that may be added to a new block
|
|
|
|
getExitMessagesForBlock[ProposerSlashing](
|
2020-10-07 16:57:21 +00:00
|
|
|
pool.proposer_slashings, pool, MAX_PROPOSER_SLASHINGS)
|
2020-09-14 14:26:31 +00:00
|
|
|
|
2020-09-16 07:16:23 +00:00
|
|
|
func getVoluntaryExitsForBlock*(pool: var ExitPool):
|
2020-09-24 17:05:49 +00:00
|
|
|
seq[SignedVoluntaryExit] =
|
2020-09-16 07:16:23 +00:00
|
|
|
## Retrieve voluntary exits that may be added to a new block
|
2020-09-24 17:05:49 +00:00
|
|
|
getExitMessagesForBlock[SignedVoluntaryExit](
|
2020-10-07 16:57:21 +00:00
|
|
|
pool.voluntary_exits, pool, MAX_VOLUNTARY_EXITS)
|