From 63d3dd27fc2cc050083861a7a82ed15c3bb08ca9 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 14 Dec 2022 12:01:33 +1100 Subject: [PATCH] Batch API for address changes (#3798) --- beacon_node/http_api/src/lib.rs | 72 ++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 23 deletions(-) diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index b75e583fc..47ea99c87 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -1673,36 +1673,62 @@ pub fn serve( .and(warp::path::end()) .and(warp::body::json()) .and(network_tx_filter.clone()) + .and(log_filter.clone()) .and_then( |chain: Arc>, - address_change: SignedBlsToExecutionChange, - network_tx: UnboundedSender>| { + address_changes: Vec, + network_tx: UnboundedSender>, + log: Logger| { blocking_json_task(move || { - let outcome = chain - .verify_bls_to_execution_change_for_gossip(address_change) - .map_err(|e| { - warp_utils::reject::object_invalid(format!( - "gossip verification failed: {:?}", - e - )) - })?; + let mut failures = vec![]; - if let ObservationOutcome::New(address_change) = outcome { - #[cfg(feature = "withdrawals-processing")] - { - publish_pubsub_message( - &network_tx, - PubsubMessage::BlsToExecutionChange(Box::new( - address_change.as_inner().clone(), - )), - )?; + for (index, address_change) in address_changes.into_iter().enumerate() { + let validator_index = address_change.message.validator_index; + + match chain.verify_bls_to_execution_change_for_gossip(address_change) { + Ok(ObservationOutcome::New(verified_address_change)) => { + #[cfg(feature = "withdrawals-processing")] + { + publish_pubsub_message( + &network_tx, + PubsubMessage::BlsToExecutionChange(Box::new( + verified_address_change.as_inner().clone(), + )), + )?; + } + + chain.import_bls_to_execution_change(verified_address_change); + } + Ok(ObservationOutcome::AlreadyKnown) => { + debug!( + log, + "BLS to execution change already known"; + "validator_index" => validator_index, + ); + } + Err(e) => { + error!( + log, + "Invalid BLS to execution change"; + "validator_index" => validator_index, + "source" => "HTTP API", + ); + failures.push(api_types::Failure::new( + index, + format!("invalid: {e:?}"), + )); + } } - drop(network_tx); - - chain.import_bls_to_execution_change(address_change); } - Ok(()) + if failures.is_empty() { + Ok(()) + } else { + Err(warp_utils::reject::indexed_bad_request( + "some BLS to execution changes failed to verify".into(), + failures, + )) + } }) }, );