Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/template-handler-validation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@graphprotocol/graph-cli": patch
---

Validate template mapping handlers during builds, including when a compiled mapping is reused from another data source or template.
89 changes: 89 additions & 0 deletions packages/cli/src/compiler/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import os from 'node:os';
import path from 'node:path';
import fs from 'fs-extra';
import immutable from 'immutable';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import Protocol from '../protocols/index.js';
import * as asc from './asc.js';
import Compiler from './index.js';

vi.mock('./asc.js', () => ({ ready: vi.fn(), compile: vi.fn() }));

// A valid WASM module exporting a single no-op function named `present`.
const wasm = Buffer.from([
0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 2, 1, 0, 7, 11, 1, 7, 112, 114, 101, 115, 101,
110, 116, 0, 0, 10, 4, 1, 2, 0, 11,
]);

describe('compiled mapping handler validation', () => {
let dir: string;
let compiler: Compiler;

beforeEach(async () => {
dir = await fs.mkdtemp(path.join(os.tmpdir(), 'graph-handlers-'));
await fs.writeFile(path.join(dir, 'mapping.ts'), 'export function present(): void {}');
// Substreams avoids resolving graph-ts for this compiler unit test.
compiler = new Compiler({
ipfs: undefined,
subgraphManifest: path.join(dir, 'subgraph.yaml'),
outputDir: path.join(dir, 'build'),
outputFormat: 'wasm',
skipMigrations: true,
protocol: new Protocol('substreams'),
});
Object.assign(compiler, { protocol: new Protocol('ethereum'), globalsFile: 'global.ts' });
vi.mocked(asc.compile).mockImplementation(({ baseDir, outputFile }) => {
fs.outputFileSync(path.resolve(baseDir, outputFile), wasm);
});
});

afterEach(async () => {
vi.clearAllMocks();
await fs.remove(dir);
});

function mapping(name: string, handler: string) {
return {
name,
mapping: { file: './mapping.ts', eventHandlers: [{ handler, event: 'Event()' }] },
};
}

it.each([false, true])('rejects missing template handlers (cached: %s)', async cached => {
const manifest = immutable.fromJS({
dataSources: cached ? [mapping('Source', 'present')] : [],
templates: [mapping('Template', 'missing')],
});
await expect(compiler.compileSubgraph(manifest, true)).rejects.toThrow(
'Missing handlers in WASM: missing',
);
});

it('checks each data source even when its mapping is cached', async () => {
const manifest = immutable.fromJS({
dataSources: [mapping('First', 'present'), mapping('Second', 'missing')],
});
await expect(compiler.compileSubgraph(manifest, true)).rejects.toThrow(
'Missing handlers in WASM: missing',
);
});

it('reuses valid mappings without compiling them twice', async () => {
const manifest = immutable.fromJS({
dataSources: [mapping('Source', 'present')],
templates: [mapping('Template', 'present')],
});
await compiler.compileSubgraph(manifest, true);
expect(asc.compile).toHaveBeenCalledTimes(1);
});

it('retains the option to skip handler validation', async () => {
await compiler.compileSubgraph(
immutable.fromJS({
dataSources: [],
templates: [mapping('Template', 'missing')],
}),
false,
);
});
});
39 changes: 37 additions & 2 deletions packages/cli/src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,13 @@ export default class Compiler {
? templates
: templates.map((template: any) =>
template.updateIn(['mapping', 'file'], (mappingPath: string) =>
this._compileTemplateMapping(template, mappingPath, compiledFiles, spinner),
this._compileTemplateMapping(
template,
mappingPath,
compiledFiles,
spinner,
validate,
),
),
),
);
Expand Down Expand Up @@ -360,6 +366,15 @@ export default class Compiler {
const alreadyCompiled = compiledFiles.has(inputCacheKey);
if (alreadyCompiled) {
const outFile = compiledFiles.get(inputCacheKey);
if (validate) {
const missingHandlers = this._validateHandlersInWasm({
pathToWasm: outFile,
dataSource,
});
if (missingHandlers.length > 0) {
throw Error(`\n\tMissing handlers in WASM: ${missingHandlers.join(', ')}`);
}
}
step(
spinner,
'Compile data source:',
Expand Down Expand Up @@ -416,10 +431,11 @@ export default class Compiler {
}

_compileTemplateMapping(
template: immutable.Collection<any, any>,
template: immutable.Map<any, any>,
mappingPath: string,
compiledFiles: Map<any, any>,
spinner: Spinner,
validate = false,
) {
try {
const templateName = template.get('name');
Expand All @@ -435,6 +451,15 @@ export default class Compiler {
const alreadyCompiled = compiledFiles.has(inputCacheKey);
if (alreadyCompiled) {
const outFile = compiledFiles.get(inputCacheKey);
if (validate) {
const missingHandlers = this._validateHandlersInWasm({
pathToWasm: outFile,
dataSource: template,
});
if (missingHandlers.length > 0) {
throw Error(`\n\tMissing handlers in WASM: ${missingHandlers.join(', ')}`);
}
}
step(
spinner,
'Compile data source template:',
Expand Down Expand Up @@ -477,6 +502,16 @@ export default class Compiler {
outputFile,
});

if (validate) {
const missingHandlers = this._validateHandlersInWasm({
pathToWasm: outFile,
dataSource: template,
});
if (missingHandlers.length > 0) {
throw Error(`\n\tMissing handlers in WASM: ${missingHandlers.join(', ')}`);
}
}

// Remember the output file to avoid compiling the same file again
compiledFiles.set(inputCacheKey, outFile);

Expand Down