@ -21,7 +21,12 @@ import {
StaticThresholdAddressSetFactory ,
TestIsm__factory ,
} from '@hyperlane-xyz/core' ;
import { Address , eqAddress , formatMessage , warn } from '@hyperlane-xyz/utils' ;
import {
Address ,
eqAddress ,
formatMessage ,
normalizeAddress ,
} from '@hyperlane-xyz/utils' ;
import { HyperlaneApp } from '../app/HyperlaneApp' ;
import {
@ -34,6 +39,7 @@ import {
ProxyFactoryFactories ,
proxyFactoryFactories ,
} from '../deploy/contracts' ;
import { logger } from '../logger' ;
import { MultiProvider } from '../providers/MultiProvider' ;
import { ChainMap , ChainName } from '../types' ;
@ -47,6 +53,7 @@ import {
MultisigIsmConfig ,
OpStackIsmConfig ,
RoutingIsmConfig ,
RoutingIsmDelta ,
ismTypeToModuleType ,
} from './types' ;
@ -89,8 +96,9 @@ export class HyperlaneIsmFactory extends HyperlaneApp<ProxyFactoryFactories> {
config : C ;
origin? : ChainName ;
mailbox? : Address ;
existingIsmAddress? : Address ;
} ) : Promise < DeployedIsm > {
const { destination , config , origin , mailbox } = params ;
const { destination , config , origin , mailbox , existingIsmAddress } = params ;
if ( typeof config === 'string' ) {
// @ts-ignore
return IInterchainSecurityModule__factory . connect (
@ -119,6 +127,7 @@ export class HyperlaneIsmFactory extends HyperlaneApp<ProxyFactoryFactories> {
config ,
origin ,
mailbox ,
existingIsmAddress ,
} ) ;
break ;
case IsmType . AGGREGATION :
@ -186,77 +195,151 @@ export class HyperlaneIsmFactory extends HyperlaneApp<ProxyFactoryFactories> {
config : RoutingIsmConfig ;
origin? : ChainName ;
mailbox? : Address ;
existingIsmAddress? : Address ;
} ) : Promise < IRoutingIsm > {
const { destination , config , mailbox } = params ;
const routingIsmFactory = this . getContracts ( destination ) . routingIsmFactory ;
const isms : ChainMap < Address > = { } ;
for ( const origin of Object . keys ( config . domains ) ) {
const ism = await this . deploy ( {
destination ,
config : config.domains [ origin ] ,
origin ,
mailbox ,
} ) ;
isms [ origin ] = ism . address ;
}
const domains = Object . keys ( isms ) . map ( ( chain ) = >
this . multiProvider . getDomainId ( chain ) ,
) ;
const submoduleAddresses = Object . values ( isms ) ;
const { destination , config , mailbox , existingIsmAddress } = params ;
const overrides = this . multiProvider . getTransactionOverrides ( destination ) ;
let receipt : ethers.providers.TransactionReceipt ;
const routingIsmFactory = this . getContracts ( destination ) . routingIsmFactory ;
let routingIsm : DomainRoutingIsm | DefaultFallbackRoutingIsm ;
if ( config . type === IsmType . FALLBACK_ROUTING ) {
if ( ! mailbox ) {
throw new Error (
'Mailbox address is required for deploying fallback routing ISM' ,
const delta : RoutingIsmDelta = existingIsmAddress
? await routingModuleDelta (
destination ,
existingIsmAddress ,
config ,
this . multiProvider ,
this . getContracts ( destination ) ,
mailbox ,
)
: {
domainsToUnenroll : [ ] ,
domainsToEnroll : Object.keys ( config . domains ) ,
} ;
const signer = this . multiProvider . getSigner ( destination ) ;
const provider = this . multiProvider . getProvider ( destination ) ;
let isOwner = false ;
if ( existingIsmAddress ) {
const owner = await DomainRoutingIsm__factory . connect (
existingIsmAddress ,
provider ,
) . owner ( ) ;
isOwner = eqAddress ( await signer . getAddress ( ) , owner ) ;
}
// reconfiguring existing routing ISM
if ( existingIsmAddress && isOwner && ! delta . mailbox ) {
const isms : ChainMap < Address > = { } ;
routingIsm = DomainRoutingIsm__factory . connect (
existingIsmAddress ,
this . multiProvider . getSigner ( destination ) ,
) ;
// deploying all the ISMs which have to be updated
for ( const origin of delta . domainsToEnroll ) {
logger (
` Reconfiguring preexisting routing ISM at for origin ${ origin } ... ` ,
) ;
const ism = await this . deploy ( {
destination ,
config : config.domains [ origin ] ,
origin ,
mailbox ,
} ) ;
isms [ origin ] = ism . address ;
const tx = await routingIsm . set (
this . multiProvider . getDomainId ( origin ) ,
isms [ origin ] ,
overrides ,
) ;
await this . multiProvider . handleTx ( destination , tx ) ;
}
debug ( 'Deploying fallback routing ISM ...' ) ;
routingIsm = await this . multiProvider . handleDeploy (
destination ,
new DefaultFallbackRoutingIsm__factory ( ) ,
[ mailbox ] ,
// unenrolling domains if needed
for ( const origin of delta . domainsToUnenroll ) {
logger (
` Unenrolling origin ${ origin } from preexisting routing ISM at ${ existingIsmAddress } ... ` ,
) ;
const tx = await routingIsm . remove (
this . multiProvider . getDomainId ( origin ) ,
overrides ,
) ;
await this . multiProvider . handleTx ( destination , tx ) ;
}
// transfer ownership if needed
if ( delta . owner ) {
logger ( ` Transferring ownership of routing ISM... ` ) ;
const tx = await routingIsm . transferOwnership ( delta . owner , overrides ) ;
await this . multiProvider . handleTx ( destination , tx ) ;
}
} else {
const isms : ChainMap < Address > = { } ;
for ( const origin of Object . keys ( config . domains ) ) {
const ism = await this . deploy ( {
destination ,
config : config.domains [ origin ] ,
origin ,
mailbox ,
} ) ;
isms [ origin ] = ism . address ;
}
const submoduleAddresses = Object . values ( isms ) ;
const domains = Object . keys ( isms ) . map ( ( chain ) = >
this . multiProvider . getDomainId ( chain ) ,
) ;
debug ( 'Initialising fallback routing ISM ...' ) ;
receipt = await this . multiProvider . handleTx (
destination ,
routingIsm [ 'initialize(address,uint32[],address[])' ] (
let receipt : ethers.providers.TransactionReceipt ;
if ( config . type === IsmType . FALLBACK_ROUTING ) {
// deploying new fallback routing ISM
if ( ! mailbox ) {
throw new Error (
'Mailbox address is required for deploying fallback routing ISM' ,
) ;
}
logger ( 'Deploying fallback routing ISM ...' ) ;
routingIsm = await this . multiProvider . handleDeploy (
destination ,
new DefaultFallbackRoutingIsm__factory ( ) ,
[ mailbox ] ,
) ;
logger ( 'Initialising fallback routing ISM ...' ) ;
receipt = await this . multiProvider . handleTx (
destination ,
routingIsm [ 'initialize(address,uint32[],address[])' ] (
config . owner ,
domains ,
submoduleAddresses ,
overrides ,
) ,
) ;
} else {
// deploying new domain routing ISM
const tx = await routingIsmFactory . deploy (
config . owner ,
domains ,
submoduleAddresses ,
overrides ,
) ,
) ;
} else {
const tx = await routingIsmFactory . deploy (
config . owner ,
domains ,
submoduleAddresses ,
overrides ,
) ;
receipt = await this . multiProvider . handleTx ( destination , tx ) ;
// TODO: Break this out into a generalized function
const dispatchLogs = receipt . logs
. map ( ( log ) = > {
try {
return routingIsmFactory . interface . parseLog ( log ) ;
} catch ( e ) {
return undefined ;
}
} )
. filter (
( log ) : log is ethers . utils . LogDescription = >
! ! log && log . name === 'ModuleDeployed' ,
) ;
const module Address = dispatchLogs [ 0 ] . args [ 'module' ] ;
routingIsm = DomainRoutingIsm__factory . connect (
module Address ,
this . multiProvider . getSigner ( destination ) ,
) ;
receipt = await this . multiProvider . handleTx ( destination , tx ) ;
// TODO: Break this out into a generalized function
const dispatchLogs = receipt . logs
. map ( ( log ) = > {
try {
return routingIsmFactory . interface . parseLog ( log ) ;
} catch ( e ) {
return undefined ;
}
} )
. filter (
( log ) : log is ethers . utils . LogDescription = >
! ! log && log . name === 'ModuleDeployed' ,
) ;
if ( dispatchLogs . length === 0 ) {
throw new Error ( 'No ModuleDeployed event found' ) ;
}
const module Address = dispatchLogs [ 0 ] . args [ 'module' ] ;
routingIsm = DomainRoutingIsm__factory . connect (
module Address ,
this . multiProvider . getSigner ( destination ) ,
) ;
}
}
return routingIsm ;
}
@ -410,7 +493,7 @@ export async function moduleCanCertainlyVerify(
throw new Error ( ` Unsupported module type: ${ module Type } ` ) ;
}
} catch ( e ) {
warn ( ` Error checking module ${ destModule } : ${ e } ` ) ;
logger ( ` Error checking module ${ destModule } : ${ e } ` ) ;
return false ;
}
} else {
@ -523,22 +606,20 @@ export async function moduleMatchesConfig(
mailbox !== undefined &&
eqAddress ( mailboxAddress , mailbox ) ;
}
// Recursively check that the submodule for each configured
// domain matches the submodule config.
for ( const [ origin , subConfig ] of Object . entries ( config . domains ) ) {
const subModule = await routingIsm . module (
multiProvider . getDomainId ( origin ) ,
) ;
const subModuleMatches = await module MatchesConfig (
chain ,
subModule ,
subConfig ,
multiProvider ,
contracts ,
mailbox ,
) ;
matches = matches && subModuleMatches ;
}
const delta = await routingModuleDelta (
chain ,
module Address ,
config ,
multiProvider ,
contracts ,
mailbox ,
) ;
matches =
matches &&
delta . domainsToEnroll . length === 0 &&
delta . domainsToUnenroll . length === 0 &&
! delta . mailbox &&
! delta . owner ;
break ;
}
case IsmType . AGGREGATION : {
@ -596,6 +677,62 @@ export async function moduleMatchesConfig(
return matches ;
}
export async function routingModuleDelta (
destination : ChainName ,
module Address : Address ,
config : RoutingIsmConfig ,
multiProvider : MultiProvider ,
contracts : HyperlaneContracts < ProxyFactoryFactories > ,
mailbox? : Address ,
) : Promise < RoutingIsmDelta > {
const provider = multiProvider . getProvider ( destination ) ;
const routingIsm = DomainRoutingIsm__factory . connect ( module Address , provider ) ;
const owner = await routingIsm . owner ( ) ;
const deployedDomains = ( await routingIsm . domains ( ) ) . map ( ( domain ) = >
multiProvider . getChainName ( domain . toNumber ( ) ) ,
) ;
const delta : RoutingIsmDelta = {
domainsToUnenroll : [ ] ,
domainsToEnroll : [ ] ,
} ;
// if owners don't match, we need to transfer ownership
if ( ! eqAddress ( owner , normalizeAddress ( config . owner ) ) )
delta . owner = config . owner ;
if ( config . type === IsmType . FALLBACK_ROUTING ) {
const client = MailboxClient__factory . connect ( module Address , provider ) ;
const mailboxAddress = await client . mailbox ( ) ;
if ( mailbox && ! eqAddress ( mailboxAddress , mailbox ) ) delta . mailbox = mailbox ;
}
// check for exclusion of domains in the config
delta . domainsToUnenroll = deployedDomains . filter (
( domain ) = > ! Object . keys ( config . domains ) . includes ( domain ) ,
) ;
// check for inclusion of domains in the config
for ( const [ origin , subConfig ] of Object . entries ( config . domains ) ) {
if ( ! deployedDomains . includes ( origin ) ) {
delta . domainsToEnroll . push ( origin ) ;
} else {
const subModule = await routingIsm . module (
multiProvider . getDomainId ( origin ) ,
) ;
// Recursively check that the submodule for each configured
// domain matches the submodule config.
const subModuleMatches = await module MatchesConfig (
destination ,
subModule ,
subConfig ,
multiProvider ,
contracts ,
origin ,
) ;
if ( ! subModuleMatches ) delta . domainsToEnroll . push ( origin ) ;
}
}
return delta ;
}
export function collectValidators (
origin : ChainName ,
config : IsmConfig ,