From b432c8c58c356c8a0c8de96176b091781adc60c6 Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Wed, 4 Sep 2019 11:18:29 +1000 Subject: [PATCH] Replaced unnecessary match statements with map_err and ok_or --- beacon_node/rest_api/src/validator.rs | 50 +++++++++------------------ 1 file changed, 17 insertions(+), 33 deletions(-) diff --git a/beacon_node/rest_api/src/validator.rs b/beacon_node/rest_api/src/validator.rs index bbc976175..84ea485b5 100644 --- a/beacon_node/rest_api/src/validator.rs +++ b/beacon_node/rest_api/src/validator.rs @@ -53,15 +53,11 @@ pub fn get_validator_duties(req: Request) - e )) })?; - let validators: Vec = match query.all_of("validator_pubkeys") { - Ok(v) => v - .iter() - .map(|pk| parse_pubkey(pk)) - .collect::, _>>()?, - Err(e) => { - return Err(e); - } - }; + let validators: Vec = query + .all_of("validator_pubkeys")? + .iter() + .map(|pk| parse_pubkey(pk)) + .collect::, _>>()?; let mut duties: Vec = Vec::new(); // Get a list of all validators for this epoch @@ -167,15 +163,14 @@ pub fn get_new_beacon_block(req: Request) - } }; - let new_block = match beacon_chain.produce_block(randao_reveal, slot) { - Ok((block, _state)) => block, - Err(e) => { - return Err(ApiError::ServerError(format!( + let (new_block, _state) = beacon_chain + .produce_block(randao_reveal, slot) + .map_err(|e| { + ApiError::ServerError(format!( "Beacon node is not able to produce a block: {:?}", e - ))); - } - }; + )) + })?; let body = Body::from( serde_json::to_string(&new_block) @@ -229,14 +224,9 @@ pub fn get_new_attestation(req: Request) -> }; // Check that we are requesting an attestation during the slot where it is relevant. - let present_slot = match beacon_chain.read_slot_clock() { - Some(s) => s, - None => { - return Err(ApiError::ServerError( - "Beacon node is unable to determine present slot, either the state isn't generated or the chain hasn't begun.".into() - )); - } - }; + let present_slot = beacon_chain.read_slot_clock().ok_or(ApiError::ServerError( + "Beacon node is unable to determine present slot, either the state isn't generated or the chain hasn't begun.".into() + ))?; if val_duty.slot != present_slot { return Err(ApiError::InvalidQueryParams(format!("Validator is only able to request an attestation during the slot they are allocated. Current slot: {:?}, allocated slot: {:?}", head_state.slot, val_duty.slot))); } @@ -296,15 +286,9 @@ pub fn get_new_attestation(req: Request) -> return Err(e); } }; - let attestation_data = match beacon_chain.produce_attestation_data(shard) { - Ok(v) => v, - Err(e) => { - return Err(ApiError::ServerError(format!( - "Could not produce an attestation: {:?}", - e - ))); - } - }; + let attestation_data = beacon_chain + .produce_attestation_data(shard) + .map_err(|e| ApiError::ServerError(format!("Could not produce an attestation: {:?}", e)))?; let attestation: Attestation = Attestation { aggregation_bits,