mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-22 22:34:45 +00:00
Refactor/stream vc vote publishing (#8880)
Changes four `ValidatorStore` batch signing methods to return `impl Stream` instead of `Future`. Services consume the stream and publish each batch as it arrives. No behavioral change for lh since `LighthouseValidatorStore` wraps everything in `stream::once` Also replaces anonymous tuples in method signatures with named structs Co-Authored-By: shane-moore <skm1790@gmail.com> Co-Authored-By: Michael Sproul <michaelsproul@users.noreply.github.com> Co-Authored-By: Mac L <mjladson@pm.me>
This commit is contained in:
@@ -2,7 +2,7 @@ use account_utils::validator_definitions::{PasswordStorage, ValidatorDefinition}
|
||||
use bls::{PublicKeyBytes, Signature};
|
||||
use doppelganger_service::DoppelgangerService;
|
||||
use eth2::types::PublishBlockRequest;
|
||||
use futures::future::join_all;
|
||||
use futures::{Stream, future::join_all, stream};
|
||||
use initialized_validators::InitializedValidators;
|
||||
use logging::crit;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
@@ -17,7 +17,7 @@ use std::marker::PhantomData;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
use task_executor::TaskExecutor;
|
||||
use tracing::{error, info, instrument, warn};
|
||||
use tracing::{Instrument, debug, error, info, info_span, instrument, warn};
|
||||
use types::{
|
||||
AbstractExecPayload, Address, AggregateAndProof, Attestation, BeaconBlock, BlindedPayload,
|
||||
ChainSpec, ContributionAndProof, Domain, Epoch, EthSpec, ExecutionPayloadEnvelope, Fork,
|
||||
@@ -28,7 +28,8 @@ use types::{
|
||||
ValidatorRegistrationData, VoluntaryExit, graffiti::GraffitiString,
|
||||
};
|
||||
use validator_store::{
|
||||
DoppelgangerStatus, Error as ValidatorStoreError, ProposalData, SignedBlock, UnsignedBlock,
|
||||
AggregateToSign, AttestationToSign, ContributionToSign, DoppelgangerStatus,
|
||||
Error as ValidatorStoreError, ProposalData, SignedBlock, SyncMessageToSign, UnsignedBlock,
|
||||
ValidatorStore,
|
||||
};
|
||||
|
||||
@@ -691,6 +692,119 @@ impl<T: SlotClock + 'static, E: EthSpec> LighthouseValidatorStore<T, E> {
|
||||
|
||||
Ok(safe_attestations)
|
||||
}
|
||||
|
||||
/// Signs an `AggregateAndProof` for a given validator.
|
||||
///
|
||||
/// The resulting `SignedAggregateAndProof` is sent on the aggregation channel and cannot be
|
||||
/// modified by actors other than the signing validator.
|
||||
pub async fn produce_signed_aggregate_and_proof(
|
||||
&self,
|
||||
validator_pubkey: PublicKeyBytes,
|
||||
aggregator_index: u64,
|
||||
aggregate: Attestation<E>,
|
||||
selection_proof: SelectionProof,
|
||||
) -> Result<SignedAggregateAndProof<E>, Error> {
|
||||
let signing_epoch = aggregate.data().target.epoch;
|
||||
let signing_context = self.signing_context(Domain::AggregateAndProof, signing_epoch);
|
||||
|
||||
let message =
|
||||
AggregateAndProof::from_attestation(aggregator_index, aggregate, selection_proof);
|
||||
|
||||
let signing_method = self.doppelganger_checked_signing_method(validator_pubkey)?;
|
||||
let signature = signing_method
|
||||
.get_signature::<E, BlindedPayload<E>>(
|
||||
SignableMessage::SignedAggregateAndProof(message.to_ref()),
|
||||
signing_context,
|
||||
&self.spec,
|
||||
&self.task_executor,
|
||||
)
|
||||
.await?;
|
||||
|
||||
validator_metrics::inc_counter_vec(
|
||||
&validator_metrics::SIGNED_AGGREGATES_TOTAL,
|
||||
&[validator_metrics::SUCCESS],
|
||||
);
|
||||
|
||||
Ok(SignedAggregateAndProof::from_aggregate_and_proof(
|
||||
message, signature,
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn produce_sync_committee_signature(
|
||||
&self,
|
||||
slot: Slot,
|
||||
beacon_block_root: Hash256,
|
||||
validator_index: u64,
|
||||
validator_pubkey: &PublicKeyBytes,
|
||||
) -> Result<SyncCommitteeMessage, Error> {
|
||||
let signing_epoch = slot.epoch(E::slots_per_epoch());
|
||||
let signing_context = self.signing_context(Domain::SyncCommittee, signing_epoch);
|
||||
|
||||
// Bypass `with_validator_signing_method`: sync committee messages are not slashable.
|
||||
let signing_method = self.doppelganger_bypassed_signing_method(*validator_pubkey)?;
|
||||
|
||||
let signature = signing_method
|
||||
.get_signature::<E, BlindedPayload<E>>(
|
||||
SignableMessage::SyncCommitteeSignature {
|
||||
beacon_block_root,
|
||||
slot,
|
||||
},
|
||||
signing_context,
|
||||
&self.spec,
|
||||
&self.task_executor,
|
||||
)
|
||||
.await
|
||||
.map_err(Error::SpecificError)?;
|
||||
|
||||
validator_metrics::inc_counter_vec(
|
||||
&validator_metrics::SIGNED_SYNC_COMMITTEE_MESSAGES_TOTAL,
|
||||
&[validator_metrics::SUCCESS],
|
||||
);
|
||||
|
||||
Ok(SyncCommitteeMessage {
|
||||
slot,
|
||||
beacon_block_root,
|
||||
validator_index,
|
||||
signature,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn produce_signed_contribution_and_proof(
|
||||
&self,
|
||||
aggregator_index: u64,
|
||||
aggregator_pubkey: PublicKeyBytes,
|
||||
contribution: SyncCommitteeContribution<E>,
|
||||
selection_proof: SyncSelectionProof,
|
||||
) -> Result<SignedContributionAndProof<E>, Error> {
|
||||
let signing_epoch = contribution.slot.epoch(E::slots_per_epoch());
|
||||
let signing_context = self.signing_context(Domain::ContributionAndProof, signing_epoch);
|
||||
|
||||
// Bypass `with_validator_signing_method`: sync committee messages are not slashable.
|
||||
let signing_method = self.doppelganger_bypassed_signing_method(aggregator_pubkey)?;
|
||||
|
||||
let message = ContributionAndProof {
|
||||
aggregator_index,
|
||||
contribution,
|
||||
selection_proof: selection_proof.into(),
|
||||
};
|
||||
|
||||
let signature = signing_method
|
||||
.get_signature::<E, BlindedPayload<E>>(
|
||||
SignableMessage::SignedContributionAndProof(&message),
|
||||
signing_context,
|
||||
&self.spec,
|
||||
&self.task_executor,
|
||||
)
|
||||
.await
|
||||
.map_err(Error::SpecificError)?;
|
||||
|
||||
validator_metrics::inc_counter_vec(
|
||||
&validator_metrics::SIGNED_SYNC_COMMITTEE_CONTRIBUTIONS_TOTAL,
|
||||
&[validator_metrics::SUCCESS],
|
||||
);
|
||||
|
||||
Ok(SignedContributionAndProof { message, signature })
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore for LighthouseValidatorStore<T, E> {
|
||||
@@ -882,72 +996,83 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore for LighthouseValidatorS
|
||||
}
|
||||
}
|
||||
|
||||
async fn sign_attestations(
|
||||
fn sign_attestations(
|
||||
self: &Arc<Self>,
|
||||
mut attestations: Vec<(u64, PublicKeyBytes, usize, Attestation<Self::E>)>,
|
||||
) -> Result<Vec<(u64, Attestation<E>)>, Error> {
|
||||
// Sign all attestations concurrently.
|
||||
let signing_futures =
|
||||
attestations
|
||||
.iter_mut()
|
||||
.map(|(_, pubkey, validator_committee_index, attestation)| {
|
||||
mut attestations: Vec<AttestationToSign<E>>,
|
||||
) -> impl Stream<Item = Result<Vec<(u64, Attestation<E>)>, Error>> + Send {
|
||||
let store = self.clone();
|
||||
stream::once(async move {
|
||||
// Sign all attestations concurrently.
|
||||
let signing_futures = attestations.iter_mut().map(
|
||||
|AttestationToSign {
|
||||
pubkey,
|
||||
validator_committee_index,
|
||||
attestation,
|
||||
..
|
||||
}| {
|
||||
let pubkey = *pubkey;
|
||||
let validator_committee_index = *validator_committee_index;
|
||||
let store = store.clone();
|
||||
async move {
|
||||
self.sign_attestation_no_slashing_protection(
|
||||
pubkey,
|
||||
validator_committee_index,
|
||||
attestation,
|
||||
)
|
||||
.await
|
||||
store
|
||||
.sign_attestation_no_slashing_protection(
|
||||
pubkey,
|
||||
validator_committee_index,
|
||||
attestation,
|
||||
)
|
||||
.await
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
// Execute all signing in parallel.
|
||||
let results: Vec<_> = join_all(signing_futures).await;
|
||||
// Execute all signing in parallel.
|
||||
let results: Vec<_> = join_all(signing_futures).await;
|
||||
|
||||
// Collect successfully signed attestations and log errors.
|
||||
let mut signed_attestations = Vec::with_capacity(attestations.len());
|
||||
for (result, (validator_index, pubkey, _, attestation)) in
|
||||
results.into_iter().zip(attestations.into_iter())
|
||||
{
|
||||
match result {
|
||||
Ok(()) => {
|
||||
signed_attestations.push((validator_index, attestation, pubkey));
|
||||
}
|
||||
Err(ValidatorStoreError::UnknownPubkey(pubkey)) => {
|
||||
warn!(
|
||||
info = "a validator may have recently been removed from this VC",
|
||||
?pubkey,
|
||||
"Missing pubkey for attestation"
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
crit!(
|
||||
error = ?e,
|
||||
"Failed to sign attestation"
|
||||
);
|
||||
// Collect successfully signed attestations and log errors.
|
||||
let mut signed_attestations = Vec::with_capacity(attestations.len());
|
||||
for (result, att) in results.into_iter().zip(attestations.into_iter()) {
|
||||
match result {
|
||||
Ok(()) => {
|
||||
signed_attestations.push((
|
||||
att.validator_index,
|
||||
att.attestation,
|
||||
att.pubkey,
|
||||
));
|
||||
}
|
||||
Err(ValidatorStoreError::UnknownPubkey(pubkey)) => {
|
||||
warn!(
|
||||
info = "a validator may have recently been removed from this VC",
|
||||
?pubkey,
|
||||
"Missing pubkey for attestation"
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
crit!(
|
||||
error = ?e,
|
||||
"Failed to sign attestation"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if signed_attestations.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
if signed_attestations.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
// Check slashing protection and insert into database. Use a dedicated blocking thread
|
||||
// to avoid clogging the async executor with blocking database I/O.
|
||||
let validator_store = self.clone();
|
||||
let safe_attestations = self
|
||||
.task_executor
|
||||
.spawn_blocking_handle(
|
||||
move || validator_store.slashing_protect_attestations(signed_attestations),
|
||||
"slashing_protect_attestations",
|
||||
)
|
||||
.ok_or(Error::ExecutorError)?
|
||||
.await
|
||||
.map_err(|_| Error::ExecutorError)??;
|
||||
Ok(safe_attestations)
|
||||
// Check slashing protection and insert into database. Use a dedicated blocking
|
||||
// thread to avoid clogging the async executor with blocking database I/O.
|
||||
let validator_store = store.clone();
|
||||
let safe_attestations = store
|
||||
.task_executor
|
||||
.spawn_blocking_handle(
|
||||
move || validator_store.slashing_protect_attestations(signed_attestations),
|
||||
"slashing_protect_attestations",
|
||||
)
|
||||
.ok_or(Error::ExecutorError)?
|
||||
.await
|
||||
.map_err(|_| Error::ExecutorError)??;
|
||||
Ok(safe_attestations)
|
||||
})
|
||||
}
|
||||
|
||||
async fn sign_validator_registration_data(
|
||||
@@ -979,43 +1104,6 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore for LighthouseValidatorS
|
||||
})
|
||||
}
|
||||
|
||||
/// Signs an `AggregateAndProof` for a given validator.
|
||||
///
|
||||
/// The resulting `SignedAggregateAndProof` is sent on the aggregation channel and cannot be
|
||||
/// modified by actors other than the signing validator.
|
||||
async fn produce_signed_aggregate_and_proof(
|
||||
&self,
|
||||
validator_pubkey: PublicKeyBytes,
|
||||
aggregator_index: u64,
|
||||
aggregate: Attestation<E>,
|
||||
selection_proof: SelectionProof,
|
||||
) -> Result<SignedAggregateAndProof<E>, Error> {
|
||||
let signing_epoch = aggregate.data().target.epoch;
|
||||
let signing_context = self.signing_context(Domain::AggregateAndProof, signing_epoch);
|
||||
|
||||
let message =
|
||||
AggregateAndProof::from_attestation(aggregator_index, aggregate, selection_proof);
|
||||
|
||||
let signing_method = self.doppelganger_checked_signing_method(validator_pubkey)?;
|
||||
let signature = signing_method
|
||||
.get_signature::<E, BlindedPayload<E>>(
|
||||
SignableMessage::SignedAggregateAndProof(message.to_ref()),
|
||||
signing_context,
|
||||
&self.spec,
|
||||
&self.task_executor,
|
||||
)
|
||||
.await?;
|
||||
|
||||
validator_metrics::inc_counter_vec(
|
||||
&validator_metrics::SIGNED_AGGREGATES_TOTAL,
|
||||
&[validator_metrics::SUCCESS],
|
||||
);
|
||||
|
||||
Ok(SignedAggregateAndProof::from_aggregate_and_proof(
|
||||
message, signature,
|
||||
))
|
||||
}
|
||||
|
||||
/// Produces a `SelectionProof` for the `slot`, signed by with corresponding secret key to
|
||||
/// `validator_pubkey`.
|
||||
async fn produce_selection_proof(
|
||||
@@ -1090,80 +1178,172 @@ impl<T: SlotClock + 'static, E: EthSpec> ValidatorStore for LighthouseValidatorS
|
||||
Ok(signature.into())
|
||||
}
|
||||
|
||||
async fn produce_sync_committee_signature(
|
||||
&self,
|
||||
slot: Slot,
|
||||
beacon_block_root: Hash256,
|
||||
validator_index: u64,
|
||||
validator_pubkey: &PublicKeyBytes,
|
||||
) -> Result<SyncCommitteeMessage, Error> {
|
||||
let signing_epoch = slot.epoch(E::slots_per_epoch());
|
||||
let signing_context = self.signing_context(Domain::SyncCommittee, signing_epoch);
|
||||
|
||||
// Bypass `with_validator_signing_method`: sync committee messages are not slashable.
|
||||
let signing_method = self.doppelganger_bypassed_signing_method(*validator_pubkey)?;
|
||||
|
||||
let signature = signing_method
|
||||
.get_signature::<E, BlindedPayload<E>>(
|
||||
SignableMessage::SyncCommitteeSignature {
|
||||
beacon_block_root,
|
||||
slot,
|
||||
fn sign_aggregate_and_proofs(
|
||||
self: &Arc<Self>,
|
||||
aggregates: Vec<AggregateToSign<E>>,
|
||||
) -> impl Stream<Item = Result<Vec<SignedAggregateAndProof<E>>, Error>> + Send {
|
||||
let store = self.clone();
|
||||
let count = aggregates.len();
|
||||
stream::once(async move {
|
||||
let signing_futures = aggregates.into_iter().map(
|
||||
|AggregateToSign {
|
||||
pubkey,
|
||||
aggregator_index,
|
||||
aggregate,
|
||||
selection_proof,
|
||||
}| {
|
||||
let store = store.clone();
|
||||
async move {
|
||||
let result = store
|
||||
.produce_signed_aggregate_and_proof(
|
||||
pubkey,
|
||||
aggregator_index,
|
||||
aggregate,
|
||||
selection_proof,
|
||||
)
|
||||
.await;
|
||||
(pubkey, result)
|
||||
}
|
||||
},
|
||||
signing_context,
|
||||
&self.spec,
|
||||
&self.task_executor,
|
||||
)
|
||||
.await
|
||||
.map_err(Error::SpecificError)?;
|
||||
);
|
||||
|
||||
validator_metrics::inc_counter_vec(
|
||||
&validator_metrics::SIGNED_SYNC_COMMITTEE_MESSAGES_TOTAL,
|
||||
&[validator_metrics::SUCCESS],
|
||||
);
|
||||
let results = join_all(signing_futures)
|
||||
.instrument(info_span!("sign_aggregates", count))
|
||||
.await;
|
||||
|
||||
Ok(SyncCommitteeMessage {
|
||||
slot,
|
||||
beacon_block_root,
|
||||
validator_index,
|
||||
signature,
|
||||
let mut signed = Vec::with_capacity(results.len());
|
||||
for (pubkey, result) in results {
|
||||
match result {
|
||||
Ok(agg) => signed.push(agg),
|
||||
Err(ValidatorStoreError::UnknownPubkey(pubkey)) => {
|
||||
// A pubkey can be missing when a validator was recently
|
||||
// removed via the API.
|
||||
debug!(?pubkey, "Missing pubkey for aggregate");
|
||||
}
|
||||
Err(e) => {
|
||||
crit!(error = ?e, pubkey = ?pubkey, "Failed to sign aggregate");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(signed)
|
||||
})
|
||||
}
|
||||
|
||||
async fn produce_signed_contribution_and_proof(
|
||||
&self,
|
||||
aggregator_index: u64,
|
||||
aggregator_pubkey: PublicKeyBytes,
|
||||
contribution: SyncCommitteeContribution<E>,
|
||||
selection_proof: SyncSelectionProof,
|
||||
) -> Result<SignedContributionAndProof<E>, Error> {
|
||||
let signing_epoch = contribution.slot.epoch(E::slots_per_epoch());
|
||||
let signing_context = self.signing_context(Domain::ContributionAndProof, signing_epoch);
|
||||
fn sign_sync_committee_signatures(
|
||||
self: &Arc<Self>,
|
||||
messages: Vec<SyncMessageToSign>,
|
||||
) -> impl Stream<Item = Result<Vec<SyncCommitteeMessage>, Error>> + Send {
|
||||
let store = self.clone();
|
||||
let count = messages.len();
|
||||
stream::once(async move {
|
||||
let signing_futures = messages.into_iter().map(
|
||||
|SyncMessageToSign {
|
||||
slot,
|
||||
beacon_block_root,
|
||||
validator_index,
|
||||
pubkey,
|
||||
}| {
|
||||
let store = store.clone();
|
||||
async move {
|
||||
let result = store
|
||||
.produce_sync_committee_signature(
|
||||
slot,
|
||||
beacon_block_root,
|
||||
validator_index,
|
||||
&pubkey,
|
||||
)
|
||||
.await;
|
||||
(pubkey, validator_index, slot, result)
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// Bypass `with_validator_signing_method`: sync committee messages are not slashable.
|
||||
let signing_method = self.doppelganger_bypassed_signing_method(aggregator_pubkey)?;
|
||||
let results = join_all(signing_futures)
|
||||
.instrument(info_span!("sign_sync_signatures", count))
|
||||
.await;
|
||||
|
||||
let message = ContributionAndProof {
|
||||
aggregator_index,
|
||||
contribution,
|
||||
selection_proof: selection_proof.into(),
|
||||
};
|
||||
let mut signed = Vec::with_capacity(results.len());
|
||||
for (_pubkey, validator_index, slot, result) in results {
|
||||
match result {
|
||||
Ok(sig) => signed.push(sig),
|
||||
Err(ValidatorStoreError::UnknownPubkey(pubkey)) => {
|
||||
// A pubkey can be missing when a validator was recently
|
||||
// removed via the API.
|
||||
debug!(
|
||||
?pubkey,
|
||||
validator_index,
|
||||
%slot,
|
||||
"Missing pubkey for sync committee signature"
|
||||
);
|
||||
}
|
||||
Err(e) => {
|
||||
crit!(
|
||||
validator_index,
|
||||
%slot,
|
||||
error = ?e,
|
||||
"Failed to sign sync committee signature"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(signed)
|
||||
})
|
||||
}
|
||||
|
||||
let signature = signing_method
|
||||
.get_signature::<E, BlindedPayload<E>>(
|
||||
SignableMessage::SignedContributionAndProof(&message),
|
||||
signing_context,
|
||||
&self.spec,
|
||||
&self.task_executor,
|
||||
)
|
||||
.await
|
||||
.map_err(Error::SpecificError)?;
|
||||
fn sign_sync_committee_contributions(
|
||||
self: &Arc<Self>,
|
||||
contributions: Vec<ContributionToSign<E>>,
|
||||
) -> impl Stream<Item = Result<Vec<SignedContributionAndProof<E>>, Error>> + Send {
|
||||
let store = self.clone();
|
||||
let count = contributions.len();
|
||||
stream::once(async move {
|
||||
let signing_futures = contributions.into_iter().map(
|
||||
|ContributionToSign {
|
||||
aggregator_index,
|
||||
aggregator_pubkey,
|
||||
contribution,
|
||||
selection_proof,
|
||||
}| {
|
||||
let store = store.clone();
|
||||
let slot = contribution.slot;
|
||||
async move {
|
||||
let result = store
|
||||
.produce_signed_contribution_and_proof(
|
||||
aggregator_index,
|
||||
aggregator_pubkey,
|
||||
contribution,
|
||||
selection_proof,
|
||||
)
|
||||
.await;
|
||||
(slot, result)
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
validator_metrics::inc_counter_vec(
|
||||
&validator_metrics::SIGNED_SYNC_COMMITTEE_CONTRIBUTIONS_TOTAL,
|
||||
&[validator_metrics::SUCCESS],
|
||||
);
|
||||
let results = join_all(signing_futures)
|
||||
.instrument(info_span!("sign_sync_contributions", count))
|
||||
.await;
|
||||
|
||||
Ok(SignedContributionAndProof { message, signature })
|
||||
let mut signed = Vec::with_capacity(results.len());
|
||||
for (slot, result) in results {
|
||||
match result {
|
||||
Ok(contribution) => signed.push(contribution),
|
||||
Err(ValidatorStoreError::UnknownPubkey(pubkey)) => {
|
||||
// A pubkey can be missing when a validator was recently
|
||||
// removed via the API.
|
||||
debug!(?pubkey, %slot, "Missing pubkey for sync contribution");
|
||||
}
|
||||
Err(e) => {
|
||||
crit!(
|
||||
%slot,
|
||||
error = ?e,
|
||||
"Unable to sign sync committee contribution"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(signed)
|
||||
})
|
||||
}
|
||||
|
||||
/// Prune the slashing protection database so that it remains performant.
|
||||
|
||||
Reference in New Issue
Block a user