remove false OnBlockAdded dependency on phase0 HashedBeaconState (#2661)
* remove false OnBlockAdded dependency on phase.HashedBeaconState * introduce altair data types into block_clearance; update some alpha.6 spec refs to alpha.7; add get_active_validator_indices_len ForkedHashedBeaconState wrapper * switch many modules from using datatypes (with phase0 states/blocks) to datatypes/base (fork-independent); update spec refs from alpha.6 to alpha.7 and remove rm'd G2_POINT_AT_INFINITY * switch more modules from using datatypes (with phase0 states/blocks) to datatypes/base (fork-independent); update spec refs from alpha.6 to alpha.7 * remove unnecessary phase0-only wrapper of get_attesting_indices(); allow signatures_batch to process either fork; remove O(n^2) nested loop in process_inactivity_updates(); add altair support to getAttestationsforTestBlock() * add Altair versions of asSigVerified(), asTrusted(), and makeBeaconBlock() * fix spec URL to be Altair for Altair makeBeaconBlock()
This commit is contained in:
parent
f8eb906b51
commit
b1d5609171
|
@ -20,14 +20,14 @@ import
|
|||
./networking/eth2_network,
|
||||
./eth1/eth1_monitor,
|
||||
./consensus_object_pools/[blockchain_dag, block_quarantine, attestation_pool],
|
||||
./spec/datatypes,
|
||||
./spec/datatypes/base,
|
||||
./sync/[sync_manager, request_manager]
|
||||
|
||||
export
|
||||
osproc, chronos, httpserver, conf, beacon_clock, beacon_chain_db,
|
||||
attestation_pool, eth2_network, beacon_node_types, eth1_monitor,
|
||||
request_manager, sync_manager, eth2_processor, blockchain_dag, block_quarantine,
|
||||
datatypes
|
||||
base
|
||||
|
||||
type
|
||||
RpcServer* = RpcHttpServer
|
||||
|
|
|
@ -10,7 +10,8 @@
|
|||
import
|
||||
std/[deques, intsets, streams, tables],
|
||||
stew/endians2,
|
||||
./spec/[datatypes, digest, crypto],
|
||||
./spec/[digest, crypto],
|
||||
./spec/datatypes/base,
|
||||
./consensus_object_pools/block_pools_types,
|
||||
./fork_choice/fork_choice_types,
|
||||
./validators/slashing_protection
|
||||
|
|
|
@ -17,7 +17,8 @@ import
|
|||
eth/p2p/discoveryv5/enr,
|
||||
json_serialization, web3/[ethtypes, confutils_defs],
|
||||
|
||||
./spec/[crypto, keystore, digest, datatypes, network],
|
||||
./spec/[crypto, keystore, digest, network],
|
||||
./spec/datatypes/base,
|
||||
./networking/network_metadata,
|
||||
./validators/slashing_protection_common,
|
||||
./filepath
|
||||
|
|
|
@ -18,7 +18,7 @@ import
|
|||
beaconstate, crypto, digest, forkedbeaconstate_helpers,
|
||||
validator],
|
||||
../spec/datatypes/[phase0, altair],
|
||||
../ssz/merkleization,
|
||||
../ssz/[merkleization, types],
|
||||
"."/[spec_cache, blockchain_dag, block_quarantine],
|
||||
".."/[beacon_clock, beacon_node_types, extras],
|
||||
../fork_choice/fork_choice
|
||||
|
@ -312,7 +312,7 @@ proc addAttestation*(pool: var AttestationPool,
|
|||
proc addForkChoice*(pool: var AttestationPool,
|
||||
epochRef: EpochRef,
|
||||
blckRef: BlockRef,
|
||||
blck: phase0.TrustedBeaconBlock,
|
||||
blck: phase0.TrustedBeaconBlock | altair.TrustedBeaconBlock,
|
||||
wallSlot: Slot) =
|
||||
## Add a verified block to the fork choice context
|
||||
let state = pool.forkChoice.process_block(
|
||||
|
@ -401,7 +401,10 @@ func init(
|
|||
for slot_committee_index in 0'u64 ..< get_committee_count_per_slot(
|
||||
state.data, epoch, cache):
|
||||
var
|
||||
validator_bits: CommitteeValidatorsBits
|
||||
validator_bits =
|
||||
CommitteeValidatorsBits.init(
|
||||
get_beacon_committee_len(
|
||||
state.data, slot, slot_committee_index.CommitteeIndex, cache).int)
|
||||
i = 0
|
||||
for index in get_beacon_committee(
|
||||
state.data, slot, slot_committee_index.CommitteeIndex, cache):
|
||||
|
@ -447,7 +450,7 @@ proc score(
|
|||
bitsScore
|
||||
|
||||
proc getAttestationsForBlock*(pool: var AttestationPool,
|
||||
state: phase0.HashedBeaconState,
|
||||
state: SomeHashedBeaconState,
|
||||
cache: var StateCache): seq[Attestation] =
|
||||
## Retrieve attestations that may be added to a new block at the slot of the
|
||||
## given state
|
||||
|
@ -466,7 +469,13 @@ proc getAttestationsForBlock*(pool: var AttestationPool,
|
|||
var
|
||||
candidates: seq[tuple[
|
||||
score: int, slot: Slot, entry: ptr AttestationEntry, validation: int]]
|
||||
attCache = AttestationCache.init(state)
|
||||
attCache =
|
||||
when state is phase0.HashedBeaconState:
|
||||
AttestationCache.init(state)
|
||||
elif state is altair.HashedBeaconState:
|
||||
AttestationCache.init(state, cache)
|
||||
else:
|
||||
static: doAssert false
|
||||
|
||||
for i in 0..<ATTESTATION_LOOKBACK:
|
||||
if i > maxAttestationSlot: # Around genesis..
|
||||
|
@ -518,8 +527,11 @@ proc getAttestationsForBlock*(pool: var AttestationPool,
|
|||
var
|
||||
prevEpoch = state.data.get_previous_epoch()
|
||||
prevEpochSpace =
|
||||
state.data.previous_epoch_attestations.maxLen -
|
||||
state.data.previous_epoch_attestations.len()
|
||||
when state is altair.HashedBeaconState:
|
||||
MAX_ATTESTATIONS
|
||||
else:
|
||||
state.data.previous_epoch_attestations.maxLen -
|
||||
state.data.previous_epoch_attestations.len()
|
||||
|
||||
var res: seq[Attestation]
|
||||
let totalCandidates = candidates.len()
|
||||
|
|
|
@ -14,8 +14,9 @@ import
|
|||
eth/keys,
|
||||
../extras, ../beacon_clock,
|
||||
../spec/[
|
||||
crypto, datatypes, digest, forkedbeaconstate_helpers, helpers, signatures,
|
||||
crypto, digest, forkedbeaconstate_helpers, helpers, signatures,
|
||||
signatures_batch, state_transition],
|
||||
../spec/datatypes/[phase0, altair],
|
||||
./block_pools_types, ./blockchain_dag, ./block_quarantine
|
||||
|
||||
from libp2p/protocols/pubsub/pubsub import ValidationResult
|
||||
|
@ -32,35 +33,49 @@ export results, ValidationResult
|
|||
logScope:
|
||||
topics = "clearance"
|
||||
|
||||
template asSigVerified(x: SignedBeaconBlock): SigVerifiedSignedBeaconBlock =
|
||||
## At the GC-level, the GC is type-agnostic; it's all type-erased so
|
||||
## casting between seq[Attestation] and seq[TrustedAttestation] will
|
||||
## not disrupt GC operations.
|
||||
##
|
||||
## These SHOULD be used in function calls to avoid expensive temporary.
|
||||
## see https://github.com/status-im/nimbus-eth2/pull/2250#discussion_r562010679
|
||||
template asSigVerified(x: phase0.SignedBeaconBlock):
|
||||
phase0.SigVerifiedSignedBeaconBlock =
|
||||
## This converts a signed beacon block to a sig verified beacon clock.
|
||||
## This assumes that their bytes representation is the same.
|
||||
##
|
||||
## At the GC-level, the GC is type-agnostic it's all type erased so
|
||||
## casting between seq[Attestation] and seq[TrustedAttestation]
|
||||
## will not disrupt GC operations.
|
||||
##
|
||||
## This SHOULD be used in function calls to avoid expensive temporary.
|
||||
## see https://github.com/status-im/nimbus-eth2/pull/2250#discussion_r562010679
|
||||
static: # TODO See isomorphicCast
|
||||
doAssert sizeof(SignedBeaconBlock) == sizeof(SigVerifiedSignedBeaconBlock)
|
||||
doAssert sizeof(phase0.SignedBeaconBlock) ==
|
||||
sizeof(phase0.SigVerifiedSignedBeaconBlock)
|
||||
|
||||
cast[ptr SigVerifiedSignedBeaconBlock](signedBlock.unsafeAddr)[]
|
||||
cast[ptr phase0.SigVerifiedSignedBeaconBlock](signedBlock.unsafeAddr)[]
|
||||
|
||||
template asTrusted(x: SignedBeaconBlock or SigVerifiedBeaconBlock): TrustedSignedBeaconBlock =
|
||||
template asSigVerified(x: altair.SignedBeaconBlock):
|
||||
altair.SigVerifiedSignedBeaconBlock =
|
||||
## This converts a signed beacon block to a sig verified beacon clock.
|
||||
## This assumes that their bytes representation is the same.
|
||||
static: # TODO See isomorphicCast
|
||||
doAssert sizeof(altair.SignedBeaconBlock) ==
|
||||
sizeof(altair.SigVerifiedSignedBeaconBlock)
|
||||
|
||||
cast[ptr altair.SigVerifiedSignedBeaconBlock](signedBlock.unsafeAddr)[]
|
||||
|
||||
template asTrusted(x: phase0.SignedBeaconBlock or phase0.SigVerifiedBeaconBlock):
|
||||
phase0.TrustedSignedBeaconBlock =
|
||||
## This converts a sigverified beacon block to a trusted beacon clock.
|
||||
## This assumes that their bytes representation is the same.
|
||||
##
|
||||
## At the GC-level, the GC is type-agnostic it's all type erased so
|
||||
## casting between seq[Attestation] and seq[TrustedAttestation]
|
||||
## will not disrupt GC operations.
|
||||
##
|
||||
## This SHOULD be used in function calls to avoid expensive temporary.
|
||||
## see https://github.com/status-im/nimbus-eth2/pull/2250#discussion_r562010679
|
||||
static: # TODO See isomorphicCast
|
||||
doAssert sizeof(x) == sizeof(TrustedSignedBeaconBlock)
|
||||
doAssert sizeof(x) == sizeof(phase0.TrustedSignedBeaconBlock)
|
||||
|
||||
cast[ptr TrustedSignedBeaconBlock](signedBlock.unsafeAddr)[]
|
||||
cast[ptr phase0.TrustedSignedBeaconBlock](signedBlock.unsafeAddr)[]
|
||||
|
||||
template asTrusted(x: altair.SignedBeaconBlock or altair.SigVerifiedBeaconBlock):
|
||||
altair.TrustedSignedBeaconBlock =
|
||||
## This converts a sigverified beacon block to a trusted beacon clock.
|
||||
## This assumes that their bytes representation is the same.
|
||||
static: # TODO See isomorphicCast
|
||||
doAssert sizeof(x) == sizeof(altair.TrustedSignedBeaconBlock)
|
||||
|
||||
cast[ptr altair.TrustedSignedBeaconBlock](signedBlock.unsafeAddr)[]
|
||||
|
||||
func batchVerify(quarantine: QuarantineRef, sigs: openArray[SignatureSet]): bool =
|
||||
var secureRandomBytes: array[32, byte]
|
||||
|
@ -71,12 +86,12 @@ func batchVerify(quarantine: QuarantineRef, sigs: openArray[SignatureSet]): bool
|
|||
|
||||
proc addRawBlock*(
|
||||
dag: ChainDAGRef, quarantine: QuarantineRef,
|
||||
signedBlock: SignedBeaconBlock, onBlockAdded: OnBlockAdded
|
||||
signedBlock: phase0.SignedBeaconBlock, onBlockAdded: OnBlockAdded
|
||||
): Result[BlockRef, (ValidationResult, BlockError)] {.gcsafe.}
|
||||
|
||||
proc addResolvedBlock(
|
||||
dag: ChainDAGRef, quarantine: QuarantineRef,
|
||||
state: var StateData, trustedBlock: TrustedSignedBeaconBlock,
|
||||
state: var StateData, trustedBlock: phase0.TrustedSignedBeaconBlock,
|
||||
parent: BlockRef, cache: var StateCache,
|
||||
onBlockAdded: OnBlockAdded, stateDataDur, sigVerifyDur,
|
||||
stateVerifyDur: Duration
|
||||
|
@ -136,7 +151,7 @@ proc addResolvedBlock(
|
|||
# Notify others of the new block before processing the quarantine, such that
|
||||
# notifications for parents happens before those of the children
|
||||
if onBlockAdded != nil:
|
||||
onBlockAdded(blockRef, trustedBlock, epochRef, state.data.hbsPhase0)
|
||||
onBlockAdded(blockRef, trustedBlock, epochRef)
|
||||
|
||||
# Now that we have the new block, we should see if any of the previously
|
||||
# unresolved blocks magically become resolved
|
||||
|
@ -151,7 +166,7 @@ proc addResolvedBlock(
|
|||
var entries = 0
|
||||
while entries != quarantine.orphans.len:
|
||||
entries = quarantine.orphans.len # keep going while quarantine is shrinking
|
||||
var resolved: seq[SignedBeaconBlock]
|
||||
var resolved: seq[phase0.SignedBeaconBlock]
|
||||
for _, v in quarantine.orphans:
|
||||
if v.message.parent_root in dag:
|
||||
resolved.add(v)
|
||||
|
@ -159,8 +174,13 @@ proc addResolvedBlock(
|
|||
for v in resolved:
|
||||
discard addRawBlock(dag, quarantine, v, onBlockAdded)
|
||||
|
||||
# TODO workaround for https://github.com/nim-lang/Nim/issues/18095
|
||||
# copy of phase0.SomeSignedBeaconBlock from datatypes/phase0.nim
|
||||
type SomeSignedPhase0Block =
|
||||
phase0.SignedBeaconBlock | phase0.SigVerifiedSignedBeaconBlock |
|
||||
phase0.TrustedSignedBeaconBlock
|
||||
proc checkStateTransition(
|
||||
dag: ChainDAGRef, signedBlock: SomeSignedBeaconBlock,
|
||||
dag: ChainDAGRef, signedBlock: SomeSignedPhase0Block,
|
||||
cache: var StateCache): (ValidationResult, BlockError) =
|
||||
## Ensure block can be applied on a state
|
||||
func restore(v: var ForkedHashedBeaconState) =
|
||||
|
@ -205,7 +225,7 @@ proc advanceClearanceState*(dag: ChainDagRef) =
|
|||
|
||||
proc addRawBlockKnownParent(
|
||||
dag: ChainDAGRef, quarantine: QuarantineRef,
|
||||
signedBlock: SignedBeaconBlock,
|
||||
signedBlock: phase0.SignedBeaconBlock,
|
||||
parent: BlockRef,
|
||||
onBlockAdded: OnBlockAdded
|
||||
): Result[BlockRef, (ValidationResult, BlockError)] =
|
||||
|
@ -283,7 +303,7 @@ proc addRawBlockKnownParent(
|
|||
proc addRawBlockUnresolved(
|
||||
dag: ChainDAGRef,
|
||||
quarantine: QuarantineRef,
|
||||
signedBlock: SignedBeaconBlock
|
||||
signedBlock: phase0.SignedBeaconBlock
|
||||
): Result[BlockRef, (ValidationResult, BlockError)] =
|
||||
## addRawBlock - Block is unresolved / has no parent
|
||||
|
||||
|
@ -321,7 +341,7 @@ proc addRawBlockUnresolved(
|
|||
|
||||
proc addRawBlock(
|
||||
dag: ChainDAGRef, quarantine: QuarantineRef,
|
||||
signedBlock: SignedBeaconBlock,
|
||||
signedBlock: phase0.SignedBeaconBlock,
|
||||
onBlockAdded: OnBlockAdded
|
||||
): Result[BlockRef, (ValidationResult, BlockError)] =
|
||||
## Try adding a block to the chain, verifying first that it passes the state
|
||||
|
|
|
@ -220,7 +220,7 @@ type
|
|||
|
||||
OnBlockAdded* = proc(
|
||||
blckRef: BlockRef, blck: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState) {.gcsafe, raises: [Defect].}
|
||||
epochRef: EpochRef) {.gcsafe, raises: [Defect].}
|
||||
|
||||
template head*(dag: ChainDagRef): BlockRef = dag.headState.blck
|
||||
|
||||
|
|
|
@ -13,7 +13,8 @@ import
|
|||
# Status libraries
|
||||
chronicles,
|
||||
# Internal
|
||||
../spec/[crypto, datatypes, forkedbeaconstate_helpers, helpers],
|
||||
../spec/[crypto, forkedbeaconstate_helpers, helpers],
|
||||
../spec/datatypes/base,
|
||||
"."/[blockchain_dag, block_quarantine],
|
||||
../beacon_node_types
|
||||
|
||||
|
|
|
@ -10,9 +10,8 @@
|
|||
import
|
||||
std/[intsets],
|
||||
chronicles,
|
||||
../spec/[
|
||||
crypto, datatypes, digest, helpers, network, presets, signatures,
|
||||
validator],
|
||||
../spec/[crypto, digest, helpers, network, presets, signatures, validator],
|
||||
../spec/datatypes/base,
|
||||
../extras,
|
||||
./block_pools_types, ./blockchain_dag
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ import
|
|||
../networking/network_metadata,
|
||||
web3, web3/confutils_defs, eth/keys, eth/p2p/discoveryv5/random2,
|
||||
stew/io2,
|
||||
../spec/[datatypes, crypto, presets], ../ssz/merkleization,
|
||||
../spec/[crypto, presets], ../spec/datatypes/base, ../ssz/merkleization,
|
||||
../validators/keystore_management
|
||||
|
||||
# Compiled version of /scripts/depositContract.v.py in this repo
|
||||
|
|
|
@ -15,7 +15,8 @@ import
|
|||
web3, web3/ethtypes as web3Types, web3/ethhexstrings, eth/common/eth_types,
|
||||
eth/async_utils, stew/byteutils,
|
||||
# Local modules:
|
||||
../spec/[datatypes, digest, crypto, forkedbeaconstate_helpers, helpers],
|
||||
../spec/[digest, crypto, forkedbeaconstate_helpers, helpers],
|
||||
../spec/datatypes/base,
|
||||
../networking/network_metadata,
|
||||
../consensus_object_pools/block_pools_types,
|
||||
../ssz,
|
||||
|
|
|
@ -16,7 +16,8 @@ import
|
|||
sequtils,
|
||||
stew/endians2,
|
||||
# Specs
|
||||
../../beacon_chain/spec/[datatypes, digest],
|
||||
../../beacon_chain/spec/digest,
|
||||
../../beacon_chain/spec/datatypes/base,
|
||||
../../beacon_chain/ssz/merkleization
|
||||
|
||||
const depositContractLimit* = Limit(1'u64 shl DEPOSIT_CONTRACT_TREE_DEPTH)
|
||||
|
|
|
@ -13,7 +13,8 @@ import
|
|||
# Status libraries
|
||||
stew/results, chronicles,
|
||||
# Internal
|
||||
../spec/[beaconstate, datatypes, digest, helpers],
|
||||
../spec/[beaconstate, digest, helpers],
|
||||
../spec/datatypes/[phase0, altair],
|
||||
# Fork choice
|
||||
./fork_choice_types, ./proto_array,
|
||||
../consensus_object_pools/[spec_cache, blockchain_dag]
|
||||
|
@ -281,11 +282,20 @@ proc process_block*(self: var ForkChoiceBackend,
|
|||
self.proto_array.onBlock(
|
||||
block_root, parent_root, justified_epoch, finalized_epoch)
|
||||
|
||||
# TODO workaround for https://github.com/nim-lang/Nim/issues/18095
|
||||
# it expresses as much of:
|
||||
# blck: phase0.SomeBeaconBlock | altair.SomeBeaconBlock
|
||||
# or
|
||||
# blck: SomeSomeBeaconBlock
|
||||
# as comes up. Other types can be added as needed.
|
||||
type ReallyAnyBeaconBlock =
|
||||
phase0.BeaconBlock | altair.BeaconBlock |
|
||||
phase0.TrustedBeaconBlock | altair.TrustedBeaconBlock
|
||||
proc process_block*(self: var ForkChoice,
|
||||
dag: ChainDAGRef,
|
||||
epochRef: EpochRef,
|
||||
blckRef: BlockRef,
|
||||
blck: SomeBeaconBlock,
|
||||
blck: ReallyAnyBeaconBlock,
|
||||
wallSlot: Slot): FcResult[void] =
|
||||
? update_time(self, dag, wallSlot)
|
||||
? process_state(self.checkpoints, dag, epochRef, blckRef)
|
||||
|
|
|
@ -15,7 +15,8 @@ import
|
|||
|
||||
chronicles,
|
||||
# Internal
|
||||
../spec/[datatypes, digest],
|
||||
../spec/datatypes/base,
|
||||
../spec/digest,
|
||||
../consensus_object_pools/block_pools_types
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v0.11.1/specs/phase0/fork-choice.md
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# beacon_chain
|
||||
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
||||
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
||||
# 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).
|
||||
|
@ -14,7 +14,8 @@ import
|
|||
chronicles,
|
||||
stew/results,
|
||||
# Internal
|
||||
../spec/[datatypes, digest],
|
||||
../spec/datatypes/base,
|
||||
../spec/digest,
|
||||
# Fork choice
|
||||
./fork_choice_types
|
||||
|
||||
|
|
|
@ -13,8 +13,8 @@ import
|
|||
stew/results,
|
||||
eth/keys,
|
||||
# Internals
|
||||
../spec/[
|
||||
datatypes, crypto, digest, helpers, signatures_batch],
|
||||
../spec/[crypto, digest, helpers, signatures_batch],
|
||||
../spec/datatypes/base,
|
||||
../consensus_object_pools/[
|
||||
blockchain_dag, block_quarantine,
|
||||
attestation_pool, exit_pool,
|
||||
|
|
|
@ -153,7 +153,7 @@ proc storeBlock(
|
|||
let blck = self.consensusManager.dag.addRawBlock(
|
||||
self.consensusManager.quarantine, signedBlock) do (
|
||||
blckRef: BlockRef, trustedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
attestationPool[].addForkChoice(
|
||||
epochRef, blckRef, trustedBlock.message, wallSlot)
|
||||
|
|
|
@ -11,7 +11,8 @@ import
|
|||
std/tables,
|
||||
stew/results,
|
||||
chronicles, chronos, metrics,
|
||||
../spec/[crypto, datatypes, digest],
|
||||
../spec/[crypto, digest],
|
||||
../spec/datatypes/base,
|
||||
../consensus_object_pools/[block_clearance, blockchain_dag, exit_pool, attestation_pool],
|
||||
./gossip_validation, ./block_processor,
|
||||
./batch_validation,
|
||||
|
|
|
@ -10,7 +10,8 @@
|
|||
import
|
||||
stew/endians2, stint,
|
||||
./extras, ./ssz/merkleization,
|
||||
spec/[crypto, datatypes, digest, keystore, signatures]
|
||||
spec/[crypto, digest, keystore, signatures],
|
||||
spec/datatypes/base
|
||||
|
||||
func get_eth1data_stub*(deposit_count: uint64, current_epoch: Epoch): Eth1Data =
|
||||
# https://github.com/ethereum/eth2.0-pm/blob/e596c70a19e22c7def4fd3519e20ae4022349390/interop/mocked_eth1data/README.md
|
||||
|
|
|
@ -35,7 +35,8 @@ import
|
|||
".."/[
|
||||
version, conf,
|
||||
ssz/ssz_serialization, beacon_clock],
|
||||
../spec/[datatypes, digest, network],
|
||||
../spec/datatypes/base,
|
||||
../spec/[digest, network],
|
||||
../validators/keystore_management,
|
||||
./eth2_discovery, ./peer_pool, ./libp2p_json_serialization
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ import
|
|||
stew/io2,
|
||||
|
||||
# Local modules
|
||||
./spec/[datatypes, crypto, helpers], beacon_clock, filepath,
|
||||
./spec/[crypto, helpers], ./spec/datatypes/base, beacon_clock, filepath,
|
||||
./networking/eth2_network
|
||||
|
||||
when defined(posix):
|
||||
|
|
|
@ -649,7 +649,7 @@ func get_total_active_balance*(state: SomeBeaconState, cache: var StateCache): G
|
|||
get_total_balance(
|
||||
state, cache.get_shuffled_active_validator_indices(state, epoch))
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#get_base_reward_per_increment
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#get_base_reward_per_increment
|
||||
func get_base_reward_per_increment*(state: altair.BeaconState, cache: var StateCache): Gwei =
|
||||
EFFECTIVE_BALANCE_INCREMENT * BASE_REWARD_FACTOR div
|
||||
integer_squareroot(get_total_active_balance(state, cache))
|
||||
|
@ -766,7 +766,7 @@ proc process_attestation*(
|
|||
|
||||
ok()
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#get_next_sync_committee_indices
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#get_next_sync_committee_indices
|
||||
func get_next_sync_committee_indices(state: altair.BeaconState):
|
||||
seq[ValidatorIndex] =
|
||||
## Return the sequence of sync committee indices (which may include
|
||||
|
@ -774,9 +774,6 @@ func get_next_sync_committee_indices(state: altair.BeaconState):
|
|||
## sync committee period boundary.
|
||||
|
||||
# TODO this size is known statically, so return array[] if possible
|
||||
|
||||
# Note: Committee can contain duplicate indices for small validator sets
|
||||
# (< SYNC_COMMITTEE_SIZE + 128)
|
||||
let epoch = get_current_epoch(state) + 1
|
||||
|
||||
const MAX_RANDOM_BYTE = 255
|
||||
|
|
|
@ -49,15 +49,15 @@ const
|
|||
PARTICIPATION_FLAG_WEIGHTS* =
|
||||
[TIMELY_SOURCE_WEIGHT, TIMELY_TARGET_WEIGHT, TIMELY_HEAD_WEIGHT]
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/validator.md#misc
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/validator.md#misc
|
||||
TARGET_AGGREGATORS_PER_SYNC_SUBCOMMITTEE* = 4
|
||||
SYNC_COMMITTEE_SUBNET_COUNT* = 4
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/setup.py#L470
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/setup.py#L473
|
||||
FINALIZED_ROOT_INDEX* = 105'u16
|
||||
NEXT_SYNC_COMMITTEE_INDEX* = 55'u16
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#participation-flag-indices
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#participation-flag-indices
|
||||
TIMELY_SOURCE_FLAG_INDEX* = 0
|
||||
TIMELY_TARGET_FLAG_INDEX* = 1
|
||||
TIMELY_HEAD_FLAG_INDEX* = 2
|
||||
|
@ -71,16 +71,6 @@ static: doAssert TIMELY_SOURCE_WEIGHT + TIMELY_TARGET_WEIGHT +
|
|||
TIMELY_HEAD_WEIGHT + SYNC_REWARD_WEIGHT + PROPOSER_WEIGHT ==
|
||||
WEIGHT_DENOMINATOR
|
||||
|
||||
let
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#misc
|
||||
# Cannot be computed at compile-time due to importc dependency
|
||||
G2_POINT_AT_INFINITY* = ValidatorSig.fromRaw([
|
||||
0xc0'u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0])
|
||||
|
||||
type
|
||||
### New types
|
||||
|
||||
|
@ -93,7 +83,7 @@ type
|
|||
sync_committee_bits*: BitArray[SYNC_COMMITTEE_SIZE]
|
||||
sync_committee_signature*: ValidatorSig
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#synccommittee
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#synccommittee
|
||||
SyncCommittee* = object
|
||||
pubkeys*: HashArray[Limit SYNC_COMMITTEE_SIZE, ValidatorPubKey]
|
||||
aggregate_pubkey*: ValidatorPubKey
|
||||
|
@ -132,13 +122,13 @@ type
|
|||
signature*: ValidatorSig ##\
|
||||
## Signature by the validator(s) over the block root of `slot`
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/validator.md#contributionandproof
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/validator.md#contributionandproof
|
||||
ContributionAndProof* = object
|
||||
aggregator_index*: uint64
|
||||
contribution*: SyncCommitteeContribution
|
||||
selection_proof*: ValidatorSig
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/validator.md#signedcontributionandproof
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/validator.md#signedcontributionandproof
|
||||
SignedContributionAndProof* = object
|
||||
message*: ContributionAndProof
|
||||
signature*: ValidatorSig
|
||||
|
@ -150,7 +140,7 @@ type
|
|||
|
||||
### Modified/overloaded
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/sync-protocol.md#lightclientsnapshot
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/sync-protocol.md#lightclientsnapshot
|
||||
LightClientSnapshot* = object
|
||||
header*: BeaconBlockHeader ##\
|
||||
## Beacon block header
|
||||
|
@ -160,7 +150,7 @@ type
|
|||
|
||||
next_sync_committee*: SyncCommittee
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/sync-protocol.md#lightclientupdate
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/sync-protocol.md#lightclientupdate
|
||||
LightClientUpdate* = object
|
||||
header*: BeaconBlockHeader ##\
|
||||
## Update beacon block header
|
||||
|
@ -318,11 +308,14 @@ type
|
|||
state_root*: Eth2Digest ##\
|
||||
body*: TrustedBeaconBlockBody
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#beaconblockbody
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#beaconblockbody
|
||||
BeaconBlockBody* = object
|
||||
randao_reveal*: ValidatorSig
|
||||
eth1_data*: Eth1Data
|
||||
graffiti*: GraffitiBytes
|
||||
eth1_data*: Eth1Data ##\
|
||||
## Eth1 data vote
|
||||
|
||||
graffiti*: GraffitiBytes ##\
|
||||
## Arbitrary data
|
||||
|
||||
# Operations
|
||||
proposer_slashings*: List[ProposerSlashing, Limit MAX_PROPOSER_SLASHINGS]
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import
|
||||
".."/[datatypes, digest, crypto]
|
||||
".."/datatypes/base,
|
||||
".."/[digest, crypto]
|
||||
|
||||
export datatypes, digest, crypto
|
||||
export base, crypto, digest
|
||||
|
||||
type
|
||||
AttesterDuties* = tuple
|
||||
|
|
|
@ -63,6 +63,14 @@ template hash_tree_root*(x: ForkedHashedBeaconState): Eth2Digest =
|
|||
of forkPhase0: hash_tree_root(x.hbsPhase0.data)
|
||||
of forkAltair: hash_tree_root(x.hbsAltair.data)
|
||||
|
||||
func get_active_validator_indices_len*(
|
||||
state: ForkedHashedBeaconState; epoch: Epoch): uint64 =
|
||||
case state.beaconStateFork:
|
||||
of forkPhase0:
|
||||
get_active_validator_indices_len(state.hbsPhase0.data, epoch)
|
||||
of forkAltair:
|
||||
get_active_validator_indices_len(state.hbsAltair.data, epoch)
|
||||
|
||||
func get_beacon_committee*(
|
||||
state: ForkedHashedBeaconState, slot: Slot, index: CommitteeIndex,
|
||||
cache: var StateCache): seq[ValidatorIndex] =
|
||||
|
@ -74,7 +82,7 @@ func get_beacon_committee*(
|
|||
case state.beaconStateFork:
|
||||
of forkPhase0: get_beacon_committee(state.hbsPhase0.data, slot, index, cache)
|
||||
of forkAltair: get_beacon_committee(state.hbsAltair.data, slot, index, cache)
|
||||
|
||||
|
||||
func get_committee_count_per_slot*(state: ForkedHashedBeaconState,
|
||||
epoch: Epoch,
|
||||
cache: var StateCache): uint64 =
|
||||
|
@ -116,11 +124,6 @@ proc get_attesting_indices*(state: ForkedHashedBeaconState;
|
|||
|
||||
var idxBuf: seq[ValidatorIndex]
|
||||
|
||||
doAssert state.beaconStateFork == forkPhase0
|
||||
for vidx in state.hbsPhase0.data.get_attesting_indices(data, bits, cache):
|
||||
idxBuf.add vidx
|
||||
if true: return idxBuf
|
||||
|
||||
if state.beaconStateFork == forkPhase0:
|
||||
for vidx in state.hbsPhase0.data.get_attesting_indices(data, bits, cache):
|
||||
idxBuf.add vidx
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# beacon_chain
|
||||
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
||||
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
||||
# 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).
|
||||
|
@ -18,7 +18,7 @@ import
|
|||
nimcrypto/[sha2, rijndael, pbkdf2, bcmode, hash, scrypt],
|
||||
# Local modules
|
||||
libp2p/crypto/crypto as lcrypto,
|
||||
./datatypes, ./crypto, ./digest, ./signatures
|
||||
./datatypes/base, ./crypto, ./digest, ./signatures
|
||||
|
||||
# We use `ncrutils` for constant-time hexadecimal encoding/decoding procedures.
|
||||
import nimcrypto/utils as ncrutils
|
||||
|
|
|
@ -8,7 +8,8 @@
|
|||
{.push raises: [Defect].}
|
||||
|
||||
import
|
||||
"."/[datatypes, digest, helpers]
|
||||
"."/[digest, helpers],
|
||||
"."/datatypes/base
|
||||
|
||||
const
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/p2p-interface.md#topics-and-messages
|
||||
|
|
|
@ -14,8 +14,9 @@ import
|
|||
# Internal
|
||||
../ssz/merkleization,
|
||||
"."/[
|
||||
crypto, datatypes, helpers, presets, beaconstate, digest,
|
||||
forkedbeaconstate_helpers]
|
||||
crypto, helpers, presets, beaconstate, digest,
|
||||
forkedbeaconstate_helpers],
|
||||
"."/datatypes/[altair, phase0]
|
||||
|
||||
# Otherwise, error.
|
||||
import chronicles
|
||||
|
@ -252,7 +253,7 @@ proc addAggregateAndProofSignature*(
|
|||
|
||||
proc collectSignatureSets*(
|
||||
sigs: var seq[SignatureSet],
|
||||
signed_block: SignedBeaconBlock,
|
||||
signed_block: phase0.SignedBeaconBlock | altair.SignedBeaconBlock,
|
||||
validatorKeys: auto,
|
||||
state: ForkedHashedBeaconState,
|
||||
cache: var StateCache): Result[void, cstring] =
|
||||
|
|
|
@ -111,6 +111,7 @@ func noRollback*(state: var phase0.BeaconState) =
|
|||
|
||||
type
|
||||
RollbackHashedProc* = proc(state: var phase0.HashedBeaconState) {.gcsafe, raises: [Defect].}
|
||||
RollbackAltairHashedProc* = proc(state: var altair.HashedBeaconState) {.gcsafe, raises: [Defect].}
|
||||
|
||||
# Hashed-state transition functions
|
||||
# ---------------------------------------------------------------
|
||||
|
@ -164,7 +165,10 @@ proc advance_slot(
|
|||
state.slot += 1
|
||||
|
||||
func noRollback*(state: var phase0.HashedBeaconState) =
|
||||
trace "Skipping rollback of broken state"
|
||||
trace "Skipping rollback of broken phase 0 state"
|
||||
|
||||
func noRollback*(state: var altair.HashedBeaconState) =
|
||||
trace "Skipping rollback of broken Altair state"
|
||||
|
||||
proc maybeUpgradeStateToAltair(
|
||||
state: var ForkedHashedBeaconState, altairForkSlot: Slot) =
|
||||
|
@ -220,7 +224,8 @@ proc state_transition_block_aux(
|
|||
preset: RuntimePreset,
|
||||
state: var SomeHashedBeaconState,
|
||||
signedBlock: phase0.SignedBeaconBlock | phase0.SigVerifiedSignedBeaconBlock |
|
||||
phase0.TrustedSignedBeaconBlock | altair.SignedBeaconBlock,
|
||||
phase0.TrustedSignedBeaconBlock | altair.SignedBeaconBlock |
|
||||
altair.SigVerifiedSignedBeaconBlock,
|
||||
cache: var StateCache, flags: UpdateFlags): bool {.nbench.} =
|
||||
# Block updates - these happen when there's a new block being suggested
|
||||
# by the block proposer. Every actor in the network will update its state
|
||||
|
@ -381,3 +386,63 @@ proc makeBeaconBlock*(
|
|||
blck.state_root = state.root
|
||||
|
||||
return some(blck)
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/validator.md#preparing-a-beaconblock
|
||||
proc makeBeaconBlock*(
|
||||
preset: RuntimePreset,
|
||||
state: var altair.HashedBeaconState,
|
||||
proposer_index: ValidatorIndex,
|
||||
parent_root: Eth2Digest,
|
||||
randao_reveal: ValidatorSig,
|
||||
eth1_data: Eth1Data,
|
||||
graffiti: GraffitiBytes,
|
||||
attestations: seq[Attestation],
|
||||
deposits: seq[Deposit],
|
||||
proposerSlashings: seq[ProposerSlashing],
|
||||
attesterSlashings: seq[AttesterSlashing],
|
||||
voluntaryExits: seq[SignedVoluntaryExit],
|
||||
executionPayload: ExecutionPayload,
|
||||
rollback: RollbackAltairHashedProc,
|
||||
cache: var StateCache): Option[altair.BeaconBlock] =
|
||||
## Create a block for the given state. The last block applied to it must be
|
||||
## the one identified by parent_root and process_slots must be called up to
|
||||
## the slot for which a block is to be created.
|
||||
|
||||
# To create a block, we'll first apply a partial block to the state, skipping
|
||||
# some validations.
|
||||
|
||||
var blck = altair.BeaconBlock(
|
||||
slot: state.data.slot,
|
||||
proposer_index: proposer_index.uint64,
|
||||
parent_root: parent_root,
|
||||
body: altair.BeaconBlockBody(
|
||||
randao_reveal: randao_reveal,
|
||||
eth1_data: eth1data,
|
||||
graffiti: graffiti,
|
||||
proposer_slashings: List[ProposerSlashing, Limit MAX_PROPOSER_SLASHINGS](
|
||||
proposerSlashings),
|
||||
attester_slashings: List[AttesterSlashing, Limit MAX_ATTESTER_SLASHINGS](
|
||||
attesterSlashings),
|
||||
attestations: List[Attestation, Limit MAX_ATTESTATIONS](attestations),
|
||||
deposits: List[Deposit, Limit MAX_DEPOSITS](deposits),
|
||||
voluntary_exits:
|
||||
List[SignedVoluntaryExit, Limit MAX_VOLUNTARY_EXITS](voluntaryExits)))
|
||||
|
||||
# TODO sync committees
|
||||
|
||||
let res = process_block(preset, state.data, blck, {skipBlsValidation}, cache)
|
||||
|
||||
if res.isErr:
|
||||
warn "Unable to apply new block to state",
|
||||
blck = shortLog(blck),
|
||||
slot = state.data.slot,
|
||||
eth1_deposit_index = state.data.eth1_deposit_index,
|
||||
deposit_root = shortLog(state.data.eth1_data.deposit_root),
|
||||
error = res.error
|
||||
rollback(state)
|
||||
return
|
||||
|
||||
state.root = hash_tree_root(state.data)
|
||||
blck.state_root = state.root
|
||||
|
||||
return some(blck)
|
||||
|
|
|
@ -382,7 +382,7 @@ proc process_justification_and_finalization*(state: var altair.BeaconState,
|
|||
return
|
||||
let
|
||||
# these ultimately differ from phase0 only in these lines
|
||||
# ref: https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/phase0/beacon-chain.md#justification-and-finalization
|
||||
# ref: https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/phase0/beacon-chain.md#justification-and-finalization
|
||||
previous_indices = get_unslashed_participating_indices(
|
||||
state, TIMELY_TARGET_FLAG_INDEX, get_previous_epoch(state))
|
||||
current_indices = get_unslashed_participating_indices(
|
||||
|
@ -412,7 +412,7 @@ func is_in_inactivity_leak(finality_delay: uint64): bool =
|
|||
func get_finality_delay(state: SomeBeaconState): uint64 =
|
||||
get_previous_epoch(state) - state.finalized_checkpoint.epoch
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/phase0/beacon-chain.md#rewards-and-penalties-1
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/phase0/beacon-chain.md#rewards-and-penalties-1
|
||||
func is_in_inactivity_leak(state: altair.BeaconState): bool =
|
||||
# TODO remove this, see above
|
||||
get_finality_delay(state) > MIN_EPOCHS_TO_INACTIVITY_PENALTY
|
||||
|
@ -615,7 +615,7 @@ iterator get_flag_index_deltas(
|
|||
else:
|
||||
(vidx, 0.Gwei, 0.Gwei)
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#modified-get_inactivity_penalty_deltas
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#modified-get_inactivity_penalty_deltas
|
||||
iterator get_inactivity_penalty_deltas(state: altair.BeaconState):
|
||||
(ValidatorIndex, Gwei) =
|
||||
## Return the inactivity penalty deltas by considering timely target
|
||||
|
@ -661,7 +661,7 @@ func process_rewards_and_penalties(
|
|||
increase_balance(state.balances.asSeq()[idx], v.delta.rewards)
|
||||
decrease_balance(state.balances.asSeq()[idx], v.delta.penalties)
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#rewards-and-penalties
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#rewards-and-penalties
|
||||
func process_rewards_and_penalties(
|
||||
state: var altair.BeaconState, total_active_balance: Gwei) {.nbench.} =
|
||||
if get_current_epoch(state) == GENESIS_EPOCH:
|
||||
|
@ -793,33 +793,38 @@ func process_participation_flag_updates*(state: var altair.BeaconState) =
|
|||
for _ in 0 ..< state.validators.len:
|
||||
doAssert state.current_epoch_participation.add 0.ParticipationFlags
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#sync-committee-updates
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#sync-committee-updates
|
||||
proc process_sync_committee_updates*(state: var altair.BeaconState) =
|
||||
let next_epoch = get_current_epoch(state) + 1
|
||||
if next_epoch mod EPOCHS_PER_SYNC_COMMITTEE_PERIOD == 0:
|
||||
state.current_sync_committee = state.next_sync_committee
|
||||
state.next_sync_committee = get_next_sync_committee(state)
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.6/specs/altair/beacon-chain.md#inactivity-scores
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.7/specs/altair/beacon-chain.md#inactivity-scores
|
||||
func process_inactivity_updates*(state: var altair.BeaconState) =
|
||||
# Score updates based on previous epoch participation, skip genesis epoch
|
||||
if get_current_epoch(state) == GENESIS_EPOCH:
|
||||
return
|
||||
|
||||
# TODO actually implement get_eligible_validator_indices() as an iterator
|
||||
let previous_epoch = get_previous_epoch(state) # get_eligible_validator_indices()
|
||||
let
|
||||
previous_epoch = get_previous_epoch(state) # get_eligible_validator_indices()
|
||||
unslashed_participating_indices =
|
||||
get_unslashed_participating_indices(
|
||||
state, TIMELY_TARGET_FLAG_INDEX, get_previous_epoch(state))
|
||||
for index in 0'u64 ..< state.validators.lenu64:
|
||||
# get_eligible_validator_indices()
|
||||
let v = state.validators[index]
|
||||
if not (is_active_validator(v, previous_epoch) or (v.slashed and previous_epoch + 1 < v.withdrawable_epoch)):
|
||||
continue
|
||||
|
||||
# Increase inactivity score of inactive validators
|
||||
if index.ValidatorIndex in get_unslashed_participating_indices(state, TIMELY_TARGET_FLAG_INDEX, get_previous_epoch(state)):
|
||||
# Increase the inactivity score of inactive validators
|
||||
if index.ValidatorIndex in unslashed_participating_indices:
|
||||
state.inactivity_scores[index] -= min(1'u64, state.inactivity_scores[index])
|
||||
else:
|
||||
state.inactivity_scores[index] += INACTIVITY_SCORE_BIAS
|
||||
# Decrease the score of all validators for forgiveness when not during a leak
|
||||
# Decrease the inactivity score of all eligible validators during a
|
||||
# leak-free epoch
|
||||
if not is_in_inactivity_leak(state):
|
||||
state.inactivity_scores[index] -= min(INACTIVITY_SCORE_RECOVERY_RATE.uint64, state.inactivity_scores[index])
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
{.push raises: [Defect].}
|
||||
|
||||
import
|
||||
./datatypes, ./digest, ./forkedbeaconstate_helpers, ./helpers
|
||||
./datatypes/base, ./digest, ./forkedbeaconstate_helpers, ./helpers
|
||||
|
||||
const
|
||||
SAFETY_DECAY* = 10'u64
|
||||
|
@ -17,7 +17,7 @@ const
|
|||
func compute_weak_subjectivity_period(state: ForkedHashedBeaconState): uint64 =
|
||||
var weak_subjectivity_period = MIN_VALIDATOR_WITHDRAWABILITY_DELAY
|
||||
let validator_count =
|
||||
get_active_validator_indices_len(state.hbsPhase0.data, get_current_epoch(state))
|
||||
get_active_validator_indices_len(state, get_current_epoch(state))
|
||||
if validator_count >= MIN_PER_EPOCH_CHURN_LIMIT * CHURN_LIMIT_QUOTIENT:
|
||||
weak_subjectivity_period += SAFETY_DECAY * CHURN_LIMIT_QUOTIENT div (2 * 100)
|
||||
else:
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# beacon_chain
|
||||
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
||||
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
||||
# 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).
|
||||
|
@ -11,7 +11,7 @@
|
|||
import
|
||||
std/[strutils, parseutils],
|
||||
stew/objects, faststreams/outputs, json_serialization/writer,
|
||||
../spec/datatypes,
|
||||
../spec/datatypes/base,
|
||||
./bytes_reader, ./types, ./navigator, ./spec_types
|
||||
|
||||
export
|
||||
|
|
|
@ -9,7 +9,8 @@
|
|||
|
||||
import
|
||||
std/options,
|
||||
../spec/[datatypes, digest, crypto, helpers],
|
||||
../spec/datatypes/base,
|
||||
../spec/[digest, crypto, helpers],
|
||||
../consensus_object_pools/[spec_cache, attestation_pool]
|
||||
|
||||
# https://github.com/ethereum/eth2.0-specs/blob/v1.0.1/specs/phase0/validator.md#aggregation-selection
|
||||
|
|
|
@ -11,7 +11,8 @@ import
|
|||
std/[os, strutils, terminal, wordwrap, unicode],
|
||||
chronicles, chronos, web3, stint, json_serialization, zxcvbn,
|
||||
serialization, blscurve, eth/common/eth_types, eth/keys, confutils, bearssl,
|
||||
../spec/[datatypes, digest, crypto, keystore],
|
||||
../spec/[digest, crypto, keystore],
|
||||
../spec/datatypes/base,
|
||||
stew/io2, libp2p/crypto/crypto as lcrypto,
|
||||
nimcrypto/utils as ncrutils,
|
||||
".."/[conf, ssz/merkleization, filepath],
|
||||
|
|
|
@ -343,7 +343,7 @@ proc proposeSignedBlock*(node: BeaconNode,
|
|||
newBlock: SignedBeaconBlock): BlockRef =
|
||||
let newBlockRef = node.dag.addRawBlock(node.quarantine, newBlock) do (
|
||||
blckRef: BlockRef, trustedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if signed block valid (and becomes trusted)
|
||||
node.attestationPool[].addForkChoice(
|
||||
epochRef, blckRef, trustedBlock.message,
|
||||
|
|
|
@ -12,7 +12,8 @@ import
|
|||
chronos, chronicles, metrics,
|
||||
json_serialization/std/[sets, net],
|
||||
eth/db/[kvstore, kvstore_sqlite3],
|
||||
../spec/[datatypes, crypto, digest, signatures, helpers],
|
||||
../spec/[crypto, digest, signatures, helpers],
|
||||
../spec/datatypes/base,
|
||||
../beacon_node_types,
|
||||
./slashing_protection
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import
|
|||
# Status libraries
|
||||
confutils, serialization,
|
||||
# Beacon-chain
|
||||
../beacon_chain/spec/datatypes,
|
||||
../beacon_chain/spec/datatypes/base,
|
||||
# Bench specific
|
||||
scenarios, bench_lab, reports
|
||||
|
||||
|
|
|
@ -179,7 +179,7 @@ cli do(slots = SLOTS_PER_EPOCH * 5,
|
|||
|
||||
let added = dag.addRawBlock(quarantine, newBlock) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
attPool.addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# beacon_chain
|
||||
# Copyright (c) 2018 Status Research & Development GmbH
|
||||
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
||||
# 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).
|
||||
|
@ -11,10 +11,11 @@ import
|
|||
# Status libraries
|
||||
stew/[results, endians2],
|
||||
# Internals
|
||||
../../beacon_chain/spec/[datatypes, digest],
|
||||
../../beacon_chain/spec/datatypes/base,
|
||||
../../beacon_chain/spec/digest,
|
||||
../../beacon_chain/fork_choice/[fork_choice, fork_choice_types]
|
||||
|
||||
export results, datatypes, digest, fork_choice, fork_choice_types, tables, options
|
||||
export results, base, digest, fork_choice, fork_choice_types, tables, options
|
||||
|
||||
func fakeHash*(index: SomeInteger): Eth2Digest =
|
||||
## Create fake hashes
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
|
||||
import
|
||||
# Specs
|
||||
../../beacon_chain/spec/[
|
||||
datatypes, forkedbeaconstate_helpers, state_transition]
|
||||
../../beacon_chain/spec/[forkedbeaconstate_helpers, state_transition],
|
||||
../../beacon_chain/spec/datatypes/base
|
||||
|
||||
proc nextEpoch*(state: var ForkedHashedBeaconState) =
|
||||
## Transition to the start of the next epoch
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# beacon_chain
|
||||
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
||||
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
||||
# 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).
|
||||
|
@ -11,7 +11,8 @@
|
|||
import
|
||||
bearssl, eth/keys,
|
||||
blscurve,
|
||||
../../beacon_chain/spec/[datatypes, crypto, presets]
|
||||
../../beacon_chain/spec/[crypto, presets],
|
||||
../../beacon_chain/spec/datatypes/base
|
||||
|
||||
proc newKeyPair(rng: var BrHmacDrbgContext): BlsResult[tuple[pub: ValidatorPubKey, priv: ValidatorPrivKey]] =
|
||||
## Generates a new public-private keypair
|
||||
|
|
|
@ -14,7 +14,8 @@ import
|
|||
# Third-party
|
||||
yaml,
|
||||
# Beacon chain internals
|
||||
../../beacon_chain/spec/[datatypes, digest],
|
||||
../../beacon_chain/spec/digest,
|
||||
../../beacon_chain/spec/datatypes/base,
|
||||
../../beacon_chain/ssz,
|
||||
# Test utilities
|
||||
./fixtures_utils
|
||||
|
|
|
@ -14,8 +14,8 @@
|
|||
import
|
||||
stew/results,
|
||||
# Specs
|
||||
../../beacon_chain/spec/[
|
||||
beaconstate, datatypes, forkedbeaconstate_helpers, helpers],
|
||||
../../beacon_chain/spec/[beaconstate, forkedbeaconstate_helpers, helpers],
|
||||
../../beacon_chain/spec/datatypes/base,
|
||||
# Mock helpers
|
||||
../mocking/[mock_genesis, mock_attestations, mock_state],
|
||||
../testutil
|
||||
|
|
|
@ -16,7 +16,8 @@ import
|
|||
# Standard library
|
||||
std/math,
|
||||
# Specs
|
||||
../../beacon_chain/spec/[beaconstate, datatypes, crypto, presets],
|
||||
../../beacon_chain/spec/[beaconstate, crypto, presets],
|
||||
../../beacon_chain/spec/datatypes/base,
|
||||
# Internals
|
||||
../../beacon_chain/ssz,
|
||||
../../beacon_chain/extras,
|
||||
|
|
|
@ -8,8 +8,8 @@
|
|||
import
|
||||
# Specs
|
||||
../../beacon_chain/spec/[
|
||||
datatypes, forkedbeaconstate_helpers, state_transition,
|
||||
state_transition_epoch]
|
||||
forkedbeaconstate_helpers, state_transition, state_transition_epoch],
|
||||
../../beacon_chain/spec/datatypes/base
|
||||
|
||||
proc processSlotsUntilEndCurrentEpoch(state: var ForkedHashedBeaconState) =
|
||||
# Process all slots until the end of the last slot of the current epoch
|
||||
|
|
|
@ -12,7 +12,8 @@ import
|
|||
# Vendored packages
|
||||
stew/bitops2,
|
||||
# Specs
|
||||
../../beacon_chain/spec/[datatypes, forkedbeaconstate_helpers],
|
||||
../../beacon_chain/spec/datatypes/base,
|
||||
../../beacon_chain/spec/forkedbeaconstate_helpers,
|
||||
# Test helpers
|
||||
../mocking/mock_genesis,
|
||||
./epoch_utils,
|
||||
|
|
|
@ -379,7 +379,7 @@ suite "Attestation pool processing" & preset():
|
|||
b1 = addTestBlock(state.data, dag.tail.root, cache)
|
||||
b1Add = dag.addRawBlock(quarantine, b1) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
@ -392,7 +392,7 @@ suite "Attestation pool processing" & preset():
|
|||
b2 = addTestBlock(state.data, b1.root, cache)
|
||||
b2Add = dag.addRawBlock(quarantine, b2) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
@ -407,7 +407,7 @@ suite "Attestation pool processing" & preset():
|
|||
b10 = makeTestBlock(state.data, dag.tail.root, cache)
|
||||
b10Add = dag.addRawBlock(quarantine, b10) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
@ -422,7 +422,7 @@ suite "Attestation pool processing" & preset():
|
|||
)
|
||||
b11Add = dag.addRawBlock(quarantine, b11) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
@ -468,7 +468,7 @@ suite "Attestation pool processing" & preset():
|
|||
b10 = makeTestBlock(state.data, dag.tail.root, cache)
|
||||
b10Add = dag.addRawBlock(quarantine, b10) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
@ -482,7 +482,7 @@ suite "Attestation pool processing" & preset():
|
|||
let b10_clone = b10 # Assumes deep copy
|
||||
let b10Add_clone = dag.addRawBlock(quarantine, b10_clone) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
@ -497,7 +497,7 @@ suite "Attestation pool processing" & preset():
|
|||
b10 = addTestBlock(state.data, dag.tail.root, cache)
|
||||
b10Add = dag.addRawBlock(quarantine, b10) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
@ -525,7 +525,7 @@ suite "Attestation pool processing" & preset():
|
|||
block_root = new_block.root
|
||||
let blockRef = dag.addRawBlock(quarantine, new_block) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
@ -567,7 +567,7 @@ suite "Attestation pool processing" & preset():
|
|||
# Add back the old block to ensure we have a duplicate error
|
||||
let b10Add_clone = dag.addRawBlock(quarantine, b10_clone) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
|
|
@ -12,9 +12,9 @@ import
|
|||
unittest2,
|
||||
stew/assign2,
|
||||
eth/keys,
|
||||
../beacon_chain/spec/datatypes/base,
|
||||
../beacon_chain/spec/[
|
||||
datatypes, digest, forkedbeaconstate_helpers, helpers, state_transition,
|
||||
presets],
|
||||
digest, forkedbeaconstate_helpers, helpers, state_transition, presets],
|
||||
../beacon_chain/beacon_node_types,
|
||||
../beacon_chain/[beacon_chain_db, ssz],
|
||||
../beacon_chain/consensus_object_pools/[
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
import
|
||||
std/typetraits,
|
||||
unittest2,
|
||||
../beacon_chain/spec/datatypes,
|
||||
../beacon_chain/spec/datatypes/base,
|
||||
./testutil
|
||||
|
||||
suite "Spec datatypes":
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
import
|
||||
unittest2,
|
||||
chronos, stew/shims/net, eth/keys, eth/p2p/discoveryv5/enr,
|
||||
../beacon_chain/spec/datatypes,
|
||||
../beacon_chain/spec/datatypes/base,
|
||||
../beacon_chain/networking/[eth2_network, eth2_discovery],
|
||||
./testutil
|
||||
|
||||
|
|
|
@ -57,7 +57,7 @@ suite "Gossip validation " & preset():
|
|||
int(SLOTS_PER_EPOCH * 5), false):
|
||||
let added = dag.addRawBlock(quarantine, blck) do (
|
||||
blckRef: BlockRef, signedBlock: TrustedSignedBeaconBlock,
|
||||
epochRef: EpochRef, state: HashedBeaconState):
|
||||
epochRef: EpochRef):
|
||||
# Callback add to fork choice if valid
|
||||
pool[].addForkChoice(epochRef, blckRef, signedBlock.message, blckRef.slot)
|
||||
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
import
|
||||
unittest2,
|
||||
./testutil,
|
||||
../beacon_chain/spec/[crypto, datatypes, network],
|
||||
../beacon_chain/spec/[crypto, network],
|
||||
../beacon_chain/spec/datatypes/base,
|
||||
../beacon_chain/validators/attestation_aggregation
|
||||
|
||||
suite "Honest validator":
|
||||
|
|
|
@ -4,7 +4,8 @@ import
|
|||
stint, ./testutil, stew/byteutils,
|
||||
../beacon_chain/interop,
|
||||
../beacon_chain/ssz,
|
||||
../beacon_chain/spec/[beaconstate, crypto, datatypes, presets]
|
||||
../beacon_chain/spec/[beaconstate, crypto, presets],
|
||||
../beacon_chain/spec/datatypes/base
|
||||
|
||||
# Interop test yaml, found here:
|
||||
# https://github.com/ethereum/eth2.0-pm/blob/a0b9d22fad424574b1307828f867b30237758468/interop/mocked_start/keygen_10_validators.yaml
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# beacon_chain
|
||||
# Copyright (c) 2018 Status Research & Development GmbH
|
||||
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
||||
# 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).
|
||||
|
@ -12,7 +12,8 @@ import
|
|||
unittest2,
|
||||
nimcrypto/hash,
|
||||
json_serialization,
|
||||
../beacon_chain/spec/[datatypes, digest],
|
||||
../beacon_chain/spec/digest,
|
||||
../beacon_chain/spec/datatypes/base,
|
||||
../beacon_chain/ssz, ../beacon_chain/ssz/[navigator, dynamic_navigator]
|
||||
|
||||
type
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
# beacon_chain
|
||||
# Copyright (c) 2018 Status Research & Development GmbH
|
||||
# Copyright (c) 2018-2021 Status Research & Development GmbH
|
||||
# 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).
|
||||
|
@ -9,7 +9,8 @@
|
|||
|
||||
import
|
||||
unittest2,
|
||||
../beacon_chain/spec/[datatypes, crypto],
|
||||
../beacon_chain/spec/crypto,
|
||||
../beacon_chain/spec/datatypes/base,
|
||||
../beacon_chain/ssz
|
||||
|
||||
# Sanity checks to make sure all the workarounds introduced
|
||||
|
|
|
@ -326,4 +326,6 @@ iterator makeTestBlocks*(state: ForkedHashedBeaconState; parent_root: Eth2Digest
|
|||
proc getAttestationsforTestBlock*(
|
||||
pool: var AttestationPool, stateData: StateData, cache: var StateCache):
|
||||
seq[Attestation] =
|
||||
pool.getAttestationsForBlock(stateData.data.hbsPhase0, cache)
|
||||
case stateData.data.beaconStateFork:
|
||||
of forkPhase0: pool.getAttestationsForBlock(stateData.data.hbsPhase0, cache)
|
||||
of forkAltair: pool.getAttestationsForBlock(stateData.data.hbsAltair, cache)
|
||||
|
|
Loading…
Reference in New Issue