Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add validation & tests for unused features in subgraph manifest #5861

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 8 additions & 1 deletion graph/src/data/subgraph/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ pub enum SubgraphFeatureValidationError {
#[error("The feature `{}` is used by the subgraph but it is not declared in the manifest.", fmt_subgraph_features(.0))]
Undeclared(BTreeSet<SubgraphFeature>),

/// A feature is declared in the `features` section of the manifest file but it is not used by the subgraph.
#[error("The feature `{}` is not used by the subgraph but it is declared in the manifest.", fmt_subgraph_features(.0))]
Unused(BTreeSet<SubgraphFeature>),

/// The provided compiled mapping is not a valid WASM module.
#[error("Failed to parse the provided mapping WASM module")]
InvalidMapping,
Expand All @@ -79,7 +83,10 @@ pub fn validate_subgraph_features<C: Blockchain>(
let declared: &BTreeSet<SubgraphFeature> = &manifest.features;
let used = detect_features(manifest)?;
let undeclared: BTreeSet<SubgraphFeature> = used.difference(declared).cloned().collect();
if !undeclared.is_empty() {
let unsed: BTreeSet<SubgraphFeature> = declared.difference(&used).cloned().collect();
if !unsed.is_empty() {
Err(SubgraphFeatureValidationError::Unused(unsed))
} else if !undeclared.is_empty() {
Err(SubgraphFeatureValidationError::Undeclared(undeclared))
} else {
Ok(used)
Expand Down
36 changes: 36 additions & 0 deletions store/test-store/tests/chain/ethereum/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,42 @@ graft:
})
}

#[test]
fn can_detect_unsed_feature_in_subgraph_manifest() {
const YAML: &str = "
specVersion: 0.0.4
dataSources: []
features:
- grafting
- fullTextSearch
- ipfsOnEthereumContracts
schema:
file:
/: /ipfs/Qmschema
";
test_store::run_test_sequentially(|store| async move {
let store = store.subgraph_store();
let unvalidated = resolve_unvalidated(YAML).await;
let error_msg = unvalidated
.validate(store.clone(), true)
.await
.expect_err("Validation must fail")
.into_iter()
.find(|e| {
matches!(
e,
SubgraphManifestValidationError::FeatureValidationError(_)
)
})
.expect("There must be a FeatureValidation error")
.to_string();
assert_eq!(
"The feature `grafting, fullTextSearch, ipfsOnEthereumContracts` is not used by the subgraph but it is declared in the manifest.",
error_msg
)
})
}

#[test]
fn declared_grafting_feature_causes_no_feature_validation_errors() {
const YAML: &str = "
Expand Down
Loading