Address some review comments

This commit is contained in:
Paul Hauner 2019-08-22 16:14:51 +10:00
parent b912e26b79
commit 5a34f86e77
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
3 changed files with 29 additions and 24 deletions

View File

@ -10,7 +10,7 @@ use url::Host;
#[derive(Debug)]
enum Error {
UrlCannotBeBase,
InvalidUrl,
HttpError(HttpError),
}
@ -38,7 +38,7 @@ impl Bootstrapper {
/// Build a multiaddr using the HTTP server URL that is not guaranteed to be correct.
///
/// The address is created by querying the HTTP server for it's listening libp2p addresses.
/// The address is created by querying the HTTP server for its listening libp2p addresses.
/// Then, we find the first TCP port in those addresses and combine the port with the URL of
/// the server.
///
@ -124,7 +124,7 @@ fn get_slots_per_epoch(mut url: Url) -> Result<Slot, Error> {
.map(|mut url| {
url.push("spec").push("slots_per_epoch");
})
.map_err(|_| Error::UrlCannotBeBase)?;
.map_err(|_| Error::InvalidUrl)?;
reqwest::get(url)?
.error_for_status()?
@ -137,7 +137,7 @@ fn get_finalized_slot(mut url: Url, slots_per_epoch: u64) -> Result<Slot, Error>
.map(|mut url| {
url.push("beacon").push("latest_finalized_checkpoint");
})
.map_err(|_| Error::UrlCannotBeBase)?;
.map_err(|_| Error::InvalidUrl)?;
let checkpoint: Checkpoint = reqwest::get(url)?.error_for_status()?.json()?;
@ -149,7 +149,7 @@ fn get_state<T: EthSpec>(mut url: Url, slot: Slot) -> Result<BeaconState<T>, Err
.map(|mut url| {
url.push("beacon").push("state");
})
.map_err(|_| Error::UrlCannotBeBase)?;
.map_err(|_| Error::InvalidUrl)?;
url.query_pairs_mut()
.append_pair("slot", &format!("{}", slot.as_u64()));
@ -165,7 +165,7 @@ fn get_block<T: EthSpec>(mut url: Url, slot: Slot) -> Result<BeaconBlock<T>, Err
.map(|mut url| {
url.push("beacon").push("block");
})
.map_err(|_| Error::UrlCannotBeBase)?;
.map_err(|_| Error::InvalidUrl)?;
url.query_pairs_mut()
.append_pair("slot", &format!("{}", slot.as_u64()));
@ -181,7 +181,7 @@ fn get_enr(mut url: Url) -> Result<Enr, Error> {
.map(|mut url| {
url.push("node").push("network").push("enr");
})
.map_err(|_| Error::UrlCannotBeBase)?;
.map_err(|_| Error::InvalidUrl)?;
reqwest::get(url)?
.error_for_status()?
@ -194,7 +194,7 @@ fn get_listen_addresses(mut url: Url) -> Result<Vec<Multiaddr>, Error> {
.map(|mut url| {
url.push("node").push("network").push("listen_addresses");
})
.map_err(|_| Error::UrlCannotBeBase)?;
.map_err(|_| Error::InvalidUrl)?;
reqwest::get(url)?
.error_for_status()?

View File

@ -54,12 +54,7 @@ pub fn get_block<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiResult
("slot", value) => {
let target = parse_slot(&value)?;
beacon_chain
.rev_iter_block_roots()
.take_while(|(_root, slot)| *slot >= target)
.find(|(_root, slot)| *slot == target)
.map(|(root, _slot)| root)
.ok_or_else(|| {
block_root_at_slot(&beacon_chain, target).ok_or_else(|| {
ApiError::NotFound(format!("Unable to find BeaconBlock for slot {}", target))
})?
}
@ -99,12 +94,7 @@ pub fn get_block_root<T: BeaconChainTypes + 'static>(req: Request<Body>) -> ApiR
let slot_string = UrlQuery::from_request(&req)?.only_one("slot")?;
let target = parse_slot(&slot_string)?;
let root = beacon_chain
.rev_iter_block_roots()
.take_while(|(_root, slot)| *slot >= target)
.find(|(_root, slot)| *slot == target)
.map(|(root, _slot)| root)
.ok_or_else(|| {
let root = block_root_at_slot(&beacon_chain, target).ok_or_else(|| {
ApiError::NotFound(format!("Unable to find BeaconBlock for slot {}", target))
})?;

View File

@ -31,6 +31,21 @@ pub fn parse_root(string: &str) -> Result<Hash256, ApiError> {
}
}
/// Returns the root of the `BeaconBlock` in the canonical chain of `beacon_chain` at the given
/// `slot`, if possible.
///
/// May return a root for a previous slot, in the case of skip slots.
pub fn block_root_at_slot<T: BeaconChainTypes>(
beacon_chain: &BeaconChain<T>,
target: Slot,
) -> Option<Hash256> {
beacon_chain
.rev_iter_block_roots()
.take_while(|(_root, slot)| *slot >= target)
.find(|(_root, slot)| *slot == target)
.map(|(root, _slot)| root)
}
/// Returns a `BeaconState` and it's root in the canonical chain of `beacon_chain` at the given
/// `slot`, if possible.
///