Skip to content

Commit

Permalink
improve(indexer): add block timestamp to depositevent data (#129)
Browse files Browse the repository at this point in the history
Signed-off-by: david <[email protected]>
  • Loading branch information
daywiss authored Dec 16, 2024
1 parent b60b955 commit 9e39da3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,7 @@ export class V3FundsDeposited {

@CreateDateColumn()
createdAt: Date;

@Column({ nullable: true })
blockTimestamp?: number;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class V3FundsDeposited1733407862578 implements MigrationInterface {
name = "V3FundsDeposited1733407862578";

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "evm"."v3_funds_deposited" ADD "blockTimestamp" integer`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "evm"."v3_funds_deposited" DROP COLUMN "blockTimestamp"`,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type FetchEventsResult = {
relayedRootBundleEvents: across.interfaces.RootBundleRelayWithBlock[];
executedRelayerRefundRootEvents: across.interfaces.RelayerRefundExecutionWithBlock[];
tokensBridgedEvents: across.interfaces.TokensBridged[];
blockTimes: Record<number, number>;
};

export type StoreEventsResult = {
Expand Down Expand Up @@ -114,6 +115,26 @@ export class SpokePoolIndexerDataHandler implements IndexerDataHandler {
await this.updateNewDepositsWithIntegratorId(newInsertedDeposits);
await this.spokePoolProcessor.process(storedEvents);
}
private async getBlockTime(blockNumber: number): Promise<number> {
const block = await this.provider.getBlock(blockNumber);
if (!block) {
throw new Error(`Block with number ${blockNumber} not found`);
}
return block.timestamp;
}

private async getBlockTimes(
blockNumbers: number[],
): Promise<Record<number, number>> {
const blockTimes: Record<number, number> = {};
for (const blockNumber of blockNumbers) {
// ensure we dont query more than we need to
if (blockTimes[blockNumber] === undefined) {
blockTimes[blockNumber] = await this.getBlockTime(blockNumber);
}
}
return blockTimes;
}

private async fetchEventsByRange(
blockRange: BlockRange,
Expand Down Expand Up @@ -147,6 +168,13 @@ export class SpokePoolIndexerDataHandler implements IndexerDataHandler {
const executedRelayerRefundRootEvents =
spokePoolClient.getRelayerRefundExecutions();
const tokensBridgedEvents = spokePoolClient.getTokensBridged();
// It may be the case we add more events in the future, but right now we just
// care about deposit event block ranges, we can have dupe block numbers here,
// but getBlockTimes will make sure we dont query more than we need to.
const v3FundsDepositedBlockNumbers = v3FundsDepositedEvents.map(
(deposit) => deposit.blockNumber,
);
const blockTimes = await this.getBlockTimes(v3FundsDepositedBlockNumbers);

return {
v3FundsDepositedEvents,
Expand All @@ -156,6 +184,7 @@ export class SpokePoolIndexerDataHandler implements IndexerDataHandler {
relayedRootBundleEvents,
executedRelayerRefundRootEvents,
tokensBridgedEvents,
blockTimes,
};
}

Expand All @@ -172,11 +201,13 @@ export class SpokePoolIndexerDataHandler implements IndexerDataHandler {
relayedRootBundleEvents,
executedRelayerRefundRootEvents,
tokensBridgedEvents,
blockTimes,
} = params;
const savedV3FundsDepositedEvents =
await spokePoolClientRepository.formatAndSaveV3FundsDepositedEvents(
v3FundsDepositedEvents,
lastFinalisedBlock,
blockTimes,
);
const savedV3RequestedSlowFills =
await spokePoolClientRepository.formatAndSaveRequestedV3SlowFillEvents(
Expand Down
20 changes: 19 additions & 1 deletion packages/indexer/src/database/SpokePoolRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class SpokePoolRepository extends dbUtils.BlockchainEventRepository {
public async formatAndSaveV3FundsDepositedEvents(
v3FundsDepositedEvents: utils.V3FundsDepositedWithIntegradorId[],
lastFinalisedBlock: number,
blockTimes: Record<number, number>,
) {
const formattedEvents = v3FundsDepositedEvents.map((event) => {
// delete fields that are not needed for the database table
Expand All @@ -52,6 +53,7 @@ export class SpokePoolRepository extends dbUtils.BlockchainEventRepository {
...this.formatRelayData(event),
quoteTimestamp: new Date(event.quoteTimestamp * 1000),
finalised: event.blockNumber <= lastFinalisedBlock,
blockTimestamp: blockTimes[event.blockNumber],
};
});
const chunkedEvents = across.utils.chunk(formattedEvents, this.chunkSize);
Expand All @@ -65,7 +67,23 @@ export class SpokePoolRepository extends dbUtils.BlockchainEventRepository {
),
),
);
return savedEvents.flat();
const result = savedEvents.flat();

// Log the time difference for each deposit event for profiling in datadog
const now = Date.now();
formattedEvents.forEach((event) => {
if (event.blockTimestamp === undefined) return;
const timeDifference = now - event.blockTimestamp * 1000;
this.logger.info({
message: "V3FundsDepositedEvent profile",
depositId: event.depositId,
chainId: event.originChainId,
timeDifference,
now,
blockTimestamp: event.blockTimestamp,
});
});
return result;
}

public async formatAndSaveFilledV3RelayEvents(
Expand Down

0 comments on commit 9e39da3

Please sign in to comment.