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

Fix filter validation with not null filter #341

Merged
merged 2 commits into from
Aug 26, 2024
Merged
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
2 changes: 2 additions & 0 deletions packages/common-ethereum/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Abi validation not working with `!null` filter (#341)

## [4.5.1] - 2024-08-16
### Changed
Expand Down
42 changes: 42 additions & 0 deletions packages/common-ethereum/src/codegen/codegen-controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ejs from 'ejs';
import {upperFirst} from 'lodash';
import rimraf from 'rimraf';
import {NOT_NULL_FILTER} from '../project/utils';
import {
AbiInterface,
generateAbis,
Expand Down Expand Up @@ -278,7 +279,7 @@
};

await expect(
generateAbis([ds], PROJECT_PATH, undefined as any, undefined as any, undefined as any)

Check warning on line 282 in packages/common-ethereum/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type

Check warning on line 282 in packages/common-ethereum/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type

Check warning on line 282 in packages/common-ethereum/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
).rejects.toThrow(/Asset: "erc20" not found in project/);
});

Expand Down Expand Up @@ -313,7 +314,7 @@
};

await expect(
generateAbis([ds], PROJECT_PATH, undefined as any, undefined as any, undefined as any)

Check warning on line 317 in packages/common-ethereum/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type

Check warning on line 317 in packages/common-ethereum/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type

Check warning on line 317 in packages/common-ethereum/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
).rejects.toThrow(/Function: "approve222\(address a,uint256 b\)" not found in erc20 contract interface/);
});

Expand Down Expand Up @@ -348,10 +349,51 @@
};

await expect(
generateAbis([ds], PROJECT_PATH, undefined as any, undefined as any, undefined as any)

Check warning on line 352 in packages/common-ethereum/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type

Check warning on line 352 in packages/common-ethereum/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type

Check warning on line 352 in packages/common-ethereum/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

Unexpected any. Specify a different type
).rejects.toThrow(/Topic: "NotExist\(address a\)" not found in erc20 contract interface/);
});

it('validates abi with !null filter', async () => {
const ds: SubqlRuntimeDatasource = {
kind: EthereumDatasourceKind.Runtime,
startBlock: 1,
options: {
abi: 'erc20',
address: '',
},
assets: new Map([['erc20', {file: './abis/erc20.json'}]]),
mapping: {
file: '',
handlers: [
{
handler: 'handleTransaction',
kind: EthereumHandlerKind.Event,
filter: {
topics: ['Transfer(address a,address b,uint256 c)', undefined, undefined, NOT_NULL_FILTER],
},
},
{
handler: 'handleTransaction',
kind: EthereumHandlerKind.Event,
filter: {
topics: ['Transfer(address a,address b,uint256 c)', undefined, undefined, null],
},
},
],
},
};

await expect(
generateAbis(
[ds],
PROJECT_PATH,
(p) => Promise.resolve(),

Check warning on line 390 in packages/common-ethereum/src/codegen/codegen-controller.spec.ts

View workflow job for this annotation

GitHub Actions / code-style

'p' is defined but never used
(v) => v as any,
() => Promise.resolve()
)
).resolves.not.toThrow();
});

it('doesnt validate if datasource has no abi option set', async () => {
const ds: SubqlRuntimeDatasource = {
kind: EthereumDatasourceKind.Runtime,
Expand Down
4 changes: 2 additions & 2 deletions packages/common-ethereum/src/codegen/codegen-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {FileReference} from '@subql/types-core';
import {EthereumHandlerKind, SubqlRuntimeDatasource} from '@subql/types-ethereum';
import {Data} from 'ejs';
import {runTypeChain, glob, parseContractPath} from 'typechain';
import {isCustomDs, isRuntimeDs} from '../project';
import {isCustomDs, isRuntimeDs, NOT_NULL_FILTER} from '../project';
import {CUSTOM_EVM_HANDLERS} from './constants';
import {loadReadAbi} from './utils';

Expand Down Expand Up @@ -81,7 +81,7 @@ function validateAbi(datasources: SubqlRuntimeDatasource[], projectPath: string)
if (!mappingHandler.filter.topics || !mappingHandler.filter.topics.length) continue;

const notMatch = mappingHandler.filter.topics.find(
(topic) => topic && !abiEvents.includes(EventFragment.fromString(topic).format())
(topic) => topic && topic !== NOT_NULL_FILTER && !abiEvents.includes(EventFragment.fromString(topic).format())
);

if (notMatch) topicIssues.push(notMatch);
Expand Down
2 changes: 2 additions & 0 deletions packages/common-ethereum/src/project/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {Interface} from 'ethers/lib/utils';
// Todo, this aligns with cli/src/generate-controller, but we should move this to common in next version
export const DEFAULT_ABI_DIR = '/abis';

export const NOT_NULL_FILTER = '!null';

type DefaultFilter = Record<string, unknown>;

export function isBlockHandlerProcessor<E>(
Expand Down
2 changes: 2 additions & 0 deletions packages/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- Use constant for filter `!null` value (#341)

## [5.1.1] - 2024-08-16
### Fixed
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/ethereum/api.ethereum.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import path from 'path';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { NOT_NULL_FILTER } from '@subql/common-ethereum';
import {
EthereumBlock,
EthereumDatasourceKind,
Expand Down Expand Up @@ -159,7 +160,7 @@ describe('Api.ethereum', () => {
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
undefined,
undefined,
'!null',
NOT_NULL_FILTER,
],
};

Expand Down
7 changes: 3 additions & 4 deletions packages/node/src/ethereum/block.ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import { filterBlockTimestamp, getLogger } from '@subql/node-core';
import { NOT_NULL_FILTER } from '@subql/common-ethereum';
import { filterBlockTimestamp } from '@subql/node-core';
import {
EthereumBlock,
EthereumTransactionFilter,
Expand All @@ -20,8 +21,6 @@ import {
stringNormalizedEq,
} from '../utils/string';

const logger = getLogger('block.ethereum');

export function filterBlocksProcessor(
block: EthereumBlock,
filter: EthereumBlockFilter,
Expand Down Expand Up @@ -105,7 +104,7 @@ export function filterLogsProcessor(
return false;
}

if (topic === '!null') {
if (topic === NOT_NULL_FILTER) {
return true;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/indexer/dictionary/v1/ethDictionaryV1.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import { NOT_NULL_FILTER } from '@subql/common-ethereum';
import { NodeConfig, DictionaryV1, getLogger } from '@subql/node-core';
import {
DictionaryQueryCondition,
Expand Down Expand Up @@ -81,7 +82,7 @@ function eventFilterToQueryEntry(
}
const field = `topics${i}`;

if (topic === '!null') {
if (topic === NOT_NULL_FILTER) {
conditions.push({
field,
value: false as any, // TODO update types to allow boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2024 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import { NOT_NULL_FILTER } from '@subql/common-ethereum';
import {
BlockHeightMap,
DictionaryResponse,
Expand Down Expand Up @@ -235,7 +236,7 @@ describe('eth dictionary v2', () => {
'Transfer(address, address, uint256)',
undefined,
undefined,
'!null',
NOT_NULL_FILTER,
],
},
},
Expand Down Expand Up @@ -339,7 +340,7 @@ describe('buildDictionaryV2QueryEntry', () => {
'Transfer(address, address, uint256)',
undefined,
undefined,
'!null',
NOT_NULL_FILTER,
],
},
},
Expand Down
3 changes: 2 additions & 1 deletion packages/node/src/indexer/dictionary/v2/ethDictionaryV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0

import { BigNumber } from '@ethersproject/bignumber';
import { NOT_NULL_FILTER } from '@subql/common-ethereum';
import {
NodeConfig,
DictionaryV2,
Expand Down Expand Up @@ -136,7 +137,7 @@ function eventFilterToDictionaryCondition(
if (!logConditions[field]) {
logConditions[field] = [];
}
if (topic === '!null') {
if (topic === NOT_NULL_FILTER) {
logConditions[field] = []; // TODO, check if !null
} else {
logConditions[field].push(eventToTopic(topic));
Expand Down
Loading