Merge branch 'api-alignment' into validator-post-fns

This commit is contained in:
Luke Anderson 2019-09-12 15:12:54 +10:00
commit 82f4303787
No known key found for this signature in database
GPG Key ID: 44408169EC61E228
6 changed files with 31 additions and 38 deletions

View File

@ -110,7 +110,7 @@ pub fn get_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
/// HTTP handler to return a `BeaconBlock` root at a given `slot`.
pub fn get_block_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let slot_string = UrlQuery::from_request(&req)?.only_one("slot")?;
let target = parse_slot(&slot_string)?;
@ -127,7 +127,8 @@ pub fn get_block_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
/// HTTP handler to return the `Fork` of the current head.
pub fn get_fork<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (_beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let head_state = get_head_state(beacon_chain)?;
let json: String = serde_json::to_string(&head_state.fork).map_err(|e| {
ApiError::ServerError(format!("Unable to serialize BeaconState::Fork: {:?}", e))
@ -141,7 +142,7 @@ pub fn get_fork<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
/// The `Epoch` parameter can be any epoch number. If it is not specified,
/// the current epoch is assumed.
pub fn get_validators<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let epoch = match UrlQuery::from_request(&req) {
// We have some parameters, so make sure it's the epoch one and parse it
@ -183,7 +184,8 @@ pub struct StateResponse<T: EthSpec> {
/// Will not return a state if the request slot is in the future. Will return states higher than
/// the current head by skipping slots.
pub fn get_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let head_state = get_head_state(beacon_chain.clone())?;
let (key, value) = match UrlQuery::from_request(&req) {
Ok(query) => {
@ -234,7 +236,7 @@ pub fn get_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
/// Will not return a state if the request slot is in the future. Will return states higher than
/// the current head by skipping slots.
pub fn get_state_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let slot_string = UrlQuery::from_request(&req)?.only_one("slot")?;
let slot = parse_slot(&slot_string)?;
@ -251,13 +253,10 @@ pub fn get_state_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
pub fn get_current_finalized_checkpoint<T: BeaconChainTypes + 'static>(
req: Request<Body>,
) -> ApiResult {
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let head_state = get_head_state(beacon_chain)?;
let checkpoint = beacon_chain
.head()
.beacon_state
.finalized_checkpoint
.clone();
let checkpoint = head_state.finalized_checkpoint.clone();
let json: String = serde_json::to_string(&checkpoint)
.map_err(|e| ApiError::ServerError(format!("Unable to serialize checkpoint: {:?}", e)))?;
@ -267,7 +266,7 @@ pub fn get_current_finalized_checkpoint<T: BeaconChainTypes + 'static>(
/// HTTP handler to return a `BeaconState` at the genesis block.
pub fn get_genesis_state<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let (_root, state) = state_at_slot(&beacon_chain, Slot::new(0))?;

View File

@ -228,21 +228,24 @@ pub fn implementation_pending_response(_req: Request<Body>) -> ApiResult {
pub fn get_beacon_chain_from_request<T: BeaconChainTypes + 'static>(
req: &Request<Body>,
) -> Result<(Arc<BeaconChain<T>>, BeaconState<T::EthSpec>), ApiError> {
) -> Result<(Arc<BeaconChain<T>>), ApiError> {
// Get beacon state
let beacon_chain = req
.extensions()
.get::<Arc<BeaconChain<T>>>()
.ok_or_else(|| ApiError::ServerError("Beacon chain extension missing".into()))?;
let mut head_state = beacon_chain
.state_now()
.map_err(|e| ApiError::ServerError(format!("Unable to get current BeaconState {:?}", e)))?;
if let Some(s) = head_state.maybe_as_mut_ref() {
s.build_all_caches(&beacon_chain.spec).ok();
Ok(beacon_chain.clone())
}
Ok((beacon_chain.clone(), head_state.clone()))
pub fn get_head_state<T: BeaconChainTypes + 'static>(
bc: Arc<BeaconChain<T>>,
) -> Result<BeaconState<T::EthSpec>, ApiError> {
let mut head_state = bc.head().beacon_state;
head_state
.build_all_caches(&bc.spec)
.map_err(|e| ApiError::ServerError(format!("Unable to build state cache: {:?}", e)))?;
Ok(head_state)
}
pub fn get_logger_from_request(req: &Request<Body>) -> slog::Logger {

View File

@ -31,7 +31,7 @@ pub fn get_prometheus<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
let mut buffer = vec![];
let encoder = TextEncoder::new();
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let db_path = req
.extensions()
.get::<DBPath>()

View File

@ -11,7 +11,8 @@ pub fn get_version(req: Request<Body>) -> ApiResult {
/// Read the genesis time from the current beacon chain state.
pub fn get_genesis_time<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (_beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let head_state = get_head_state(beacon_chain)?;
let gen_time: u64 = head_state.genesis_time;
success_response_2(req, &gen_time)
}

View File

@ -9,7 +9,7 @@ use types::EthSpec;
/// HTTP handler to return the full spec object.
pub fn get_spec<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let json: String = serde_json::to_string(&beacon_chain.spec)
.map_err(|e| ApiError::ServerError(format!("Unable to serialize spec: {:?}", e)))?;

View File

@ -43,7 +43,8 @@ impl ValidatorDuty {
pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let log = get_logger_from_request(&req);
slog::trace!(log, "Validator duties requested of API: {:?}", &req);
let (beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let head_state = get_head_state(beacon_chain.clone())?;
slog::trace!(log, "Got head state from request.");
// Parse and check query parameters
@ -80,18 +81,6 @@ pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -
.collect::<Result<Vec<_>, _>>()?;
let mut duties: Vec<ValidatorDuty> = Vec::new();
// Update the committee cache
// TODO: Do we need to update the cache on the state, for the epoch which has been specified?
beacon_chain
.state_now()
.map_err(|e| ApiError::ServerError(format!("Unable to get current BeaconState {:?}", e)))?
.maybe_as_mut_ref()
.ok_or(ApiError::ServerError(
"Unable to get mutable BeaconState".into(),
))?
.build_committee_cache(relative_epoch, &beacon_chain.spec)
.map_err(|e| ApiError::ServerError(format!("Unable to build state caches: {:?}", e)))?;
// Get a list of all validators for this epoch
let validator_proposers: Vec<usize> = epoch
.slot_iter(T::EthSpec::slots_per_epoch())
@ -166,7 +155,7 @@ pub fn get_validator_duties<T: BeaconChainTypes + 'static>(req: Request<Body>) -
/// HTTP Handler to produce a new BeaconBlock from the current state, ready to be signed by a validator.
pub fn get_new_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (beacon_chain, _head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let query = UrlQuery::from_request(&req)?;
let slot = query
@ -267,7 +256,8 @@ pub fn publish_beacon_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -
/// HTTP Handler to produce a new Attestation from the current state, ready to be signed by a validator.
pub fn get_new_attestation<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult {
let (beacon_chain, head_state) = get_beacon_chain_from_request::<T>(&req)?;
let beacon_chain = get_beacon_chain_from_request::<T>(&req)?;
let head_state = get_head_state(beacon_chain.clone())?;
let query = UrlQuery::from_request(&req)?;
let val_pk_str = query