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 <micsproul@gmail.com>
This commit is contained in:
ethDreamer
2024-05-02 18:00:21 -05:00
committed by GitHub
parent 3b7132bc0d
commit e6c7f145dd
53 changed files with 1405 additions and 437 deletions

View File

@@ -2113,7 +2113,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.fork_choice_write_lock()
.on_attestation(
self.slot()?,
verified.indexed_attestation(),
verified.indexed_attestation().to_ref(),
AttestationFromBlock::False,
)
.map_err(Into::into)
@@ -2465,7 +2465,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// Add to fork choice.
self.canonical_head
.fork_choice_write_lock()
.on_attester_slashing(attester_slashing.as_inner());
.on_attester_slashing(attester_slashing.as_inner().to_ref());
// Add to the op pool (if we have the ability to propose blocks).
if self.eth1_chain.is_some() {
@@ -3820,7 +3820,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
continue;
}
};
slasher.accept_attestation(indexed_attestation.clone());
slasher.accept_attestation(indexed_attestation.clone_as_indexed_attestation());
}
}
}
@@ -3839,7 +3839,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
if block.slot() + 2 * T::EthSpec::slots_per_epoch() >= current_slot {
metrics::observe(
&metrics::OPERATIONS_PER_BLOCK_ATTESTATION,
block.body().attestations().len() as f64,
block.body().attestations().count() as f64,
);
if let Ok(sync_aggregate) = block.body().sync_aggregate() {
@@ -4859,7 +4859,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
metrics::start_timer(&metrics::BLOCK_PRODUCTION_UNAGGREGATED_TIMES);
for attestation in self.naive_aggregation_pool.read().iter() {
let import = |attestation: &Attestation<T::EthSpec>| {
let attesting_indices = get_attesting_indices_from_state(&state, attestation)?;
let attesting_indices =
get_attesting_indices_from_state(&state, attestation.to_ref())?;
self.op_pool
.insert_attestation(attestation.clone(), attesting_indices)
};
@@ -4909,7 +4910,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
attestations.retain(|att| {
verify_attestation_for_block_inclusion(
&state,
att,
att.to_ref(),
&mut tmp_ctxt,
VerifySignatures::True,
&self.spec,
@@ -5040,6 +5041,72 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
bls_to_execution_changes,
} = partial_beacon_block;
let (attester_slashings_base, attester_slashings_electra) =
attester_slashings.into_iter().fold(
(Vec::new(), Vec::new()),
|(mut base, mut electra), slashing| {
match slashing {
AttesterSlashing::Base(slashing) => base.push(slashing),
AttesterSlashing::Electra(slashing) => electra.push(slashing),
}
(base, electra)
},
);
let (attestations_base, attestations_electra) = attestations.into_iter().fold(
(Vec::new(), Vec::new()),
|(mut base, mut electra), attestation| {
match attestation {
Attestation::Base(attestation) => base.push(attestation),
Attestation::Electra(attestation) => electra.push(attestation),
}
(base, electra)
},
);
// TODO(electra): figure out what should *actually* be done here when we have attestations / attester_slashings of the wrong type
match &state {
BeaconState::Base(_)
| BeaconState::Altair(_)
| BeaconState::Merge(_)
| BeaconState::Capella(_)
| BeaconState::Deneb(_) => {
if !attestations_electra.is_empty() {
error!(
self.log,
"Tried to produce block with attestations of the wrong type";
"slot" => slot,
"attestations" => attestations_electra.len(),
);
}
if !attester_slashings_electra.is_empty() {
error!(
self.log,
"Tried to produce block with attester slashings of the wrong type";
"slot" => slot,
"attester_slashings" => attester_slashings_electra.len(),
);
}
}
BeaconState::Electra(_) => {
if !attestations_base.is_empty() {
error!(
self.log,
"Tried to produce block with attestations of the wrong type";
"slot" => slot,
"attestations" => attestations_base.len(),
);
}
if !attester_slashings_base.is_empty() {
error!(
self.log,
"Tried to produce block with attester slashings of the wrong type";
"slot" => slot,
"attester_slashings" => attester_slashings_base.len(),
);
}
}
};
let (inner_block, maybe_blobs_and_proofs, execution_payload_value) = match &state {
BeaconState::Base(_) => (
BeaconBlock::Base(BeaconBlockBase {
@@ -5052,8 +5119,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_base.into(),
attestations: attestations_base.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
_phantom: PhantomData,
@@ -5073,8 +5140,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_base.into(),
attestations: attestations_base.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
sync_aggregate: sync_aggregate
@@ -5100,8 +5167,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_base.into(),
attestations: attestations_base.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
sync_aggregate: sync_aggregate
@@ -5132,8 +5199,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_base.into(),
attestations: attestations_base.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
sync_aggregate: sync_aggregate
@@ -5166,8 +5233,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_base.into(),
attestations: attestations_base.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
sync_aggregate: sync_aggregate
@@ -5204,8 +5271,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
eth1_data,
graffiti,
proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(),
attestations: attestations.into(),
attester_slashings: attester_slashings_electra.into(),
attestations: attestations_electra.into(),
deposits: deposits.into(),
voluntary_exits: voluntary_exits.into(),
sync_aggregate: sync_aggregate
@@ -5216,6 +5283,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
bls_to_execution_changes: bls_to_execution_changes.into(),
blob_kzg_commitments: kzg_commitments
.ok_or(BlockProductionError::InvalidPayloadFork)?,
// TODO(electra): finish consolidations when they're more spec'd out
consolidations: Vec::new().into(),
},
}),
maybe_blobs_and_proofs,
@@ -5321,7 +5390,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
self.log,
"Produced beacon block";
"parent" => ?block.parent_root(),
"attestations" => block.body().attestations().len(),
"attestations" => block.body().attestations_len(),
"slot" => block.slot()
);