Skip to content

Commit b8422ea

Browse files
authored
feat: allow configuring the block batch size for contract subscriptions (#881)
* feat: allow configuring the block batch size for contract subcriptions * fix lint
1 parent c9695b3 commit b8422ea

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/server/routes/contract/subscriptions/remove-contract-subscription.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ export async function removeContractSubscription(fastify: FastifyInstance) {
4343
handler: async (request, reply) => {
4444
const { contractSubscriptionId } = request.body;
4545

46-
const contractSubscription = await deleteContractSubscription(
47-
contractSubscriptionId,
48-
);
46+
await deleteContractSubscription(contractSubscriptionId);
4947

5048
reply.status(StatusCodes.OK).send({
5149
result: {

src/shared/utils/env.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export const env = createEnv({
8686
QUEUE_FAIL_HISTORY_COUNT: z.coerce.number().default(10_000),
8787
// Sets the number of recent nonces to map to queue IDs.
8888
NONCE_MAP_COUNT: z.coerce.number().default(10_000),
89+
// Sets the estimated number of blocks to query per contract subscription job. Defaults to 1 block (real-time).
90+
CONTRACT_SUBSCRIPTION_BLOCK_RANGE: z.coerce.number().default(1),
8991

9092
ENABLE_KEYPAIR_AUTH: boolEnvSchema(false),
9193
ENABLE_CUSTOM_HMAC_AUTH: boolEnvSchema(false),
@@ -136,6 +138,8 @@ export const env = createEnv({
136138
QUEUE_COMPLETE_HISTORY_COUNT: process.env.QUEUE_COMPLETE_HISTORY_COUNT,
137139
QUEUE_FAIL_HISTORY_COUNT: process.env.QUEUE_FAIL_HISTORY_COUNT,
138140
NONCE_MAP_COUNT: process.env.NONCE_MAP_COUNT,
141+
CONTRACT_SUBSCRIPTION_BLOCK_RANGE:
142+
process.env.CONTRACT_SUBSCRIPTION_BLOCK_RANGE,
139143
EXPERIMENTAL__MINE_WORKER_TIMEOUT_SECONDS:
140144
process.env.EXPERIMENTAL__MINE_WORKER_TIMEOUT_SECONDS,
141145
EXPERIMENTAL__MAX_GAS_PRICE_WEI:

src/worker/indexers/chain-indexer-registry.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import cron from "node-cron";
22
import { getBlockTimeSeconds } from "../../shared/utils/indexer/get-block-time";
33
import { logger } from "../../shared/utils/logger";
44
import { handleContractSubscriptions } from "../tasks/chain-indexer";
5+
import { env } from "../../shared/utils/env";
56

67
// @TODO: Move all worker logic to Bullmq to better handle multiple hosts.
78
export const INDEXER_REGISTRY = {} as Record<number, cron.ScheduledTask>;
@@ -24,9 +25,10 @@ export const addChainIndexer = async (chainId: number) => {
2425
});
2526
blockTimeSeconds = 2;
2627
}
27-
const cronSchedule = createScheduleSeconds(
28-
Math.max(Math.round(blockTimeSeconds), 1),
29-
);
28+
const cronSchedule = createScheduleSeconds({
29+
blockTimeSeconds,
30+
numBlocks: env.CONTRACT_SUBSCRIPTION_BLOCK_RANGE,
31+
});
3032
logger({
3133
service: "worker",
3234
level: "info",
@@ -74,5 +76,17 @@ export const removeChainIndexer = async (chainId: number) => {
7476
delete INDEXER_REGISTRY[chainId];
7577
};
7678

77-
export const createScheduleSeconds = (seconds: number) =>
78-
`*/${seconds} * * * * *`;
79+
/**
80+
* Returns the cron schedule given the chain's block time and the number of blocks to batch per job.
81+
* Minimum is every 2 seconds.
82+
*/
83+
function createScheduleSeconds({
84+
blockTimeSeconds,
85+
numBlocks,
86+
}: { blockTimeSeconds: number; numBlocks: number }) {
87+
const pollFrequencySeconds = Math.max(
88+
Math.round(blockTimeSeconds * numBlocks),
89+
2,
90+
);
91+
return `*/${pollFrequencySeconds} * * * * *`;
92+
}

0 commit comments

Comments
 (0)