Move register_validator off beacon processor

This commit is contained in:
Paul Hauner
2023-07-11 17:11:07 +10:00
parent 311e1bd197
commit 219a61c547

View File

@@ -59,7 +59,10 @@ use std::sync::Arc;
use sysinfo::{System, SystemExt}; use sysinfo::{System, SystemExt};
use system_health::observe_system_health_bn; use system_health::observe_system_health_bn;
use task_spawner::{Priority, TaskSpawner}; use task_spawner::{Priority, TaskSpawner};
use tokio::sync::mpsc::{Sender, UnboundedSender}; use tokio::sync::{
mpsc::{Sender, UnboundedSender},
oneshot,
};
use tokio_stream::{wrappers::BroadcastStream, StreamExt}; use tokio_stream::{wrappers::BroadcastStream, StreamExt};
use types::{ use types::{
Attestation, AttestationData, AttestationShufflingId, AttesterSlashing, BeaconStateError, Attestation, AttestationData, AttestationShufflingId, AttesterSlashing, BeaconStateError,
@@ -3336,8 +3339,11 @@ pub fn serve<T: BeaconChainTypes>(
|task_spawner: TaskSpawner<T::EthSpec>, |task_spawner: TaskSpawner<T::EthSpec>,
chain: Arc<BeaconChain<T>>, chain: Arc<BeaconChain<T>>,
log: Logger, log: Logger,
register_val_data: Vec<SignedValidatorRegistrationData>| { register_val_data: Vec<SignedValidatorRegistrationData>| async {
task_spawner.spawn_async_with_rejection(Priority::P0, async move { let (tx, rx) = oneshot::channel();
task_spawner
.spawn_async_with_rejection(Priority::P0, async move {
let execution_layer = chain let execution_layer = chain
.execution_layer .execution_layer
.as_ref() .as_ref()
@@ -3382,14 +3388,19 @@ pub fn serve<T: BeaconChainTypes>(
.superstatus(); .superstatus();
let is_active_or_pending = let is_active_or_pending =
matches!(validator_status, ValidatorStatus::Pending) matches!(validator_status, ValidatorStatus::Pending)
|| matches!(validator_status, ValidatorStatus::Active); || matches!(
validator_status,
ValidatorStatus::Active
);
// Filter out validators who are not 'active' or 'pending'. // Filter out validators who are not 'active' or 'pending'.
is_active_or_pending.then_some({ is_active_or_pending.then_some({
( (
ProposerPreparationData { ProposerPreparationData {
validator_index: validator_index as u64, validator_index: validator_index as u64,
fee_recipient: register_data.message.fee_recipient, fee_recipient: register_data
.message
.fee_recipient,
}, },
register_data, register_data,
) )
@@ -3416,18 +3427,28 @@ pub fn serve<T: BeaconChainTypes>(
)) ))
})?; })?;
let builder = execution_layer
.builder()
.as_ref()
.ok_or(BeaconChainError::BuilderMissing)
.map_err(warp_utils::reject::beacon_chain_error)?;
info!( info!(
log, log,
"Forwarding register validator request to connected builder"; "Forwarding register validator request to connected builder";
"count" => filtered_registration_data.len(), "count" => filtered_registration_data.len(),
); );
// It's a waste of a `BeaconProcessor` worker to just
// wait on a response from the builder (especially since
// they have frequent timeouts). Spawn a new task and
// send the response back to our original HTTP request
// task via a channel.
let builder_future = async move {
let builder = chain
.execution_layer
.as_ref()
.ok_or(BeaconChainError::ExecutionLayerMissing)
.map_err(warp_utils::reject::beacon_chain_error)?
.builder()
.as_ref()
.ok_or(BeaconChainError::BuilderMissing)
.map_err(warp_utils::reject::beacon_chain_error)?;
builder builder
.post_builder_validators(&filtered_registration_data) .post_builder_validators(&filtered_registration_data)
.await .await
@@ -3443,7 +3464,9 @@ pub fn serve<T: BeaconChainTypes>(
// to a server error. // to a server error.
if let eth2::Error::ServerMessage(message) = e { if let eth2::Error::ServerMessage(message) = e {
if message.code == StatusCode::BAD_REQUEST.as_u16() { if message.code == StatusCode::BAD_REQUEST.as_u16() {
return warp_utils::reject::custom_bad_request(message.message); return warp_utils::reject::custom_bad_request(
message.message,
);
} else { } else {
// According to the spec this response should only be a 400 or 500, // According to the spec this response should only be a 400 or 500,
// so we fall back to a 500 here. // so we fall back to a 500 here.
@@ -3454,6 +3477,24 @@ pub fn serve<T: BeaconChainTypes>(
} }
warp_utils::reject::custom_server_error(format!("{e:?}")) warp_utils::reject::custom_server_error(format!("{e:?}"))
}) })
};
tokio::task::spawn(async move { tx.send(builder_future.await) });
// Just send a generic 200 OK from this closure. We'll
// ignore the `Ok` variant and form a proper response
// from what is sent back down the channel.
Ok(warp::reply::reply().into_response())
})
.await?;
// Await a response from the builder without blocking a
// `BeaconProcessor` worker.
rx.await.unwrap_or_else(|_| {
Ok(warp::reply::with_status(
warp::reply::json(&"No response from channel"),
eth2::StatusCode::INTERNAL_SERVER_ERROR,
)
.into_response())
}) })
}, },
); );