pub fn state_at_slot(
        &self,
        slot: Slot,
        config: StateSkipConfig,
    ) -> Result<BeaconState<T::EthSpec>, Error> {
        let head_state = self.head()?.beacon_state;

        match slot.cmp(&head_state.slot) {
            Ordering::Equal => Ok(head_state),
            Ordering::Greater => {
                if slot > head_state.slot + T::EthSpec::slots_per_epoch() {
                    warn!(
                        self.log,
                        "Skipping more than an epoch";
                        "head_slot" => head_state.slot,
                        "request_slot" => slot
                    )
                }

                let start_slot = head_state.slot;
                let task_start = Instant::now();
                let max_task_runtime = Duration::from_secs(self.spec.seconds_per_slot);

                let head_state_slot = head_state.slot;
                let mut state = head_state;

                let skip_state_root = match config {
                    StateSkipConfig::WithStateRoots => None,
                    StateSkipConfig::WithoutStateRoots => Some(Hash256::zero()),
                };

                while state.slot < slot {
                    // Do not allow and forward state skip that takes longer than the maximum task duration.
                    //
                    // This is a protection against nodes doing too much work when they're not synced
                    // to a chain.
                    if task_start + max_task_runtime < Instant::now() {
                        return Err(Error::StateSkipTooLarge {
                            start_slot,
                            requested_slot: slot,
                            max_task_runtime,
                        });
                    }

                    // Note: supplying some `state_root` when it is known would be a cheap and easy
                    // optimization.
                    match per_slot_processing(&mut state, skip_state_root, &self.spec) {
                        Ok(_) => (),
                        Err(e) => {
                            warn!(
                                self.log,
                                "Unable to load state at slot";
                                "error" => ?e,
                                "head_slot" => head_state_slot,
                                "requested_slot" => slot
                            );
                            return Err(Error::NoStateForSlot(slot));
                        }
                    };
                }
                Ok(state)
            }
            Ordering::Less => {
                let state_root = process_results(self.rev_iter_state_roots()?, |iter| {
                    iter.take_while(|(_, current_slot)| *current_slot >= slot)
                        .find(|(_, current_slot)| *current_slot == slot)
                        .map(|(root, _slot)| root)
                })?
                .ok_or(Error::NoStateForSlot(slot))?;

                Ok(self
                    .get_state(&state_root, Some(slot))?
                    .ok_or(Error::NoStateForSlot(slot))?)
            }
        }
    }
/// Returns the `BeaconState` at the given slot.
///
/// Returns `None` when the state is not found in the database or there is an error skipping
/// to a future state.
Returns the `BeaconState` at the given slot.
Returns `None` when the state is not found in the database or there is an error skipping
to a future state.
Returns the ` BeaconState ` at the given slot . Returns ` None ` when the state is not found in the database or there is an error skipping to a future state .
pub fn state_at_slot(
        &self,
        slot: Slot,
        config: StateSkipConfig,
    ) -> Result<BeaconState<T::EthSpec>, Error> {
        let head_state = self.head()?.beacon_state;
        match slot.cmp(&head_state.slot) {
            Ordering::Equal => Ok(head_state),
            Ordering::Greater => {
                if slot > head_state.slot + T::EthSpec::slots_per_epoch() {
                    warn!(
                        self.log,
                        "Skipping more than an epoch";
                        "head_slot" => head_state.slot,
                        "request_slot" => slot
                    )
                }
                let start_slot = head_state.slot;
                let task_start = Instant::now();
                let max_task_runtime = Duration::from_secs(self.spec.seconds_per_slot);
                let head_state_slot = head_state.slot;
                let mut state = head_state;
                let skip_state_root = match config {
                    StateSkipConfig::WithStateRoots => None,
                    StateSkipConfig::WithoutStateRoots => Some(Hash256::zero()),
                };
                while state.slot < slot {
                    // Do not allow and forward state skip that takes longer than the maximum task duration.
                    //
                    // This is a protection against nodes doing too much work when they're not synced
                    // to a chain.
                    if task_start + max_task_runtime < Instant::now() {
                        return Err(Error::StateSkipTooLarge {
                            start_slot,
                            requested_slot: slot,
                            max_task_runtime,
                        });
                    }
                    // Note: supplying some `state_root` when it is known would be a cheap and easy
                    // optimization.
                    match per_slot_processing(&mut state, skip_state_root, &self.spec) {
                        Ok(_) => (),
                        Err(e) => {
                            warn!(
                                self.log,
                                "Unable to load state at slot";
                                "error" => ?e,
                                "head_slot" => head_state_slot,
                                "requested_slot" => slot
                            );
                            return Err(Error::NoStateForSlot(slot));
                        }
                    };
                }
                Ok(state)
            }
            Ordering::Less => {
                let state_root = process_results(self.rev_iter_state_roots()?, |iter| {
                    iter.take_while(|(_, current_slot)| *current_slot >= slot)
                        .find(|(_, current_slot)| *current_slot == slot)
                        .map(|(root, _slot)| root)
                })?
                .ok_or(Error::NoStateForSlot(slot))?;
                Ok(self
                    .get_state(&state_root, Some(slot))?
                    .ok_or(Error::NoStateForSlot(slot))?)
            }
        }
    }
pub fn state_at_slot ( & self , slot : Slot , config : StateSkipConfig , ) -> Result < BeaconState < T :: EthSpec > , Error > { let head_state = self . head ( ) ? . beacon_state ; match slot . cmp ( & head_state . slot ) { Ordering :: Equal => Ok ( head_state ) , Ordering :: Greater => { if slot > head_state . slot + T :: EthSpec :: slots_per_epoch ( ) { warn ! ( self log " " " " head_state slot " " slot ) } let start_slot = head_state . slot ; let task_start = Instant :: now ( ) ; let max_task_runtime = Duration :: from_secs ( self . spec . seconds_per_slot ) ; let head_state_slot = head_state . slot ; let mut state = head_state ; let skip_state_root = match config { StateSkipConfig :: WithStateRoots => None , StateSkipConfig :: WithoutStateRoots => Some ( Hash256 :: zero ( ) ) , } ; while state . slot < slot { // Do not allow and forward state skip that takes longer than the maximum task duration. // // This is a protection against nodes doing too much work when they're not synced // to a chain. if task_start + max_task_runtime < Instant :: now ( ) { return Err ( Error :: StateSkipTooLarge { start_slot , requested_slot : slot , max_task_runtime , } ) ; } // Note: supplying some `state_root` when it is known would be a cheap and easy // optimization. match per_slot_processing ( & mut state , skip_state_root , & self . spec ) { Ok ( _ ) => ( ) , Err ( e ) => { warn ! ( self log " " " " e " " head_state_slot " " slot ) ; return Err ( Error :: NoStateForSlot ( slot ) ) ; } } ; } Ok ( state ) } Ordering :: Less => { let state_root = process_results ( self . rev_iter_state_roots ( ) ? , | iter | { iter . take_while ( | ( _ , current_slot ) | * current_slot >= slot ) . find ( | ( _ , current_slot ) | * current_slot == slot ) . map ( | ( root , _slot ) | root ) } ) ? . ok_or ( Error :: NoStateForSlot ( slot ) ) ? ; Ok ( self . get_state ( & state_root , Some ( slot ) ) ? . ok_or ( Error :: NoStateForSlot ( slot ) ) ? ) } } }
Returns the `BeaconState` at the given slot.
Returns the ` BeaconState ` at the given slot .
&self
slot
Slot
config
StateSkipConfig