Skip to content

Commit a543765

Browse files
committed
Reduce boilerplate for future forks
1 parent 3909fef commit a543765

File tree

20 files changed

+280
-440
lines changed

20 files changed

+280
-440
lines changed

beacon_node/beacon_chain/src/attestation_rewards.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,10 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
5151
.get_state(&state_root, Some(state_slot))?
5252
.ok_or(BeaconChainError::MissingBeaconState(state_root))?;
5353

54-
match state {
55-
BeaconState::Base(_) => self.compute_attestation_rewards_base(state, validators),
56-
BeaconState::Altair(_)
57-
| BeaconState::Bellatrix(_)
58-
| BeaconState::Capella(_)
59-
| BeaconState::Deneb(_)
60-
| BeaconState::Electra(_)
61-
| BeaconState::Fulu(_) => self.compute_attestation_rewards_altair(state, validators),
54+
if state.fork_name_unchecked().altair_enabled() {
55+
self.compute_attestation_rewards_altair(state, validators)
56+
} else {
57+
self.compute_attestation_rewards_base(state, validators)
6258
}
6359
}
6460

beacon_node/beacon_chain/src/execution_payload.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -374,22 +374,15 @@ pub fn get_execution_payload<T: BeaconChainTypes>(
374374
let latest_execution_payload_header = state.latest_execution_payload_header()?;
375375
let latest_execution_payload_header_block_hash = latest_execution_payload_header.block_hash();
376376
let latest_execution_payload_header_gas_limit = latest_execution_payload_header.gas_limit();
377-
let withdrawals = match state {
378-
&BeaconState::Capella(_)
379-
| &BeaconState::Deneb(_)
380-
| &BeaconState::Electra(_)
381-
| &BeaconState::Fulu(_) => Some(get_expected_withdrawals(state, spec)?.0.into()),
382-
&BeaconState::Bellatrix(_) => None,
383-
// These shouldn't happen but they're here to make the pattern irrefutable
384-
&BeaconState::Base(_) | &BeaconState::Altair(_) => None,
377+
let withdrawals = if state.fork_name_unchecked().capella_enabled() {
378+
Some(get_expected_withdrawals(state, spec)?.0.into())
379+
} else {
380+
None
385381
};
386-
let parent_beacon_block_root = match state {
387-
BeaconState::Deneb(_) | BeaconState::Electra(_) | BeaconState::Fulu(_) => {
388-
Some(parent_block_root)
389-
}
390-
BeaconState::Bellatrix(_) | BeaconState::Capella(_) => None,
391-
// These shouldn't happen but they're here to make the pattern irrefutable
392-
BeaconState::Base(_) | BeaconState::Altair(_) => None,
382+
let parent_beacon_block_root = if state.fork_name_unchecked().deneb_enabled() {
383+
Some(parent_block_root)
384+
} else {
385+
None
393386
};
394387

395388
// Spawn a task to obtain the execution payload from the EL via a series of async calls. The

beacon_node/beacon_chain/src/test_utils.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -920,15 +920,12 @@ where
920920
&self.spec,
921921
));
922922

923-
let block_contents: SignedBlockContentsTuple<E> = match *signed_block {
924-
SignedBeaconBlock::Base(_)
925-
| SignedBeaconBlock::Altair(_)
926-
| SignedBeaconBlock::Bellatrix(_)
927-
| SignedBeaconBlock::Capella(_) => (signed_block, None),
928-
SignedBeaconBlock::Deneb(_)
929-
| SignedBeaconBlock::Electra(_)
930-
| SignedBeaconBlock::Fulu(_) => (signed_block, block_response.blob_items),
931-
};
923+
let block_contents: SignedBlockContentsTuple<E> =
924+
if signed_block.fork_name_unchecked().deneb_enabled() {
925+
(signed_block, block_response.blob_items)
926+
} else {
927+
(signed_block, None)
928+
};
932929

933930
(block_contents, block_response.state)
934931
}

beacon_node/lighthouse_network/src/rpc/codec.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -685,18 +685,18 @@ fn handle_rpc_response<E: EthSpec>(
685685
SignedBeaconBlock::Base(SignedBeaconBlockBase::from_ssz_bytes(decoded_buffer)?),
686686
)))),
687687
SupportedProtocol::BlobsByRangeV1 => match fork_name {
688-
Some(ForkName::Deneb) | Some(ForkName::Electra) | Some(ForkName::Fulu) => {
689-
Ok(Some(RpcSuccessResponse::BlobsByRange(Arc::new(
690-
BlobSidecar::from_ssz_bytes(decoded_buffer)?,
691-
))))
688+
Some(fork_name) => {
689+
if fork_name.deneb_enabled() {
690+
Ok(Some(RpcSuccessResponse::BlobsByRange(Arc::new(
691+
BlobSidecar::from_ssz_bytes(decoded_buffer)?,
692+
))))
693+
} else {
694+
Err(RPCError::ErrorResponse(
695+
RpcErrorResponse::InvalidRequest,
696+
"Invalid fork name for blobs by range".to_string(),
697+
))
698+
}
692699
}
693-
Some(ForkName::Base)
694-
| Some(ForkName::Altair)
695-
| Some(ForkName::Bellatrix)
696-
| Some(ForkName::Capella) => Err(RPCError::ErrorResponse(
697-
RpcErrorResponse::InvalidRequest,
698-
"Invalid fork name for blobs by range".to_string(),
699-
)),
700700
None => Err(RPCError::ErrorResponse(
701701
RpcErrorResponse::InvalidRequest,
702702
format!(
@@ -706,18 +706,18 @@ fn handle_rpc_response<E: EthSpec>(
706706
)),
707707
},
708708
SupportedProtocol::BlobsByRootV1 => match fork_name {
709-
Some(ForkName::Deneb) | Some(ForkName::Electra) | Some(ForkName::Fulu) => {
710-
Ok(Some(RpcSuccessResponse::BlobsByRoot(Arc::new(
711-
BlobSidecar::from_ssz_bytes(decoded_buffer)?,
712-
))))
709+
Some(fork_name) => {
710+
if fork_name.deneb_enabled() {
711+
Ok(Some(RpcSuccessResponse::BlobsByRoot(Arc::new(
712+
BlobSidecar::from_ssz_bytes(decoded_buffer)?,
713+
))))
714+
} else {
715+
Err(RPCError::ErrorResponse(
716+
RpcErrorResponse::InvalidRequest,
717+
"Invalid fork name for blobs by root".to_string(),
718+
))
719+
}
713720
}
714-
Some(ForkName::Base)
715-
| Some(ForkName::Altair)
716-
| Some(ForkName::Bellatrix)
717-
| Some(ForkName::Capella) => Err(RPCError::ErrorResponse(
718-
RpcErrorResponse::InvalidRequest,
719-
"Invalid fork name for blobs by root".to_string(),
720-
)),
721721
None => Err(RPCError::ErrorResponse(
722722
RpcErrorResponse::InvalidRequest,
723723
format!(

beacon_node/network/src/sync/tests/lookups.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,7 @@ impl TestRig {
174174
}
175175

176176
pub fn after_deneb(&self) -> bool {
177-
matches!(
178-
self.fork_name,
179-
ForkName::Deneb | ForkName::Electra | ForkName::Fulu
180-
)
177+
self.fork_name.deneb_enabled()
181178
}
182179

183180
fn trigger_unknown_parent_block(&mut self, peer_id: PeerId, block: Arc<SignedBeaconBlock<E>>) {

beacon_node/operation_pool/src/lib.rs

Lines changed: 28 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,14 +1243,11 @@ mod release_tests {
12431243
let stats = op_pool.attestation_stats();
12441244
let fork_name = state.fork_name_unchecked();
12451245

1246-
match fork_name {
1247-
ForkName::Electra | ForkName::Fulu => {
1248-
assert_eq!(stats.num_attestation_data, 1);
1249-
}
1250-
_ => {
1251-
assert_eq!(stats.num_attestation_data, committees.len());
1252-
}
1253-
};
1246+
if fork_name.electra_enabled() {
1247+
assert_eq!(stats.num_attestation_data, 1);
1248+
} else {
1249+
assert_eq!(stats.num_attestation_data, committees.len());
1250+
}
12541251

12551252
assert_eq!(
12561253
stats.num_attestations,
@@ -1262,25 +1259,19 @@ mod release_tests {
12621259
let best_attestations = op_pool
12631260
.get_attestations(&state, |_| true, |_| true, spec)
12641261
.expect("should have best attestations");
1265-
match fork_name {
1266-
ForkName::Electra | ForkName::Fulu => {
1267-
assert_eq!(best_attestations.len(), 8);
1268-
}
1269-
_ => {
1270-
assert_eq!(best_attestations.len(), max_attestations);
1271-
}
1272-
};
1262+
if fork_name.electra_enabled() {
1263+
assert_eq!(best_attestations.len(), 8);
1264+
} else {
1265+
assert_eq!(best_attestations.len(), max_attestations);
1266+
}
12731267

12741268
// All the best attestations should be signed by at least `big_step_size` (4) validators.
12751269
for att in &best_attestations {
1276-
match fork_name {
1277-
ForkName::Electra | ForkName::Fulu => {
1278-
assert!(att.num_set_aggregation_bits() >= small_step_size);
1279-
}
1280-
_ => {
1281-
assert!(att.num_set_aggregation_bits() >= big_step_size);
1282-
}
1283-
};
1270+
if fork_name.electra_enabled() {
1271+
assert!(att.num_set_aggregation_bits() >= small_step_size);
1272+
} else {
1273+
assert!(att.num_set_aggregation_bits() >= big_step_size);
1274+
}
12841275
}
12851276
}
12861277

@@ -1361,17 +1352,14 @@ mod release_tests {
13611352
let num_big = target_committee_size / big_step_size;
13621353
let fork_name = state.fork_name_unchecked();
13631354

1364-
match fork_name {
1365-
ForkName::Electra | ForkName::Fulu => {
1366-
assert_eq!(op_pool.attestation_stats().num_attestation_data, 1);
1367-
}
1368-
_ => {
1369-
assert_eq!(
1370-
op_pool.attestation_stats().num_attestation_data,
1371-
committees.len()
1372-
);
1373-
}
1374-
};
1355+
if fork_name.electra_enabled() {
1356+
assert_eq!(op_pool.attestation_stats().num_attestation_data, 1);
1357+
} else {
1358+
assert_eq!(
1359+
op_pool.attestation_stats().num_attestation_data,
1360+
committees.len()
1361+
);
1362+
}
13751363

13761364
assert_eq!(
13771365
op_pool.num_attestations(),
@@ -1384,14 +1372,11 @@ mod release_tests {
13841372
.get_attestations(&state, |_| true, |_| true, spec)
13851373
.expect("should have valid best attestations");
13861374

1387-
match fork_name {
1388-
ForkName::Electra | ForkName::Fulu => {
1389-
assert_eq!(best_attestations.len(), 8);
1390-
}
1391-
_ => {
1392-
assert_eq!(best_attestations.len(), max_attestations);
1393-
}
1394-
};
1375+
if fork_name.electra_enabled() {
1376+
assert_eq!(best_attestations.len(), 8);
1377+
} else {
1378+
assert_eq!(best_attestations.len(), max_attestations);
1379+
}
13951380

13961381
let total_active_balance = state.get_total_active_balance().unwrap();
13971382

common/eth2/src/types.rs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,16 +1864,10 @@ impl<E: EthSpec> PublishBlockRequest<E> {
18641864
impl<E: EthSpec> TryFrom<Arc<SignedBeaconBlock<E>>> for PublishBlockRequest<E> {
18651865
type Error = &'static str;
18661866
fn try_from(block: Arc<SignedBeaconBlock<E>>) -> Result<Self, Self::Error> {
1867-
match *block {
1868-
SignedBeaconBlock::Base(_)
1869-
| SignedBeaconBlock::Altair(_)
1870-
| SignedBeaconBlock::Bellatrix(_)
1871-
| SignedBeaconBlock::Capella(_) => Ok(PublishBlockRequest::Block(block)),
1872-
SignedBeaconBlock::Deneb(_)
1873-
| SignedBeaconBlock::Electra(_)
1874-
| SignedBeaconBlock::Fulu(_) => Err(
1875-
"post-Deneb block contents cannot be fully constructed from just the signed block",
1876-
),
1867+
if block.message().fork_name_unchecked().deneb_enabled() {
1868+
Err("post-Deneb block contents cannot be fully constructed from just the signed block")
1869+
} else {
1870+
Ok(PublishBlockRequest::Block(block))
18771871
}
18781872
}
18791873
}
@@ -1977,16 +1971,18 @@ impl<E: EthSpec> ForkVersionDeserialize for FullPayloadContents<E> {
19771971
value: Value,
19781972
fork_name: ForkName,
19791973
) -> Result<Self, D::Error> {
1980-
match fork_name {
1981-
ForkName::Bellatrix | ForkName::Capella => serde_json::from_value(value)
1982-
.map(Self::Payload)
1983-
.map_err(serde::de::Error::custom),
1984-
ForkName::Deneb | ForkName::Electra | ForkName::Fulu => serde_json::from_value(value)
1974+
if fork_name.deneb_enabled() {
1975+
serde_json::from_value(value)
19851976
.map(Self::PayloadAndBlobs)
1986-
.map_err(serde::de::Error::custom),
1987-
ForkName::Base | ForkName::Altair => Err(serde::de::Error::custom(format!(
1977+
.map_err(serde::de::Error::custom)
1978+
} else if fork_name.bellatrix_enabled() {
1979+
serde_json::from_value(value)
1980+
.map(Self::Payload)
1981+
.map_err(serde::de::Error::custom)
1982+
} else {
1983+
Err(serde::de::Error::custom(format!(
19881984
"FullPayloadContents deserialization for {fork_name} not implemented"
1989-
))),
1985+
)))
19901986
}
19911987
}
19921988
}

consensus/fork_choice/src/fork_choice.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -755,21 +755,15 @@ where
755755
if let Some((parent_justified, parent_finalized)) = parent_checkpoints {
756756
(parent_justified, parent_finalized)
757757
} else {
758-
let justification_and_finalization_state = match block {
759-
BeaconBlockRef::Fulu(_)
760-
| BeaconBlockRef::Electra(_)
761-
| BeaconBlockRef::Deneb(_)
762-
| BeaconBlockRef::Capella(_)
763-
| BeaconBlockRef::Bellatrix(_)
764-
| BeaconBlockRef::Altair(_) => {
758+
let justification_and_finalization_state =
759+
if block.fork_name_unchecked().altair_enabled() {
765760
// NOTE: Processing justification & finalization requires the progressive
766761
// balances cache, but we cannot initialize it here as we only have an
767762
// immutable reference. The state *should* have come straight from block
768763
// processing, which initialises the cache, but if we add other `on_block`
769764
// calls in future it could be worth passing a mutable reference.
770765
per_epoch_processing::altair::process_justification_and_finalization(state)?
771-
}
772-
BeaconBlockRef::Base(_) => {
766+
} else {
773767
let mut validator_statuses =
774768
per_epoch_processing::base::ValidatorStatuses::new(state, spec)
775769
.map_err(Error::ValidatorStatuses)?;
@@ -781,8 +775,7 @@ where
781775
&validator_statuses.total_balances,
782776
spec,
783777
)?
784-
}
785-
};
778+
};
786779

787780
(
788781
justification_and_finalization_state.current_justified_checkpoint(),

consensus/state_processing/src/common/get_attestation_participation.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,14 @@ pub fn get_attestation_participation_flag_indices<E: EthSpec>(
4444
if is_matching_source && inclusion_delay <= E::slots_per_epoch().integer_sqrt() {
4545
participation_flag_indices.push(TIMELY_SOURCE_FLAG_INDEX);
4646
}
47-
match state {
48-
&BeaconState::Base(_)
49-
| &BeaconState::Altair(_)
50-
| &BeaconState::Bellatrix(_)
51-
| &BeaconState::Capella(_) => {
52-
if is_matching_target && inclusion_delay <= E::slots_per_epoch() {
53-
participation_flag_indices.push(TIMELY_TARGET_FLAG_INDEX);
54-
}
47+
if state.fork_name_unchecked().deneb_enabled() {
48+
if is_matching_target {
49+
// [Modified in Deneb:EIP7045]
50+
participation_flag_indices.push(TIMELY_TARGET_FLAG_INDEX);
5551
}
56-
&BeaconState::Deneb(_) | &BeaconState::Electra(_) | &BeaconState::Fulu(_) => {
57-
if is_matching_target {
58-
// [Modified in Deneb:EIP7045]
59-
participation_flag_indices.push(TIMELY_TARGET_FLAG_INDEX);
60-
}
52+
} else {
53+
if is_matching_target && inclusion_delay <= E::slots_per_epoch() {
54+
participation_flag_indices.push(TIMELY_TARGET_FLAG_INDEX);
6155
}
6256
}
6357
if is_matching_head && inclusion_delay == spec.min_attestation_inclusion_delay {

consensus/state_processing/src/common/slash_validator.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,12 @@ pub fn slash_validator<E: EthSpec>(
5555
let whistleblower_index = opt_whistleblower_index.unwrap_or(proposer_index);
5656
let whistleblower_reward = validator_effective_balance
5757
.safe_div(spec.whistleblower_reward_quotient_for_state(state))?;
58-
let proposer_reward = match state {
59-
BeaconState::Base(_) => whistleblower_reward.safe_div(spec.proposer_reward_quotient)?,
60-
BeaconState::Altair(_)
61-
| BeaconState::Bellatrix(_)
62-
| BeaconState::Capella(_)
63-
| BeaconState::Deneb(_)
64-
| BeaconState::Electra(_)
65-
| BeaconState::Fulu(_) => whistleblower_reward
58+
let proposer_reward = if state.fork_name_unchecked().altair_enabled() {
59+
whistleblower_reward
6660
.safe_mul(PROPOSER_WEIGHT)?
67-
.safe_div(WEIGHT_DENOMINATOR)?,
61+
.safe_div(WEIGHT_DENOMINATOR)?
62+
} else {
63+
whistleblower_reward.safe_div(spec.proposer_reward_quotient)?
6864
};
6965

7066
// Ensure the whistleblower index is in the validator registry.

0 commit comments

Comments
 (0)