Skip to content

Commit

Permalink
Remove reject into new warp_utils
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 16, 2020
1 parent e8ad813 commit 7c01e39
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 68 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions beacon_node/http_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
6 changes: 3 additions & 3 deletions beacon_node/http_api/src/beacon_proposer_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
19 changes: 11 additions & 8 deletions beacon_node/http_api/src/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
))
Expand Down
16 changes: 8 additions & 8 deletions beacon_node/http_api/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ pub fn global_validator_inclusion_data<T: BeaconChainTypes>(

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;

Expand All @@ -45,22 +45,22 @@ pub fn validator_inclusion_data<T: BeaconChainTypes>(

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 {
Expand Down
Loading

0 comments on commit 7c01e39

Please sign in to comment.