Skip to content
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

improve(LineaFinalizer): Force RPC calls through custom provider #1931

Merged
merged 20 commits into from
Dec 17, 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
4 changes: 4 additions & 0 deletions src/finalizer/utils/arbStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export async function arbStackFinalizer(
undefined,
redis
);
const l2BlockTime = (await averageBlockTime(spokePoolClient.spokePool.provider)).average;
nicholaspai marked this conversation as resolved.
Show resolved Hide resolved
logger.debug({
at: `Finalizer#${networkName}Finalizer`,
message: `${networkName} TokensBridged event filter`,
Expand Down Expand Up @@ -209,6 +210,9 @@ export async function arbStackFinalizer(
logger.debug({
at: `Finalizer#${networkName}Finalizer`,
message: `Withdrawal event for ${amountFromWei} of ${l1TokenInfo.symbol} is too recent to finalize`,
timeUntilFinalization: `${Math.floor(
((event.blockNumber - latestBlockToFinalize) * l2BlockTime) / 60 / 60
)}`,
});
}
} catch (err) {
Expand Down
108 changes: 90 additions & 18 deletions src/finalizer/utils/linea/common.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { LineaSDK, Message, OnChainMessageStatus } from "@consensys/linea-sdk";
import { L1MessageServiceContract, L2MessageServiceContract } from "@consensys/linea-sdk/dist/lib/contracts";
import { L1ClaimingService } from "@consensys/linea-sdk/dist/lib/sdk/claiming/L1ClaimingService";
import { MessageSentEvent } from "@consensys/linea-sdk/dist/typechain/L2MessageService";
import { Linea_Adapter__factory } from "@across-protocol/contracts";
import {
BigNumber,
Expand All @@ -16,11 +13,12 @@ import {
getNodeUrlList,
getRedisCache,
paginatedEventQuery,
retryAsync,
CHAIN_IDs,
} from "../../../utils";
import { HubPoolClient } from "../../../clients";
import { CONTRACT_ADDRESSES } from "../../../common";
import { Log } from "../../../interfaces";
import { L1ClaimingService, L1MessageServiceContract, L2MessageServiceContract, MessageSentEvent } from "./imports";

export type MessageWithStatus = Message & {
logIndex: number;
Expand All @@ -40,8 +38,12 @@ export function initLineaSdk(l1ChainId: number, l2ChainId: number): LineaSDK {
}

export function makeGetMessagesWithStatusByTxHash(
srcMessageService: L1MessageServiceContract | L2MessageServiceContract,
dstClaimingService: L1ClaimingService | L2MessageServiceContract
l2Provider: ethers.providers.Provider,
l1Provider: ethers.providers.Provider,
l2MessageService: L2MessageServiceContract,
l1ClaimingService: L1ClaimingService,
l1SearchConfig: EventSearchConfig,
l2SearchConfig: EventSearchConfig
) {
/**
* Retrieves Linea's MessageSent events for a given transaction hash and enhances them with their status.
Expand All @@ -51,18 +53,16 @@ export function makeGetMessagesWithStatusByTxHash(
*/
return async (txHashOrReceipt: string | TransactionReceipt): Promise<MessageWithStatus[]> => {
const txReceipt =
typeof txHashOrReceipt === "string"
? await srcMessageService.provider.getTransactionReceipt(txHashOrReceipt)
: txHashOrReceipt;
typeof txHashOrReceipt === "string" ? await l2Provider.getTransactionReceipt(txHashOrReceipt) : txHashOrReceipt;

if (!txReceipt) {
return [];
}

const messages = txReceipt.logs
.filter((log) => log.address === srcMessageService.contract.address)
.filter((log) => log.address === l2MessageService.contract.address)
.flatMap((log) => {
const parsedLog = srcMessageService.contract.interface.parseLog(log);
const parsedLog = l2MessageService.contract.interface.parseLog(log);

if (!parsedLog || parsedLog.name !== "MessageSent") {
return [];
Expand All @@ -83,11 +83,17 @@ export function makeGetMessagesWithStatusByTxHash(
logIndex: log.logIndex,
};
});

// The Linea SDK MessageServiceContract constructs its own Provider without our retry logic so we retry each call
// twice with a 1 second delay between in case of intermittent RPC failures.
const messageStatus = await Promise.all(
messages.map((message) => retryAsync(() => dstClaimingService.getMessageStatus(message.messageHash), 2, 1))
messages.map((message) =>
getL2L1MessageStatusUsingCustomProvider(
l1ClaimingService,
message.messageHash,
l1Provider,
l1SearchConfig,
l2Provider,
l2SearchConfig
)
)
);
return messages.map((message, index) => ({
...message,
Expand All @@ -96,6 +102,72 @@ export function makeGetMessagesWithStatusByTxHash(
};
}

// Temporary re-implementation of the SDK's `L1ClaimingService.getMessageStatus` functions that allow us to use
nicholaspai marked this conversation as resolved.
Show resolved Hide resolved
// our custom provider, with retry and caching logic, to get around the SDK's hardcoded logic to query events
// from 0 to "latest" which will not work on all RPC's.
async function getL2L1MessageStatusUsingCustomProvider(
messageService: L1ClaimingService,
messageHash: string,
l1Provider: ethers.providers.Provider,
l1SearchConfig: EventSearchConfig,
l2Provider: ethers.providers.Provider,
l2SearchConfig: EventSearchConfig
): Promise<OnChainMessageStatus> {
const l2Contract = getL2MessageServiceContractFromL1ClaimingService(messageService, l2Provider);
const messageEvent = await getMessageSentEventForMessageHash(messageHash, l2Contract, l2SearchConfig);
const l1Contract = getL1MessageServiceContractFromL1ClaimingService(messageService, l1Provider);
const [l2MessagingBlockAnchoredEvents, isMessageClaimed] = await Promise.all([
getL2MessagingBlockAnchoredFromMessageSentEvent(messageEvent, l1Contract, l1SearchConfig),
l1Contract.isMessageClaimed(messageEvent.args?._nonce),
]);
if (isMessageClaimed) {
return OnChainMessageStatus.CLAIMED;
}
if (l2MessagingBlockAnchoredEvents.length > 0) {
return OnChainMessageStatus.CLAIMABLE;
}
return OnChainMessageStatus.UNKNOWN;
}
export function getL2MessageServiceContractFromL1ClaimingService(
l1ClaimingService: L1ClaimingService,
l2Provider: ethers.providers.Provider
): Contract {
return l1ClaimingService.l2Contract.contract.connect(l2Provider);
}
export function getL1MessageServiceContractFromL1ClaimingService(
l1ClaimingService: L1ClaimingService,
l1Provider: ethers.providers.Provider
): Contract {
return l1ClaimingService.l1Contract.contract.connect(l1Provider);
}
export async function getMessageSentEventForMessageHash(
messageHash: string,
l2MessageServiceContract: Contract,
l2SearchConfig: EventSearchConfig
): Promise<Log> {
const [messageEvent] = await paginatedEventQuery(
l2MessageServiceContract,
l2MessageServiceContract.filters.MessageSent(null, null, null, null, null, null, messageHash),
l2SearchConfig
);
if (!messageEvent) {
throw new Error(`Message hash does not exist on L2. Message hash: ${messageHash}`);
}
return messageEvent;
}
export async function getL2MessagingBlockAnchoredFromMessageSentEvent(
messageSentEvent: Log,
l1MessageServiceContract: Contract,
l1SearchConfig: EventSearchConfig
): Promise<Log[]> {
const l2MessagingBlockAnchoredEvents = await paginatedEventQuery(
l1MessageServiceContract,
l1MessageServiceContract.filters.L2MessagingBlockAnchored(messageSentEvent.blockNumber),
l1SearchConfig
);
return l2MessagingBlockAnchoredEvents;
}

export async function getBlockRangeByHoursOffsets(
chainId: number,
fromBlockHoursOffsetToNow: number,
Expand Down Expand Up @@ -188,13 +260,13 @@ export function determineMessageType(
}

export async function findMessageSentEvents(
contract: L1MessageServiceContract | L2MessageServiceContract,
contract: Contract,
l1ToL2AddressesToFinalize: string[],
searchConfig: EventSearchConfig
): Promise<MessageSentEvent[]> {
return paginatedEventQuery(
contract.contract,
(contract.contract as Contract).filters.MessageSent(l1ToL2AddressesToFinalize, l1ToL2AddressesToFinalize),
contract,
contract.filters.MessageSent(l1ToL2AddressesToFinalize, l1ToL2AddressesToFinalize),
searchConfig
) as Promise<MessageSentEvent[]>;
}
Expand Down
23 changes: 23 additions & 0 deletions src/finalizer/utils/linea/imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Normally we avoid importing directly from a node_modules' /dist package but we need access to some
// of the internal classes and functions in order to replicate SDK logic so that we can by pass hardcoded
// ethers.Provider instances and use our own custom provider instead.
import { L1MessageServiceContract, L2MessageServiceContract } from "@consensys/linea-sdk/dist/lib/contracts";
import { L1ClaimingService } from "@consensys/linea-sdk/dist/lib/sdk/claiming/L1ClaimingService";
import { MessageSentEvent } from "@consensys/linea-sdk/dist/typechain/L2MessageService";
import { SparseMerkleTreeFactory } from "@consensys/linea-sdk/dist/lib/sdk/merkleTree/MerkleTreeFactory";
import {
DEFAULT_L2_MESSAGE_TREE_DEPTH,
L2_MERKLE_TREE_ADDED_EVENT_SIGNATURE,
L2_MESSAGING_BLOCK_ANCHORED_EVENT_SIGNATURE,
} from "@consensys/linea-sdk/dist/lib/utils/constants";

export {
L1ClaimingService,
L1MessageServiceContract,
L2MessageServiceContract,
MessageSentEvent,
SparseMerkleTreeFactory,
DEFAULT_L2_MESSAGE_TREE_DEPTH,
L2_MERKLE_TREE_ADDED_EVENT_SIGNATURE,
L2_MESSAGING_BLOCK_ANCHORED_EVENT_SIGNATURE,
};
36 changes: 31 additions & 5 deletions src/finalizer/utils/linea/l1ToL2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,36 @@ import { Contract } from "ethers";
import { groupBy } from "lodash";
import { HubPoolClient, SpokePoolClient } from "../../../clients";
import { CHAIN_MAX_BLOCK_LOOKBACK, CONTRACT_ADDRESSES } from "../../../common";
import { EventSearchConfig, Signer, convertFromWei, retryAsync, winston, CHAIN_IDs } from "../../../utils";
import { EventSearchConfig, Signer, convertFromWei, winston, CHAIN_IDs, ethers, BigNumber } from "../../../utils";
import { CrossChainMessage, FinalizerPromise } from "../../types";
import {
determineMessageType,
findMessageFromTokenBridge,
findMessageFromUsdcBridge,
findMessageSentEvents,
getBlockRangeByHoursOffsets,
getL1MessageServiceContractFromL1ClaimingService,
initLineaSdk,
} from "./common";
import { L2MessageServiceContract } from "./imports";

const L1L2MessageStatuses = {
0: "UNKNOWN",
1: "CLAIMABLE",
2: "CLAIMED",
};
// Temporary re-implementation of the SDK's `L2MessageServiceContract.getMessageStatus` functions that allow us to use
nicholaspai marked this conversation as resolved.
Show resolved Hide resolved
// our custom provider, with retry and caching logic, to get around the SDK's hardcoded logic to query events
// from 0 to "latest" which will not work on all RPC's.
async function getL1ToL2MessageStatusUsingCustomProvider(
messageService: L2MessageServiceContract,
messageHash: string,
l2Provider: ethers.providers.Provider
): Promise<OnChainMessageStatus> {
const l2Contract = messageService.contract.connect(l2Provider);
const status: BigNumber = await l2Contract.inboxL1L2MessageStatus(messageHash);
return L1L2MessageStatuses[status.toString()];
}

export async function lineaL1ToL2Finalizer(
logger: winston.Logger,
Expand Down Expand Up @@ -58,7 +78,11 @@ export async function lineaL1ToL2Finalizer(
};

const [wethAndRelayEvents, tokenBridgeEvents, usdcBridgeEvents] = await Promise.all([
findMessageSentEvents(l1MessageServiceContract, l1ToL2AddressesToFinalize, searchConfig),
findMessageSentEvents(
getL1MessageServiceContractFromL1ClaimingService(lineaSdk.getL1ClaimingService(), hubPoolClient.hubPool.provider),
l1ToL2AddressesToFinalize,
searchConfig
),
findMessageFromTokenBridge(l1TokenBridge, l1MessageServiceContract, l1ToL2AddressesToFinalize, searchConfig),
findMessageFromUsdcBridge(l1UsdcBridge, l1MessageServiceContract, l1ToL2AddressesToFinalize, searchConfig),
]);
Expand All @@ -73,9 +97,11 @@ export async function lineaL1ToL2Finalizer(
// It's unlikely that our multicall will have multiple transactions to bridge to Linea
// so we can grab the statuses individually.

// The Linea SDK MessageServiceContract constructs its own Provider without our retry logic so we retry each call
// twice with a 1 second delay between in case of intermittent RPC failures.
const messageStatus = await retryAsync(() => l2MessageServiceContract.getMessageStatus(_messageHash), 2, 1);
const messageStatus = await getL1ToL2MessageStatusUsingCustomProvider(
l2MessageServiceContract,
_messageHash,
_spokePoolClient.spokePool.provider
);
return {
messageSender: _from,
destination: _to,
Expand Down
Loading
Loading