tor-netdir: Provide relay_by_rs_idx

This commit is contained in:
Ian Jackson 2023-03-27 16:51:55 +01:00
parent 13edaf68de
commit 7924e995f1
1 changed files with 16 additions and 0 deletions

View File

@ -843,6 +843,22 @@ impl NetDir {
assert!(answer.has_identity(id));
Some(answer)
}
/// Obtain a `Relay` given a `RouterStatusIdx`
///
/// Differs from `relay_from_rs_and_rsi` as follow:
/// * That function expects the caller to already have an `MdConsensusRouterStatus`;
/// it checks with `debug_assert` that the relay in the netdir matches.
/// * That function panics if the `RouterStatusIdx` is invalid; this one returns `None`.
/// * That function returns an `UncheckedRelay`; this one a `Relay`.
///
/// `None` could be returned here, even with a valid `rsi`,
/// if `rsi` refers to an unuseable relay.
#[cfg_attr(not(feature = "hs-common"), allow(dead_code))]
pub(crate) fn relay_by_rs_idx(&self, rs_idx: RouterStatusIdx) -> Option<Relay<'_>> {
let rs = self.c_relays().get(rs_idx)?;
let md = self.mds.get(rs_idx)?.as_deref();
UncheckedRelay { rs, md }.into_relay()
}
/// Return a relay with the same identities as those in `target`, if one
/// exists.