Skip to content

Commit

Permalink
Add support for indexer subgraph pruning (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus authored Oct 17, 2022
1 parent 17bb15e commit 04cd476
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 108 deletions.
7 changes: 7 additions & 0 deletions graph-gateway/src/indexer_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Data {
pub struct IndexingStatus {
pub network: String,
pub block: BlockPointer,
pub min_block: Option<u64>,
pub cost_model: Option<Ptr<CostModel>>,
}

Expand Down Expand Up @@ -186,6 +187,7 @@ impl Actor {
chains {
network
latestBlock { number hash }
earliestBlock { number hash }
}
}
}"# });
Expand Down Expand Up @@ -248,6 +250,10 @@ impl Actor {
number: block_status.number.parse().ok()?,
hash: block_status.hash.clone(),
},
min_block: chain
.earliest_block
.as_ref()
.and_then(|b| b.number.parse::<u64>().ok()),
cost_model,
};
Some((indexing, status))
Expand Down Expand Up @@ -295,6 +301,7 @@ struct IndexingStatusResponse {
struct ChainStatus {
network: String,
latest_block: Option<BlockStatus>,
earliest_block: Option<BlockStatus>,
}

#[derive(Deserialize)]
Expand Down
1 change: 1 addition & 0 deletions graph-gateway/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ async fn write_indexer_inputs(
reported_number: status.block.number,
blocks_behind: latest.saturating_sub(status.block.number),
behind_reported_block: false,
min_block: status.min_block,
}),
},
);
Expand Down
22 changes: 11 additions & 11 deletions graph-gateway/src/query_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};
use futures::future::join_all;
use indexer_selection::{
self, actor::IndexerErrorObservation, Context, FreshnessRequirements, IndexerError,
self, actor::IndexerErrorObservation, BlockRequirements, Context, IndexerError,
IndexerPreferences, IndexerScore, ScoringSample, Selection, SelectionError, UnresolvedBlock,
};
use indexer_selection::{actor::Update, Indexing, UtilityConfig};
Expand Down Expand Up @@ -263,7 +263,7 @@ where
.ok_or_else(|| NetworkNotSupported(subgraph.network.clone()))?;

let block_constraints = block_constraints(&context).ok_or(MalformedQuery)?;
let block_requirements = join_all(
let resolved_blocks = join_all(
block_constraints
.iter()
.filter_map(|constraint| constraint.clone().into_unresolved())
Expand All @@ -272,20 +272,20 @@ where
.await
.into_iter()
.collect::<Result<BTreeSet<BlockPointer>, UnresolvedBlock>>()?;

let freshness_requirements = FreshnessRequirements {
minimum_block: block_requirements.iter().map(|b| b.number).max(),
let min_block = resolved_blocks.iter().map(|b| b.number).min();
let max_block = resolved_blocks.iter().map(|b| b.number).max();
let block_requirements = BlockRequirements {
range: min_block.map(|min| (min, max_block.unwrap())),
has_latest: block_constraints.iter().any(|c| match c {
BlockConstraint::Unconstrained | BlockConstraint::NumberGTE(_) => true,
BlockConstraint::Hash(_) | BlockConstraint::Number(_) => false,
}),
};

// Reject queries for blocks before minimum start block of subgraph manifest.
match freshness_requirements.minimum_block {
Some(min_block) if min_block < subgraph.min_block => return Err(BlockBeforeMin),
_ => (),
};
if matches!(min_block, Some(min_block) if min_block < subgraph.min_block) {
return Err(BlockBeforeMin);
}

let query_count = context.operations.len().max(1) as u64;
let api_key = query.api_key.as_ref().unwrap();
Expand Down Expand Up @@ -360,7 +360,7 @@ where
latest_block.number,
&deployment_indexers,
budget,
&freshness_requirements,
&block_requirements,
)?;
drop(selection_timer);

Expand Down Expand Up @@ -394,7 +394,7 @@ where
let blocks_behind = selection.score.blocks_behind + (latest_unresolved / 2);
let latest_query_block = block_cache.latest(blocks_behind).await?;
let deterministic_query =
make_query_deterministic(context, &block_requirements, &latest_query_block)
make_query_deterministic(context, &resolved_blocks, &latest_query_block)
.ok_or(MalformedQuery)?;

let indexing = selection.indexing;
Expand Down
Loading

0 comments on commit 04cd476

Please sign in to comment.