rm unused ForkedTrustedBeaconBlock; add some Electra overloads to consensus_object_pools; Electra BeaconBlock gossip support (#5965)

This commit is contained in:
tersec 2024-02-26 06:49:12 +00:00 committed by GitHub
parent f65c1121d2
commit fef831d92a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 64 additions and 39 deletions

View File

@ -483,7 +483,8 @@ func init(
func init( func init(
T: type AttestationCache, T: type AttestationCache,
state: altair.HashedBeaconState | bellatrix.HashedBeaconState | state: altair.HashedBeaconState | bellatrix.HashedBeaconState |
capella.HashedBeaconState | deneb.HashedBeaconState, capella.HashedBeaconState | deneb.HashedBeaconState |
electra.HashedBeaconState,
cache: var StateCache): T = cache: var StateCache): T =
# Load attestations that are scheduled for being given rewards for # Load attestations that are scheduled for being given rewards for
let let

View File

@ -73,7 +73,8 @@ func init*(
T: type BlockRef, root: Eth2Digest, executionValid: bool, T: type BlockRef, root: Eth2Digest, executionValid: bool,
blck: bellatrix.SomeBeaconBlock | bellatrix.TrustedBeaconBlock | blck: bellatrix.SomeBeaconBlock | bellatrix.TrustedBeaconBlock |
capella.SomeBeaconBlock | capella.TrustedBeaconBlock | capella.SomeBeaconBlock | capella.TrustedBeaconBlock |
deneb.SomeBeaconBlock | deneb.TrustedBeaconBlock): BlockRef = deneb.SomeBeaconBlock | deneb.TrustedBeaconBlock |
electra.SomeBeaconBlock | electra.TrustedBeaconBlock): BlockRef =
BlockRef.init( BlockRef.init(
root, Opt.some Eth2Digest(blck.body.execution_payload.block_hash), root, Opt.some Eth2Digest(blck.body.execution_payload.block_hash),
executionValid = executionValid =

View File

@ -293,10 +293,11 @@ type
OnBellatrixBlockAdded* = OnBlockAdded[bellatrix.TrustedSignedBeaconBlock] OnBellatrixBlockAdded* = OnBlockAdded[bellatrix.TrustedSignedBeaconBlock]
OnCapellaBlockAdded* = OnBlockAdded[capella.TrustedSignedBeaconBlock] OnCapellaBlockAdded* = OnBlockAdded[capella.TrustedSignedBeaconBlock]
OnDenebBlockAdded* = OnBlockAdded[deneb.TrustedSignedBeaconBlock] OnDenebBlockAdded* = OnBlockAdded[deneb.TrustedSignedBeaconBlock]
OnElectraBlockAdded* = OnBlockAdded[electra.TrustedSignedBeaconBlock]
OnForkyBlockAdded* = OnForkyBlockAdded* =
OnPhase0BlockAdded | OnAltairBlockAdded | OnBellatrixBlockAdded | OnPhase0BlockAdded | OnAltairBlockAdded | OnBellatrixBlockAdded |
OnCapellaBlockAdded | OnDenebBlockAdded OnCapellaBlockAdded | OnDenebBlockAdded | OnElectraBlockAdded
HeadChangeInfoObject* = object HeadChangeInfoObject* = object
slot*: Slot slot*: Slot

View File

@ -328,7 +328,7 @@ proc newExecutionPayload*(
proc getExecutionValidity( proc getExecutionValidity(
elManager: ELManager, elManager: ELManager,
blck: bellatrix.SignedBeaconBlock | capella.SignedBeaconBlock | blck: bellatrix.SignedBeaconBlock | capella.SignedBeaconBlock |
deneb.SignedBeaconBlock): deneb.SignedBeaconBlock | electra.SignedBeaconBlock):
Future[NewPayloadStatus] {.async: (raises: [CancelledError]).} = Future[NewPayloadStatus] {.async: (raises: [CancelledError]).} =
if not blck.message.is_execution_block: if not blck.message.is_execution_block:
return NewPayloadStatus.valid # vacuously return NewPayloadStatus.valid # vacuously
@ -361,9 +361,10 @@ proc getExecutionValidity(
blck = shortLog(blck) blck = shortLog(blck)
return NewPayloadStatus.noResponse return NewPayloadStatus.noResponse
proc checkBloblessSignature(self: BlockProcessor, proc checkBloblessSignature(
signed_beacon_block: deneb.SignedBeaconBlock): self: BlockProcessor,
Result[void, cstring] = signed_beacon_block: deneb.SignedBeaconBlock | electra.SignedBeaconBlock):
Result[void, cstring] =
let dag = self.consensusManager.dag let dag = self.consensusManager.dag
let parent = dag.getBlockRef(signed_beacon_block.message.parent_root).valueOr: let parent = dag.getBlockRef(signed_beacon_block.message.parent_root).valueOr:
return err("checkBloblessSignature called with orphan block") return err("checkBloblessSignature called with orphan block")

View File

@ -268,7 +268,8 @@ template validateBeaconBlockBellatrix(
signed_beacon_block: signed_beacon_block:
bellatrix.SignedBeaconBlock | bellatrix.SignedBeaconBlock |
capella.SignedBeaconBlock | capella.SignedBeaconBlock |
deneb.SignedBeaconBlock, deneb.SignedBeaconBlock |
electra.SignedBeaconBlock,
parent: BlockRef): untyped = parent: BlockRef): untyped =
# If the execution is enabled for the block -- i.e. # If the execution is enabled for the block -- i.e.
# is_execution_enabled(state, block.body) then validate the following: # is_execution_enabled(state, block.body) then validate the following:

View File

@ -830,7 +830,7 @@ template gossipMaxSize(T: untyped): uint32 =
when isFixedSize(T): when isFixedSize(T):
fixedPortionSize(T).uint32 fixedPortionSize(T).uint32
elif T is bellatrix.SignedBeaconBlock or T is capella.SignedBeaconBlock or elif T is bellatrix.SignedBeaconBlock or T is capella.SignedBeaconBlock or
T is deneb.SignedBeaconBlock: T is deneb.SignedBeaconBlock or T is electra.SignedBeaconBlock:
GOSSIP_MAX_SIZE GOSSIP_MAX_SIZE
# TODO https://github.com/status-im/nim-ssz-serialization/issues/20 for # TODO https://github.com/status-im/nim-ssz-serialization/issues/20 for
# Attestation, AttesterSlashing, and SignedAggregateAndProof, which all # Attestation, AttesterSlashing, and SignedAggregateAndProof, which all
@ -2637,6 +2637,12 @@ proc broadcastBeaconBlock*(
let topic = getBeaconBlocksTopic(node.forkDigests.deneb) let topic = getBeaconBlocksTopic(node.forkDigests.deneb)
node.broadcast(topic, blck) node.broadcast(topic, blck)
proc broadcastBeaconBlock*(
node: Eth2Node, blck: electra.SignedBeaconBlock):
Future[SendResult] {.async: (raises: [CancelledError], raw: true).} =
let topic = getBeaconBlocksTopic(node.forkDigests.electra)
node.broadcast(topic, blck)
proc broadcastBlobSidecar*( proc broadcastBlobSidecar*(
node: Eth2Node, subnet_id: BlobId, blob: deneb.BlobSidecar): node: Eth2Node, subnet_id: BlobId, blob: deneb.BlobSidecar):
Future[SendResult] {.async: (raises: [CancelledError], raw: true).} = Future[SendResult] {.async: (raises: [CancelledError], raw: true).} =

View File

@ -275,6 +275,7 @@ when const_preset == "gnosis":
doAssert network.cfg.BELLATRIX_FORK_EPOCH < FAR_FUTURE_EPOCH doAssert network.cfg.BELLATRIX_FORK_EPOCH < FAR_FUTURE_EPOCH
doAssert network.cfg.CAPELLA_FORK_EPOCH < FAR_FUTURE_EPOCH doAssert network.cfg.CAPELLA_FORK_EPOCH < FAR_FUTURE_EPOCH
doAssert network.cfg.DENEB_FORK_EPOCH < FAR_FUTURE_EPOCH doAssert network.cfg.DENEB_FORK_EPOCH < FAR_FUTURE_EPOCH
doAssert network.cfg.ELECTRA_FORK_EPOCH == FAR_FUTURE_EPOCH
static: doAssert ConsensusFork.high == ConsensusFork.Deneb static: doAssert ConsensusFork.high == ConsensusFork.Deneb
elif const_preset == "mainnet": elif const_preset == "mainnet":
@ -340,6 +341,7 @@ elif const_preset == "mainnet":
doAssert network.cfg.BELLATRIX_FORK_EPOCH < FAR_FUTURE_EPOCH doAssert network.cfg.BELLATRIX_FORK_EPOCH < FAR_FUTURE_EPOCH
doAssert network.cfg.CAPELLA_FORK_EPOCH < FAR_FUTURE_EPOCH doAssert network.cfg.CAPELLA_FORK_EPOCH < FAR_FUTURE_EPOCH
doAssert network.cfg.DENEB_FORK_EPOCH < FAR_FUTURE_EPOCH doAssert network.cfg.DENEB_FORK_EPOCH < FAR_FUTURE_EPOCH
doAssert network.cfg.ELECTRA_FORK_EPOCH == FAR_FUTURE_EPOCH
static: doAssert ConsensusFork.high == ConsensusFork.Deneb static: doAssert ConsensusFork.high == ConsensusFork.Deneb
proc getMetadataForNetwork*(networkName: string): Eth2NetworkMetadata = proc getMetadataForNetwork*(networkName: string): Eth2NetworkMetadata =

View File

@ -84,8 +84,8 @@ func get_validator_churn_limit*(
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/deneb/beacon-chain.md#new-get_validator_activation_churn_limit # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/deneb/beacon-chain.md#new-get_validator_activation_churn_limit
func get_validator_activation_churn_limit*( func get_validator_activation_churn_limit*(
cfg: RuntimeConfig, state: deneb.BeaconState, cache: var StateCache): cfg: RuntimeConfig, state: deneb.BeaconState | electra.BeaconState,
uint64 = cache: var StateCache): uint64 =
## Return the validator activation churn limit for the current epoch. ## Return the validator activation churn limit for the current epoch.
min( min(
cfg.MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT, cfg.MAX_PER_EPOCH_ACTIVATION_CHURN_LIMIT,
@ -154,7 +154,7 @@ func get_slashing_penalty*(state: ForkyBeaconState,
elif state is altair.BeaconState: elif state is altair.BeaconState:
validator_effective_balance div MIN_SLASHING_PENALTY_QUOTIENT_ALTAIR validator_effective_balance div MIN_SLASHING_PENALTY_QUOTIENT_ALTAIR
elif state is bellatrix.BeaconState or state is capella.BeaconState or elif state is bellatrix.BeaconState or state is capella.BeaconState or
state is deneb.BeaconState: state is deneb.BeaconState or state is electra.BeaconState:
validator_effective_balance div MIN_SLASHING_PENALTY_QUOTIENT_BELLATRIX validator_effective_balance div MIN_SLASHING_PENALTY_QUOTIENT_BELLATRIX
else: else:
{.fatal: "invalid BeaconState type".} {.fatal: "invalid BeaconState type".}
@ -172,7 +172,8 @@ func get_proposer_reward(state: ForkyBeaconState, whistleblower_reward: Gwei): G
when state is phase0.BeaconState: when state is phase0.BeaconState:
whistleblower_reward div PROPOSER_REWARD_QUOTIENT whistleblower_reward div PROPOSER_REWARD_QUOTIENT
elif state is altair.BeaconState or state is bellatrix.BeaconState or elif state is altair.BeaconState or state is bellatrix.BeaconState or
state is capella.BeaconState or state is deneb.BeaconState: state is capella.BeaconState or state is deneb.BeaconState or
state is electra.BeaconState:
whistleblower_reward * PROPOSER_WEIGHT div WEIGHT_DENOMINATOR whistleblower_reward * PROPOSER_WEIGHT div WEIGHT_DENOMINATOR
else: else:
{.fatal: "invalid BeaconState type".} {.fatal: "invalid BeaconState type".}
@ -292,6 +293,20 @@ func get_initial_beacon_block*(state: deneb.HashedBeaconState):
deneb.TrustedSignedBeaconBlock( deneb.TrustedSignedBeaconBlock(
message: message, root: hash_tree_root(message)) message: message, root: hash_tree_root(message))
from ./datatypes/electra import HashedBeaconState, TrustedSignedBeaconBlock
# TODO spec link here when it exists
func get_initial_beacon_block*(state: electra.HashedBeaconState):
electra.TrustedSignedBeaconBlock =
# The genesis block is implicitly trusted
let message = electra.TrustedBeaconBlock(
slot: state.data.slot,
state_root: state.root)
# parent_root, randao_reveal, eth1_data, signature, and body automatically
# initialized to default values.
electra.TrustedSignedBeaconBlock(
message: message, root: hash_tree_root(message))
func get_initial_beacon_block*(state: ForkedHashedBeaconState): func get_initial_beacon_block*(state: ForkedHashedBeaconState):
ForkedTrustedSignedBeaconBlock = ForkedTrustedSignedBeaconBlock =
withState(state): withState(state):
@ -549,7 +564,7 @@ func get_attestation_participation_flag_indices(
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/deneb/beacon-chain.md#modified-get_attestation_participation_flag_indices # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/deneb/beacon-chain.md#modified-get_attestation_participation_flag_indices
func get_attestation_participation_flag_indices( func get_attestation_participation_flag_indices(
state: deneb.BeaconState, state: deneb.BeaconState | electra.BeaconState,
data: AttestationData, inclusion_delay: uint64): set[TimelyFlag] = data: AttestationData, inclusion_delay: uint64): set[TimelyFlag] =
## Return the flag indices that are satisfied by an attestation. ## Return the flag indices that are satisfied by an attestation.
let justified_checkpoint = let justified_checkpoint =
@ -613,7 +628,7 @@ func get_base_reward_per_increment*(
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/altair/beacon-chain.md#get_base_reward # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/altair/beacon-chain.md#get_base_reward
func get_base_reward( func get_base_reward(
state: altair.BeaconState | bellatrix.BeaconState | capella.BeaconState | state: altair.BeaconState | bellatrix.BeaconState | capella.BeaconState |
deneb.BeaconState, deneb.BeaconState | electra.BeaconState,
index: ValidatorIndex, base_reward_per_increment: Gwei): Gwei = index: ValidatorIndex, base_reward_per_increment: Gwei): Gwei =
## Return the base reward for the validator defined by ``index`` with respect ## Return the base reward for the validator defined by ``index`` with respect
## to the current ``state``. ## to the current ``state``.
@ -658,7 +673,8 @@ proc check_attestation*(
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/capella/beacon-chain.md#new-process_bls_to_execution_change # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/capella/beacon-chain.md#new-process_bls_to_execution_change
proc check_bls_to_execution_change*( proc check_bls_to_execution_change*(
genesisFork: Fork, state: capella.BeaconState | deneb.BeaconState, genesisFork: Fork,
state: capella.BeaconState | deneb.BeaconState | electra.BeaconState,
signed_address_change: SignedBLSToExecutionChange, flags: UpdateFlags): signed_address_change: SignedBLSToExecutionChange, flags: UpdateFlags):
Result[void, cstring] = Result[void, cstring] =
let address_change = signed_address_change.message let address_change = signed_address_change.message
@ -746,7 +762,8 @@ proc process_attestation*(
else: else:
addPendingAttestation(state.previous_epoch_attestations) addPendingAttestation(state.previous_epoch_attestations)
elif state is altair.BeaconState or state is bellatrix.BeaconState or elif state is altair.BeaconState or state is bellatrix.BeaconState or
state is capella.BeaconState or state is deneb.BeaconState: state is capella.BeaconState or state is deneb.BeaconState or
state is electra.BeaconState:
template updateParticipationFlags(epoch_participation: untyped) = template updateParticipationFlags(epoch_participation: untyped) =
let proposer_reward = get_proposer_reward( let proposer_reward = get_proposer_reward(
state, attestation, base_reward_per_increment, cache, epoch_participation) state, attestation, base_reward_per_increment, cache, epoch_participation)
@ -765,7 +782,7 @@ proc process_attestation*(
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/altair/beacon-chain.md#get_next_sync_committee_indices # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/altair/beacon-chain.md#get_next_sync_committee_indices
func get_next_sync_committee_keys( func get_next_sync_committee_keys(
state: altair.BeaconState | bellatrix.BeaconState | capella.BeaconState | state: altair.BeaconState | bellatrix.BeaconState | capella.BeaconState |
deneb.BeaconState): deneb.BeaconState | electra.BeaconState):
array[SYNC_COMMITTEE_SIZE, ValidatorPubKey] = array[SYNC_COMMITTEE_SIZE, ValidatorPubKey] =
## Return the sequence of sync committee indices, with possible duplicates, ## Return the sequence of sync committee indices, with possible duplicates,
## for the next sync committee. ## for the next sync committee.
@ -825,7 +842,8 @@ func is_partially_withdrawable_validator(
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/capella/beacon-chain.md#new-get_expected_withdrawals # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/capella/beacon-chain.md#new-get_expected_withdrawals
func get_expected_withdrawals*( func get_expected_withdrawals*(
state: capella.BeaconState | deneb.BeaconState): seq[Withdrawal] = state: capella.BeaconState | deneb.BeaconState | electra.BeaconState):
seq[Withdrawal] =
let let
epoch = get_current_epoch(state) epoch = get_current_epoch(state)
num_validators = lenu64(state.validators) num_validators = lenu64(state.validators)
@ -862,7 +880,7 @@ func get_expected_withdrawals*(
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/altair/beacon-chain.md#get_next_sync_committee # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/altair/beacon-chain.md#get_next_sync_committee
func get_next_sync_committee*( func get_next_sync_committee*(
state: altair.BeaconState | bellatrix.BeaconState | capella.BeaconState | state: altair.BeaconState | bellatrix.BeaconState | capella.BeaconState |
deneb.BeaconState): deneb.BeaconState | electra.BeaconState):
SyncCommittee = SyncCommittee =
## Return the next sync committee, with possible pubkey duplicates. ## Return the next sync committee, with possible pubkey duplicates.
var res: SyncCommittee var res: SyncCommittee
@ -1436,7 +1454,7 @@ func latest_block_root*(state: ForkedHashedBeaconState): Eth2Digest =
func get_sync_committee_cache*( func get_sync_committee_cache*(
state: altair.BeaconState | bellatrix.BeaconState | capella.BeaconState | state: altair.BeaconState | bellatrix.BeaconState | capella.BeaconState |
deneb.BeaconState, deneb.BeaconState | electra.BeaconState,
cache: var StateCache): SyncCommitteeCache = cache: var StateCache): SyncCommitteeCache =
let period = state.slot.sync_committee_period() let period = state.slot.sync_committee_period()

View File

@ -17,7 +17,7 @@ import
std/[json, tables], std/[json, tables],
stew/base10, web3/primitives, httputils, stew/base10, web3/primitives, httputils,
".."/forks, ".."/forks,
".."/datatypes/[phase0, altair, bellatrix, deneb], ".."/datatypes/[phase0, altair, bellatrix, deneb, electra],
".."/mev/[capella_mev, deneb_mev] ".."/mev/[capella_mev, deneb_mev]
from ".."/datatypes/capella import BeaconBlockBody from ".."/datatypes/capella import BeaconBlockBody
@ -331,6 +331,11 @@ type
kzg_proofs*: deneb.KzgProofs kzg_proofs*: deneb.KzgProofs
blobs*: deneb.Blobs blobs*: deneb.Blobs
ElectraSignedBlockContents* = object
signed_block*: electra.SignedBeaconBlock
kzg_proofs*: deneb.KzgProofs
blobs*: deneb.Blobs
RestPublishedSignedBlockContents* = object RestPublishedSignedBlockContents* = object
case kind*: ConsensusFork case kind*: ConsensusFork
of ConsensusFork.Phase0: phase0Data*: phase0.SignedBeaconBlock of ConsensusFork.Phase0: phase0Data*: phase0.SignedBeaconBlock
@ -689,6 +694,12 @@ func init*(t: typedesc[RestPublishedSignedBlockContents],
) )
) )
func init*(t: typedesc[RestPublishedSignedBlockContents],
contents: electra.BeaconBlock, root: Eth2Digest,
signature: ValidatorSig): RestPublishedSignedBlockContents =
debugRaiseAssert "init*(t: typedesc[RestPublishedSignedBlockContents],"
default(RestPublishedSignedBlockContents)
func init*(t: typedesc[StateIdent], v: StateIdentType): StateIdent = func init*(t: typedesc[StateIdent], v: StateIdentType): StateIdent =
StateIdent(kind: StateQueryKind.Named, value: v) StateIdent(kind: StateQueryKind.Named, value: v)

View File

@ -186,14 +186,6 @@ type
of ConsensusFork.Capella: capellaData*: capella_mev.BlindedBeaconBlock of ConsensusFork.Capella: capellaData*: capella_mev.BlindedBeaconBlock
of ConsensusFork.Deneb: denebData*: deneb_mev.BlindedBeaconBlock of ConsensusFork.Deneb: denebData*: deneb_mev.BlindedBeaconBlock
ForkedTrustedBeaconBlock* = object
case kind*: ConsensusFork
of ConsensusFork.Phase0: phase0Data*: phase0.TrustedBeaconBlock
of ConsensusFork.Altair: altairData*: altair.TrustedBeaconBlock
of ConsensusFork.Bellatrix: bellatrixData*: bellatrix.TrustedBeaconBlock
of ConsensusFork.Capella: capellaData*: capella.TrustedBeaconBlock
of ConsensusFork.Deneb: denebData*: deneb.TrustedBeaconBlock
ForkySignedBeaconBlock* = ForkySignedBeaconBlock* =
phase0.SignedBeaconBlock | phase0.SignedBeaconBlock |
altair.SignedBeaconBlock | altair.SignedBeaconBlock |
@ -604,15 +596,6 @@ template init*(T: type ForkedBeaconBlock, blck: capella.BeaconBlock): T =
template init*(T: type ForkedBeaconBlock, blck: deneb.BeaconBlock): T = template init*(T: type ForkedBeaconBlock, blck: deneb.BeaconBlock): T =
T(kind: ConsensusFork.Deneb, denebData: blck) T(kind: ConsensusFork.Deneb, denebData: blck)
template init*(T: type ForkedTrustedBeaconBlock, blck: phase0.TrustedBeaconBlock): T =
T(kind: ConsensusFork.Phase0, phase0Data: blck)
template init*(T: type ForkedTrustedBeaconBlock, blck: altair.TrustedBeaconBlock): T =
T(kind: ConsensusFork.Altair, altairData: blck)
template init*(T: type ForkedTrustedBeaconBlock, blck: bellatrix.TrustedBeaconBlock): T =
T(kind: ConsensusFork.Bellatrix, bellatrixData: blck)
template init*(T: type ForkedTrustedBeaconBlock, blck: capella.TrustedBeaconBlock): T =
T(kind: ConsensusFork.Capella, capellaData: blck)
template init*(T: type ForkedSignedBeaconBlock, blck: phase0.SignedBeaconBlock): T = template init*(T: type ForkedSignedBeaconBlock, blck: phase0.SignedBeaconBlock): T =
T(kind: ConsensusFork.Phase0, phase0Data: blck) T(kind: ConsensusFork.Phase0, phase0Data: blck)
template init*(T: type ForkedSignedBeaconBlock, blck: altair.SignedBeaconBlock): T = template init*(T: type ForkedSignedBeaconBlock, blck: altair.SignedBeaconBlock): T =