-
Notifications
You must be signed in to change notification settings - Fork 52
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
base: master
Are you sure you want to change the base?
Changes from all commits
91fae57
bad866e
64bcb86
2561c1a
5f1df9a
bec9101
b8311c1
de12585
79536e3
15498b4
c47c4e8
259ac60
22a1123
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||||||||||||||
|
@@ -16,11 +13,18 @@ import { | |||||||||||||
getNodeUrlList, | ||||||||||||||
getRedisCache, | ||||||||||||||
paginatedEventQuery, | ||||||||||||||
retryAsync, | ||||||||||||||
CHAIN_IDs, | ||||||||||||||
} from "../../../utils"; | ||||||||||||||
import { HubPoolClient } from "../../../clients"; | ||||||||||||||
import { CONTRACT_ADDRESSES } from "../../../common"; | ||||||||||||||
import { Log } from "../../../interfaces"; | ||||||||||||||
|
||||||||||||||
// 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"; | ||||||||||||||
Comment on lines
+25
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the only location we need these imports? Could we make a patchfile to have these vars be exported? |
||||||||||||||
|
||||||||||||||
export type MessageWithStatus = Message & { | ||||||||||||||
logIndex: number; | ||||||||||||||
|
@@ -40,8 +44,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. | ||||||||||||||
|
@@ -51,18 +59,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 []; | ||||||||||||||
|
@@ -83,11 +89,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, | ||||||||||||||
|
@@ -96,6 +108,80 @@ export function makeGetMessagesWithStatusByTxHash( | |||||||||||||
}; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
// Temporary re-implementation of the SDK's `L1ClaimingService.getMessageStatus` functions that allow us to use | ||||||||||||||
// 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 new Contract( | ||||||||||||||
l1ClaimingService.l2Contract.contract.address, | ||||||||||||||
l1ClaimingService.l2Contract.contract.interface, | ||||||||||||||
l2Provider | ||||||||||||||
); | ||||||||||||||
Comment on lines
+141
to
+145
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this alternatively work?
Suggested change
|
||||||||||||||
} | ||||||||||||||
export function getL1MessageServiceContractFromL1ClaimingService( | ||||||||||||||
l1ClaimingService: L1ClaimingService, | ||||||||||||||
l1Provider: ethers.providers.Provider | ||||||||||||||
): Contract { | ||||||||||||||
return new Contract( | ||||||||||||||
l1ClaimingService.l1Contract.contract.address, | ||||||||||||||
l1ClaimingService.l1Contract.contract.interface, | ||||||||||||||
l1Provider | ||||||||||||||
); | ||||||||||||||
} | ||||||||||||||
Comment on lines
+147
to
+156
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||||||||||||||
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, | ||||||||||||||
|
@@ -188,13 +274,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[]>; | ||||||||||||||
} | ||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,17 +4,43 @@ 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"; | ||||||
|
||||||
// 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 { L2MessageServiceContract } from "@consensys/linea-sdk/dist/lib/contracts"; | ||||||
Comment on lines
+19
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wdyt about exporting these from a new file, so we can contain it and only have to document it there? i.e. |
||||||
|
||||||
// Temporary re-implementation of the SDK's `L2MessageServiceContract.getMessageStatus` functions that allow us to use | ||||||
// 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 = new Contract(messageService.contract.address, messageService.contract.interface, l2Provider); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also wondering if you can just connect the provider to the existing contract, but not sure whether it might be some weird type.
Suggested change
|
||||||
const status: BigNumber = await l2Contract.inboxL1L2MessageStatus(messageHash); | ||||||
switch (status.toString()) { | ||||||
case "0": | ||||||
return OnChainMessageStatus.UNKNOWN; | ||||||
case "1": | ||||||
return OnChainMessageStatus.CLAIMABLE; | ||||||
case "2": | ||||||
return OnChainMessageStatus.CLAIMED; | ||||||
} | ||||||
} | ||||||
|
||||||
export async function lineaL1ToL2Finalizer( | ||||||
logger: winston.Logger, | ||||||
signer: Signer, | ||||||
|
@@ -58,7 +84,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), | ||||||
]); | ||||||
|
@@ -73,9 +103,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, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did we want to floor this? Could be good for readability to at least
toFixed()
this output to keep the logs clean