Skip to content

Commit e6c7f14

Browse files
superstruct the AttesterSlashing (#5636)
* `superstruct` Attester Fork Variants * Push a little further * Deal with Encode / Decode of AttesterSlashing * not so sure about this.. * Stop Encode/Decode Bounds from Propagating Out * Tons of Changes.. * More Conversions to AttestationRef * Add AsReference trait (#15) * Add AsReference trait * Fix some snafus * Got it Compiling! :D * Got Tests Building * Get beacon chain tests compiling --------- Co-authored-by: Michael Sproul <[email protected]>
1 parent 3b7132b commit e6c7f14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1408
-440
lines changed

beacon_node/beacon_chain/src/attestation_verification.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ impl<'a, T: BeaconChainTypes> IndexedAggregatedAttestation<'a, T> {
471471
if chain
472472
.observed_attestations
473473
.write()
474-
.is_known_subset(attestation, attestation_data_root)
474+
.is_known_subset(attestation.to_ref(), attestation_data_root)
475475
.map_err(|e| Error::BeaconChainError(e.into()))?
476476
{
477477
metrics::inc_counter(&metrics::AGGREGATED_ATTESTATION_SUBSETS);
@@ -559,7 +559,7 @@ impl<'a, T: BeaconChainTypes> IndexedAggregatedAttestation<'a, T> {
559559
return Err(Error::AggregatorNotInCommittee { aggregator_index });
560560
}
561561

562-
get_indexed_attestation(committee.committee, attestation)
562+
get_indexed_attestation(committee.committee, attestation.to_ref())
563563
.map_err(|e| BeaconChainError::from(e).into())
564564
};
565565

@@ -597,7 +597,7 @@ impl<'a, T: BeaconChainTypes> VerifiedAggregatedAttestation<'a, T> {
597597
if let ObserveOutcome::Subset = chain
598598
.observed_attestations
599599
.write()
600-
.observe_item(attestation, Some(attestation_data_root))
600+
.observe_item(attestation.to_ref(), Some(attestation_data_root))
601601
.map_err(|e| Error::BeaconChainError(e.into()))?
602602
{
603603
metrics::inc_counter(&metrics::AGGREGATED_ATTESTATION_SUBSETS);
@@ -1241,7 +1241,7 @@ pub fn obtain_indexed_attestation_and_committees_per_slot<T: BeaconChainTypes>(
12411241
attestation: &Attestation<T::EthSpec>,
12421242
) -> Result<(IndexedAttestation<T::EthSpec>, CommitteesPerSlot), Error> {
12431243
map_attestation_committee(chain, attestation, |(committee, committees_per_slot)| {
1244-
get_indexed_attestation(committee.committee, attestation)
1244+
get_indexed_attestation(committee.committee, attestation.to_ref())
12451245
.map(|attestation| (attestation, committees_per_slot))
12461246
.map_err(Error::Invalid)
12471247
})

beacon_node/beacon_chain/src/beacon_chain.rs

Lines changed: 88 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
21132113
.fork_choice_write_lock()
21142114
.on_attestation(
21152115
self.slot()?,
2116-
verified.indexed_attestation(),
2116+
verified.indexed_attestation().to_ref(),
21172117
AttestationFromBlock::False,
21182118
)
21192119
.map_err(Into::into)
@@ -2465,7 +2465,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
24652465
// Add to fork choice.
24662466
self.canonical_head
24672467
.fork_choice_write_lock()
2468-
.on_attester_slashing(attester_slashing.as_inner());
2468+
.on_attester_slashing(attester_slashing.as_inner().to_ref());
24692469

24702470
// Add to the op pool (if we have the ability to propose blocks).
24712471
if self.eth1_chain.is_some() {
@@ -3820,7 +3820,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
38203820
continue;
38213821
}
38223822
};
3823-
slasher.accept_attestation(indexed_attestation.clone());
3823+
slasher.accept_attestation(indexed_attestation.clone_as_indexed_attestation());
38243824
}
38253825
}
38263826
}
@@ -3839,7 +3839,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
38393839
if block.slot() + 2 * T::EthSpec::slots_per_epoch() >= current_slot {
38403840
metrics::observe(
38413841
&metrics::OPERATIONS_PER_BLOCK_ATTESTATION,
3842-
block.body().attestations().len() as f64,
3842+
block.body().attestations().count() as f64,
38433843
);
38443844

38453845
if let Ok(sync_aggregate) = block.body().sync_aggregate() {
@@ -4859,7 +4859,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
48594859
metrics::start_timer(&metrics::BLOCK_PRODUCTION_UNAGGREGATED_TIMES);
48604860
for attestation in self.naive_aggregation_pool.read().iter() {
48614861
let import = |attestation: &Attestation<T::EthSpec>| {
4862-
let attesting_indices = get_attesting_indices_from_state(&state, attestation)?;
4862+
let attesting_indices =
4863+
get_attesting_indices_from_state(&state, attestation.to_ref())?;
48634864
self.op_pool
48644865
.insert_attestation(attestation.clone(), attesting_indices)
48654866
};
@@ -4909,7 +4910,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
49094910
attestations.retain(|att| {
49104911
verify_attestation_for_block_inclusion(
49114912
&state,
4912-
att,
4913+
att.to_ref(),
49134914
&mut tmp_ctxt,
49144915
VerifySignatures::True,
49154916
&self.spec,
@@ -5040,6 +5041,72 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
50405041
bls_to_execution_changes,
50415042
} = partial_beacon_block;
50425043

5044+
let (attester_slashings_base, attester_slashings_electra) =
5045+
attester_slashings.into_iter().fold(
5046+
(Vec::new(), Vec::new()),
5047+
|(mut base, mut electra), slashing| {
5048+
match slashing {
5049+
AttesterSlashing::Base(slashing) => base.push(slashing),
5050+
AttesterSlashing::Electra(slashing) => electra.push(slashing),
5051+
}
5052+
(base, electra)
5053+
},
5054+
);
5055+
let (attestations_base, attestations_electra) = attestations.into_iter().fold(
5056+
(Vec::new(), Vec::new()),
5057+
|(mut base, mut electra), attestation| {
5058+
match attestation {
5059+
Attestation::Base(attestation) => base.push(attestation),
5060+
Attestation::Electra(attestation) => electra.push(attestation),
5061+
}
5062+
(base, electra)
5063+
},
5064+
);
5065+
5066+
// TODO(electra): figure out what should *actually* be done here when we have attestations / attester_slashings of the wrong type
5067+
match &state {
5068+
BeaconState::Base(_)
5069+
| BeaconState::Altair(_)
5070+
| BeaconState::Merge(_)
5071+
| BeaconState::Capella(_)
5072+
| BeaconState::Deneb(_) => {
5073+
if !attestations_electra.is_empty() {
5074+
error!(
5075+
self.log,
5076+
"Tried to produce block with attestations of the wrong type";
5077+
"slot" => slot,
5078+
"attestations" => attestations_electra.len(),
5079+
);
5080+
}
5081+
if !attester_slashings_electra.is_empty() {
5082+
error!(
5083+
self.log,
5084+
"Tried to produce block with attester slashings of the wrong type";
5085+
"slot" => slot,
5086+
"attester_slashings" => attester_slashings_electra.len(),
5087+
);
5088+
}
5089+
}
5090+
BeaconState::Electra(_) => {
5091+
if !attestations_base.is_empty() {
5092+
error!(
5093+
self.log,
5094+
"Tried to produce block with attestations of the wrong type";
5095+
"slot" => slot,
5096+
"attestations" => attestations_base.len(),
5097+
);
5098+
}
5099+
if !attester_slashings_base.is_empty() {
5100+
error!(
5101+
self.log,
5102+
"Tried to produce block with attester slashings of the wrong type";
5103+
"slot" => slot,
5104+
"attester_slashings" => attester_slashings_base.len(),
5105+
);
5106+
}
5107+
}
5108+
};
5109+
50435110
let (inner_block, maybe_blobs_and_proofs, execution_payload_value) = match &state {
50445111
BeaconState::Base(_) => (
50455112
BeaconBlock::Base(BeaconBlockBase {
@@ -5052,8 +5119,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
50525119
eth1_data,
50535120
graffiti,
50545121
proposer_slashings: proposer_slashings.into(),
5055-
attester_slashings: attester_slashings.into(),
5056-
attestations: attestations.into(),
5122+
attester_slashings: attester_slashings_base.into(),
5123+
attestations: attestations_base.into(),
50575124
deposits: deposits.into(),
50585125
voluntary_exits: voluntary_exits.into(),
50595126
_phantom: PhantomData,
@@ -5073,8 +5140,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
50735140
eth1_data,
50745141
graffiti,
50755142
proposer_slashings: proposer_slashings.into(),
5076-
attester_slashings: attester_slashings.into(),
5077-
attestations: attestations.into(),
5143+
attester_slashings: attester_slashings_base.into(),
5144+
attestations: attestations_base.into(),
50785145
deposits: deposits.into(),
50795146
voluntary_exits: voluntary_exits.into(),
50805147
sync_aggregate: sync_aggregate
@@ -5100,8 +5167,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
51005167
eth1_data,
51015168
graffiti,
51025169
proposer_slashings: proposer_slashings.into(),
5103-
attester_slashings: attester_slashings.into(),
5104-
attestations: attestations.into(),
5170+
attester_slashings: attester_slashings_base.into(),
5171+
attestations: attestations_base.into(),
51055172
deposits: deposits.into(),
51065173
voluntary_exits: voluntary_exits.into(),
51075174
sync_aggregate: sync_aggregate
@@ -5132,8 +5199,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
51325199
eth1_data,
51335200
graffiti,
51345201
proposer_slashings: proposer_slashings.into(),
5135-
attester_slashings: attester_slashings.into(),
5136-
attestations: attestations.into(),
5202+
attester_slashings: attester_slashings_base.into(),
5203+
attestations: attestations_base.into(),
51375204
deposits: deposits.into(),
51385205
voluntary_exits: voluntary_exits.into(),
51395206
sync_aggregate: sync_aggregate
@@ -5166,8 +5233,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
51665233
eth1_data,
51675234
graffiti,
51685235
proposer_slashings: proposer_slashings.into(),
5169-
attester_slashings: attester_slashings.into(),
5170-
attestations: attestations.into(),
5236+
attester_slashings: attester_slashings_base.into(),
5237+
attestations: attestations_base.into(),
51715238
deposits: deposits.into(),
51725239
voluntary_exits: voluntary_exits.into(),
51735240
sync_aggregate: sync_aggregate
@@ -5204,8 +5271,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
52045271
eth1_data,
52055272
graffiti,
52065273
proposer_slashings: proposer_slashings.into(),
5207-
attester_slashings: attester_slashings.into(),
5208-
attestations: attestations.into(),
5274+
attester_slashings: attester_slashings_electra.into(),
5275+
attestations: attestations_electra.into(),
52095276
deposits: deposits.into(),
52105277
voluntary_exits: voluntary_exits.into(),
52115278
sync_aggregate: sync_aggregate
@@ -5216,6 +5283,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
52165283
bls_to_execution_changes: bls_to_execution_changes.into(),
52175284
blob_kzg_commitments: kzg_commitments
52185285
.ok_or(BlockProductionError::InvalidPayloadFork)?,
5286+
// TODO(electra): finish consolidations when they're more spec'd out
5287+
consolidations: Vec::new().into(),
52195288
},
52205289
}),
52215290
maybe_blobs_and_proofs,
@@ -5321,7 +5390,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
53215390
self.log,
53225391
"Produced beacon block";
53235392
"parent" => ?block.parent_root(),
5324-
"attestations" => block.body().attestations().len(),
5393+
"attestations" => block.body().attestations_len(),
53255394
"slot" => block.slot()
53265395
);
53275396

beacon_node/beacon_chain/src/block_reward.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
2727
let split_attestations = block
2828
.body()
2929
.attestations()
30-
.iter()
3130
.map(|att| {
3231
let attesting_indices = get_attesting_indices_from_state(state, att)?;
33-
Ok(SplitAttestation::new(att.clone(), attesting_indices))
32+
Ok(SplitAttestation::new(
33+
att.clone_as_attestation(),
34+
attesting_indices,
35+
))
3436
})
3537
.collect::<Result<Vec<_>, BeaconChainError>>()?;
3638

@@ -86,7 +88,6 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
8688
block
8789
.body()
8890
.attestations()
89-
.iter()
9091
.map(|a| a.data().clone())
9192
.collect()
9293
} else {

beacon_node/beacon_chain/src/block_verification.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ impl<T: BeaconChainTypes> ExecutionPendingBlock<T> {
16231623
}
16241624

16251625
// Register each attestation in the block with fork choice.
1626-
for (i, attestation) in block.message().body().attestations().iter().enumerate() {
1626+
for (i, attestation) in block.message().body().attestations().enumerate() {
16271627
let _fork_choice_attestation_timer =
16281628
metrics::start_timer(&metrics::FORK_CHOICE_PROCESS_ATTESTATION_TIMES);
16291629

0 commit comments

Comments
 (0)