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

Emit a warning when data sources have duplicate functionality #556

Open
wants to merge 1 commit into
base: main
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
25 changes: 24 additions & 1 deletion src/subgraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,29 @@ More than one data source named '${name}', data source names must be unique.`,
}, immutable.List())
}

static validateUniqueDataSources(manifest) {
let data_source_identifiers = immutable.List()
return manifest.get('dataSources').reduce((errors, dataSource, dataSourceIndex) => {
let path = ['dataSources', dataSourceIndex]
let data_source_identifier = dataSource.get('source').merge(dataSource.get('mapping'))
if (data_source_identifiers.includes(data_source_identifier)) {
console.log("duplicate")
errors = errors.push(
immutable.fromJS({
path,
message: `\
This data source, '${dataSource.get('name')}', has identical source and mapping to an already declared data source.
They will both listen to the same triggers, process them with the same mappings, and generate the same entity updates.`,
}),
)
}

data_source_identifiers = data_source_identifiers.push(data_source_identifier)
return errors
}, immutable.List())
}


static validateUniqueTemplateNames(manifest) {
let names = []
return manifest
Expand Down Expand Up @@ -460,7 +483,7 @@ More than one template named '${name}', template names must be unique.`,
: immutable.List.of(
...Subgraph.validateRepository(manifest, { resolveFile }),
...Subgraph.validateDescription(manifest, { resolveFile }),
...Subgraph.validateEthereumContractHandlers(manifest),
...Subgraph.validateUniqueDataSource(manifest),
)

return {
Expand Down
9 changes: 9 additions & 0 deletions tests/cli/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ describe('Validation', () => {
},
)

cliTest(
'Duplicate data source functionality',
['codegen', '--skip-migrations'],
'validation/duplicate-data-source-functionality',
{
exitCode: 0,
},
)

cliTest(
'Duplicate template name',
['codegen', '--skip-migrations'],
Expand Down
36 changes: 36 additions & 0 deletions tests/cli/validation/duplicate-data-source-functionality.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
- Load subgraph from subgraph.yaml
⚠ Warnings while loading subgraph from subgraph.yaml: Warnings in subgraph.yaml:

Path: dataSources > 1
This data source, 'ExampleSubgraph1', has identical source and mapping to an already declared data source.
They will both listen to the same triggers, process them with the same mappings, and generate the same entity updates.

✔ Load subgraph from subgraph.yaml
- Load contract ABIs
Load contract ABI from Abi.json
- Load contract ABIs
Load contract ABI from Abi.json
- Load contract ABIs
✔ Load contract ABIs
- Generate types for contract ABIs
Generate types for contract ABI: ExampleContract (Abi.json)
- Generate types for contract ABIs
Write types to generated/ExampleSubgraph/ExampleContract.ts
- Generate types for contract ABIs
Generate types for contract ABI: ExampleContract (Abi.json)
- Generate types for contract ABIs
Write types to generated/ExampleSubgraph1/ExampleContract.ts
- Generate types for contract ABIs
✔ Generate types for contract ABIs
- Generate types for data source templates
✔ Generate types for data source templates
- Load data source template ABIs
✔ Load data source template ABIs
- Generate types for data source template ABIs
✔ Generate types for data source template ABIs
- Load GraphQL schema from schema.graphql
✔ Load GraphQL schema from schema.graphql
- Generate types for GraphQL schema
Write types to generated/schema.ts
- Generate types for GraphQL schema
✔ Generate types for GraphQL schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "event",
"name": "ExampleEvent",
"inputs": [{ "type": "string" }]
}
]
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type MyEntity @entity {
id: ID!
x: BigDecimal!
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
specVersion: 0.0.1
repository: https://github.com/graphprotocol/test-subgraph
description: Test subgraph
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum/contract
name: ExampleSubgraph
source:
abi: ExampleContract
mapping:
kind: ethereum/events
apiVersion: 0.0.1
language: wasm/assemblyscript
file: ./mapping.ts
entities:
- ExampleEntity
abis:
- name: ExampleContract
file: ./Abi.json
eventHandlers:
- event: ExampleEvent(string)
handler: handleExampleEvent
- kind: ethereum/contract
name: ExampleSubgraph1
source:
abi: ExampleContract
mapping:
kind: ethereum/events
apiVersion: 0.0.1
language: wasm/assemblyscript
file: ./mapping.ts
entities:
- ExampleEntity
abis:
- name: ExampleContract
file: ./Abi.json
eventHandlers:
- event: ExampleEvent(string)
handler: handleExampleEvent