De-dup attestation constructor logic

This commit is contained in:
dapplion
2024-06-17 15:44:32 +02:00
parent 9a01b6b363
commit d87541c045
3 changed files with 48 additions and 56 deletions

View File

@@ -4,6 +4,7 @@ use super::{
SignedRoot, SignedRoot,
}; };
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use crate::Attestation;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode}; use ssz_derive::{Decode, Encode};
use superstruct::superstruct; use superstruct::superstruct;
@@ -88,28 +89,39 @@ impl<E: EthSpec> AggregateAndProof<E> {
genesis_validators_root: Hash256, genesis_validators_root: Hash256,
spec: &ChainSpec, spec: &ChainSpec,
) -> Self { ) -> Self {
let selection_proof = selection_proof let selection_proof = selection_proof.unwrap_or_else(|| {
.unwrap_or_else(|| { SelectionProof::new::<E>(
SelectionProof::new::<E>( aggregate.data().slot,
aggregate.data().slot, secret_key,
secret_key, fork,
fork, genesis_validators_root,
genesis_validators_root, spec,
spec, )
) });
})
.into();
Self::from_attestation(
aggregator_index,
aggregate.clone_as_attestation(),
selection_proof,
)
}
/// Produces a new `AggregateAndProof` given a `selection_proof`
pub fn from_attestation(
aggregator_index: u64,
aggregate: Attestation<E>,
selection_proof: SelectionProof,
) -> Self {
match aggregate { match aggregate {
AttestationRef::Base(attestation) => Self::Base(AggregateAndProofBase { Attestation::Base(aggregate) => Self::Base(AggregateAndProofBase {
aggregator_index, aggregator_index,
aggregate: attestation.clone(), aggregate,
selection_proof, selection_proof: selection_proof.into(),
}), }),
AttestationRef::Electra(attestation) => Self::Electra(AggregateAndProofElectra { Attestation::Electra(aggregate) => Self::Electra(AggregateAndProofElectra {
aggregator_index, aggregator_index,
aggregate: attestation.clone(), aggregate,
selection_proof, selection_proof: selection_proof.into(),
}), }),
} }
} }

View File

@@ -83,17 +83,19 @@ impl<E: EthSpec> SignedAggregateAndProof<E> {
); );
let signing_message = message.signing_root(domain); let signing_message = message.signing_root(domain);
match message { Self::from_aggregate_and_proof(message, secret_key.sign(signing_message))
}
/// Produces a new `SignedAggregateAndProof` given a `signature` of `aggregate`
pub fn from_aggregate_and_proof(aggregate: AggregateAndProof<E>, signature: Signature) -> Self {
match aggregate {
AggregateAndProof::Base(message) => { AggregateAndProof::Base(message) => {
SignedAggregateAndProof::Base(SignedAggregateAndProofBase { SignedAggregateAndProof::Base(SignedAggregateAndProofBase { message, signature })
message,
signature: secret_key.sign(signing_message),
})
} }
AggregateAndProof::Electra(message) => { AggregateAndProof::Electra(message) => {
SignedAggregateAndProof::Electra(SignedAggregateAndProofElectra { SignedAggregateAndProof::Electra(SignedAggregateAndProofElectra {
message, message,
signature: secret_key.sign(signing_message), signature,
}) })
} }
} }

View File

@@ -18,16 +18,12 @@ use std::sync::Arc;
use task_executor::TaskExecutor; use task_executor::TaskExecutor;
use types::{ use types::{
attestation::Error as AttestationError, graffiti::GraffitiString, AbstractExecPayload, Address, attestation::Error as AttestationError, graffiti::GraffitiString, AbstractExecPayload, Address,
Attestation, BeaconBlock, BlindedPayload, ChainSpec, ContributionAndProof, Domain, Epoch, AggregateAndProof, Attestation, BeaconBlock, BlindedPayload, ChainSpec, ContributionAndProof,
EthSpec, Fork, ForkName, Graffiti, Hash256, PublicKeyBytes, SelectionProof, Signature, Domain, Epoch, EthSpec, Fork, ForkName, Graffiti, Hash256, PublicKeyBytes, SelectionProof,
SignedBeaconBlock, SignedContributionAndProof, SignedRoot, SignedValidatorRegistrationData, Signature, SignedAggregateAndProof, SignedBeaconBlock, SignedContributionAndProof, SignedRoot,
SignedVoluntaryExit, Slot, SyncAggregatorSelectionData, SyncCommitteeContribution, SignedValidatorRegistrationData, SignedVoluntaryExit, Slot, SyncAggregatorSelectionData,
SyncCommitteeMessage, SyncSelectionProof, SyncSubnetId, ValidatorRegistrationData, SyncCommitteeContribution, SyncCommitteeMessage, SyncSelectionProof, SyncSubnetId,
VoluntaryExit, ValidatorRegistrationData, VoluntaryExit,
};
use types::{
AggregateAndProof, AggregateAndProofBase, AggregateAndProofElectra, SignedAggregateAndProof,
SignedAggregateAndProofBase, SignedAggregateAndProofElectra,
}; };
pub use crate::doppelganger_service::DoppelgangerStatus; pub use crate::doppelganger_service::DoppelgangerStatus;
@@ -805,18 +801,8 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
let signing_epoch = aggregate.data().target.epoch; let signing_epoch = aggregate.data().target.epoch;
let signing_context = self.signing_context(Domain::AggregateAndProof, signing_epoch); let signing_context = self.signing_context(Domain::AggregateAndProof, signing_epoch);
let message = match aggregate { let message =
Attestation::Base(att) => AggregateAndProof::Base(AggregateAndProofBase { AggregateAndProof::from_attestation(aggregator_index, aggregate, selection_proof);
aggregator_index,
aggregate: att,
selection_proof: selection_proof.into(),
}),
Attestation::Electra(att) => AggregateAndProof::Electra(AggregateAndProofElectra {
aggregator_index,
aggregate: att,
selection_proof: selection_proof.into(),
}),
};
let signing_method = self.doppelganger_checked_signing_method(validator_pubkey)?; let signing_method = self.doppelganger_checked_signing_method(validator_pubkey)?;
let signature = signing_method let signature = signing_method
@@ -830,17 +816,9 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore<T, E> {
metrics::inc_counter_vec(&metrics::SIGNED_AGGREGATES_TOTAL, &[metrics::SUCCESS]); metrics::inc_counter_vec(&metrics::SIGNED_AGGREGATES_TOTAL, &[metrics::SUCCESS]);
match message { Ok(SignedAggregateAndProof::from_aggregate_and_proof(
AggregateAndProof::Base(message) => { message, signature,
Ok(SignedAggregateAndProof::Base(SignedAggregateAndProofBase { ))
message,
signature,
}))
}
AggregateAndProof::Electra(message) => Ok(SignedAggregateAndProof::Electra(
SignedAggregateAndProofElectra { message, signature },
)),
}
} }
/// Produces a `SelectionProof` for the `slot`, signed by with corresponding secret key to /// Produces a `SelectionProof` for the `slot`, signed by with corresponding secret key to