From 7c01e39d857b5010752d48e984f47de4737a5e86 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Wed, 16 Sep 2020 20:38:46 +1000 Subject: [PATCH] Remove reject into new warp_utils --- Cargo.lock | 11 +++ Cargo.toml | 1 + beacon_node/http_api/Cargo.toml | 1 + .../http_api/src/beacon_proposer_cache.rs | 6 +- beacon_node/http_api/src/block_id.rs | 19 ++-- beacon_node/http_api/src/consensus.rs | 16 ++-- beacon_node/http_api/src/lib.rs | 89 ++++++++++--------- beacon_node/http_api/src/state_id.rs | 19 ++-- common/warp_utils/Cargo.toml | 13 +++ common/warp_utils/src/lib.rs | 1 + .../warp_utils}/src/reject.rs | 0 11 files changed, 108 insertions(+), 68 deletions(-) create mode 100644 common/warp_utils/Cargo.toml create mode 100644 common/warp_utils/src/lib.rs rename {beacon_node/http_api => common/warp_utils}/src/reject.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 85a5c6dd85f..cc449ccfb67 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2327,6 +2327,7 @@ dependencies = [ "tree_hash", "types", "warp", + "warp_utils", ] [[package]] @@ -6383,6 +6384,16 @@ dependencies = [ "urlencoding", ] +[[package]] +name = "warp_utils" +version = "0.1.0" +dependencies = [ + "beacon_chain", + "eth2", + "types", + "warp", +] + [[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 107ea18c88d..241a32dfcca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ members = [ "common/rest_types", "common/slot_clock", "common/test_random_derive", + "common/warp_utils", "common/validator_dir", "consensus/cached_tree_hash", diff --git a/beacon_node/http_api/Cargo.toml b/beacon_node/http_api/Cargo.toml index ecc65b2d45a..c5e025b2aee 100644 --- a/beacon_node/http_api/Cargo.toml +++ b/beacon_node/http_api/Cargo.toml @@ -24,6 +24,7 @@ state_processing = { path = "../../consensus/state_processing" } lighthouse_version = { path = "../../common/lighthouse_version" } lighthouse_metrics = { path = "../../common/lighthouse_metrics" } lazy_static = "1.4.0" +warp_utils = { path = "../../common/warp_utils" } [dev-dependencies] store = { path = "../store" } diff --git a/beacon_node/http_api/src/beacon_proposer_cache.rs b/beacon_node/http_api/src/beacon_proposer_cache.rs index 39d582aea91..aea9aee8345 100644 --- a/beacon_node/http_api/src/beacon_proposer_cache.rs +++ b/beacon_node/http_api/src/beacon_proposer_cache.rs @@ -95,21 +95,21 @@ impl BeaconProposerCache { .unwrap_or_else(|_| chain.spec.genesis_slot.epoch(T::EthSpec::slots_per_epoch())); if current_epoch != epoch { - return Err(crate::reject::custom_bad_request(format!( + return Err(warp_utils::reject::custom_bad_request(format!( "requested epoch is {} but only current epoch {} is allowed", epoch, current_epoch ))); } let (head_root, head_block) = - Self::current_head_block(chain).map_err(crate::reject::beacon_chain_error)?; + Self::current_head_block(chain).map_err(warp_utils::reject::beacon_chain_error)?; let epoch_boundary_root = head_block.target_root; if self.epoch != current_epoch || self.epoch_boundary_root != epoch_boundary_root { metrics::inc_counter(&metrics::HTTP_API_BEACON_PROPOSER_CACHE_MISSES_TOTAL); *self = Self::for_head_block(chain, current_epoch, head_root, head_block) - .map_err(crate::reject::beacon_chain_error)?; + .map_err(warp_utils::reject::beacon_chain_error)?; } else { metrics::inc_counter(&metrics::HTTP_API_BEACON_PROPOSER_CACHE_HITS_TOTAL); } diff --git a/beacon_node/http_api/src/block_id.rs b/beacon_node/http_api/src/block_id.rs index 4ceb20efa59..b034a6816a3 100644 --- a/beacon_node/http_api/src/block_id.rs +++ b/beacon_node/http_api/src/block_id.rs @@ -23,22 +23,25 @@ impl BlockId { CoreBlockId::Head => chain .head_info() .map(|head| head.block_root) - .map_err(crate::reject::beacon_chain_error), + .map_err(warp_utils::reject::beacon_chain_error), CoreBlockId::Genesis => Ok(chain.genesis_block_root), CoreBlockId::Finalized => chain .head_info() .map(|head| head.finalized_checkpoint.root) - .map_err(crate::reject::beacon_chain_error), + .map_err(warp_utils::reject::beacon_chain_error), CoreBlockId::Justified => chain .head_info() .map(|head| head.current_justified_checkpoint.root) - .map_err(crate::reject::beacon_chain_error), + .map_err(warp_utils::reject::beacon_chain_error), CoreBlockId::Slot(slot) => chain .block_root_at_slot(*slot) - .map_err(crate::reject::beacon_chain_error) + .map_err(warp_utils::reject::beacon_chain_error) .and_then(|root_opt| { root_opt.ok_or_else(|| { - crate::reject::custom_not_found(format!("beacon block at slot {}", slot)) + warp_utils::reject::custom_not_found(format!( + "beacon block at slot {}", + slot + )) }) }), CoreBlockId::Root(root) => Ok(*root), @@ -52,15 +55,15 @@ impl BlockId { match &self.0 { CoreBlockId::Head => chain .head_beacon_block() - .map_err(crate::reject::beacon_chain_error), + .map_err(warp_utils::reject::beacon_chain_error), _ => { let root = self.root(chain)?; chain .get_block(&root) - .map_err(crate::reject::beacon_chain_error) + .map_err(warp_utils::reject::beacon_chain_error) .and_then(|root_opt| { root_opt.ok_or_else(|| { - crate::reject::custom_not_found(format!( + warp_utils::reject::custom_not_found(format!( "beacon block with root {}", root )) diff --git a/beacon_node/http_api/src/consensus.rs b/beacon_node/http_api/src/consensus.rs index 09d9b86896d..49f6cf3cec8 100644 --- a/beacon_node/http_api/src/consensus.rs +++ b/beacon_node/http_api/src/consensus.rs @@ -16,11 +16,11 @@ pub fn global_validator_inclusion_data( let state = StateId::slot(target_slot).state(chain)?; - let mut validator_statuses = - ValidatorStatuses::new(&state, &chain.spec).map_err(crate::reject::beacon_state_error)?; + let mut validator_statuses = ValidatorStatuses::new(&state, &chain.spec) + .map_err(warp_utils::reject::beacon_state_error)?; validator_statuses .process_attestations(&state, &chain.spec) - .map_err(crate::reject::beacon_state_error)?; + .map_err(warp_utils::reject::beacon_state_error)?; let totals = validator_statuses.total_balances; @@ -45,22 +45,22 @@ pub fn validator_inclusion_data( let mut state = StateId::slot(target_slot).state(chain)?; - let mut validator_statuses = - ValidatorStatuses::new(&state, &chain.spec).map_err(crate::reject::beacon_state_error)?; + let mut validator_statuses = ValidatorStatuses::new(&state, &chain.spec) + .map_err(warp_utils::reject::beacon_state_error)?; validator_statuses .process_attestations(&state, &chain.spec) - .map_err(crate::reject::beacon_state_error)?; + .map_err(warp_utils::reject::beacon_state_error)?; state .update_pubkey_cache() - .map_err(crate::reject::beacon_state_error)?; + .map_err(warp_utils::reject::beacon_state_error)?; let validator_index = match validator_id { ValidatorId::Index(index) => *index as usize, ValidatorId::PublicKey(pubkey) => { if let Some(index) = state .get_validator_index(pubkey) - .map_err(crate::reject::beacon_state_error)? + .map_err(warp_utils::reject::beacon_state_error)? { index } else { diff --git a/beacon_node/http_api/src/lib.rs b/beacon_node/http_api/src/lib.rs index 1cc37feae9b..24646e0a407 100644 --- a/beacon_node/http_api/src/lib.rs +++ b/beacon_node/http_api/src/lib.rs @@ -2,7 +2,6 @@ mod beacon_proposer_cache; mod block_id; mod consensus; mod metrics; -mod reject; mod state_id; use beacon_chain::{ @@ -195,7 +194,7 @@ pub fn serve( .and_then(|beacon_proposer_cache| async move { match beacon_proposer_cache { Some(cache) => Ok(cache), - None => Err(crate::reject::custom_not_found( + None => Err(warp_utils::reject::custom_not_found( "Beacon proposer cache is not initialized.".to_string(), )), } @@ -208,7 +207,7 @@ pub fn serve( .and_then(|network_globals| async move { match network_globals { Some(globals) => Ok(globals), - None => Err(crate::reject::custom_not_found( + None => Err(warp_utils::reject::custom_not_found( "network globals are not initialized.".to_string(), )), } @@ -221,7 +220,7 @@ pub fn serve( .and_then(|chain| async move { match chain { Some(chain) => Ok(chain), - None => Err(crate::reject::custom_not_found( + None => Err(warp_utils::reject::custom_not_found( "Beacon chain genesis has not yet been observed.".to_string(), )), } @@ -233,7 +232,7 @@ pub fn serve( .and_then(|network_tx| async move { match network_tx { Some(network_tx) => Ok(network_tx), - None => Err(crate::reject::custom_not_found( + None => Err(warp_utils::reject::custom_not_found( "The networking stack has not yet started.".to_string(), )), } @@ -251,7 +250,7 @@ pub fn serve( blocking_json_task(move || { chain .head_info() - .map_err(crate::reject::beacon_chain_error) + .map_err(warp_utils::reject::beacon_chain_error) .map(|head| api_types::GenesisData { genesis_time: head.genesis_time, genesis_validators_root: head.genesis_validators_root, @@ -410,7 +409,7 @@ pub fn serve( let relative_epoch = RelativeEpoch::from_epoch(state.current_epoch(), epoch).map_err( |_| { - crate::reject::custom_bad_request(format!( + warp_utils::reject::custom_bad_request(format!( "state is epoch {} and only previous, current and next epochs are supported", state.current_epoch() )) @@ -425,7 +424,7 @@ pub fn serve( CommitteeCache::initialized(state, epoch, &chain.spec).map(Cow::Owned) } .map_err(BeaconChainError::BeaconStateError) - .map_err(crate::reject::beacon_chain_error)?; + .map_err(warp_utils::reject::beacon_chain_error)?; // Use either the supplied slot or all slots in the epoch. let slots = query.slot.map(|slot| vec![slot]).unwrap_or_else(|| { @@ -443,7 +442,7 @@ pub fn serve( // It is not acceptable to query with a slot that is not within the // specified epoch. if slot.epoch(T::EthSpec::slots_per_epoch()) != epoch { - return Err(crate::reject::custom_bad_request(format!( + return Err(warp_utils::reject::custom_bad_request(format!( "{} is not in epoch {}", slot, epoch ))); @@ -453,7 +452,7 @@ pub fn serve( let committee = committee_cache .get_beacon_committee(slot, index) .ok_or_else(|| { - crate::reject::custom_bad_request(format!( + warp_utils::reject::custom_bad_request(format!( "committee index {} does not exist in epoch {}", index, epoch )) @@ -497,22 +496,22 @@ pub fn serve( // No query parameters, return the canonical head block. (None, None) => chain .head_beacon_block() - .map_err(crate::reject::beacon_chain_error) + .map_err(warp_utils::reject::beacon_chain_error) .map(|block| (block.canonical_root(), block))?, // Only the parent root parameter, do a forwards-iterator lookup. (None, Some(parent_root)) => { let parent = BlockId::from_root(parent_root).block(&chain)?; let (root, _slot) = chain .forwards_iter_block_roots(parent.slot()) - .map_err(crate::reject::beacon_chain_error)? + .map_err(warp_utils::reject::beacon_chain_error)? // Ignore any skip-slots immediately following the parent. .find(|res| { res.as_ref().map_or(false, |(root, _)| *root != parent_root) }) .transpose() - .map_err(crate::reject::beacon_chain_error)? + .map_err(warp_utils::reject::beacon_chain_error)? .ok_or_else(|| { - crate::reject::custom_not_found(format!( + warp_utils::reject::custom_not_found(format!( "child of block with root {}", parent_root )) @@ -532,7 +531,7 @@ pub fn serve( // obtained via a slot lookup. if let Some(parent_root) = parent_root_opt { if block.parent_root() != parent_root { - return Err(crate::reject::custom_not_found(format!( + return Err(warp_utils::reject::custom_not_found(format!( "no canonical block at slot {} with parent root {}", slot, parent_root ))); @@ -571,7 +570,7 @@ pub fn serve( let canonical = chain .block_root_at_slot(block.slot()) - .map_err(crate::reject::beacon_chain_error)? + .map_err(warp_utils::reject::beacon_chain_error)? .map_or(false, |canonical| root == canonical); let data = api_types::BlockHeaderData { @@ -625,7 +624,7 @@ pub fn serve( // head. chain .fork_choice() - .map_err(crate::reject::beacon_chain_error)?; + .map_err(warp_utils::reject::beacon_chain_error)?; Ok(()) } @@ -636,7 +635,7 @@ pub fn serve( "Invalid block provided to HTTP API"; "reason" => &msg ); - Err(crate::reject::broadcast_without_import(msg)) + Err(warp_utils::reject::broadcast_without_import(msg)) } } }) @@ -708,7 +707,7 @@ pub fn serve( let attestation = chain .verify_unaggregated_attestation_for_gossip(attestation.clone(), None) .map_err(|e| { - crate::reject::object_invalid(format!( + warp_utils::reject::object_invalid(format!( "gossip verification failed: {:?}", e )) @@ -725,7 +724,7 @@ pub fn serve( chain .apply_attestation_to_fork_choice(&attestation) .map_err(|e| { - crate::reject::broadcast_without_import(format!( + warp_utils::reject::broadcast_without_import(format!( "not applied to fork choice: {:?}", e )) @@ -734,7 +733,7 @@ pub fn serve( chain .add_to_naive_aggregation_pool(attestation) .map_err(|e| { - crate::reject::broadcast_without_import(format!( + warp_utils::reject::broadcast_without_import(format!( "not applied to naive aggregation pool: {:?}", e )) @@ -773,7 +772,7 @@ pub fn serve( let outcome = chain .verify_attester_slashing_for_gossip(slashing.clone()) .map_err(|e| { - crate::reject::object_invalid(format!( + warp_utils::reject::object_invalid(format!( "gossip verification failed: {:?}", e )) @@ -789,7 +788,7 @@ pub fn serve( chain .import_attester_slashing(slashing) - .map_err(crate::reject::beacon_chain_error)?; + .map_err(warp_utils::reject::beacon_chain_error)?; } Ok(()) @@ -824,7 +823,7 @@ pub fn serve( let outcome = chain .verify_proposer_slashing_for_gossip(slashing.clone()) .map_err(|e| { - crate::reject::object_invalid(format!( + warp_utils::reject::object_invalid(format!( "gossip verification failed: {:?}", e )) @@ -873,7 +872,7 @@ pub fn serve( let outcome = chain .verify_voluntary_exit_for_gossip(exit.clone()) .map_err(|e| { - crate::reject::object_invalid(format!( + warp_utils::reject::object_invalid(format!( "gossip verification failed: {:?}", e )) @@ -1044,8 +1043,10 @@ pub fn serve( let head_slot = chain .head_info() .map(|info| info.slot) - .map_err(crate::reject::beacon_chain_error)?; - let current_slot = chain.slot().map_err(crate::reject::beacon_chain_error)?; + .map_err(warp_utils::reject::beacon_chain_error)?; + let current_slot = chain + .slot() + .map_err(warp_utils::reject::beacon_chain_error)?; // Taking advantage of saturating subtraction on slot. let sync_distance = current_slot - head_slot; @@ -1077,11 +1078,13 @@ pub fn serve( .and_then( |epoch: Epoch, query: api_types::ValidatorDutiesQuery, chain: Arc>| { blocking_json_task(move || { - let current_epoch = chain.epoch().map_err(crate::reject::beacon_chain_error)?; + let current_epoch = chain + .epoch() + .map_err(warp_utils::reject::beacon_chain_error)?; // Taking advantage of saturating addition on epoch. if epoch + 1 < current_epoch { - return Err(crate::reject::custom_bad_request(format!( + return Err(warp_utils::reject::custom_bad_request(format!( "request epoch {} is more than one epoch prior to current epoch {}", epoch, current_epoch ))); @@ -1111,8 +1114,9 @@ pub fn serve( )) }) .map(|(validator_index, pubkey_res, duty_res)| { - let pubkey = pubkey_res.map_err(crate::reject::beacon_chain_error)?; - let duty = duty_res.map_err(crate::reject::beacon_chain_error)?; + let pubkey = + pubkey_res.map_err(warp_utils::reject::beacon_chain_error)?; + let duty = duty_res.map_err(warp_utils::reject::beacon_chain_error)?; Ok(api_types::AttesterData { pubkey: pubkey.into(), @@ -1165,7 +1169,7 @@ pub fn serve( |slot: Slot, query: api_types::ValidatorBlocksQuery, chain: Arc>| { blocking_json_task(move || { let randao_reveal = (&query.randao_reveal).try_into().map_err(|e| { - crate::reject::custom_bad_request(format!( + warp_utils::reject::custom_bad_request(format!( "randao reveal is not valid BLS signature: {:?}", e )) @@ -1175,7 +1179,7 @@ pub fn serve( .produce_block(randao_reveal, slot, query.graffiti.map(Into::into)) .map(|block_and_state| block_and_state.0) .map(api_types::GenericResponse::from) - .map_err(crate::reject::block_production_error) + .map_err(warp_utils::reject::block_production_error) }) }, ); @@ -1194,7 +1198,7 @@ pub fn serve( .produce_unaggregated_attestation(query.slot, query.committee_index) .map(|attestation| attestation.data) .map(api_types::GenericResponse::from) - .map_err(crate::reject::beacon_chain_error) + .map_err(warp_utils::reject::beacon_chain_error) }) }, ); @@ -1216,7 +1220,7 @@ pub fn serve( ) .map(api_types::GenericResponse::from) .ok_or_else(|| { - crate::reject::custom_not_found( + warp_utils::reject::custom_not_found( "no matching aggregate found".to_string(), ) }) @@ -1248,7 +1252,7 @@ pub fn serve( // node. Err(AttnError::AttestationAlreadyKnown(_)) => return Ok(()), Err(e) => { - return Err(crate::reject::object_invalid(format!( + return Err(warp_utils::reject::object_invalid(format!( "gossip verification failed: {:?}", e ))) @@ -1265,14 +1269,14 @@ pub fn serve( chain .apply_attestation_to_fork_choice(&aggregate) .map_err(|e| { - crate::reject::broadcast_without_import(format!( + warp_utils::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!( + warp_utils::reject::broadcast_without_import(format!( "not applied to block inclusion pool: {:?}", e )) @@ -1325,7 +1329,7 @@ pub fn serve( blocking_json_task(move || { lighthouse_metrics::health::Health::observe() .map(api_types::GenericResponse::from) - .map_err(crate::reject::custom_bad_request) + .map_err(warp_utils::reject::custom_bad_request) }) }); @@ -1483,7 +1487,7 @@ pub fn serve( ) .boxed()) .boxed() - .recover(crate::reject::handle_rejection) + .recover(warp_utils::reject::handle_rejection) .with(slog_logging(log.clone())) .with(prometheus_metrics()); @@ -1520,7 +1524,10 @@ fn publish_network_message( message: NetworkMessage, ) -> Result<(), warp::Rejection> { network_tx.send(message).map_err(|e| { - crate::reject::custom_server_error(format!("unable to publish to network channel: {}", e)) + warp_utils::reject::custom_server_error(format!( + "unable to publish to network channel: {}", + e + )) }) } diff --git a/beacon_node/http_api/src/state_id.rs b/beacon_node/http_api/src/state_id.rs index 3f22bf84924..a453eaff59f 100644 --- a/beacon_node/http_api/src/state_id.rs +++ b/beacon_node/http_api/src/state_id.rs @@ -23,7 +23,7 @@ impl StateId { return chain .head_info() .map(|head| head.state_root) - .map_err(crate::reject::beacon_chain_error) + .map_err(warp_utils::reject::beacon_chain_error) } CoreStateId::Genesis => return Ok(chain.genesis_state_root), CoreStateId::Finalized => chain.head_info().map(|head| { @@ -39,13 +39,13 @@ impl StateId { CoreStateId::Slot(slot) => Ok(*slot), CoreStateId::Root(root) => return Ok(*root), } - .map_err(crate::reject::beacon_chain_error)?; + .map_err(warp_utils::reject::beacon_chain_error)?; chain .state_root_at_slot(slot) - .map_err(crate::reject::beacon_chain_error)? + .map_err(warp_utils::reject::beacon_chain_error)? .ok_or_else(|| { - crate::reject::custom_not_found(format!("beacon state at slot {}", slot)) + warp_utils::reject::custom_not_found(format!("beacon state at slot {}", slot)) }) } @@ -64,7 +64,7 @@ impl StateId { CoreStateId::Head => { return chain .head_beacon_state() - .map_err(crate::reject::beacon_chain_error) + .map_err(warp_utils::reject::beacon_chain_error) } CoreStateId::Slot(slot) => (self.root(chain)?, Some(*slot)), _ => (self.root(chain)?, None), @@ -72,10 +72,13 @@ impl StateId { chain .get_state(&state_root, slot_opt) - .map_err(crate::reject::beacon_chain_error) + .map_err(warp_utils::reject::beacon_chain_error) .and_then(|opt| { opt.ok_or_else(|| { - crate::reject::custom_not_found(format!("beacon state at root {}", state_root)) + warp_utils::reject::custom_not_found(format!( + "beacon state at root {}", + state_root + )) }) }) } @@ -91,7 +94,7 @@ impl StateId { match &self.0 { CoreStateId::Head => chain .with_head(|snapshot| Ok(func(&snapshot.beacon_state))) - .map_err(crate::reject::beacon_chain_error)?, + .map_err(warp_utils::reject::beacon_chain_error)?, _ => func(&self.state(chain)?), } } diff --git a/common/warp_utils/Cargo.toml b/common/warp_utils/Cargo.toml new file mode 100644 index 00000000000..5ee3aa8454c --- /dev/null +++ b/common/warp_utils/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "warp_utils" +version = "0.1.0" +authors = ["Paul Hauner "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +warp = "0.2.5" +eth2 = { path = "../eth2" } +types = { path = "../../consensus/types" } +beacon_chain = { path = "../../beacon_node/beacon_chain" } diff --git a/common/warp_utils/src/lib.rs b/common/warp_utils/src/lib.rs new file mode 100644 index 00000000000..e181278ba37 --- /dev/null +++ b/common/warp_utils/src/lib.rs @@ -0,0 +1 @@ +pub mod reject; diff --git a/beacon_node/http_api/src/reject.rs b/common/warp_utils/src/reject.rs similarity index 100% rename from beacon_node/http_api/src/reject.rs rename to common/warp_utils/src/reject.rs