forked from sigp/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement POST validators/validator_balances APIs (sigp#4872)
* Add POST for fetching validators from state * Implement POST for balances * Tests
- Loading branch information
1 parent
e02adbf
commit b882519
Showing
5 changed files
with
305 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
use crate::state_id::StateId; | ||
use beacon_chain::{BeaconChain, BeaconChainTypes}; | ||
use eth2::types::{ | ||
self as api_types, ExecutionOptimisticFinalizedResponse, ValidatorBalanceData, ValidatorData, | ||
ValidatorId, ValidatorStatus, | ||
}; | ||
use std::sync::Arc; | ||
|
||
pub fn get_beacon_state_validators<T: BeaconChainTypes>( | ||
state_id: StateId, | ||
chain: Arc<BeaconChain<T>>, | ||
query_ids: &Option<Vec<ValidatorId>>, | ||
query_statuses: &Option<Vec<ValidatorStatus>>, | ||
) -> Result<ExecutionOptimisticFinalizedResponse<Vec<ValidatorData>>, warp::Rejection> { | ||
let (data, execution_optimistic, finalized) = state_id | ||
.map_state_and_execution_optimistic_and_finalized( | ||
&chain, | ||
|state, execution_optimistic, finalized| { | ||
let epoch = state.current_epoch(); | ||
let far_future_epoch = chain.spec.far_future_epoch; | ||
|
||
Ok(( | ||
state | ||
.validators() | ||
.iter() | ||
.zip(state.balances().iter()) | ||
.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 | ||
} | ||
}) | ||
}) | ||
}) | ||
// filter by status(es) if provided and map the result | ||
.filter_map(|(index, (validator, balance))| { | ||
let status = api_types::ValidatorStatus::from_validator( | ||
validator, | ||
epoch, | ||
far_future_epoch, | ||
); | ||
|
||
let status_matches = query_statuses.as_ref().map_or(true, |statuses| { | ||
statuses.contains(&status) | ||
|| statuses.contains(&status.superstatus()) | ||
}); | ||
|
||
if status_matches { | ||
Some(ValidatorData { | ||
index: index as u64, | ||
balance: *balance, | ||
status, | ||
validator: validator.clone(), | ||
}) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect::<Vec<_>>(), | ||
execution_optimistic, | ||
finalized, | ||
)) | ||
}, | ||
)?; | ||
|
||
Ok(ExecutionOptimisticFinalizedResponse { | ||
data, | ||
execution_optimistic: Some(execution_optimistic), | ||
finalized: Some(finalized), | ||
}) | ||
} | ||
|
||
pub fn get_beacon_state_validator_balances<T: BeaconChainTypes>( | ||
state_id: StateId, | ||
chain: Arc<BeaconChain<T>>, | ||
optional_ids: Option<&[ValidatorId]>, | ||
) -> Result<ExecutionOptimisticFinalizedResponse<Vec<ValidatorBalanceData>>, warp::Rejection> { | ||
let (data, execution_optimistic, finalized) = state_id | ||
.map_state_and_execution_optimistic_and_finalized( | ||
&chain, | ||
|state, execution_optimistic, finalized| { | ||
Ok(( | ||
state | ||
.validators() | ||
.iter() | ||
.zip(state.balances().iter()) | ||
.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 | ||
} | ||
}) | ||
}) | ||
}) | ||
.map(|(index, (_, balance))| ValidatorBalanceData { | ||
index: index as u64, | ||
balance: *balance, | ||
}) | ||
.collect::<Vec<_>>(), | ||
execution_optimistic, | ||
finalized, | ||
)) | ||
}, | ||
)?; | ||
|
||
Ok(api_types::ExecutionOptimisticFinalizedResponse { | ||
data, | ||
execution_optimistic: Some(execution_optimistic), | ||
finalized: Some(finalized), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.