From 1c788afde0130971a7370531c5efe7a329d4993b Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 9 Sep 2020 16:35:34 +1000 Subject: [PATCH] Add aggregates POST method --- .../src/attestation_verification.rs | 5 ++ beacon_node/http_api/src/lib.rs | 57 ++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/beacon_node/beacon_chain/src/attestation_verification.rs b/beacon_node/beacon_chain/src/attestation_verification.rs index 47b061927ae..e019c92e841 100644 --- a/beacon_node/beacon_chain/src/attestation_verification.rs +++ b/beacon_node/beacon_chain/src/attestation_verification.rs @@ -411,6 +411,11 @@ impl VerifiedAggregatedAttestation { pub fn attestation(&self) -> &Attestation { &self.signed_aggregate.message.aggregate } + + /// Returns the underlying `signed_aggregate`. + pub fn aggregate(&self) -> &SignedAggregateAndProof { + &self.signed_aggregate + } } impl VerifiedUnaggregatedAttestation { diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index 8246eb5a97f..6e98036fad9 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -23,7 +23,7 @@ use std::sync::Arc; use tokio::sync::mpsc::UnboundedSender; use types::{ Attestation, AttesterSlashing, CommitteeCache, Epoch, EthSpec, ProposerSlashing, RelativeEpoch, - SignedBeaconBlock, SignedVoluntaryExit, Slot, YamlConfig, + SignedAggregateAndProof, SignedBeaconBlock, SignedVoluntaryExit, Slot, YamlConfig, }; use warp::Filter; @@ -760,7 +760,7 @@ pub fn serve( .and(warp::path("voluntary_exits")) .and(warp::path::end()) .and(warp::body::json()) - .and(network_tx_filter) + .and(network_tx_filter.clone()) .and_then( |chain: Arc>, exit: SignedVoluntaryExit, @@ -1037,7 +1037,7 @@ pub fn serve( .and(warp::path("aggregate_attestation")) .and(warp::path::end()) .and(warp::query::()) - .and(chain_filter) + .and(chain_filter.clone()) .and_then( |query: api_types::ValidatorAggregateAttestationQuery, chain: Arc>| { blocking_json_task(move || { @@ -1051,6 +1051,56 @@ pub fn serve( }, ); + // POST validator/aggregate_and_proofs + let post_validator_aggregate_and_proofs = eth1_v1 + .and(warp::path("validator")) + .and(warp::path("aggregate_and_proofs")) + .and(warp::path::end()) + .and(chain_filter) + .and(warp::body::json()) + .and(network_tx_filter.clone()) + .and_then( + |chain: Arc>, + aggregate: SignedAggregateAndProof, + network_tx: UnboundedSender>| { + blocking_json_task(move || { + let aggregate = chain + .verify_aggregated_attestation_for_gossip(aggregate.clone()) + .map_err(|e| { + crate::reject::object_invalid(format!( + "gossip verification failed: {:?}", + e + )) + })?; + + publish_network_message( + &network_tx, + PubsubMessage::AggregateAndProofAttestation(Box::new( + aggregate.aggregate().clone(), + )), + )?; + + chain + .apply_attestation_to_fork_choice(&aggregate) + .map_err(|e| { + crate::reject::broadcast_without_import(format!( + "not applied to fork choice: {:?}", + e + )) + })?; + + chain.add_to_block_inclusion_pool(aggregate).map_err(|e| { + crate::reject::broadcast_without_import(format!( + "not applied to block inclusion pool: {:?}", + e + )) + })?; + + Ok(()) + }) + }, + ); + let routes = warp::get() .and( get_beacon_genesis @@ -1087,6 +1137,7 @@ pub fn serve( .or(post_beacon_pool_attester_slashings.boxed()) .or(post_beacon_pool_proposer_slashings.boxed()) .or(post_beacon_pool_voluntary_exits.boxed()) + .or(post_validator_aggregate_and_proofs.boxed()) .boxed(), )) .recover(crate::reject::handle_rejection);