Skip to content

Commit

Permalink
feat: Allow 'startingBlock' to be a negative offset (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsanmigimeno authored May 30, 2024
1 parent 10e2106 commit 7855a64
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
15 changes: 12 additions & 3 deletions src/collector/mock/mock.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,18 @@ class MockCollectorWorker {
// Do not initialize 'fromBlock' whilst 'currentStatus' is null, even if
// 'startingBlock' is specified.
if (this.currentStatus != null) {
fromBlock = (
this.config.startingBlock ?? this.currentStatus.blockNumber
);
if (this.config.startingBlock != null) {
if (this.config.startingBlock < 0) {
fromBlock = this.currentStatus.blockNumber + this.config.startingBlock;
if (fromBlock < 0) {
throw new Error(`Invalid 'startingBlock': negative offset is larger than the current block number.`)
}
} else {
fromBlock = this.config.startingBlock;
}
} else {
fromBlock = this.currentStatus.blockNumber;
}
}

await wait(this.config.processingInterval);
Expand Down
15 changes: 12 additions & 3 deletions src/collector/polymer/polymer.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,18 @@ class PolymerCollectorSnifferWorker {
// Do not initialize 'fromBlock' whilst 'currentStatus' is null, even if
// 'startingBlock' is specified.
if (this.currentStatus != null) {
fromBlock = (
this.config.startingBlock ?? this.currentStatus.blockNumber
);
if (this.config.startingBlock != null) {
if (this.config.startingBlock < 0) {
fromBlock = this.currentStatus.blockNumber + this.config.startingBlock;
if (fromBlock < 0) {
throw new Error(`Invalid 'startingBlock': negative offset is larger than the current block number.`)
}
} else {
fromBlock = this.config.startingBlock;
}
} else {
fromBlock = this.currentStatus.blockNumber;
}
}

await wait(this.config.processingInterval);
Expand Down
15 changes: 12 additions & 3 deletions src/collector/wormhole/wormhole-message-sniffer.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,18 @@ class WormholeMessageSnifferWorker {
// Do not initialize 'fromBlock' whilst 'currentStatus' is null, even if
// 'startingBlock' is specified.
if (this.currentStatus != null) {
fromBlock = (
this.config.startingBlock ?? this.currentStatus.blockNumber
);
if (this.config.startingBlock != null) {
if (this.config.startingBlock < 0) {
fromBlock = this.currentStatus.blockNumber + this.config.startingBlock;
if (fromBlock < 0) {
throw new Error(`Invalid 'startingBlock': negative offset is larger than the current block number.`)
}
} else {
fromBlock = this.config.startingBlock;
}
} else {
fromBlock = this.currentStatus.blockNumber;
}
}

await wait(this.config.processingInterval);
Expand Down
9 changes: 8 additions & 1 deletion src/config/config.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const POSITIVE_NUMBER_SCHEMA = {
minimum: 0,
}

const INTEGER_SCHEMA = {
$id: "integer-schema",
type: "number",
multipleOf: 1,
}

const POSITIVE_NON_ZERO_INTEGER_SCHEMA = {
$id: "positive-non-zero-integer-schema",
type: "number",
Expand Down Expand Up @@ -212,7 +218,7 @@ const CHAINS_SCHEMA = {
rpc: { $ref: "non-empty-string-schema" },
resolver: { $ref: "non-empty-string-schema" },

startingBlock: { $ref: "positive-number-schema" },
startingBlock: { $ref: "integer-schema" },
stoppingBlock: { $ref: "positive-number-schema" },

monitor: { $ref: "monitor-schema" },
Expand All @@ -229,6 +235,7 @@ const CHAINS_SCHEMA = {
export function getConfigValidator(): AnyValidateFunction<unknown> {
const ajv = new Ajv({ strict: true });
ajv.addSchema(POSITIVE_NUMBER_SCHEMA);
ajv.addSchema(INTEGER_SCHEMA);
ajv.addSchema(POSITIVE_NON_ZERO_INTEGER_SCHEMA);
ajv.addSchema(NON_EMPTY_STRING_SCHEMA);
ajv.addSchema(ADDRESS_FIELD_SCHEMA);
Expand Down
15 changes: 12 additions & 3 deletions src/getter/getter.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,18 @@ class GetterWorker {
// Do not initialize 'fromBlock' whilst 'currentStatus' is null, even if
// 'startingBlock' is specified.
if (this.currentStatus != null) {
fromBlock = (
this.config.startingBlock ?? this.currentStatus.blockNumber
);
if (this.config.startingBlock != null) {
if (this.config.startingBlock < 0) {
fromBlock = this.currentStatus.blockNumber + this.config.startingBlock;
if (fromBlock < 0) {
throw new Error(`Invalid 'startingBlock': negative offset is larger than the current block number.`)
}
} else {
fromBlock = this.config.startingBlock;
}
} else {
fromBlock = this.currentStatus.blockNumber;
}
}

await wait(this.config.processingInterval);
Expand Down

0 comments on commit 7855a64

Please sign in to comment.