Add timeouts to canonical head rwlock (#759)

* Add TimeoutRwLock to BeaconChain

* Update network crate

* Update rest api

* Fix beacon chain tests

* Fix rest api tests

* Set test back to !debug_assertions
This commit is contained in:
Paul Hauner
2020-01-06 17:30:37 +11:00
committed by GitHub
parent b0c8b2b700
commit f04c55075e
21 changed files with 391 additions and 156 deletions

View File

@@ -120,12 +120,12 @@ pub fn parse_pubkey_bytes(string: &str) -> Result<PublicKeyBytes, ApiError> {
pub fn block_root_at_slot<T: BeaconChainTypes>(
beacon_chain: &BeaconChain<T>,
target: Slot,
) -> Option<Hash256> {
beacon_chain
.rev_iter_block_roots()
) -> Result<Option<Hash256>, ApiError> {
Ok(beacon_chain
.rev_iter_block_roots()?
.take_while(|(_root, slot)| *slot >= target)
.find(|(_root, slot)| *slot == target)
.map(|(root, _slot)| root)
.map(|(root, _slot)| root))
}
/// Returns a `BeaconState` and it's root in the canonical chain of `beacon_chain` at the given
@@ -137,15 +137,15 @@ pub fn state_at_slot<T: BeaconChainTypes>(
beacon_chain: &BeaconChain<T>,
slot: Slot,
) -> Result<(Hash256, BeaconState<T::EthSpec>), ApiError> {
let head_state = &beacon_chain.head().beacon_state;
let head_state = &beacon_chain.head()?.beacon_state;
if head_state.slot == slot {
// The request slot is the same as the best block (head) slot.
// I'm not sure if this `.clone()` will be optimized out. If not, it seems unnecessary.
Ok((
beacon_chain.head().beacon_state_root,
beacon_chain.head().beacon_state.clone(),
beacon_chain.head()?.beacon_state_root,
beacon_chain.head()?.beacon_state.clone(),
))
} else {
let root = state_root_at_slot(beacon_chain, slot)?;
@@ -168,7 +168,7 @@ pub fn state_root_at_slot<T: BeaconChainTypes>(
beacon_chain: &BeaconChain<T>,
slot: Slot,
) -> Result<Hash256, ApiError> {
let head_state = &beacon_chain.head().beacon_state;
let head_state = &beacon_chain.head()?.beacon_state;
let current_slot = beacon_chain
.slot()
.map_err(|_| ApiError::ServerError("Unable to read slot clock".to_string()))?;
@@ -192,7 +192,7 @@ pub fn state_root_at_slot<T: BeaconChainTypes>(
// 2. The request slot is the same as the best block (head) slot.
//
// The head state root is stored in memory, return a reference.
Ok(beacon_chain.head().beacon_state_root)
Ok(beacon_chain.head()?.beacon_state_root)
} else if head_state.slot > slot {
// 3. The request slot is prior to the head slot.
//
@@ -209,7 +209,7 @@ pub fn state_root_at_slot<T: BeaconChainTypes>(
//
// Use `per_slot_processing` to advance the head state to the present slot,
// assuming that all slots do not contain a block (i.e., they are skipped slots).
let mut state = beacon_chain.head().beacon_state.clone();
let mut state = beacon_chain.head()?.beacon_state.clone();
let spec = &T::EthSpec::default_spec();
for _ in state.slot.as_u64()..slot.as_u64() {