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

feat: run checkBytecodeHandle on proxy implementation contract in debugMessage #152

Merged
merged 5 commits into from
Dec 16, 2024
Merged
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
77 changes: 52 additions & 25 deletions src/features/debugger/debugMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,20 @@ import {
IMultisigIsm__factory as MultisigIsmFactory,
} from '@hyperlane-xyz/core';
import { IRegistry } from '@hyperlane-xyz/registry';
import { ChainMap, ChainMetadata, MAILBOX_VERSION, MultiProvider } from '@hyperlane-xyz/sdk';
import {
ChainMap,
ChainMetadata,
MAILBOX_VERSION,
MultiProvider,
isProxy,
proxyImplementation,
} from '@hyperlane-xyz/sdk';
import {
addressToBytes32,
errorToString,
formatMessage,
isValidAddress,
strip0x,
trimToLength,
} from '@hyperlane-xyz/utils';

Expand All @@ -29,7 +37,7 @@ import { GasPayment, IsmModuleTypes, MessageDebugResult, MessageDebugStatus } fr

type Provider = providers.Provider;

// const HANDLE_FUNCTION_SIG = 'handle(uint32,bytes32,bytes)';
const HANDLE_FUNCTION_SIG = 'handle(uint32,bytes32,bytes)';
const IGP_PAYMENT_CHECK_DELAY = 30_000; // 30 seconds

export async function debugMessage(
Expand Down Expand Up @@ -174,16 +182,6 @@ async function debugMessageDelivery(
const errorReason = extractReasonString(err);
logger.debug(errorReason);

// const bytecodeHasHandle = await tryCheckBytecodeHandle(destProvider, recipient);
// if (!bytecodeHasHandle) {
// logger.info('Bytecode does not have function matching handle sig');
// return {
// status: MessageDebugStatus.RecipientNotHandler,
// description: `Recipient contract should have handle function of signature: ${HANDLE_FUNCTION_SIG}. Check that recipient is not a proxy. Error: ${errorReason}`,
// calldataDetails,
// };
// }

if (debugIgnoredChains.includes(destName)) {
return {
status: null,
Expand All @@ -192,6 +190,23 @@ async function debugMessageDelivery(
};
}

const proxyImplementationContract = await tryGetProxyImplementationContract(
destProvider,
recipient,
);
const bytecodeHasHandle = await tryCheckBytecodeHandle(
destProvider,
proxyImplementationContract || recipient,
);
if (!bytecodeHasHandle) {
logger.info('Bytecode does not have function matching handle sig');
return {
status: MessageDebugStatus.RecipientNotHandler,
description: `Recipient contract should have handle function of signature: ${HANDLE_FUNCTION_SIG}. Error: ${errorReason}`,
calldataDetails,
};
}

const icaCallErr = await tryDebugIcaMsg(sender, recipient, body, originDomain, destProvider);
if (icaCallErr) {
return {
Expand Down Expand Up @@ -338,19 +353,31 @@ async function fetchGasPaymentEvents(provider: Provider, messageId: string) {
return { contractToPayments, contractToTotalGas, numPayments, numIGPs };
}

// async function tryCheckBytecodeHandle(provider: Provider, recipientAddress: string) {
// try {
// // scan bytecode for handle function selector
// const bytecode = await provider.getCode(recipientAddress);
// const msgRecipientInterface = MessageRecipientFactory.createInterface();
// const handleFunction = msgRecipientInterface.functions[HANDLE_FUNCTION_SIG];
// const handleSignature = msgRecipientInterface.getSighash(handleFunction);
// return bytecode.includes(strip0x(handleSignature));
// } catch (error) {
// logger.error('Error checking bytecode for handle fn', error);
// return true;
// }
// }
async function tryGetProxyImplementationContract(provider: Provider, recipientAddress: string) {
try {
const isProxyContract = await isProxy(provider, recipientAddress);
if (!isProxyContract) return undefined;

return await proxyImplementation(provider, recipientAddress);
} catch (error) {
logger.error('Error trying to check proxy contract', error);
return undefined;
}
}

async function tryCheckBytecodeHandle(provider: Provider, recipientAddress: string) {
try {
// scan bytecode for handle function selector
const bytecode = await provider.getCode(recipientAddress);
const msgRecipientInterface = MessageRecipientFactory.createInterface();
const handleFunction = msgRecipientInterface.functions[HANDLE_FUNCTION_SIG];
const handleSignature = msgRecipientInterface.getSighash(handleFunction);
return bytecode.includes(strip0x(handleSignature));
} catch (error) {
logger.error('Error checking bytecode for handle fn', error);
return true;
}
}

async function tryDebugIcaMsg(
sender: Address,
Expand Down
Loading