Skip to content

Commit

Permalink
Use hashset to filter validators ids in http_api (#5468)
Browse files Browse the repository at this point in the history
* Use hashset to filter validators ids in http_api

* Update beacon_node/http_api/src/validators.rs
  • Loading branch information
0xalex88 authored Apr 9, 2024
1 parent 06eb181 commit 1b88d29
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
27 changes: 12 additions & 15 deletions beacon_node/http_api/src/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use eth2::types::{
self as api_types, ExecutionOptimisticFinalizedResponse, ValidatorBalanceData, ValidatorData,
ValidatorId, ValidatorStatus,
};
use std::sync::Arc;
use std::{collections::HashSet, sync::Arc};

pub fn get_beacon_state_validators<T: BeaconChainTypes>(
state_id: StateId,
Expand All @@ -18,6 +18,8 @@ pub fn get_beacon_state_validators<T: BeaconChainTypes>(
|state, execution_optimistic, finalized| {
let epoch = state.current_epoch();
let far_future_epoch = chain.spec.far_future_epoch;
let ids_filter_set: Option<HashSet<&ValidatorId>> =
query_ids.as_ref().map(HashSet::from_iter);

Ok((
state
Expand All @@ -27,13 +29,9 @@ pub fn get_beacon_state_validators<T: BeaconChainTypes>(
.enumerate()
// filter by validator id(s) if provided
.filter(|(index, (validator, _))| {
query_ids.as_ref().map_or(true, |ids| {
ids.iter().any(|id| match id {
ValidatorId::PublicKey(pubkey) => &validator.pubkey == pubkey,
ValidatorId::Index(param_index) => {
*param_index == *index as u64
}
})
ids_filter_set.as_ref().map_or(true, |ids_set| {
ids_set.contains(&ValidatorId::PublicKey(validator.pubkey))
|| ids_set.contains(&ValidatorId::Index(*index as u64))
})
})
// filter by status(es) if provided and map the result
Expand Down Expand Up @@ -83,6 +81,9 @@ pub fn get_beacon_state_validator_balances<T: BeaconChainTypes>(
.map_state_and_execution_optimistic_and_finalized(
&chain,
|state, execution_optimistic, finalized| {
let ids_filter_set: Option<HashSet<&ValidatorId>> =
optional_ids.map(|f| HashSet::from_iter(f.iter()));

Ok((
state
.validators()
Expand All @@ -91,13 +92,9 @@ pub fn get_beacon_state_validator_balances<T: BeaconChainTypes>(
.enumerate()
// filter by validator id(s) if provided
.filter(|(index, (validator, _))| {
optional_ids.map_or(true, |ids| {
ids.iter().any(|id| match id {
ValidatorId::PublicKey(pubkey) => &validator.pubkey == pubkey,
ValidatorId::Index(param_index) => {
*param_index == *index as u64
}
})
ids_filter_set.as_ref().map_or(true, |ids_set| {
ids_set.contains(&ValidatorId::PublicKey(validator.pubkey))
|| ids_set.contains(&ValidatorId::Index(*index as u64))
})
})
.map(|(index, (_, balance))| ValidatorBalanceData {
Expand Down
2 changes: 1 addition & 1 deletion common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ pub struct FinalityCheckpointsData {
pub finalized: Checkpoint,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(into = "String")]
#[serde(try_from = "std::borrow::Cow<str>")]
pub enum ValidatorId {
Expand Down

0 comments on commit 1b88d29

Please sign in to comment.