From 2ffb78f5c2e1091e89405c2ae69219a809424dfd Mon Sep 17 00:00:00 2001 From: Paul Balaji <10051819+paulbalaji@users.noreply.github.com> Date: Thu, 22 Aug 2024 17:00:40 +0100 Subject: [PATCH] feat: improve mailbox initialized check (#4371) resolves https://github.com/hyperlane-xyz/hyperlane-monorepo/issues/4233 - feat: improve mailbox initialized check - drive-by: update pinned registry + agent config --- .changeset/warm-grapes-talk.md | 5 + .registryrc | 2 +- .../sdk/src/core/HyperlaneCoreDeployer.ts | 101 +++++++++--------- 3 files changed, 55 insertions(+), 53 deletions(-) create mode 100644 .changeset/warm-grapes-talk.md diff --git a/.changeset/warm-grapes-talk.md b/.changeset/warm-grapes-talk.md new file mode 100644 index 000000000..75c996c8b --- /dev/null +++ b/.changeset/warm-grapes-talk.md @@ -0,0 +1,5 @@ +--- +'@hyperlane-xyz/sdk': patch +--- + +Improved check for mailbox initialization diff --git a/.registryrc b/.registryrc index a558b9b4b..fc93fc209 100644 --- a/.registryrc +++ b/.registryrc @@ -1 +1 @@ -06faae6fd81eec5584f830a18ae0355d7da63144 +488c6eb828e46821e3858275c1e6f53bc5721db3 diff --git a/typescript/sdk/src/core/HyperlaneCoreDeployer.ts b/typescript/sdk/src/core/HyperlaneCoreDeployer.ts index c17fe20d9..79d5456bd 100644 --- a/typescript/sdk/src/core/HyperlaneCoreDeployer.ts +++ b/typescript/sdk/src/core/HyperlaneCoreDeployer.ts @@ -5,7 +5,7 @@ import { TestRecipient, ValidatorAnnounce, } from '@hyperlane-xyz/core'; -import { Address, rootLogger } from '@hyperlane-xyz/utils'; +import { Address, isZeroishAddress, rootLogger } from '@hyperlane-xyz/utils'; import { HyperlaneContracts } from '../contracts/types.js'; import { HyperlaneDeployer } from '../deploy/HyperlaneDeployer.js'; @@ -107,63 +107,60 @@ export class HyperlaneCoreDeployer extends HyperlaneDeployer< hookAddresses, ); - // configure mailbox - try { + const txOverrides = this.multiProvider.getTransactionOverrides(chain); + + // Check if the mailbox has already been initialized + const currentDefaultIsm = await mailbox.defaultIsm(); + if (isZeroishAddress(currentDefaultIsm)) { + // If the default ISM is the zero address, the mailbox hasn't been initialized this.logger.debug('Initializing mailbox'); - await this.multiProvider.handleTx( - chain, - mailbox.initialize( - config.owner, - defaultIsm, - defaultHook.address, - requiredHook.address, - this.multiProvider.getTransactionOverrides(chain), - ), - ); - } catch (e: any) { - if ( - !e.message.includes('already initialized') && - // Some RPC providers dont return the revert reason (nor allow ethers to parse it), so we have to check the message - !e.message.includes('Reverted 0x08c379a') && - // Handle situation where the gas estimation fails on the call function, - // then the real error reason is not available in `e.message`, but rather in `e.error.reason` - !e.error?.reason?.includes('already initialized') && - // Some providers, like on Viction, return a generic error message for all revert reasons - !e.message.includes('always failing transaction') - ) { + try { + await this.multiProvider.handleTx( + chain, + mailbox.initialize( + config.owner, + defaultIsm, + defaultHook.address, + requiredHook.address, + txOverrides, + ), + ); + } catch (e: any) { + // If we still get an error here, it's likely a genuine error + this.logger.error('Failed to initialize mailbox:', e); throw e; } + } else { + // If the default ISM is not the zero address, the mailbox has likely been initialized + this.logger.debug('Mailbox appears to be already initialized'); + } - this.logger.debug('Mailbox already initialized'); - - const overrides = this.multiProvider.getTransactionOverrides(chain); - await this.configureHook( - chain, - mailbox, - defaultHook.address, - (_mailbox) => _mailbox.defaultHook(), - (_mailbox, _hook) => - _mailbox.populateTransaction.setDefaultHook(_hook, { ...overrides }), - ); + await this.configureHook( + chain, + mailbox, + defaultHook.address, + (_mailbox) => _mailbox.defaultHook(), + (_mailbox, _hook) => + _mailbox.populateTransaction.setDefaultHook(_hook, { ...txOverrides }), + ); - await this.configureHook( - chain, - mailbox, - requiredHook.address, - (_mailbox) => _mailbox.requiredHook(), - (_mailbox, _hook) => - _mailbox.populateTransaction.setRequiredHook(_hook, { ...overrides }), - ); + await this.configureHook( + chain, + mailbox, + requiredHook.address, + (_mailbox) => _mailbox.requiredHook(), + (_mailbox, _hook) => + _mailbox.populateTransaction.setRequiredHook(_hook, { ...txOverrides }), + ); - await this.configureIsm( - chain, - mailbox, - defaultIsm, - (_mailbox) => _mailbox.defaultIsm(), - (_mailbox, _module) => - _mailbox.populateTransaction.setDefaultIsm(_module), - ); - } + await this.configureIsm( + chain, + mailbox, + defaultIsm, + (_mailbox) => _mailbox.defaultIsm(), + (_mailbox, _module) => + _mailbox.populateTransaction.setDefaultIsm(_module), + ); return mailbox; }