/// Unvalidated pool below or equal to the latest expected batch height can
/// be purged from the pool.
///
/// To avoid producing redundant purge action, we use a transient cache to
/// remember the previous expected batch height, and only produce a new
/// purge action when the expected batch height changes.
///
/// There are two important exceptions:
/// 1. we do not purge unvalidated pool when expected_batch_height >
/// finalized_height + 1. This is because under normal condition,
/// expected_batch_height <= finalized_height + 1. The only time it
/// might become greater than finalized_height + 1 is when
/// we just finished a state sync. In this case we may not have moved
/// CatchUpPackage to the validated pool. So we should not purge the
/// unvalidated pool.
///
/// 2. We do not purge unvalidated pool when there exists unvalidated
/// CatchUpPackage or share with height higher than catch_up_height
/// but lower than the expected batch height. This is to ensure we do not
/// miss processing unvalidated CatchUpPackages or shares.
Unvalidated pool below or equal to the latest expected batch height can
be purged from the pool.

To avoid producing redundant purge action, we use a transient cache to
remember the previous expected batch height, and only produce a new
purge action when the expected batch height changes.

There are two important exceptions:
1. we do not purge unvalidated pool when expected_batch_height >
finalized_height + 1. This is because under normal condition,
expected_batch_height <= finalized_height + 1. The only time it
might become greater than finalized_height + 1 is when
we just finished a state sync. In this case we may not have moved
CatchUpPackage to the validated pool. So we should not purge the
unvalidated pool.

2. We do not purge unvalidated pool when there exists unvalidated
CatchUpPackage or share with height higher than catch_up_height
but lower than the expected batch height. This is to ensure we do not
miss processing unvalidated CatchUpPackages or shares.
Unvalidated pool below or equal to the latest expected batch height can be purged from the pool . To avoid producing redundant purge action we use a transient cache to remember the previous expected batch height and only produce a new purge action when the expected batch height changes . There are two important exceptions : 1 . we do not purge unvalidated pool when expected_batch_height > finalized_height + 1 . This is because under normal condition expected_batch_height < = finalized_height + 1 . The only time it might become greater than finalized_height + 1 is when we just finished a state sync . In this case we may not have moved CatchUpPackage to the validated pool . So we should not purge the unvalidated pool . 2 . We do not purge unvalidated pool when there exists unvalidated CatchUpPackage or share with height higher than catch_up_height but lower than the expected batch height . This is to ensure we do not miss processing unvalidated CatchUpPackages or shares .
fn purge_unvalidated_pool_by_expected_batch_height(
        &self,
        pool_reader: &PoolReader<'_>,
        changeset: &mut ChangeSet,
    ) {
        let finalized_height = pool_reader.get_finalized_height();
        let expected_batch_height = self.message_routing.expected_batch_height();
        let mut prev_expected_batch_height = self.prev_expected_batch_height.borrow_mut();
        if *prev_expected_batch_height < expected_batch_height
            && expected_batch_height <= finalized_height.increment()
        {
            let catch_up_height = pool_reader.get_catch_up_height();
            let unvalidated_pool = pool_reader.pool().unvalidated();
            fn below_range_max(h: Height, range: &Option<HeightRange>) -> bool {
                range.as_ref().map(|r| h < r.max).unwrap_or(false)
            }
            fn above_range_min(h: Height, range: &Option<HeightRange>) -> bool {
                range.as_ref().map(|r| h > r.min).unwrap_or(false)
            }
            // Skip purging if we have unprocessed but needed CatchUpPackage
            let unvalidated_catch_up_range = unvalidated_pool.catch_up_package().height_range();
            if below_range_max(catch_up_height, &unvalidated_catch_up_range)
                && above_range_min(expected_batch_height, &unvalidated_catch_up_range)
            {
                return;
            }
            // Skip purging if we have unprocessed but needed CatchUpPackageShare
            let unvalidated_catch_up_share_range =
                unvalidated_pool.catch_up_package_share().height_range();
            if below_range_max(catch_up_height, &unvalidated_catch_up_share_range)
                && above_range_min(expected_batch_height, &unvalidated_catch_up_share_range)
            {
                return;
            }
            *prev_expected_batch_height = expected_batch_height;
            // Because random_beacon of expected_batch_height - 1 is required to
            // make progress, we should only purge below expected_batch_height - 1.
            // This is safe because expected_batch_height is always greater than 0.
            changeset.push(ChangeAction::PurgeUnvalidatedBelow(
                expected_batch_height.decrement(),
            ));
            trace!(
                self.log,
                "Purge unvalidated pool below {:?}",
                expected_batch_height
            );
            self.metrics
                .unvalidated_pool_purge_height
                .set(expected_batch_height.get() as i64);
        }
    }
fn purge_unvalidated_pool_by_expected_batch_height ( & self , pool_reader : & PoolReader < ' _ > , changeset : & mut ChangeSet , ) { let finalized_height = pool_reader . get_finalized_height ( ) ; let expected_batch_height = self . message_routing . expected_batch_height ( ) ; let mut prev_expected_batch_height = self . prev_expected_batch_height . borrow_mut ( ) ; if * prev_expected_batch_height < expected_batch_height && expected_batch_height <= finalized_height . increment ( ) { let catch_up_height = pool_reader . get_catch_up_height ( ) ; let unvalidated_pool = pool_reader . pool ( ) . unvalidated ( ) ; fn below_range_max ( h : Height , range : & Option < HeightRange > ) -> bool { range . as_ref ( ) . map ( | r | h < r . max ) . unwrap_or ( false ) } fn above_range_min ( h : Height , range : & Option < HeightRange > ) -> bool { range . as_ref ( ) . map ( | r | h > r . min ) . unwrap_or ( false ) } // Skip purging if we have unprocessed but needed CatchUpPackage let unvalidated_catch_up_range = unvalidated_pool . catch_up_package ( ) . height_range ( ) ; if below_range_max ( catch_up_height , & unvalidated_catch_up_range ) && above_range_min ( expected_batch_height , & unvalidated_catch_up_range ) { return ; } // Skip purging if we have unprocessed but needed CatchUpPackageShare let unvalidated_catch_up_share_range = unvalidated_pool . catch_up_package_share ( ) . height_range ( ) ; if below_range_max ( catch_up_height , & unvalidated_catch_up_share_range ) && above_range_min ( expected_batch_height , & unvalidated_catch_up_share_range ) { return ; } * prev_expected_batch_height = expected_batch_height ; // Because random_beacon of expected_batch_height - 1 is required to // make progress, we should only purge below expected_batch_height - 1. // This is safe because expected_batch_height is always greater than 0. changeset . push ( ChangeAction :: PurgeUnvalidatedBelow ( expected_batch_height . decrement ( ) , ) ) ; trace ! ( self log " " expected_batch_height ) ; self . metrics . unvalidated_pool_purge_height . set ( expected_batch_height . get ( ) as i64 ) ; } }
Unvalidated pool below or equal to the latest expected batch height can
be purged from the pool.
Unvalidated pool below or equal to the latest expected batch height can be purged from the pool .
&self
pool_reader
&PoolReader<'_>
changeset
&mut ChangeSet