Skip to content

feat: Add subscription delay envvar #513

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

Merged
merged 2 commits into from
May 2, 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
6 changes: 6 additions & 0 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export const env = createEnv({
SDK_BATCH_TIME_LIMIT: z.coerce.number().default(0),
SDK_BATCH_SIZE_LIMIT: z.coerce.number().default(100),
ENABLE_KEYPAIR_AUTH: boolSchema("false"),
CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS: z.coerce
.number()
.nonnegative()
.default(0),
},
clientPrefix: "NEVER_USED",
client: {},
Expand All @@ -88,6 +92,8 @@ export const env = createEnv({
SDK_BATCH_TIME_LIMIT: process.env.SDK_BATCH_TIME_LIMIT,
SDK_BATCH_SIZE_LIMIT: process.env.SDK_BATCH_SIZE_LIMIT,
ENABLE_KEYPAIR_AUTH: process.env.ENABLE_KEYPAIR_AUTH,
CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS:
process.env.CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS,
},
onValidationError: (error: ZodError) => {
console.error(
Expand Down
15 changes: 14 additions & 1 deletion src/worker/indexers/chainIndexerRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import cron from "node-cron";
import { getConfig } from "../../utils/cache/getConfig";
import { env } from "../../utils/env";
import { getBlockTimeSeconds } from "../../utils/indexer/getBlockTime";
import { logger } from "../../utils/logger";
import { createChainIndexerTask } from "../tasks/chainIndexer";
Expand All @@ -18,6 +19,8 @@ export const addChainIndexer = async (chainId: number) => {

let processStarted = false;
const config = await getConfig();

// Estimate block time.
const blockTimeSeconds = await getBlockTimeSeconds(chainId);

const blocksIn5Seconds = Math.round((1 / blockTimeSeconds) * 5);
Expand All @@ -26,7 +29,17 @@ export const addChainIndexer = async (chainId: number) => {
blocksIn5Seconds * 4,
);

const handler = await createChainIndexerTask(chainId, maxBlocksToIndex);
// Compute block offset based on delay.
// Example: 10s delay with a 3s block time = 4 blocks offset
const toBlockOffset = env.CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS
? Math.ceil(env.CONTRACT_SUBSCRIPTIONS_DELAY_SECONDS / blockTimeSeconds)
: 0;

const handler = await createChainIndexerTask({
chainId,
maxBlocksToIndex,
toBlockOffset,
});

const cronSchedule = createScheduleSeconds(
Math.max(Math.round(blockTimeSeconds), 1),
Expand Down
22 changes: 13 additions & 9 deletions src/worker/tasks/chainIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,13 @@ const indexTransactionReceipts = async ({
}
};

export const createChainIndexerTask = async (
chainId: number,
maxBlocksToIndex: number,
) => {
export const createChainIndexerTask = async (args: {
chainId: number;
maxBlocksToIndex: number;
toBlockOffset: number;
}) => {
const { chainId, maxBlocksToIndex, toBlockOffset } = args;

const chainIndexerTask = async () => {
try {
await prisma.$transaction(
Expand All @@ -287,7 +290,8 @@ export const createChainIndexerTask = async (
const sdk = await getSdk({ chainId });

const provider = sdk.getProvider();
const currentBlockNumber = await provider.getBlockNumber();
const currentBlockNumber =
(await provider.getBlockNumber()) - toBlockOffset;

// check if up-to-date
if (lastIndexedBlock >= currentBlockNumber) {
Expand All @@ -296,8 +300,8 @@ export const createChainIndexerTask = async (

// limit max block numbers
let toBlockNumber = currentBlockNumber;
if (currentBlockNumber - (lastIndexedBlock + 1) > maxBlocksToIndex) {
toBlockNumber = lastIndexedBlock + 1 + maxBlocksToIndex;
if (currentBlockNumber - lastIndexedBlock > maxBlocksToIndex) {
toBlockNumber = lastIndexedBlock + maxBlocksToIndex;
}

const subscribedContracts = await getContractSubscriptionsByChainId(
Expand All @@ -315,14 +319,14 @@ export const createChainIndexerTask = async (
indexContractEvents({
pgtx,
chainId,
fromBlockNumber: lastIndexedBlock + 1,
fromBlockNumber: lastIndexedBlock,
toBlockNumber,
subscribedContractAddresses,
}),
indexTransactionReceipts({
pgtx,
chainId,
fromBlockNumber: lastIndexedBlock + 1,
fromBlockNumber: lastIndexedBlock,
toBlockNumber,
subscribedContractAddresses,
}),
Expand Down
Loading