Skip to content

Commit

Permalink
fix(Finalizer): Dedup transaction hash list when creating list of tra…
Browse files Browse the repository at this point in the history
…nsactions to query for CCTP l2->l1 transactions (#1535)
  • Loading branch information
nicholaspai authored May 19, 2024
1 parent 1f8aed4 commit 13ec536
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/finalizer/utils/arbitrum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function arbitrumOneFinalizer(
// Arbitrum takes 7 days to finalize withdrawals, so don't look up events younger than that.
const redis = await getRedisCache(logger);
const [fromBlock, toBlock] = await Promise.all([
getBlockForTimestamp(chainId, getCurrentTime() - 9 * 60 * 60 * 24, undefined, redis),
getBlockForTimestamp(chainId, getCurrentTime() - 14 * 60 * 60 * 24, undefined, redis),
getBlockForTimestamp(chainId, getCurrentTime() - 7 * 60 * 60 * 24, undefined, redis),
]);
logger.debug({
Expand Down
4 changes: 1 addition & 3 deletions src/finalizer/utils/cctp/l1ToL2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ export async function cctpL1toL2Finalizer(
spokePoolClient: SpokePoolClient,
l1ToL2AddressesToFinalize: string[]
): Promise<FinalizerPromise> {
// Let's just assume for now CCTP transfers don't take longer than 1 day and can
// happen very quickly.
const lookback = getCurrentTime() - 60 * 60 * 24;
const lookback = getCurrentTime() - 60 * 60 * 24 * 7;
const redis = await getRedisCache(logger);
const fromBlock = await getBlockForTimestamp(hubPoolClient.chainId, lookback, undefined, redis);
logger.debug({
Expand Down
27 changes: 19 additions & 8 deletions src/finalizer/utils/cctp/l2ToL1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { CONTRACT_ADDRESSES, Multicall2Call, chainIdsToCctpDomains } from "../..
import {
Contract,
Signer,
TOKEN_SYMBOLS_MAP,
assert,
compareAddressesSimple,
getBlockForTimestamp,
getCurrentTime,
getNetworkName,
Expand All @@ -24,9 +26,7 @@ export async function cctpL2toL1Finalizer(
hubPoolClient: HubPoolClient,
spokePoolClient: SpokePoolClient
): Promise<FinalizerPromise> {
// Let's just assume for now CCTP transfers don't take longer than 1 day and can
// happen very quickly.
const lookback = getCurrentTime() - 60 * 60 * 24;
const lookback = getCurrentTime() - 60 * 60 * 24 * 7;
const redis = await getRedisCache(logger);
const fromBlock = await getBlockForTimestamp(hubPoolClient.chainId, lookback, undefined, redis);
logger.debug({
Expand Down Expand Up @@ -67,14 +67,25 @@ async function resolveRelatedTxnReceipts(
targetDestinationChainId: number,
latestBlockToFinalize: number
): Promise<DecodedCCTPMessage[]> {
const sourceChainId = client.chainId;
// Dedup the txnReceipt list because there might be multiple tokens bridged events in the same txn hash.

const uniqueTxnHashes = new Set<string>();
client
.getTokensBridged()
.filter(
(bridgeEvent) =>
bridgeEvent.blockNumber >= latestBlockToFinalize &&
compareAddressesSimple(bridgeEvent.l2TokenAddress, TOKEN_SYMBOLS_MAP._USDC.addresses[sourceChainId])
)
.forEach((bridgeEvent) => uniqueTxnHashes.add(bridgeEvent.transactionHash));

// Resolve the receipts to all collected txns
const txnReceipts = await Promise.all(
client
.getTokensBridged()
.filter((bridgeEvent) => bridgeEvent.blockNumber >= latestBlockToFinalize)
.map((bridgeEvent) => client.spokePool.provider.getTransactionReceipt(bridgeEvent.transactionHash))
Array.from(uniqueTxnHashes).map((hash) => client.spokePool.provider.getTransactionReceipt(hash))
);
return resolveCCTPRelatedTxns(txnReceipts, client.chainId, targetDestinationChainId);

return resolveCCTPRelatedTxns(txnReceipts, sourceChainId, targetDestinationChainId);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/finalizer/utils/opStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export async function opStackFinalizer(
// - Don't try to withdraw tokens that are not past the 7 day challenge period
const redis = await getRedisCache(logger);
const [earliestBlockToFinalize, latestBlockToProve] = await Promise.all([
getBlockForTimestamp(chainId, getCurrentTime() - 14 * 60 * 60 * 24, undefined, redis),
getBlockForTimestamp(chainId, getCurrentTime() - 7 * 60 * 60 * 24, undefined, redis),
getBlockForTimestamp(chainId, getCurrentTime() - 60 * 60 * 24, undefined, redis),
]);
const { recentTokensBridgedEvents = [], olderTokensBridgedEvents = [] } = groupBy(
spokePoolClient.getTokensBridged(),
Expand Down
2 changes: 1 addition & 1 deletion src/finalizer/utils/polygon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function polygonFinalizer(
const { chainId } = spokePoolClient;

const posClient = await getPosClient(signer);
const lookback = getCurrentTime() - 60 * 60 * 24;
const lookback = getCurrentTime() - 60 * 60 * 24 * 7;
const redis = await getRedisCache(logger);
const fromBlock = await getBlockForTimestamp(chainId, lookback, undefined, redis);

Expand Down
2 changes: 1 addition & 1 deletion src/finalizer/utils/zkSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function zkSyncFinalizer(
// older than 2 days and earlier than 1 day.
const redis = await getRedisCache(logger);
const [fromBlock, toBlock] = await Promise.all([
getBlockForTimestamp(l2ChainId, getCurrentTime() - 2 * 60 * 60 * 24, undefined, redis),
getBlockForTimestamp(l2ChainId, getCurrentTime() - 8 * 60 * 60 * 24, undefined, redis),
getBlockForTimestamp(l2ChainId, getCurrentTime() - 1 * 60 * 60 * 24, undefined, redis),
]);
logger.debug({
Expand Down

0 comments on commit 13ec536

Please sign in to comment.