2021-01-25 18:45:48 +00:00
|
|
|
# beacon_chain
|
2021-02-25 13:37:22 +00:00
|
|
|
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
2021-01-25 18:45:48 +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
|
|
|
|
# Status lib
|
|
|
|
blscurve,
|
|
|
|
stew/byteutils,
|
|
|
|
# Internal
|
|
|
|
../ssz/merkleization,
|
|
|
|
./crypto, ./datatypes, ./helpers, ./presets,
|
|
|
|
./beaconstate, ./digest
|
|
|
|
|
|
|
|
export SignatureSet, BatchedBLSVerifierCache, batchVerify, batchVerifySerial, batchVerifyParallel
|
|
|
|
|
|
|
|
func `$`*(s: SignatureSet): string =
|
|
|
|
"(pubkey: 0x" & s.pubkey.toHex() &
|
|
|
|
", signing_root: 0x" & s.message.toHex() &
|
|
|
|
", signature: 0x" & s.signature.toHex() & ')'
|
|
|
|
|
|
|
|
# Important:
|
|
|
|
# - Due to lazy loading, when we do crypto verification
|
|
|
|
# and only then state-transition verification,
|
|
|
|
# there is no guarantee that pubkeys and signatures received are valid
|
|
|
|
# unlike when Nimbus did eager loading which ensured they were correct beforehand
|
|
|
|
|
2021-04-09 12:59:24 +00:00
|
|
|
template loadOrExit(signature: ValidatorSig, failReturn: auto):
|
2021-04-26 20:39:44 +00:00
|
|
|
CookedSig =
|
2021-01-25 18:45:48 +00:00
|
|
|
## Load a BLS signature from a raw signature
|
2021-04-09 12:59:24 +00:00
|
|
|
## Exits the **caller** with false if the signature is invalid
|
2021-01-25 18:45:48 +00:00
|
|
|
let sig = signature.load()
|
|
|
|
if sig.isNone:
|
2021-04-09 12:59:24 +00:00
|
|
|
return failReturn # this exits the calling scope, as templates are inlined.
|
2021-01-25 18:45:48 +00:00
|
|
|
sig.unsafeGet()
|
|
|
|
|
2021-04-09 12:59:24 +00:00
|
|
|
template loadWithCacheOrExit(pubkey: ValidatorPubKey, failReturn: auto):
|
|
|
|
blscurve.PublicKey =
|
2021-01-25 18:45:48 +00:00
|
|
|
## Load a BLS signature from a raw public key
|
2021-04-09 12:59:24 +00:00
|
|
|
## Exits the **caller** with false if the public key is invalid
|
2021-01-25 18:45:48 +00:00
|
|
|
let pk = pubkey.loadWithCache()
|
|
|
|
if pk.isNone:
|
2021-04-09 12:59:24 +00:00
|
|
|
return failReturn # this exits the calling scope, as templates are inlined.
|
2021-01-25 18:45:48 +00:00
|
|
|
pk.unsafeGet()
|
|
|
|
|
|
|
|
func addSignatureSet[T](
|
|
|
|
sigs: var seq[SignatureSet],
|
|
|
|
pubkey: blscurve.PublicKey,
|
|
|
|
sszObj: T,
|
2021-04-26 20:39:44 +00:00
|
|
|
signature: CookedSig,
|
2021-01-25 18:45:48 +00:00
|
|
|
genesis_validators_root: Eth2Digest,
|
|
|
|
fork: Fork,
|
|
|
|
epoch: Epoch,
|
2021-04-26 20:39:44 +00:00
|
|
|
domain: DomainType) =
|
2021-01-25 18:45:48 +00:00
|
|
|
## Add a new signature set triplet (pubkey, message, signature)
|
|
|
|
## to a collection of signature sets for batch verification.
|
|
|
|
## Can return false if `signature` wasn't deserialized to a valid BLS signature.
|
|
|
|
let signing_root = compute_signing_root(
|
|
|
|
sszObj,
|
|
|
|
get_domain(
|
|
|
|
fork, domain,
|
|
|
|
epoch,
|
|
|
|
genesis_validators_root
|
|
|
|
)
|
|
|
|
).data
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.add((
|
|
|
|
pubkey,
|
|
|
|
signing_root,
|
|
|
|
blscurve.Signature(signature)
|
|
|
|
))
|
2021-01-25 18:45:48 +00:00
|
|
|
|
|
|
|
proc aggregateAttesters(
|
|
|
|
aggPK: var blscurve.PublicKey,
|
|
|
|
attestation: IndexedAttestation,
|
2021-05-21 09:23:28 +00:00
|
|
|
validators: seq[Validator],
|
2021-01-25 18:45:48 +00:00
|
|
|
): bool =
|
|
|
|
doAssert attestation.attesting_indices.len > 0
|
|
|
|
var attestersAgg{.noInit.}: AggregatePublicKey
|
2021-05-21 09:23:28 +00:00
|
|
|
attestersAgg.init(validators[attestation.attesting_indices[0]]
|
2021-04-09 12:59:24 +00:00
|
|
|
.pubkey.loadWithCacheOrExit(false))
|
2021-01-25 18:45:48 +00:00
|
|
|
for i in 1 ..< attestation.attesting_indices.len:
|
2021-05-21 09:23:28 +00:00
|
|
|
attestersAgg.aggregate(validators[attestation.attesting_indices[i]]
|
2021-04-09 12:59:24 +00:00
|
|
|
.pubkey.loadWithCacheOrExit(false))
|
2021-01-25 18:45:48 +00:00
|
|
|
aggPK.finish(attestersAgg)
|
|
|
|
return true
|
|
|
|
|
2021-04-02 14:36:43 +00:00
|
|
|
proc aggregateAttesters(
|
|
|
|
aggPK: var blscurve.PublicKey,
|
|
|
|
attestation: IndexedAttestation,
|
|
|
|
epochRef: auto
|
|
|
|
): bool =
|
|
|
|
mixin validator_keys
|
|
|
|
|
|
|
|
doAssert attestation.attesting_indices.len > 0
|
|
|
|
var attestersAgg{.noInit.}: AggregatePublicKey
|
|
|
|
attestersAgg.init(epochRef.validator_keys[attestation.attesting_indices[0]]
|
|
|
|
.pubkey.loadWithCacheOrExitFalse())
|
|
|
|
for i in 1 ..< attestation.attesting_indices.len:
|
|
|
|
attestersAgg.aggregate(epochRef.validator_keys[attestation.attesting_indices[i]]
|
|
|
|
.pubkey.loadWithCacheOrExitFalse())
|
|
|
|
aggPK.finish(attestersAgg)
|
|
|
|
return true
|
|
|
|
|
2021-01-25 18:45:48 +00:00
|
|
|
proc addIndexedAttestation(
|
|
|
|
sigs: var seq[SignatureSet],
|
|
|
|
attestation: IndexedAttestation,
|
2021-05-21 09:23:28 +00:00
|
|
|
state: StateData
|
2021-01-25 18:45:48 +00:00
|
|
|
): bool =
|
2021-04-02 14:36:43 +00:00
|
|
|
## Add an indexed attestation for batched BLS verification
|
|
|
|
## purposes
|
|
|
|
## This only verifies cryptography, checking that
|
|
|
|
## the indices are sorted and unique is not checked for example.
|
|
|
|
##
|
|
|
|
## Returns true if the indexed attestations was added to the batching buffer
|
|
|
|
## Returns false if saniy checks failed (non-empty, keys are valid)
|
2021-01-25 18:45:48 +00:00
|
|
|
if attestation.attesting_indices.len == 0:
|
|
|
|
# Aggregation spec requires non-empty collection
|
|
|
|
# - https://tools.ietf.org/html/draft-irtf-cfrg-bls-signature-04
|
|
|
|
# Eth2 spec requires at least one attesting indice in slashing
|
2021-02-25 13:37:22 +00:00
|
|
|
# - https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/beacon-chain.md#is_valid_indexed_attestation
|
2021-01-25 18:45:48 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
var aggPK {.noInit.}: blscurve.PublicKey
|
2021-05-21 09:23:28 +00:00
|
|
|
if not aggPK.aggregateAttesters(
|
|
|
|
attestation, getStateField(state, validators).asSeq):
|
2021-01-25 18:45:48 +00:00
|
|
|
return false
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.addSignatureSet(
|
2021-01-25 18:45:48 +00:00
|
|
|
aggPK,
|
|
|
|
attestation.data,
|
2021-04-26 20:39:44 +00:00
|
|
|
attestation.signature.loadOrExit(false),
|
2021-05-21 09:23:28 +00:00
|
|
|
getStateField(state, genesis_validators_root),
|
|
|
|
getStateField(state, fork),
|
2021-01-25 18:45:48 +00:00
|
|
|
attestation.data.target.epoch,
|
2021-04-26 20:39:44 +00:00
|
|
|
DOMAIN_BEACON_ATTESTER)
|
2021-01-25 18:45:48 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
proc addAttestation(
|
|
|
|
sigs: var seq[SignatureSet],
|
|
|
|
attestation: Attestation,
|
2021-05-21 09:23:28 +00:00
|
|
|
state: StateData,
|
2021-01-25 18:45:48 +00:00
|
|
|
cache: var StateCache
|
|
|
|
): bool =
|
2021-04-26 20:39:44 +00:00
|
|
|
var inited = false
|
2021-01-25 18:45:48 +00:00
|
|
|
var attestersAgg{.noInit.}: AggregatePublicKey
|
2021-05-21 09:23:28 +00:00
|
|
|
for valIndex in state.data.data.get_attesting_indices(
|
2021-01-25 18:45:48 +00:00
|
|
|
attestation.data,
|
|
|
|
attestation.aggregation_bits,
|
|
|
|
cache
|
|
|
|
):
|
2021-04-26 20:39:44 +00:00
|
|
|
if not inited: # first iteration
|
2021-05-21 09:23:28 +00:00
|
|
|
attestersAgg.init(getStateField(state, validators)[valIndex]
|
2021-04-09 12:59:24 +00:00
|
|
|
.pubkey.loadWithCacheOrExit(false))
|
2021-04-26 20:39:44 +00:00
|
|
|
inited = true
|
2021-01-25 18:45:48 +00:00
|
|
|
else:
|
2021-05-21 09:23:28 +00:00
|
|
|
attestersAgg.aggregate(getStateField(state, validators)[valIndex]
|
2021-04-09 12:59:24 +00:00
|
|
|
.pubkey.loadWithCacheOrExit(false))
|
2021-01-25 18:45:48 +00:00
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
if not inited:
|
2021-04-09 12:59:24 +00:00
|
|
|
# There were no attesters
|
2021-01-25 18:45:48 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
var attesters{.noinit.}: blscurve.PublicKey
|
|
|
|
attesters.finish(attestersAgg)
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.addSignatureSet(
|
2021-01-25 18:45:48 +00:00
|
|
|
attesters,
|
|
|
|
attestation.data,
|
2021-04-26 20:39:44 +00:00
|
|
|
attestation.signature.loadOrExit(false),
|
2021-05-21 09:23:28 +00:00
|
|
|
getStateField(state, genesis_validators_root),
|
|
|
|
getStateField(state, fork),
|
2021-01-25 18:45:48 +00:00
|
|
|
attestation.data.target.epoch,
|
2021-04-26 20:39:44 +00:00
|
|
|
DOMAIN_BEACON_ATTESTER)
|
|
|
|
|
|
|
|
true
|
2021-01-25 18:45:48 +00:00
|
|
|
|
2021-04-02 14:36:43 +00:00
|
|
|
# Public API
|
|
|
|
# ------------------------------------------------------
|
|
|
|
|
|
|
|
proc addAttestation*(
|
|
|
|
sigs: var seq[SignatureSet],
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
epochRef: auto,
|
|
|
|
attestation: Attestation
|
2021-04-26 20:39:44 +00:00
|
|
|
): Option[CookedSig] =
|
2021-04-02 14:36:43 +00:00
|
|
|
## Add an attestation for batched BLS verification
|
|
|
|
## purposes
|
|
|
|
## This only verifies cryptography
|
|
|
|
##
|
|
|
|
## Returns true if the attestation was added to the batching buffer
|
2021-04-09 12:59:24 +00:00
|
|
|
## Returns false if sanity checks failed (non-empty, keys are valid)
|
2021-04-02 14:36:43 +00:00
|
|
|
## In that case the seq[SignatureSet] is unmodified
|
|
|
|
mixin get_attesting_indices, validator_keys, pubkey
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
var inited = false
|
2021-04-02 14:36:43 +00:00
|
|
|
var attestersAgg{.noInit.}: AggregatePublicKey
|
|
|
|
for valIndex in epochRef.get_attesting_indices(
|
|
|
|
attestation.data,
|
|
|
|
attestation.aggregation_bits):
|
2021-04-26 20:39:44 +00:00
|
|
|
if not inited: # first iteration
|
2021-04-02 14:36:43 +00:00
|
|
|
attestersAgg.init(epochRef.validator_keys[valIndex]
|
2021-04-26 20:39:44 +00:00
|
|
|
.loadWithCacheOrExit(none(CookedSig)))
|
|
|
|
inited = true
|
2021-04-02 14:36:43 +00:00
|
|
|
else:
|
|
|
|
attestersAgg.aggregate(epochRef.validator_keys[valIndex]
|
2021-04-26 20:39:44 +00:00
|
|
|
.loadWithCacheOrExit(none(CookedSig)))
|
2021-04-02 14:36:43 +00:00
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
if not inited:
|
2021-04-09 12:59:24 +00:00
|
|
|
# There were no attesters
|
2021-04-26 20:39:44 +00:00
|
|
|
return none(CookedSig)
|
2021-04-02 14:36:43 +00:00
|
|
|
|
|
|
|
var attesters{.noinit.}: blscurve.PublicKey
|
|
|
|
attesters.finish(attestersAgg)
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
let cookedSig = attestation.signature.loadOrExit(none(CookedSig))
|
2021-04-09 12:59:24 +00:00
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.addSignatureSet(
|
2021-04-09 12:59:24 +00:00
|
|
|
attesters,
|
|
|
|
attestation.data,
|
|
|
|
cookedSig,
|
|
|
|
genesis_validators_root,
|
|
|
|
fork,
|
|
|
|
attestation.data.target.epoch,
|
2021-04-26 20:39:44 +00:00
|
|
|
DOMAIN_BEACON_ATTESTER)
|
2021-04-02 14:36:43 +00:00
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
some(CookedSig(cookedSig))
|
2021-04-02 14:36:43 +00:00
|
|
|
|
|
|
|
proc addSlotSignature*(
|
|
|
|
sigs: var seq[SignatureSet],
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
slot: Slot,
|
|
|
|
pubkey: ValidatorPubKey,
|
|
|
|
signature: ValidatorSig): bool =
|
|
|
|
let epoch = compute_epoch_at_slot(slot)
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.addSignatureSet(
|
2021-04-09 12:59:24 +00:00
|
|
|
pubkey.loadWithCacheOrExit(false),
|
2021-04-02 14:36:43 +00:00
|
|
|
sszObj = slot,
|
2021-04-26 20:39:44 +00:00
|
|
|
signature.loadOrExit(false),
|
2021-04-02 14:36:43 +00:00
|
|
|
genesis_validators_root,
|
|
|
|
fork,
|
|
|
|
epoch,
|
|
|
|
DOMAIN_SELECTION_PROOF
|
|
|
|
)
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
true
|
|
|
|
|
2021-04-02 14:36:43 +00:00
|
|
|
proc addAggregateAndProofSignature*(
|
|
|
|
sigs: var seq[SignatureSet],
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest,
|
|
|
|
aggregate_and_proof: AggregateAndProof,
|
|
|
|
pubkey: ValidatorPubKey,
|
|
|
|
signature: ValidatorSig
|
|
|
|
): bool =
|
|
|
|
|
|
|
|
let epoch = compute_epoch_at_slot(aggregate_and_proof.aggregate.data.slot)
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.addSignatureSet(
|
2021-04-09 12:59:24 +00:00
|
|
|
pubkey.loadWithCacheOrExit(false),
|
2021-04-02 14:36:43 +00:00
|
|
|
sszObj = aggregate_and_proof,
|
2021-04-26 20:39:44 +00:00
|
|
|
signature.loadOrExit(false),
|
2021-04-02 14:36:43 +00:00
|
|
|
genesis_validators_root,
|
|
|
|
fork,
|
|
|
|
epoch,
|
|
|
|
DOMAIN_AGGREGATE_AND_PROOF
|
|
|
|
)
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
true
|
|
|
|
|
2021-01-25 18:45:48 +00:00
|
|
|
proc collectSignatureSets*(
|
|
|
|
sigs: var seq[SignatureSet],
|
|
|
|
signed_block: SignedBeaconBlock,
|
2021-05-21 09:23:28 +00:00
|
|
|
state: StateData,
|
2021-01-25 18:45:48 +00:00
|
|
|
cache: var StateCache): bool =
|
|
|
|
## Collect all signatures in a single signed block.
|
|
|
|
## This includes
|
|
|
|
## - Block proposer
|
|
|
|
## - Randao Reaveal
|
|
|
|
## - Proposer slashings
|
|
|
|
## - Attester slashings
|
|
|
|
## - Attestations
|
|
|
|
## - VoluntaryExits
|
|
|
|
##
|
|
|
|
## We do not include deposits as they can be invalid per protocol
|
|
|
|
## (secp256k1 signature instead of BLS)
|
|
|
|
|
|
|
|
# Metadata
|
|
|
|
# ----------------------------------------------------
|
|
|
|
|
|
|
|
let
|
|
|
|
proposer_index = signed_block.message.proposer_index
|
2021-05-21 09:23:28 +00:00
|
|
|
if proposer_index >= getStateField(state, validators).lenu64:
|
2021-01-25 18:45:48 +00:00
|
|
|
return false
|
|
|
|
|
2021-05-21 09:23:28 +00:00
|
|
|
let pubkey = getStateField(state, validators)[proposer_index]
|
2021-04-09 12:59:24 +00:00
|
|
|
.pubkey.loadWithCacheOrExit(false)
|
2021-01-25 18:45:48 +00:00
|
|
|
let epoch = signed_block.message.slot.compute_epoch_at_slot()
|
|
|
|
|
|
|
|
# 1. Block proposer
|
|
|
|
# ----------------------------------------------------
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.addSignatureSet(
|
2021-01-25 18:45:48 +00:00
|
|
|
pubkey,
|
|
|
|
signed_block.message,
|
2021-04-26 20:39:44 +00:00
|
|
|
signed_block.signature.loadOrExit(false),
|
2021-05-21 09:23:28 +00:00
|
|
|
getStateField(state, genesis_validators_root),
|
|
|
|
getStateField(state, fork),
|
2021-01-25 18:45:48 +00:00
|
|
|
epoch,
|
2021-04-26 20:39:44 +00:00
|
|
|
DOMAIN_BEACON_PROPOSER)
|
2021-01-25 18:45:48 +00:00
|
|
|
|
|
|
|
# 2. Randao Reveal
|
|
|
|
# ----------------------------------------------------
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.addSignatureSet(
|
2021-01-25 18:45:48 +00:00
|
|
|
pubkey,
|
|
|
|
epoch,
|
2021-04-26 20:39:44 +00:00
|
|
|
signed_block.message.body.randao_reveal.loadOrExit(false),
|
2021-05-21 09:23:28 +00:00
|
|
|
getStateField(state, genesis_validators_root),
|
|
|
|
getStateField(state, fork),
|
2021-01-25 18:45:48 +00:00
|
|
|
epoch,
|
2021-04-26 20:39:44 +00:00
|
|
|
DOMAIN_RANDAO)
|
2021-01-25 18:45:48 +00:00
|
|
|
|
|
|
|
# 3. Proposer slashings
|
|
|
|
# ----------------------------------------------------
|
|
|
|
# Denial-of-service:
|
|
|
|
# SSZ deserialization guarantees that blocks received from random sources
|
|
|
|
# including peer or RPC
|
|
|
|
# have at most MAX_PROPOSER_SLASHINGS proposer slashings.
|
|
|
|
for i in 0 ..< signed_block.message.body.proposer_slashings.len:
|
|
|
|
# don't use "items" for iterating over large type
|
|
|
|
# due to https://github.com/nim-lang/Nim/issues/14421
|
|
|
|
# fixed in 1.4.2
|
|
|
|
|
|
|
|
# Alias
|
|
|
|
template slashing: untyped = signed_block.message.body.proposer_slashings[i]
|
|
|
|
|
|
|
|
# Proposed block 1
|
|
|
|
block:
|
|
|
|
let header_1 = slashing.signed_header_1
|
2021-05-21 09:23:28 +00:00
|
|
|
let proposer1 =
|
|
|
|
getStateField(state, validators)[header_1.message.proposer_index]
|
2021-01-25 18:45:48 +00:00
|
|
|
let epoch1 = header_1.message.slot.compute_epoch_at_slot()
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.addSignatureSet(
|
2021-04-09 12:59:24 +00:00
|
|
|
proposer1.pubkey.loadWithCacheOrExit(false),
|
2021-01-25 18:45:48 +00:00
|
|
|
header_1.message,
|
2021-04-26 20:39:44 +00:00
|
|
|
header_1.signature.loadOrExit(false),
|
2021-05-21 09:23:28 +00:00
|
|
|
getStateField(state, genesis_validators_root),
|
|
|
|
getStateField(state, fork),
|
2021-01-25 18:45:48 +00:00
|
|
|
epoch1,
|
|
|
|
DOMAIN_BEACON_PROPOSER
|
2021-04-26 20:39:44 +00:00
|
|
|
)
|
2021-01-25 18:45:48 +00:00
|
|
|
|
|
|
|
# Conflicting block 2
|
|
|
|
block:
|
|
|
|
let header_2 = slashing.signed_header_2
|
2021-05-21 09:23:28 +00:00
|
|
|
let proposer2 =
|
|
|
|
getStateField(state, validators)[header_2.message.proposer_index]
|
2021-01-25 18:45:48 +00:00
|
|
|
let epoch2 = header_2.message.slot.compute_epoch_at_slot()
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.addSignatureSet(
|
2021-04-09 12:59:24 +00:00
|
|
|
proposer2.pubkey.loadWithCacheOrExit(false),
|
2021-01-25 18:45:48 +00:00
|
|
|
header_2.message,
|
2021-04-26 20:39:44 +00:00
|
|
|
header_2.signature.loadOrExit(false),
|
2021-05-21 09:23:28 +00:00
|
|
|
getStateField(state, genesis_validators_root),
|
|
|
|
getStateField(state, fork),
|
2021-01-25 18:45:48 +00:00
|
|
|
epoch2,
|
|
|
|
DOMAIN_BEACON_PROPOSER
|
2021-04-26 20:39:44 +00:00
|
|
|
)
|
2021-01-25 18:45:48 +00:00
|
|
|
|
|
|
|
# 4. Attester slashings
|
|
|
|
# ----------------------------------------------------
|
|
|
|
# Denial-of-service:
|
|
|
|
# SSZ deserialization guarantees that blocks received from random sources
|
|
|
|
# including peer or RPC
|
|
|
|
# have at most MAX_ATTESTER_SLASHINGS attester slashings.
|
|
|
|
for i in 0 ..< signed_block.message.body.attester_slashings.len:
|
|
|
|
# don't use "items" for iterating over large type
|
|
|
|
# due to https://github.com/nim-lang/Nim/issues/14421
|
|
|
|
# fixed in 1.4.2
|
|
|
|
|
|
|
|
# Alias
|
|
|
|
template slashing: untyped = signed_block.message.body.attester_slashings[i]
|
|
|
|
|
|
|
|
# Attestation 1
|
|
|
|
if not sigs.addIndexedAttestation(
|
|
|
|
slashing.attestation_1,
|
|
|
|
state):
|
|
|
|
return false
|
|
|
|
|
|
|
|
# Conflicting attestation 2
|
|
|
|
if not sigs.addIndexedAttestation(
|
|
|
|
slashing.attestation_2,
|
|
|
|
state):
|
|
|
|
return false
|
|
|
|
|
|
|
|
# 5. Attestations
|
|
|
|
# ----------------------------------------------------
|
|
|
|
# Denial-of-service:
|
|
|
|
# SSZ deserialization guarantees that blocks received from random sources
|
|
|
|
# including peer or RPC
|
|
|
|
# have at most MAX_ATTESTATIONS attestations.
|
|
|
|
for i in 0 ..< signed_block.message.body.attestations.len:
|
|
|
|
# don't use "items" for iterating over large type
|
|
|
|
# due to https://github.com/nim-lang/Nim/issues/14421
|
|
|
|
# fixed in 1.4.2
|
|
|
|
if not sigs.addAttestation(
|
|
|
|
signed_block.message.body.attestations[i],
|
|
|
|
state, cache):
|
|
|
|
return false
|
|
|
|
|
|
|
|
# 6. VoluntaryExits
|
|
|
|
# ----------------------------------------------------
|
|
|
|
# Denial-of-service:
|
|
|
|
# SSZ deserialization guarantees that blocks received from random sources
|
|
|
|
# including peer or RPC
|
|
|
|
# have at most MAX_VOLUNTARY_EXITS voluntary exits.
|
|
|
|
for i in 0 ..< signed_block.message.body.voluntary_exits.len:
|
|
|
|
# don't use "items" for iterating over large type
|
|
|
|
# due to https://github.com/nim-lang/Nim/issues/14421
|
|
|
|
# fixed in 1.4.2
|
|
|
|
template volex: untyped = signed_block.message.body.voluntary_exits[i]
|
|
|
|
|
2021-04-26 20:39:44 +00:00
|
|
|
sigs.addSignatureSet(
|
2021-05-21 09:23:28 +00:00
|
|
|
getStateField(state, validators)[volex.message.validator_index]
|
2021-04-09 12:59:24 +00:00
|
|
|
.pubkey.loadWithCacheOrExit(false),
|
2021-01-25 18:45:48 +00:00
|
|
|
volex.message,
|
2021-04-26 20:39:44 +00:00
|
|
|
volex.signature.loadOrExit(false),
|
2021-05-21 09:23:28 +00:00
|
|
|
getStateField(state, genesis_validators_root),
|
|
|
|
getStateField(state, fork),
|
2021-01-25 18:45:48 +00:00
|
|
|
volex.message.epoch,
|
2021-04-26 20:39:44 +00:00
|
|
|
DOMAIN_VOLUNTARY_EXIT)
|
2021-01-25 18:45:48 +00:00
|
|
|
|
|
|
|
return true
|