Skip to content

Commit

Permalink
Add missing filtering for timestamp (#229)
Browse files Browse the repository at this point in the history
* Add missing filtering for timestamp

* Update changelog
  • Loading branch information
stwiname authored Jan 4, 2024
1 parent 5cf71f2 commit 7f2ab54
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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]
### Fixed
- Timestamp filters not working (#229)

## [3.4.4] - 2023-12-20
### Fixed
Expand Down
9 changes: 9 additions & 0 deletions packages/node/src/configure/SubqueryProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ import {
EthereumNetworkConfig,
RuntimeDatasourceTemplate,
CustomDatasourceTemplate,
EthereumBlockFilter,
} from '@subql/types-ethereum';
import { buildSchemaFromString } from '@subql/utils';
import Cron from 'cron-converter';
import { GraphQLSchema } from 'graphql';
import { updateDatasourcesFlare } from '../utils/project';

Expand All @@ -36,6 +38,13 @@ export type EthereumProjectDsTemplate =
| SubqlProjectDs<RuntimeDatasourceTemplate>
| SubqlProjectDs<CustomDatasourceTemplate>;

export type SubqlProjectBlockFilter = EthereumBlockFilter & {
cronSchedule?: {
schedule: Cron.Seeker;
next: number;
};
};

const NOT_SUPPORT = (name: string) => {
throw new Error(`Manifest specVersion ${name} is not supported`);
};
Expand Down
31 changes: 31 additions & 0 deletions packages/node/src/ethereum/block.ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2023 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import { getLogger } from '@subql/node-core';
import {
EthereumBlock,
EthereumTransactionFilter,
Expand All @@ -10,6 +11,7 @@ import {
EthereumTransaction,
LightEthereumLog,
} from '@subql/types-ethereum';
import { SubqlProjectBlockFilter } from '../configure/SubqueryProject';
import { BlockContent } from '../indexer/types';
import {
eventToTopic,
Expand All @@ -18,6 +20,8 @@ import {
stringNormalizedEq,
} from '../utils/string';

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

export function filterBlocksProcessor(
block: EthereumBlock,
filter: EthereumBlockFilter,
Expand All @@ -26,9 +30,36 @@ export function filterBlocksProcessor(
if (filter?.modulo && block.number % filter.modulo !== 0) {
return false;
}
if (filter.timestamp) {
return filterBlockTimestamp(block, filter as SubqlProjectBlockFilter);
}
return true;
}

export function filterBlockTimestamp(
block: EthereumBlock,
filter: SubqlProjectBlockFilter,
): boolean {
const unixTimestamp = Number(block.timestamp) * 1000; // Multiply to add MS
if (unixTimestamp > filter.cronSchedule.next) {
logger.info(
`Block with timestamp ${new Date(
unixTimestamp,
).toString()} is about to be indexed`,
);
logger.info(
`Next block will be indexed at ${new Date(
filter.cronSchedule.next,
).toString()}`,
);
filter.cronSchedule.schedule.prev();
return true;
} else {
filter.cronSchedule.schedule.prev();
return false;
}
}

export function filterTransactionsProcessor(
transaction: EthereumTransaction,
filter: EthereumTransactionFilter,
Expand Down

0 comments on commit 7f2ab54

Please sign in to comment.