Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Onchain scraper in dispute-coordinator will scrape SCRAPED_FINALIZED_BLOCKS_COUNT blocks before finality #7013

Merged
merged 3 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions node/core/dispute-coordinator/src/scraping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::{
collections::{BTreeMap, HashSet},
num::NonZeroUsize,
num::{NonZeroU32, NonZeroUsize},
};

use futures::channel::oneshot;
Expand Down Expand Up @@ -177,6 +177,17 @@ impl ChainScraper {
/// As long as we have `MAX_FINALITY_LAG` this makes sense as a value.
pub(crate) const ANCESTRY_SIZE_LIMIT: u32 = MAX_FINALITY_LAG;

/// How many finalized block the scraper should process.
///
/// Initialize this value with `DISPUTE_CANDIDATE_LIFETIME_AFTER_FINALIZATION`
/// If `SCRAPED_FINALIZED_BLOCKS_COUNT` > `DISPUTE_CANDIDATE_LIFETIME_AFTER_FINALIZATION`
/// scraper's job will be undone on pruning.
pub(crate) const SCRAPED_FINALIZED_BLOCKS_COUNT: NonZeroU32 =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why these two values would ever need to differ. I would have just used DISPUTE_CANDIDATE_LIFETIME_AFTER_FINALIZATION directly, just changing docs. Name is actually still fine, I think.

match NonZeroU32::new(DISPUTE_CANDIDATE_LIFETIME_AFTER_FINALIZATION) {
Some(cap) => cap,
None => panic!("Scraped finalized blocks count must be non-zero"),
};

/// Create a properly initialized `OrderingProvider`.
///
/// Returns: `Self` and any scraped votes.
Expand Down Expand Up @@ -228,9 +239,10 @@ impl ChainScraper {
None => return Ok(ScrapedUpdates::new()),
};

// Fetch ancestry up to last finalized block.
// Fetch ancestry up to `SCRAPED_FINALIZED_BLOCKS_COUNT` blocks beyond
// the last finalized one
let ancestors = self
.get_unfinalized_block_ancestors(sender, activated.hash, activated.number)
.get_relevant_block_ancestors(sender, activated.hash, activated.number)
.await?;

// Ancestors block numbers are consecutive in the descending order.
Expand Down Expand Up @@ -330,10 +342,11 @@ impl ChainScraper {
}

/// Returns ancestors of `head` in the descending order, stopping
/// either at the block present in cache or at the last finalized block.
/// either at the block present in cache or at `SCRAPED_FINALIZED_BLOCKS_COUNT -1` blocks after
/// the last finalized one (called `target_ancestor`).
///
/// Both `head` and the latest finalized block are **not** included in the result.
async fn get_unfinalized_block_ancestors<Sender>(
/// Both `head` and the `target_ancestor` blocks are **not** included in the result.
async fn get_relevant_block_ancestors<Sender>(
&mut self,
sender: &mut Sender,
mut head: Hash,
Expand All @@ -342,7 +355,9 @@ impl ChainScraper {
where
Sender: overseer::DisputeCoordinatorSenderTrait,
{
let target_ancestor = get_finalized_block_number(sender).await?;
let target_ancestor = get_finalized_block_number(sender)
.await?
.saturating_sub(Self::SCRAPED_FINALIZED_BLOCKS_COUNT.get());

let mut ancestors = Vec::new();

Expand Down
4 changes: 3 additions & 1 deletion node/core/dispute-coordinator/src/scraping/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,9 @@ fn scraper_requests_candidates_of_non_finalized_ancestors() {
&mut virtual_overseer,
&chain,
finalized_block_number,
BLOCKS_TO_SKIP - finalized_block_number as usize, // Expect the provider not to go past finalized block.
BLOCKS_TO_SKIP -
(finalized_block_number - ChainScraper::SCRAPED_FINALIZED_BLOCKS_COUNT.get())
as usize, // Expect the provider not to go past finalized block.
get_backed_and_included_candidate_events,
);
join(process_active_leaves_update(ctx.sender(), &mut ordering, next_update), overseer_fut)
Expand Down